chiark / gitweb /
libudev: ctrl - log accept4() errors
[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         const int on = 1;
180
181         conn = calloc(1, sizeof(struct udev_ctrl_connection));
182         if (conn == NULL)
183                 return NULL;
184         conn->refcount = 1;
185         conn->uctrl = uctrl;
186
187         conn->sock = accept4(uctrl->sock, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK);
188         if (conn->sock < 0) {
189                 if (errno != EINTR)
190                         err(uctrl->udev, "unable to receive ctrl connection: %m\n");
191                 free(conn);
192                 return NULL;
193         }
194
195         /* enable receiving of the sender credentials */
196         setsockopt(conn->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
197         udev_ctrl_ref(uctrl);
198         return conn;
199 }
200
201 struct udev_ctrl_connection *udev_ctrl_connection_ref(struct udev_ctrl_connection *conn)
202 {
203         if (conn == NULL)
204                 return NULL;
205         conn->refcount++;
206         return conn;
207 }
208
209 struct udev_ctrl_connection *udev_ctrl_connection_unref(struct udev_ctrl_connection *conn)
210 {
211         if (conn == NULL)
212                 return NULL;
213         conn->refcount--;
214         if (conn->refcount > 0)
215                 return conn;
216         if (conn->sock >= 0)
217                 close(conn->sock);
218         udev_ctrl_unref(conn->uctrl);
219         free(conn);
220         return NULL;
221 }
222
223 static int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf, int timeout)
224 {
225         struct udev_ctrl_msg_wire ctrl_msg_wire;
226         int err = 0;
227
228         memset(&ctrl_msg_wire, 0x00, sizeof(struct udev_ctrl_msg_wire));
229         strcpy(ctrl_msg_wire.version, "udev-" VERSION);
230         ctrl_msg_wire.magic = UDEV_CTRL_MAGIC;
231         ctrl_msg_wire.type = type;
232
233         if (buf != NULL)
234                 util_strscpy(ctrl_msg_wire.buf, sizeof(ctrl_msg_wire.buf), buf);
235         else
236                 ctrl_msg_wire.intval = intval;
237
238         if (!uctrl->connected) {
239                 if (connect(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen) < 0) {
240                         err = -errno;
241                         goto out;
242                 }
243                 uctrl->connected = true;
244         }
245         if (send(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0) < 0) {
246                 err = -errno;
247                 goto out;
248         }
249
250         /* wait for peer message handling or disconnect */
251         for (;;) {
252                 struct pollfd pfd[1];
253                 int r;
254
255                 pfd[0].fd = uctrl->sock;
256                 pfd[0].events = POLLIN;
257                 r = poll(pfd, 1, timeout * 1000);
258                 if (r  < 0) {
259                         if (errno == EINTR)
260                                 continue;
261                         err = -errno;
262                         break;
263                 }
264
265                 if (r > 0 && pfd[0].revents & POLLERR) {
266                         err = -EIO;
267                         break;
268                 }
269
270                 if (r == 0)
271                         err = -ETIMEDOUT;
272                 break;
273         }
274 out:
275         return err;
276 }
277
278 int udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority, int timeout)
279 {
280         return ctrl_send(uctrl, UDEV_CTRL_SET_LOG_LEVEL, priority, NULL, timeout);
281 }
282
283 int udev_ctrl_send_stop_exec_queue(struct udev_ctrl *uctrl, int timeout)
284 {
285         return ctrl_send(uctrl, UDEV_CTRL_STOP_EXEC_QUEUE, 0, NULL, timeout);
286 }
287
288 int udev_ctrl_send_start_exec_queue(struct udev_ctrl *uctrl, int timeout)
289 {
290         return ctrl_send(uctrl, UDEV_CTRL_START_EXEC_QUEUE, 0, NULL, timeout);
291 }
292
293 int udev_ctrl_send_reload_rules(struct udev_ctrl *uctrl, int timeout)
294 {
295         return ctrl_send(uctrl, UDEV_CTRL_RELOAD_RULES, 0, NULL, timeout);
296 }
297
298 int udev_ctrl_send_set_env(struct udev_ctrl *uctrl, const char *key, int timeout)
299 {
300         return ctrl_send(uctrl, UDEV_CTRL_SET_ENV, 0, key, timeout);
301 }
302
303 int udev_ctrl_send_set_children_max(struct udev_ctrl *uctrl, int count, int timeout)
304 {
305         return ctrl_send(uctrl, UDEV_CTRL_SET_CHILDREN_MAX, count, NULL, timeout);
306 }
307
308 int udev_ctrl_send_ping(struct udev_ctrl *uctrl, int timeout)
309 {
310         return ctrl_send(uctrl, UDEV_CTRL_PING, 0, NULL, timeout);
311 }
312
313 int udev_ctrl_send_exit(struct udev_ctrl *uctrl, int timeout)
314 {
315         return ctrl_send(uctrl, UDEV_CTRL_EXIT, 0, NULL, timeout);
316 }
317
318 struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl_connection *conn)
319 {
320         struct udev *udev = conn->uctrl->udev;
321         struct udev_ctrl_msg *uctrl_msg;
322         ssize_t size;
323         struct msghdr smsg;
324         struct cmsghdr *cmsg;
325         struct iovec iov;
326         struct ucred *cred;
327         char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
328
329         uctrl_msg = calloc(1, sizeof(struct udev_ctrl_msg));
330         if (uctrl_msg == NULL)
331                 return NULL;
332         uctrl_msg->refcount = 1;
333         uctrl_msg->conn = conn;
334         udev_ctrl_connection_ref(conn);
335
336         iov.iov_base = &uctrl_msg->ctrl_msg_wire;
337         iov.iov_len = sizeof(struct udev_ctrl_msg_wire);
338         memset(&smsg, 0x00, sizeof(struct msghdr));
339         smsg.msg_iov = &iov;
340         smsg.msg_iovlen = 1;
341         smsg.msg_control = cred_msg;
342         smsg.msg_controllen = sizeof(cred_msg);
343         size = recvmsg(conn->sock, &smsg, 0);
344         if (size <  0) {
345                 err(udev, "unable to receive user udevd message: %m\n");
346                 goto err;
347         }
348         cmsg = CMSG_FIRSTHDR(&smsg);
349         cred = (struct ucred *) CMSG_DATA(cmsg);
350
351         if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
352                 err(udev, "no sender credentials received, message ignored\n");
353                 goto err;
354         }
355
356         if (cred->uid != 0) {
357                 err(udev, "sender uid=%i, message ignored\n", cred->uid);
358                 goto err;
359         }
360
361         if (uctrl_msg->ctrl_msg_wire.magic != UDEV_CTRL_MAGIC) {
362                 err(udev, "message magic 0x%08x doesn't match, ignore it\n", uctrl_msg->ctrl_msg_wire.magic);
363                 goto err;
364         }
365
366         dbg(udev, "created ctrl_msg %p (%i)\n", uctrl_msg, uctrl_msg->ctrl_msg_wire.type);
367         return uctrl_msg;
368 err:
369         udev_ctrl_msg_unref(uctrl_msg);
370         return NULL;
371 }
372
373 struct udev_ctrl_msg *udev_ctrl_msg_ref(struct udev_ctrl_msg *ctrl_msg)
374 {
375         if (ctrl_msg == NULL)
376                 return NULL;
377         ctrl_msg->refcount++;
378         return ctrl_msg;
379 }
380
381 struct udev_ctrl_msg *udev_ctrl_msg_unref(struct udev_ctrl_msg *ctrl_msg)
382 {
383         if (ctrl_msg == NULL)
384                 return NULL;
385         ctrl_msg->refcount--;
386         if (ctrl_msg->refcount > 0)
387                 return ctrl_msg;
388         dbg(ctrl_msg->conn->uctrl->udev, "release ctrl_msg %p\n", ctrl_msg);
389         udev_ctrl_connection_unref(ctrl_msg->conn);
390         free(ctrl_msg);
391         return NULL;
392 }
393
394 int udev_ctrl_get_set_log_level(struct udev_ctrl_msg *ctrl_msg)
395 {
396         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_LOG_LEVEL)
397                 return ctrl_msg->ctrl_msg_wire.intval;
398         return -1;
399 }
400
401 int udev_ctrl_get_stop_exec_queue(struct udev_ctrl_msg *ctrl_msg)
402 {
403         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_STOP_EXEC_QUEUE)
404                 return 1;
405         return -1;
406 }
407
408 int udev_ctrl_get_start_exec_queue(struct udev_ctrl_msg *ctrl_msg)
409 {
410         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_START_EXEC_QUEUE)
411                 return 1;
412         return -1;
413 }
414
415 int udev_ctrl_get_reload_rules(struct udev_ctrl_msg *ctrl_msg)
416 {
417         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_RELOAD_RULES)
418                 return 1;
419         return -1;
420 }
421
422 const char *udev_ctrl_get_set_env(struct udev_ctrl_msg *ctrl_msg)
423 {
424         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_ENV)
425                 return ctrl_msg->ctrl_msg_wire.buf;
426         return NULL;
427 }
428
429 int udev_ctrl_get_set_children_max(struct udev_ctrl_msg *ctrl_msg)
430 {
431         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_CHILDREN_MAX)
432                 return ctrl_msg->ctrl_msg_wire.intval;
433         return -1;
434 }
435
436 int udev_ctrl_get_ping(struct udev_ctrl_msg *ctrl_msg)
437 {
438         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_PING)
439                 return 1;
440         return -1;
441 }
442
443 int udev_ctrl_get_exit(struct udev_ctrl_msg *ctrl_msg)
444 {
445         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_EXIT)
446                 return 1;
447         return -1;
448 }