kill.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /*++
  2. Copyright (c) 2013 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. kill.c
  5. Abstract:
  6. This module implements the kill (process termination and signalling)
  7. utility.
  8. Author:
  9. Chris Stevens 19-Aug-2014
  10. Environment:
  11. POSIX
  12. --*/
  13. //
  14. // ------------------------------------------------------------------- Includes
  15. //
  16. #include <minoca/lib/types.h>
  17. #include <assert.h>
  18. #include <ctype.h>
  19. #include <errno.h>
  20. #include <getopt.h>
  21. #include <signal.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include "swlib.h"
  26. //
  27. // ---------------------------------------------------------------- Definitions
  28. //
  29. #define KILL_VERSION_MAJOR 1
  30. #define KILL_VERSION_MINOR 0
  31. #define KILL_USAGE \
  32. "usage: kill -s signal_name pid...\n" \
  33. " kill -l [exit_status...]\n" \
  34. " kill [-signal_name] pid...\n" \
  35. " kill [-signal_number] pid...\n\n" \
  36. "The kill utility sends a signal to one or more processes. Options are:\n" \
  37. " -l --list -- Lists all supported values for signal_name if an " \
  38. "exit_status\n" \
  39. " is not supplied. If an exit_status is supplied and it is a\n" \
  40. " signal_number, then the corresponding signal_name is written.\n" \
  41. " If the exit_status the '?' special shell character for a\n" \
  42. " terminated process, then signal_name of the signal that " \
  43. "terminated\n" \
  44. " the process is written.\n" \
  45. " -s --signal <signal_name> -- Specify a signal to send using a signal\n" \
  46. " name.\n" \
  47. " --help -- Show this help text and exit.\n" \
  48. " --version -- Print the application version information and exit.\n\n"
  49. #define KILL_OPTIONS_STRING "+ls:hV"
  50. //
  51. // Define kill options.
  52. //
  53. //
  54. // Set this option if the utility should list signal names and values.
  55. //
  56. #define KILL_OPTIONS_LIST_SIGNALS 0x00000001
  57. //
  58. // Set this option if the utility should send signals.
  59. //
  60. #define KILL_OPTIONS_SEND_SIGNALS 0x00000002
  61. //
  62. // Define the default signal number to send.
  63. //
  64. #define KILL_DEFAULT_SIGNAL_NUMBER SIGTERM
  65. //
  66. // ------------------------------------------------------ Data Type Definitions
  67. //
  68. //
  69. // ----------------------------------------------- Internal Function Prototypes
  70. //
  71. VOID
  72. KillpPrintSignals (
  73. VOID
  74. );
  75. INT
  76. KillpPrintSignal (
  77. PSTR Argument
  78. );
  79. INT
  80. KillpParseSignalName (
  81. PSTR Argument,
  82. PINT SignalNumber
  83. );
  84. //
  85. // -------------------------------------------------------------------- Globals
  86. //
  87. struct option KillLongOptions[] = {
  88. {"list", no_argument, 0, 'l'},
  89. {"signal", required_argument, 0, 's'},
  90. {"help", no_argument, 0, 'h'},
  91. {"version", no_argument, 0, 'V'},
  92. {NULL, 0, 0, 0},
  93. };
  94. //
  95. // ------------------------------------------------------------------ Functions
  96. //
  97. INT
  98. KillMain (
  99. INT ArgumentCount,
  100. CHAR **Arguments
  101. )
  102. /*++
  103. Routine Description:
  104. This routine is the main entry point for the kill utility.
  105. Arguments:
  106. ArgumentCount - Supplies the number of command line arguments the program
  107. was invoked with.
  108. Arguments - Supplies a tokenized array of command line arguments.
  109. Return Value:
  110. Returns an integer exit code. 0 for success, non-zero otherwise.
  111. --*/
  112. {
  113. PSTR AfterScan;
  114. PSTR Argument;
  115. ULONG ArgumentIndex;
  116. BOOL ContinueLoop;
  117. ULONG Index;
  118. INT Option;
  119. ULONG Options;
  120. INT PreviousOptionIndex;
  121. pid_t ProcessId;
  122. PSTR SignalName;
  123. INT SignalNumber;
  124. INT Status;
  125. Status = 0;
  126. //
  127. // Disable option errors for the kill utility as it allows some
  128. // non-traditional options.
  129. //
  130. opterr = 0;
  131. //
  132. // The default is to send SIGTERM signals to all process IDs listed.
  133. //
  134. Options = KILL_OPTIONS_SEND_SIGNALS;
  135. SignalNumber = KILL_DEFAULT_SIGNAL_NUMBER;
  136. //
  137. // Process the control arguments.
  138. //
  139. SignalName = NULL;
  140. ContinueLoop = TRUE;
  141. while (ContinueLoop != FALSE) {
  142. PreviousOptionIndex = optind;
  143. Option = getopt_long(ArgumentCount,
  144. Arguments,
  145. KILL_OPTIONS_STRING,
  146. KillLongOptions,
  147. NULL);
  148. if (Option == -1) {
  149. break;
  150. }
  151. if (Option == ':') {
  152. Status = 1;
  153. goto MainEnd;
  154. }
  155. switch (Option) {
  156. case '?':
  157. //
  158. // If a signal name has already been found, then stop processing
  159. // and roll back the index.
  160. //
  161. if (SignalName != NULL) {
  162. optind = PreviousOptionIndex;
  163. ContinueLoop = FALSE;
  164. break;
  165. }
  166. //
  167. // Otherwise there may be a signal name at the previous index. If
  168. // it was just a '-9' then 'optind' incremented. If it was '-kill',
  169. // then 'optind' did not increment. So, go with the previous option
  170. // index, saving the signal name for later.
  171. //
  172. Argument = Arguments[PreviousOptionIndex];
  173. assert(*Argument == '-');
  174. Argument += 1;
  175. SignalName = Argument;
  176. Options |= KILL_OPTIONS_SEND_SIGNALS;
  177. //
  178. // Also, skip over the whole option if it did not move forward.
  179. //
  180. if (optind == PreviousOptionIndex) {
  181. optind += 1;
  182. }
  183. break;
  184. case 'l':
  185. Options |= KILL_OPTIONS_LIST_SIGNALS;
  186. break;
  187. case 's':
  188. //
  189. // Check to see if this is a direct signal name. For example,
  190. // "kill -sigkill 12345" should kill process 12345. If the option
  191. // index only advanced 1, then the argument is attached to the 's'.
  192. // Test these cases for '-sig*'.
  193. //
  194. if ((optind - 1) == PreviousOptionIndex) {
  195. //
  196. // If a signal name has already been found, exit and roll back
  197. // the option.
  198. //
  199. if (SignalName != NULL) {
  200. optind = PreviousOptionIndex;
  201. ContinueLoop = FALSE;
  202. break;
  203. }
  204. Argument = optarg;
  205. //
  206. // Move back a character to include the initial 's' and save it
  207. // for later.
  208. //
  209. Argument -= 1;
  210. assert(*Argument == 's');
  211. SignalName = Argument;
  212. Options |= KILL_OPTIONS_SEND_SIGNALS;
  213. //
  214. // Otherwise, the argument is in a separate array entry that the
  215. // 's' option. Save it for later.
  216. //
  217. } else {
  218. Argument = optarg;
  219. assert(Argument != NULL);
  220. SignalName = Argument;
  221. Options |= KILL_OPTIONS_SEND_SIGNALS;
  222. }
  223. break;
  224. case 'V':
  225. SwPrintVersion(KILL_VERSION_MAJOR, KILL_VERSION_MINOR);
  226. return 1;
  227. case 'h':
  228. printf(KILL_USAGE);
  229. return 1;
  230. default:
  231. assert(FALSE);
  232. Status = 1;
  233. goto MainEnd;
  234. }
  235. }
  236. //
  237. // One of the kill options should be sent.
  238. //
  239. assert(((Options & KILL_OPTIONS_LIST_SIGNALS) != 0) ||
  240. ((Options & KILL_OPTIONS_SEND_SIGNALS) != 0));
  241. //
  242. // Get the current argument index, making sure it is not too big.
  243. //
  244. ArgumentIndex = optind;
  245. if (ArgumentIndex > ArgumentCount) {
  246. Status = 1;
  247. goto MainEnd;
  248. }
  249. //
  250. // If listing the signal numbers was requested, then prefer that over
  251. // sending signals.
  252. //
  253. if ((Options & KILL_OPTIONS_LIST_SIGNALS) != 0) {
  254. if (ArgumentIndex == ArgumentCount) {
  255. KillpPrintSignals();
  256. } else {
  257. for (Index = ArgumentIndex; Index < ArgumentCount; Index += 1) {
  258. Status = KillpPrintSignal(Arguments[Index]);
  259. if (Status != 0) {
  260. goto MainEnd;
  261. }
  262. }
  263. }
  264. //
  265. // Sending signals is the default. Send signals to all remaining arguments,
  266. // interpreting them as process IDs or process groups.
  267. //
  268. } else {
  269. //
  270. // Figure out the signal to use.
  271. //
  272. if (SignalName != NULL) {
  273. Status = KillpParseSignalName(SignalName, &SignalNumber);
  274. if (Status != 0) {
  275. goto MainEnd;
  276. }
  277. }
  278. //
  279. // Exit and print the help if no process IDs were provided.
  280. //
  281. if (ArgumentIndex == ArgumentCount) {
  282. printf(KILL_USAGE);
  283. Status = 1;
  284. goto MainEnd;
  285. }
  286. for (Index = ArgumentIndex; Index < ArgumentCount; Index += 1) {
  287. ProcessId = (pid_t)strtol(Arguments[Index], &AfterScan, 10);
  288. if ((AfterScan == Arguments[Index]) || (*AfterScan != '\0')) {
  289. SwPrintError(0,
  290. NULL,
  291. "Invalid process ID '%s'",
  292. Arguments[Index]);
  293. continue;
  294. }
  295. Status = SwKill(ProcessId, SignalNumber);
  296. if (Status != 0) {
  297. SwPrintError(errno,
  298. NULL,
  299. "Failed to signal process %lu",
  300. ProcessId);
  301. }
  302. }
  303. }
  304. Status = 0;
  305. MainEnd:
  306. return Status;
  307. }
  308. //
  309. // --------------------------------------------------------- Internal Functions
  310. //
  311. VOID
  312. KillpPrintSignals (
  313. VOID
  314. )
  315. /*++
  316. Routine Description:
  317. This routine prints out all the allowable signal names and signal numbers.
  318. Arguments:
  319. None.
  320. Return Value:
  321. None.
  322. --*/
  323. {
  324. PSTR Name;
  325. INT Signal;
  326. //
  327. // Print all signals, skipping the zero signal.
  328. //
  329. for (Signal = 1; Signal <= NSIG; Signal += 1) {
  330. if ((SIGRTMAX > SIGRTMIN) &&
  331. (Signal >= SIGRTMIN) && (Signal <= SIGRTMAX)) {
  332. if (Signal == SIGRTMIN) {
  333. printf("%d) SIGRTMIN\n", Signal);
  334. } else if (Signal == SIGRTMAX) {
  335. printf("%d) SIGRTMAX\n", Signal);
  336. } else {
  337. printf("%d) SIGRTMIN+%d\n", Signal, Signal - SIGRTMIN);
  338. }
  339. } else {
  340. Name = SwGetSignalNameFromNumber(Signal);
  341. if (Name != NULL) {
  342. printf("%d) %s\n", Signal, Name);
  343. }
  344. }
  345. }
  346. return;
  347. }
  348. INT
  349. KillpPrintSignal (
  350. PSTR Argument
  351. )
  352. /*++
  353. Routine Description:
  354. This routine prints the signal name for a given signal number string or
  355. prints the signal number for a given signal name string.
  356. Arguments:
  357. Argument - Supplies the string representation of either a signal name or
  358. signal number.
  359. Return Value:
  360. 0 on success.
  361. EINVAL if the signal argument is invalid.
  362. --*/
  363. {
  364. PSTR Name;
  365. INT SignalNumber;
  366. INT Status;
  367. Status = 0;
  368. SignalNumber = SwGetSignalNumberFromName(Argument);
  369. if (SignalNumber == -1) {
  370. SwPrintError(0, Argument, "Invalid signal specification");
  371. Status = EINVAL;
  372. } else {
  373. if (!isdigit(*Argument)) {
  374. printf("%d\n", SignalNumber);
  375. } else {
  376. if ((SIGRTMAX > SIGRTMIN) &&
  377. (SignalNumber >= SIGRTMIN) &&
  378. (SignalNumber <= SIGRTMAX)) {
  379. if (SignalNumber == SIGRTMIN) {
  380. printf("RTMIN\n");
  381. } else if (SignalNumber < SIGRTMAX) {
  382. printf("RTMAX\n");
  383. } else {
  384. printf("RTMIN+%d", SignalNumber - SIGRTMIN);
  385. }
  386. } else {
  387. Name = SwGetSignalNameFromNumber(SignalNumber);
  388. if (Name == NULL) {
  389. SwPrintError(0, Argument, "Invalid signal specification");
  390. Status = EINVAL;
  391. } else {
  392. printf("%s\n", Name);
  393. }
  394. }
  395. }
  396. }
  397. return Status;
  398. }
  399. INT
  400. KillpParseSignalName (
  401. PSTR Argument,
  402. PINT SignalNumber
  403. )
  404. /*++
  405. Routine Description:
  406. This routine parses the signal name argument to validate it and conver it
  407. into a signal number.
  408. Arguments:
  409. Argument - Supplies the argument string to parse for the signal name.
  410. SignalNumber - Supplies a pointer that receives the numeric signal value.
  411. Return Value:
  412. 0 on succes. Non-zero on failure.
  413. --*/
  414. {
  415. *SignalNumber = SwGetSignalNumberFromName(Argument);
  416. if (*SignalNumber == -1) {
  417. return EINVAL;
  418. }
  419. return 0;
  420. }