chiark / gitweb /
500cd23e2718c7e46ac3b3555330665ebc1f26c0
[elogind.git] / 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
31 static const UnitActiveState state_translation_table[_DEVICE_STATE_MAX] = {
32         [DEVICE_DEAD] = UNIT_INACTIVE,
33         [DEVICE_AVAILABLE] = UNIT_ACTIVE
34 };
35
36 static const char* const state_string_table[_DEVICE_STATE_MAX] = {
37         [DEVICE_DEAD] = "dead",
38         [DEVICE_AVAILABLE] = "available"
39 };
40
41 static void device_done(Unit *u) {
42         Device *d = DEVICE(u);
43
44         assert(d);
45         free(d->sysfs);
46 }
47
48 static void device_set_state(Device *d, DeviceState state) {
49         DeviceState old_state;
50         assert(d);
51
52         if (state == d->state)
53                 return;
54
55         old_state = d->state;
56         d->state = state;
57
58         log_debug("%s changed %s → %s", unit_id(UNIT(d)), state_string_table[old_state], state_string_table[state]);
59
60         unit_notify(UNIT(d), state_translation_table[old_state], state_translation_table[state]);
61 }
62
63 static int device_coldplug(Unit *u) {
64         Device *d = DEVICE(u);
65
66         assert(d);
67         assert(d->state == DEVICE_DEAD);
68
69         if (d->sysfs)
70                 device_set_state(d, DEVICE_AVAILABLE);
71
72         return 0;
73 }
74
75 static void device_dump(Unit *u, FILE *f, const char *prefix) {
76         Device *d = DEVICE(u);
77
78         assert(d);
79
80         fprintf(f,
81                 "%sDevice State: %s\n"
82                 "%sSysfs Path: %s\n",
83                 prefix, state_string_table[d->state],
84                 prefix, strna(d->sysfs));
85 }
86
87 static UnitActiveState device_active_state(Unit *u) {
88         assert(u);
89
90         return state_translation_table[DEVICE(u)->state];
91 }
92
93 static int device_add_escaped_name(Unit *u, const char *dn, bool make_id) {
94         char *e;
95         int r;
96
97         assert(u);
98         assert(dn);
99         assert(dn[0] == '/');
100
101         if (!(e = unit_name_escape_path(dn+1, ".device")))
102                 return -ENOMEM;
103
104         r = unit_add_name(u, e);
105
106         if (r >= 0 && make_id)
107                 unit_choose_id(u, e);
108
109         free(e);
110
111         if (r < 0 && r != -EEXIST)
112                 return r;
113
114         return 0;
115 }
116
117 static int device_find_escape_name(Manager *m, const char *dn, Unit **_u) {
118         char *e;
119         Unit *u;
120
121         assert(m);
122         assert(dn);
123         assert(dn[0] == '/');
124         assert(_u);
125
126         if (!(e = unit_name_escape_path(dn+1, ".device")))
127                 return -ENOMEM;
128
129         u = manager_get_unit(m, e);
130         free(e);
131
132         if (u) {
133                 *_u = u;
134                 return 1;
135         }
136
137         return 0;
138 }
139
140 static int device_process_new_device(Manager *m, struct udev_device *dev, bool update_state) {
141         const char *dn, *names, *wants, *sysfs, *expose, *model;
142         Unit *u = NULL;
143         int r;
144         char *w, *state;
145         size_t l;
146         bool delete;
147         struct udev_list_entry *item = NULL, *first = NULL;
148
149         assert(m);
150
151         if (!(sysfs = udev_device_get_syspath(dev)))
152                 return -ENOMEM;
153
154         /* Check whether this entry is even relevant for us. */
155         dn = udev_device_get_devnode(dev);
156         expose = udev_device_get_property_value(dev, "SYSTEMD_EXPOSE");
157         names = udev_device_get_property_value(dev, "SYSTEMD_NAMES");
158         wants = udev_device_get_property_value(dev, "SYSTEMD_WANTS");
159
160         if (expose) {
161                 int b;
162
163                 if ((b = parse_boolean(expose)) < 0) {
164                         log_error("Failed to parse SYSTEMD_EXPOSE udev property for device %s: %s", sysfs, expose);
165                         return 0;
166                 }
167
168                 if (!b)
169                         return 0;
170         } else
171                 if (!dn && !names && !wants)
172                         return 0;
173
174         /* Ok, seems kinda interesting. Now, let's see if this one
175          * already exists. */
176
177         if ((r = device_find_escape_name(m, sysfs, &u)) < 0)
178                 return r;
179
180         if (r == 0 && dn)
181                 if ((r = device_find_escape_name(m, dn, &u)) < 0)
182                         return r;
183
184         if (r == 0) {
185                 first = udev_device_get_devlinks_list_entry(dev);
186                 udev_list_entry_foreach(item, first) {
187                         if ((r = device_find_escape_name(m, udev_list_entry_get_name(item), &u)) < 0)
188                                 return r;
189
190                         if (r > 0)
191                                 break;
192                 }
193         }
194
195         /* FIXME: this needs proper merging */
196
197         assert((r > 0) == !!u);
198
199         /* If this is a different unit, then let's not merge things */
200         if (u && DEVICE(u)->sysfs && !streq(DEVICE(u)->sysfs, sysfs))
201                 u = NULL;
202
203         if (!u) {
204                 delete = true;
205
206                 if (!(u = unit_new(m)))
207                         return -ENOMEM;
208
209                 if ((r = device_add_escaped_name(u, sysfs, true)) < 0)
210                         goto fail;
211
212                 if (!(DEVICE(u)->sysfs = strdup(sysfs))) {
213                         r = -ENOMEM;
214                         goto fail;
215                 }
216
217                 unit_add_to_load_queue(u);
218         } else
219                 delete = false;
220
221         if (dn)
222                 if ((r = device_add_escaped_name(u, dn, true)) < 0)
223                         goto fail;
224
225         first = udev_device_get_devlinks_list_entry(dev);
226         udev_list_entry_foreach(item, first)
227                 if ((r = device_add_escaped_name(u, udev_list_entry_get_name(item), false)) < 0)
228                         goto fail;
229
230         if ((model = udev_device_get_property_value(dev, "ID_MODEL_FROM_DATABASE")) ||
231             (model = udev_device_get_property_value(dev, "ID_MODEL"))) {
232                 if ((r = unit_set_description(u, model)) < 0)
233                         goto fail;
234         } else if (dn)
235                 if ((r = unit_set_description(u, dn)) < 0)
236                         goto fail;
237
238         /* We don't remove names that are gone. But that should be
239          * fine and should probably be fixed only on a configuration
240          * refresh. */
241
242         if (names) {
243                 FOREACH_WORD(w, l, names, state) {
244                         char *e;
245
246                         if (!(e = strndup(w, l))) {
247                                 r = -ENOMEM;
248                                 goto fail;
249                         }
250
251                         r = unit_add_name(u, e);
252                         free(e);
253
254                         if (r < 0 && r != -EEXIST)
255                                 goto fail;
256                 }
257         }
258
259         if (wants) {
260                 FOREACH_WORD(w, l, wants, state) {
261                         char *e;
262
263                         if (!(e = strndup(w, l))) {
264                                 r = -ENOMEM;
265                                 goto fail;
266                         }
267
268                         r = unit_add_dependency_by_name(u, UNIT_WANTS, e);
269                         free(e);
270
271                         if (r < 0)
272                                 goto fail;
273                 }
274         }
275
276         if (update_state) {
277                 manager_dispatch_load_queue(u->meta.manager);
278                 device_set_state(DEVICE(u), DEVICE_AVAILABLE);
279         }
280
281         unit_add_to_dbus_queue(u);
282
283         return 0;
284
285 fail:
286         if (delete && u)
287                 unit_free(u);
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_escape_path(sysfs+1, ".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
344         if (m->udev)
345                 udev_unref(m->udev);
346 }
347
348 static int device_enumerate(Manager *m) {
349         struct epoll_event ev;
350         int r;
351         struct udev_enumerate *e = NULL;
352         struct udev_list_entry *item = NULL, *first = NULL;
353
354         assert(m);
355
356         if (!(m->udev = udev_new()))
357                 return -ENOMEM;
358
359         if (!(m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev"))) {
360                 r = -ENOMEM;
361                 goto fail;
362         }
363
364         if (udev_monitor_enable_receiving(m->udev_monitor) < 0) {
365                 r = -EIO;
366                 goto fail;
367         }
368
369         m->udev_watch.type = WATCH_UDEV;
370         m->udev_watch.fd = udev_monitor_get_fd(m->udev_monitor);
371
372         zero(ev);
373         ev.events = EPOLLIN;
374         ev.data.ptr = &m->udev_watch;
375
376         if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->udev_watch.fd, &ev) < 0)
377                 return -errno;
378
379         if (!(e = udev_enumerate_new(m->udev))) {
380                 r = -ENOMEM;
381                 goto fail;
382         }
383
384         if (udev_enumerate_scan_devices(e) < 0) {
385                 r = -EIO;
386                 goto fail;
387         }
388
389         first = udev_enumerate_get_list_entry(e);
390         udev_list_entry_foreach(item, first)
391                 device_process_path(m, udev_list_entry_get_name(item), false);
392
393         udev_enumerate_unref(e);
394         return 0;
395
396 fail:
397         if (e)
398                 udev_enumerate_unref(e);
399
400         device_shutdown(m);
401         return r;
402 }
403
404 void device_fd_event(Manager *m, int events) {
405         struct udev_device *dev;
406         int r;
407         const char *action;
408
409         assert(m);
410         assert(events == EPOLLIN);
411
412         if (!(dev = udev_monitor_receive_device(m->udev_monitor))) {
413                 log_error("Failed to receive device.");
414                 return;
415         }
416
417         if (!(action = udev_device_get_action(dev))) {
418                 log_error("Failed to get udev action string.");
419                 goto fail;
420         }
421
422         if (streq(action, "remove")) {
423                 if ((r = device_process_removed_device(m, dev)) < 0) {
424                         log_error("Failed to process udev device event: %s", strerror(-r));
425                         goto fail;
426                 }
427         } else {
428                 if ((r = device_process_new_device(m, dev, true)) < 0) {
429                         log_error("Failed to process udev device event: %s", strerror(-r));
430                         goto fail;
431                 }
432         }
433
434 fail:
435         udev_device_unref(dev);
436 }
437
438 const UnitVTable device_vtable = {
439         .suffix = ".device",
440
441         .init = unit_load_fragment_and_dropin_optional,
442         .done = device_done,
443         .coldplug = device_coldplug,
444
445         .dump = device_dump,
446
447         .active_state = device_active_state,
448
449         .enumerate = device_enumerate,
450         .shutdown = device_shutdown
451 };