Rob Landley | 87aef24 | 2013-07-14 22:12:22 -0500 | [diff] [blame] | 1 | /* pending.c - reusable stuff awaiting review |
| 2 | * |
| 3 | * new lib entries for stuff in toys/pending |
| 4 | */ |
| 5 | |
| 6 | #include "toys.h" |
| 7 | |
| 8 | // Execute a callback for each PID that matches a process name from a list. |
Rob Landley | cd0b70e | 2013-09-11 12:09:53 -0500 | [diff] [blame] | 9 | void names_to_pid(char **names, int (*callback)(pid_t pid, char *name)) |
Rob Landley | 87aef24 | 2013-07-14 22:12:22 -0500 | [diff] [blame] | 10 | { |
| 11 | DIR *dp; |
| 12 | struct dirent *entry; |
Rob Landley | 87aef24 | 2013-07-14 22:12:22 -0500 | [diff] [blame] | 13 | |
| 14 | if (!(dp = opendir("/proc"))) perror_exit("opendir"); |
| 15 | |
| 16 | while ((entry = readdir(dp))) { |
Rob Landley | 7d64dae | 2013-09-03 18:43:32 -0500 | [diff] [blame] | 17 | unsigned u; |
| 18 | char *cmd, **curname; |
Rob Landley | 87aef24 | 2013-07-14 22:12:22 -0500 | [diff] [blame] | 19 | |
Rob Landley | 7d64dae | 2013-09-03 18:43:32 -0500 | [diff] [blame] | 20 | if (!(u = atoi(entry->d_name))) continue; |
| 21 | sprintf(libbuf, "/proc/%u/cmdline", u); |
| 22 | if (!(cmd = readfile(libbuf, libbuf, sizeof(libbuf)))) continue; |
Rob Landley | 87aef24 | 2013-07-14 22:12:22 -0500 | [diff] [blame] | 23 | |
| 24 | for (curname = names; *curname; curname++) |
Rob Landley | cd0b70e | 2013-09-11 12:09:53 -0500 | [diff] [blame] | 25 | if (**curname == '/' ? !strcmp(cmd, *curname) |
| 26 | : !strcmp(basename(cmd), basename(*curname))) |
| 27 | if (callback(u, *curname)) break; |
Rob Landley | 7d64dae | 2013-09-03 18:43:32 -0500 | [diff] [blame] | 28 | if (*curname) break; |
Rob Landley | 87aef24 | 2013-07-14 22:12:22 -0500 | [diff] [blame] | 29 | } |
Rob Landley | 87aef24 | 2013-07-14 22:12:22 -0500 | [diff] [blame] | 30 | closedir(dp); |
| 31 | } |
| 32 | |
Rob Landley | 87aef24 | 2013-07-14 22:12:22 -0500 | [diff] [blame] | 33 | /* |
| 34 | * used to get the interger value. |
| 35 | */ |
Rob Landley | 12c8814 | 2013-09-06 12:21:28 -0500 | [diff] [blame] | 36 | unsigned long get_int_value(const char *numstr, unsigned long lowrange, unsigned long highrange) |
Rob Landley | 87aef24 | 2013-07-14 22:12:22 -0500 | [diff] [blame] | 37 | { |
| 38 | unsigned long rvalue = 0; |
| 39 | char *ptr; |
Rob Landley | e3e8084 | 2013-09-10 01:01:35 -0500 | [diff] [blame] | 40 | |
| 41 | if (!isdigit(*numstr)) perror_exit("bad number '%s'", numstr); |
Rob Landley | 87aef24 | 2013-07-14 22:12:22 -0500 | [diff] [blame] | 42 | errno = 0; |
| 43 | rvalue = strtoul(numstr, &ptr, 10); |
Rob Landley | 87aef24 | 2013-07-14 22:12:22 -0500 | [diff] [blame] | 44 | |
Rob Landley | e3e8084 | 2013-09-10 01:01:35 -0500 | [diff] [blame] | 45 | if (errno || numstr == ptr || *ptr || rvalue < lowrange || rvalue > highrange) |
| 46 | perror_exit("bad number '%s'", numstr); |
Rob Landley | 87aef24 | 2013-07-14 22:12:22 -0500 | [diff] [blame] | 47 | |
Rob Landley | e3e8084 | 2013-09-10 01:01:35 -0500 | [diff] [blame] | 48 | return rvalue; |
Rob Landley | 87aef24 | 2013-07-14 22:12:22 -0500 | [diff] [blame] | 49 | } |
Felix Janda | e49fe14 | 2013-08-10 20:18:18 +0200 | [diff] [blame] | 50 | |
| 51 | void daemonize(void) |
| 52 | { |
| 53 | int fd = open("/dev/null", O_RDWR); |
| 54 | if (fd < 0) fd = xcreate("/", O_RDONLY, 0666); |
| 55 | |
| 56 | pid_t pid = fork(); |
| 57 | if (pid < 0) perror_exit("DAEMON: failed to fork"); |
| 58 | if (pid) exit(EXIT_SUCCESS); |
| 59 | |
| 60 | setsid(); |
| 61 | dup2(fd, 0); |
| 62 | dup2(fd, 1); |
| 63 | dup2(fd, 2); |
| 64 | if (fd > 2) close(fd); |
| 65 | } |
Rob Landley | 35b40be | 2013-11-10 18:23:19 -0600 | [diff] [blame] | 66 | |
| 67 | char *human_readable(unsigned long long size) |
| 68 | { |
| 69 | static char buf[32]; |
| 70 | char *tmp = (buf+4); //unsigned long long can come in 20byte string. |
| 71 | int index, sz; |
| 72 | |
| 73 | for (index = 0; 1024 < size>>(10*index); index++); |
| 74 | sz = size>>(10*index); |
| 75 | if (sz < 10 && index) { |
| 76 | sprintf(tmp, "%llu", size>>(10*(index-1))); |
| 77 | sprintf(buf, "%c.%c", tmp[0], tmp[1]); |
| 78 | } else sprintf(buf, "%u", sz); |
| 79 | sprintf(buf, "%s%c", buf, " KMGTPE"[index]); |
| 80 | return buf; |
| 81 | } |