annotate lib/pending.c @ 1066:4263a4390758 draft

Remove two unused functions and shrink another.
author Rob Landley <rob@landley.net>
date Tue, 10 Sep 2013 01:01:35 -0500
parents 242c5de2bb22
children 7ada6da9540a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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;
1066
4263a4390758 Remove two unused functions and shrink another.
Rob Landley <rob@landley.net>
parents: 1057
diff changeset
41
4263a4390758 Remove two unused functions and shrink another.
Rob Landley <rob@landley.net>
parents: 1057
diff changeset
42 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
43 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
44 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
45
1066
4263a4390758 Remove two unused functions and shrink another.
Rob Landley <rob@landley.net>
parents: 1057
diff changeset
46 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
47 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
48
1066
4263a4390758 Remove two unused functions and shrink another.
Rob Landley <rob@landley.net>
parents: 1057
diff changeset
49 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
50 }
995
893c86bbe452 Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents: 949
diff changeset
51
893c86bbe452 Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents: 949
diff changeset
52 void daemonize(void)
893c86bbe452 Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents: 949
diff changeset
53 {
893c86bbe452 Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents: 949
diff changeset
54 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
55 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
56
893c86bbe452 Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents: 949
diff changeset
57 pid_t pid = fork();
893c86bbe452 Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents: 949
diff changeset
58 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
59 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
60
893c86bbe452 Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents: 949
diff changeset
61 setsid();
893c86bbe452 Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents: 949
diff changeset
62 dup2(fd, 0);
893c86bbe452 Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents: 949
diff changeset
63 dup2(fd, 1);
893c86bbe452 Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents: 949
diff changeset
64 dup2(fd, 2);
893c86bbe452 Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents: 949
diff changeset
65 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
66 }