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