new tool
[hband-tools.git] / compiled-tools / kill-rogue.c
blobb0c1da4be7e0040dbfe0b62b8107ff104763c6d0
1 /*
2 * reaper.c: A process capable of killing `runner`
4 * Copyright (C) 2018 Citrix Systems UK Ltd
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, version 2 of the
9 * License.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02110-1301, USA.
21 #define _GNU_SOURCE
22 #include <sys/types.h>
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <err.h>
29 int main(int argc, char ** argv) {
30 argc--;
31 if (argc != 2)
33 errx(EXIT_FAILURE, "Usage: kill-rogue <rogue-uid> <reaper-uid>");
35 uid_t euid;
36 uid_t tuid = atoi(argv[1]);
37 uid_t xuid = atoi(argv[2]);
38 int rc;
40 if (tuid == 0 || xuid == 0)
42 errx(EXIT_FAILURE, "rouge-uid or reaper-uid is 0, which should not");
45 /* Check to make sure we have enough permissions */
46 euid = geteuid();
48 if ( euid != 0 ) {
49 errx(EXIT_FAILURE, "Must run as euid 0 to set uid");
52 /*
53 * Set euid to TARGET_UID so that we can kill the rogue 'runner'; but
54 * set ruid to REAPER_UID so that the rogue runner can't kill us.
56 if ( setresuid(xuid, tuid, 0) ) {
57 err(EXIT_FAILURE, "Setting uid to target");
60 rc = kill(-1, 9);
61 if ( rc )
62 err(EXIT_FAILURE, "No processes killed");
63 else
64 warnx("At least one process killed successfully");
66 return EXIT_SUCCESS;