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