-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathkillall.c
230 lines (204 loc) · 4.46 KB
/
killall.c
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**
* @brief killall - Send signals to processes matching name
*
* Find processes by name and send them signals.
*
* @copyright
* This file is part of ToaruOS and is released under the terms
* of the NCSA / University of Illinois License - see LICENSE.md
* Copyright (C) 2018 K. Lange
*/
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <signal.h>
#include <getopt.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/signal.h>
typedef struct process {
int pid;
int ppid;
int tgid;
char name[100];
char path[200];
} p_t;
#define LINE_LEN 4096
p_t * build_entry(struct dirent * dent) {
char tmp[300];
FILE * f;
char line[LINE_LEN];
sprintf(tmp, "/proc/%s/status", dent->d_name);
f = fopen(tmp, "r");
p_t * proc = malloc(sizeof(p_t));
while (fgets(line, LINE_LEN, f) != NULL) {
char * n = strstr(line,"\n");
if (n) { *n = '\0'; }
char * tab = strstr(line,"\t");
if (tab) {
*tab = '\0';
tab++;
}
if (strstr(line, "Pid:") == line) {
proc->pid = atoi(tab);
} else if (strstr(line, "PPid:") == line) {
proc->ppid = atoi(tab);
} else if (strstr(line, "Tgid:") == line) {
proc->tgid = atoi(tab);
} else if (strstr(line, "Name:") == line) {
strcpy(proc->name, tab);
} else if (strstr(line, "Path:") == line) {
strcpy(proc->path, tab);
}
}
if (strstr(proc->name,"python") == proc->name) {
char * name = proc->path + strlen(proc->path) - 1;
while (1) {
if (*name == '/') {
name++;
break;
}
if (name == proc->name) break;
name--;
}
memcpy(proc->name, name, strlen(name)+1);
}
if (proc->tgid != proc->pid) {
char tmp[100] = {0};
sprintf(tmp, "{%s}", proc->name);
memcpy(proc->name, tmp, strlen(tmp)+1);
}
fclose(f);
return proc;
}
void show_usage(int argc, char * argv[]) {
printf(
"killall - send signal to processes with given name\n"
"\n"
"usage: %s [-s SIG] name\n"
"\n"
" -s \033[3msignal to send\033[0m\n"
" -? \033[3mshow this help text\033[0m\n"
"\n", argv[0]);
}
struct sig_def {
int sig;
const char * name;
};
struct sig_def signals[] = {
{SIGHUP,"HUP"},
{SIGINT,"INT"},
{SIGQUIT,"QUIT"},
{SIGILL,"ILL"},
{SIGTRAP,"TRAP"},
{SIGABRT,"ABRT"},
{SIGEMT,"EMT"},
{SIGFPE,"FPE"},
{SIGKILL,"KILL"},
{SIGBUS,"BUS"},
{SIGSEGV,"SEGV"},
{SIGSYS,"SYS"},
{SIGPIPE,"PIPE"},
{SIGALRM,"ALRM"},
{SIGTERM,"TERM"},
{SIGUSR1,"USR1"},
{SIGUSR2,"USR2"},
{SIGCHLD,"CHLD"},
{SIGPWR,"PWR"},
{SIGWINCH,"WINCH"},
{SIGURG,"URG"},
{SIGPOLL,"POLL"},
{SIGSTOP,"STOP"},
{SIGTSTP,"TSTP"},
{SIGCONT,"CONT"},
{SIGTTIN,"TTIN"},
{SIGTTOUT,"TTOUT"},
{SIGVTALRM,"VTALRM"},
{SIGPROF,"PROF"},
{SIGXCPU,"XCPU"},
{SIGXFSZ,"XFSZ"},
{SIGWAITING,"WAITING"},
{SIGDIAF,"DIAF"},
{SIGHATE,"HATE"},
{SIGWINEVENT,"WINEVENT"},
{SIGCAT,"CAT"},
{0,NULL},
};
int main (int argc, char * argv[]) {
int signum = SIGTERM;
int c;
while ((c = getopt(argc, argv, "s:?")) != -1) {
switch (c) {
case 's':
{
signum = -1;
if (strlen(optarg) > 3 && strstr(optarg,"SIG") == (optarg)) {
struct sig_def * s = signals;
while (s->name) {
if (!strcmp(optarg+3,s->name)) {
signum = s->sig;
break;
}
s++;
}
} else {
if (optarg[0] < '0' || optarg[0] > '9') {
struct sig_def * s = signals;
while (s->name) {
if (!strcmp(optarg,s->name)) {
signum = s->sig;
break;
}
s++;
}
} else {
signum = atoi(optarg);
}
}
if (signum == -1) {
fprintf(stderr,"%s: %s: invalid signal specification\n",argv[0],optarg);
return 1;
}
}
break;
case '?':
show_usage(argc, argv);
return 0;
}
}
if (optind >= argc) {
show_usage(argc, argv);
return 1;
}
int killed_something = 0;
int retval = 0;
for (int i = optind; i < argc; ++i) {
/* Open the directory */
DIR * dirp = opendir("/proc");
struct dirent * ent = readdir(dirp);
while (ent != NULL) {
if (ent->d_name[0] >= '0' && ent->d_name[0] <= '9') {
p_t * proc = build_entry(ent);
if (!strcmp(proc->name, argv[i])) {
if (kill(proc->pid, signum) < 0) {
fprintf(stderr, "%s(%d) %s\n", argv[i], proc->pid, strerror(errno));
} else {
killed_something = 1;
}
}
free(proc);
}
ent = readdir(dirp);
}
closedir(dirp);
if (!killed_something) {
fprintf(stderr, "%s: no process found\n", argv[i]);
retval = 1;
}
}
return retval;
}