aboutsummaryrefslogtreecommitdiff
path: root/sinit.c
blob: 0bb65038cd43204a024564c882d6742bf7c9929e (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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* See LICENSE file for copyright and license details. */

#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/signalfd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "util.h"

typedef union {
        const void *v;
} Arg;

typedef struct {
        const char *name;
        void (*func)(const Arg *arg);
        const Arg arg;
} Command;

volatile sig_atomic_t signum;

typedef struct {
	int sig;
	void (*func)(void);
} Sigmap;

static void dispatchcmd(int);
static void sigfifo(void);
static void sigreap(void);
static void sigreboot(void);
static void spawn(const Arg *);

static Sigmap dispatchsig[] = {
	{ SIGHUP,  sigfifo   },
	{ SIGCHLD, sigreap   },
	{ SIGINT,  sigreboot },
};

static int sigfd = -1;
static int fifofd = -1;

#include "config.h"

int
main(void)
{
	struct signalfd_siginfo siginfo;
	sigset_t sigset;
	int maxfd, i, ret;
	ssize_t n;
	fd_set rfds;

	if (getpid() != 1)
		return EXIT_FAILURE;
	setsid();

	sigemptyset(&sigset);
	for (i = 0; i < LEN(dispatchsig); i++)
		sigaddset(&sigset, dispatchsig[i].sig);
	sigprocmask(SIG_BLOCK, &sigset, NULL);

	sigfd = signalfd(-1, &sigset, 0);
	if (sigfd < 0)
		eprintf("sinit: signalfd:");

	spawn(&(Arg){ .v = rcinitcmd });

	while (1) {
		FD_ZERO(&rfds);
		FD_SET(sigfd, &rfds);
		maxfd = sigfd;
		if (fifofd != -1) {
			FD_SET(fifofd, &rfds);
			if (fifofd > maxfd)
				maxfd = fifofd;
		}
		ret = select(maxfd + 1, &rfds, NULL, NULL, NULL);
		if (ret < 0)
			eprintf("sinit: select:");
		if (ret > 0) {
			if (FD_ISSET(sigfd, &rfds)) {
				n = read(sigfd, &siginfo, sizeof(siginfo));
				if (n <= 0)
					continue;
				for (i = 0; i < LEN(dispatchsig); i++)
					if (dispatchsig[i].sig == siginfo.ssi_signo)
						dispatchsig[i].func();
			}
			if (fifofd != -1)
				if (FD_ISSET(fifofd, &rfds))
					dispatchcmd(fifofd);
		}
	}

	return EXIT_SUCCESS;
}

static void
dispatchcmd(int fd)
{
	int i;
	char buf[BUFSIZ], *p;
	ssize_t n;

	n = read(fd, buf, sizeof(buf) - 1);
	if (n < 0)
		weprintf("sinit: read:");
	buf[n] = '\0';
	p = strchr(buf, '\n');
	if (p)
		*p = '\0';
	for (i = 0; i < LEN(commands); i++) {
		if (strcmp(commands[i].name, buf) == 0) {
			commands[i].func(&commands[i].arg);
			break;
		}
	}
}

static void
sigfifo(void)
{
	if (!fifopath)
		return;
	if (fifofd != -1)
		close(fifofd);
	unlink(fifopath);
	umask(0);
	if (mkfifo(fifopath, 0600) < 0)
		weprintf("sinit: mkfifo %s:", fifopath);
	fifofd = open(fifopath, O_RDWR | O_NONBLOCK);
	if (fifofd < 0)
		weprintf("sinit: open %s:", fifopath);
}

static void
sigreap(void)
{
	while (waitpid(-1, NULL, WNOHANG) > 0)
		;
}

static void
sigreboot(void)
{
	spawn(&(Arg){ .v = rcrebootcmd });
}

static void
spawn(const Arg *arg)
{
	int status;
	pid_t pid;
	char *const *p = arg->v;

	pid = fork();
	if (pid < 0) {
		weprintf("sinit: fork:");
	} else if (pid == 0) {
		pid = fork();
		if (pid < 0)
			weprintf("sinit: fork:");
		else if (pid > 0)
			exit(0);
		setsid();
		setpgid(0, 0);
		execvp(*p, p);
		weprintf("sinit: execvp %s:", p);
		_exit(errno == ENOENT ? 127 : 126);
	}
	waitpid(pid, &status, 0);
}