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