aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2012-02-26 13:48:00 -0600
committerRob Landley <rob@landley.net>2012-02-26 13:48:00 -0600
commit2dd50adc460e63c57eddb696f908dd6b1abfd723 (patch)
treefe0a05588e4d8c2485cdba1113b213442693d92d /lib
parentbef57ed266cfad82114b5bb16052571035e9fff1 (diff)
downloadtoybox-2dd50adc460e63c57eddb696f908dd6b1abfd723.tar.gz
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Diffstat (limited to 'lib')
-rw-r--r--lib/lib.c51
-rw-r--r--lib/lib.h5
2 files changed, 56 insertions, 0 deletions
diff --git a/lib/lib.c b/lib/lib.c
index ba6d132d..3f04cb61 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -842,3 +842,54 @@ void for_each_pid_with_name_in(char **names, void (*callback)(pid_t pid))
closedir(dp);
}
+
+struct signame {
+ int num;
+ char *name;
+};
+
+// Signals required by POSIX 2008:
+// http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html
+
+#define SIGNIFY(x) {SIG##x, #x}
+
+static struct signame signames[] = {
+ SIGNIFY(ABRT), SIGNIFY(ALRM), SIGNIFY(BUS), SIGNIFY(CHLD), SIGNIFY(CONT),
+ SIGNIFY(FPE), SIGNIFY(HUP), SIGNIFY(ILL), SIGNIFY(INT), SIGNIFY(KILL),
+ SIGNIFY(PIPE), SIGNIFY(QUIT), SIGNIFY(SEGV), SIGNIFY(STOP), SIGNIFY(TERM),
+ SIGNIFY(TSTP), SIGNIFY(TTIN), SIGNIFY(TTOU), SIGNIFY(USR1), SIGNIFY(USR2),
+ SIGNIFY(SYS), SIGNIFY(TRAP), SIGNIFY(URG), SIGNIFY(VTALRM), SIGNIFY(XCPU),
+ SIGNIFY(XFSZ)
+};
+
+// not in posix: SIGNIFY(STKFLT), SIGNIFY(WINCH), SIGNIFY(IO), SIGNIFY(PWR)
+// obsolete: SIGNIFY(PROF) SIGNIFY(POLL)
+
+// Convert name to signal number. If name == NULL print names.
+int sig_to_num(char *pidstr)
+{
+ int i;
+
+ if (pidstr) {
+ char *s;
+ i = strtol(pidstr, &s, 10);
+ if (!*s) return i;
+
+ if (!strncasecmp(pidstr, "sig", 3)) pidstr+=3;
+ }
+ for (i = 0; i < sizeof(signames)/sizeof(struct signame); i++)
+ if (!pidstr) xputs(signames[i].name);
+ else if (!strcasecmp(pidstr, signames[i].name))
+ return signames[i].num;
+
+ return -1;
+}
+
+char *num_to_sig(int sig)
+{
+ int i;
+
+ for (i=0; i<sizeof(signames)/sizeof(struct signame); i++)
+ if (signames[i].num == sig) return signames[i].name;
+ return NULL;
+}
diff --git a/lib/lib.h b/lib/lib.h
index dd8a3f17..a6959730 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -126,3 +126,8 @@ struct mtab_list {
struct mtab_list *getmountlist(int die);
void bunzipStream(int src_fd, int dst_fd);
+
+// signal
+
+int sig_to_num(char *pidstr);
+char *num_to_sig(int sig);