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