95d203920d6f72bf5176c2629e7d2f4aa430a447
[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 "paths.h"
25 #include "boot.h"
26
27 struct device_handler {
28         struct discover_server  *server;
29         int                     dry_run;
30
31         struct discover_device  **devices;
32         unsigned int            n_devices;
33
34         struct waitset          *waitset;
35         struct waiter           *timeout_waiter;
36         bool                    autoboot_enabled;
37         unsigned int            sec_to_boot;
38
39         struct discover_boot_option *default_boot_option;
40         struct list             unresolved_boot_options;
41 };
42
43 static int mount_device(struct discover_device *dev);
44 static int umount_device(struct discover_device *dev);
45
46 void discover_context_add_boot_option(struct discover_context *ctx,
47                 struct discover_boot_option *boot_option)
48 {
49         boot_option->source = ctx->parser;
50         list_add_tail(&ctx->boot_options, &boot_option->list);
51         talloc_steal(ctx, boot_option);
52 }
53
54 /**
55  * device_handler_get_device_count - Get the count of current handler devices.
56  */
57
58 int device_handler_get_device_count(const struct device_handler *handler)
59 {
60         return handler->n_devices;
61 }
62
63 /**
64  * device_handler_get_device - Get a handler device by index.
65  */
66
67 const struct discover_device *device_handler_get_device(
68         const struct device_handler *handler, unsigned int index)
69 {
70         if (index >= handler->n_devices) {
71                 assert(0 && "bad index");
72                 return NULL;
73         }
74
75         return handler->devices[index];
76 }
77
78 struct discover_boot_option *discover_boot_option_create(
79                 struct discover_context *ctx,
80                 struct discover_device *device)
81 {
82         struct discover_boot_option *opt;
83
84         opt = talloc_zero(ctx, struct discover_boot_option);
85         opt->option = talloc_zero(opt, struct boot_option);
86         opt->device = device;
87
88         return opt;
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         if (!strncmp(name, "/dev/", strlen("/dev/")))
131                 name += strlen("/dev/");
132
133         return device_lookup_by_id(handler, name);
134 }
135
136 struct discover_device *device_lookup_by_uuid(
137                 struct device_handler *device_handler,
138                 const char *uuid)
139 {
140         return device_lookup(device_handler, device_match_uuid, uuid);
141 }
142
143 struct discover_device *device_lookup_by_label(
144                 struct device_handler *device_handler,
145                 const char *label)
146 {
147         return device_lookup(device_handler, device_match_label, label);
148 }
149
150 struct discover_device *device_lookup_by_id(
151                 struct device_handler *device_handler,
152                 const char *id)
153 {
154         return device_lookup(device_handler, device_match_id, id);
155 }
156
157 void device_handler_destroy(struct device_handler *handler)
158 {
159         talloc_free(handler);
160 }
161
162 static int destroy_device(void *arg)
163 {
164         struct discover_device *dev = arg;
165
166         umount_device(dev);
167
168         return 0;
169 }
170
171 struct discover_device *discover_device_create(struct device_handler *handler,
172                 const char *id)
173 {
174         struct discover_device *dev;
175
176         dev = device_lookup_by_id(handler, id);
177         if (dev)
178                 return dev;
179
180         dev = talloc_zero(handler, struct discover_device);
181         dev->device = talloc_zero(dev, struct device);
182         dev->device->id = talloc_strdup(dev->device, id);
183         list_init(&dev->params);
184         list_init(&dev->boot_options);
185
186         talloc_set_destructor(dev, destroy_device);
187
188         return dev;
189 }
190
191 struct discover_device_param {
192         char                    *name;
193         char                    *value;
194         struct list_item        list;
195 };
196
197 void discover_device_set_param(struct discover_device *device,
198                 const char *name, const char *value)
199 {
200         struct discover_device_param *param;
201         bool found = false;
202
203         list_for_each_entry(&device->params, param, list) {
204                 if (!strcmp(param->name, name)) {
205                         found = true;
206                         break;
207                 }
208         }
209
210         if (!found) {
211                 if (!value)
212                         return;
213                 param = talloc(device, struct discover_device_param);
214                 param->name = talloc_strdup(param, name);
215                 list_add(&device->params, &param->list);
216         } else {
217                 if (!value) {
218                         list_remove(&param->list);
219                         talloc_free(param);
220                         return;
221                 }
222                 talloc_free(param->value);
223         }
224
225         param->value = talloc_strdup(param, value);
226 }
227
228 const char *discover_device_get_param(struct discover_device *device,
229                 const char *name)
230 {
231         struct discover_device_param *param;
232
233         list_for_each_entry(&device->params, param, list) {
234                 if (!strcmp(param->name, name))
235                         return param->name;
236         }
237         return NULL;
238 }
239
240 struct device_handler *device_handler_init(struct discover_server *server,
241                 struct waitset *waitset, int dry_run)
242 {
243         struct device_handler *handler;
244
245         handler = talloc_zero(NULL, struct device_handler);
246         handler->server = server;
247         handler->waitset = waitset;
248         handler->dry_run = dry_run;
249         handler->autoboot_enabled = config_get()->autoboot_enabled;
250
251         list_init(&handler->unresolved_boot_options);
252
253         /* set up our mount point base */
254         pb_mkdir_recursive(mount_base());
255
256         parser_init();
257
258         return handler;
259 }
260
261 void device_handler_remove(struct device_handler *handler,
262                 struct discover_device *device)
263 {
264         unsigned int i;
265
266         for (i = 0; i < handler->n_devices; i++)
267                 if (handler->devices[i] == device)
268                         break;
269
270         if (i == handler->n_devices) {
271                 talloc_free(device);
272                 return;
273         }
274
275         handler->n_devices--;
276         memmove(&handler->devices[i], &handler->devices[i + 1],
277                 (handler->n_devices - i) * sizeof(handler->devices[0]));
278         handler->devices = talloc_realloc(handler, handler->devices,
279                 struct discover_device *, handler->n_devices);
280
281         if (device->notified)
282                 discover_server_notify_device_remove(handler->server,
283                                                         device->device);
284
285         talloc_free(device);
286 }
287
288 static void boot_status(void *arg, struct boot_status *status)
289 {
290         struct device_handler *handler = arg;
291
292         discover_server_notify_boot_status(handler->server, status);
293 }
294
295 static void countdown_status(struct device_handler *handler,
296                 struct discover_boot_option *opt, unsigned int sec)
297 {
298         struct boot_status status;
299
300         status.type = BOOT_STATUS_INFO;
301         status.progress = -1;
302         status.detail = NULL;
303         status.message = talloc_asprintf(handler,
304                         "Booting %s in %u sec", opt->option->name, sec);
305
306         discover_server_notify_boot_status(handler->server, &status);
307
308         talloc_free(status.message);
309 }
310
311 static int default_timeout(void *arg)
312 {
313         struct device_handler *handler = arg;
314         struct discover_boot_option *opt;
315
316         if (!handler->default_boot_option)
317                 return 0;
318
319         opt = handler->default_boot_option;
320
321         if (handler->sec_to_boot) {
322                 countdown_status(handler, opt, handler->sec_to_boot);
323                 handler->sec_to_boot--;
324                 handler->timeout_waiter = waiter_register_timeout(
325                                                 handler->waitset, 1000,
326                                                 default_timeout, handler);
327                 return 0;
328         }
329
330         handler->timeout_waiter = NULL;
331
332         pb_log("Timeout expired, booting default option %s\n", opt->option->id);
333
334         boot(handler, handler->default_boot_option, NULL,
335                         handler->dry_run, boot_status, handler);
336         return 0;
337 }
338
339 static bool priority_match(struct boot_priority *prio,
340                 struct discover_boot_option *opt)
341 {
342         return prio->type == opt->device->device->type;
343 }
344
345 static int default_option_priority(struct discover_boot_option *opt)
346 {
347         const struct config *config;
348         struct boot_priority *prio;
349         int i;
350
351         config = config_get();
352
353         for (i = 0; i < config->n_boot_priorities; i++) {
354                 prio = &config->boot_priorities[i];
355                 if (priority_match(prio, opt))
356                         break;
357         }
358
359         return i;
360 }
361
362 static void set_default(struct device_handler *handler,
363                 struct discover_boot_option *opt)
364 {
365         if (!handler->autoboot_enabled)
366                 return;
367
368         /* Resolve any conflicts: if we have a new default option, it only
369          * replaces the current if it has a higher priority. */
370         if (handler->default_boot_option) {
371                 int new_prio, cur_prio;
372
373                 new_prio = default_option_priority(opt);
374                 cur_prio = default_option_priority(
375                                         handler->default_boot_option);
376
377                 if (new_prio < cur_prio) {
378                         handler->default_boot_option = opt;
379                         /* extend the timeout a little, so the user sees some
380                          * indication of the change */
381                         handler->sec_to_boot += 2;
382                 }
383
384                 return;
385         }
386
387         handler->sec_to_boot = config_get()->autoboot_timeout_sec;
388         handler->default_boot_option = opt;
389
390         pb_log("Boot option %s set as default, timeout %u sec.\n",
391                opt->option->id, handler->sec_to_boot);
392
393         default_timeout(handler);
394 }
395
396 static bool resource_is_resolved(struct resource *res)
397 {
398         return !res || res->resolved;
399 }
400
401 /* We only use this in an assert, which will disappear if we're compiling
402  * with NDEBUG, so we need the 'used' attribute for these builds */
403 static bool __attribute__((used)) boot_option_is_resolved(
404                 struct discover_boot_option *opt)
405 {
406         return resource_is_resolved(opt->boot_image) &&
407                 resource_is_resolved(opt->initrd) &&
408                 resource_is_resolved(opt->dtb) &&
409                 resource_is_resolved(opt->icon);
410 }
411
412 static bool resource_resolve(struct resource *res, const char *name,
413                 struct discover_boot_option *opt,
414                 struct device_handler *handler)
415 {
416         struct parser *parser = opt->source;
417
418         if (resource_is_resolved(res))
419                 return true;
420
421         pb_log("Attempting to resolve resource %s->%s with parser %s\n",
422                         opt->option->id, name, parser->name);
423         parser->resolve_resource(handler, res);
424
425         return res->resolved;
426 }
427
428 static bool boot_option_resolve(struct discover_boot_option *opt,
429                 struct device_handler *handler)
430 {
431         return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
432                 resource_resolve(opt->initrd, "initrd", opt, handler) &&
433                 resource_resolve(opt->dtb, "dtb", opt, handler) &&
434                 resource_resolve(opt->icon, "icon", opt, handler);
435 }
436
437 static void boot_option_finalise(struct device_handler *handler,
438                 struct discover_boot_option *opt)
439 {
440         assert(boot_option_is_resolved(opt));
441
442         /* check that the parsers haven't set any of the final data */
443         assert(!opt->option->boot_image_file);
444         assert(!opt->option->initrd_file);
445         assert(!opt->option->dtb_file);
446         assert(!opt->option->icon_file);
447         assert(!opt->option->device_id);
448
449         if (opt->boot_image)
450                 opt->option->boot_image_file = opt->boot_image->url->full;
451         if (opt->initrd)
452                 opt->option->initrd_file = opt->initrd->url->full;
453         if (opt->dtb)
454                 opt->option->dtb_file = opt->dtb->url->full;
455         if (opt->icon)
456                 opt->option->icon_file = opt->icon->url->full;
457
458         opt->option->device_id = opt->device->device->id;
459
460         if (opt->option->is_default)
461                 set_default(handler, opt);
462 }
463
464 static void notify_boot_option(struct device_handler *handler,
465                 struct discover_boot_option *opt)
466 {
467         struct discover_device *dev = opt->device;
468
469         if (!dev->notified)
470                 discover_server_notify_device_add(handler->server,
471                                                   opt->device->device);
472         dev->notified = true;
473         discover_server_notify_boot_option_add(handler->server, opt->option);
474 }
475
476 static void process_boot_option_queue(struct device_handler *handler)
477 {
478         struct discover_boot_option *opt, *tmp;
479
480         list_for_each_entry_safe(&handler->unresolved_boot_options,
481                         opt, tmp, list) {
482
483                 pb_log("queue: attempting resolution for %s\n",
484                                 opt->option->id);
485
486                 if (!boot_option_resolve(opt, handler))
487                         continue;
488
489                 pb_log("\tresolved!\n");
490
491                 list_remove(&opt->list);
492                 list_add_tail(&opt->device->boot_options, &opt->list);
493                 talloc_steal(opt->device, opt);
494                 boot_option_finalise(handler, opt);
495                 notify_boot_option(handler, opt);
496         }
497 }
498
499 struct discover_context *device_handler_discover_context_create(
500                 struct device_handler *handler,
501                 struct discover_device *device)
502 {
503         struct discover_context *ctx;
504
505         ctx = talloc(handler, struct discover_context);
506         ctx->device = device;
507         ctx->conf_url = NULL;
508         list_init(&ctx->boot_options);
509
510         return ctx;
511 }
512
513 /**
514  * context_commit - Commit a temporary discovery context to the handler,
515  * and notify the clients about any new options / devices
516  */
517 void device_handler_discover_context_commit(struct device_handler *handler,
518                 struct discover_context *ctx)
519 {
520         struct discover_device *dev = ctx->device;
521         struct discover_boot_option *opt, *tmp;
522
523         if (!device_lookup_by_id(handler, dev->device->id))
524                 device_handler_add_device(handler, dev);
525
526         /* move boot options from the context to the device */
527         list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
528                 list_remove(&opt->list);
529
530                 if (boot_option_resolve(opt, handler)) {
531                         pb_log("boot option %s is resolved, "
532                                         "sending to clients\n",
533                                         opt->option->id);
534                         list_add_tail(&dev->boot_options, &opt->list);
535                         talloc_steal(dev, opt);
536                         boot_option_finalise(handler, opt);
537                         notify_boot_option(handler, opt);
538                 } else {
539                         if (!opt->source->resolve_resource) {
540                                 pb_log("parser %s gave us an unresolved "
541                                         "resource (%s), but no way to "
542                                         "resolve it\n",
543                                         opt->source->name, opt->option->id);
544                                 talloc_free(opt);
545                         } else {
546                                 pb_log("boot option %s is unresolved, "
547                                                 "adding to queue\n",
548                                                 opt->option->id);
549                                 list_add(&handler->unresolved_boot_options,
550                                                 &opt->list);
551                                 talloc_steal(handler, opt);
552                         }
553                 }
554         }
555 }
556
557 void device_handler_add_device(struct device_handler *handler,
558                 struct discover_device *device)
559 {
560         handler->n_devices++;
561         handler->devices = talloc_realloc(handler, handler->devices,
562                                 struct discover_device *, handler->n_devices);
563         handler->devices[handler->n_devices - 1] = device;
564
565 }
566
567 /* Start discovery on a hotplugged device. The device will be in our devices
568  * array, but has only just been initialised by the hotplug source.
569  */
570 int device_handler_discover(struct device_handler *handler,
571                 struct discover_device *dev, enum conf_method method)
572 {
573         struct discover_context *ctx;
574
575         process_boot_option_queue(handler);
576
577         /* create our context */
578         ctx = device_handler_discover_context_create(handler, dev);
579
580         mount_device(dev);
581
582         /* run the parsers. This will populate the ctx's boot_option list. */
583         iterate_parsers(ctx, method);
584
585         /* add discovered stuff to the handler */
586         device_handler_discover_context_commit(handler, ctx);
587
588         talloc_free(ctx);
589
590         return 0;
591 }
592
593 /* incoming conf event */
594 int device_handler_conf(struct device_handler *handler,
595                 struct discover_device *dev, struct pb_url *url,
596                 enum conf_method method)
597 {
598         struct discover_context *ctx;
599
600         /* create our context */
601         ctx = device_handler_discover_context_create(handler, dev);
602         ctx->conf_url = url;
603
604         iterate_parsers(ctx, method);
605
606         device_handler_discover_context_commit(handler, ctx);
607
608         talloc_free(ctx);
609
610         return 0;
611 }
612
613 static struct discover_boot_option *find_boot_option_by_id(
614                 struct device_handler *handler, const char *id)
615 {
616         unsigned int i;
617
618         for (i = 0; i < handler->n_devices; i++) {
619                 struct discover_device *dev = handler->devices[i];
620                 struct discover_boot_option *opt;
621
622                 list_for_each_entry(&dev->boot_options, opt, list)
623                         if (!strcmp(opt->option->id, id))
624                                 return opt;
625         }
626
627         return NULL;
628 }
629
630 void device_handler_boot(struct device_handler *handler,
631                 struct boot_command *cmd)
632 {
633         struct discover_boot_option *opt;
634
635         opt = find_boot_option_by_id(handler, cmd->option_id);
636
637         boot(handler, opt, cmd, handler->dry_run, boot_status, handler);
638 }
639
640 void device_handler_cancel_default(struct device_handler *handler)
641 {
642         struct boot_status status;
643
644         if (handler->timeout_waiter)
645                 waiter_remove(handler->timeout_waiter);
646
647         handler->timeout_waiter = NULL;
648         handler->autoboot_enabled = false;
649
650         /* we only send status if we had a default boot option queued */
651         if (!handler->default_boot_option)
652                 return;
653
654         pb_log("Cancelling default boot option\n");
655
656         handler->default_boot_option = NULL;
657
658         status.type = BOOT_STATUS_INFO;
659         status.progress = -1;
660         status.detail = NULL;
661         status.message = "Default boot cancelled";
662
663         discover_server_notify_boot_status(handler->server, &status);
664 }
665
666 #ifndef PETITBOOT_TEST
667 static int mount_device(struct discover_device *dev)
668 {
669         int rc;
670
671         if (!dev->device_path)
672                 return -1;
673
674         if (!dev->mount_path)
675                 dev->mount_path = join_paths(dev, mount_base(),
676                                                 dev->device_path);
677
678         if (pb_mkdir_recursive(dev->mount_path))
679                 pb_log("couldn't create mount directory %s: %s\n",
680                                 dev->mount_path, strerror(errno));
681
682         rc = process_run_simple(dev, pb_system_apps.mount,
683                         dev->device_path, dev->mount_path,
684                         "-o", "ro", NULL);
685
686         if (!rc)
687                 return 0;
688
689         /* Retry mount without ro option. */
690         rc = process_run_simple(dev, pb_system_apps.mount,
691                         dev->device_path, dev->mount_path, NULL);
692
693         if (!rc)
694                 return 0;
695
696         pb_rmdir_recursive(mount_base(), dev->mount_path);
697         return -1;
698 }
699
700 static int umount_device(struct discover_device *dev)
701 {
702         int status;
703
704         if (!dev->mount_path)
705                 return 0;
706
707         status = process_run_simple(dev, pb_system_apps.umount,
708                         dev->mount_path, NULL);
709
710         if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
711                 return -1;
712
713         pb_rmdir_recursive(mount_base(), dev->mount_path);
714
715         return 0;
716 }
717 #else
718
719 static int umount_device(struct discover_device *dev __attribute__((unused)))
720 {
721         return 0;
722 }
723
724 static int __attribute__((unused)) mount_device(
725                 struct discover_device *dev __attribute__((unused)))
726 {
727         return 0;
728 }
729
730 #endif
731