parser: Use list to hold parsers
[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 #include <url/url.h>
16
17 #include "device-handler.h"
18 #include "discover-server.h"
19 #include "event.h"
20 #include "parser.h"
21 #include "resource.h"
22 #include "udev.h"
23 #include "paths.h"
24 #include "boot.h"
25
26 struct device_handler {
27         struct discover_server  *server;
28         int                     dry_run;
29
30         struct discover_device  **devices;
31         unsigned int            n_devices;
32
33         struct list             unresolved_boot_options;
34 };
35
36 void discover_context_add_boot_option(struct discover_context *ctx,
37                 struct discover_boot_option *boot_option)
38 {
39         boot_option->source = ctx->parser;
40         list_add_tail(&ctx->boot_options, &boot_option->list);
41         talloc_steal(ctx, boot_option);
42 }
43
44 /**
45  * device_handler_get_device_count - Get the count of current handler devices.
46  */
47
48 int device_handler_get_device_count(const struct device_handler *handler)
49 {
50         return handler->n_devices;
51 }
52
53 /**
54  * device_handler_get_device - Get a handler device by index.
55  */
56
57 const struct discover_device *device_handler_get_device(
58         const struct device_handler *handler, unsigned int index)
59 {
60         if (index >= handler->n_devices) {
61                 assert(0 && "bad index");
62                 return NULL;
63         }
64
65         return handler->devices[index];
66 }
67
68 struct discover_boot_option *discover_boot_option_create(
69                 struct discover_context *ctx,
70                 struct discover_device *device)
71 {
72         struct discover_boot_option *opt;
73
74         opt = talloc_zero(ctx, struct discover_boot_option);
75         opt->option = talloc_zero(opt, struct boot_option);
76         opt->device = device;
77
78         return opt;
79 }
80
81 static int device_match_path(struct discover_device *dev, const char *path)
82 {
83         return dev->device_path && !strcmp(dev->device_path, path);
84 }
85
86 static int device_match_uuid(struct discover_device *dev, const char *uuid)
87 {
88         return dev->uuid && !strcmp(dev->uuid, uuid);
89 }
90
91 static int device_match_label(struct discover_device *dev, const char *label)
92 {
93         return dev->label && !strcmp(dev->label, label);
94 }
95
96 static int device_match_id(struct discover_device *dev, const char *id)
97 {
98         return !strcmp(dev->device->id, id);
99 }
100
101 static struct discover_device *device_lookup(
102                 struct device_handler *device_handler,
103                 int (match_fn)(struct discover_device *, const char *),
104                 const char *str)
105 {
106         struct discover_device *dev;
107         unsigned int i;
108
109         if (!str)
110                 return NULL;
111
112         for (i = 0; i < device_handler->n_devices; i++) {
113                 dev = device_handler->devices[i];
114
115                 if (match_fn(dev, str))
116                         return dev;
117         }
118
119         return NULL;
120 }
121
122 struct discover_device *device_lookup_by_name(struct device_handler *handler,
123                 const char *name)
124 {
125         struct discover_device *dev;
126         char *path;
127
128         if (strncmp(name, "/dev/", strlen("/dev/")))
129                 path = talloc_asprintf(NULL, "/dev/%s", name);
130         else
131                 path = talloc_strdup(NULL, name);
132
133         dev = device_lookup_by_path(handler, path);
134
135         talloc_free(path);
136
137         return dev;
138 }
139
140 struct discover_device *device_lookup_by_path(
141                 struct device_handler *device_handler,
142                 const char *path)
143 {
144         return device_lookup(device_handler, device_match_path, path);
145 }
146
147 struct discover_device *device_lookup_by_uuid(
148                 struct device_handler *device_handler,
149                 const char *uuid)
150 {
151         return device_lookup(device_handler, device_match_uuid, uuid);
152 }
153
154 struct discover_device *device_lookup_by_label(
155                 struct device_handler *device_handler,
156                 const char *label)
157 {
158         return device_lookup(device_handler, device_match_label, label);
159 }
160
161 struct discover_device *device_lookup_by_id(
162                 struct device_handler *device_handler,
163                 const char *id)
164 {
165         return device_lookup(device_handler, device_match_id, id);
166 }
167
168 void device_handler_destroy(struct device_handler *handler)
169 {
170         talloc_free(handler);
171 }
172
173 #ifdef PETITBOOT_TEST
174
175 /* we have a simplified interface for petitboot testing, but still want
176  * to keep struct device_handler opaque. */
177 struct device_handler *device_handler_init(
178                 struct discover_server *server __attribute__((unused)),
179                 int dry_run __attribute__((unused)))
180 {
181         struct device_handler *handler;
182
183         handler = talloc_zero(NULL, struct device_handler);
184         list_init(&handler->unresolved_boot_options);
185
186         return handler;
187 }
188
189 void device_handler_add_device(struct device_handler *handler,
190                 struct discover_device *dev)
191 {
192         handler->n_devices++;
193         handler->devices = talloc_realloc(handler, handler->devices,
194                 struct discover_device *, handler->n_devices);
195         handler->devices[handler->n_devices - 1] = dev;
196 }
197
198 #else
199
200 static int mount_device(struct discover_device *dev)
201 {
202         const char *argv[6];
203
204         if (!dev->device_path)
205                 return -1;
206
207         if (!dev->mount_path)
208                 dev->mount_path = join_paths(dev, mount_base(),
209                                                 dev->device_path);
210
211         if (pb_mkdir_recursive(dev->mount_path))
212                 pb_log("couldn't create mount directory %s: %s\n",
213                                 dev->mount_path, strerror(errno));
214
215         argv[0] = pb_system_apps.mount;
216         argv[1] = dev->device_path;
217         argv[2] = dev->mount_path;
218         argv[3] = "-o";
219         argv[4] = "ro";
220         argv[5] = NULL;
221
222         if (pb_run_cmd(argv, 1, 0)) {
223
224                 /* Retry mount without ro option. */
225
226                 argv[0] = pb_system_apps.mount;
227                 argv[1] = dev->device_path;
228                 argv[2] = dev->mount_path;
229                 argv[3] = NULL;
230
231                 if (pb_run_cmd(argv, 1, 0))
232                         goto out_rmdir;
233         }
234
235         return 0;
236
237 out_rmdir:
238         pb_rmdir_recursive(mount_base(), dev->mount_path);
239         return -1;
240 }
241
242 static int umount_device(struct discover_device *dev)
243 {
244         int status;
245         pid_t pid;
246
247         if (!dev->mount_path)
248                 return 0;
249
250         pid = fork();
251         if (pid == -1) {
252                 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
253                 return -1;
254         }
255
256         if (pid == 0) {
257                 execl(pb_system_apps.umount, pb_system_apps.umount,
258                                                 dev->mount_path, NULL);
259                 exit(EXIT_FAILURE);
260         }
261
262         if (waitpid(pid, &status, 0) == -1) {
263                 pb_log("%s: waitpid failed: %s\n", __func__,
264                                 strerror(errno));
265                 return -1;
266         }
267
268         if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
269                 return -1;
270
271         pb_rmdir_recursive(mount_base(), dev->mount_path);
272
273         return 0;
274 }
275
276 struct device_handler *device_handler_init(struct discover_server *server,
277                 int dry_run)
278 {
279         struct device_handler *handler;
280
281         handler = talloc(NULL, struct device_handler);
282         handler->devices = NULL;
283         handler->n_devices = 0;
284         handler->server = server;
285         handler->dry_run = dry_run;
286         list_init(&handler->unresolved_boot_options);
287
288         /* set up our mount point base */
289         pb_mkdir_recursive(mount_base());
290
291         parser_init();
292
293         return handler;
294 }
295
296 static int destroy_device(void *arg)
297 {
298         struct discover_device *dev = arg;
299
300         umount_device(dev);
301
302         return 0;
303 }
304
305 static struct discover_device *find_device(struct device_handler *handler,
306                 const char *id)
307 {
308         struct discover_device *dev;
309         unsigned int i;
310
311         for (i = 0; i < handler->n_devices; i++) {
312                 dev = handler->devices[i];
313                 if (!strcmp(dev->device->id, id))
314                         return dev;
315         }
316
317         return NULL;
318 }
319
320 static struct discover_device *discover_device_create(
321                 struct device_handler *handler,
322                 struct discover_context *ctx,
323                 struct event *event)
324 {
325         struct discover_device *dev;
326         const char *devname;
327
328         dev = find_device(handler, event->device);
329         if (dev)
330                 return dev;
331
332         dev = talloc_zero(ctx, struct discover_device);
333         dev->device = talloc_zero(dev, struct device);
334         list_init(&dev->boot_options);
335
336         devname = event_get_param(ctx->event, "DEVNAME");
337         if (devname)
338                 dev->device_path = talloc_strdup(dev, devname);
339
340         dev->device->id = talloc_strdup(dev, event->device);
341
342         talloc_set_destructor(dev, destroy_device);
343
344         return dev;
345 }
346
347 /**
348  * device_handler_remove - Remove a device from the handler device array.
349  */
350
351 static void device_handler_remove(struct device_handler *handler,
352         struct discover_device *device)
353 {
354         unsigned int i;
355
356         for (i = 0; i < handler->n_devices; i++)
357                 if (handler->devices[i] == device)
358                         break;
359
360         if (i == handler->n_devices) {
361                 assert(0 && "unknown device");
362                 return;
363         }
364
365         handler->n_devices--;
366         memmove(&handler->devices[i], &handler->devices[i + 1],
367                 (handler->n_devices - i) * sizeof(handler->devices[0]));
368         handler->devices = talloc_realloc(handler, handler->devices,
369                 struct discover_device *, handler->n_devices);
370
371         discover_server_notify_device_remove(handler->server, device->device);
372
373         talloc_free(device);
374 }
375
376 static bool resource_is_resolved(struct resource *res)
377 {
378         return !res || res->resolved;
379 }
380
381 /* We only use this in an assert, which will disappear if we're compiling
382  * with NDEBUG, so we need the 'used' attribute for these builds */
383 static bool __attribute__((used)) boot_option_is_resolved(
384                 struct discover_boot_option *opt)
385 {
386         return resource_is_resolved(opt->boot_image) &&
387                 resource_is_resolved(opt->initrd) &&
388                 resource_is_resolved(opt->icon);
389 }
390
391 static bool resource_resolve(struct resource *res, const char *name,
392                 struct discover_boot_option *opt,
393                 struct device_handler *handler)
394 {
395         struct parser *parser = opt->source;
396
397         if (resource_is_resolved(res))
398                 return true;
399
400         pb_log("Attempting to resolve resource %s->%s with parser %s\n",
401                         opt->option->id, name, parser->name);
402         parser->resolve_resource(handler, res);
403
404         return res->resolved;
405 }
406
407 static bool boot_option_resolve(struct discover_boot_option *opt,
408                 struct device_handler *handler)
409 {
410         return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
411                 resource_resolve(opt->initrd, "initrd", opt, handler) &&
412                 resource_resolve(opt->icon, "icon", opt, handler);
413 }
414
415 static void boot_option_finalise(struct discover_boot_option *opt)
416 {
417         assert(boot_option_is_resolved(opt));
418
419         /* check that the parsers haven't set any of the final data */
420         assert(!opt->option->boot_image_file);
421         assert(!opt->option->initrd_file);
422         assert(!opt->option->icon_file);
423         assert(!opt->option->device_id);
424
425         if (opt->boot_image)
426                 opt->option->boot_image_file = opt->boot_image->url->full;
427         if (opt->initrd)
428                 opt->option->initrd_file = opt->initrd->url->full;
429         if (opt->icon)
430                 opt->option->icon_file = opt->icon->url->full;
431
432         opt->option->device_id = opt->device->device->id;
433 }
434
435 static void process_boot_option_queue(struct device_handler *handler)
436 {
437         struct discover_boot_option *opt, *tmp;
438
439         list_for_each_entry_safe(&handler->unresolved_boot_options,
440                         opt, tmp, list) {
441
442                 pb_log("queue: attempting resolution for %s\n",
443                                 opt->option->id);
444
445                 if (!boot_option_resolve(opt, handler))
446                         continue;
447
448                 pb_log("\tresolved!\n");
449
450                 list_remove(&opt->list);
451                 list_add(&opt->device->boot_options, &opt->list);
452                 talloc_steal(opt->device, opt);
453                 boot_option_finalise(opt);
454                 discover_server_notify_boot_option_add(handler->server,
455                                                         opt->option);
456         }
457 }
458
459 /**
460  * context_commit - Commit a temporary discovery context to the handler,
461  * and notify the clients about any new options / devices
462  */
463 static void context_commit(struct device_handler *handler,
464                 struct discover_context *ctx)
465 {
466         struct discover_device *dev = ctx->device;
467         struct discover_boot_option *opt, *tmp;
468         unsigned int i, existing_device = 0;
469
470         /* do we already have this device? */
471         for (i = 0; i < handler->n_devices; i++) {
472                 if (ctx->device == handler->devices[i]) {
473                         existing_device = 1;
474                         break;
475                 }
476         }
477
478         /* if not already present, add the device to the handler's array */
479         if (!existing_device) {
480                 handler->n_devices++;
481                 handler->devices = talloc_realloc(handler, handler->devices,
482                         struct discover_device *, handler->n_devices);
483                 handler->devices[handler->n_devices - 1] = dev;
484                 talloc_steal(handler, dev);
485
486                 discover_server_notify_device_add(handler->server, dev->device);
487
488                 /* this new device might be able to resolve existing boot
489                  * options */
490                 pb_log("New device %s, processing queue\n", dev->device->id);
491                 process_boot_option_queue(handler);
492         }
493
494
495         /* move boot options from the context to the device */
496         list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
497                 list_remove(&opt->list);
498
499                 if (boot_option_resolve(opt, handler)) {
500                         pb_log("boot option %s is resolved, "
501                                         "sending to clients\n",
502                                         opt->option->id);
503                         list_add(&dev->boot_options, &opt->list);
504                         talloc_steal(dev, opt);
505                         boot_option_finalise(opt);
506                         discover_server_notify_boot_option_add(handler->server,
507                                                                 opt->option);
508                 } else {
509                         if (!opt->source->resolve_resource) {
510                                 pb_log("parser %s gave us an unresolved "
511                                         "resource (%s), but no way to "
512                                         "resolve it\n",
513                                         opt->source->name, opt->option->id);
514                                 talloc_free(opt);
515                         } else {
516                                 pb_log("boot option %s is unresolved, "
517                                                 "adding to queue\n",
518                                                 opt->option->id);
519                                 list_add(&handler->unresolved_boot_options,
520                                                 &opt->list);
521                                 talloc_steal(handler, opt);
522                         }
523                 }
524         }
525 }
526
527 static int handle_add_udev_event(struct device_handler *handler,
528                 struct event *event)
529 {
530         struct discover_context *ctx;
531         struct discover_device *dev;
532         const char *param;
533         int rc;
534
535         /* create our context */
536         ctx = talloc(handler, struct discover_context);
537         ctx->event = event;
538         list_init(&ctx->boot_options);
539
540         /* create our top-level device */
541         dev = discover_device_create(handler, ctx, event);
542
543         ctx->device = dev;
544
545         /* try to parse UUID and labels */
546         param = event_get_param(ctx->event, "ID_FS_UUID");
547         if (param)
548                 dev->uuid = talloc_strdup(dev, param);
549
550         param = event_get_param(ctx->event, "ID_FS_LABEL");
551         if (param)
552                 dev->label = talloc_strdup(dev, param);
553
554         rc = mount_device(dev);
555         if (rc) {
556                 talloc_free(ctx);
557                 return 0;
558         }
559
560         /* run the parsers. This will populate the ctx's boot_option list. */
561         iterate_parsers(ctx, CONF_METHOD_LOCAL_FILE);
562
563         /* add discovered stuff to the handler */
564         context_commit(handler, ctx);
565
566         talloc_free(ctx);
567
568         return 0;
569 }
570
571 static int handle_remove_udev_event(struct device_handler *handler,
572                 struct event *event)
573 {
574         struct discover_device *dev;
575
576         dev = find_device(handler, event->device);
577         if (!dev)
578                 return 0;
579
580         /* remove device from handler device array */
581         device_handler_remove(handler, dev);
582
583         return 0;
584 }
585
586 static int handle_add_user_event(struct device_handler *handler,
587                 struct event *event)
588 {
589         struct discover_context *ctx;
590         struct discover_device *dev;
591         int rc;
592
593         assert(event->device);
594
595         ctx = talloc(handler, struct discover_context);
596         ctx->event = event;
597         list_init(&ctx->boot_options);
598
599         dev = discover_device_create(handler, ctx, event);
600         ctx->device = dev;
601
602         rc = parse_user_event(ctx, event);
603
604         if (!rc)
605                 context_commit(handler, ctx);
606
607         return rc;
608 }
609
610 static int handle_remove_user_event(struct device_handler *handler,
611                 struct event *event)
612 {
613         struct discover_device *dev = find_device(handler, event->device);
614
615         if (!dev)
616                 return 0;
617
618         /* remove device from handler device array */
619         device_handler_remove(handler, dev);
620
621         return 0;
622 }
623
624 static enum conf_method parse_conf_method(const char *str)
625 {
626
627         if (!strcasecmp(str, "dhcp")) {
628                 return CONF_METHOD_DHCP;
629         }
630         return CONF_METHOD_UNKNOWN;
631 }
632
633 static int handle_conf_user_event(struct device_handler *handler,
634                 struct event *event)
635 {
636         struct discover_context *ctx;
637         struct discover_device *dev;
638         enum conf_method method;
639         const char *val;
640
641         ctx = talloc(handler, struct discover_context);
642         ctx->event = event;
643         list_init(&ctx->boot_options);
644
645         val = event_get_param(event, "url");
646         if (!val) {
647                 talloc_free(ctx);
648                 return 0;
649         }
650
651         ctx->conf_url = pb_url_parse(ctx, val);
652         if (!ctx->conf_url) {
653                 talloc_free(ctx);
654                 return 0;
655         }
656
657         val = event_get_param(event, "method");
658         if (!val) {
659                 talloc_free(ctx);
660                 return 0;
661         }
662
663         method = parse_conf_method(val);
664         if (method == CONF_METHOD_UNKNOWN) {
665                 talloc_free(ctx);
666                 return 0;
667         }
668
669         dev = discover_device_create(handler, ctx, event);
670         ctx->device = dev;
671
672         iterate_parsers(ctx, method);
673
674         context_commit(handler, ctx);
675
676         return 0;
677 }
678
679 typedef int (*event_handler)(struct device_handler *, struct event *);
680
681 static event_handler handlers[EVENT_TYPE_MAX][EVENT_ACTION_MAX] = {
682         [EVENT_TYPE_UDEV] = {
683                 [EVENT_ACTION_ADD]      = handle_add_udev_event,
684                 [EVENT_ACTION_REMOVE]   = handle_remove_udev_event,
685         },
686         [EVENT_TYPE_USER] = {
687                 [EVENT_ACTION_ADD]      = handle_add_user_event,
688                 [EVENT_ACTION_REMOVE]   = handle_remove_user_event,
689                 [EVENT_ACTION_CONF]     = handle_conf_user_event,
690         }
691 };
692
693 int device_handler_event(struct device_handler *handler,
694                 struct event *event)
695 {
696         if (event->type >= EVENT_TYPE_MAX ||
697                         event->action >= EVENT_ACTION_MAX ||
698                         !handlers[event->type][event->action]) {
699                 pb_log("%s unknown type/action: %d/%d\n", __func__,
700                                 event->type, event->action);
701                 return 0;
702         }
703
704         return handlers[event->type][event->action](handler, event);
705 }
706
707 static void boot_status(void *arg, struct boot_status *status)
708 {
709         struct device_handler *handler = arg;
710
711         discover_server_notify_boot_status(handler->server, status);
712 }
713
714 static struct discover_boot_option *find_boot_option_by_id(
715                 struct device_handler *handler, const char *id)
716 {
717         unsigned int i;
718
719         for (i = 0; i < handler->n_devices; i++) {
720                 struct discover_device *dev = handler->devices[i];
721                 struct discover_boot_option *opt;
722
723                 list_for_each_entry(&dev->boot_options, opt, list)
724                         if (!strcmp(opt->option->id, id))
725                                 return opt;
726         }
727
728         return NULL;
729 }
730
731 void device_handler_boot(struct device_handler *handler,
732                 struct boot_command *cmd)
733 {
734         struct discover_boot_option *opt;
735
736         opt = find_boot_option_by_id(handler, cmd->option_id);
737
738         boot(handler, opt, cmd, handler->dry_run, boot_status, handler);
739 }
740 #endif