Created
February 11, 2018 09:13
-
-
Save siddontang/51ad16049a6aa389255d5f82ef476027 to your computer and use it in GitHub Desktop.
A simple implementation for tgkill
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <unistd.h> | |
#include <stdio.h> | |
#include <sys/syscall.h> | |
#include <sys/types.h> | |
#include <sys/signal.h> | |
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) | |
// List common use signal names | |
static const struct signal_name { | |
const char *name; | |
int sig; | |
} signal_names[] = { | |
{ "HUP", SIGHUP }, | |
{ "INT", SIGINT }, | |
{ "QUIT", SIGQUIT }, | |
{ "ILL", SIGILL }, | |
{ "TRAP", SIGTRAP }, | |
{ "ABRT", SIGABRT }, | |
{ "FPE", SIGFPE }, | |
{ "KILL", SIGKILL }, | |
{ "USR1", SIGUSR1 }, | |
{ "SEGV", SIGSEGV }, | |
{ "USR2", SIGUSR2 }, | |
{ "PIPE", SIGPIPE }, | |
{ "ALRM", SIGALRM }, | |
{ "TERM", SIGTERM }, | |
{ "CONT", SIGCONT }, | |
{ "STOP", SIGSTOP }, | |
}; | |
int signame_to_num(char *name) { | |
size_t n; | |
if(isdigit(*name)) { | |
return atoi(name); | |
} | |
if(!strncasecmp(name, "sig", 3)) | |
name += 3; | |
for(n = 0; n < ARRAY_SIZE(signal_names); n++) { | |
if(!strncasecmp(signal_names[n].name, name)) { | |
return signal_names[n].sig; | |
} | |
} | |
return -1; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
int sig = SIGTERM; | |
int tgid = -1; | |
int tid; | |
if (argc != 4) { | |
printf("Usage: %s SIGNAL|SIGNUM <tgid> <tid>\n", argv[0]); | |
return 1; | |
} | |
sig = signame_to_num(argv[1]); | |
tgid = atoi(argv[2]); | |
tid = atoi(argv[3]); | |
return syscall(SYS_tgkill, tgid, tid, sig); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment