6 #include "params.h" // for 'dangerous'
14 int find_pid_slot(pid_t mypid
)
19 if (shm
->pids
[i
] == mypid
)
22 return PIDSLOT_NOT_FOUND
;
25 bool pidmap_empty(void)
30 if (shm
->pids
[i
] != EMPTY_PIDSLOT
)
36 void dump_pid_slots(void)
38 unsigned int i
, j
= 0;
39 char string
[512], *sptr
= string
;
41 sptr
+= sprintf(sptr
, "## pids: (%u active)\n", shm
->running_childs
);
43 for (i
= 0; i
< max_children
; i
+= 8) {
44 sptr
+= sprintf(sptr
, "%u-%u: ", i
, i
+7);
45 for (j
= 0; j
< 8; j
++) {
46 if (shm
->pids
[i
+j
] != EMPTY_PIDSLOT
) {
47 if (pid_alive(shm
->pids
[i
+j
] == -1))
51 sptr
+= sprintf(sptr
, "%d ", shm
->pids
[i
+j
]);
54 sptr
+= sprintf(sptr
, "\n");
57 outputerr("%s", string
);
62 static int read_pid_max(void)
69 fp
= fopen("/proc/sys/kernel/pid_max", "r");
76 if (!fgets(buf
, sizeof(buf
), fp
))
79 result
= strtoul(buf
, &end
, 10);
98 outputerr("Couldn't read pid_max from proc\n");
101 output(0, "Using pid_max = %d\n", pidmax
);
104 int pid_is_valid(pid_t pid
)
106 if ((pid
> pidmax
) || (pid
< 1)) {
107 output(0, "Sanity check failed! Found pid %d!\n", pid
);
114 unsigned int get_pid(void)
119 /* If we get called from the parent, and there are no
120 * children around yet, we need to not look at the pidmap. */
121 if (shm
->running_childs
== 0)
124 switch (rand() % 3) {
126 retry
: i
= rand() % max_children
;
128 if (pid
== EMPTY_PIDSLOT
)
135 case 2: if (dangerous
== FALSE
) // We don't want root trying to kill init.