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