annotate toys/killall.c @ 562:4d802d438983

Match uint64_t with PRIu64 to avoid warnings on 64 bit builds.
author Rob Landley <rob@landley.net>
date Sat, 14 Apr 2012 21:27:00 -0500
parents c9aaceccd6bd
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* vi: set sw=4 ts=4:
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
477
f0b07ce5f125 Cleanups to pidof (including some global infrastructure shared with killall).
Rob Landley <rob@landley.net>
parents: 475
diff changeset
3 * killall.c - Send signal (default: TERM) to all processes with given names.
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 *
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
5 * Copyright 2012 Andreas Heck <aheck@gmx.de>
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
6 *
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
7 * Not in SUSv4.
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
8
477
f0b07ce5f125 Cleanups to pidof (including some global infrastructure shared with killall).
Rob Landley <rob@landley.net>
parents: 475
diff changeset
9 USE_KILLALL(NEWTOY(killall, "<1?lq", TOYFLAG_USR|TOYFLAG_BIN))
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
10
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
11 config KILLALL
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
12 bool "killall"
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
13 default y
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
14 help
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
15 usage: killall [-l] [-q] [-SIG] PROCESS_NAME...
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
16
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
17 Send a signal (default: TERM) to all processes with the given names.
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
18
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
19 -l print list of all available signals
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
20 -q don't print any warnings or error messages
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
21 */
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
22
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
23 #include "toys.h"
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
24
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
25 #define FLAG_q 1
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
26 #define FLAG_l 2
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
27
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
28 DEFINE_GLOBALS(
477
f0b07ce5f125 Cleanups to pidof (including some global infrastructure shared with killall).
Rob Landley <rob@landley.net>
parents: 475
diff changeset
29 int signum;
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
30 )
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
31 #define TT this.killall
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
32
477
f0b07ce5f125 Cleanups to pidof (including some global infrastructure shared with killall).
Rob Landley <rob@landley.net>
parents: 475
diff changeset
33 static void kill_process(pid_t pid)
f0b07ce5f125 Cleanups to pidof (including some global infrastructure shared with killall).
Rob Landley <rob@landley.net>
parents: 475
diff changeset
34 {
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
35 int ret;
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
36
479
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
37 toys.exitval = 0;
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
38 ret = kill(pid, TT.signum);
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
39
477
f0b07ce5f125 Cleanups to pidof (including some global infrastructure shared with killall).
Rob Landley <rob@landley.net>
parents: 475
diff changeset
40 if (ret == -1 && !(toys.optflags & FLAG_q)) perror("kill");
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
41 }
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
42
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
43 void killall_main(void)
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
44 {
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
45 char **names;
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
46
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
47 if (toys.optflags & FLAG_l) {
479
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
48 sig_to_num(NULL);
477
f0b07ce5f125 Cleanups to pidof (including some global infrastructure shared with killall).
Rob Landley <rob@landley.net>
parents: 475
diff changeset
49 return;
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
50 }
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
51
479
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
52 TT.signum = SIGTERM;
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
53 toys.exitval++;
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
54
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
55 if (!*toys.optargs) {
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
56 toys.exithelp = 1;
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
57 error_exit("Process name missing!");
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
58 }
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
59
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
60 names = toys.optargs;
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
61
477
f0b07ce5f125 Cleanups to pidof (including some global infrastructure shared with killall).
Rob Landley <rob@landley.net>
parents: 475
diff changeset
62 if (**names == '-') {
479
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
63 if (0 > (TT.signum = sig_to_num((*names)+1))) {
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
64 if (toys.optflags & FLAG_q) exit(1);
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
65 error_exit("Invalid signal");
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
66 }
477
f0b07ce5f125 Cleanups to pidof (including some global infrastructure shared with killall).
Rob Landley <rob@landley.net>
parents: 475
diff changeset
67 names++;
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
68
479
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
69 if (!*names) {
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
70 toys.exithelp++;
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
71 error_exit("Process name missing!");
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
72 }
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
73 }
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
74
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
75 for_each_pid_with_name_in(names, kill_process);
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
76
479
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
77 if (toys.exitval && !(toys.optflags & FLAG_q))
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
78 error_exit("No such process");
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
79 }