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