chiark / gitweb /
ca8b845b94ff8367a478c5e8dcd179ec9f2e6f8f
[elogind.git] / udev / lib / 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 program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <sys/un.h>
29
30 #include "libudev.h"
31 #include "libudev-private.h"
32
33 /* last known version with this wire protocol */
34 #define UDEV_CTRL_MAGIC                         0xdead1dea
35
36 enum udev_ctrl_msg_type {
37         UDEV_CTRL_UNKNOWN,
38         UDEV_CTRL_SET_LOG_LEVEL,
39         UDEV_CTRL_STOP_EXEC_QUEUE,
40         UDEV_CTRL_START_EXEC_QUEUE,
41         UDEV_CTRL_RELOAD_RULES,
42         UDEV_CTRL_SET_ENV,
43         UDEV_CTRL_SET_MAX_CHILDS,
44         UDEV_CTRL_SET_MAX_CHILDS_RUNNING,
45         UDEV_CTRL_SETTLE,
46 };
47
48 struct udev_ctrl_msg_wire {
49         char version[16];
50         unsigned int magic;
51         enum udev_ctrl_msg_type type;
52         union {
53                 int intval;
54                 char buf[256];
55         };
56 };
57
58 struct udev_ctrl_msg {
59         int refcount;
60         struct udev_ctrl *uctrl;
61         struct udev_ctrl_msg_wire ctrl_msg_wire;
62         pid_t pid;
63 };
64
65 struct udev_ctrl {
66         int refcount;
67         struct udev *udev;
68         int sock;
69         struct sockaddr_un saddr;
70         socklen_t addrlen;
71 };
72
73 struct udev_ctrl *udev_ctrl_new_from_socket(struct udev *udev, const char *socket_path)
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
83         uctrl->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
84         if (uctrl->sock < 0) {
85                 err(udev, "error getting socket: %m\n");
86                 udev_ctrl_unref(uctrl);
87                 return NULL;
88         }
89
90         uctrl->saddr.sun_family = AF_LOCAL;
91         strcpy(uctrl->saddr.sun_path, socket_path);
92         uctrl->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(uctrl->saddr.sun_path);
93         /* translate leading '@' to abstract namespace */
94         if (uctrl->saddr.sun_path[0] == '@')
95                 uctrl->saddr.sun_path[0] = '\0';
96
97         return uctrl;
98 }
99
100 int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl)
101 {
102         int err;
103         const int feature_on = 1;
104
105         err= bind(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
106         if (err < 0) {
107                 err(uctrl->udev, "bind failed: %m\n");
108                 return err;
109         }
110
111         /* enable receiving of the sender credentials */
112         setsockopt(uctrl->sock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
113         return 0;
114 }
115
116 struct udev *udev_ctrl_get_udev(struct udev_ctrl *uctrl)
117 {
118         return uctrl->udev;
119 }
120
121 struct udev_ctrl *udev_ctrl_ref(struct udev_ctrl *uctrl)
122 {
123         if (uctrl == NULL)
124                 return NULL;
125         uctrl->refcount++;
126         return uctrl;
127 }
128
129 void udev_ctrl_unref(struct udev_ctrl *uctrl)
130 {
131         if (uctrl == NULL)
132                 return;
133         uctrl->refcount--;
134         if (uctrl->refcount > 0)
135                 return;
136         if (uctrl->sock >= 0)
137                 close(uctrl->sock);
138         free(uctrl);
139 }
140
141 int udev_ctrl_get_fd(struct udev_ctrl *uctrl)
142 {
143         if (uctrl == NULL)
144                 return -1;
145         return uctrl->sock;
146 }
147
148 static int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf)
149 {
150         struct udev_ctrl_msg_wire ctrl_msg_wire;
151         int err;
152
153         memset(&ctrl_msg_wire, 0x00, sizeof(struct udev_ctrl_msg_wire));
154         strcpy(ctrl_msg_wire.version, "udev-" VERSION);
155         ctrl_msg_wire.magic = UDEV_CTRL_MAGIC;
156         ctrl_msg_wire.type = type;
157
158         if (buf != NULL)
159                 util_strlcpy(ctrl_msg_wire.buf, buf, sizeof(ctrl_msg_wire.buf));
160         else
161                 ctrl_msg_wire.intval = intval;
162
163         err = sendto(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0,
164                      (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
165         if (err == -1) {
166                 err(uctrl->udev, "error sending message: %m\n");
167         }
168         return err;
169 }
170
171 int udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority)
172 {
173         return ctrl_send(uctrl, UDEV_CTRL_SET_LOG_LEVEL, priority, NULL);
174 }
175
176 int udev_ctrl_send_stop_exec_queue(struct udev_ctrl *uctrl)
177 {
178         return ctrl_send(uctrl, UDEV_CTRL_STOP_EXEC_QUEUE, 0, NULL);
179 }
180
181 int udev_ctrl_send_start_exec_queue(struct udev_ctrl *uctrl)
182 {
183         return ctrl_send(uctrl, UDEV_CTRL_START_EXEC_QUEUE, 0, NULL);
184 }
185
186 int udev_ctrl_send_reload_rules(struct udev_ctrl *uctrl)
187 {
188         return ctrl_send(uctrl, UDEV_CTRL_RELOAD_RULES, 0, NULL);
189 }
190
191 int udev_ctrl_send_set_env(struct udev_ctrl *uctrl, const char *key)
192 {
193         return ctrl_send(uctrl, UDEV_CTRL_SET_ENV, 0, key);
194 }
195
196 int udev_ctrl_send_set_max_childs(struct udev_ctrl *uctrl, int count)
197 {
198         return ctrl_send(uctrl, UDEV_CTRL_SET_MAX_CHILDS, count, NULL);
199 }
200
201 int udev_ctrl_send_settle(struct udev_ctrl *uctrl)
202 {
203         return ctrl_send(uctrl, UDEV_CTRL_SETTLE, 0, NULL);
204 }
205
206 struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl *uctrl)
207 {
208         struct udev_ctrl_msg *uctrl_msg;
209         ssize_t size;
210         struct msghdr smsg;
211         struct cmsghdr *cmsg;
212         struct iovec iov;
213         struct ucred *cred;
214         char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
215
216         uctrl_msg = calloc(1, sizeof(struct udev_ctrl_msg));
217         if (uctrl_msg == NULL)
218                 return NULL;
219         uctrl_msg->refcount = 1;
220         uctrl_msg->uctrl = uctrl;
221
222         iov.iov_base = &uctrl_msg->ctrl_msg_wire;
223         iov.iov_len = sizeof(struct udev_ctrl_msg_wire);
224
225         memset(&smsg, 0x00, sizeof(struct msghdr));
226         smsg.msg_iov = &iov;
227         smsg.msg_iovlen = 1;
228         smsg.msg_control = cred_msg;
229         smsg.msg_controllen = sizeof(cred_msg);
230
231         size = recvmsg(uctrl->sock, &smsg, 0);
232         if (size <  0) {
233                 err(uctrl->udev, "unable to receive user udevd message: %m\n");
234                 goto err;
235         }
236         cmsg = CMSG_FIRSTHDR(&smsg);
237         cred = (struct ucred *) CMSG_DATA(cmsg);
238
239         if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
240                 err(uctrl->udev, "no sender credentials received, message ignored\n");
241                 goto err;
242         }
243
244         if (cred->uid != 0) {
245                 err(uctrl->udev, "sender uid=%i, message ignored\n", cred->uid);
246                 goto err;
247         }
248
249         uctrl_msg->pid = cred->pid;
250
251         if (uctrl_msg->ctrl_msg_wire.magic != UDEV_CTRL_MAGIC) {
252                 err(uctrl->udev, "message magic 0x%08x doesn't match, ignore it\n", uctrl_msg->ctrl_msg_wire.magic);
253                 goto err;
254         }
255
256         dbg(uctrl->udev, "created ctrl_msg %p (%i)\n", uctrl_msg, uctrl_msg->ctrl_msg_wire.type);
257         return uctrl_msg;
258 err:
259         udev_ctrl_msg_unref(uctrl_msg);
260         return NULL;
261 }
262
263 struct udev_ctrl_msg *udev_ctrl_msg_ref(struct udev_ctrl_msg *ctrl_msg)
264 {
265         if (ctrl_msg == NULL)
266                 return NULL;
267         ctrl_msg->refcount++;
268         return ctrl_msg;
269 }
270
271 void udev_ctrl_msg_unref(struct udev_ctrl_msg *ctrl_msg)
272 {
273         if (ctrl_msg == NULL)
274                 return;
275         ctrl_msg->refcount--;
276         if (ctrl_msg->refcount > 0)
277                 return;
278         dbg(ctrl_msg->uctrl->udev, "release ctrl_msg %p\n", ctrl_msg);
279         free(ctrl_msg);
280 }
281
282 int udev_ctrl_get_set_log_level(struct udev_ctrl_msg *ctrl_msg)
283 {
284         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_LOG_LEVEL)
285                 return ctrl_msg->ctrl_msg_wire.intval;
286         return -1;
287 }
288
289 int udev_ctrl_get_stop_exec_queue(struct udev_ctrl_msg *ctrl_msg)
290 {
291         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_STOP_EXEC_QUEUE)
292                 return 1;
293         return -1;
294 }
295
296 int udev_ctrl_get_start_exec_queue(struct udev_ctrl_msg *ctrl_msg)
297 {
298         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_START_EXEC_QUEUE)
299                 return 1;
300         return -1;
301 }
302
303 int udev_ctrl_get_reload_rules(struct udev_ctrl_msg *ctrl_msg)
304 {
305         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_RELOAD_RULES)
306                 return 1;
307         return -1;
308 }
309
310 const char *udev_ctrl_get_set_env(struct udev_ctrl_msg *ctrl_msg)
311 {
312         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_ENV)
313                 return ctrl_msg->ctrl_msg_wire.buf;
314         return NULL;
315 }
316
317 int udev_ctrl_get_set_max_childs(struct udev_ctrl_msg *ctrl_msg)
318 {
319         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_MAX_CHILDS)
320                 return ctrl_msg->ctrl_msg_wire.intval;
321         return -1;
322 }
323
324 pid_t udev_ctrl_get_settle(struct udev_ctrl_msg *ctrl_msg)
325 {
326         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SETTLE)
327                 return ctrl_msg->pid;
328         return -1;
329 }