-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhotplugd.c
184 lines (168 loc) · 4.09 KB
/
hotplugd.c
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
180
181
182
183
184
/*
hotplugd reads kernel uevents via netlink and for each one spawns
the /etc/hotplug script with an environment consisting of the uevent
keys and values.
After setting up the netlink socket, hotplugd does a double fork
to execute an orphaned /libexec/hotplugd-trigger, which walks
/sys/devices, triggering "add" uevents for each one.
*/
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdnoreturn.h>
#include <string.h>
#include <fcntl.h>
#include <spawn.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <linux/netlink.h>
#ifndef PREFIX
#define PREFIX
#endif
#define HOTPLUG PREFIX "/etc/hotplug"
#define TRIGGER PREFIX "/libexec/hotplugd/trigger"
#define LEN(a) (sizeof(a) / sizeof((a)[0]))
static posix_spawn_file_actions_t fileactions;
static noreturn void
fatal(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
if (fmt[0] && fmt[strlen(fmt) - 1] == ':') {
fputc(' ', stderr);
perror(NULL);
} else {
fputc('\n', stderr);
}
exit(1);
}
static int
spawn(char *cmd, char *const env[])
{
int err, status;
pid_t pid;
err = posix_spawn(&pid, cmd, &fileactions, NULL, (char *[]){cmd, NULL}, env);
if (err) {
fprintf(stderr, "spawn %s: %s\n", cmd, strerror(err));
return -1;
}
if (waitpid(pid, &status, 0) == -1) {
fprintf(stderr, "wait %jd: %s\n", (intmax_t)pid, strerror(errno));
return -1;
}
if (!WIFEXITED(status))
return -1;
return WEXITSTATUS(status);
}
int
main(void)
{
extern char **environ;
int sock, err, status, rcvbuf = 8 * 1024 * 1024;
struct sockaddr_nl addr = {
.nl_family = AF_NETLINK,
.nl_groups = 1,
};
char buf[8192], *var, *eql, *nul, **envp, *env[256];
size_t envlen, envbase;
struct iovec iov = {
.iov_base = buf,
.iov_len = sizeof(buf),
};
struct msghdr msg = {
.msg_iov = &iov,
.msg_iovlen = 1,
};
struct iovec outiov = {
.iov_base = buf,
};
struct msghdr outmsg = {
.msg_name = &addr,
.msg_namelen = sizeof(addr),
.msg_iov = &outiov,
.msg_iovlen = 1,
};
ssize_t ret;
pid_t pid;
/* setup environment */
envlen = 0;
for (envp = environ; *envp; ++envp) {
if (strncmp(*envp, "PATH=", 5) == 0 ||
strncmp(*envp, "HOME=", 5) == 0)
{
env[envlen++] = *envp;
}
}
envbase = envlen;
/* create spawn file actions for child */
err = posix_spawn_file_actions_init(&fileactions);
if (err) {
perror(NULL);
return 1;
}
err = posix_spawn_file_actions_addopen(&fileactions, 0, "/dev/null", O_RDONLY, 0);
if (err) {
perror(NULL);
return 1;
}
/* open netlink socket */
sock = socket(AF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT);
if (sock == -1)
fatal("socket:");
if (setsockopt(sock, SOL_SOCKET, SO_RCVBUFFORCE, &rcvbuf, sizeof(rcvbuf)) == -1)
fatal("setsockopt:");
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1)
fatal("bind:");
/* trigger system uevents */
pid = fork();
if (pid == -1)
fatal("fork:");
if (pid == 0) {
err = posix_spawn(&pid, TRIGGER, NULL, NULL, (char *[]){TRIGGER, NULL}, environ);
if (err)
fatal("spawn %s:", TRIGGER);
return 0;
}
if (waitpid(pid, &status, 0) == -1)
fatal("waitpid %jd:", (intmax_t)pid);
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
return 1;
next:
/* set group mask to use for uevent rebroadcast (1 = kernel, 2 = udev) */
addr.nl_groups = 4;
for (;;) {
ret = recvmsg(sock, &msg, 0);
if (ret == 0)
break;
if (ret == -1)
fatal("recvmsg:");
if (msg.msg_flags & MSG_TRUNC)
continue;
envlen = envbase;
for (var = buf; var < buf + ret; var = nul + 1) {
nul = memchr(var, '\0', ret - (var - buf));
if (!nul) {
fprintf(stderr, "netlink message is not NULL-terminated\n");
goto next;
}
eql = strchr(var, '=');
if (!eql)
continue;
if (envlen == LEN(env) - 1) {
fprintf(stderr, "uevent has too many variables\n");
goto next;
}
env[envlen++] = var;
}
env[envlen] = NULL;
outiov.iov_len = ret;
if (spawn(HOTPLUG, env) == 0 && sendmsg(sock, &outmsg, 0) == -1)
fprintf(stderr, "uevent rebroadcast: %s", strerror(errno));
}
}