-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkill1_u.c
189 lines (173 loc) · 4.65 KB
/
kill1_u.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
/* kill1 - toolbox
Copyright 2007-2015 PC GO Ld.
Copyright 2015 libdll.so
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*/
#include <sys/types.h>
#if defined __sun && defined __SVR4
#ifndef _STRUCTURED_PROC
//#define _STRUCTURED_PROC 0
#define _STRUCTURED_PROC 1
#endif
#include <sys/procfs.h>
#include <unistd.h>
#include <fcntl.h>
#else
#include <sys/ptrace.h>
#endif
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <ctype.h>
static const char *signals[] = {
"NONE", "HUP", "INT", "QUIT", "ILL", "TRAP", "ABRT", "BUS", "FPE", "KILL", "USR1", "SEGV",
"USR2", "PIPE", "ALRM", "TERM", "STKFLT", "CHLD", "CONT", "STOP", "TSTP", "TTIN", "TTOU",
"URG", "XCPU", "XFSZ", "VTALRM", "PROF", "WINCH", "IO", "PWR", "SYS"
};
static int get_signal_from_name(const char *signal_name) {
if(!signal_name || !*signal_name) return -1;
int i;
for(i=1; i<32; i++) if(strcmp(signal_name, signals[i]) == 0) return i;
if(!isdigit(*signal_name)) return -1;
return atoi(signal_name);
}
static void print_signals() {
int i;
for(i=1; i<32; i++) {
printf("%2d %s%s", i, signals[i], (i % 4 && i != 31) ? (strlen(signals[i]) > 4 ? " " : " ") : "\n");
//putchar(i % 4 ? ' ' : '\n');
}
}
static void print_usage() {
fprintf(stderr, "Usage: kill1 [<options>]\n\n"
"options:\n"
" -s <signal> Specify the <signal> to send\n"
#if !defined __sun || !defined __SVR4
" -n Keep going when ptrace failed\n"
#endif
" -l List all available signals\n"
" -v Verbose\n\n");
}
int kill1_main(int argc, char **argv) {
#if !defined __sun || !defined __SVR4
int no_stop_flag = 0;
#endif
int verbose = 0;
char *signal1 = "TERM";
first_loop:
while(*++argv) {
if(strcmp(*argv, "--help") == 0) {
print_usage();
return -1;
} else if(*argv[0] == '-') {
const char *arg = *argv;
if(!arg[1]) goto eoption;
while(*++arg) switch(*arg) {
case 'h':
print_usage();
return -1;
case 'l':
print_signals();
return 0;
#if !defined __sun || !defined __SVR4
case 'n':
no_stop_flag = 1;
continue;
#endif
case 's':
if(arg[1]) {
fprintf(stderr, "Option '-s' not the end of a set of combined options\n");
return -2;
}
signal1 = *++argv;
if(!signal1) {
fprintf(stderr, "Option '-s' need an argument\n");
return -2;
}
goto first_loop;
case 'v':
verbose = 1;
continue;
eoption:
default:
fprintf(stderr, "Unknown option '%s'\n", *argv);
return -2;
}
}
}
int signal2 = get_signal_from_name(signal1);
if(signal2 < 0) {
fprintf(stderr, "Unknown signal '%s'\n", signal1);
return -3;
}
int r = 0;
#if defined __sun && defined __SVR4
if(verbose) fprintf(stderr, "Opening /proc/1/ctl ... ");
int fd = open("/proc/1/ctl", O_WRONLY);
if(fd == -1) {
perror("/proc/1/ctl");
return 1;
}
if(verbose) fprintf(stderr, "OK\nSending signal %s (%d) ... ", signal1, signal2);
#if _STRUCTURED_PROC
long int msg[2] = { PCKILL, signal2 };
if(write(fd, msg, sizeof msg) < 0) {
perror("write");
r = 1;
}
#else
if(ioctl(fd, PIOCKILL, &signal2) < 0) {
perror("ioctl");
r = 1;
}
#endif
if(r == 0 && verbose) fprintf(stderr, "OK\n");
if(verbose) fprintf(stderr, "Closing /proc/1/ctl ... ");
if(close(fd) < 0) {
perror("close");
return 1;
}
if(verbose) fprintf(stderr, "OK\n");
#else
if(verbose) fprintf(stderr, "Attaching to process 1... ");
if(ptrace(PT_ATTACH, 1, NULL, 0) < 0) {
//perror("ptrace");
int e = errno;
if(e) {
fprintf(stderr, "ptrace: %s\n", strerror(e));
if(no_stop_flag) {
r = 1;
goto send_signal;
}
return 1;
}
}
waitpid(1, NULL, 0);
if(verbose) fprintf(stderr, "OK\n");
send_signal:
if(verbose) fprintf(stderr, "Sending signal %s (%d) ... ", signal1, signal2);
//int r = 0;
if(kill(1, signal2) < 0) {
perror("kill");
//return 2;
//r += 2;
r |= 1 << 1;
} else if(verbose) fprintf(stderr, "OK\n");
if(r & 1) return r;
if(verbose) fprintf(stderr, "Detaching from process 1... ");
if(ptrace(PT_DETACH, 1, NULL, 0) < 0) {
int e = errno;
if(e) {
if(e != ESRCH || verbose) fprintf(stderr, "ptrace: %s\n", strerror(e));
//r += 3;
r |= 1 << 2;
}
}
if(verbose && !(r & (1 << 2))) fprintf(stderr, "OK\n");
#endif
return r;
}