chiark / gitweb /
dbus: print a line when we connect to dbus
[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         old_state = d->state;
53         d->state = state;
54
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
60 static 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
72 static void device_dump(Unit *u, FILE *f, const char *prefix) {
73         Device *d = DEVICE(u);
74
75         assert(d);
76
77         fprintf(f,
78                 "%sDevice State: %s\n"
79                 "%sSysfs Path: %s\n",
80                 prefix, state_string_table[d->state],
81                 prefix, strna(d->sysfs));
82 }
83
84 static UnitActiveState device_active_state(Unit *u) {
85         assert(u);
86
87         return state_translation_table[DEVICE(u)->state];
88 }
89
90 static int device_add_escaped_name(Unit *u, const char *dn, bool make_id) {
91         char *e;
92         int r;
93
94         assert(u);
95         assert(dn);
96         assert(dn[0] == '/');
97
98         if (!(e = unit_name_escape_path(dn+1, ".device")))
99                 return -ENOMEM;
100
101         r = unit_add_name(u, e);
102
103         if (r >= 0 && make_id)
104                 unit_choose_id(u, e);
105
106         free(e);
107
108         if (r < 0 && r != -EEXIST)
109                 return r;
110
111         return 0;
112 }
113
114 static int device_process_new_device(Manager *m, struct udev_device *dev, bool update_state) {
115         const char *dn, *names, *wants, *sysfs, *expose;
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
125         if (!(sysfs = udev_device_get_syspath(dev)))
126                 return -ENOMEM;
127
128         /* Check whether this entry is even relevant for us. */
129         dn = udev_device_get_devnode(dev);
130         expose = udev_device_get_property_value(dev, "SYSTEMD_EXPOSE");
131         names = udev_device_get_property_value(dev, "SYSTEMD_NAMES");
132         wants = udev_device_get_property_value(dev, "SYSTEMD_WANTS");
133
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;
147
148         /* Ok, seems kinda interesting. Now, let's see if this one
149          * already exists. */
150
151         assert(sysfs[0] == '/');
152         if (!(e = unit_name_escape_path(sysfs+1, ".device")))
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")) ||
177                     (model = udev_device_get_property_value(dev, "ID_MODEL"))) {
178                         if ((r = unit_set_description(u, model)) < 0)
179                                 goto fail;
180                 } else if (dn)
181                         if ((r = unit_set_description(u, dn)) < 0)
182                                 goto fail;
183
184                 unit_add_to_load_queue(u);
185         } else {
186                 delete = false;
187                 free(e);
188         }
189
190         if (dn)
191                 if ((r = device_add_escaped_name(u, dn, true)) < 0)
192                         goto fail;
193
194         first = udev_device_get_devlinks_list_entry(dev);
195         udev_list_entry_foreach(item, first)
196                 if ((r = device_add_escaped_name(u, udev_list_entry_get_name(item), false)) < 0)
197                         goto fail;
198
199         if (names) {
200                 FOREACH_WORD(w, l, names, state) {
201                         if (!(e = strndup(w, l))) {
202                                 r = -ENOMEM;
203                                 goto fail;
204                         }
205
206                         r = unit_add_name(u, e);
207                         free(e);
208
209                         if (r < 0 && r != -EEXIST)
210                                 goto fail;
211                 }
212         }
213
214         if (wants) {
215                 FOREACH_WORD(w, l, wants, state) {
216                         if (!(e = strndup(w, l))) {
217                                 r = -ENOMEM;
218                                 goto fail;
219                         }
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
229         if (update_state) {
230                 manager_dispatch_load_queue(u->meta.manager);
231                 device_set_state(DEVICE(u), DEVICE_AVAILABLE);
232         }
233
234         unit_add_to_dbus_queue(u);
235
236         return 0;
237
238 fail:
239         if (delete && u)
240                 unit_free(u);
241         return r;
242 }
243
244 static int device_process_path(Manager *m, const char *path, bool update_state) {
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
256         r = device_process_new_device(m, dev, update_state);
257         udev_device_unref(dev);
258         return r;
259 }
260
261 static 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] == '/');
274         if (!(e = unit_name_escape_path(sysfs+1, ".device")))
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
291 static void device_shutdown(Manager *m) {
292         assert(m);
293
294         if (m->udev_monitor)
295                 udev_monitor_unref(m->udev_monitor);
296
297         if (m->udev)
298                 udev_unref(m->udev);
299 }
300
301 static int device_enumerate(Manager *m) {
302         struct epoll_event ev;
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
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
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)
344                 device_process_path(m, udev_list_entry_get_name(item), false);
345
346         udev_enumerate_unref(e);
347         return 0;
348
349 fail:
350         if (e)
351                 udev_enumerate_unref(e);
352
353         device_shutdown(m);
354         return r;
355 }
356
357 void 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
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
387 fail:
388         udev_device_unref(dev);
389 }
390
391 const UnitVTable device_vtable = {
392         .suffix = ".device",
393
394         .init = unit_load_fragment_and_dropin_optional,
395         .done = device_done,
396         .coldplug = device_coldplug,
397
398         .dump = device_dump,
399
400         .active_state = device_active_state,
401
402         .enumerate = device_enumerate,
403         .shutdown = device_shutdown
404 };