From 67bcb173c4c24977f6690a364c514285856a913d Mon Sep 17 00:00:00 2001 From: Kevin Lange Date: Tue, 3 Jan 2017 15:51:22 +0900 Subject: [PATCH] Support kill by signal name --- userspace/core/kill.c | 79 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 75 insertions(+), 4 deletions(-) diff --git a/userspace/core/kill.c b/userspace/core/kill.c index 9f532288..175a7560 100644 --- a/userspace/core/kill.c +++ b/userspace/core/kill.c @@ -1,8 +1,7 @@ /* This file is part of ToaruOS and is released under the terms * of the NCSA / University of Illinois License - see LICENSE.md - * Copyright (C) 2013 Kevin Lange - */ -/* + * Copyright (C) 2013-2017 Kevin Lange + * * kill * * Send a signal to another process @@ -13,6 +12,51 @@ #include #include +struct sig_def { + int sig; + const char * name; +}; + +struct sig_def signals[] = { + {SIGHUP,"HUP"}, + {SIGINT,"INT"}, + {SIGQUIT,"QUIT"}, + {SIGILL,"ILL"}, + {SIGTRAP,"TRAP"}, + {SIGABRT,"ABRT"}, + {SIGEMT,"EMT"}, + {SIGFPE,"FPE"}, + {SIGKILL,"KILL"}, + {SIGBUS,"BUS"}, + {SIGSEGV,"SEGV"}, + {SIGSYS,"SYS"}, + {SIGPIPE,"PIPE"}, + {SIGALRM,"ALRM"}, + {SIGTERM,"TERM"}, + {SIGUSR1,"USR1"}, + {SIGUSR2,"USR2"}, + {SIGCHLD,"CHLD"}, + {SIGPWR,"PWR"}, + {SIGWINCH,"WINCH"}, + {SIGURG,"URG"}, + {SIGPOLL,"POLL"}, + {SIGSTOP,"STOP"}, + {SIGTSTP,"TSTP"}, + {SIGCONT,"CONT"}, + {SIGTTIN,"TTIN"}, + {SIGTTOUT,"TTOUT"}, + {SIGVTALRM,"VTALRM"}, + {SIGPROF,"PROF"}, + {SIGXCPU,"XCPU"}, + {SIGXFSZ,"XFSZ"}, + {SIGWAITING,"WAITING"}, + {SIGDIAF,"DIAF"}, + {SIGHATE,"HATE"}, + {SIGWINEVENT,"WINEVENT"}, + {SIGCAT,"CAT"}, + {0,NULL}, +}; + void usage(char * argv[]) { printf( "kill - send a signal to another process\n" @@ -36,7 +80,34 @@ int main(int argc, char * argv[]) { if (argc > 2) { if (argv[1][0] == '-') { - signum = atoi(argv[1]+1); + signum = -1; + if (strlen(argv[1]+1) > 3 && !strncmp(argv[1]+1,"SIG",3)) { + struct sig_def * s = signals; + while (s->name) { + if (!strcmp(argv[1]+4,s->name)) { + signum = s->sig; + break; + } + s++; + } + } else { + if (argv[1][1] < '0' || argv[1][1] > '9') { + struct sig_def * s = signals; + while (s->name) { + if (!strcmp(argv[1]+1,s->name)) { + signum = s->sig; + break; + } + s++; + } + } else { + signum = atoi(argv[1]+1); + } + } + if (signum == -1) { + fprintf(stderr,"%s: %s: invalid signal specification\n",argv[0],argv[1]+1); + return 1; + } } else { usage(argv); return 1;