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