chiark / gitweb /
libudev: ctrl - change magic to integer
[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                         0xdead1dea
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 udev_ctrl_msg_wire {
48         char version[16];
49         unsigned int magic;
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 udev_ctrl_msg_wire ctrl_msg_wire;
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: %m\n");
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: %m\n");
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 udev_ctrl_msg_wire ctrl_msg_wire;
150         int err;
151
152         memset(&ctrl_msg_wire, 0x00, sizeof(struct udev_ctrl_msg_wire));
153         strcpy(ctrl_msg_wire.version, "udev-" VERSION);
154         ctrl_msg_wire.magic = UDEV_CTRL_MAGIC;
155         ctrl_msg_wire.type = type;
156
157         if (buf != NULL)
158                 util_strlcpy(ctrl_msg_wire.buf, buf, sizeof(ctrl_msg_wire.buf));
159         else
160                 ctrl_msg_wire.intval = intval;
161
162         err = sendto(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0,
163                      (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
164         if (err == -1) {
165                 err(uctrl->udev, "error sending message: %m\n");
166         }
167         return err;
168 }
169
170 int udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority)
171 {
172         ctrl_send(uctrl, UDEV_CTRL_SET_LOG_LEVEL, priority, NULL);
173         return 0;
174 }
175
176 int udev_ctrl_send_stop_exec_queue(struct udev_ctrl *uctrl)
177 {
178         ctrl_send(uctrl, UDEV_CTRL_STOP_EXEC_QUEUE, 0, NULL);
179         return 0;
180 }
181
182 int udev_ctrl_send_start_exec_queue(struct udev_ctrl *uctrl)
183 {
184         ctrl_send(uctrl, UDEV_CTRL_START_EXEC_QUEUE, 0, NULL);
185         return 0;
186 }
187
188 int udev_ctrl_send_reload_rules(struct udev_ctrl *uctrl)
189 {
190         ctrl_send(uctrl, UDEV_CTRL_RELOAD_RULES, 0, NULL);
191         return 0;
192 }
193
194 int udev_ctrl_send_set_env(struct udev_ctrl *uctrl, const char *key)
195 {
196         ctrl_send(uctrl, UDEV_CTRL_SET_ENV, 0, key);
197         return 0;
198 }
199
200 int udev_ctrl_send_set_max_childs(struct udev_ctrl *uctrl, int count)
201 {
202         ctrl_send(uctrl, UDEV_CTRL_SET_MAX_CHILDS, count, NULL);
203         return 0;
204 }
205
206 struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl *uctrl)
207 {
208         struct udev_ctrl_msg *uctrl_msg;
209         ssize_t size;
210         struct msghdr smsg;
211         struct cmsghdr *cmsg;
212         struct iovec iov;
213         struct ucred *cred;
214         char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
215
216         uctrl_msg = malloc(sizeof(struct udev_ctrl_msg));
217         if (uctrl_msg == NULL)
218                 return NULL;
219         memset(uctrl_msg, 0x00, sizeof(struct udev_ctrl_msg));
220         uctrl_msg->refcount = 1;
221         uctrl_msg->uctrl = uctrl;
222
223         iov.iov_base = &uctrl_msg->ctrl_msg_wire;
224         iov.iov_len = sizeof(struct udev_ctrl_msg_wire);
225
226         memset(&smsg, 0x00, sizeof(struct msghdr));
227         smsg.msg_iov = &iov;
228         smsg.msg_iovlen = 1;
229         smsg.msg_control = cred_msg;
230         smsg.msg_controllen = sizeof(cred_msg);
231
232         size = recvmsg(uctrl->sock, &smsg, 0);
233         if (size <  0) {
234                 err(uctrl->udev, "unable to receive user udevd message: %m\n");
235                 goto err;
236         }
237         cmsg = CMSG_FIRSTHDR(&smsg);
238         cred = (struct ucred *) CMSG_DATA(cmsg);
239
240         if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
241                 err(uctrl->udev, "no sender credentials received, message ignored\n");
242                 goto err;
243         }
244
245         if (cred->uid != 0) {
246                 err(uctrl->udev, "sender uid=%i, message ignored\n", cred->uid);
247                 goto err;
248         }
249
250         if (uctrl_msg->ctrl_msg_wire.magic != UDEV_CTRL_MAGIC) {
251                 err(uctrl->udev, "message magic 0x%08x doesn't match, ignore it\n", uctrl_msg->ctrl_msg_wire.magic);
252                 goto err;
253         }
254
255         info(uctrl->udev, "created ctrl_msg %p (%i)\n", uctrl_msg, uctrl_msg->ctrl_msg_wire.type);
256         return uctrl_msg;
257 err:
258         udev_ctrl_msg_unref(uctrl_msg);
259         return NULL;
260 }
261
262 struct udev_ctrl_msg *udev_ctrl_msg_ref(struct udev_ctrl_msg *ctrl_msg)
263 {
264         if (ctrl_msg == NULL)
265                 return NULL;
266         ctrl_msg->refcount++;
267         return ctrl_msg;
268 }
269
270 void udev_ctrl_msg_unref(struct udev_ctrl_msg *ctrl_msg)
271 {
272         if (ctrl_msg == NULL)
273                 return;
274         ctrl_msg->refcount--;
275         if (ctrl_msg->refcount > 0)
276                 return;
277         info(ctrl_msg->uctrl->udev, "release ctrl_msg %p\n", ctrl_msg);
278         free(ctrl_msg);
279 }
280
281 int udev_ctrl_get_set_log_level(struct udev_ctrl_msg *ctrl_msg)
282 {
283         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_LOG_LEVEL)
284                 return ctrl_msg->ctrl_msg_wire.intval;
285         return -1;
286 }
287
288 int udev_ctrl_get_stop_exec_queue(struct udev_ctrl_msg *ctrl_msg)
289 {
290         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_STOP_EXEC_QUEUE)
291                 return 1;
292         return -1;
293 }
294
295 int udev_ctrl_get_start_exec_queue(struct udev_ctrl_msg *ctrl_msg)
296 {
297         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_START_EXEC_QUEUE)
298                 return 1;
299         return -1;
300 }
301
302 int udev_ctrl_get_reload_rules(struct udev_ctrl_msg *ctrl_msg)
303 {
304         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_RELOAD_RULES)
305                 return 1;
306         return -1;
307 }
308
309 const char *udev_ctrl_get_set_env(struct udev_ctrl_msg *ctrl_msg)
310 {
311         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_ENV)
312                 return ctrl_msg->ctrl_msg_wire.buf;
313         return NULL;
314 }
315
316 int udev_ctrl_get_set_max_childs(struct udev_ctrl_msg *ctrl_msg)
317 {
318         if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_MAX_CHILDS)
319                 return ctrl_msg->ctrl_msg_wire.intval;
320         return -1;
321 }