-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkill.c
More file actions
53 lines (48 loc) · 1.29 KB
/
kill.c
File metadata and controls
53 lines (48 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char **argv) {
if (geteuid() != 0) {
fprintf(stderr, "(%s: must be run as root)\n", argv[0]);
return EXIT_FAILURE;
}
// get -p option
char *end;
int signum = SIGKILL;
int opt;
while ((opt = getopt(argc, argv, "p:")) != -1) {
if (opt == 'p') {
long sl = strtol(optarg, &end, 0);
if (*end != '\0' || sl < 0) {
fprintf(stderr, "(%s: invalid signal %s)\n", argv[0], optarg);
return EXIT_FAILURE;
}
signum = (int)sl;
} else {
fprintf(stderr, "usage: %s [-p signum] <pid>\n", argv[0]);
return EXIT_FAILURE;
}
}
if ((argc - optind) != 1) {
fprintf(stderr, "usage: %s [-p signum] <pid>\n", argv[0]);
return EXIT_FAILURE;
}
// get the pid
long pidl = strtol(argv[optind], &end, 0);
if (*end != '\0') {
fprintf(stderr, "(%s: process id %s must be number)\n", argv[0],
argv[optind]);
return EXIT_FAILURE;
}
// try to kill the pid
if (kill((pid_t)pidl, signum) < 0) {
fprintf(stderr, "(%s: cannot signal %s: %s)\n", argv[0], argv[optind],
strerror(errno));
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}