summaryrefslogtreecommitdiff
path: root/python/runner/terminator.c
blob: 0706957f10e71c3e9fd7b06898b2d5bbea1d62a1 (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
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>

int main(int argc, char* argv[])
{
	if (argc < 4) {
		fprintf(stderr, "usage: %s USERNAME PID SIGNAL\n", argv[0]);
		return 1;
	}

	// switch user (requires root or "setcap cap_setuid,cap_setgid+ep")
	char const* username = argv[1];
	struct passwd const* pw = getpwnam(username);
	if (!pw) {
		fprintf(stderr, "no such user: %s\n", username);
		return 1;
	}
	int ret = 0;
	if ((ret = setgid(pw->pw_gid)) != 0)
		fprintf(stderr, "setgid returned %d\n", ret);
	if ((ret = setuid(pw->pw_uid)) != 0)
		fprintf(stderr, "setuid returned %d\n", ret);

	pid_t pid = atol(argv[2]);
	int signum = atoi(argv[3]);
	kill(pid, signum);
	return 0;
}