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