chiark / gitweb /
importd: add new bus calls for importing local tar and raw images
[elogind.git] / src / udev / udev-ctrl.c
1 /*
2  * libudev - interface to udev device information
3  *
4  * Copyright (C) 2008 Kay Sievers <kay@vrfy.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  */
11
12 #include <errno.h>
13 #include <stdlib.h>
14 #include <stddef.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <poll.h>
18 #include <sys/socket.h>
19 #include <sys/un.h>
20
21 #include "udev.h"
22
23 /* wire protocol magic must match */
24 #define UDEV_CTRL_MAGIC                                0xdead1dea
25
26 enum udev_ctrl_msg_type {
27         UDEV_CTRL_UNKNOWN,
28         UDEV_CTRL_SET_LOG_LEVEL,
29         UDEV_CTRL_STOP_EXEC_QUEUE,
30         UDEV_CTRL_START_EXEC_QUEUE,
31         UDEV_CTRL_RELOAD,
32         UDEV_CTRL_SET_ENV,
33         UDEV_CTRL_SET_CHILDREN_MAX,
34         UDEV_CTRL_PING,
35         UDEV_CTRL_EXIT,
36 };
37
38 struct udev_ctrl_msg_wire {
39         char version[16];
40         unsigned int magic;
41         enum udev_ctrl_msg_type type;
42         union {
43                 int intval;
44                 char buf[256];
45         };
46 };
47
48 struct udev_ctrl_msg {
49         int refcount;
50         struct udev_ctrl_connection *conn;
51         struct udev_ctrl_msg_wire ctrl_msg_wire;
52 };
53
54 struct udev_ctrl {
55         int refcount;
56         struct udev *udev;
57         int sock;
58         struct sockaddr_un saddr;
59         socklen_t addrlen;
60         bool bound;
61         bool cleanup_socket;
62         bool connected;
63 };
64
65 struct udev_ctrl_connection {
66         int refcount;
67         struct udev_ctrl *uctrl;
68         int sock;
69 };
70
71 struct udev_ctrl *udev_ctrl_new_from_fd(struct udev *udev, int fd) {
72         struct udev_ctrl *uctrl;
73         const int on = 1;
74         int r;
75
76         uctrl = new0(struct udev_ctrl, 1);
77         if (uctrl == NULL)
78                 return NULL;
79         uctrl->refcount = 1;
80         uctrl->udev = udev;
81
82         if (fd < 0) {
83                 uctrl->sock = socket(AF_LOCAL, SOCK_SEQPACKET|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
84                 if (uctrl->sock < 0) {
85                         log_error_errno(errno, "error getting socket: %m");
86                         udev_ctrl_unref(uctrl);
87                         return NULL;
88                 }
89         } else {
90                 uctrl->bound = true;
91                 uctrl->sock = fd;
92         }
93         r = setsockopt(uctrl->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
94         if (r < 0)
95                 log_warning_errno(errno, "could not set SO_PASSCRED: %m");
96
97         uctrl->saddr.sun_family = AF_LOCAL;
98         strscpy(uctrl->saddr.sun_path, sizeof(uctrl->saddr.sun_path), "/run/udev/control");
99         uctrl->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(uctrl->saddr.sun_path);
100         return uctrl;
101 }
102
103 struct udev_ctrl *udev_ctrl_new(struct udev *udev) {
104         return udev_ctrl_new_from_fd(udev, -1);
105 }
106
107 int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl) {
108         int err;
109
110         if (!uctrl->bound) {
111                 err = bind(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
112                 if (err < 0 && errno == EADDRINUSE) {
113                         unlink(uctrl->saddr.sun_path);
114                         err = bind(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
115                 }
116
117                 if (err < 0) {
118                         err = -errno;
119                         log_error_errno(errno, "bind failed: %m");
120                         return err;
121                 }
122
123                 err = listen(uctrl->sock, 0);
124                 if (err < 0) {
125                         err = -errno;
126                         log_error_errno(errno, "listen failed: %m");
127                         return err;
128                 }
129
130                 uctrl->bound = true;
131                 uctrl->cleanup_socket = true;
132         }
133         return 0;
134 }
135
136 struct udev *udev_ctrl_get_udev(struct udev_ctrl *uctrl) {
137         return uctrl->udev;
138 }
139
140 static struct udev_ctrl *udev_ctrl_ref(struct udev_ctrl *uctrl) {
141         if (uctrl == NULL)
142                 return NULL;
143         uctrl->refcount++;
144         return uctrl;
145 }
146
147 struct udev_ctrl *udev_ctrl_unref(struct udev_ctrl *uctrl) {
148         if (uctrl == NULL)
149                 return NULL;
150         uctrl->refcount--;
151         if (uctrl->refcount > 0)
152                 return uctrl;
153         if (uctrl->sock >= 0)
154                 close(uctrl->sock);
155         free(uctrl);
156         return NULL;
157 }
158
159 int udev_ctrl_cleanup(struct udev_ctrl *uctrl) {
160         if (uctrl == NULL)
161                 return 0;
162         if (uctrl->cleanup_socket)
163                 unlink(uctrl->saddr.sun_path);
164         return 0;
165 }
166
167 int udev_ctrl_get_fd(struct udev_ctrl *uctrl) {
168         if (uctrl == NULL)
169                 return -EINVAL;
170         return uctrl->sock;
171 }
172
173 struct udev_ctrl_connection *udev_ctrl_get_connection(struct udev_ctrl *uctrl) {
174         struct udev_ctrl_connection *conn;
175         struct ucred ucred = {};
176         const int on = 1;
177         int r;
178
179         conn = new(struct udev_ctrl_connection, 1);
180         if (conn == NULL)
181                 return NULL;
182         conn->refcount = 1;
183         conn->uctrl = uctrl;
184
185         conn->sock = accept4(uctrl->sock, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK);
186         if (conn->sock < 0) {
187                 if (errno != EINTR)
188                         log_error_errno(errno, "unable to receive ctrl connection: %m");
189                 goto err;
190         }
191
192         /* check peer credential of connection */
193         r = getpeercred(conn->sock, &ucred);
194         if (r < 0) {
195                 log_error_errno(r, "unable to receive credentials of ctrl connection: %m");
196                 goto err;
197         }
198         if (ucred.uid > 0) {
199                 log_error("sender uid="UID_FMT", message ignored", ucred.uid);
200                 goto err;
201         }
202
203         /* enable receiving of the sender credentials in the messages */
204         r = setsockopt(conn->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
205         if (r < 0)
206                 log_warning_errno(errno, "could not set SO_PASSCRED: %m");
207
208         udev_ctrl_ref(uctrl);
209         return conn;
210 err:
211         if (conn->sock >= 0)
212                 close(conn->sock);
213         free(conn);
214         return NULL;
215 }
216
217 struct udev_ctrl_connection *udev_ctrl_connection_ref(struct udev_ctrl_connection *conn) {
218         if (conn == NULL)
219                 return NULL;
220         conn->refcount++;
221         return conn;
222 }
223
224 struct udev_ctrl_connection *udev_ctrl_connection_unref(struct udev_ctrl_connection *conn) {
225         if (conn == NULL)
226                 return NULL;
227         conn->refcount--;
228         if (conn->refcount > 0)
229                 return conn;
230         if (conn->sock >= 0)
231                 close(conn->sock);
232         udev_ctrl_unref(conn->uctrl);
233         free(conn);
234         return NULL;
235 }
236
237 static int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf, int timeout) {
238         struct udev_ctrl_msg_wire ctrl_msg_wire;
239         int err = 0;
240
241         memzero(&ctrl_msg_wire, sizeof(struct udev_ctrl_msg_wire));
242         strcpy(ctrl_msg_wire.version, "udev-" VERSION);
243         ctrl_msg_wire.magic = UDEV_CTRL_MAGIC;
244         ctrl_msg_wire.type = type;
245
246         if (buf != NULL)
247                 strscpy(ctrl_msg_wire.buf, sizeof(ctrl_msg_wire.buf), buf);
248         else
249                 ctrl_msg_wire.intval = intval;
250
251         if (!uctrl->connected) {
252                 if (connect(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen) < 0) {
253                         err = -errno;
254                         goto out;
255                 }
256                 uctrl->connected = true;
257         }
258         if (send(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0) < 0) {
259                 err = -errno;
260                 goto out;
261         }
262
263         /* wait for peer message handling or disconnect */
264         for (;;) {
265                 struct pollfd pfd[1];
266                 int r;
267
268                 pfd[0].fd = uctrl->sock;
269                 pfd[0].events = POLLIN;
270                 r = poll(pfd, 1, timeout * MSEC_PER_SEC);
271                 if (r  < 0) {
272                         if (errno == EINTR)
273                                 continue;
274                         err = -errno;
275                         break;
276                 }
277
278                 if (r > 0 && pfd[0].revents & POLLERR) {
279                         err = -EIO;
280                         break;
281                 }
282
283                 if (r == 0)
284                         err = -ETIMEDOUT;
285                 break;
286         }
287 out:
288         return err;
289 }
290
291 int udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority, int timeout) {
292         return ctrl_send(uctrl, UDEV_CTRL_SET_LOG_LEVEL, priority, NULL, timeout);
293 }
294
295 int udev_ctrl_send_stop_exec_queue(struct udev_ctrl *uctrl, int timeout) {
296         return ctrl_send(uctrl, UDEV_CTRL_STOP_EXEC_QUEUE, 0, NULL, timeout);
297 }
298
299 int udev_ctrl_send_start_exec_queue(struct udev_ctrl *uctrl, int timeout) {
300         return ctrl_send(uctrl, UDEV_CTRL_START_EXEC_QUEUE, 0, NULL, timeout);
301 }
302
303 int udev_ctrl_send_reload(struct udev_ctrl *uctrl, int timeout) {
304         return ctrl_send(uctrl, UDEV_CTRL_RELOAD, 0, NULL, timeout);
305 }
306
307 int udev_ctrl_send_set_env(struct udev_ctrl *uctrl, const char *key, int timeout) {
308         return ctrl_send(uctrl, UDEV_CTRL_SET_ENV, 0, key, timeout);
309 }
310
311 int udev_ctrl_send_set_children_max(struct udev_ctrl *uctrl, int count, int timeout) {
312         return ctrl_send(uctrl, UDEV_CTRL_SET_CHILDREN_MAX, count, NULL, timeout);
313 }
314
315 int udev_ctrl_send_ping(struct udev_ctrl *uctrl, int timeout) {
316         return ctrl_send(uctrl, UDEV_CTRL_PING, 0, NULL, timeout);
317 }
318
319 int udev_ctrl_send_exit(struct udev_ctrl *uctrl, int timeout) {
320         return ctrl_send(uctrl, UDEV_CTRL_EXIT, 0, NULL, timeout);
321 }
322
323 struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl_connection *conn) {
324         struct udev_ctrl_msg *uctrl_msg;
325         ssize_t size;
326         struct cmsghdr *cmsg;
327         struct iovec iov;
328         char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
329         struct msghdr smsg = {
330                 .msg_iov = &iov,
331                 .msg_iovlen = 1,
332                 .msg_control = cred_msg,
333                 .msg_controllen = sizeof(cred_msg),
334         };
335         struct ucred *cred;
336
337         uctrl_msg = new0(struct udev_ctrl_msg, 1);
338         if (uctrl_msg == NULL)
339                 return NULL;
340         uctrl_msg->refcount = 1;
341         uctrl_msg->conn = conn;
342         udev_ctrl_connection_ref(conn);
343
344         /* wait for the incoming message */
345         for (;;) {
346                 struct pollfd pfd[1];
347                 int r;
348
349                 pfd[0].fd = conn->sock;
350                 pfd[0].events = POLLIN;
351
352                 r = poll(pfd, 1, 10000);
353                 if (r  < 0) {
354                         if (errno == EINTR)
355                                 continue;
356                         goto err;
357                 } else if (r == 0) {
358                         log_error("timeout waiting for ctrl message");
359                         goto err;
360                 } else {
361                         if (!(pfd[0].revents & POLLIN)) {
362                                 log_error_errno(errno, "ctrl connection error: %m");
363                                 goto err;
364                         }
365                 }
366
367                 break;
368         }
369
370         iov.iov_base = &uctrl_msg->ctrl_msg_wire;
371         iov.iov_len = sizeof(struct udev_ctrl_msg_wire);
372
373         size = recvmsg(conn->sock, &smsg, 0);
374         if (size <  0) {
375                 log_error_errno(errno, "unable to receive ctrl message: %m");
376                 goto err;
377         }
378
379         cmsg_close_all(&smsg);
380
381         cmsg = CMSG_FIRSTHDR(&smsg);
382         cred = (struct ucred *) CMSG_DATA(cmsg);
383
384         if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
385                 log_error("no sender credentials received, message ignored");
386                 goto err;
387         }
388
389         if (cred->uid != 0) {
390                 log_error("sender uid="UID_FMT", message ignored", cred->uid);
391                 goto err;
392         }
393
394         if (uctrl_msg->ctrl_msg_wire.magic != UDEV_CTRL_MAGIC) {
395                 log_error("message magic 0x%08x doesn't match, ignore it", uctrl_msg->ctrl_msg_wire.magic);
396                 goto err;
397         }
398
399         return uctrl_msg;
400 err:
401         udev_ctrl_msg_unref(uctrl_msg);
402         return NULL;
403 }
404
405 struct udev_ctrl_msg *udev_ctrl_msg_unref(struct udev_ctrl_msg *ctrl_msg) {
406         if (ctrl_msg == NULL)
407                 return NULL;
408         ctrl_msg->refcount--;
409         if (ctrl_msg->refcount > 0)
410                 return ctrl_msg;
411         udev_ctrl_connection_unref(ctrl_msg->conn);
412         free(ctrl_msg);
413         return NULL;
414 }
415
416 int udev_ctrl_get_set_log_level(struct udev_ctrl_msg *ctrl_msg) {
417         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_LOG_LEVEL)
418                 return ctrl_msg->ctrl_msg_wire.intval;
419         return -1;
420 }
421
422 int udev_ctrl_get_stop_exec_queue(struct udev_ctrl_msg *ctrl_msg) {
423         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_STOP_EXEC_QUEUE)
424                 return 1;
425         return -1;
426 }
427
428 int udev_ctrl_get_start_exec_queue(struct udev_ctrl_msg *ctrl_msg) {
429         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_START_EXEC_QUEUE)
430                 return 1;
431         return -1;
432 }
433
434 int udev_ctrl_get_reload(struct udev_ctrl_msg *ctrl_msg) {
435         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_RELOAD)
436                 return 1;
437         return -1;
438 }
439
440 const char *udev_ctrl_get_set_env(struct udev_ctrl_msg *ctrl_msg) {
441         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_ENV)
442                 return ctrl_msg->ctrl_msg_wire.buf;
443         return NULL;
444 }
445
446 int udev_ctrl_get_set_children_max(struct udev_ctrl_msg *ctrl_msg) {
447         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_CHILDREN_MAX)
448                 return ctrl_msg->ctrl_msg_wire.intval;
449         return -1;
450 }
451
452 int udev_ctrl_get_ping(struct udev_ctrl_msg *ctrl_msg) {
453         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_PING)
454                 return 1;
455         return -1;
456 }
457
458 int udev_ctrl_get_exit(struct udev_ctrl_msg *ctrl_msg) {
459         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_EXIT)
460                 return 1;
461         return -1;
462 }