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