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