77f9e3c59b9bb761d02a668f26ad130187f516d9
[petitboot] / discover / device-handler.c
1 #include <assert.h>
2 #include <stdlib.h>
3 #include <stdbool.h>
4 #include <unistd.h>
5 #include <string.h>
6 #include <errno.h>
7 #include <mntent.h>
8 #include <locale.h>
9 #include <sys/stat.h>
10 #include <sys/wait.h>
11 #include <sys/mount.h>
12
13 #include <talloc/talloc.h>
14 #include <list/list.h>
15 #include <log/log.h>
16 #include <types/types.h>
17 #include <system/system.h>
18 #include <process/process.h>
19 #include <url/url.h>
20 #include <i18n/i18n.h>
21
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netdb.h>
25 #include <arpa/inet.h>
26
27 #include "device-handler.h"
28 #include "discover-server.h"
29 #include "devmapper.h"
30 #include "user-event.h"
31 #include "platform.h"
32 #include "event.h"
33 #include "parser.h"
34 #include "resource.h"
35 #include "paths.h"
36 #include "sysinfo.h"
37 #include "boot.h"
38 #include "udev.h"
39 #include "network.h"
40 #include "ipmi.h"
41
42 enum default_priority {
43         DEFAULT_PRIORITY_REMOTE         = 1,
44         DEFAULT_PRIORITY_LOCAL_FIRST    = 2,
45         DEFAULT_PRIORITY_LOCAL_LAST     = 0xfe,
46         DEFAULT_PRIORITY_DISABLED       = 0xff,
47 };
48
49 struct device_handler {
50         struct discover_server  *server;
51         int                     dry_run;
52
53         struct pb_udev          *udev;
54         struct network          *network;
55         struct user_event       *user_event;
56
57         struct discover_device  **devices;
58         unsigned int            n_devices;
59
60         struct ramdisk_device   **ramdisks;
61         unsigned int            n_ramdisks;
62
63         struct waitset          *waitset;
64         struct waiter           *timeout_waiter;
65         bool                    autoboot_enabled;
66         unsigned int            sec_to_boot;
67
68         struct discover_boot_option *default_boot_option;
69         int                     default_boot_option_priority;
70
71         struct list             unresolved_boot_options;
72
73         struct boot_task        *pending_boot;
74         bool                    pending_boot_is_default;
75 };
76
77 static int mount_device(struct discover_device *dev);
78 static int umount_device(struct discover_device *dev);
79
80 static int device_handler_init_sources(struct device_handler *handler);
81 static void device_handler_reinit_sources(struct device_handler *handler);
82
83 static void device_handler_update_lang(const char *lang);
84
85 void discover_context_add_boot_option(struct discover_context *ctx,
86                 struct discover_boot_option *boot_option)
87 {
88         boot_option->source = ctx->parser;
89         list_add_tail(&ctx->boot_options, &boot_option->list);
90         talloc_steal(ctx, boot_option);
91 }
92
93 /**
94  * device_handler_get_device_count - Get the count of current handler devices.
95  */
96
97 int device_handler_get_device_count(const struct device_handler *handler)
98 {
99         return handler->n_devices;
100 }
101
102 /**
103  * device_handler_get_device - Get a handler device by index.
104  */
105
106 const struct discover_device *device_handler_get_device(
107         const struct device_handler *handler, unsigned int index)
108 {
109         if (index >= handler->n_devices) {
110                 assert(0 && "bad index");
111                 return NULL;
112         }
113
114         return handler->devices[index];
115 }
116
117 struct discover_boot_option *discover_boot_option_create(
118                 struct discover_context *ctx,
119                 struct discover_device *device)
120 {
121         struct discover_boot_option *opt;
122
123         opt = talloc_zero(ctx, struct discover_boot_option);
124         opt->option = talloc_zero(opt, struct boot_option);
125         opt->device = device;
126
127         return opt;
128 }
129
130 static int device_match_uuid(struct discover_device *dev, const char *uuid)
131 {
132         return dev->uuid && !strcmp(dev->uuid, uuid);
133 }
134
135 static int device_match_label(struct discover_device *dev, const char *label)
136 {
137         return dev->label && !strcmp(dev->label, label);
138 }
139
140 static int device_match_id(struct discover_device *dev, const char *id)
141 {
142         return !strcmp(dev->device->id, id);
143 }
144
145 static int device_match_serial(struct discover_device *dev, const char *serial)
146 {
147         const char *val = discover_device_get_param(dev, "ID_SERIAL");
148         return val && !strcmp(val, serial);
149 }
150
151 static struct discover_device *device_lookup(
152                 struct device_handler *device_handler,
153                 int (match_fn)(struct discover_device *, const char *),
154                 const char *str)
155 {
156         struct discover_device *dev;
157         unsigned int i;
158
159         if (!str)
160                 return NULL;
161
162         for (i = 0; i < device_handler->n_devices; i++) {
163                 dev = device_handler->devices[i];
164
165                 if (match_fn(dev, str))
166                         return dev;
167         }
168
169         return NULL;
170 }
171
172 struct discover_device *device_lookup_by_name(struct device_handler *handler,
173                 const char *name)
174 {
175         if (!strncmp(name, "/dev/", strlen("/dev/")))
176                 name += strlen("/dev/");
177
178         return device_lookup_by_id(handler, name);
179 }
180
181 struct discover_device *device_lookup_by_uuid(
182                 struct device_handler *device_handler,
183                 const char *uuid)
184 {
185         return device_lookup(device_handler, device_match_uuid, uuid);
186 }
187
188 struct discover_device *device_lookup_by_label(
189                 struct device_handler *device_handler,
190                 const char *label)
191 {
192         return device_lookup(device_handler, device_match_label, label);
193 }
194
195 struct discover_device *device_lookup_by_id(
196                 struct device_handler *device_handler,
197                 const char *id)
198 {
199         return device_lookup(device_handler, device_match_id, id);
200 }
201
202 struct discover_device *device_lookup_by_serial(
203                 struct device_handler *device_handler,
204                 const char *serial)
205 {
206         return device_lookup(device_handler, device_match_serial, serial);
207 }
208
209 void device_handler_destroy(struct device_handler *handler)
210 {
211         talloc_free(handler);
212 }
213
214 static int destroy_device(void *arg)
215 {
216         struct discover_device *dev = arg;
217
218         umount_device(dev);
219
220         return 0;
221 }
222
223 struct discover_device *discover_device_create(struct device_handler *handler,
224                 const char *id)
225 {
226         struct discover_device *dev;
227
228         dev = device_lookup_by_id(handler, id);
229         if (dev)
230                 return dev;
231
232         dev = talloc_zero(handler, struct discover_device);
233         dev->device = talloc_zero(dev, struct device);
234         dev->device->id = talloc_strdup(dev->device, id);
235         list_init(&dev->params);
236         list_init(&dev->boot_options);
237
238         talloc_set_destructor(dev, destroy_device);
239
240         return dev;
241 }
242
243 struct discover_device_param {
244         char                    *name;
245         char                    *value;
246         struct list_item        list;
247 };
248
249 void discover_device_set_param(struct discover_device *device,
250                 const char *name, const char *value)
251 {
252         struct discover_device_param *param;
253         bool found = false;
254
255         list_for_each_entry(&device->params, param, list) {
256                 if (!strcmp(param->name, name)) {
257                         found = true;
258                         break;
259                 }
260         }
261
262         if (!found) {
263                 if (!value)
264                         return;
265                 param = talloc(device, struct discover_device_param);
266                 param->name = talloc_strdup(param, name);
267                 list_add(&device->params, &param->list);
268         } else {
269                 if (!value) {
270                         list_remove(&param->list);
271                         talloc_free(param);
272                         return;
273                 }
274                 talloc_free(param->value);
275         }
276
277         param->value = talloc_strdup(param, value);
278 }
279
280 const char *discover_device_get_param(struct discover_device *device,
281                 const char *name)
282 {
283         struct discover_device_param *param;
284
285         list_for_each_entry(&device->params, param, list) {
286                 if (!strcmp(param->name, name))
287                         return param->value;
288         }
289         return NULL;
290 }
291
292 struct device_handler *device_handler_init(struct discover_server *server,
293                 struct waitset *waitset, int dry_run)
294 {
295         struct device_handler *handler;
296         int rc;
297
298         handler = talloc_zero(NULL, struct device_handler);
299         handler->server = server;
300         handler->waitset = waitset;
301         handler->dry_run = dry_run;
302         handler->autoboot_enabled = config_get()->autoboot_enabled;
303
304         list_init(&handler->unresolved_boot_options);
305
306         /* set up our mount point base */
307         pb_mkdir_recursive(mount_base());
308
309         parser_init();
310
311         if (config_get()->safe_mode)
312                 return handler;
313
314         rc = device_handler_init_sources(handler);
315         if (rc) {
316                 talloc_free(handler);
317                 return NULL;
318         }
319
320         return handler;
321 }
322
323 void device_handler_reinit(struct device_handler *handler)
324 {
325         struct discover_boot_option *opt, *tmp;
326         struct ramdisk_device *ramdisk;
327         unsigned int i;
328
329         device_handler_cancel_default(handler);
330
331         /* free unresolved boot options */
332         list_for_each_entry_safe(&handler->unresolved_boot_options,
333                         opt, tmp, list)
334                 talloc_free(opt);
335         list_init(&handler->unresolved_boot_options);
336
337         /* drop all devices */
338         for (i = 0; i < handler->n_devices; i++) {
339                 discover_server_notify_device_remove(handler->server,
340                                 handler->devices[i]->device);
341                 ramdisk = handler->devices[i]->ramdisk;
342                 talloc_free(handler->devices[i]);
343                 talloc_free(ramdisk);
344         }
345
346         talloc_free(handler->devices);
347         handler->devices = NULL;
348         handler->n_devices = 0;
349         talloc_free(handler->ramdisks);
350         handler->ramdisks = NULL;
351         handler->n_ramdisks = 0;
352
353         device_handler_reinit_sources(handler);
354 }
355
356 void device_handler_remove(struct device_handler *handler,
357                 struct discover_device *device)
358 {
359         struct discover_boot_option *opt, *tmp;
360         unsigned int i;
361
362         for (i = 0; i < handler->n_devices; i++)
363                 if (handler->devices[i] == device)
364                         break;
365
366         if (i == handler->n_devices) {
367                 talloc_free(device);
368                 return;
369         }
370
371         /* Free any unresolved options, as they're currently allocated
372          * against the handler */
373         list_for_each_entry_safe(&handler->unresolved_boot_options,
374                         opt, tmp, list) {
375                 if (opt->device != device)
376                         continue;
377                 list_remove(&opt->list);
378                 talloc_free(opt);
379         }
380
381         /* if this is a network device, we have to unregister it from the
382          * network code */
383         if (device->device->type == DEVICE_TYPE_NETWORK)
384                 network_unregister_device(handler->network, device);
385
386         handler->n_devices--;
387         memmove(&handler->devices[i], &handler->devices[i + 1],
388                 (handler->n_devices - i) * sizeof(handler->devices[0]));
389         handler->devices = talloc_realloc(handler, handler->devices,
390                 struct discover_device *, handler->n_devices);
391
392         if (device->notified)
393                 discover_server_notify_device_remove(handler->server,
394                                                         device->device);
395
396         talloc_free(device);
397 }
398
399 static void boot_status(void *arg, struct boot_status *status)
400 {
401         struct device_handler *handler = arg;
402
403         discover_server_notify_boot_status(handler->server, status);
404 }
405
406 static void countdown_status(struct device_handler *handler,
407                 struct discover_boot_option *opt, unsigned int sec)
408 {
409         struct boot_status status;
410
411         status.type = BOOT_STATUS_INFO;
412         status.progress = -1;
413         status.detail = NULL;
414         status.message = talloc_asprintf(handler,
415                         _("Booting in %d sec: %s"), sec, opt->option->name);
416
417         discover_server_notify_boot_status(handler->server, &status);
418
419         talloc_free(status.message);
420 }
421
422 static int default_timeout(void *arg)
423 {
424         struct device_handler *handler = arg;
425         struct discover_boot_option *opt;
426
427         if (!handler->default_boot_option)
428                 return 0;
429
430         if (handler->pending_boot)
431                 return 0;
432
433         opt = handler->default_boot_option;
434
435         if (handler->sec_to_boot) {
436                 countdown_status(handler, opt, handler->sec_to_boot);
437                 handler->sec_to_boot--;
438                 handler->timeout_waiter = waiter_register_timeout(
439                                                 handler->waitset, 1000,
440                                                 default_timeout, handler);
441                 return 0;
442         }
443
444         handler->timeout_waiter = NULL;
445
446         pb_log("Timeout expired, booting default option %s\n", opt->option->id);
447
448         handler->pending_boot = boot(handler, handler->default_boot_option,
449                         NULL, handler->dry_run, boot_status, handler);
450         handler->pending_boot_is_default = true;
451         return 0;
452 }
453
454 struct {
455         enum ipmi_bootdev       ipmi_type;
456         enum device_type        device_type;
457 } device_type_map[] = {
458         { IPMI_BOOTDEV_NETWORK, DEVICE_TYPE_NETWORK },
459         { IPMI_BOOTDEV_DISK, DEVICE_TYPE_DISK },
460         { IPMI_BOOTDEV_DISK, DEVICE_TYPE_USB },
461         { IPMI_BOOTDEV_CDROM, DEVICE_TYPE_OPTICAL },
462 };
463
464 static bool ipmi_device_type_matches(enum ipmi_bootdev ipmi_type,
465                 enum device_type device_type)
466 {
467         unsigned int i;
468
469         for (i = 0; i < ARRAY_SIZE(device_type_map); i++) {
470                 if (device_type_map[i].device_type == device_type)
471                         return device_type_map[i].ipmi_type == ipmi_type;
472         }
473
474         return false;
475 }
476
477 static int autoboot_option_priority(const struct config *config,
478                                 struct discover_boot_option *opt)
479 {
480         enum device_type type = opt->device->device->type;
481         const char *uuid = opt->device->uuid;
482         struct autoboot_option *auto_opt;
483         unsigned int i;
484
485         for (i = 0; i < config->n_autoboot_opts; i++) {
486                 auto_opt = &config->autoboot_opts[i];
487                 if (auto_opt->boot_type == BOOT_DEVICE_UUID)
488                         if (!strcmp(auto_opt->uuid, uuid))
489                                 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
490
491                 if (auto_opt->boot_type == BOOT_DEVICE_TYPE)
492                         if (auto_opt->type == type ||
493                             auto_opt->type == DEVICE_TYPE_ANY)
494                                 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
495         }
496
497         return -1;
498 }
499
500 /*
501  * We have different priorities to resolve conflicts between boot options that
502  * report to be the default for their device. This function assigns a priority
503  * for these options.
504  */
505 static enum default_priority default_option_priority(
506                 struct discover_boot_option *opt)
507 {
508         const struct config *config;
509
510         config = config_get();
511
512         /* We give highest priority to IPMI-configured boot options. If
513          * we have an IPMI bootdev configuration set, then we don't allow
514          * any other defaults */
515         if (config->ipmi_bootdev) {
516                 bool ipmi_match = ipmi_device_type_matches(config->ipmi_bootdev,
517                                 opt->device->device->type);
518                 if (ipmi_match)
519                         return DEFAULT_PRIORITY_REMOTE;
520
521                 pb_debug("handler: disabled default priority due to "
522                                 "non-matching IPMI type %x\n",
523                                 config->ipmi_bootdev);
524                 return DEFAULT_PRIORITY_DISABLED;
525         }
526
527         /* Next, try to match the option against the user-defined autoboot
528          * options, either by device UUID or type. */
529         if (config->n_autoboot_opts) {
530                 int boot_match = autoboot_option_priority(config, opt);
531                 if (boot_match > 0)
532                         return boot_match;
533         }
534
535         /* If the option didn't match any entry in the array, it is disabled */
536         pb_debug("handler: disabled default priority due to "
537                         "non-matching UUID or type\n");
538         return DEFAULT_PRIORITY_DISABLED;
539 }
540
541 static void set_default(struct device_handler *handler,
542                 struct discover_boot_option *opt)
543 {
544         enum default_priority cur_prio, new_prio;
545
546         if (!handler->autoboot_enabled)
547                 return;
548
549         pb_debug("handler: new default option: %s\n", opt->option->id);
550
551         new_prio = default_option_priority(opt);
552
553         /* Anything outside our range prevents a default boot */
554         if (new_prio >= DEFAULT_PRIORITY_DISABLED)
555                 return;
556
557         pb_debug("handler: calculated priority %d\n", new_prio);
558
559         /* Resolve any conflicts: if we have a new default option, it only
560          * replaces the current if it has a higher priority. */
561         if (handler->default_boot_option) {
562
563                 cur_prio = handler->default_boot_option_priority;
564
565                 if (new_prio < cur_prio) {
566                         pb_log("handler: new prio %d beats "
567                                         "old prio %d for %s\n",
568                                         new_prio, cur_prio,
569                                         handler->default_boot_option
570                                                 ->option->id);
571                         handler->default_boot_option = opt;
572                         handler->default_boot_option_priority = new_prio;
573                         /* extend the timeout a little, so the user sees some
574                          * indication of the change */
575                         handler->sec_to_boot += 2;
576                 }
577
578                 return;
579         }
580
581         handler->sec_to_boot = config_get()->autoboot_timeout_sec;
582         handler->default_boot_option = opt;
583         handler->default_boot_option_priority = new_prio;
584
585         pb_log("handler: boot option %s set as default, timeout %u sec.\n",
586                opt->option->id, handler->sec_to_boot);
587
588         default_timeout(handler);
589 }
590
591 static bool resource_is_resolved(struct resource *res)
592 {
593         return !res || res->resolved;
594 }
595
596 /* We only use this in an assert, which will disappear if we're compiling
597  * with NDEBUG, so we need the 'used' attribute for these builds */
598 static bool __attribute__((used)) boot_option_is_resolved(
599                 struct discover_boot_option *opt)
600 {
601         return resource_is_resolved(opt->boot_image) &&
602                 resource_is_resolved(opt->initrd) &&
603                 resource_is_resolved(opt->dtb) &&
604                 resource_is_resolved(opt->icon);
605 }
606
607 static bool resource_resolve(struct resource *res, const char *name,
608                 struct discover_boot_option *opt,
609                 struct device_handler *handler)
610 {
611         struct parser *parser = opt->source;
612
613         if (resource_is_resolved(res))
614                 return true;
615
616         pb_debug("Attempting to resolve resource %s->%s with parser %s\n",
617                         opt->option->id, name, parser->name);
618         parser->resolve_resource(handler, res);
619
620         return res->resolved;
621 }
622
623 static bool boot_option_resolve(struct discover_boot_option *opt,
624                 struct device_handler *handler)
625 {
626         return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
627                 resource_resolve(opt->initrd, "initrd", opt, handler) &&
628                 resource_resolve(opt->dtb, "dtb", opt, handler) &&
629                 resource_resolve(opt->icon, "icon", opt, handler);
630 }
631
632 static void boot_option_finalise(struct device_handler *handler,
633                 struct discover_boot_option *opt)
634 {
635         assert(boot_option_is_resolved(opt));
636
637         /* check that the parsers haven't set any of the final data */
638         assert(!opt->option->boot_image_file);
639         assert(!opt->option->initrd_file);
640         assert(!opt->option->dtb_file);
641         assert(!opt->option->icon_file);
642         assert(!opt->option->device_id);
643
644         if (opt->boot_image)
645                 opt->option->boot_image_file = opt->boot_image->url->full;
646         if (opt->initrd)
647                 opt->option->initrd_file = opt->initrd->url->full;
648         if (opt->dtb)
649                 opt->option->dtb_file = opt->dtb->url->full;
650         if (opt->icon)
651                 opt->option->icon_file = opt->icon->url->full;
652
653         opt->option->device_id = opt->device->device->id;
654
655         if (opt->option->is_default)
656                 set_default(handler, opt);
657 }
658
659 static void notify_boot_option(struct device_handler *handler,
660                 struct discover_boot_option *opt)
661 {
662         struct discover_device *dev = opt->device;
663
664         if (!dev->notified)
665                 discover_server_notify_device_add(handler->server,
666                                                   opt->device->device);
667         dev->notified = true;
668         discover_server_notify_boot_option_add(handler->server, opt->option);
669 }
670
671 static void process_boot_option_queue(struct device_handler *handler)
672 {
673         struct discover_boot_option *opt, *tmp;
674
675         list_for_each_entry_safe(&handler->unresolved_boot_options,
676                         opt, tmp, list) {
677
678                 pb_debug("queue: attempting resolution for %s\n",
679                                 opt->option->id);
680
681                 if (!boot_option_resolve(opt, handler))
682                         continue;
683
684                 pb_debug("\tresolved!\n");
685
686                 list_remove(&opt->list);
687                 list_add_tail(&opt->device->boot_options, &opt->list);
688                 talloc_steal(opt->device, opt);
689                 boot_option_finalise(handler, opt);
690                 notify_boot_option(handler, opt);
691         }
692 }
693
694 struct discover_context *device_handler_discover_context_create(
695                 struct device_handler *handler,
696                 struct discover_device *device)
697 {
698         struct discover_context *ctx;
699
700         ctx = talloc_zero(handler, struct discover_context);
701         ctx->device = device;
702         ctx->network = handler->network;
703         list_init(&ctx->boot_options);
704
705         return ctx;
706 }
707
708 /**
709  * context_commit - Commit a temporary discovery context to the handler,
710  * and notify the clients about any new options / devices
711  */
712 void device_handler_discover_context_commit(struct device_handler *handler,
713                 struct discover_context *ctx)
714 {
715         struct discover_device *dev = ctx->device;
716         struct discover_boot_option *opt, *tmp;
717
718         if (!device_lookup_by_id(handler, dev->device->id))
719                 device_handler_add_device(handler, dev);
720
721         /* move boot options from the context to the device */
722         list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
723                 list_remove(&opt->list);
724
725                 if (boot_option_resolve(opt, handler)) {
726                         pb_log("boot option %s is resolved, "
727                                         "sending to clients\n",
728                                         opt->option->id);
729                         list_add_tail(&dev->boot_options, &opt->list);
730                         talloc_steal(dev, opt);
731                         boot_option_finalise(handler, opt);
732                         notify_boot_option(handler, opt);
733                 } else {
734                         if (!opt->source->resolve_resource) {
735                                 pb_log("parser %s gave us an unresolved "
736                                         "resource (%s), but no way to "
737                                         "resolve it\n",
738                                         opt->source->name, opt->option->id);
739                                 talloc_free(opt);
740                         } else {
741                                 pb_log("boot option %s is unresolved, "
742                                                 "adding to queue\n",
743                                                 opt->option->id);
744                                 list_add(&handler->unresolved_boot_options,
745                                                 &opt->list);
746                                 talloc_steal(handler, opt);
747                         }
748                 }
749         }
750 }
751
752 void device_handler_add_device(struct device_handler *handler,
753                 struct discover_device *device)
754 {
755         handler->n_devices++;
756         handler->devices = talloc_realloc(handler, handler->devices,
757                                 struct discover_device *, handler->n_devices);
758         handler->devices[handler->n_devices - 1] = device;
759
760         if (device->device->type == DEVICE_TYPE_NETWORK)
761                 network_register_device(handler->network, device);
762 }
763
764 void device_handler_add_ramdisk(struct device_handler *handler,
765                 const char *path)
766 {
767         struct ramdisk_device *dev;
768         unsigned int i;
769
770         if (!path)
771                 return;
772
773         for (i = 0; i < handler->n_ramdisks; i++)
774                 if (!strcmp(handler->ramdisks[i]->path, path))
775                         return;
776
777         dev = talloc_zero(handler, struct ramdisk_device);
778         if (!dev) {
779                 pb_log("Failed to allocate memory to track %s\n", path);
780                 return;
781         }
782
783         dev->path = talloc_strdup(handler, path);
784
785         handler->ramdisks = talloc_realloc(handler, handler->ramdisks,
786                                 struct ramdisk_device *,
787                                 handler->n_ramdisks + 1);
788         if (!handler->ramdisks) {
789                 pb_log("Failed to reallocate memory"
790                        "- ramdisk tracking inconsistent!\n");
791                 return;
792         }
793
794         handler->ramdisks[i] = dev;
795         i = handler->n_ramdisks++;
796 }
797
798 struct ramdisk_device *device_handler_get_ramdisk(
799                 struct device_handler *handler)
800 {
801         unsigned int i;
802         char *name;
803         dev_t id;
804
805         /* Check if free ramdisk exists */
806         for (i = 0; i < handler->n_ramdisks; i++)
807                 if (!handler->ramdisks[i]->snapshot &&
808                     !handler->ramdisks[i]->origin &&
809                     !handler->ramdisks[i]->base)
810                         return handler->ramdisks[i];
811
812         /* Otherwise create a new one */
813         name = talloc_asprintf(handler, "/dev/ram%d",
814                         handler->n_ramdisks);
815         if (!name) {
816                 pb_debug("Failed to allocate memory to name /dev/ram%d",
817                         handler->n_ramdisks);
818                 return NULL;
819         }
820
821         id = makedev(1, handler->n_ramdisks);
822         if (mknod(name, S_IFBLK, id)) {
823                 if (errno == EEXIST) {
824                         /* We haven't yet received updates for existing
825                          * ramdisks - add and use this one */
826                         pb_debug("Using untracked ramdisk %s\n", name);
827                 } else {
828                         pb_log("Failed to create new ramdisk %s: %s\n",
829                                name, strerror(errno));
830                         return NULL;
831                 }
832         }
833         device_handler_add_ramdisk(handler, name);
834         talloc_free(name);
835
836         return handler->ramdisks[i];
837 }
838
839 void device_handler_release_ramdisk(struct discover_device *device)
840 {
841         struct ramdisk_device *ramdisk = device->ramdisk;
842
843         talloc_free(ramdisk->snapshot);
844         talloc_free(ramdisk->origin);
845         talloc_free(ramdisk->base);
846
847         ramdisk->snapshot = ramdisk->origin = ramdisk->base = NULL;
848         ramdisk->sectors = 0;
849
850         device->ramdisk = NULL;
851 }
852
853 /* Start discovery on a hotplugged device. The device will be in our devices
854  * array, but has only just been initialised by the hotplug source.
855  */
856 int device_handler_discover(struct device_handler *handler,
857                 struct discover_device *dev)
858 {
859         struct discover_context *ctx;
860         struct boot_status *status;
861         int rc;
862
863         status = talloc_zero(handler, struct boot_status);
864         status->type = BOOT_STATUS_INFO;
865         /*
866          * TRANSLATORS: this string will be passed the type and identifier
867          * of the device. For example, the first parameter could be "Disk",
868          * (which will be translated accordingly) and the second a Linux device
869          * identifier like 'sda1' (which will not be translated)
870          */
871         status->message = talloc_asprintf(status, _("Processing %s device %s"),
872                                 device_type_display_name(dev->device->type),
873                                 dev->device->id);
874         boot_status(handler, status);
875
876         process_boot_option_queue(handler);
877
878         /* create our context */
879         ctx = device_handler_discover_context_create(handler, dev);
880
881         rc = mount_device(dev);
882         if (rc)
883                 goto out;
884
885         /* add this device to our system info */
886         system_info_register_blockdev(dev->device->id, dev->uuid,
887                         dev->mount_path);
888
889         /* run the parsers. This will populate the ctx's boot_option list. */
890         iterate_parsers(ctx);
891
892         /* add discovered stuff to the handler */
893         device_handler_discover_context_commit(handler, ctx);
894
895 out:
896         /*
897          * TRANSLATORS: the format specifier in this string is a Linux
898          * device identifier, like 'sda1'
899          */
900         status->message = talloc_asprintf(status,_("Processing %s complete"),
901                                 dev->device->id);
902         boot_status(handler, status);
903
904         talloc_free(status);
905         talloc_free(ctx);
906
907         return 0;
908 }
909
910 /* Incoming dhcp event */
911 int device_handler_dhcp(struct device_handler *handler,
912                 struct discover_device *dev, struct event *event)
913 {
914         struct discover_context *ctx;
915         struct boot_status *status;
916
917         status = talloc_zero(handler, struct boot_status);
918         status->type = BOOT_STATUS_INFO;
919         /*
920          * TRANSLATORS: this format specifier will be the name of a network
921          * device, like 'eth0'.
922          */
923         status->message = talloc_asprintf(status, _("Processing dhcp event on %s"),
924                                 dev->device->id);
925         boot_status(handler, status);
926
927         /* create our context */
928         ctx = device_handler_discover_context_create(handler, dev);
929         ctx->event = event;
930
931         iterate_parsers(ctx);
932
933         device_handler_discover_context_commit(handler, ctx);
934
935         /*
936          * TRANSLATORS: this format specifier will be the name of a network
937          * device, like 'eth0'.
938          */
939         status->message = talloc_asprintf(status,_("Processing %s complete"),
940                                 dev->device->id);
941         boot_status(handler, status);
942
943         talloc_free(status);
944         talloc_free(ctx);
945
946         return 0;
947 }
948
949 /* incoming conf event */
950 int device_handler_conf(struct device_handler *handler,
951                 struct discover_device *dev, struct pb_url *url)
952 {
953         struct discover_context *ctx;
954         struct boot_status *status;
955
956         status = talloc_zero(handler, struct boot_status);
957         status->type = BOOT_STATUS_INFO;
958         status->message = talloc_asprintf(status, _("Processing user config"));
959         boot_status(handler, status);
960
961         /* create our context */
962         ctx = device_handler_discover_context_create(handler, dev);
963         ctx->conf_url = url;
964
965         iterate_parsers(ctx);
966
967         device_handler_discover_context_commit(handler, ctx);
968
969         status->message = talloc_asprintf(status,
970                                 _("Processing user config complete"));
971         boot_status(handler, status);
972
973         talloc_free(status);
974         talloc_free(ctx);
975
976         return 0;
977 }
978
979 static struct discover_boot_option *find_boot_option_by_id(
980                 struct device_handler *handler, const char *id)
981 {
982         unsigned int i;
983
984         for (i = 0; i < handler->n_devices; i++) {
985                 struct discover_device *dev = handler->devices[i];
986                 struct discover_boot_option *opt;
987
988                 list_for_each_entry(&dev->boot_options, opt, list)
989                         if (!strcmp(opt->option->id, id))
990                                 return opt;
991         }
992
993         return NULL;
994 }
995
996 void device_handler_boot(struct device_handler *handler,
997                 struct boot_command *cmd)
998 {
999         struct discover_boot_option *opt = NULL;
1000
1001         if (cmd->option_id && strlen(cmd->option_id))
1002                 opt = find_boot_option_by_id(handler, cmd->option_id);
1003
1004         if (handler->pending_boot)
1005                 boot_cancel(handler->pending_boot);
1006
1007         platform_pre_boot();
1008
1009         handler->pending_boot = boot(handler, opt, cmd, handler->dry_run,
1010                         boot_status, handler);
1011         handler->pending_boot_is_default = false;
1012 }
1013
1014 void device_handler_cancel_default(struct device_handler *handler)
1015 {
1016         struct boot_status status;
1017
1018         if (handler->timeout_waiter)
1019                 waiter_remove(handler->timeout_waiter);
1020
1021         handler->timeout_waiter = NULL;
1022         handler->autoboot_enabled = false;
1023
1024         /* we only send status if we had a default boot option queued */
1025         if (!handler->default_boot_option)
1026                 return;
1027
1028         pb_log("Cancelling default boot option\n");
1029
1030         if (handler->pending_boot && handler->pending_boot_is_default) {
1031                 boot_cancel(handler->pending_boot);
1032                 handler->pending_boot = NULL;
1033                 handler->pending_boot_is_default = false;
1034         }
1035
1036         handler->default_boot_option = NULL;
1037
1038         status.type = BOOT_STATUS_INFO;
1039         status.progress = -1;
1040         status.detail = NULL;
1041         status.message = _("Default boot cancelled");
1042
1043         discover_server_notify_boot_status(handler->server, &status);
1044 }
1045
1046 void device_handler_update_config(struct device_handler *handler,
1047                 struct config *config)
1048 {
1049         int rc;
1050
1051         rc = config_set(config);
1052         if (rc)
1053                 return;
1054
1055         discover_server_notify_config(handler->server, config);
1056         device_handler_update_lang(config->lang);
1057         device_handler_reinit(handler);
1058 }
1059
1060 static char *device_from_addr(void *ctx, struct pb_url *url)
1061 {
1062         char *ipaddr, *buf, *tok, *dev = NULL;
1063         const char *delim = " ";
1064         struct sockaddr_in *ip;
1065         struct sockaddr_in si;
1066         struct addrinfo *res;
1067         struct process *p;
1068         int rc;
1069
1070         /* Note: IPv4 only */
1071         rc = inet_pton(AF_INET, url->host, &(si.sin_addr));
1072         if (rc > 0) {
1073                 ipaddr = url->host;
1074         } else {
1075                 /* need to turn hostname into a valid IP */
1076                 rc = getaddrinfo(url->host, NULL, NULL, &res);
1077                 if (rc) {
1078                         pb_debug("%s: Invalid URL\n",__func__);
1079                         return NULL;
1080                 }
1081                 ipaddr = talloc_array(ctx,char,INET_ADDRSTRLEN);
1082                 ip = (struct sockaddr_in *) res->ai_addr;
1083                 inet_ntop(AF_INET, &(ip->sin_addr), ipaddr, INET_ADDRSTRLEN);
1084                 freeaddrinfo(res);
1085         }
1086
1087         const char *argv[] = {
1088                 pb_system_apps.ip,
1089                 "route", "show", "to", "match",
1090                 ipaddr,
1091                 NULL
1092         };
1093
1094         p = process_create(ctx);
1095
1096         p->path = pb_system_apps.ip;
1097         p->argv = argv;
1098         p->keep_stdout = true;
1099
1100         rc = process_run_sync(p);
1101
1102         if (rc) {
1103                 /* ip has complained for some reason; most likely
1104                  * there is no route to the host - bail out */
1105                 pb_debug("%s: No route to %s\n",__func__,url->host);
1106                 return NULL;
1107         }
1108
1109         buf = p->stdout_buf;
1110         /* If a route is found, ip-route output will be of the form
1111          * "... dev DEVNAME ... " */
1112         tok = strtok(buf, delim);
1113         while (tok) {
1114                 if (!strcmp(tok, "dev")) {
1115                         tok = strtok(NULL, delim);
1116                         dev = talloc_strdup(ctx, tok);
1117                         break;
1118                 }
1119                 tok = strtok(NULL, delim);
1120         }
1121
1122         process_release(p);
1123         if (dev)
1124                 pb_debug("%s: Found interface '%s'\n", __func__,dev);
1125         return dev;
1126 }
1127
1128 void device_handler_process_url(struct device_handler *handler,
1129                 const char *url, const char *mac, const char *ip)
1130 {
1131         struct discover_context *ctx;
1132         struct discover_device *dev;
1133         struct boot_status *status;
1134         struct pb_url *pb_url;
1135         struct event *event;
1136         struct param *param;
1137
1138         status = talloc(handler, struct boot_status);
1139
1140         status->type = BOOT_STATUS_ERROR;
1141         status->progress = 0;
1142         status->detail = talloc_asprintf(status,
1143                         _("Received config URL %s"), url);
1144
1145         if (!handler->network) {
1146                 status->message = talloc_asprintf(handler,
1147                                         _("No network configured"));
1148                 goto msg;
1149         }
1150
1151         event = talloc(handler, struct event);
1152         event->type = EVENT_TYPE_USER;
1153         event->action = EVENT_ACTION_CONF;
1154
1155         if (url[strlen(url) - 1] == '/') {
1156                 event->params = talloc_array(event, struct param, 3);
1157                 param = &event->params[0];
1158                 param->name = talloc_strdup(event, "pxepathprefix");
1159                 param->value = talloc_strdup(event, url);
1160                 param = &event->params[1];
1161                 param->name = talloc_strdup(event, "mac");
1162                 param->value = talloc_strdup(event, mac);
1163                 param = &event->params[2];
1164                 param->name = talloc_strdup(event, "ip");
1165                 param->value = talloc_strdup(event, ip);
1166                 event->n_params = 3;
1167         } else {
1168                 event->params = talloc_array(event, struct param, 1);
1169                 param = &event->params[0];
1170                 param->name = talloc_strdup(event, "pxeconffile");
1171                 param->value = talloc_strdup(event, url);
1172                 event->n_params = 1;
1173         }
1174
1175         pb_url = pb_url_parse(event, event->params->value);
1176         if (!pb_url || !pb_url->host) {
1177                 status->message = talloc_asprintf(handler,
1178                                         _("Invalid config URL!"));
1179                 goto msg;
1180         }
1181
1182         event->device = device_from_addr(event, pb_url);
1183         if (!event->device) {
1184                 status->message = talloc_asprintf(status,
1185                                         _("Unable to route to host %s"),
1186                                         pb_url->host);
1187                 goto msg;
1188         }
1189
1190         dev = discover_device_create(handler, event->device);
1191         ctx = device_handler_discover_context_create(handler, dev);
1192         ctx->event = event;
1193
1194         iterate_parsers(ctx);
1195
1196         device_handler_discover_context_commit(handler, ctx);
1197
1198         talloc_free(ctx);
1199
1200         status->type = BOOT_STATUS_INFO;
1201         status->message = talloc_asprintf(status, _("Config file %s parsed"),
1202                                         pb_url->file);
1203 msg:
1204         boot_status(handler, status);
1205         talloc_free(status);
1206 }
1207
1208 #ifndef PETITBOOT_TEST
1209
1210 static void device_handler_update_lang(const char *lang)
1211 {
1212         const char *cur_lang;
1213
1214         if (!lang)
1215                 return;
1216
1217         cur_lang = setlocale(LC_ALL, NULL);
1218         if (cur_lang && !strcmp(cur_lang, lang))
1219                 return;
1220
1221         setlocale(LC_ALL, lang);
1222 }
1223
1224 static int device_handler_init_sources(struct device_handler *handler)
1225 {
1226         /* init our device sources: udev, network and user events */
1227         handler->udev = udev_init(handler, handler->waitset);
1228         if (!handler->udev)
1229                 return -1;
1230
1231         handler->network = network_init(handler, handler->waitset,
1232                         handler->dry_run);
1233         if (!handler->network)
1234                 return -1;
1235
1236         handler->user_event = user_event_init(handler, handler->waitset);
1237         if (!handler->user_event)
1238                 return -1;
1239
1240         return 0;
1241 }
1242
1243 static void device_handler_reinit_sources(struct device_handler *handler)
1244 {
1245         /* if we haven't initialised sources previously (becuase we started in
1246          * safe mode), then init once here. */
1247         if (!(handler->udev || handler->network || handler->user_event)) {
1248                 device_handler_init_sources(handler);
1249                 return;
1250         }
1251
1252         udev_reinit(handler->udev);
1253
1254         network_shutdown(handler->network);
1255         handler->network = network_init(handler, handler->waitset,
1256                         handler->dry_run);
1257 }
1258
1259 static const char *fs_parameters(struct discover_device *dev,
1260                                  unsigned int rw_flags)
1261 {
1262         const char *fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1263
1264         /* XFS journals are not cross-endian compatible; don't try recovery
1265          * even if we have a snapshot */
1266         if (!strncmp(fstype, "xfs", strlen("xfs")))
1267                 return "norecovery";
1268
1269         /* If we have a snapshot available allow touching the filesystem */
1270         if (dev->ramdisk)
1271                 return "";
1272
1273         if ((rw_flags | MS_RDONLY) != MS_RDONLY)
1274                 return "";
1275
1276         /* Avoid writes due to journal replay if we don't have a snapshot */
1277         if (!strncmp(fstype, "ext4", strlen("ext4")))
1278                 return "norecovery";
1279
1280         return "";
1281 }
1282
1283 static inline const char *get_device_path(struct discover_device *dev)
1284 {
1285         return dev->ramdisk ? dev->ramdisk->snapshot : dev->device_path;
1286 }
1287
1288 static char *check_subvols(struct discover_device *dev)
1289 {
1290         const char *fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1291         struct stat sb;
1292         char *path;
1293         int rc;
1294
1295         if (strncmp(fstype, "btrfs", strlen("btrfs")))
1296                 return dev->mount_path;
1297
1298         /* On btrfs a device's root may be under a subvolume path */
1299         path = join_paths(dev, dev->mount_path, "@");
1300         rc = stat(path, &sb);
1301         if (!rc && S_ISDIR(sb.st_mode)) {
1302                 pb_debug("Using '%s' for btrfs root path\n", path);
1303                 return path;
1304         }
1305
1306         talloc_free(path);
1307         return dev->mount_path;
1308 }
1309
1310 static bool check_existing_mount(struct discover_device *dev)
1311 {
1312         struct stat devstat, mntstat;
1313         const char *device_path;
1314         struct mntent *mnt;
1315         FILE *fp;
1316         int rc;
1317
1318         device_path = get_device_path(dev);
1319
1320         rc = stat(device_path, &devstat);
1321         if (rc) {
1322                 pb_debug("%s: stat failed: %s\n", __func__, strerror(errno));
1323                 return false;
1324         }
1325
1326         if (!S_ISBLK(devstat.st_mode)) {
1327                 pb_debug("%s: %s isn't a block device?\n", __func__,
1328                                 dev->device_path);
1329                 return false;
1330         }
1331
1332         fp = fopen("/proc/self/mounts", "r");
1333
1334         for (;;) {
1335                 mnt = getmntent(fp);
1336                 if (!mnt)
1337                         break;
1338
1339                 if (!mnt->mnt_fsname || mnt->mnt_fsname[0] != '/')
1340                         continue;
1341
1342                 rc = stat(mnt->mnt_fsname, &mntstat);
1343                 if (rc)
1344                         continue;
1345
1346                 if (!S_ISBLK(mntstat.st_mode))
1347                         continue;
1348
1349                 if (mntstat.st_rdev == devstat.st_rdev) {
1350                         dev->mount_path = talloc_strdup(dev, mnt->mnt_dir);
1351                         dev->root_path = check_subvols(dev);
1352                         dev->mounted_rw = !!hasmntopt(mnt, "rw");
1353                         dev->mounted = true;
1354                         dev->unmount = false;
1355
1356                         pb_debug("%s: %s is already mounted (r%c) at %s\n",
1357                                         __func__, dev->device_path,
1358                                         dev->mounted_rw ? 'w' : 'o',
1359                                         mnt->mnt_dir);
1360                         break;
1361                 }
1362         }
1363
1364         fclose(fp);
1365
1366         return mnt != NULL;
1367 }
1368
1369 static int mount_device(struct discover_device *dev)
1370 {
1371         const char *fstype, *device_path;
1372         int rc;
1373
1374         if (!dev->device_path)
1375                 return -1;
1376
1377         if (dev->mounted)
1378                 return 0;
1379
1380         if (check_existing_mount(dev))
1381                 return 0;
1382
1383         fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1384         if (!fstype)
1385                 return 0;
1386
1387         /* ext3 treats the norecovery option as an error, so mount the device
1388          * as an ext4 filesystem instead */
1389         if (!strncmp(fstype, "ext3", strlen("ext3"))) {
1390                 pb_debug("Mounting ext3 filesystem as ext4\n");
1391                 fstype = talloc_asprintf(dev, "ext4");
1392         }
1393
1394         dev->mount_path = join_paths(dev, mount_base(),
1395                                         dev->device_path);
1396
1397         if (pb_mkdir_recursive(dev->mount_path)) {
1398                 pb_log("couldn't create mount directory %s: %s\n",
1399                                 dev->mount_path, strerror(errno));
1400                 goto err_free;
1401         }
1402
1403         device_path = get_device_path(dev);
1404
1405         pb_log("mounting device %s read-only\n", dev->device_path);
1406         errno = 0;
1407         rc = mount(device_path, dev->mount_path, fstype,
1408                         MS_RDONLY | MS_SILENT,
1409                         fs_parameters(dev, MS_RDONLY));
1410         if (!rc) {
1411                 dev->mounted = true;
1412                 dev->mounted_rw = false;
1413                 dev->unmount = true;
1414                 dev->root_path = check_subvols(dev);
1415                 return 0;
1416         }
1417
1418         pb_log("couldn't mount device %s: mount failed: %s\n",
1419                         device_path, strerror(errno));
1420
1421         /* If mount fails clean up any snapshot */
1422         devmapper_destroy_snapshot(dev);
1423
1424         pb_rmdir_recursive(mount_base(), dev->mount_path);
1425 err_free:
1426         talloc_free(dev->mount_path);
1427         dev->mount_path = NULL;
1428         return -1;
1429 }
1430
1431 static int umount_device(struct discover_device *dev)
1432 {
1433         const char *device_path;
1434         int rc;
1435
1436         if (!dev->mounted || !dev->unmount)
1437                 return 0;
1438
1439         device_path = get_device_path(dev);
1440
1441         pb_log("unmounting device %s\n", device_path);
1442         rc = umount(dev->mount_path);
1443         if (rc)
1444                 return -1;
1445
1446         dev->mounted = false;
1447         devmapper_destroy_snapshot(dev);
1448
1449         pb_rmdir_recursive(mount_base(), dev->mount_path);
1450
1451         talloc_free(dev->mount_path);
1452         dev->mount_path = NULL;
1453         dev->root_path = NULL;
1454
1455         return 0;
1456 }
1457
1458 int device_request_write(struct discover_device *dev, bool *release)
1459 {
1460         const char *fstype, *device_path;
1461         const struct config *config;
1462         int rc;
1463
1464         *release = false;
1465
1466         config = config_get();
1467         if (!config->allow_writes)
1468                 return -1;
1469
1470         if (!dev->mounted)
1471                 return -1;
1472
1473         if (dev->mounted_rw)
1474                 return 0;
1475
1476         fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1477
1478         device_path = get_device_path(dev);
1479
1480         pb_log("remounting device %s read-write\n", device_path);
1481
1482         rc = umount(dev->mount_path);
1483         if (rc) {
1484                 pb_log("Failed to unmount %s: %s\n",
1485                        dev->mount_path, strerror(errno));
1486                 return -1;
1487         }
1488
1489         rc = mount(device_path, dev->mount_path, fstype,
1490                         MS_SILENT,
1491                         fs_parameters(dev, MS_REMOUNT));
1492         if (rc)
1493                 goto mount_ro;
1494
1495         dev->mounted_rw = true;
1496         *release = true;
1497         return 0;
1498
1499 mount_ro:
1500         pb_log("Unable to remount device %s read-write: %s\n",
1501                device_path, strerror(errno));
1502         if (mount(device_path, dev->mount_path, fstype,
1503                         MS_RDONLY | MS_SILENT,
1504                         fs_parameters(dev, MS_RDONLY)))
1505                 pb_log("Unable to recover mount for %s: %s\n",
1506                        device_path, strerror(errno));
1507         return -1;
1508 }
1509
1510 void device_release_write(struct discover_device *dev, bool release)
1511 {
1512         const char *fstype, *device_path;
1513
1514         if (!release)
1515                 return;
1516
1517         device_path = get_device_path(dev);
1518
1519         fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1520
1521         pb_log("remounting device %s read-only\n", device_path);
1522
1523         if (umount(dev->mount_path)) {
1524                 pb_log("Failed to unmount %s\n", dev->mount_path);
1525                 return;
1526         }
1527         dev->mounted_rw = dev->mounted = false;
1528
1529         if (dev->ramdisk) {
1530                 devmapper_merge_snapshot(dev);
1531                 /* device_path becomes stale after merge */
1532                 device_path = get_device_path(dev);
1533         }
1534
1535         if (mount(device_path, dev->mount_path, fstype,
1536                         MS_RDONLY | MS_SILENT,
1537                         fs_parameters(dev, MS_RDONLY)))
1538                 pb_log("Failed to remount %s read-only: %s\n",
1539                        device_path, strerror(errno));
1540         else
1541                 dev->mounted = true;
1542 }
1543
1544 #else
1545
1546 static void device_handler_update_lang(const char *lang __attribute__((unused)))
1547 {
1548 }
1549
1550 static int device_handler_init_sources(
1551                 struct device_handler *handler __attribute__((unused)))
1552 {
1553         return 0;
1554 }
1555
1556 static void device_handler_reinit_sources(
1557                 struct device_handler *handler __attribute__((unused)))
1558 {
1559 }
1560
1561 static int umount_device(struct discover_device *dev __attribute__((unused)))
1562 {
1563         return 0;
1564 }
1565
1566 static int __attribute__((unused)) mount_device(
1567                 struct discover_device *dev __attribute__((unused)))
1568 {
1569         return 0;
1570 }
1571
1572 int device_request_write(struct discover_device *dev __attribute__((unused)),
1573                 bool *release)
1574 {
1575         *release = true;
1576         return 0;
1577 }
1578
1579 void device_release_write(struct discover_device *dev __attribute__((unused)),
1580         bool release __attribute__((unused)))
1581 {
1582 }
1583
1584 #endif
1585