kill.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2013-2018 K. Lange
  5. *
  6. * kill - Send a signal to a process
  7. *
  8. * Supports signal names like any mature `kill` should.
  9. */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <unistd.h>
  14. #include <signal.h>
  15. struct sig_def {
  16. int sig;
  17. const char * name;
  18. };
  19. struct sig_def signals[] = {
  20. {SIGHUP,"HUP"},
  21. {SIGINT,"INT"},
  22. {SIGQUIT,"QUIT"},
  23. {SIGILL,"ILL"},
  24. {SIGTRAP,"TRAP"},
  25. {SIGABRT,"ABRT"},
  26. {SIGEMT,"EMT"},
  27. {SIGFPE,"FPE"},
  28. {SIGKILL,"KILL"},
  29. {SIGBUS,"BUS"},
  30. {SIGSEGV,"SEGV"},
  31. {SIGSYS,"SYS"},
  32. {SIGPIPE,"PIPE"},
  33. {SIGALRM,"ALRM"},
  34. {SIGTERM,"TERM"},
  35. {SIGUSR1,"USR1"},
  36. {SIGUSR2,"USR2"},
  37. {SIGCHLD,"CHLD"},
  38. {SIGPWR,"PWR"},
  39. {SIGWINCH,"WINCH"},
  40. {SIGURG,"URG"},
  41. {SIGPOLL,"POLL"},
  42. {SIGSTOP,"STOP"},
  43. {SIGTSTP,"TSTP"},
  44. {SIGCONT,"CONT"},
  45. {SIGTTIN,"TTIN"},
  46. {SIGTTOUT,"TTOUT"},
  47. {SIGVTALRM,"VTALRM"},
  48. {SIGPROF,"PROF"},
  49. {SIGXCPU,"XCPU"},
  50. {SIGXFSZ,"XFSZ"},
  51. {SIGWAITING,"WAITING"},
  52. {SIGDIAF,"DIAF"},
  53. {SIGHATE,"HATE"},
  54. {SIGWINEVENT,"WINEVENT"},
  55. {SIGCAT,"CAT"},
  56. {0,NULL},
  57. };
  58. void usage(char * argv[]) {
  59. printf(
  60. "%s - send a signal to another process\n"
  61. "\n"
  62. "usage: %s [-\033[3mx\033[0m] \033[3mprocess\033[0m\n"
  63. "\n"
  64. " -h --help \033[3mShow this help message.\033[0m\n"
  65. " -\033[3mx\033[0m \033[3mSignal number to send\033[0m\n"
  66. "\n",
  67. argv[0], argv[0]);
  68. }
  69. int main(int argc, char * argv[]) {
  70. int signum = SIGKILL;
  71. int pid = 0;
  72. if (argc < 2) {
  73. usage(argv);
  74. return 1;
  75. }
  76. if (argc > 2) {
  77. if (argv[1][0] == '-') {
  78. signum = -1;
  79. if (strlen(argv[1]+1) > 3 && strstr(argv[1]+1,"SIG") == (argv[1]+1)) {
  80. struct sig_def * s = signals;
  81. while (s->name) {
  82. if (!strcmp(argv[1]+4,s->name)) {
  83. signum = s->sig;
  84. break;
  85. }
  86. s++;
  87. }
  88. } else {
  89. if (argv[1][1] < '0' || argv[1][1] > '9') {
  90. struct sig_def * s = signals;
  91. while (s->name) {
  92. if (!strcmp(argv[1]+1,s->name)) {
  93. signum = s->sig;
  94. break;
  95. }
  96. s++;
  97. }
  98. } else {
  99. signum = atoi(argv[1]+1);
  100. }
  101. }
  102. if (signum == -1) {
  103. fprintf(stderr,"%s: %s: invalid signal specification\n",argv[0],argv[1]+1);
  104. return 1;
  105. }
  106. } else {
  107. usage(argv);
  108. return 1;
  109. }
  110. pid = atoi(argv[2]);
  111. } else if (argc == 2) {
  112. pid = atoi(argv[1]);
  113. }
  114. if (pid) {
  115. kill(pid, signum);
  116. }
  117. return 0;
  118. }