Mercurial > hg > toybox
annotate lib/pending.c @ 1057:242c5de2bb22 draft
Replace for_each_pid_with_name_in_array_perform_callback_function_upon_translated_value() with name_to_pid(), comparing absolute paths or just basename() consistently as spotted by Lukasz Skalski, and adjust callers.
author | Rob Landley <rob@landley.net> |
---|---|
date | Tue, 03 Sep 2013 18:43:32 -0500 |
parents | f8824260d057 |
children | 4263a4390758 |
rev | line source |
---|---|
949
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
1 /* pending.c - reusable stuff awaiting review |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
2 * |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
3 * new lib entries for stuff in toys/pending |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
4 */ |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
5 |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
6 #include "toys.h" |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
7 |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
8 // Execute a callback for each PID that matches a process name from a list. |
1057
242c5de2bb22
Replace for_each_pid_with_name_in_array_perform_callback_function_upon_translated_value() with name_to_pid(), comparing absolute paths or just basename() consistently as spotted by Lukasz Skalski, and adjust callers.
Rob Landley <rob@landley.net>
parents:
1055
diff
changeset
|
9 void name_to_pid(char **names, int (*callback)(pid_t pid, char *name)) |
949
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
10 { |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
11 DIR *dp; |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
12 struct dirent *entry; |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
13 |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
14 if (!(dp = opendir("/proc"))) perror_exit("opendir"); |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
15 |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
16 while ((entry = readdir(dp))) { |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
17 int fd, n; |
1057
242c5de2bb22
Replace for_each_pid_with_name_in_array_perform_callback_function_upon_translated_value() with name_to_pid(), comparing absolute paths or just basename() consistently as spotted by Lukasz Skalski, and adjust callers.
Rob Landley <rob@landley.net>
parents:
1055
diff
changeset
|
18 unsigned u; |
242c5de2bb22
Replace for_each_pid_with_name_in_array_perform_callback_function_upon_translated_value() with name_to_pid(), comparing absolute paths or just basename() consistently as spotted by Lukasz Skalski, and adjust callers.
Rob Landley <rob@landley.net>
parents:
1055
diff
changeset
|
19 char *cmd, **curname; |
949
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
20 |
1057
242c5de2bb22
Replace for_each_pid_with_name_in_array_perform_callback_function_upon_translated_value() with name_to_pid(), comparing absolute paths or just basename() consistently as spotted by Lukasz Skalski, and adjust callers.
Rob Landley <rob@landley.net>
parents:
1055
diff
changeset
|
21 if (!(u = atoi(entry->d_name))) continue; |
242c5de2bb22
Replace for_each_pid_with_name_in_array_perform_callback_function_upon_translated_value() with name_to_pid(), comparing absolute paths or just basename() consistently as spotted by Lukasz Skalski, and adjust callers.
Rob Landley <rob@landley.net>
parents:
1055
diff
changeset
|
22 sprintf(libbuf, "/proc/%u/cmdline", u); |
242c5de2bb22
Replace for_each_pid_with_name_in_array_perform_callback_function_upon_translated_value() with name_to_pid(), comparing absolute paths or just basename() consistently as spotted by Lukasz Skalski, and adjust callers.
Rob Landley <rob@landley.net>
parents:
1055
diff
changeset
|
23 if (!(cmd = readfile(libbuf, libbuf, sizeof(libbuf)))) continue; |
949
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
24 |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
25 for (curname = names; *curname; curname++) |
1057
242c5de2bb22
Replace for_each_pid_with_name_in_array_perform_callback_function_upon_translated_value() with name_to_pid(), comparing absolute paths or just basename() consistently as spotted by Lukasz Skalski, and adjust callers.
Rob Landley <rob@landley.net>
parents:
1055
diff
changeset
|
26 if (*curname == '/' ? !strcmp(cmd, *curname) |
242c5de2bb22
Replace for_each_pid_with_name_in_array_perform_callback_function_upon_translated_value() with name_to_pid(), comparing absolute paths or just basename() consistently as spotted by Lukasz Skalski, and adjust callers.
Rob Landley <rob@landley.net>
parents:
1055
diff
changeset
|
27 : !strcmp(basename(cmd), basename(*curname)) |
242c5de2bb22
Replace for_each_pid_with_name_in_array_perform_callback_function_upon_translated_value() with name_to_pid(), comparing absolute paths or just basename() consistently as spotted by Lukasz Skalski, and adjust callers.
Rob Landley <rob@landley.net>
parents:
1055
diff
changeset
|
28 if (!callback(u, *curname)) break; |
242c5de2bb22
Replace for_each_pid_with_name_in_array_perform_callback_function_upon_translated_value() with name_to_pid(), comparing absolute paths or just basename() consistently as spotted by Lukasz Skalski, and adjust callers.
Rob Landley <rob@landley.net>
parents:
1055
diff
changeset
|
29 if (*curname) break; |
949
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
30 } |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
31 closedir(dp); |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
32 } |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
33 |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
34 /* |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
35 * used to get the interger value. |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
36 */ |
1055
f8824260d057
Ashwini Sharma submitted route.c, adding it to pending.
Rob Landley <rob@landley.net>
parents:
1011
diff
changeset
|
37 unsigned long get_int_value(const char *numstr, unsigned long lowrange, unsigned long highrange) |
949
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
38 { |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
39 unsigned long rvalue = 0; |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
40 char *ptr; |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
41 if(*numstr == '-' || *numstr == '+' || isspace(*numstr)) perror_exit("invalid number '%s'", numstr); |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
42 errno = 0; |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
43 rvalue = strtoul(numstr, &ptr, 10); |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
44 if(errno || numstr == ptr) perror_exit("invalid number '%s'", numstr); |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
45 if(*ptr) perror_exit("invalid number '%s'", numstr); |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
46 if(rvalue >= lowrange && rvalue <= highrange) return rvalue; |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
47 else { |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
48 perror_exit("invalid number '%s'", numstr); |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
49 return rvalue; //Not reachable; to avoid waring message. |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
50 } |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
51 } |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
52 |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
53 /* |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
54 * strcat to mallocated buffer |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
55 * reallocate if need be |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
56 */ |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
57 char *astrcat (char *x, char *y) { |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
58 char *z; |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
59 z = x; |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
60 x = realloc (x, (x ? strlen (x) : 0) + strlen (y) + 1); |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
61 if (!x) return 0; |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
62 (z ? strcat : strcpy) (x, y); |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
63 return x; |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
64 } |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
65 |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
66 /* |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
67 * astrcat, but die on failure |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
68 */ |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
69 char *xastrcat (char *x, char *y) { |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
70 x = astrcat (x, y); |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
71 if (!x) error_exit ("xastrcat"); |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
72 return x; |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
73 } |
995
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
74 |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
75 void daemonize(void) |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
76 { |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
77 int fd = open("/dev/null", O_RDWR); |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
78 if (fd < 0) fd = xcreate("/", O_RDONLY, 0666); |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
79 |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
80 pid_t pid = fork(); |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
81 if (pid < 0) perror_exit("DAEMON: failed to fork"); |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
82 if (pid) exit(EXIT_SUCCESS); |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
83 |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
84 setsid(); |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
85 dup2(fd, 0); |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
86 dup2(fd, 1); |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
87 dup2(fd, 2); |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
88 if (fd > 2) close(fd); |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
89 } |