chiark / gitweb /
selinux: firmware - do not label files in runtime dir
[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/socket.h>
20 #include <sys/un.h>
21
22 #include "libudev.h"
23 #include "libudev-private.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_SETTLE,
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 *uctrl;
52         struct udev_ctrl_msg_wire ctrl_msg_wire;
53         pid_t pid;
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 };
63
64 static struct udev_ctrl *udev_ctrl_new(struct udev *udev)
65 {
66         struct udev_ctrl *uctrl;
67
68         uctrl = calloc(1, sizeof(struct udev_ctrl));
69         if (uctrl == NULL)
70                 return NULL;
71         uctrl->refcount = 1;
72         uctrl->udev = udev;
73         return uctrl;
74 }
75
76 struct udev_ctrl *udev_ctrl_new_from_socket(struct udev *udev, const char *socket_path)
77 {
78         struct udev_ctrl *uctrl;
79
80         uctrl = udev_ctrl_new(udev);
81         if (uctrl == NULL)
82                 return NULL;
83
84         uctrl->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
85         if (uctrl->sock < 0) {
86                 err(udev, "error getting socket: %m\n");
87                 udev_ctrl_unref(uctrl);
88                 return NULL;
89         }
90
91         uctrl->saddr.sun_family = AF_LOCAL;
92         strcpy(uctrl->saddr.sun_path, socket_path);
93         uctrl->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(uctrl->saddr.sun_path);
94         /* translate leading '@' to abstract namespace */
95         if (uctrl->saddr.sun_path[0] == '@')
96                 uctrl->saddr.sun_path[0] = '\0';
97         return uctrl;
98 }
99
100 struct udev_ctrl *udev_ctrl_new_from_fd(struct udev *udev, int fd)
101 {
102         struct udev_ctrl *uctrl;
103
104         uctrl = udev_ctrl_new(udev);
105         if (uctrl == NULL)
106                 return NULL;
107         uctrl->sock = fd;
108
109         return uctrl;
110 }
111
112 int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl)
113 {
114         int err;
115         const int on = 1;
116
117         if (uctrl->addrlen > 0) {
118                 err = bind(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
119                 if (err < 0) {
120                         err(uctrl->udev, "bind failed: %m\n");
121                         return err;
122                 }
123         }
124
125         /* enable receiving of the sender credentials */
126         setsockopt(uctrl->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
127         return 0;
128 }
129
130 struct udev *udev_ctrl_get_udev(struct udev_ctrl *uctrl)
131 {
132         return uctrl->udev;
133 }
134
135 struct udev_ctrl *udev_ctrl_ref(struct udev_ctrl *uctrl)
136 {
137         if (uctrl == NULL)
138                 return NULL;
139         uctrl->refcount++;
140         return uctrl;
141 }
142
143 void udev_ctrl_unref(struct udev_ctrl *uctrl)
144 {
145         if (uctrl == NULL)
146                 return;
147         uctrl->refcount--;
148         if (uctrl->refcount > 0)
149                 return;
150         if (uctrl->sock >= 0)
151                 close(uctrl->sock);
152         free(uctrl);
153 }
154
155 int udev_ctrl_get_fd(struct udev_ctrl *uctrl)
156 {
157         if (uctrl == NULL)
158                 return -1;
159         return uctrl->sock;
160 }
161
162 static int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf)
163 {
164         struct udev_ctrl_msg_wire ctrl_msg_wire;
165         int err;
166
167         memset(&ctrl_msg_wire, 0x00, sizeof(struct udev_ctrl_msg_wire));
168         strcpy(ctrl_msg_wire.version, "udev-" VERSION);
169         ctrl_msg_wire.magic = UDEV_CTRL_MAGIC;
170         ctrl_msg_wire.type = type;
171
172         if (buf != NULL)
173                 util_strscpy(ctrl_msg_wire.buf, sizeof(ctrl_msg_wire.buf), buf);
174         else
175                 ctrl_msg_wire.intval = intval;
176
177         err = sendto(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0,
178                      (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
179         if (err == -1) {
180                 err(uctrl->udev, "error sending message: %m\n");
181         }
182         return err;
183 }
184
185 int udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority)
186 {
187         return ctrl_send(uctrl, UDEV_CTRL_SET_LOG_LEVEL, priority, NULL);
188 }
189
190 int udev_ctrl_send_stop_exec_queue(struct udev_ctrl *uctrl)
191 {
192         return ctrl_send(uctrl, UDEV_CTRL_STOP_EXEC_QUEUE, 0, NULL);
193 }
194
195 int udev_ctrl_send_start_exec_queue(struct udev_ctrl *uctrl)
196 {
197         return ctrl_send(uctrl, UDEV_CTRL_START_EXEC_QUEUE, 0, NULL);
198 }
199
200 int udev_ctrl_send_reload_rules(struct udev_ctrl *uctrl)
201 {
202         return ctrl_send(uctrl, UDEV_CTRL_RELOAD_RULES, 0, NULL);
203 }
204
205 int udev_ctrl_send_set_env(struct udev_ctrl *uctrl, const char *key)
206 {
207         return ctrl_send(uctrl, UDEV_CTRL_SET_ENV, 0, key);
208 }
209
210 int udev_ctrl_send_set_children_max(struct udev_ctrl *uctrl, int count)
211 {
212         return ctrl_send(uctrl, UDEV_CTRL_SET_CHILDREN_MAX, count, NULL);
213 }
214
215 int udev_ctrl_send_settle(struct udev_ctrl *uctrl)
216 {
217         return ctrl_send(uctrl, UDEV_CTRL_SETTLE, 0, NULL);
218 }
219
220 struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl *uctrl)
221 {
222         struct udev_ctrl_msg *uctrl_msg;
223         ssize_t size;
224         struct msghdr smsg;
225         struct cmsghdr *cmsg;
226         struct iovec iov;
227         struct ucred *cred;
228         char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
229
230         uctrl_msg = calloc(1, sizeof(struct udev_ctrl_msg));
231         if (uctrl_msg == NULL)
232                 return NULL;
233         uctrl_msg->refcount = 1;
234         uctrl_msg->uctrl = uctrl;
235
236         iov.iov_base = &uctrl_msg->ctrl_msg_wire;
237         iov.iov_len = sizeof(struct udev_ctrl_msg_wire);
238
239         memset(&smsg, 0x00, sizeof(struct msghdr));
240         smsg.msg_iov = &iov;
241         smsg.msg_iovlen = 1;
242         smsg.msg_control = cred_msg;
243         smsg.msg_controllen = sizeof(cred_msg);
244
245         size = recvmsg(uctrl->sock, &smsg, 0);
246         if (size <  0) {
247                 err(uctrl->udev, "unable to receive user udevd message: %m\n");
248                 goto err;
249         }
250         cmsg = CMSG_FIRSTHDR(&smsg);
251         cred = (struct ucred *) CMSG_DATA(cmsg);
252
253         if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
254                 err(uctrl->udev, "no sender credentials received, message ignored\n");
255                 goto err;
256         }
257
258         if (cred->uid != 0) {
259                 err(uctrl->udev, "sender uid=%i, message ignored\n", cred->uid);
260                 goto err;
261         }
262
263         uctrl_msg->pid = cred->pid;
264
265         if (uctrl_msg->ctrl_msg_wire.magic != UDEV_CTRL_MAGIC) {
266                 err(uctrl->udev, "message magic 0x%08x doesn't match, ignore it\n", uctrl_msg->ctrl_msg_wire.magic);
267                 goto err;
268         }
269
270         dbg(uctrl->udev, "created ctrl_msg %p (%i)\n", uctrl_msg, uctrl_msg->ctrl_msg_wire.type);
271         return uctrl_msg;
272 err:
273         udev_ctrl_msg_unref(uctrl_msg);
274         return NULL;
275 }
276
277 struct udev_ctrl_msg *udev_ctrl_msg_ref(struct udev_ctrl_msg *ctrl_msg)
278 {
279         if (ctrl_msg == NULL)
280                 return NULL;
281         ctrl_msg->refcount++;
282         return ctrl_msg;
283 }
284
285 void udev_ctrl_msg_unref(struct udev_ctrl_msg *ctrl_msg)
286 {
287         if (ctrl_msg == NULL)
288                 return;
289         ctrl_msg->refcount--;
290         if (ctrl_msg->refcount > 0)
291                 return;
292         dbg(ctrl_msg->uctrl->udev, "release ctrl_msg %p\n", ctrl_msg);
293         free(ctrl_msg);
294 }
295
296 int udev_ctrl_get_set_log_level(struct udev_ctrl_msg *ctrl_msg)
297 {
298         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_LOG_LEVEL)
299                 return ctrl_msg->ctrl_msg_wire.intval;
300         return -1;
301 }
302
303 int udev_ctrl_get_stop_exec_queue(struct udev_ctrl_msg *ctrl_msg)
304 {
305         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_STOP_EXEC_QUEUE)
306                 return 1;
307         return -1;
308 }
309
310 int udev_ctrl_get_start_exec_queue(struct udev_ctrl_msg *ctrl_msg)
311 {
312         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_START_EXEC_QUEUE)
313                 return 1;
314         return -1;
315 }
316
317 int udev_ctrl_get_reload_rules(struct udev_ctrl_msg *ctrl_msg)
318 {
319         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_RELOAD_RULES)
320                 return 1;
321         return -1;
322 }
323
324 const char *udev_ctrl_get_set_env(struct udev_ctrl_msg *ctrl_msg)
325 {
326         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_ENV)
327                 return ctrl_msg->ctrl_msg_wire.buf;
328         return NULL;
329 }
330
331 int udev_ctrl_get_set_children_max(struct udev_ctrl_msg *ctrl_msg)
332 {
333         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_CHILDREN_MAX)
334                 return ctrl_msg->ctrl_msg_wire.intval;
335         return -1;
336 }
337
338 pid_t udev_ctrl_get_settle(struct udev_ctrl_msg *ctrl_msg)
339 {
340         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SETTLE)
341                 return ctrl_msg->pid;
342         return -1;
343 }