CLEANUP: global: introduce variable pid_bit to avoid shifts with relative_pid
At a number of places, bitmasks are used for process affinity and to map
listeners to processes. Every time 1UL<<(relative_pid-1) is used. Let's
create a "pid_bit" variable corresponding to this value to clean this up.
diff --git a/include/types/global.h b/include/types/global.h
index b3aa29c..48b6f9d 100644
--- a/include/types/global.h
+++ b/include/types/global.h
@@ -176,6 +176,7 @@
extern struct global global;
extern int pid; /* current process id */
extern int relative_pid; /* process id starting at 1 */
+extern unsigned long pid_bit; /* bit corresponding to the process id */
extern int actconn; /* # of active sessions */
extern int listeners;
extern int jobs; /* # of active jobs (listeners, sessions, open devices) */
diff --git a/src/haproxy.c b/src/haproxy.c
index 8993b89..a22ed32 100644
--- a/src/haproxy.c
+++ b/src/haproxy.c
@@ -118,6 +118,7 @@
static struct list cfg_cfgfiles = LIST_HEAD_INIT(cfg_cfgfiles);
int pid; /* current process id */
int relative_pid = 1; /* process id starting at 1 */
+unsigned long pid_bit = 1; /* bit corresponding to the process id */
/* global options */
struct global global = {
@@ -2655,6 +2656,7 @@
shut_your_big_mouth_gcc(write(pidfd, pidstr, strlen(pidstr)));
}
relative_pid++; /* each child will get a different one */
+ pid_bit <<= 1;
}
#ifdef USE_CPU_AFFINITY
diff --git a/src/listener.c b/src/listener.c
index 2ac25fd..a6bd449 100644
--- a/src/listener.c
+++ b/src/listener.c
@@ -64,7 +64,7 @@
if (listener->state == LI_LISTEN) {
if ((global.mode & (MODE_DAEMON | MODE_MWORKER)) &&
listener->bind_conf->bind_proc &&
- !(listener->bind_conf->bind_proc & (1UL << (relative_pid - 1)))) {
+ !(listener->bind_conf->bind_proc & pid_bit)) {
/* we don't want to enable this listener and don't
* want any fd event to reach it.
*/
@@ -168,7 +168,7 @@
if ((global.mode & (MODE_DAEMON | MODE_MWORKER)) &&
l->bind_conf->bind_proc &&
- !(l->bind_conf->bind_proc & (1UL << (relative_pid - 1))))
+ !(l->bind_conf->bind_proc & pid_bit))
goto end;
if (l->state == LI_ASSIGNED) {
diff --git a/src/proxy.c b/src/proxy.c
index 3af01ef..ccbc7b2 100644
--- a/src/proxy.c
+++ b/src/proxy.c
@@ -1431,7 +1431,7 @@
int bk_f_forced_id, srv_f_forced_id;
/* we don't want to report any state if the backend is not enabled on this process */
- if (px->bind_proc && !(px->bind_proc & (1UL << (relative_pid - 1))))
+ if (px->bind_proc && !(px->bind_proc & pid_bit))
return 1;
if (!appctx->ctx.cli.p1)
@@ -1546,7 +1546,7 @@
continue;
/* we don't want to list a backend which is bound to this process */
- if (curproxy->bind_proc && !(curproxy->bind_proc & (1UL << (relative_pid - 1))))
+ if (curproxy->bind_proc && !(curproxy->bind_proc & pid_bit))
continue;
chunk_appendf(&trash, "%s\n", curproxy->id);