-
Notifications
You must be signed in to change notification settings - Fork 3
/
sendsig.c
323 lines (271 loc) · 8.28 KB
/
sendsig.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
*
* Module: sendsig
* Description: A small hack to kill crazy real time processes
*
* Copyright 2010, Alca Società Cooperativa
*
* 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 3 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/version.h>
#include <linux/init.h>
#include <asm/siginfo.h>
#include <linux/rcupdate.h>
#include <linux/sched.h>
#include <linux/pid.h>
#include <linux/debugfs.h>
#include <linux/uaccess.h>
#include <linux/timer.h>
#include <linux/jiffies.h>
#include <asm/cputime.h>
#define MOD_AUTHOR "Domenico Delle Side <[email protected]>"
#define MOD_DESC "Small kernel module to check a process' cpu usage and kill it if too high"
/* safe defaults for the module params */
#define SIG_TO_SEND 9
#define MAX_CPU_SHARE 90
#define WAIT_TIMEOUT 10
#define MAX_CHECKS 6
static int sig_to_send = SIG_TO_SEND;
static ushort max_cpu_share = MAX_CPU_SHARE;
static ushort wait_timeout = WAIT_TIMEOUT;
static ushort max_checks = MAX_CHECKS;
module_param(sig_to_send, int, 0000);
MODULE_PARM_DESC(sig_to_send, " The signal code you want to send (default: SIGKILL, 9)");
module_param(max_cpu_share, ushort, 0000);
MODULE_PARM_DESC(max_cpu_share, " The maximum cpu share admissible for the process, a value between 0 and 100 (default: 90)");
module_param(wait_timeout, ushort, 0000);
MODULE_PARM_DESC(wait_timeout, " The number of seconds to wait between each check (default: 10)");
module_param(max_checks, ushort, 0000);
MODULE_PARM_DESC(max_checks, " The number of checks after which the signal is sent (default: 6)");
static struct timer_list check_timer;
static struct task_struct *check_task;
static struct dentry *file;
static pid_t pid;
static cputime_t last_cputime;
static ushort count_check;
/*
This function is not exported to modules by the kernel, so let's
re-define it there. Taken from
LINUX_SOURCE/kernel/posix-cpu-timers.c. Kudos to its author.
*/
void my_thread_group_cputime(struct task_struct *tsk, struct task_cputime *times)
{
struct signal_struct *sig;
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
struct task_struct *t;
struct sighand_struct *sighand;
*times = INIT_CPUTIME;
rcu_read_lock();
sighand = rcu_dereference(tsk->sighand);
if (!sighand)
goto out;
sig = tsk->signal;
t = tsk;
do {
times->utime = cputime_add(times->utime, t->utime);
times->stime = cputime_add(times->stime, t->stime);
times->sum_exec_runtime += t->se.sum_exec_runtime;
t = next_thread(t);
} while (t != tsk);
times->utime = cputime_add(times->utime, sig->utime);
times->stime = cputime_add(times->stime, sig->stime);
times->sum_exec_runtime += sig->sum_sched_runtime;
out:
rcu_read_unlock();
#else
int i;
struct task_cputime *tot;
sig = tsk->signal;
if (unlikely(!sig) || !sig->cputime.totals) {
times->utime = tsk->utime;
times->stime = tsk->stime;
times->sum_exec_runtime = tsk->se.sum_exec_runtime;
return;
}
times->stime = times->utime = cputime_zero;
times->sum_exec_runtime = 0;
for_each_possible_cpu(i) {
tot = per_cpu_ptr(tsk->signal->cputime.totals, i);
times->utime = cputime_add(times->utime, tot->utime);
times->stime = cputime_add(times->stime, tot->stime);
times->sum_exec_runtime += tot->sum_exec_runtime;
}
#endif
}
static ushort thread_group_cpu_share(struct task_struct *task)
{
struct task_cputime times;
cputime_t num_load, div_load, total_time;
ushort share;
my_thread_group_cputime(task, ×);
total_time = cputime_add(times.utime, times.stime);
/*
last_cputime == 0 means that the timer_function has been called
for the first time and we have to collect info before doing any
check.
*/
if (unlikely(last_cputime == 0)) {
share = 0;
printk(KERN_INFO "sendsig: timer initialization completed\n");
} else {
/*
Let's compute the share of cpu usage for the last WAIT_TIMEOUT
seconds
*/
num_load = cputime_sub(total_time, last_cputime) * 100;
div_load = jiffies_to_cputime(wait_timeout * HZ);
share = (ushort)cputime_div(num_load, div_load);
printk(KERN_DEBUG "sendsig: computed cpu share for process %d: %d\n",
pid, share);
}
/*
Update last_cputime
*/
last_cputime = total_time;
return share;
}
static struct task_struct *get_check_task(pid_t pid)
{
struct task_struct *task;
struct pid *struct_pid = NULL;
rcu_read_lock();
struct_pid = find_get_pid(pid);
task = pid_task(struct_pid, PIDTYPE_PID);
rcu_read_unlock();
if(unlikely(task == NULL)){
printk(KERN_INFO "sendsig: no process with pid %d found\n", pid);
return NULL;
}
return task;
}
void signal_send(struct task_struct *task)
{
struct siginfo info;
/*
initialize the signal structure
*/
memset(&info, 0, sizeof(struct siginfo));
info.si_signo = sig_to_send;
info.si_code = SI_KERNEL;
/*
send the signal to the process
*/
send_sig_info(sig_to_send, &info, task);
}
static void timer_function(unsigned long par)
{
ushort cpu_share;
if (unlikely(!pid_alive(check_task))) {
del_timer(&check_timer);
printk(KERN_INFO "sendsig: cannot find pid %i. Is the process still active? Timer removed\n", pid);
return;
}
cpu_share = thread_group_cpu_share(check_task);
if (cpu_share >= max_cpu_share) {
count_check++;
printk(KERN_INFO "sendsig: current cpu share over limit of %i (check #%i)\n",
max_cpu_share, count_check);
/* the ratio is: if the process has a cpu share higher than
max_cpu_share for more than max_checks * wait_timeout seconds, then
we'll send the signal sig_to_send to it
*/
if (count_check >= max_checks) {
/*
sending the signal to the process
*/
signal_send(check_task);
/*
remove the timer
*/
del_timer(&check_timer);
printk(KERN_INFO "sendsig: sent signal to process %i, timer removed\n", pid);
return;
}
} else {
/*
if the process is being good, let's reset its counter
*/
count_check = 0;
}
/*
update the timer
*/
mod_timer(&check_timer, jiffies + wait_timeout * HZ);
return;
}
static ssize_t write_pid(struct file *file, const char __user *buf,
size_t count, loff_t *ppos)
{
char mybuf[10];
if(unlikely(count > 10))
return -EINVAL;
/*
let's see if a timer already exists.
*/
if (unlikely(check_timer.expires)) {
del_timer(&check_timer); /*... delete it */
printk(KERN_DEBUG "sendsig: initialization removed previous timer active on pid %i\n", pid);
}
copy_from_user(mybuf, buf, count);
sscanf(mybuf, "%d", &pid);
printk(KERN_INFO "sendsig: got pid = %d. Checking it every %i seconds, after timer initialization\n",
pid, wait_timeout);
/*
get the task struct to check
*/
check_task = get_check_task(pid);
if (unlikely(check_task == NULL)) {
printk(KERN_INFO "sendsig: can't check non-existent process, exiting\n");
return -ENODEV;
}
/*
update to zero the value of the last cputime usage
*/
last_cputime = 0;
/*
update to zero the value of the check counter
*/
count_check = 0;
/*
install the new timer
*/
init_timer(&check_timer);
check_timer.function = timer_function;
check_timer.expires = jiffies + wait_timeout*HZ;
add_timer(&check_timer);
return count;
}
static const struct file_operations sendsig_fops = {
.write = write_pid,
};
static int __init sendsig_module_init(void)
{
file = debugfs_create_file("sendsig", 0200, NULL, NULL, &sendsig_fops);
printk(KERN_INFO "Module sendsig loaded\n");
return 0;
}
static void __exit sendsig_module_exit(void)
{
del_timer(&check_timer);
debugfs_remove(file);
printk("Module sendsig unloaded\n");
}
module_init(sendsig_module_init);
module_exit(sendsig_module_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR(MOD_AUTHOR);
MODULE_DESCRIPTION(MOD_DESC);