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