-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkiller.c
33 lines (28 loc) · 935 Bytes
/
killer.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
#define _POSIX_SOURCE
#include <pthread.h>
#include <unistd.h>
#include <signal.h>
#include "killer.h"
int kill_pid(pid_t pid) {
return kill(pid, SIGKILL);
}
void *timeout_killer(void *timeout_killer_args) {
// this is a new thread, kill the process if timeout
pid_t pid = ((struct timeout_killer_args *)timeout_killer_args)->pid;
int timeout = ((struct timeout_killer_args *)timeout_killer_args)->timeout;
// On success, pthread_detach() returns 0; on error, it returns an error number.
if (pthread_detach(pthread_self()) != 0) {
kill_pid(pid);
return NULL;
}
// usleep can't be used, for time args must < 1000ms
// this may sleep longer that expected, but we will have a check at the end
if (sleep((unsigned int)((timeout + 1000) / 1000)) != 0) {
kill_pid(pid);
return NULL;
}
if (kill_pid(pid) != 0) {
return NULL;
}
return NULL;
}