Mercurial > hg > toybox
annotate lib/pending.c @ 1011:6cb9e7142574
Remove files du no longer needs.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sun, 18 Aug 2013 04:12:51 -0500 |
parents | 893c86bbe452 |
children | f8824260d057 |
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. |
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
|
9 void for_each_pid_with_name_in(char **names, int (*callback)(pid_t pid, char *name)) |
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 char cmd[sizeof(toybuf)], path[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
|
14 char **curname; |
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 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
|
17 |
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
|
18 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
|
19 int fd, n; |
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 |
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
|
21 if (!isdigit(*entry->d_name)) continue; |
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
|
22 |
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 if (sizeof(path) <= snprintf(path, sizeof(path), "/proc/%s/cmdline", |
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 entry->d_name)) continue; |
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 |
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
|
26 if (-1 == (fd=open(path, O_RDONLY))) continue; |
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
|
27 n = read(fd, cmd, sizeof(cmd)); |
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
|
28 close(fd); |
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 if (n<1) continue; |
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 for (curname = names; *curname; curname++) |
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 if (!strcmp(basename(cmd), *curname)) |
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 if (!callback(atol(entry->d_name), *curname)) goto done; |
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 done: |
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 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
|
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 |
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 /* |
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 * 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
|
41 */ |
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 unsigned long get_int_value(const char *numstr, unsigned lowrange, unsigned highrange) |
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 { |
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 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
|
45 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
|
46 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
|
47 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
|
48 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
|
49 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
|
50 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
|
51 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
|
52 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
|
53 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
|
54 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
|
55 } |
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 |
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 /* |
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 * 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
|
60 * 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
|
61 */ |
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 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
|
63 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
|
64 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
|
65 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
|
66 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
|
67 (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
|
68 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
|
69 } |
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 |
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 /* |
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 * 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
|
73 */ |
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
|
74 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
|
75 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
|
76 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
|
77 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
|
78 } |
995
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 void daemonize(void) |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
81 { |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
82 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
|
83 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
|
84 |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
85 pid_t pid = fork(); |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
86 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
|
87 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
|
88 |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
89 setsid(); |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
90 dup2(fd, 0); |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
91 dup2(fd, 1); |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
92 dup2(fd, 2); |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
93 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
|
94 } |