summaryrefslogtreecommitdiffstats
path: root/toolbox/kill.c
blob: 4d0e47999c261c7e15289973c8d48c802fdbd006 (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#include <sys/types.h>
#include <signal.h>

int kill_main(int argc, char **argv)
{
    int sig = SIGTERM;
    int result = 0;
    
    argc--;
    argv++;

    if(argc >= 2 && argv[0][0] == '-'){
        sig = atoi(argv[0] + 1);
        argc--;
        argv++;
    }

    while(argc > 0){
        int pid = atoi(argv[0]);
        int err = kill(pid, sig);
        if (err < 0) {
            result = err;
            fprintf(stderr, "could not kill pid %d: %s\n", pid, strerror(errno));
        }
            
        argc--;
        argv++;
    }
    
    return result;
}