lib/process: replace pb_run_cmd
[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 struct discover_device *discover_device_create(
308                 struct device_handler *handler,
309                 struct discover_context *ctx,
310                 struct event *event)
311 {
312         struct discover_device *dev;
313         const char *devname;
314
315         dev = find_device(handler, event->device);
316         if (dev)
317                 return dev;
318
319         dev = talloc_zero(ctx, struct discover_device);
320         dev->device = talloc_zero(dev, struct device);
321         list_init(&dev->boot_options);
322
323         devname = event_get_param(ctx->event, "DEVNAME");
324         if (devname)
325                 dev->device_path = talloc_strdup(dev, devname);
326
327         dev->device->id = talloc_strdup(dev, event->device);
328
329         talloc_set_destructor(dev, destroy_device);
330
331         return dev;
332 }
333
334 /**
335  * device_handler_remove - Remove a device from the handler device array.
336  */
337
338 static void device_handler_remove(struct device_handler *handler,
339         struct discover_device *device)
340 {
341         unsigned int i;
342
343         for (i = 0; i < handler->n_devices; i++)
344                 if (handler->devices[i] == device)
345                         break;
346
347         if (i == handler->n_devices) {
348                 assert(0 && "unknown device");
349                 return;
350         }
351
352         handler->n_devices--;
353         memmove(&handler->devices[i], &handler->devices[i + 1],
354                 (handler->n_devices - i) * sizeof(handler->devices[0]));
355         handler->devices = talloc_realloc(handler, handler->devices,
356                 struct discover_device *, handler->n_devices);
357
358         discover_server_notify_device_remove(handler->server, device->device);
359
360         talloc_free(device);
361 }
362
363 static void boot_status(void *arg, struct boot_status *status)
364 {
365         struct device_handler *handler = arg;
366
367         discover_server_notify_boot_status(handler->server, status);
368 }
369
370 static void countdown_status(struct device_handler *handler,
371                 struct discover_boot_option *opt, unsigned int sec)
372 {
373         struct boot_status status;
374
375         status.type = BOOT_STATUS_INFO;
376         status.progress = -1;
377         status.detail = NULL;
378         status.message = talloc_asprintf(handler,
379                         "Booting %s in %u sec", opt->option->name, sec);
380
381         discover_server_notify_boot_status(handler->server, &status);
382
383         talloc_free(status.message);
384 }
385
386 static int default_timeout(void *arg)
387 {
388         struct device_handler *handler = arg;
389         struct discover_boot_option *opt;
390
391         if (!handler->default_boot_option)
392                 return 0;
393
394         opt = handler->default_boot_option;
395
396         if (handler->sec_to_boot) {
397                 countdown_status(handler, opt, handler->sec_to_boot);
398                 handler->sec_to_boot--;
399                 handler->timeout_waiter = waiter_register_timeout(
400                                                 handler->waitset, 1000,
401                                                 default_timeout, handler);
402                 return 0;
403         }
404
405         pb_log("Timeout expired, booting default option %s\n", opt->option->id);
406
407         boot(handler, handler->default_boot_option, NULL,
408                         handler->dry_run, boot_status, handler);
409         return 0;
410 }
411
412 static void set_default(struct device_handler *handler,
413                 struct discover_boot_option *opt)
414 {
415         if (handler->default_boot_option)
416                 return;
417
418         if (!handler->autoboot_enabled)
419                 return;
420
421         handler->default_boot_option = opt;
422         handler->sec_to_boot = DEFAULT_BOOT_TIMEOUT_SEC;
423
424         pb_log("Boot option %s set as default, timeout %u sec.\n",
425                opt->option->id, handler->sec_to_boot);
426
427         default_timeout(handler);
428 }
429
430 static bool resource_is_resolved(struct resource *res)
431 {
432         return !res || res->resolved;
433 }
434
435 /* We only use this in an assert, which will disappear if we're compiling
436  * with NDEBUG, so we need the 'used' attribute for these builds */
437 static bool __attribute__((used)) boot_option_is_resolved(
438                 struct discover_boot_option *opt)
439 {
440         return resource_is_resolved(opt->boot_image) &&
441                 resource_is_resolved(opt->initrd) &&
442                 resource_is_resolved(opt->dtb) &&
443                 resource_is_resolved(opt->icon);
444 }
445
446 static bool resource_resolve(struct resource *res, const char *name,
447                 struct discover_boot_option *opt,
448                 struct device_handler *handler)
449 {
450         struct parser *parser = opt->source;
451
452         if (resource_is_resolved(res))
453                 return true;
454
455         pb_log("Attempting to resolve resource %s->%s with parser %s\n",
456                         opt->option->id, name, parser->name);
457         parser->resolve_resource(handler, res);
458
459         return res->resolved;
460 }
461
462 static bool boot_option_resolve(struct discover_boot_option *opt,
463                 struct device_handler *handler)
464 {
465         return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
466                 resource_resolve(opt->initrd, "initrd", opt, handler) &&
467                 resource_resolve(opt->dtb, "dtb", opt, handler) &&
468                 resource_resolve(opt->icon, "icon", opt, handler);
469 }
470
471 static void boot_option_finalise(struct device_handler *handler,
472                 struct discover_boot_option *opt)
473 {
474         assert(boot_option_is_resolved(opt));
475
476         /* check that the parsers haven't set any of the final data */
477         assert(!opt->option->boot_image_file);
478         assert(!opt->option->initrd_file);
479         assert(!opt->option->dtb_file);
480         assert(!opt->option->icon_file);
481         assert(!opt->option->device_id);
482
483         if (opt->boot_image)
484                 opt->option->boot_image_file = opt->boot_image->url->full;
485         if (opt->initrd)
486                 opt->option->initrd_file = opt->initrd->url->full;
487         if (opt->dtb)
488                 opt->option->dtb_file = opt->dtb->url->full;
489         if (opt->icon)
490                 opt->option->icon_file = opt->icon->url->full;
491
492         opt->option->device_id = opt->device->device->id;
493
494         if (opt->option->is_default)
495                 set_default(handler, opt);
496 }
497
498 static void process_boot_option_queue(struct device_handler *handler)
499 {
500         struct discover_boot_option *opt, *tmp;
501
502         list_for_each_entry_safe(&handler->unresolved_boot_options,
503                         opt, tmp, list) {
504
505                 pb_log("queue: attempting resolution for %s\n",
506                                 opt->option->id);
507
508                 if (!boot_option_resolve(opt, handler))
509                         continue;
510
511                 pb_log("\tresolved!\n");
512
513                 list_remove(&opt->list);
514                 list_add_tail(&opt->device->boot_options, &opt->list);
515                 talloc_steal(opt->device, opt);
516                 boot_option_finalise(handler, opt);
517                 discover_server_notify_boot_option_add(handler->server,
518                                                         opt->option);
519         }
520 }
521
522 /**
523  * context_commit - Commit a temporary discovery context to the handler,
524  * and notify the clients about any new options / devices
525  */
526 static void context_commit(struct device_handler *handler,
527                 struct discover_context *ctx)
528 {
529         struct discover_device *dev = ctx->device;
530         struct discover_boot_option *opt, *tmp;
531         unsigned int i, existing_device = 0;
532
533         /* do we already have this device? */
534         for (i = 0; i < handler->n_devices; i++) {
535                 if (ctx->device == handler->devices[i]) {
536                         existing_device = 1;
537                         break;
538                 }
539         }
540
541         /* if not already present, add the device to the handler's array */
542         if (!existing_device) {
543                 handler->n_devices++;
544                 handler->devices = talloc_realloc(handler, handler->devices,
545                         struct discover_device *, handler->n_devices);
546                 handler->devices[handler->n_devices - 1] = dev;
547                 talloc_steal(handler, dev);
548
549                 discover_server_notify_device_add(handler->server, dev->device);
550
551                 /* this new device might be able to resolve existing boot
552                  * options */
553                 pb_log("New device %s, processing queue\n", dev->device->id);
554                 process_boot_option_queue(handler);
555         }
556
557
558         /* move boot options from the context to the device */
559         list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
560                 list_remove(&opt->list);
561
562                 if (boot_option_resolve(opt, handler)) {
563                         pb_log("boot option %s is resolved, "
564                                         "sending to clients\n",
565                                         opt->option->id);
566                         list_add_tail(&dev->boot_options, &opt->list);
567                         talloc_steal(dev, opt);
568                         boot_option_finalise(handler, opt);
569                         discover_server_notify_boot_option_add(handler->server,
570                                                                 opt->option);
571                 } else {
572                         if (!opt->source->resolve_resource) {
573                                 pb_log("parser %s gave us an unresolved "
574                                         "resource (%s), but no way to "
575                                         "resolve it\n",
576                                         opt->source->name, opt->option->id);
577                                 talloc_free(opt);
578                         } else {
579                                 pb_log("boot option %s is unresolved, "
580                                                 "adding to queue\n",
581                                                 opt->option->id);
582                                 list_add(&handler->unresolved_boot_options,
583                                                 &opt->list);
584                                 talloc_steal(handler, opt);
585                         }
586                 }
587         }
588 }
589
590 static int handle_add_udev_event(struct device_handler *handler,
591                 struct event *event)
592 {
593         struct discover_context *ctx;
594         struct discover_device *dev;
595         const char *param;
596         int rc;
597
598         /* create our context */
599         ctx = talloc(handler, struct discover_context);
600         ctx->event = event;
601         list_init(&ctx->boot_options);
602
603         /* create our top-level device */
604         dev = discover_device_create(handler, ctx, event);
605
606         ctx->device = dev;
607
608         /* try to parse UUID and labels */
609         param = event_get_param(ctx->event, "ID_FS_UUID");
610         if (param)
611                 dev->uuid = talloc_strdup(dev, param);
612
613         param = event_get_param(ctx->event, "ID_FS_LABEL");
614         if (param)
615                 dev->label = talloc_strdup(dev, param);
616
617         rc = mount_device(dev);
618         if (rc) {
619                 talloc_free(ctx);
620                 return 0;
621         }
622
623         /* run the parsers. This will populate the ctx's boot_option list. */
624         iterate_parsers(ctx, CONF_METHOD_LOCAL_FILE);
625
626         /* add discovered stuff to the handler */
627         context_commit(handler, ctx);
628
629         talloc_free(ctx);
630
631         return 0;
632 }
633
634 static int handle_remove_udev_event(struct device_handler *handler,
635                 struct event *event)
636 {
637         struct discover_device *dev;
638
639         dev = find_device(handler, event->device);
640         if (!dev)
641                 return 0;
642
643         /* remove device from handler device array */
644         device_handler_remove(handler, dev);
645
646         return 0;
647 }
648
649 static int handle_add_user_event(struct device_handler *handler,
650                 struct event *event)
651 {
652         struct discover_context *ctx;
653         struct discover_device *dev;
654         int rc;
655
656         assert(event->device);
657
658         ctx = talloc(handler, struct discover_context);
659         ctx->event = event;
660         list_init(&ctx->boot_options);
661
662         dev = discover_device_create(handler, ctx, event);
663         ctx->device = dev;
664
665         rc = parse_user_event(ctx, event);
666
667         if (!rc)
668                 context_commit(handler, ctx);
669
670         return rc;
671 }
672
673 static int handle_remove_user_event(struct device_handler *handler,
674                 struct event *event)
675 {
676         struct discover_device *dev = find_device(handler, event->device);
677
678         if (!dev)
679                 return 0;
680
681         /* remove device from handler device array */
682         device_handler_remove(handler, dev);
683
684         return 0;
685 }
686
687 static enum conf_method parse_conf_method(const char *str)
688 {
689
690         if (!strcasecmp(str, "dhcp")) {
691                 return CONF_METHOD_DHCP;
692         }
693         return CONF_METHOD_UNKNOWN;
694 }
695
696 static int handle_conf_user_event(struct device_handler *handler,
697                 struct event *event)
698 {
699         struct discover_context *ctx;
700         struct discover_device *dev;
701         enum conf_method method;
702         const char *val;
703
704         ctx = talloc(handler, struct discover_context);
705         ctx->event = event;
706         list_init(&ctx->boot_options);
707
708         val = event_get_param(event, "url");
709         if (!val) {
710                 talloc_free(ctx);
711                 return 0;
712         }
713
714         ctx->conf_url = pb_url_parse(ctx, val);
715         if (!ctx->conf_url) {
716                 talloc_free(ctx);
717                 return 0;
718         }
719
720         val = event_get_param(event, "method");
721         if (!val) {
722                 talloc_free(ctx);
723                 return 0;
724         }
725
726         method = parse_conf_method(val);
727         if (method == CONF_METHOD_UNKNOWN) {
728                 talloc_free(ctx);
729                 return 0;
730         }
731
732         dev = discover_device_create(handler, ctx, event);
733         ctx->device = dev;
734
735         iterate_parsers(ctx, method);
736
737         context_commit(handler, ctx);
738
739         return 0;
740 }
741
742 typedef int (*event_handler)(struct device_handler *, struct event *);
743
744 static event_handler handlers[EVENT_TYPE_MAX][EVENT_ACTION_MAX] = {
745         [EVENT_TYPE_UDEV] = {
746                 [EVENT_ACTION_ADD]      = handle_add_udev_event,
747                 [EVENT_ACTION_REMOVE]   = handle_remove_udev_event,
748         },
749         [EVENT_TYPE_USER] = {
750                 [EVENT_ACTION_ADD]      = handle_add_user_event,
751                 [EVENT_ACTION_REMOVE]   = handle_remove_user_event,
752                 [EVENT_ACTION_CONF]     = handle_conf_user_event,
753         }
754 };
755
756 int device_handler_event(struct device_handler *handler,
757                 struct event *event)
758 {
759         if (event->type >= EVENT_TYPE_MAX ||
760                         event->action >= EVENT_ACTION_MAX ||
761                         !handlers[event->type][event->action]) {
762                 pb_log("%s unknown type/action: %d/%d\n", __func__,
763                                 event->type, event->action);
764                 return 0;
765         }
766
767         return handlers[event->type][event->action](handler, event);
768 }
769
770 static struct discover_boot_option *find_boot_option_by_id(
771                 struct device_handler *handler, const char *id)
772 {
773         unsigned int i;
774
775         for (i = 0; i < handler->n_devices; i++) {
776                 struct discover_device *dev = handler->devices[i];
777                 struct discover_boot_option *opt;
778
779                 list_for_each_entry(&dev->boot_options, opt, list)
780                         if (!strcmp(opt->option->id, id))
781                                 return opt;
782         }
783
784         return NULL;
785 }
786
787 void device_handler_boot(struct device_handler *handler,
788                 struct boot_command *cmd)
789 {
790         struct discover_boot_option *opt;
791
792         opt = find_boot_option_by_id(handler, cmd->option_id);
793
794         boot(handler, opt, cmd, handler->dry_run, boot_status, handler);
795 }
796
797 void device_handler_cancel_default(struct device_handler *handler)
798 {
799         struct boot_status status;
800
801         if (handler->timeout_waiter)
802                 waiter_remove(handler->timeout_waiter);
803
804         handler->timeout_waiter = NULL;
805         handler->autoboot_enabled = false;
806
807         /* we only send status if we had a default boot option queued */
808         if (!handler->default_boot_option)
809                 return;
810
811         pb_log("Cancelling default boot option\n");
812
813         handler->default_boot_option = NULL;
814
815         status.type = BOOT_STATUS_INFO;
816         status.progress = -1;
817         status.detail = NULL;
818         status.message = "Default boot cancelled";
819
820         discover_server_notify_boot_status(handler->server, &status);
821 }
822 #endif