aboutsummaryrefslogtreecommitdiffstats
path: root/orphan-reaper.c
blob: bfc5a808411b56c708617f1ff55ac11d72aa800d (plain) (blame)
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
/* See LICENSE file for copyright and license details. */
#include <sys/prctl.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "arg.h"

#ifdef TEST
# define LIBEXECDIR "."
# define REAPD "reapd.test"
#else
# ifndef REAPD
#  define REAPD "reapd"
# endif
#endif


char *argv0;

static
void usage(void)
{
	fprintf(stderr, "usage: %s [-f] command ...\n", argv0);
	exit(1);
}

int
main(int argc, char *argv[])
{
	int fatal = 0;

	ARGBEGIN {
	case 'f':
		fatal = 1;
		break;
	default:
		usage();
	} ARGEND;

	if (!argc)
		usage();

	if (access(LIBEXECDIR"/"REAPD, X_OK) < 0) {
		fprintf(stderr, "%s: access %s: %s\n", argv0, LIBEXECDIR"/"REAPD, strerror(errno));
		return 2;
	}

	if (prctl(PR_SET_CHILD_SUBREAPER, 1) < 0) {
		fprintf(stderr, "%s: prctl PR_SET_CHILD_SUBREAPER: %s\n", argv0, strerror(errno));
		if (fatal)
			return 2;
		goto exec;
	}

	switch (fork()) {
	case -1:
		fprintf(stderr, "%s: fork: %s\n", argv0, strerror(errno));
		if (fatal)
			break;
		/* fall through */

	case 0:
		(void) prctl(PR_SET_CHILD_SUBREAPER, 0);
	exec:
		execvp(*argv, argv);
		fprintf(stderr, "%s: execvp %s: %s\n", argv0, *argv, strerror(errno));
		break;

	default:
		execl(LIBEXECDIR"/"REAPD, LIBEXECDIR"/"REAPD, NULL);
		fprintf(stderr, "%s: execl %s: %s\n", argv0, *argv, strerror(errno));
		break;
	}

	return 2;
}