kill.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #include <ctype.h>
  16. #include <errno.h>
  17. struct sig_def {
  18. int sig;
  19. const char * name;
  20. };
  21. struct sig_def signals[] = {
  22. {SIGHUP,"HUP"},
  23. {SIGINT,"INT"},
  24. {SIGQUIT,"QUIT"},
  25. {SIGILL,"ILL"},
  26. {SIGTRAP,"TRAP"},
  27. {SIGABRT,"ABRT"},
  28. {SIGEMT,"EMT"},
  29. {SIGFPE,"FPE"},
  30. {SIGKILL,"KILL"},
  31. {SIGBUS,"BUS"},
  32. {SIGSEGV,"SEGV"},
  33. {SIGSYS,"SYS"},
  34. {SIGPIPE,"PIPE"},
  35. {SIGALRM,"ALRM"},
  36. {SIGTERM,"TERM"},
  37. {SIGUSR1,"USR1"},
  38. {SIGUSR2,"USR2"},
  39. {SIGCHLD,"CHLD"},
  40. {SIGPWR,"PWR"},
  41. {SIGWINCH,"WINCH"},
  42. {SIGURG,"URG"},
  43. {SIGPOLL,"POLL"},
  44. {SIGSTOP,"STOP"},
  45. {SIGTSTP,"TSTP"},
  46. {SIGCONT,"CONT"},
  47. {SIGTTIN,"TTIN"},
  48. {SIGTTOUT,"TTOUT"},
  49. {SIGVTALRM,"VTALRM"},
  50. {SIGPROF,"PROF"},
  51. {SIGXCPU,"XCPU"},
  52. {SIGXFSZ,"XFSZ"},
  53. {SIGWAITING,"WAITING"},
  54. {SIGDIAF,"DIAF"},
  55. {SIGHATE,"HATE"},
  56. {SIGWINEVENT,"WINEVENT"},
  57. {SIGCAT,"CAT"},
  58. {0,NULL},
  59. };
  60. void usage(char * argv[]) {
  61. printf(
  62. "%s - send a signal to another process\n"
  63. "\n"
  64. "usage: %s [-\033[3mx\033[0m] \033[3mprocess\033[0m\n"
  65. "\n"
  66. " -h --help \033[3mShow this help message.\033[0m\n"
  67. " -\033[3mx\033[0m \033[3mSignal number to send\033[0m\n"
  68. "\n",
  69. argv[0], argv[0]);
  70. }
  71. int main(int argc, char * argv[]) {
  72. int signum = SIGKILL;
  73. int pid = 0;
  74. int i = 1;
  75. if (argc < 2) {
  76. usage(argv);
  77. return 1;
  78. }
  79. if (argv[1][0] == '-') {
  80. signum = -1;
  81. if (strlen(argv[1]+1) > 3 && strstr(argv[1]+1,"SIG") == (argv[1]+1)) {
  82. struct sig_def * s = signals;
  83. while (s->name) {
  84. if (!strcmp(argv[1]+4,s->name)) {
  85. signum = s->sig;
  86. break;
  87. }
  88. s++;
  89. }
  90. } else {
  91. if (!isdigit(argv[1][1] < '0')) {
  92. struct sig_def * s = signals;
  93. while (s->name) {
  94. if (!strcmp(argv[1]+1,s->name)) {
  95. signum = s->sig;
  96. break;
  97. }
  98. s++;
  99. }
  100. } else {
  101. signum = atoi(argv[1]+1);
  102. }
  103. }
  104. if (signum == -1) {
  105. fprintf(stderr,"%s: %s: invalid signal specification\n",argv[0],argv[1]+1);
  106. return 1;
  107. }
  108. i++;
  109. }
  110. if (i == argc) {
  111. usage(argv);
  112. return 1;
  113. }
  114. int retval = 0;
  115. for (; i < argc; ++i) {
  116. pid = atoi(argv[i]);
  117. if (pid) {
  118. if (kill(pid, signum) < 0) {
  119. fprintf(stderr, "%s: (%d) %s\n", argv[0], pid, strerror(errno));
  120. retval = 1;
  121. }
  122. } else {
  123. fprintf(stderr, "%s: invalid pid (%s)\n", argv[0], argv[i]);
  124. retval = 1;
  125. }
  126. }
  127. return retval;
  128. }