chiark / gitweb /
39ab29110374058a125e94d064f7991219cf91d0
[elogind.git] / src / device.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
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
22 #include <errno.h>
23 #include <sys/epoll.h>
24 #include <libudev.h>
25
26 #include "unit.h"
27 #include "device.h"
28 #include "strv.h"
29 #include "log.h"
30 #include "unit-name.h"
31 #include "dbus-device.h"
32
33 static const UnitActiveState state_translation_table[_DEVICE_STATE_MAX] = {
34         [DEVICE_DEAD] = UNIT_INACTIVE,
35         [DEVICE_PLUGGED] = UNIT_ACTIVE
36 };
37
38 static void device_init(Unit *u) {
39         Device *d = DEVICE(u);
40
41         assert(d);
42         assert(d->meta.load_state == UNIT_STUB);
43
44         d->meta.job_timeout = DEFAULT_TIMEOUT_USEC;
45 }
46
47 static void device_done(Unit *u) {
48         Device *d = DEVICE(u);
49
50         assert(d);
51
52         free(d->sysfs);
53         d->sysfs = NULL;
54 }
55
56 static void device_set_state(Device *d, DeviceState state) {
57         DeviceState old_state;
58         assert(d);
59
60         old_state = d->state;
61         d->state = state;
62
63         if (state != old_state)
64                 log_debug("%s changed %s -> %s",
65                           d->meta.id,
66                           device_state_to_string(old_state),
67                           device_state_to_string(state));
68
69         unit_notify(UNIT(d), state_translation_table[old_state], state_translation_table[state]);
70 }
71
72 static int device_coldplug(Unit *u) {
73         Device *d = DEVICE(u);
74
75         assert(d);
76         assert(d->state == DEVICE_DEAD);
77
78         if (d->sysfs)
79                 device_set_state(d, DEVICE_PLUGGED);
80
81         return 0;
82 }
83
84 static void device_dump(Unit *u, FILE *f, const char *prefix) {
85         Device *d = DEVICE(u);
86
87         assert(d);
88
89         fprintf(f,
90                 "%sDevice State: %s\n"
91                 "%sSysfs Path: %s\n",
92                 prefix, device_state_to_string(d->state),
93                 prefix, strna(d->sysfs));
94 }
95
96 static UnitActiveState device_active_state(Unit *u) {
97         assert(u);
98
99         return state_translation_table[DEVICE(u)->state];
100 }
101
102 static const char *device_sub_state_to_string(Unit *u) {
103         assert(u);
104
105         return device_state_to_string(DEVICE(u)->state);
106 }
107
108 static int device_add_escaped_name(Unit *u, const char *dn, bool make_id) {
109         char *e;
110         int r;
111
112         assert(u);
113         assert(dn);
114         assert(dn[0] == '/');
115
116         if (!(e = unit_name_from_path(dn, ".device")))
117                 return -ENOMEM;
118
119         r = unit_add_name(u, e);
120
121         if (r >= 0 && make_id)
122                 unit_choose_id(u, e);
123
124         free(e);
125
126         if (r < 0 && r != -EEXIST)
127                 return r;
128
129         return 0;
130 }
131
132 static int device_find_escape_name(Manager *m, const char *dn, Unit **_u) {
133         char *e;
134         Unit *u;
135
136         assert(m);
137         assert(dn);
138         assert(dn[0] == '/');
139         assert(_u);
140
141         if (!(e = unit_name_from_path(dn, ".device")))
142                 return -ENOMEM;
143
144         u = manager_get_unit(m, e);
145         free(e);
146
147         if (u) {
148                 *_u = u;
149                 return 1;
150         }
151
152         return 0;
153 }
154
155 static int device_process_new_device(Manager *m, struct udev_device *dev, bool update_state) {
156         const char *dn, *wants, *sysfs, *model, *alias;
157         Unit *u = NULL;
158         int r;
159         char *w, *state;
160         size_t l;
161         bool delete;
162         struct udev_list_entry *item = NULL, *first = NULL;
163
164         assert(m);
165
166         if (!(sysfs = udev_device_get_syspath(dev)))
167                 return -ENOMEM;
168
169         /* Check whether this entry is even relevant for us. */
170         dn = udev_device_get_devnode(dev);
171         wants = udev_device_get_property_value(dev, "SYSTEMD_WANTS");
172         alias = udev_device_get_property_value(dev, "SYSTEMD_ALIAS");
173
174         /* We allow exactly one alias to be configured a this time and
175          * it must be a path */
176
177         if (alias && !is_path(alias)) {
178                 log_warning("SYSTEMD_ALIAS for %s is not a path, ignoring: %s", sysfs, alias);
179                 alias = NULL;
180         }
181
182         if ((r = device_find_escape_name(m, sysfs, &u)) < 0)
183                 return r;
184
185         if (r == 0 && dn)
186                 if ((r = device_find_escape_name(m, dn, &u)) < 0)
187                         return r;
188
189         if (r == 0) {
190                 first = udev_device_get_devlinks_list_entry(dev);
191                 udev_list_entry_foreach(item, first) {
192                         if ((r = device_find_escape_name(m, udev_list_entry_get_name(item), &u)) < 0)
193                                 return r;
194
195                         if (r > 0)
196                                 break;
197                 }
198         }
199
200         if (r == 0 && alias)
201                 if ((r = device_find_escape_name(m, alias, &u)) < 0)
202                         return r;
203
204         /* FIXME: this needs proper merging */
205
206         assert((r > 0) == !!u);
207
208         /* If this is a different unit, then let's not merge things */
209         if (u && DEVICE(u)->sysfs && !path_equal(DEVICE(u)->sysfs, sysfs))
210                 u = NULL;
211
212         if (!u) {
213                 delete = true;
214
215                 if (!(u = unit_new(m)))
216                         return -ENOMEM;
217
218                 if ((r = device_add_escaped_name(u, sysfs, true)) < 0)
219                         goto fail;
220
221                 unit_add_to_load_queue(u);
222         } else
223                 delete = false;
224
225         if (!(DEVICE(u)->sysfs))
226                 if (!(DEVICE(u)->sysfs = strdup(sysfs))) {
227                         r = -ENOMEM;
228                         goto fail;
229                 }
230
231         if (alias)
232                 if ((r = device_add_escaped_name(u, alias, true)) < 0)
233                         goto fail;
234
235         if (dn)
236                 if ((r = device_add_escaped_name(u, dn, true)) < 0)
237                         goto fail;
238
239         first = udev_device_get_devlinks_list_entry(dev);
240         udev_list_entry_foreach(item, first)
241                 if ((r = device_add_escaped_name(u, udev_list_entry_get_name(item), false)) < 0)
242                         goto fail;
243
244         if ((model = udev_device_get_property_value(dev, "ID_MODEL_FROM_DATABASE")) ||
245             (model = udev_device_get_property_value(dev, "ID_MODEL"))) {
246                 if ((r = unit_set_description(u, model)) < 0)
247                         goto fail;
248         } else if (dn) {
249                 if ((r = unit_set_description(u, dn)) < 0)
250                         goto fail;
251         } else
252                 if ((r = unit_set_description(u, sysfs)) < 0)
253                         goto fail;
254
255         if (wants) {
256                 FOREACH_WORD_QUOTED(w, l, wants, state) {
257                         char *e;
258
259                         if (!(e = strndup(w, l))) {
260                                 r = -ENOMEM;
261                                 goto fail;
262                         }
263
264                         r = unit_add_dependency_by_name(u, UNIT_WANTS, e, NULL, true);
265                         free(e);
266
267                         if (r < 0)
268                                 goto fail;
269                 }
270         }
271
272         if (update_state) {
273                 manager_dispatch_load_queue(u->meta.manager);
274                 device_set_state(DEVICE(u), DEVICE_PLUGGED);
275         }
276
277         unit_add_to_dbus_queue(u);
278
279         return 0;
280
281 fail:
282
283         log_warning("Failed to load device unit: %s", strerror(-r));
284
285         if (delete && u)
286                 unit_free(u);
287
288         return r;
289 }
290
291 static int device_process_path(Manager *m, const char *path, bool update_state) {
292         int r;
293         struct udev_device *dev;
294
295         assert(m);
296         assert(path);
297
298         if (!(dev = udev_device_new_from_syspath(m->udev, path))) {
299                 log_warning("Failed to get udev device object from udev for path %s.", path);
300                 return -ENOMEM;
301         }
302
303         r = device_process_new_device(m, dev, update_state);
304         udev_device_unref(dev);
305         return r;
306 }
307
308 static int device_process_removed_device(Manager *m, struct udev_device *dev) {
309         const char *sysfs;
310         char *e;
311         Unit *u;
312         Device *d;
313
314         assert(m);
315         assert(dev);
316
317         if (!(sysfs = udev_device_get_syspath(dev)))
318                 return -ENOMEM;
319
320         assert(sysfs[0] == '/');
321         if (!(e = unit_name_from_path(sysfs, ".device")))
322                 return -ENOMEM;
323
324         u = manager_get_unit(m, e);
325         free(e);
326
327         if (!u)
328                 return 0;
329
330         d = DEVICE(u);
331         free(d->sysfs);
332         d->sysfs = NULL;
333
334         device_set_state(d, DEVICE_DEAD);
335         return 0;
336 }
337
338 static void device_shutdown(Manager *m) {
339         assert(m);
340
341         if (m->udev_monitor) {
342                 udev_monitor_unref(m->udev_monitor);
343                 m->udev_monitor = NULL;
344         }
345
346         if (m->udev) {
347                 udev_unref(m->udev);
348                 m->udev = NULL;
349         }
350 }
351
352 static int device_enumerate(Manager *m) {
353         struct epoll_event ev;
354         int r;
355         struct udev_enumerate *e = NULL;
356         struct udev_list_entry *item = NULL, *first = NULL;
357
358         assert(m);
359
360         if (!m->udev) {
361                 if (!(m->udev = udev_new()))
362                         return -ENOMEM;
363
364                 if (!(m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev"))) {
365                         r = -ENOMEM;
366                         goto fail;
367                 }
368
369                 if (udev_monitor_filter_add_match_tag(m->udev_monitor, "systemd") < 0) {
370                         r = -ENOMEM;
371                         goto fail;
372                 }
373
374                 if (udev_monitor_enable_receiving(m->udev_monitor) < 0) {
375                         r = -EIO;
376                         goto fail;
377                 }
378
379                 m->udev_watch.type = WATCH_UDEV;
380                 m->udev_watch.fd = udev_monitor_get_fd(m->udev_monitor);
381
382                 zero(ev);
383                 ev.events = EPOLLIN;
384                 ev.data.ptr = &m->udev_watch;
385
386                 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->udev_watch.fd, &ev) < 0)
387                         return -errno;
388         }
389
390         if (!(e = udev_enumerate_new(m->udev))) {
391                 r = -ENOMEM;
392                 goto fail;
393         }
394         if (udev_enumerate_add_match_tag(e, "systemd") < 0) {
395                 r = -EIO;
396                 goto fail;
397         }
398
399         if (udev_enumerate_scan_devices(e) < 0) {
400                 r = -EIO;
401                 goto fail;
402         }
403
404         first = udev_enumerate_get_list_entry(e);
405         udev_list_entry_foreach(item, first)
406                 device_process_path(m, udev_list_entry_get_name(item), false);
407
408         udev_enumerate_unref(e);
409         return 0;
410
411 fail:
412         if (e)
413                 udev_enumerate_unref(e);
414
415         device_shutdown(m);
416         return r;
417 }
418
419 void device_fd_event(Manager *m, int events) {
420         struct udev_device *dev;
421         int r;
422         const char *action;
423
424         assert(m);
425         assert(events == EPOLLIN);
426
427         if (!(dev = udev_monitor_receive_device(m->udev_monitor))) {
428                 log_error("Failed to receive device.");
429                 return;
430         }
431
432         if (!(action = udev_device_get_action(dev))) {
433                 log_error("Failed to get udev action string.");
434                 goto fail;
435         }
436
437         if (streq(action, "remove")) {
438                 if ((r = device_process_removed_device(m, dev)) < 0) {
439                         log_error("Failed to process udev device event: %s", strerror(-r));
440                         goto fail;
441                 }
442         } else {
443                 if ((r = device_process_new_device(m, dev, true)) < 0) {
444                         log_error("Failed to process udev device event: %s", strerror(-r));
445                         goto fail;
446                 }
447         }
448
449 fail:
450         udev_device_unref(dev);
451 }
452
453 static const char* const device_state_table[_DEVICE_STATE_MAX] = {
454         [DEVICE_DEAD] = "dead",
455         [DEVICE_PLUGGED] = "plugged"
456 };
457
458 DEFINE_STRING_TABLE_LOOKUP(device_state, DeviceState);
459
460 const UnitVTable device_vtable = {
461         .suffix = ".device",
462
463         .no_requires = true,
464         .no_instances = true,
465         .no_snapshots = true,
466         .no_isolate = true,
467
468         .init = device_init,
469
470         .load = unit_load_fragment_and_dropin_optional,
471         .done = device_done,
472         .coldplug = device_coldplug,
473
474         .dump = device_dump,
475
476         .active_state = device_active_state,
477         .sub_state_to_string = device_sub_state_to_string,
478
479         .bus_message_handler = bus_device_message_handler,
480
481         .enumerate = device_enumerate,
482         .shutdown = device_shutdown
483 };