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