2018-08-15 04:07:33 +03:00
|
|
|
/* vim: tabstop=4 shiftwidth=4 noexpandtab
|
|
|
|
* This file is part of ToaruOS and is released under the terms
|
2018-03-03 13:37:50 +03:00
|
|
|
* of the NCSA / University of Illinois License - see LICENSE.md
|
2018-05-01 11:12:56 +03:00
|
|
|
* Copyright (C) 2013-2018 K. Lange
|
2018-03-03 13:37:50 +03:00
|
|
|
*
|
2018-08-15 04:07:33 +03:00
|
|
|
* kill - Send a signal to a process
|
2018-03-03 13:37:50 +03:00
|
|
|
*
|
2018-08-15 04:07:33 +03:00
|
|
|
* Supports signal names like any mature `kill` should.
|
2018-03-03 13:37:50 +03:00
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
2018-10-08 04:23:26 +03:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
2018-03-03 13:37:50 +03:00
|
|
|
|
|
|
|
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(
|
2018-08-15 04:07:33 +03:00
|
|
|
"%s - send a signal to another process\n"
|
2018-03-03 13:37:50 +03:00
|
|
|
"\n"
|
|
|
|
"usage: %s [-\033[3mx\033[0m] \033[3mprocess\033[0m\n"
|
|
|
|
"\n"
|
|
|
|
" -h --help \033[3mShow this help message.\033[0m\n"
|
|
|
|
" -\033[3mx\033[0m \033[3mSignal number to send\033[0m\n"
|
|
|
|
"\n",
|
2018-08-15 04:07:33 +03:00
|
|
|
argv[0], argv[0]);
|
2018-03-03 13:37:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char * argv[]) {
|
|
|
|
int signum = SIGKILL;
|
|
|
|
int pid = 0;
|
2018-10-08 04:23:26 +03:00
|
|
|
int i = 1;
|
2018-03-03 13:37:50 +03:00
|
|
|
|
|
|
|
if (argc < 2) {
|
|
|
|
usage(argv);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-10-08 04:23:26 +03:00
|
|
|
if (argv[1][0] == '-') {
|
|
|
|
signum = -1;
|
|
|
|
if (strlen(argv[1]+1) > 3 && strstr(argv[1]+1,"SIG") == (argv[1]+1)) {
|
|
|
|
struct sig_def * s = signals;
|
|
|
|
while (s->name) {
|
|
|
|
if (!strcmp(argv[1]+4,s->name)) {
|
|
|
|
signum = s->sig;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
} else {
|
2020-12-21 04:07:23 +03:00
|
|
|
if (!isdigit(argv[1][1])) {
|
2018-03-03 13:37:50 +03:00
|
|
|
struct sig_def * s = signals;
|
|
|
|
while (s->name) {
|
2018-10-08 04:23:26 +03:00
|
|
|
if (!strcmp(argv[1]+1,s->name)) {
|
2018-03-03 13:37:50 +03:00
|
|
|
signum = s->sig;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
} else {
|
2018-10-08 04:23:26 +03:00
|
|
|
signum = atoi(argv[1]+1);
|
2018-03-03 13:37:50 +03:00
|
|
|
}
|
2018-10-08 04:23:26 +03:00
|
|
|
}
|
|
|
|
if (signum == -1) {
|
|
|
|
fprintf(stderr,"%s: %s: invalid signal specification\n",argv[0],argv[1]+1);
|
2018-03-03 13:37:50 +03:00
|
|
|
return 1;
|
|
|
|
}
|
2018-10-08 04:23:26 +03:00
|
|
|
i++;
|
2018-03-03 13:37:50 +03:00
|
|
|
}
|
|
|
|
|
2018-10-08 04:41:04 +03:00
|
|
|
if (i == argc) {
|
|
|
|
usage(argv);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int retval = 0;
|
|
|
|
|
|
|
|
for (; i < argc; ++i) {
|
2018-10-08 04:23:26 +03:00
|
|
|
pid = atoi(argv[i]);
|
|
|
|
if (pid) {
|
|
|
|
if (kill(pid, signum) < 0) {
|
2018-10-08 04:35:30 +03:00
|
|
|
fprintf(stderr, "%s: (%d) %s\n", argv[0], pid, strerror(errno));
|
2018-10-08 04:41:04 +03:00
|
|
|
retval = 1;
|
2018-10-08 04:23:26 +03:00
|
|
|
}
|
|
|
|
} else {
|
2018-10-08 04:35:30 +03:00
|
|
|
fprintf(stderr, "%s: invalid pid (%s)\n", argv[0], argv[i]);
|
2018-10-08 04:41:04 +03:00
|
|
|
retval = 1;
|
2018-10-08 04:23:26 +03:00
|
|
|
}
|
2018-03-03 13:37:50 +03:00
|
|
|
}
|
|
|
|
|
2018-10-08 04:41:04 +03:00
|
|
|
return retval;
|
2018-03-03 13:37:50 +03:00
|
|
|
}
|