discover: Separate temporary and permanent device data
[petitboot] / discover / device-handler.c
1
2 #include <assert.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <string.h>
6 #include <errno.h>
7 #include <sys/stat.h>
8 #include <sys/wait.h>
9
10 #include <talloc/talloc.h>
11 #include <list/list.h>
12 #include <log/log.h>
13 #include <types/types.h>
14 #include <system/system.h>
15
16 #include "device-handler.h"
17 #include "discover-server.h"
18 #include "event.h"
19 #include "parser.h"
20 #include "udev.h"
21 #include "paths.h"
22 #include "boot.h"
23
24 struct device_handler {
25         struct discover_server  *server;
26         int                     dry_run;
27
28         struct discover_device  **devices;
29         unsigned int            n_devices;
30 };
31
32 /**
33  * context_commit - Commit a temporary discovery context to the handler,
34  * and notify the clients about any new options / devices
35  */
36 static void context_commit(struct device_handler *handler,
37                 struct discover_context *ctx)
38 {
39         struct discover_device *dev = ctx->device;
40         unsigned int i, existing_device;
41         struct boot_option *opt, *tmp;
42
43         /* do we already have this device? */
44         for (i = 0; i < handler->n_devices; i++) {
45                 if (ctx->device == handler->devices[i]) {
46                         existing_device = 1;
47                         break;
48                 }
49         }
50
51         /* if not already present, add the device to the handler's array */
52         if (!existing_device) {
53                 handler->n_devices++;
54                 handler->devices = talloc_realloc(handler, handler->devices,
55                         struct discover_device *, handler->n_devices);
56                 handler->devices[handler->n_devices - 1] = dev;
57                 talloc_steal(handler, dev);
58
59                 discover_server_notify_device_add(handler->server, dev->device);
60         }
61
62
63         /* move boot options from the context to the device */
64         list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
65                 list_remove(&opt->list);
66                 list_add(&dev->device->boot_options, &opt->list);
67                 dev->device->n_options++;
68                 discover_server_notify_boot_option_add(handler->server, opt);
69         }
70 }
71
72 void discover_context_add_boot_option(struct discover_context *ctx,
73                 struct boot_option *boot_option)
74 {
75         list_add(&ctx->boot_options, &boot_option->list);
76         talloc_steal(ctx, boot_option);
77 }
78
79 /**
80  * device_handler_remove - Remove a device from the handler device array.
81  */
82
83 static void device_handler_remove(struct device_handler *handler,
84         struct discover_device *device)
85 {
86         unsigned int i;
87
88         for (i = 0; i < handler->n_devices; i++)
89                 if (handler->devices[i] == device)
90                         break;
91
92         if (i == handler->n_devices) {
93                 assert(0 && "unknown device");
94                 return;
95         }
96
97         handler->n_devices--;
98         memmove(&handler->devices[i], &handler->devices[i + 1],
99                 (handler->n_devices - i) * sizeof(handler->devices[0]));
100         handler->devices = talloc_realloc(handler, handler->devices,
101                 struct discover_device *, handler->n_devices);
102
103         discover_server_notify_device_remove(handler->server, device->device);
104
105         talloc_free(device);
106 }
107
108 /**
109  * device_handler_get_device_count - Get the count of current handler devices.
110  */
111
112 int device_handler_get_device_count(const struct device_handler *handler)
113 {
114         return handler->n_devices;
115 }
116
117 /**
118  * device_handler_get_device - Get a handler device by index.
119  */
120
121 const struct device *device_handler_get_device(
122         const struct device_handler *handler, unsigned int index)
123 {
124         if (index >= handler->n_devices) {
125                 assert(0 && "bad index");
126                 return NULL;
127         }
128
129         return handler->devices[index]->device;
130 }
131
132 static void setup_device_links(struct discover_context *ctx)
133 {
134         struct discover_device *dev = ctx->device;
135         struct link {
136                 char *env, *dir;
137         } *link, links[] = {
138                 {
139                         .env = "ID_FS_UUID",
140                         .dir = "disk/by-uuid"
141                 },
142                 {
143                         .env = "ID_FS_LABEL",
144                         .dir = "disk/by-label"
145                 },
146                 {
147                         .env = NULL
148                 }
149         };
150
151         for (link = links; link->env; link++) {
152                 char *enc, *dir, *path;
153                 const char *value;
154
155                 value = event_get_param(ctx->event, link->env);
156                 if (!value || !*value)
157                         continue;
158
159                 enc = encode_label(ctx, value);
160                 dir = join_paths(ctx, mount_base(), link->dir);
161                 path = join_paths(dev, dir, value);
162
163                 if (!pb_mkdir_recursive(dir)) {
164                         unlink(path);
165                         if (symlink(dev->mount_path, path)) {
166                                 pb_log("symlink(%s,%s): %s\n",
167                                                 dev->mount_path, path,
168                                                 strerror(errno));
169                                 talloc_free(path);
170                         } else {
171                                 int i = dev->n_links++;
172                                 dev->links = talloc_realloc(dev,
173                                                 dev->links, char *,
174                                                 dev->n_links);
175                                 dev->links[i] = path;
176                         }
177
178                 }
179
180                 talloc_free(dir);
181                 talloc_free(enc);
182         }
183 }
184
185 static void remove_device_links(struct discover_device *dev)
186 {
187         int i;
188
189         for (i = 0; i < dev->n_links; i++)
190                 unlink(dev->links[i]);
191 }
192
193 static int mount_device(struct discover_context *ctx)
194 {
195         struct discover_device *dev = ctx->device;
196         const char *mountpoint;
197         const char *argv[6];
198
199         if (!dev->mount_path) {
200                 mountpoint = mountpoint_for_device(dev->device_path);
201                 dev->mount_path = talloc_strdup(dev, mountpoint);
202         }
203
204         if (pb_mkdir_recursive(dev->mount_path))
205                 pb_log("couldn't create mount directory %s: %s\n",
206                                 dev->mount_path, strerror(errno));
207
208         argv[0] = pb_system_apps.mount;
209         argv[1] = dev->device_path;
210         argv[2] = dev->mount_path;
211         argv[3] = "-o";
212         argv[4] = "ro";
213         argv[5] = NULL;
214
215         if (pb_run_cmd(argv, 1, 0)) {
216
217                 /* Retry mount without ro option. */
218
219                 argv[0] = pb_system_apps.mount;
220                 argv[1] = dev->device_path;
221                 argv[2] = dev->mount_path;
222                 argv[3] = NULL;
223
224                 if (pb_run_cmd(argv, 1, 0))
225                         goto out_rmdir;
226         }
227
228         setup_device_links(ctx);
229         return 0;
230
231 out_rmdir:
232         pb_rmdir_recursive(mount_base(), dev->mount_path);
233         return -1;
234 }
235
236 static int umount_device(struct discover_device *dev)
237 {
238         int status;
239         pid_t pid;
240
241         remove_device_links(dev);
242
243         pid = fork();
244         if (pid == -1) {
245                 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
246                 return -1;
247         }
248
249         if (pid == 0) {
250                 execl(pb_system_apps.umount, pb_system_apps.umount,
251                                                 dev->mount_path, NULL);
252                 exit(EXIT_FAILURE);
253         }
254
255         if (waitpid(pid, &status, 0) == -1) {
256                 pb_log("%s: waitpid failed: %s\n", __func__,
257                                 strerror(errno));
258                 return -1;
259         }
260
261         if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
262                 return -1;
263
264         pb_rmdir_recursive(mount_base(), dev->mount_path);
265
266         return 0;
267 }
268
269 static struct discover_device *find_device(struct device_handler *handler,
270                 const char *id)
271 {
272         struct discover_device *dev;
273         unsigned int i;
274
275         for (i = 0; i < handler->n_devices; i++) {
276                 dev = handler->devices[i];
277                 if (!strcmp(dev->device->id, id))
278                         return dev;
279         }
280
281         return NULL;
282 }
283
284 static int destroy_device(void *arg)
285 {
286         struct discover_device *dev = arg;
287
288         umount_device(dev);
289
290         return 0;
291 }
292
293 static struct discover_device *discover_device_create(
294                 struct device_handler *handler,
295                 struct discover_context *ctx,
296                 struct event *event)
297 {
298         struct discover_device *dev;
299         const char *devname;
300
301         dev = find_device(handler, event->device);
302         if (dev)
303                 return dev;
304
305         dev = talloc_zero(ctx, struct discover_device);
306         dev->device = talloc_zero(dev, struct device);
307         list_init(&dev->device->boot_options);
308
309         devname = event_get_param(ctx->event, "DEVNAME");
310         if (devname)
311                 dev->device_path = talloc_strdup(dev, devname);
312
313         dev->device->id = talloc_strdup(dev, event->device);
314
315         talloc_set_destructor(dev, destroy_device);
316
317         return dev;
318 }
319
320 static int handle_add_udev_event(struct device_handler *handler,
321                 struct event *event)
322 {
323         struct discover_context *ctx;
324         struct discover_device *dev;
325         int rc;
326
327         /* create our context */
328         ctx = talloc(handler, struct discover_context);
329         ctx->event = event;
330         list_init(&ctx->boot_options);
331
332         /* create our top-level device */
333         dev = discover_device_create(handler, ctx, event);
334
335         ctx->device = dev;
336
337         rc = mount_device(ctx);
338         if (rc) {
339                 talloc_free(ctx);
340                 return 0;
341         }
342
343         /* run the parsers. This will populate the ctx's boot_option list. */
344         iterate_parsers(ctx);
345
346         /* add discovered stuff to the handler */
347         context_commit(handler, ctx);
348
349         talloc_free(ctx);
350
351         return 0;
352 }
353
354 static int handle_remove_udev_event(struct device_handler *handler,
355                 struct event *event)
356 {
357         struct discover_device *dev;
358
359         dev = find_device(handler, event->device);
360         if (!dev)
361                 return 0;
362
363         /* remove device from handler device array */
364         device_handler_remove(handler, dev);
365
366         return 0;
367 }
368
369 static int handle_add_user_event(struct device_handler *handler,
370                 struct event *event)
371 {
372         struct discover_context *ctx;
373         struct discover_device *dev;
374         int rc;
375
376         assert(event->device);
377
378         ctx = talloc(handler, struct discover_context);
379         ctx->event = event;
380         list_init(&ctx->boot_options);
381
382         dev = discover_device_create(handler, ctx, event);
383         ctx->device = dev;
384
385         rc = parse_user_event(ctx, event);
386
387         if (!rc)
388                 context_commit(handler, ctx);
389
390         return rc;
391 }
392
393 static int handle_remove_user_event(struct device_handler *handler,
394                 struct event *event)
395 {
396         struct discover_device *dev = find_device(handler, event->device);
397
398         if (!dev)
399                 return 0;
400
401         /* remove device from handler device array */
402         device_handler_remove(handler, dev);
403
404         return 0;
405 }
406
407 typedef int (*event_handler)(struct device_handler *, struct event *);
408
409 static event_handler handlers[EVENT_TYPE_MAX][EVENT_ACTION_MAX] = {
410         [EVENT_TYPE_UDEV] = {
411                 [EVENT_ACTION_ADD]      = handle_add_udev_event,
412                 [EVENT_ACTION_REMOVE]   = handle_remove_udev_event,
413         },
414         [EVENT_TYPE_USER] = {
415                 [EVENT_ACTION_ADD]      = handle_add_user_event,
416                 [EVENT_ACTION_REMOVE]   = handle_remove_user_event,
417         }
418 };
419
420 int device_handler_event(struct device_handler *handler,
421                 struct event *event)
422 {
423         if (event->type >= EVENT_TYPE_MAX ||
424                         event->action >= EVENT_ACTION_MAX ||
425                         !handlers[event->type][event->action]) {
426                 pb_log("%s unknown type/action: %d/%d\n", __func__,
427                                 event->type, event->action);
428                 return 0;
429         }
430
431         return handlers[event->type][event->action](handler, event);
432 }
433
434 struct device_handler *device_handler_init(struct discover_server *server,
435                 int dry_run)
436 {
437         struct device_handler *handler;
438
439         handler = talloc(NULL, struct device_handler);
440         handler->devices = NULL;
441         handler->n_devices = 0;
442         handler->server = server;
443         handler->dry_run = dry_run;
444
445         /* set up our mount point base */
446         pb_mkdir_recursive(mount_base());
447
448         parser_init();
449
450         return handler;
451 }
452
453 void device_handler_destroy(struct device_handler *handler)
454 {
455         talloc_free(handler);
456 }
457
458 static struct boot_option *find_boot_option_by_id(
459                 struct device_handler *handler, const char *id)
460 {
461         unsigned int i;
462
463         for (i = 0; i < handler->n_devices; i++) {
464                 struct discover_device *dev = handler->devices[i];
465                 struct boot_option *opt;
466
467                 list_for_each_entry(&dev->device->boot_options, opt, list)
468                         if (!strcmp(opt->id, id))
469                                 return opt;
470         }
471
472         return NULL;
473 }
474
475 void device_handler_boot(struct device_handler *handler,
476                 struct boot_command *cmd)
477 {
478         struct boot_option *opt;
479
480         opt = find_boot_option_by_id(handler, cmd->option_id);
481
482         boot(handler, opt, cmd, handler->dry_run);
483 }