Mercurial > hg > toybox
annotate lib/pending.c @ 1101:ccf4193167c3 draft
Make the patch -x option (only enabled with CONFIG_DEBUG) provide more information about why a patch didn't apply. (Offset of first nonmatching character at each line during seek phase.)
author | Rob Landley <rob@landley.net> |
---|---|
date | Thu, 31 Oct 2013 09:36:55 -0500 |
parents | 7ada6da9540a |
children | b2a8f64a564b |
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. |
1068
7ada6da9540a
Ah, that's why commit 1057 was skipped last pull: it was unfinished. Oops. (Fix it.)
Rob Landley <rob@landley.net>
parents:
1066
diff
changeset
|
9 void names_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))) { |
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
|
17 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
|
18 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
|
19 |
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
|
20 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
|
21 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
|
22 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
|
23 |
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 for (curname = names; *curname; curname++) |
1068
7ada6da9540a
Ah, that's why commit 1057 was skipped last pull: it was unfinished. Oops. (Fix it.)
Rob Landley <rob@landley.net>
parents:
1066
diff
changeset
|
25 if (**curname == '/' ? !strcmp(cmd, *curname) |
7ada6da9540a
Ah, that's why commit 1057 was skipped last pull: it was unfinished. Oops. (Fix it.)
Rob Landley <rob@landley.net>
parents:
1066
diff
changeset
|
26 : !strcmp(basename(cmd), basename(*curname))) |
7ada6da9540a
Ah, that's why commit 1057 was skipped last pull: it was unfinished. Oops. (Fix it.)
Rob Landley <rob@landley.net>
parents:
1066
diff
changeset
|
27 if (callback(u, *curname)) break; |
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
|
28 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
|
29 } |
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 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
|
31 } |
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 * 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
|
35 */ |
1055
f8824260d057
Ashwini Sharma submitted route.c, adding it to pending.
Rob Landley <rob@landley.net>
parents:
1011
diff
changeset
|
36 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
|
37 { |
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 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
|
39 char *ptr; |
1066
4263a4390758
Remove two unused functions and shrink another.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
40 |
4263a4390758
Remove two unused functions and shrink another.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
41 if (!isdigit(*numstr)) perror_exit("bad number '%s'", numstr); |
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
|
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 |
1066
4263a4390758
Remove two unused functions and shrink another.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
45 if (errno || numstr == ptr || *ptr || rvalue < lowrange || rvalue > highrange) |
4263a4390758
Remove two unused functions and shrink another.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
46 perror_exit("bad number '%s'", numstr); |
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
|
47 |
1066
4263a4390758
Remove two unused functions and shrink another.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
48 return rvalue; |
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
|
49 } |
995
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
50 |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
51 void daemonize(void) |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
52 { |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
53 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
|
54 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
|
55 |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
56 pid_t pid = fork(); |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
57 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
|
58 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
|
59 |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
60 setsid(); |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
61 dup2(fd, 0); |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
62 dup2(fd, 1); |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
63 dup2(fd, 2); |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
64 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
|
65 } |