6 #include "params.h" // for 'dangerous'
13 int find_pid_slot(pid_t mypid
)
18 if (shm
->pids
[i
] == mypid
)
21 return PIDSLOT_NOT_FOUND
;
24 bool pidmap_empty(void)
29 if (shm
->pids
[i
] != EMPTY_PIDSLOT
)
35 void dump_pid_slots(void)
37 unsigned int i
, j
= 0;
38 char string
[512], *sptr
= string
;
40 sptr
+= sprintf(sptr
, "## pids: (%d active)\n", shm
->running_childs
);
42 for (i
= 0; i
< shm
->max_children
; i
+=8) {
43 sptr
+= sprintf(sptr
, "%d-%d: ", i
, i
+7);
44 for (j
= 0; j
< 8; j
++) {
45 if (shm
->pids
[i
+j
] != EMPTY_PIDSLOT
) {
46 if (pid_alive(shm
->pids
[i
+j
] == -1))
50 sptr
+= sprintf(sptr
, "%d ", shm
->pids
[i
+j
]);
53 sptr
+= sprintf(sptr
, "\n");
56 outputerr("%s", string
);
61 static int read_pid_max(void)
68 fp
= fopen("/proc/sys/kernel/pid_max", "r");
75 if (!fgets(buf
, sizeof(buf
), fp
))
78 result
= strtoul(buf
, &end
, 10);
97 outputerr("Couldn't read pid_max from proc\n");
100 output(0, "Using pid_max = %d\n", pidmax
);
103 int pid_is_valid(pid_t pid
)
105 if ((pid
> pidmax
) || (pid
< 1)) {
106 output(0, "Sanity check failed! Found pid %d!\n", pid
);
113 unsigned int get_pid(void)
118 /* If we get called from the parent, and there are no
119 * children around yet, we need to not look at the pidmap. */
120 if (shm
->running_childs
== 0)
123 switch (rand() % 3) {
125 retry
: i
= rand() % shm
->max_children
;
127 if (pid
== EMPTY_PIDSLOT
)
134 case 2: if (dangerous
== FALSE
) // We don't want root trying to kill init.