]> git.ipfire.org Git - people/ms/systemd.git/blame - device.c
udev: introduce SYSTEMD_EXPOSE as binary udev property for hiding/showing devices...
[people/ms/systemd.git] / device.c
CommitLineData
5cb5a6ff
LP
1/*-*- Mode: C; c-basic-offset: 8 -*-*/
2
a7334b09
LP
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
25ac040b 22#include <errno.h>
f94ea366 23#include <sys/epoll.h>
25ac040b
LP
24#include <libudev.h>
25
87f0e418 26#include "unit.h"
5cb5a6ff
LP
27#include "device.h"
28#include "strv.h"
25ac040b 29#include "log.h"
5cb5a6ff 30
f50e0a01
LP
31static const UnitActiveState state_translation_table[_DEVICE_STATE_MAX] = {
32 [DEVICE_DEAD] = UNIT_INACTIVE,
33 [DEVICE_AVAILABLE] = UNIT_ACTIVE
34};
35
36static const char* const state_string_table[_DEVICE_STATE_MAX] = {
37 [DEVICE_DEAD] = "dead",
38 [DEVICE_AVAILABLE] = "available"
39};
40
87f0e418
LP
41static void device_done(Unit *u) {
42 Device *d = DEVICE(u);
034c6ed7
LP
43
44 assert(d);
25ac040b 45 free(d->sysfs);
034c6ed7
LP
46}
47
f50e0a01
LP
48static void device_set_state(Device *d, DeviceState state) {
49 DeviceState old_state;
50 assert(d);
5cb5a6ff 51
f50e0a01
LP
52 old_state = d->state;
53 d->state = state;
5cb5a6ff 54
f50e0a01
LP
55 log_debug("%s changed %s → %s", unit_id(UNIT(d)), state_string_table[old_state], state_string_table[state]);
56
57 unit_notify(UNIT(d), state_translation_table[old_state], state_translation_table[state]);
58}
59
60static int device_coldplug(Unit *u) {
61 Device *d = DEVICE(u);
62
63 assert(d);
64 assert(d->state == DEVICE_DEAD);
65
66 if (d->sysfs)
67 device_set_state(d, DEVICE_AVAILABLE);
68
69 return 0;
70}
71
72static void device_dump(Unit *u, FILE *f, const char *prefix) {
25ac040b 73 Device *d = DEVICE(u);
5cb5a6ff 74
25ac040b 75 assert(d);
5cb5a6ff
LP
76
77 fprintf(f,
25ac040b
LP
78 "%sDevice State: %s\n"
79 "%sSysfs Path: %s\n",
f50e0a01
LP
80 prefix, state_string_table[d->state],
81 prefix, strna(d->sysfs));
82}
83
84static UnitActiveState device_active_state(Unit *u) {
85 assert(u);
86
87 return state_translation_table[DEVICE(u)->state];
25ac040b
LP
88}
89
2e478a46 90static int device_add_escaped_name(Unit *u, const char *dn, bool make_id) {
25ac040b
LP
91 char *e;
92 int r;
93
94 assert(u);
95 assert(dn);
96 assert(dn[0] == '/');
97
2e478a46 98 if (!(e = unit_name_escape_path(dn+1, ".device")))
25ac040b
LP
99 return -ENOMEM;
100
101 r = unit_add_name(u, e);
b08d03ff
LP
102
103 if (r >= 0 && make_id)
104 unit_choose_id(u, e);
105
25ac040b
LP
106 free(e);
107
108 if (r < 0 && r != -EEXIST)
109 return r;
110
111 return 0;
112}
113
f94ea366 114static int device_process_new_device(Manager *m, struct udev_device *dev, bool update_state) {
7f275a9f 115 const char *dn, *names, *wants, *sysfs, *expose;
25ac040b
LP
116 Unit *u = NULL;
117 int r;
118 char *e, *w, *state;
119 size_t l;
120 bool delete;
121 struct udev_list_entry *item = NULL, *first = NULL;
122
123 assert(m);
124
7f275a9f
LP
125 if (!(sysfs = udev_device_get_syspath(dev)))
126 return -ENOMEM;
127
25ac040b
LP
128 /* Check whether this entry is even relevant for us. */
129 dn = udev_device_get_devnode(dev);
7f275a9f 130 expose = udev_device_get_property_value(dev, "SYSTEMD_EXPOSE");
25ac040b
LP
131 names = udev_device_get_property_value(dev, "SYSTEMD_NAMES");
132 wants = udev_device_get_property_value(dev, "SYSTEMD_WANTS");
133
7f275a9f
LP
134 if (expose) {
135 int b;
136
137 if ((b = parse_boolean(expose)) < 0) {
138 log_error("Failed to parse SYSTEMD_EXPOSE udev property for device %s: %s", sysfs, expose);
139 return 0;
140 }
141
142 if (!b)
143 return 0;
144 } else
145 if (!dn && !names && !wants)
146 return 0;
25ac040b
LP
147
148 /* Ok, seems kinda interesting. Now, let's see if this one
149 * already exists. */
150
25ac040b 151 assert(sysfs[0] == '/');
2e478a46 152 if (!(e = unit_name_escape_path(sysfs+1, ".device")))
25ac040b
LP
153 return -ENOMEM;
154
155 if (!(u = manager_get_unit(m, e))) {
156 const char *model;
157
158 delete = true;
159
160 if (!(u = unit_new(m))) {
161 free(e);
162 return -ENOMEM;
163 }
164
165 r = unit_add_name(u, e);
166 free(e);
167
168 if (r < 0)
169 goto fail;
170
171 if (!(DEVICE(u)->sysfs = strdup(sysfs))) {
172 r = -ENOMEM;
173 goto fail;
174 }
175
176 if ((model = udev_device_get_property_value(dev, "ID_MODEL_FROM_DATABASE")) ||
c18315a8 177 (model = udev_device_get_property_value(dev, "ID_MODEL"))) {
f50e0a01 178 if ((r = unit_set_description(u, model)) < 0)
25ac040b 179 goto fail;
c18315a8
LP
180 } else if (dn)
181 if ((r = unit_set_description(u, dn)) < 0)
182 goto fail;
25ac040b 183
f94ea366 184 unit_add_to_load_queue(u);
25ac040b
LP
185 } else {
186 delete = false;
187 free(e);
188 }
189
190 if (dn)
2e478a46 191 if ((r = device_add_escaped_name(u, dn, true)) < 0)
25ac040b
LP
192 goto fail;
193
194 first = udev_device_get_devlinks_list_entry(dev);
195 udev_list_entry_foreach(item, first)
2e478a46 196 if ((r = device_add_escaped_name(u, udev_list_entry_get_name(item), false)) < 0)
25ac040b
LP
197 goto fail;
198
199 if (names) {
200 FOREACH_WORD(w, l, names, state) {
b866264a
LP
201 if (!(e = strndup(w, l))) {
202 r = -ENOMEM;
25ac040b 203 goto fail;
b866264a 204 }
25ac040b
LP
205
206 r = unit_add_name(u, e);
207 free(e);
208
209 if (r < 0 && r != -EEXIST)
210 goto fail;
211 }
212 }
213
25ac040b
LP
214 if (wants) {
215 FOREACH_WORD(w, l, wants, state) {
b866264a
LP
216 if (!(e = strndup(w, l))) {
217 r = -ENOMEM;
25ac040b 218 goto fail;
b866264a 219 }
25ac040b
LP
220
221 r = unit_add_dependency_by_name(u, UNIT_WANTS, e);
222 free(e);
223
224 if (r < 0)
225 goto fail;
226 }
227 }
228
f94ea366
LP
229 if (update_state) {
230 manager_dispatch_load_queue(u->meta.manager);
231 device_set_state(DEVICE(u), DEVICE_AVAILABLE);
232 }
233
c1e1601e
LP
234 unit_add_to_dbus_queue(u);
235
25ac040b
LP
236 return 0;
237
238fail:
239 if (delete && u)
240 unit_free(u);
241 return r;
242}
243
f94ea366 244static int device_process_path(Manager *m, const char *path, bool update_state) {
25ac040b
LP
245 int r;
246 struct udev_device *dev;
247
248 assert(m);
249 assert(path);
250
251 if (!(dev = udev_device_new_from_syspath(m->udev, path))) {
252 log_warning("Failed to get udev device object from udev for path %s.", path);
253 return -ENOMEM;
254 }
255
f94ea366 256 r = device_process_new_device(m, dev, update_state);
25ac040b
LP
257 udev_device_unref(dev);
258 return r;
259}
260
f94ea366
LP
261static int device_process_removed_device(Manager *m, struct udev_device *dev) {
262 const char *sysfs;
263 char *e;
264 Unit *u;
265 Device *d;
266
267 assert(m);
268 assert(dev);
269
270 if (!(sysfs = udev_device_get_syspath(dev)))
271 return -ENOMEM;
272
273 assert(sysfs[0] == '/');
2e478a46 274 if (!(e = unit_name_escape_path(sysfs+1, ".device")))
f94ea366
LP
275 return -ENOMEM;
276
277 u = manager_get_unit(m, e);
278 free(e);
279
280 if (!u)
281 return 0;
282
283 d = DEVICE(u);
284 free(d->sysfs);
285 d->sysfs = NULL;
286
287 device_set_state(d, DEVICE_DEAD);
288 return 0;
289}
290
25ac040b
LP
291static void device_shutdown(Manager *m) {
292 assert(m);
293
f94ea366
LP
294 if (m->udev_monitor)
295 udev_monitor_unref(m->udev_monitor);
296
25ac040b
LP
297 if (m->udev)
298 udev_unref(m->udev);
299}
300
301static int device_enumerate(Manager *m) {
f94ea366 302 struct epoll_event ev;
25ac040b
LP
303 int r;
304 struct udev_enumerate *e = NULL;
305 struct udev_list_entry *item = NULL, *first = NULL;
306
307 assert(m);
308
309 if (!(m->udev = udev_new()))
310 return -ENOMEM;
311
f94ea366
LP
312 if (!(m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev"))) {
313 r = -ENOMEM;
314 goto fail;
315 }
316
317 if (udev_monitor_enable_receiving(m->udev_monitor) < 0) {
318 r = -EIO;
319 goto fail;
320 }
321
322 m->udev_watch.type = WATCH_UDEV;
323 m->udev_watch.fd = udev_monitor_get_fd(m->udev_monitor);
324
325 zero(ev);
326 ev.events = EPOLLIN;
327 ev.data.ptr = &m->udev_watch;
328
329 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->udev_watch.fd, &ev) < 0)
330 return -errno;
331
25ac040b
LP
332 if (!(e = udev_enumerate_new(m->udev))) {
333 r = -ENOMEM;
334 goto fail;
335 }
336
337 if (udev_enumerate_scan_devices(e) < 0) {
338 r = -EIO;
339 goto fail;
340 }
341
342 first = udev_enumerate_get_list_entry(e);
343 udev_list_entry_foreach(item, first)
f94ea366 344 device_process_path(m, udev_list_entry_get_name(item), false);
25ac040b
LP
345
346 udev_enumerate_unref(e);
25ac040b
LP
347 return 0;
348
349fail:
350 if (e)
351 udev_enumerate_unref(e);
352
353 device_shutdown(m);
354 return r;
5cb5a6ff
LP
355}
356
f94ea366
LP
357void device_fd_event(Manager *m, int events) {
358 struct udev_device *dev;
359 int r;
360 const char *action;
361
362 assert(m);
363 assert(events == EPOLLIN);
364
f94ea366
LP
365 if (!(dev = udev_monitor_receive_device(m->udev_monitor))) {
366 log_error("Failed to receive device.");
367 return;
368 }
369
370 if (!(action = udev_device_get_action(dev))) {
371 log_error("Failed to get udev action string.");
372 goto fail;
373 }
374
375 if (streq(action, "remove")) {
376 if ((r = device_process_removed_device(m, dev)) < 0) {
377 log_error("Failed to process udev device event: %s", strerror(-r));
378 goto fail;
379 }
380 } else {
381 if ((r = device_process_new_device(m, dev, true)) < 0) {
382 log_error("Failed to process udev device event: %s", strerror(-r));
383 goto fail;
384 }
385 }
386
387fail:
388 udev_device_unref(dev);
389}
390
87f0e418 391const UnitVTable device_vtable = {
5cb5a6ff
LP
392 .suffix = ".device",
393
23a177ef 394 .init = unit_load_fragment_and_dropin_optional,
034c6ed7 395 .done = device_done,
f50e0a01
LP
396 .coldplug = device_coldplug,
397
5cb5a6ff
LP
398 .dump = device_dump,
399
f50e0a01 400 .active_state = device_active_state,
25ac040b 401
f50e0a01
LP
402 .enumerate = device_enumerate,
403 .shutdown = device_shutdown
5cb5a6ff 404};