1 // Try to print the cmdlines of all process spawns by polling /proc.
2 // Doesn't do exactly what you probably need because it might catch
3 // a process between fork and exec, then ignore it.
13 static unsigned int active[65536];
15 // assumes that *str can't be empty
16 static int str_to_int(char *str) {
19 if (*str < '0' || *str > '9') return -1;
22 if (*str == '\0') return res;
27 int main(int argc, char *argv[]) {
28 for (int i=0; i<65536; i++) {
33 DIR *dir = opendir(".");
34 unsigned int cycle = 2, lastcycle;
39 while ((dent = readdir(dir)) != NULL) {
40 int name_id = str_to_int(dent->d_name);
41 if (name_id < 0 || name_id > 65535) continue;
42 if (active[name_id] != lastcycle) {
43 sprintf(path, "%s/cmdline", dent->d_name);
44 int fd = open(path, O_RDONLY);
47 int cmdline_len = read(fd, cmdline, 65536);
48 if (cmdline_len != -1) {
49 write(1, cmdline, cmdline_len);
55 active[name_id] = cycle;