summaryrefslogtreecommitdiffstats
path: root/nlsockd.c
blob: 95cd751ad90dd3c6f34ac6d3dbe1dc5babc864ab (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
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
/*
 * Copy me if you can.
 * by 20h
 *
 * Copyright (c) 2015 Natanael Copa <ncopa@alpinelinux.org>
 */

#include <err.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <sys/socket.h>
#include <sys/wait.h>

#include <linux/netlink.h>

#include "arg.h"

char *argv0;

pid_t
spawn_handler(int fd, char **argv)
{
	pid_t pid = fork();

	if (pid < 0)
		err(1, "fork");

	if (pid == 0) {
		if (fd > 0)
			dup2(fd, 0);
		execv(argv[0], argv);
		err(1, argv[0]);
	}
	return pid;
}

void
sighandler(int sig)
{
	switch(sig) {
	case SIGHUP:
	case SIGINT:
	case SIGQUIT:
	case SIGABRT:
	case SIGTERM:
		exit(0);
		break;
	default:
		break;
	}
}

void
initsignals(void)
{
	signal(SIGHUP, sighandler);
	signal(SIGINT, sighandler);
	signal(SIGQUIT, sighandler);
	signal(SIGABRT, sighandler);
	signal(SIGTERM, sighandler);

	signal(SIGPIPE, SIG_IGN);
}

int
init_netlink_socket(void)
{
	struct sockaddr_nl nls;
	int fd, slen;

	memset(&nls, 0, sizeof(nls));
	nls.nl_family = AF_NETLINK;
	nls.nl_pid = getpid();
	nls.nl_groups = -1;

	fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
	if (fd < 0)
		err(1, "socket");

	/* kernel will not create events bigger than 16kb, but we need
	   buffer up all events during coldplug */
	slen = 1024*1024;
	if (setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &slen,
				sizeof(slen)) < 0) {
		err(1, "setsockopt");
	}
	slen = 1;
	if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &slen,
				sizeof(slen)) < 0) {
		err(1, "setsockopt");
	}

	if (bind(fd, (void *)&nls, sizeof(nls)))
		err(1, "bind");

	fcntl(fd, F_SETFD, FD_CLOEXEC);
	return fd;
}

void
usage(void)
{
	errx(1, "usage: %s [-s] [-t triggerpath] runpath [...]\n", argv0);

}

int
main(int argc, char *argv[])
{
	struct pollfd fds;
	int single_run = 0;
	char *trigger_argv[3] = { NULL, NULL, NULL};
	pid_t childpid;
	int status, r;

	ARGBEGIN {
	case 's':
		single_run = 1;
		break;
	case 't':
		trigger_argv[0] = EARGF(usage());
		trigger_argv[1] = trigger_argv[0];
		break;
	default:
		usage();
	} ARGEND;

	if (argv[0] == NULL)
		usage();

	fds.events = POLLIN;
	fds.fd = init_netlink_socket();

	initsignals();
	if (trigger_argv[0]) {
		childpid = spawn_handler(0, trigger_argv);
		r = waitpid(childpid, &status, 0);
	}

	while (poll(&fds, 1, -1) > -1) {
		if (fds.revents & POLLIN) {
			childpid = spawn_handler(fds.fd, argv);
			r = waitpid(childpid, &status, 0);
			if(r == -1) {
				warn("waitpid(%d)", childpid);
			} else if (single_run && WIFEXITED(status)) {
				return WEXITSTATUS(status);
			}
		}
	}
	return 0;
}