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