discover: Remove unnecessary event passing
[petitboot] / discover / udev.c
1
2 #define _GNU_SOURCE
3
4 #include <assert.h>
5 #include <errno.h>
6 #include <libudev.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <sys/socket.h>
12 #include <sys/types.h>
13 #include <sys/un.h>
14
15 #include <log/log.h>
16 #include <types/types.h>
17 #include <talloc/talloc.h>
18 #include <waiter/waiter.h>
19 #include <system/system.h>
20
21 #include "event.h"
22 #include "udev.h"
23 #include "pb-discover.h"
24 #include "device-handler.h"
25
26 #if defined(DEBUG)
27 #define DBG(fmt, args...) pb_log("DBG: " fmt, ## args)
28 #define DBGS(fmt, args...) \
29         pb_log("DBG:%s:%d: " fmt, __func__, __LINE__, ## args)
30 #else
31 #define DBG(fmt, args...)
32 #define DBGS(fmt, args...)
33 #endif
34
35 struct pb_udev {
36         struct udev *udev;
37         struct udev_monitor *monitor;
38         struct device_handler *handler;
39 };
40
41 static int udev_destructor(void *p)
42 {
43         struct pb_udev *udev = p;
44
45         udev_monitor_unref(udev->monitor);
46         udev->monitor = NULL;
47
48         udev_unref(udev->udev);
49         udev->udev = NULL;
50
51         return 0;
52 }
53
54 static void udev_setup_device_params(struct udev_device *udev,
55                 struct discover_device *dev)
56 {
57         struct udev_list_entry *list, *entry;
58
59         list = udev_device_get_properties_list_entry(udev);
60         if (!list)
61                 return;
62
63         udev_list_entry_foreach(entry, list)
64                 discover_device_set_param(dev,
65                                 udev_list_entry_get_name(entry),
66                                 udev_list_entry_get_value(entry));
67 }
68
69 static int udev_handle_dev_add(struct pb_udev *udev, struct udev_device *dev)
70 {
71         struct discover_device *ddev;
72         const char *typestr;
73         const char *path;
74         const char *name;
75
76         name = udev_device_get_sysname(dev);
77         if (!name) {
78                 pb_debug("udev_device_get_sysname failed\n");
79                 return -1;
80         }
81
82         typestr = udev_device_get_devtype(dev);
83         if (!typestr) {
84                 pb_debug("udev_device_get_devtype failed\n");
85                 return -1;
86         }
87
88         if (!(!strcmp(typestr, "disk") || !strcmp(typestr, "partition"))) {
89                 pb_debug("SKIP %s: invalid type %s\n", name, typestr);
90                 return 0;
91         }
92
93         path = udev_device_get_devpath(dev);
94         if (path && (strstr(path, "virtual/block/loop")
95                         || strstr(path, "virtual/block/ram"))) {
96                 pb_debug("SKIP: %s: ignored (path=%s)\n", name, path);
97                 return 0;
98         }
99
100         /* We have enough info to create the device and start discovery */
101         ddev = device_lookup_by_id(udev->handler, name);
102         if (ddev) {
103                 pb_debug("device %s is already present?\n", name);
104                 return -1;
105         }
106
107         ddev = discover_device_create(udev->handler, name);
108
109         ddev->device_path = udev_device_get_devnode(dev);
110         ddev->uuid = udev_device_get_property_value(dev, "ID_FS_UUID");
111         ddev->label = udev_device_get_property_value(dev, "ID_FS_LABEL");
112         ddev->device->type = DEVICE_TYPE_DISK;
113
114         udev_setup_device_params(dev, ddev);
115
116         device_handler_discover(udev->handler, ddev, CONF_METHOD_LOCAL_FILE);
117
118         return 0;
119 }
120
121 static int udev_handle_dev_remove(struct pb_udev *udev, struct udev_device *dev)
122 {
123         struct discover_device *ddev;
124         const char *name;
125
126         name = udev_device_get_sysname(dev);
127         if (!name) {
128                 pb_debug("udev_device_get_sysname failed\n");
129                 return -1;
130         }
131
132         ddev = device_lookup_by_id(udev->handler, name);
133         if (!ddev)
134                 return 0;
135
136         device_handler_remove(udev->handler, ddev);
137
138         return 0;
139 }
140 static int udev_handle_dev_action(struct udev_device *dev, const char *action)
141 {
142         struct pb_udev *udev = udev_get_userdata(udev_device_get_udev(dev));
143
144         if (!strcmp(action, "add"))
145                 return udev_handle_dev_add(udev, dev);
146
147         else if (!strcmp(action, "remove"))
148                 return udev_handle_dev_remove(udev, dev);
149
150         return 0;
151 }
152
153 static int udev_enumerate(struct udev *udev)
154 {
155         int result;
156         struct udev_list_entry *list, *entry;
157         struct udev_enumerate *enumerate;
158
159         enumerate = udev_enumerate_new(udev);
160
161         if (!enumerate) {
162                 pb_log("udev_enumerate_new failed\n");
163                 return -1;
164         }
165
166         result = udev_enumerate_add_match_subsystem(enumerate, "block");
167
168         if (result) {
169                 pb_log("udev_enumerate_add_match_subsystem failed\n");
170                 goto fail;
171         }
172
173         udev_enumerate_scan_devices(enumerate);
174
175         list = udev_enumerate_get_list_entry(enumerate);
176
177         if (!list) {
178                 pb_log("udev_enumerate_get_list_entry failed\n");
179                 goto fail;
180         }
181
182         udev_list_entry_foreach(entry, list) {
183                 const char *syspath;
184                 struct udev_device *dev;
185
186                 syspath = udev_list_entry_get_name(entry);
187                 dev = udev_device_new_from_syspath(udev, syspath);
188
189                 udev_handle_dev_action(dev, "add");
190
191                 udev_device_unref(dev);
192         }
193
194         udev_enumerate_unref(enumerate);
195         return 0;
196
197 fail:
198         udev_enumerate_unref(enumerate);
199         return -1;
200 }
201
202 static int udev_setup_monitor(struct udev *udev, struct udev_monitor **monitor)
203 {
204         int result;
205         struct udev_monitor *m;
206
207         *monitor = NULL;
208         m = udev_monitor_new_from_netlink(udev, "udev");
209
210         if (!m) {
211                 pb_log("udev_monitor_new_from_netlink failed\n");
212                 goto out_err;
213         }
214
215         result = udev_monitor_filter_add_match_subsystem_devtype(m, "block",
216                 NULL);
217
218         if (result) {
219                 pb_log("udev_monitor_filter_add_match_subsystem_devtype failed\n");
220                 goto out_err;
221         }
222
223         result = udev_monitor_enable_receiving(m);
224
225         if (result) {
226                 pb_log("udev_monitor_enable_receiving failed\n");
227                 goto out_err;
228         }
229
230         *monitor = m;
231         return 0;
232
233 out_err:
234         udev_monitor_unref(m);
235         return -1;
236 }
237
238 /*
239  * udev_process - waiter callback for monitor netlink.
240  */
241
242 static int udev_process(void *arg)
243 {
244         struct udev_monitor *monitor = arg;
245         struct udev_device *dev;
246         const char *action;
247         int result;
248
249         dev = udev_monitor_receive_device(monitor);
250
251         if (!dev) {
252                 pb_log("udev_monitor_receive_device failed\n");
253                 return -1;
254         }
255
256         action = udev_device_get_action(dev);
257
258         if (!action) {
259                 pb_log("udev_device_get_action failed\n");
260                 goto fail;
261         }
262
263         result = udev_handle_dev_action(dev, action);
264
265         udev_device_unref(dev);
266         return result;
267
268 fail:
269         udev_device_unref(dev);
270         return -1;
271 }
272
273 static void udev_log_fn(struct udev __attribute__((unused)) *udev,
274         int __attribute__((unused)) priority, const char *file, int line,
275         const char *fn, const char *format, va_list args)
276 {
277       pb_log("libudev: %s %s:%d: ", fn, file, line);
278       vfprintf(pb_log_get_stream(), format, args);
279 }
280
281 struct pb_udev *udev_init(struct waitset *waitset,
282         struct device_handler *handler)
283 {
284         int result;
285         struct pb_udev *udev = talloc(NULL, struct pb_udev);
286
287         talloc_set_destructor(udev, udev_destructor);
288         udev->handler = handler;
289
290         udev->udev = udev_new();
291
292         if (!udev->udev) {
293                 pb_log("udev_new failed\n");
294                 goto fail_new;
295         }
296
297         udev_set_userdata(udev->udev, udev);
298
299         udev_set_log_fn(udev->udev, udev_log_fn);
300
301         result = udev_enumerate(udev->udev);
302
303         if (result)
304                 goto fail_enumerate;
305
306         result = udev_setup_monitor(udev->udev, &udev->monitor);
307
308         if (result)
309                 goto fail_monitor;
310
311         waiter_register_io(waitset, udev_monitor_get_fd(udev->monitor), WAIT_IN,
312                 udev_process, udev->monitor);
313
314         pb_log("%s: waiting on udev\n", __func__);
315
316         return udev;
317
318 fail_monitor:
319 fail_enumerate:
320         udev_unref(udev->udev);
321 fail_new:
322         talloc_free(udev);
323         return NULL;
324 }
325
326 void udev_destroy(struct pb_udev *udev)
327 {
328         talloc_free(udev);
329 }