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