a6dbf63e88a07635575e93c24473d3fd24eed9c5
[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                    default_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->default_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 %d 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->default_enabled)
443                 return;
444
445         pb_log("Boot option %s set as default\n", opt->option->id);
446
447         handler->default_boot_option = opt;
448         handler->sec_to_boot = DEFAULT_BOOT_TIMEOUT_SEC;
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->default_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