discover: remove register_parser printf
[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 *serial;
74         const char *path;
75         const char *name;
76         const char *prop;
77
78         name = udev_device_get_sysname(dev);
79         if (!name) {
80                 pb_debug("udev_device_get_sysname failed\n");
81                 return -1;
82         }
83
84         typestr = udev_device_get_devtype(dev);
85         if (!typestr) {
86                 pb_debug("udev_device_get_devtype failed\n");
87                 return -1;
88         }
89
90         if (!(!strcmp(typestr, "disk") || !strcmp(typestr, "partition"))) {
91                 pb_debug("SKIP %s: invalid type %s\n", name, typestr);
92                 return 0;
93         }
94
95         path = udev_device_get_devpath(dev);
96         if (path && (strstr(path, "virtual/block/loop")
97                         || strstr(path, "virtual/block/ram"))) {
98                 pb_debug("SKIP: %s: ignored (path=%s)\n", name, path);
99                 return 0;
100         }
101
102         /* We have enough info to create the device and start discovery */
103         ddev = device_lookup_by_id(udev->handler, name);
104         if (ddev) {
105                 pb_debug("device %s is already present?\n", name);
106                 return -1;
107         }
108
109         /* we may also see multipath devices; same dev nodes (hence id), but
110          * different serial numbers */
111         serial = udev_device_get_property_value(dev, "ID_SERIAL");
112         if (serial && device_lookup_by_serial(udev->handler, serial))
113                 return -1;
114
115         ddev = discover_device_create(udev->handler, name);
116
117         ddev->device_path = udev_device_get_devnode(dev);
118         prop = udev_device_get_property_value(dev, "ID_FS_UUID");
119         if (prop)
120                 ddev->uuid = talloc_strdup(ddev, prop);
121         prop = udev_device_get_property_value(dev, "ID_FS_LABEL");
122         if (prop)
123                 ddev->label = talloc_strdup(ddev, prop);
124         ddev->device->type = DEVICE_TYPE_DISK;
125
126         udev_setup_device_params(dev, ddev);
127
128         device_handler_discover(udev->handler, ddev, CONF_METHOD_LOCAL_FILE);
129
130         return 0;
131 }
132
133 static int udev_handle_dev_remove(struct pb_udev *udev, struct udev_device *dev)
134 {
135         struct discover_device *ddev;
136         const char *name;
137
138         name = udev_device_get_sysname(dev);
139         if (!name) {
140                 pb_debug("udev_device_get_sysname failed\n");
141                 return -1;
142         }
143
144         ddev = device_lookup_by_id(udev->handler, name);
145         if (!ddev)
146                 return 0;
147
148         device_handler_remove(udev->handler, ddev);
149
150         return 0;
151 }
152 static int udev_handle_dev_action(struct udev_device *dev, const char *action)
153 {
154         struct pb_udev *udev = udev_get_userdata(udev_device_get_udev(dev));
155
156         if (!strcmp(action, "add"))
157                 return udev_handle_dev_add(udev, dev);
158
159         else if (!strcmp(action, "remove"))
160                 return udev_handle_dev_remove(udev, dev);
161
162         return 0;
163 }
164
165 static int udev_enumerate(struct udev *udev)
166 {
167         int result;
168         struct udev_list_entry *list, *entry;
169         struct udev_enumerate *enumerate;
170
171         enumerate = udev_enumerate_new(udev);
172
173         if (!enumerate) {
174                 pb_log("udev_enumerate_new failed\n");
175                 return -1;
176         }
177
178         result = udev_enumerate_add_match_subsystem(enumerate, "block");
179
180         if (result) {
181                 pb_log("udev_enumerate_add_match_subsystem failed\n");
182                 goto fail;
183         }
184
185         udev_enumerate_scan_devices(enumerate);
186
187         list = udev_enumerate_get_list_entry(enumerate);
188
189         if (!list) {
190                 pb_log("udev_enumerate_get_list_entry failed\n");
191                 goto fail;
192         }
193
194         udev_list_entry_foreach(entry, list) {
195                 const char *syspath;
196                 struct udev_device *dev;
197
198                 syspath = udev_list_entry_get_name(entry);
199                 dev = udev_device_new_from_syspath(udev, syspath);
200
201                 udev_handle_dev_action(dev, "add");
202
203                 udev_device_unref(dev);
204         }
205
206         udev_enumerate_unref(enumerate);
207         return 0;
208
209 fail:
210         udev_enumerate_unref(enumerate);
211         return -1;
212 }
213
214 static int udev_setup_monitor(struct udev *udev, struct udev_monitor **monitor)
215 {
216         int result;
217         struct udev_monitor *m;
218
219         *monitor = NULL;
220         m = udev_monitor_new_from_netlink(udev, "udev");
221
222         if (!m) {
223                 pb_log("udev_monitor_new_from_netlink failed\n");
224                 goto out_err;
225         }
226
227         result = udev_monitor_filter_add_match_subsystem_devtype(m, "block",
228                 NULL);
229
230         if (result) {
231                 pb_log("udev_monitor_filter_add_match_subsystem_devtype failed\n");
232                 goto out_err;
233         }
234
235         result = udev_monitor_enable_receiving(m);
236
237         if (result) {
238                 pb_log("udev_monitor_enable_receiving failed\n");
239                 goto out_err;
240         }
241
242         *monitor = m;
243         return 0;
244
245 out_err:
246         udev_monitor_unref(m);
247         return -1;
248 }
249
250 /*
251  * udev_process - waiter callback for monitor netlink.
252  */
253
254 static int udev_process(void *arg)
255 {
256         struct udev_monitor *monitor = arg;
257         struct udev_device *dev;
258         const char *action;
259         int result;
260
261         dev = udev_monitor_receive_device(monitor);
262
263         if (!dev) {
264                 pb_log("udev_monitor_receive_device failed\n");
265                 return -1;
266         }
267
268         action = udev_device_get_action(dev);
269
270         if (!action) {
271                 pb_log("udev_device_get_action failed\n");
272                 goto fail;
273         }
274
275         result = udev_handle_dev_action(dev, action);
276
277         udev_device_unref(dev);
278         return result;
279
280 fail:
281         udev_device_unref(dev);
282         return -1;
283 }
284
285 static void udev_log_fn(struct udev __attribute__((unused)) *udev,
286         int __attribute__((unused)) priority, const char *file, int line,
287         const char *fn, const char *format, va_list args)
288 {
289       pb_log("libudev: %s %s:%d: ", fn, file, line);
290       vfprintf(pb_log_get_stream(), format, args);
291 }
292
293 struct pb_udev *udev_init(struct waitset *waitset,
294         struct device_handler *handler)
295 {
296         int result;
297         struct pb_udev *udev = talloc(NULL, struct pb_udev);
298
299         talloc_set_destructor(udev, udev_destructor);
300         udev->handler = handler;
301
302         udev->udev = udev_new();
303
304         if (!udev->udev) {
305                 pb_log("udev_new failed\n");
306                 goto fail_new;
307         }
308
309         udev_set_userdata(udev->udev, udev);
310
311         udev_set_log_fn(udev->udev, udev_log_fn);
312
313         result = udev_enumerate(udev->udev);
314
315         if (result)
316                 goto fail_enumerate;
317
318         result = udev_setup_monitor(udev->udev, &udev->monitor);
319
320         if (result)
321                 goto fail_monitor;
322
323         waiter_register_io(waitset, udev_monitor_get_fd(udev->monitor), WAIT_IN,
324                 udev_process, udev->monitor);
325
326         pb_log("%s: waiting on udev\n", __func__);
327
328         return udev;
329
330 fail_monitor:
331 fail_enumerate:
332         udev_unref(udev->udev);
333 fail_new:
334         talloc_free(udev);
335         return NULL;
336 }
337
338 void udev_destroy(struct pb_udev *udev)
339 {
340         talloc_free(udev);
341 }