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