chiark / gitweb /
core: convert PID 1 to libsystemd-bus
[elogind.git] / src / core / device.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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 Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 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   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser 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 #include "def.h"
33 #include "path-util.h"
34 #include "udev-util.h"
35
36 static const UnitActiveState state_translation_table[_DEVICE_STATE_MAX] = {
37         [DEVICE_DEAD] = UNIT_INACTIVE,
38         [DEVICE_PLUGGED] = UNIT_ACTIVE
39 };
40
41 static int device_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
42
43 static void device_unset_sysfs(Device *d) {
44         Device *first;
45
46         assert(d);
47
48         if (!d->sysfs)
49                 return;
50
51         /* Remove this unit from the chain of devices which share the
52          * same sysfs path. */
53         first = hashmap_get(UNIT(d)->manager->devices_by_sysfs, d->sysfs);
54         LIST_REMOVE(same_sysfs, first, d);
55
56         if (first)
57                 hashmap_remove_and_replace(UNIT(d)->manager->devices_by_sysfs, d->sysfs, first->sysfs, first);
58         else
59                 hashmap_remove(UNIT(d)->manager->devices_by_sysfs, d->sysfs);
60
61         free(d->sysfs);
62         d->sysfs = NULL;
63 }
64
65 static void device_init(Unit *u) {
66         Device *d = DEVICE(u);
67
68         assert(d);
69         assert(UNIT(d)->load_state == UNIT_STUB);
70
71         /* In contrast to all other unit types we timeout jobs waiting
72          * for devices by default. This is because they otherwise wait
73          * indefinitely for plugged in devices, something which cannot
74          * happen for the other units since their operations time out
75          * anyway. */
76         UNIT(d)->job_timeout = u->manager->default_timeout_start_usec;
77
78         UNIT(d)->ignore_on_isolate = true;
79         UNIT(d)->ignore_on_snapshot = true;
80 }
81
82 static void device_done(Unit *u) {
83         Device *d = DEVICE(u);
84
85         assert(d);
86
87         device_unset_sysfs(d);
88 }
89
90 static void device_set_state(Device *d, DeviceState state) {
91         DeviceState old_state;
92         assert(d);
93
94         old_state = d->state;
95         d->state = state;
96
97         if (state != old_state)
98                 log_debug_unit(UNIT(d)->id,
99                                "%s changed %s -> %s", UNIT(d)->id,
100                                device_state_to_string(old_state),
101                                device_state_to_string(state));
102
103         unit_notify(UNIT(d), state_translation_table[old_state], state_translation_table[state], true);
104 }
105
106 static int device_coldplug(Unit *u) {
107         Device *d = DEVICE(u);
108
109         assert(d);
110         assert(d->state == DEVICE_DEAD);
111
112         if (d->sysfs)
113                 device_set_state(d, DEVICE_PLUGGED);
114
115         return 0;
116 }
117
118 static void device_dump(Unit *u, FILE *f, const char *prefix) {
119         Device *d = DEVICE(u);
120
121         assert(d);
122
123         fprintf(f,
124                 "%sDevice State: %s\n"
125                 "%sSysfs Path: %s\n",
126                 prefix, device_state_to_string(d->state),
127                 prefix, strna(d->sysfs));
128 }
129
130 _pure_ static UnitActiveState device_active_state(Unit *u) {
131         assert(u);
132
133         return state_translation_table[DEVICE(u)->state];
134 }
135
136 _pure_ static const char *device_sub_state_to_string(Unit *u) {
137         assert(u);
138
139         return device_state_to_string(DEVICE(u)->state);
140 }
141
142 static int device_add_escaped_name(Unit *u, const char *dn) {
143         _cleanup_free_ char *e = NULL;
144         int r;
145
146         assert(u);
147         assert(dn);
148         assert(dn[0] == '/');
149
150         e = unit_name_from_path(dn, ".device");
151         if (!e)
152                 return -ENOMEM;
153
154         r = unit_add_name(u, e);
155         if (r < 0 && r != -EEXIST)
156                 return r;
157
158         return 0;
159 }
160
161 static int device_find_escape_name(Manager *m, const char *dn, Unit **_u) {
162         _cleanup_free_ char *e = NULL;
163         Unit *u;
164
165         assert(m);
166         assert(dn);
167         assert(dn[0] == '/');
168         assert(_u);
169
170         e = unit_name_from_path(dn, ".device");
171         if (!e)
172                 return -ENOMEM;
173
174         u = manager_get_unit(m, e);
175         if (u) {
176                 *_u = u;
177                 return 1;
178         }
179
180         return 0;
181 }
182
183 static int device_update_unit(Manager *m, struct udev_device *dev, const char *path, bool main) {
184         const char *sysfs, *model;
185         Unit *u = NULL;
186         int r;
187         bool delete;
188
189         assert(m);
190
191         sysfs = udev_device_get_syspath(dev);
192         if (!sysfs)
193                 return -ENOMEM;
194
195         r = device_find_escape_name(m, path, &u);
196         if (r < 0)
197                 return r;
198
199         if (u && DEVICE(u)->sysfs && !path_equal(DEVICE(u)->sysfs, sysfs))
200                 return -EEXIST;
201
202         if (!u) {
203                 delete = true;
204
205                 u = unit_new(m, sizeof(Device));
206                 if (!u)
207                         return -ENOMEM;
208
209                 r = device_add_escaped_name(u, path);
210                 if (r < 0)
211                         goto fail;
212
213                 unit_add_to_load_queue(u);
214         } else
215                 delete = false;
216
217         /* If this was created via some dependency and has not
218          * actually been seen yet ->sysfs will not be
219          * initialized. Hence initialize it if necessary. */
220
221         if (!DEVICE(u)->sysfs) {
222                 Device *first;
223
224                 DEVICE(u)->sysfs = strdup(sysfs);
225                 if (!DEVICE(u)->sysfs) {
226                         r = -ENOMEM;
227                         goto fail;
228                 }
229
230                 r = hashmap_ensure_allocated(&m->devices_by_sysfs, string_hash_func, string_compare_func);
231                 if (r < 0)
232                         goto fail;
233
234                 first = hashmap_get(m->devices_by_sysfs, sysfs);
235                 LIST_PREPEND(same_sysfs, first, DEVICE(u));
236
237                 r = hashmap_replace(m->devices_by_sysfs, DEVICE(u)->sysfs, first);
238                 if (r < 0)
239                         goto fail;
240         }
241
242         if ((model = udev_device_get_property_value(dev, "ID_MODEL_FROM_DATABASE")) ||
243             (model = udev_device_get_property_value(dev, "ID_MODEL")))
244                 r = unit_set_description(u, model);
245         else
246                 r = unit_set_description(u, path);
247         if (r < 0)
248                 goto fail;
249
250         if (main) {
251                 /* The additional systemd udev properties we only
252                  * interpret for the main object */
253                 const char *wants, *alias;
254
255                 alias = udev_device_get_property_value(dev, "SYSTEMD_ALIAS");
256                 if (alias) {
257                         char *state, *w;
258                         size_t l;
259
260                         FOREACH_WORD_QUOTED(w, l, alias, state) {
261                                 _cleanup_free_ char *e;
262
263                                 e = strndup(w, l);
264                                 if (!e) {
265                                         r = -ENOMEM;
266                                         goto fail;
267                                 }
268
269                                 if (is_path(e))
270                                         device_update_unit(m, dev, e, false);
271                                 else
272                                         log_warning("SYSTEMD_ALIAS for %s is not a path, ignoring: %s", sysfs, e);
273                         }
274                 }
275
276                 wants = udev_device_get_property_value(dev, m->running_as == SYSTEMD_USER ? "SYSTEMD_USER_WANTS" : "SYSTEMD_WANTS");
277                 if (wants) {
278                         char *state, *w;
279                         size_t l;
280
281                         FOREACH_WORD_QUOTED(w, l, wants, state) {
282                                 _cleanup_free_ char *e, *n = NULL;
283
284                                 e = strndup(w, l);
285                                 if (!e) {
286                                         r = -ENOMEM;
287                                         goto fail;
288                                 }
289
290                                 n = unit_name_mangle(e);
291                                 if (!n) {
292                                         r = -ENOMEM;
293                                         goto fail;
294                                 }
295
296                                 r = unit_add_dependency_by_name(u, UNIT_WANTS, n, NULL, true);
297                                 if (r < 0)
298                                         goto fail;
299                         }
300                 }
301         }
302
303         unit_add_to_dbus_queue(u);
304         return 0;
305
306 fail:
307         log_warning("Failed to load device unit: %s", strerror(-r));
308
309         if (delete && u)
310                 unit_free(u);
311
312         return r;
313 }
314
315 static int device_process_new_device(Manager *m, struct udev_device *dev, bool update_state) {
316         const char *sysfs, *dn;
317         struct udev_list_entry *item = NULL, *first = NULL;
318         int r;
319
320         assert(m);
321
322         sysfs = udev_device_get_syspath(dev);
323         if (!sysfs)
324                 return -ENOMEM;
325
326         /* Add the main unit named after the sysfs path */
327         r = device_update_unit(m, dev, sysfs, true);
328         if (r < 0)
329                 return r;
330
331         /* Add an additional unit for the device node */
332         if ((dn = udev_device_get_devnode(dev)))
333                 device_update_unit(m, dev, dn, false);
334
335         /* Add additional units for all symlinks */
336         first = udev_device_get_devlinks_list_entry(dev);
337         udev_list_entry_foreach(item, first) {
338                 const char *p;
339                 struct stat st;
340
341                 /* Don't bother with the /dev/block links */
342                 p = udev_list_entry_get_name(item);
343
344                 if (path_startswith(p, "/dev/block/") ||
345                     path_startswith(p, "/dev/char/"))
346                         continue;
347
348                 /* Verify that the symlink in the FS actually belongs
349                  * to this device. This is useful to deal with
350                  * conflicting devices, e.g. when two disks want the
351                  * same /dev/disk/by-label/xxx link because they have
352                  * the same label. We want to make sure that the same
353                  * device that won the symlink wins in systemd, so we
354                  * check the device node major/minor*/
355                 if (stat(p, &st) >= 0)
356                         if ((!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode)) ||
357                             st.st_rdev != udev_device_get_devnum(dev))
358                                 continue;
359
360                 device_update_unit(m, dev, p, false);
361         }
362
363         if (update_state) {
364                 Device *d, *l;
365
366                 manager_dispatch_load_queue(m);
367
368                 l = hashmap_get(m->devices_by_sysfs, sysfs);
369                 LIST_FOREACH(same_sysfs, d, l)
370                         device_set_state(d, DEVICE_PLUGGED);
371         }
372
373         return 0;
374 }
375
376 static int device_process_path(Manager *m, const char *path, bool update_state) {
377         int r;
378         struct udev_device *dev;
379
380         assert(m);
381         assert(path);
382
383         dev = udev_device_new_from_syspath(m->udev, path);
384         if (!dev) {
385                 log_warning("Failed to get udev device object from udev for path %s.", path);
386                 return -ENOMEM;
387         }
388
389         r = device_process_new_device(m, dev, update_state);
390         udev_device_unref(dev);
391         return r;
392 }
393
394 static int device_process_removed_device(Manager *m, struct udev_device *dev) {
395         const char *sysfs;
396         Device *d;
397
398         assert(m);
399         assert(dev);
400
401         if (!(sysfs = udev_device_get_syspath(dev)))
402                 return -ENOMEM;
403
404         /* Remove all units of this sysfs path */
405         while ((d = hashmap_get(m->devices_by_sysfs, sysfs))) {
406                 device_unset_sysfs(d);
407                 device_set_state(d, DEVICE_DEAD);
408         }
409
410         return 0;
411 }
412
413 static Unit *device_following(Unit *u) {
414         Device *d = DEVICE(u);
415         Device *other, *first = NULL;
416
417         assert(d);
418
419         if (startswith(u->id, "sys-"))
420                 return NULL;
421
422         /* Make everybody follow the unit that's named after the sysfs path */
423         for (other = d->same_sysfs_next; other; other = other->same_sysfs_next)
424                 if (startswith(UNIT(other)->id, "sys-"))
425                         return UNIT(other);
426
427         for (other = d->same_sysfs_prev; other; other = other->same_sysfs_prev) {
428                 if (startswith(UNIT(other)->id, "sys-"))
429                         return UNIT(other);
430
431                 first = other;
432         }
433
434         return UNIT(first);
435 }
436
437 static int device_following_set(Unit *u, Set **_s) {
438         Device *d = DEVICE(u);
439         Device *other;
440         Set *s;
441         int r;
442
443         assert(d);
444         assert(_s);
445
446         if (!d->same_sysfs_prev && !d->same_sysfs_next) {
447                 *_s = NULL;
448                 return 0;
449         }
450
451         if (!(s = set_new(NULL, NULL)))
452                 return -ENOMEM;
453
454         for (other = d->same_sysfs_next; other; other = other->same_sysfs_next)
455                 if ((r = set_put(s, other)) < 0)
456                         goto fail;
457
458         for (other = d->same_sysfs_prev; other; other = other->same_sysfs_prev)
459                 if ((r = set_put(s, other)) < 0)
460                         goto fail;
461
462         *_s = s;
463         return 1;
464
465 fail:
466         set_free(s);
467         return r;
468 }
469
470 static void device_shutdown(Manager *m) {
471         assert(m);
472
473         m->udev_event_source = sd_event_source_unref(m->udev_event_source);
474
475         if (m->udev_monitor) {
476                 udev_monitor_unref(m->udev_monitor);
477                 m->udev_monitor = NULL;
478         }
479
480         if (m->udev) {
481                 udev_unref(m->udev);
482                 m->udev = NULL;
483         }
484
485         hashmap_free(m->devices_by_sysfs);
486         m->devices_by_sysfs = NULL;
487 }
488
489 static int device_enumerate(Manager *m) {
490         _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
491         struct udev_list_entry *item = NULL, *first = NULL;
492         int r;
493
494         assert(m);
495
496         if (!m->udev) {
497                 m->udev = udev_new();
498                 if (!m->udev)
499                         return -ENOMEM;
500
501                 m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
502                 if (!m->udev_monitor) {
503                         r = -ENOMEM;
504                         goto fail;
505                 }
506
507                 /* This will fail if we are unprivileged, but that
508                  * should not matter much, as user instances won't run
509                  * during boot. */
510                 udev_monitor_set_receive_buffer_size(m->udev_monitor, 128*1024*1024);
511
512                 r = udev_monitor_filter_add_match_tag(m->udev_monitor, "systemd");
513                 if (r < 0)
514                         goto fail;
515
516                 r = udev_monitor_enable_receiving(m->udev_monitor);
517                 if (r < 0)
518                         goto fail;
519
520                 r = sd_event_add_io(m->event, udev_monitor_get_fd(m->udev_monitor), EPOLLIN, device_dispatch_io, m, &m->udev_event_source);
521                 if (r < 0)
522                         goto fail;
523         }
524
525         e = udev_enumerate_new(m->udev);
526         if (!e) {
527                 r = -ENOMEM;
528                 goto fail;
529         }
530
531         r = udev_enumerate_add_match_tag(e, "systemd");
532         if (r < 0)
533                 goto fail;
534
535         r = udev_enumerate_scan_devices(e);
536         if (r < 0)
537                 goto fail;
538
539         first = udev_enumerate_get_list_entry(e);
540         udev_list_entry_foreach(item, first)
541                 device_process_path(m, udev_list_entry_get_name(item), false);
542
543         return 0;
544
545 fail:
546         device_shutdown(m);
547         return r;
548 }
549
550 static int device_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
551         _cleanup_udev_device_unref_ struct udev_device *dev = NULL;
552         const char *action, *ready;
553         Manager *m = userdata;
554         int r;
555
556         assert(m);
557
558         if (revents != EPOLLIN) {
559                 static RATELIMIT_DEFINE(limit, 10*USEC_PER_SEC, 5);
560
561                 if (!ratelimit_test(&limit))
562                         log_error("Failed to get udev event: %m");
563                 if (!(revents & EPOLLIN))
564                         return 0;
565         }
566
567         /*
568          * libudev might filter-out devices which pass the bloom
569          * filter, so getting NULL here is not necessarily an error.
570          */
571         dev = udev_monitor_receive_device(m->udev_monitor);
572         if (!dev)
573                 return 0;
574
575         action = udev_device_get_action(dev);
576         if (!action) {
577                 log_error("Failed to get udev action string.");
578                 return 0;
579         }
580
581         ready = udev_device_get_property_value(dev, "SYSTEMD_READY");
582
583         if (streq(action, "remove") || (ready && parse_boolean(ready) == 0)) {
584                 r = device_process_removed_device(m, dev);
585                 if (r < 0)
586                         log_error("Failed to process udev device event: %s", strerror(-r));
587         } else {
588                 r = device_process_new_device(m, dev, true);
589                 if (r < 0)
590                         log_error("Failed to process udev device event: %s", strerror(-r));
591         }
592
593         return 0;
594 }
595
596 static const char* const device_state_table[_DEVICE_STATE_MAX] = {
597         [DEVICE_DEAD] = "dead",
598         [DEVICE_PLUGGED] = "plugged"
599 };
600
601 DEFINE_STRING_TABLE_LOOKUP(device_state, DeviceState);
602
603 const UnitVTable device_vtable = {
604         .object_size = sizeof(Device),
605         .sections =
606                 "Unit\0"
607                 "Device\0"
608                 "Install\0",
609
610         .no_instances = true,
611
612         .init = device_init,
613         .done = device_done,
614         .load = unit_load_fragment_and_dropin_optional,
615
616         .coldplug = device_coldplug,
617
618         .dump = device_dump,
619
620         .active_state = device_active_state,
621         .sub_state_to_string = device_sub_state_to_string,
622
623         .bus_interface = "org.freedesktop.systemd1.Device",
624         .bus_changing_properties = bus_device_changing_properties,
625         .bus_vtable = bus_device_vtable,
626
627         .following = device_following,
628         .following_set = device_following_set,
629
630         .enumerate = device_enumerate,
631         .shutdown = device_shutdown,
632
633         .status_message_formats = {
634                 .starting_stopping = {
635                         [0] = "Expecting device %s...",
636                 },
637                 .finished_start_job = {
638                         [JOB_DONE]       = "Found device %s.",
639                         [JOB_TIMEOUT]    = "Timed out waiting for device %s.",
640                 },
641         },
642 };