]> git.ipfire.org Git - thirdparty/systemd.git/blob - udev/lib/libudev-ctrl.c
6d91ea4dbcca048a442f0d29547a2ab19e75f1a0
[thirdparty/systemd.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 "config.h"
21
22 #include <errno.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stddef.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <sys/un.h>
31
32 #include "../udev.h"
33 #include "libudev.h"
34 #include "libudev-private.h"
35
36 #define UDEV_CTRL_MAGIC "udevd-128"
37
38 enum udev_ctrl_msg_type {
39 UDEV_CTRL_UNKNOWN,
40 UDEV_CTRL_SET_LOG_LEVEL,
41 UDEV_CTRL_STOP_EXEC_QUEUE,
42 UDEV_CTRL_START_EXEC_QUEUE,
43 UDEV_CTRL_RELOAD_RULES,
44 UDEV_CTRL_SET_ENV,
45 UDEV_CTRL_SET_MAX_CHILDS,
46 UDEV_CTRL_SET_MAX_CHILDS_RUNNING,
47 };
48
49 struct ctrl_msg {
50 char magic[32];
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 ctrl_msg ctrl_msg;
62 };
63
64 struct udev_ctrl {
65 int refcount;
66 struct udev *udev;
67 int sock;
68 struct sockaddr_un saddr;
69 socklen_t addrlen;
70 };
71
72 struct udev_ctrl *udev_ctrl_new_from_socket(struct udev *udev, const char *socket_path)
73 {
74 struct udev_ctrl *uctrl;
75
76 uctrl = malloc(sizeof(struct udev_ctrl));
77 if (uctrl == NULL)
78 return NULL;
79 memset(uctrl, 0x00, sizeof(struct udev_ctrl));
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: %s\n", strerror(errno));
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: %s\n", strerror(errno));
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 ctrl_msg ctrl_msg;
151 int err;
152
153 memset(&ctrl_msg, 0x00, sizeof(struct ctrl_msg));
154 strcpy(ctrl_msg.magic, UDEV_CTRL_MAGIC);
155 ctrl_msg.type = type;
156
157 if (buf != NULL)
158 strlcpy(ctrl_msg.buf, buf, sizeof(ctrl_msg.buf));
159 else
160 ctrl_msg.intval = intval;
161
162 err = sendto(uctrl->sock, &ctrl_msg, sizeof(ctrl_msg), 0, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
163 if (err == -1) {
164 err(uctrl->udev, "error sending message: %s\n", strerror(errno));
165 }
166 return err;
167 }
168
169 int udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority)
170 {
171 ctrl_send(uctrl, UDEV_CTRL_SET_LOG_LEVEL, priority, NULL);
172 return 0;
173 }
174
175 int udev_ctrl_send_stop_exec_queue(struct udev_ctrl *uctrl)
176 {
177 ctrl_send(uctrl, UDEV_CTRL_STOP_EXEC_QUEUE, 0, NULL);
178 return 0;
179 }
180
181 int udev_ctrl_send_start_exec_queue(struct udev_ctrl *uctrl)
182 {
183 ctrl_send(uctrl, UDEV_CTRL_START_EXEC_QUEUE, 0, NULL);
184 return 0;
185 }
186
187 int udev_ctrl_send_reload_rules(struct udev_ctrl *uctrl)
188 {
189 ctrl_send(uctrl, UDEV_CTRL_RELOAD_RULES, 0, NULL);
190 return 0;
191 }
192
193 int udev_ctrl_send_set_env(struct udev_ctrl *uctrl, const char *key)
194 {
195 ctrl_send(uctrl, UDEV_CTRL_SET_ENV, 0, optarg);
196 return 0;
197 }
198
199 int udev_ctrl_send_set_max_childs(struct udev_ctrl *uctrl, int count)
200 {
201 ctrl_send(uctrl, UDEV_CTRL_SET_MAX_CHILDS, count, NULL);
202 return 0;
203 }
204
205 struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl *uctrl)
206 {
207 struct udev_ctrl_msg *uctrl_msg;
208 ssize_t size;
209 struct msghdr smsg;
210 struct cmsghdr *cmsg;
211 struct iovec iov;
212 struct ucred *cred;
213 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
214
215 uctrl_msg = malloc(sizeof(struct udev_ctrl_msg));
216 if (uctrl_msg == NULL)
217 return NULL;
218 memset(uctrl_msg, 0x00, sizeof(struct udev_ctrl_msg));
219 uctrl_msg->refcount = 1;
220 uctrl_msg->uctrl = uctrl;
221
222 iov.iov_base = &uctrl_msg->ctrl_msg;
223 iov.iov_len = sizeof(struct udev_ctrl_msg);
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: %s\n", strerror(errno));
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 if (strncmp(uctrl_msg->ctrl_msg.magic, UDEV_CTRL_MAGIC, sizeof(UDEV_CTRL_MAGIC)) != 0 ) {
250 err(uctrl->udev, "message magic '%s' doesn't match, ignore it\n", uctrl_msg->ctrl_msg.magic);
251 goto err;
252 }
253
254 info(uctrl->udev, "created ctrl_msg %p (%i)\n", uctrl_msg, uctrl_msg->ctrl_msg.type);
255 return uctrl_msg;
256 err:
257 udev_ctrl_msg_unref(uctrl_msg);
258 return NULL;
259 }
260
261 struct udev_ctrl_msg *udev_ctrl_msg_ref(struct udev_ctrl_msg *ctrl_msg)
262 {
263 if (ctrl_msg == NULL)
264 return NULL;
265 ctrl_msg->refcount++;
266 return ctrl_msg;
267 }
268
269 void udev_ctrl_msg_unref(struct udev_ctrl_msg *ctrl_msg)
270 {
271 if (ctrl_msg == NULL)
272 return;
273 ctrl_msg->refcount--;
274 if (ctrl_msg->refcount > 0)
275 return;
276 info(ctrl_msg->uctrl->udev, "release ctrl_msg %p\n", ctrl_msg);
277 free(ctrl_msg);
278 }
279
280 int udev_ctrl_get_set_log_level(struct udev_ctrl_msg *ctrl_msg)
281 {
282 if (ctrl_msg->ctrl_msg.type == UDEV_CTRL_SET_LOG_LEVEL)
283 return ctrl_msg->ctrl_msg.intval;
284 return -1;
285 }
286
287 int udev_ctrl_get_stop_exec_queue(struct udev_ctrl_msg *ctrl_msg)
288 {
289 if (ctrl_msg->ctrl_msg.type == UDEV_CTRL_STOP_EXEC_QUEUE)
290 return 1;
291 return -1;
292 }
293
294 int udev_ctrl_get_start_exec_queue(struct udev_ctrl_msg *ctrl_msg)
295 {
296 if (ctrl_msg->ctrl_msg.type == UDEV_CTRL_START_EXEC_QUEUE)
297 return 1;
298 return -1;
299 }
300
301 int udev_ctrl_get_reload_rules(struct udev_ctrl_msg *ctrl_msg)
302 {
303 if (ctrl_msg->ctrl_msg.type == UDEV_CTRL_RELOAD_RULES)
304 return 1;
305 return -1;
306 }
307
308 const char *udev_ctrl_get_set_env(struct udev_ctrl_msg *ctrl_msg)
309 {
310 if (ctrl_msg->ctrl_msg.type == UDEV_CTRL_SET_ENV)
311 return ctrl_msg->ctrl_msg.buf;
312 return NULL;
313 }
314
315 int udev_ctrl_get_set_max_childs(struct udev_ctrl_msg *ctrl_msg)
316 {
317 if (ctrl_msg->ctrl_msg.type == UDEV_CTRL_SET_MAX_CHILDS)
318 return ctrl_msg->ctrl_msg.intval;
319 return -1;
320 }