blob: a804d5b8ffcfcd9d7715b9da79e38092b49a2332 [file] [log] [blame]
Rob Landley87aef242013-07-14 22:12:22 -05001/* 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 Landleycd0b70e2013-09-11 12:09:53 -05009void names_to_pid(char **names, int (*callback)(pid_t pid, char *name))
Rob Landley87aef242013-07-14 22:12:22 -050010{
11 DIR *dp;
12 struct dirent *entry;
Rob Landley87aef242013-07-14 22:12:22 -050013
14 if (!(dp = opendir("/proc"))) perror_exit("opendir");
15
16 while ((entry = readdir(dp))) {
Rob Landley7d64dae2013-09-03 18:43:32 -050017 unsigned u;
18 char *cmd, **curname;
Rob Landley87aef242013-07-14 22:12:22 -050019
Rob Landley7d64dae2013-09-03 18:43:32 -050020 if (!(u = atoi(entry->d_name))) continue;
21 sprintf(libbuf, "/proc/%u/cmdline", u);
22 if (!(cmd = readfile(libbuf, libbuf, sizeof(libbuf)))) continue;
Rob Landley87aef242013-07-14 22:12:22 -050023
24 for (curname = names; *curname; curname++)
Rob Landleycd0b70e2013-09-11 12:09:53 -050025 if (**curname == '/' ? !strcmp(cmd, *curname)
26 : !strcmp(basename(cmd), basename(*curname)))
27 if (callback(u, *curname)) break;
Rob Landley7d64dae2013-09-03 18:43:32 -050028 if (*curname) break;
Rob Landley87aef242013-07-14 22:12:22 -050029 }
Rob Landley87aef242013-07-14 22:12:22 -050030 closedir(dp);
31}
32
Rob Landley87aef242013-07-14 22:12:22 -050033/*
34 * used to get the interger value.
35 */
Rob Landley12c88142013-09-06 12:21:28 -050036unsigned long get_int_value(const char *numstr, unsigned long lowrange, unsigned long highrange)
Rob Landley87aef242013-07-14 22:12:22 -050037{
38 unsigned long rvalue = 0;
39 char *ptr;
Rob Landleye3e80842013-09-10 01:01:35 -050040
41 if (!isdigit(*numstr)) perror_exit("bad number '%s'", numstr);
Rob Landley87aef242013-07-14 22:12:22 -050042 errno = 0;
43 rvalue = strtoul(numstr, &ptr, 10);
Rob Landley87aef242013-07-14 22:12:22 -050044
Rob Landleye3e80842013-09-10 01:01:35 -050045 if (errno || numstr == ptr || *ptr || rvalue < lowrange || rvalue > highrange)
46 perror_exit("bad number '%s'", numstr);
Rob Landley87aef242013-07-14 22:12:22 -050047
Rob Landleye3e80842013-09-10 01:01:35 -050048 return rvalue;
Rob Landley87aef242013-07-14 22:12:22 -050049}
Felix Jandae49fe142013-08-10 20:18:18 +020050
51void 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 Landley35b40be2013-11-10 18:23:19 -060066
67char *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}