]> git.ozlabs.org Git - petitboot/blob - discover/device-handler.c
discover/device-handler: Remove default option if device removed
[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         list_for_each_entry_safe(&device->boot_options, opt, tmp, list) {
363                 if (opt == handler->default_boot_option) {
364                         pb_log("Default option %s cancelled since device removed",
365                                         opt->option->name);
366                         device_handler_cancel_default(handler);
367                         break;
368                 }
369         }
370
371         for (i = 0; i < handler->n_devices; i++)
372                 if (handler->devices[i] == device)
373                         break;
374
375         if (i == handler->n_devices) {
376                 talloc_free(device);
377                 return;
378         }
379
380         /* Free any unresolved options, as they're currently allocated
381          * against the handler */
382         list_for_each_entry_safe(&handler->unresolved_boot_options,
383                         opt, tmp, list) {
384                 if (opt->device != device)
385                         continue;
386                 list_remove(&opt->list);
387                 talloc_free(opt);
388         }
389
390         /* if this is a network device, we have to unregister it from the
391          * network code */
392         if (device->device->type == DEVICE_TYPE_NETWORK)
393                 network_unregister_device(handler->network, device);
394
395         handler->n_devices--;
396         memmove(&handler->devices[i], &handler->devices[i + 1],
397                 (handler->n_devices - i) * sizeof(handler->devices[0]));
398         handler->devices = talloc_realloc(handler, handler->devices,
399                 struct discover_device *, handler->n_devices);
400
401         if (device->notified)
402                 discover_server_notify_device_remove(handler->server,
403                                                         device->device);
404
405         talloc_free(device);
406 }
407
408 void device_handler_boot_status(void *arg, struct boot_status *status)
409 {
410         struct device_handler *handler = arg;
411
412         discover_server_notify_boot_status(handler->server, status);
413 }
414
415 static void countdown_status(struct device_handler *handler,
416                 struct discover_boot_option *opt, unsigned int sec)
417 {
418         struct boot_status status;
419
420         status.type = BOOT_STATUS_INFO;
421         status.progress = -1;
422         status.detail = NULL;
423         status.message = talloc_asprintf(handler,
424                         _("Booting in %d sec: %s"), sec, opt->option->name);
425
426         discover_server_notify_boot_status(handler->server, &status);
427
428         talloc_free(status.message);
429 }
430
431 static int default_timeout(void *arg)
432 {
433         struct device_handler *handler = arg;
434         struct discover_boot_option *opt;
435
436         if (!handler->default_boot_option)
437                 return 0;
438
439         if (handler->pending_boot)
440                 return 0;
441
442         opt = handler->default_boot_option;
443
444         if (handler->sec_to_boot) {
445                 countdown_status(handler, opt, handler->sec_to_boot);
446                 handler->sec_to_boot--;
447                 handler->timeout_waiter = waiter_register_timeout(
448                                                 handler->waitset, 1000,
449                                                 default_timeout, handler);
450                 return 0;
451         }
452
453         handler->timeout_waiter = NULL;
454
455         pb_log("Timeout expired, booting default option %s\n", opt->option->id);
456
457         platform_pre_boot();
458
459         handler->pending_boot = boot(handler, handler->default_boot_option,
460                         NULL, handler->dry_run, device_handler_boot_status,
461                         handler);
462         handler->pending_boot_is_default = true;
463         return 0;
464 }
465
466 struct {
467         enum ipmi_bootdev       ipmi_type;
468         enum device_type        device_type;
469 } device_type_map[] = {
470         { IPMI_BOOTDEV_NETWORK, DEVICE_TYPE_NETWORK },
471         { IPMI_BOOTDEV_DISK, DEVICE_TYPE_DISK },
472         { IPMI_BOOTDEV_DISK, DEVICE_TYPE_USB },
473         { IPMI_BOOTDEV_CDROM, DEVICE_TYPE_OPTICAL },
474 };
475
476 static bool ipmi_device_type_matches(enum ipmi_bootdev ipmi_type,
477                 enum device_type device_type)
478 {
479         unsigned int i;
480
481         for (i = 0; i < ARRAY_SIZE(device_type_map); i++) {
482                 if (device_type_map[i].device_type == device_type)
483                         return device_type_map[i].ipmi_type == ipmi_type;
484         }
485
486         return false;
487 }
488
489 static int autoboot_option_priority(const struct config *config,
490                                 struct discover_boot_option *opt)
491 {
492         enum device_type type = opt->device->device->type;
493         const char *uuid = opt->device->uuid;
494         struct autoboot_option *auto_opt;
495         unsigned int i;
496
497         for (i = 0; i < config->n_autoboot_opts; i++) {
498                 auto_opt = &config->autoboot_opts[i];
499                 if (auto_opt->boot_type == BOOT_DEVICE_UUID)
500                         if (!strcmp(auto_opt->uuid, uuid))
501                                 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
502
503                 if (auto_opt->boot_type == BOOT_DEVICE_TYPE)
504                         if (auto_opt->type == type ||
505                             auto_opt->type == DEVICE_TYPE_ANY)
506                                 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
507         }
508
509         return -1;
510 }
511
512 /*
513  * We have different priorities to resolve conflicts between boot options that
514  * report to be the default for their device. This function assigns a priority
515  * for these options.
516  */
517 static enum default_priority default_option_priority(
518                 struct discover_boot_option *opt)
519 {
520         const struct config *config;
521
522         config = config_get();
523
524         /* We give highest priority to IPMI-configured boot options. If
525          * we have an IPMI bootdev configuration set, then we don't allow
526          * any other defaults */
527         if (config->ipmi_bootdev) {
528                 bool ipmi_match = ipmi_device_type_matches(config->ipmi_bootdev,
529                                 opt->device->device->type);
530                 if (ipmi_match)
531                         return DEFAULT_PRIORITY_REMOTE;
532
533                 pb_debug("handler: disabled default priority due to "
534                                 "non-matching IPMI type %x\n",
535                                 config->ipmi_bootdev);
536                 return DEFAULT_PRIORITY_DISABLED;
537         }
538
539         /* Next, try to match the option against the user-defined autoboot
540          * options, either by device UUID or type. */
541         if (config->n_autoboot_opts) {
542                 int boot_match = autoboot_option_priority(config, opt);
543                 if (boot_match > 0)
544                         return boot_match;
545         }
546
547         /* If the option didn't match any entry in the array, it is disabled */
548         pb_debug("handler: disabled default priority due to "
549                         "non-matching UUID or type\n");
550         return DEFAULT_PRIORITY_DISABLED;
551 }
552
553 static void set_default(struct device_handler *handler,
554                 struct discover_boot_option *opt)
555 {
556         enum default_priority cur_prio, new_prio;
557
558         if (!handler->autoboot_enabled)
559                 return;
560
561         pb_debug("handler: new default option: %s\n", opt->option->id);
562
563         new_prio = default_option_priority(opt);
564
565         /* Anything outside our range prevents a default boot */
566         if (new_prio >= DEFAULT_PRIORITY_DISABLED)
567                 return;
568
569         pb_debug("handler: calculated priority %d\n", new_prio);
570
571         /* Resolve any conflicts: if we have a new default option, it only
572          * replaces the current if it has a higher priority. */
573         if (handler->default_boot_option) {
574
575                 cur_prio = handler->default_boot_option_priority;
576
577                 if (new_prio < cur_prio) {
578                         pb_log("handler: new prio %d beats "
579                                         "old prio %d for %s\n",
580                                         new_prio, cur_prio,
581                                         handler->default_boot_option
582                                                 ->option->id);
583                         handler->default_boot_option = opt;
584                         handler->default_boot_option_priority = new_prio;
585                         /* extend the timeout a little, so the user sees some
586                          * indication of the change */
587                         handler->sec_to_boot += 2;
588                 }
589
590                 return;
591         }
592
593         handler->sec_to_boot = config_get()->autoboot_timeout_sec;
594         handler->default_boot_option = opt;
595         handler->default_boot_option_priority = new_prio;
596
597         pb_log("handler: boot option %s set as default, timeout %u sec.\n",
598                opt->option->id, handler->sec_to_boot);
599
600         default_timeout(handler);
601 }
602
603 static bool resource_is_resolved(struct resource *res)
604 {
605         return !res || res->resolved;
606 }
607
608 /* We only use this in an assert, which will disappear if we're compiling
609  * with NDEBUG, so we need the 'used' attribute for these builds */
610 static bool __attribute__((used)) boot_option_is_resolved(
611                 struct discover_boot_option *opt)
612 {
613         return resource_is_resolved(opt->boot_image) &&
614                 resource_is_resolved(opt->initrd) &&
615                 resource_is_resolved(opt->dtb) &&
616                 resource_is_resolved(opt->icon);
617 }
618
619 static bool resource_resolve(struct resource *res, const char *name,
620                 struct discover_boot_option *opt,
621                 struct device_handler *handler)
622 {
623         struct parser *parser = opt->source;
624
625         if (resource_is_resolved(res))
626                 return true;
627
628         pb_debug("Attempting to resolve resource %s->%s with parser %s\n",
629                         opt->option->id, name, parser->name);
630         parser->resolve_resource(handler, res);
631
632         return res->resolved;
633 }
634
635 static bool boot_option_resolve(struct discover_boot_option *opt,
636                 struct device_handler *handler)
637 {
638         return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
639                 resource_resolve(opt->initrd, "initrd", opt, handler) &&
640                 resource_resolve(opt->dtb, "dtb", opt, handler) &&
641                 resource_resolve(opt->icon, "icon", opt, handler);
642 }
643
644 static void boot_option_finalise(struct device_handler *handler,
645                 struct discover_boot_option *opt)
646 {
647         assert(boot_option_is_resolved(opt));
648
649         /* check that the parsers haven't set any of the final data */
650         assert(!opt->option->boot_image_file);
651         assert(!opt->option->initrd_file);
652         assert(!opt->option->dtb_file);
653         assert(!opt->option->icon_file);
654         assert(!opt->option->device_id);
655
656         if (opt->boot_image)
657                 opt->option->boot_image_file = opt->boot_image->url->full;
658         if (opt->initrd)
659                 opt->option->initrd_file = opt->initrd->url->full;
660         if (opt->dtb)
661                 opt->option->dtb_file = opt->dtb->url->full;
662         if (opt->icon)
663                 opt->option->icon_file = opt->icon->url->full;
664
665         opt->option->device_id = opt->device->device->id;
666
667         if (opt->option->is_default)
668                 set_default(handler, opt);
669 }
670
671 static void notify_boot_option(struct device_handler *handler,
672                 struct discover_boot_option *opt)
673 {
674         struct discover_device *dev = opt->device;
675
676         if (!dev->notified)
677                 discover_server_notify_device_add(handler->server,
678                                                   opt->device->device);
679         dev->notified = true;
680         discover_server_notify_boot_option_add(handler->server, opt->option);
681 }
682
683 static void process_boot_option_queue(struct device_handler *handler)
684 {
685         struct discover_boot_option *opt, *tmp;
686
687         list_for_each_entry_safe(&handler->unresolved_boot_options,
688                         opt, tmp, list) {
689
690                 pb_debug("queue: attempting resolution for %s\n",
691                                 opt->option->id);
692
693                 if (!boot_option_resolve(opt, handler))
694                         continue;
695
696                 pb_debug("\tresolved!\n");
697
698                 list_remove(&opt->list);
699                 list_add_tail(&opt->device->boot_options, &opt->list);
700                 talloc_steal(opt->device, opt);
701                 boot_option_finalise(handler, opt);
702                 notify_boot_option(handler, opt);
703         }
704 }
705
706 struct discover_context *device_handler_discover_context_create(
707                 struct device_handler *handler,
708                 struct discover_device *device)
709 {
710         struct discover_context *ctx;
711
712         ctx = talloc_zero(handler, struct discover_context);
713         ctx->device = device;
714         ctx->network = handler->network;
715         list_init(&ctx->boot_options);
716
717         return ctx;
718 }
719
720 void device_handler_add_device(struct device_handler *handler,
721                 struct discover_device *device)
722 {
723         handler->n_devices++;
724         handler->devices = talloc_realloc(handler, handler->devices,
725                                 struct discover_device *, handler->n_devices);
726         handler->devices[handler->n_devices - 1] = device;
727
728         if (device->device->type == DEVICE_TYPE_NETWORK)
729                 network_register_device(handler->network, device);
730 }
731
732 void device_handler_add_ramdisk(struct device_handler *handler,
733                 const char *path)
734 {
735         struct ramdisk_device *dev;
736         unsigned int i;
737
738         if (!path)
739                 return;
740
741         for (i = 0; i < handler->n_ramdisks; i++)
742                 if (!strcmp(handler->ramdisks[i]->path, path))
743                         return;
744
745         dev = talloc_zero(handler, struct ramdisk_device);
746         if (!dev) {
747                 pb_log("Failed to allocate memory to track %s\n", path);
748                 return;
749         }
750
751         dev->path = talloc_strdup(handler, path);
752
753         handler->ramdisks = talloc_realloc(handler, handler->ramdisks,
754                                 struct ramdisk_device *,
755                                 handler->n_ramdisks + 1);
756         if (!handler->ramdisks) {
757                 pb_log("Failed to reallocate memory"
758                        "- ramdisk tracking inconsistent!\n");
759                 return;
760         }
761
762         handler->ramdisks[i] = dev;
763         i = handler->n_ramdisks++;
764 }
765
766 struct ramdisk_device *device_handler_get_ramdisk(
767                 struct device_handler *handler)
768 {
769         unsigned int i;
770         char *name;
771         dev_t id;
772
773         /* Check if free ramdisk exists */
774         for (i = 0; i < handler->n_ramdisks; i++)
775                 if (!handler->ramdisks[i]->snapshot &&
776                     !handler->ramdisks[i]->origin &&
777                     !handler->ramdisks[i]->base)
778                         return handler->ramdisks[i];
779
780         /* Otherwise create a new one */
781         name = talloc_asprintf(handler, "/dev/ram%d",
782                         handler->n_ramdisks);
783         if (!name) {
784                 pb_debug("Failed to allocate memory to name /dev/ram%d",
785                         handler->n_ramdisks);
786                 return NULL;
787         }
788
789         id = makedev(1, handler->n_ramdisks);
790         if (mknod(name, S_IFBLK, id)) {
791                 if (errno == EEXIST) {
792                         /* We haven't yet received updates for existing
793                          * ramdisks - add and use this one */
794                         pb_debug("Using untracked ramdisk %s\n", name);
795                 } else {
796                         pb_log("Failed to create new ramdisk %s: %s\n",
797                                name, strerror(errno));
798                         return NULL;
799                 }
800         }
801         device_handler_add_ramdisk(handler, name);
802         talloc_free(name);
803
804         return handler->ramdisks[i];
805 }
806
807 void device_handler_release_ramdisk(struct discover_device *device)
808 {
809         struct ramdisk_device *ramdisk = device->ramdisk;
810
811         talloc_free(ramdisk->snapshot);
812         talloc_free(ramdisk->origin);
813         talloc_free(ramdisk->base);
814
815         ramdisk->snapshot = ramdisk->origin = ramdisk->base = NULL;
816         ramdisk->sectors = 0;
817
818         device->ramdisk = NULL;
819 }
820
821 /* Start discovery on a hotplugged device. The device will be in our devices
822  * array, but has only just been initialised by the hotplug source.
823  */
824 int device_handler_discover(struct device_handler *handler,
825                 struct discover_device *dev)
826 {
827         struct discover_context *ctx;
828         struct boot_status *status;
829         int rc;
830
831         status = talloc_zero(handler, struct boot_status);
832         status->type = BOOT_STATUS_INFO;
833         /*
834          * TRANSLATORS: this string will be passed the type and identifier
835          * of the device. For example, the first parameter could be "Disk",
836          * (which will be translated accordingly) and the second a Linux device
837          * identifier like 'sda1' (which will not be translated)
838          */
839         status->message = talloc_asprintf(status, _("Processing %s device %s"),
840                                 device_type_display_name(dev->device->type),
841                                 dev->device->id);
842         device_handler_boot_status(handler, status);
843
844         process_boot_option_queue(handler);
845
846         /* create our context */
847         ctx = device_handler_discover_context_create(handler, dev);
848
849         rc = mount_device(dev);
850         if (rc)
851                 goto out;
852
853         /* add this device to our system info */
854         system_info_register_blockdev(dev->device->id, dev->uuid,
855                         dev->mount_path);
856
857         /* run the parsers. This will populate the ctx's boot_option list. */
858         iterate_parsers(ctx);
859
860         /* add discovered stuff to the handler */
861         device_handler_discover_context_commit(handler, ctx);
862
863 out:
864         /*
865          * TRANSLATORS: the format specifier in this string is a Linux
866          * device identifier, like 'sda1'
867          */
868         status->message = talloc_asprintf(status,_("Processing %s complete"),
869                                 dev->device->id);
870         device_handler_boot_status(handler, status);
871
872         talloc_free(status);
873         talloc_unlink(handler, ctx);
874
875         return 0;
876 }
877
878 /* Incoming dhcp event */
879 int device_handler_dhcp(struct device_handler *handler,
880                 struct discover_device *dev, struct event *event)
881 {
882         struct discover_context *ctx;
883         struct boot_status *status;
884
885         status = talloc_zero(handler, struct boot_status);
886         status->type = BOOT_STATUS_INFO;
887         /*
888          * TRANSLATORS: this format specifier will be the name of a network
889          * device, like 'eth0'.
890          */
891         status->message = talloc_asprintf(status, _("Processing dhcp event on %s"),
892                                 dev->device->id);
893         device_handler_boot_status(handler, status);
894
895         /* create our context */
896         ctx = device_handler_discover_context_create(handler, dev);
897         ctx->event = event;
898
899         iterate_parsers(ctx);
900
901         device_handler_discover_context_commit(handler, ctx);
902
903         /*
904          * TRANSLATORS: this format specifier will be the name of a network
905          * device, like 'eth0'.
906          */
907         status->message = talloc_asprintf(status,_("Processing %s complete"),
908                                 dev->device->id);
909         device_handler_boot_status(handler, status);
910
911         talloc_free(status);
912         talloc_unlink(handler, ctx);
913
914         return 0;
915 }
916
917 /* incoming conf event */
918 int device_handler_conf(struct device_handler *handler,
919                 struct discover_device *dev, struct pb_url *url)
920 {
921         struct discover_context *ctx;
922         struct boot_status *status;
923
924         status = talloc_zero(handler, struct boot_status);
925         status->type = BOOT_STATUS_INFO;
926         status->message = talloc_asprintf(status, _("Processing user config"));
927         device_handler_boot_status(handler, status);
928
929         /* create our context */
930         ctx = device_handler_discover_context_create(handler, dev);
931         ctx->conf_url = url;
932
933         iterate_parsers(ctx);
934
935         device_handler_discover_context_commit(handler, ctx);
936
937         status->message = talloc_asprintf(status,
938                                 _("Processing user config complete"));
939         device_handler_boot_status(handler, status);
940
941         talloc_free(status);
942         talloc_unlink(handler, ctx);
943
944         return 0;
945 }
946
947 static struct discover_boot_option *find_boot_option_by_id(
948                 struct device_handler *handler, const char *id)
949 {
950         unsigned int i;
951
952         for (i = 0; i < handler->n_devices; i++) {
953                 struct discover_device *dev = handler->devices[i];
954                 struct discover_boot_option *opt;
955
956                 list_for_each_entry(&dev->boot_options, opt, list)
957                         if (!strcmp(opt->option->id, id))
958                                 return opt;
959         }
960
961         return NULL;
962 }
963
964 void device_handler_boot(struct device_handler *handler,
965                 struct boot_command *cmd)
966 {
967         struct discover_boot_option *opt = NULL;
968
969         if (cmd->option_id && strlen(cmd->option_id))
970                 opt = find_boot_option_by_id(handler, cmd->option_id);
971
972         if (handler->pending_boot)
973                 boot_cancel(handler->pending_boot);
974
975         platform_pre_boot();
976
977         handler->pending_boot = boot(handler, opt, cmd, handler->dry_run,
978                         device_handler_boot_status, handler);
979         handler->pending_boot_is_default = false;
980 }
981
982 void device_handler_cancel_default(struct device_handler *handler)
983 {
984         struct boot_status status;
985
986         if (handler->timeout_waiter)
987                 waiter_remove(handler->timeout_waiter);
988
989         handler->timeout_waiter = NULL;
990         handler->autoboot_enabled = false;
991
992         /* we only send status if we had a default boot option queued */
993         if (!handler->default_boot_option)
994                 return;
995
996         pb_log("Cancelling default boot option\n");
997
998         if (handler->pending_boot && handler->pending_boot_is_default) {
999                 boot_cancel(handler->pending_boot);
1000                 handler->pending_boot = NULL;
1001                 handler->pending_boot_is_default = false;
1002         }
1003
1004         handler->default_boot_option = NULL;
1005
1006         status.type = BOOT_STATUS_INFO;
1007         status.progress = -1;
1008         status.detail = NULL;
1009         status.message = _("Default boot cancelled");
1010
1011         discover_server_notify_boot_status(handler->server, &status);
1012 }
1013
1014 void device_handler_update_config(struct device_handler *handler,
1015                 struct config *config)
1016 {
1017         int rc;
1018
1019         rc = config_set(config);
1020         if (rc)
1021                 return;
1022
1023         discover_server_notify_config(handler->server, config);
1024         device_handler_update_lang(config->lang);
1025         device_handler_reinit(handler);
1026 }
1027
1028 static char *device_from_addr(void *ctx, struct pb_url *url)
1029 {
1030         char *ipaddr, *buf, *tok, *dev = NULL;
1031         const char *delim = " ";
1032         struct sockaddr_in *ip;
1033         struct sockaddr_in si;
1034         struct addrinfo *res;
1035         struct process *p;
1036         int rc;
1037
1038         /* Note: IPv4 only */
1039         rc = inet_pton(AF_INET, url->host, &(si.sin_addr));
1040         if (rc > 0) {
1041                 ipaddr = url->host;
1042         } else {
1043                 /* need to turn hostname into a valid IP */
1044                 rc = getaddrinfo(url->host, NULL, NULL, &res);
1045                 if (rc) {
1046                         pb_debug("%s: Invalid URL\n",__func__);
1047                         return NULL;
1048                 }
1049                 ipaddr = talloc_array(ctx,char,INET_ADDRSTRLEN);
1050                 ip = (struct sockaddr_in *) res->ai_addr;
1051                 inet_ntop(AF_INET, &(ip->sin_addr), ipaddr, INET_ADDRSTRLEN);
1052                 freeaddrinfo(res);
1053         }
1054
1055         const char *argv[] = {
1056                 pb_system_apps.ip,
1057                 "route", "show", "to", "match",
1058                 ipaddr,
1059                 NULL
1060         };
1061
1062         p = process_create(ctx);
1063
1064         p->path = pb_system_apps.ip;
1065         p->argv = argv;
1066         p->keep_stdout = true;
1067
1068         rc = process_run_sync(p);
1069
1070         if (rc || p->exit_status) {
1071                 /* ip has complained for some reason; most likely
1072                  * there is no route to the host - bail out */
1073                 pb_debug("%s: `ip` returns non-zero exit status\n", __func__);
1074                 pb_debug("ip buf: %s\n", p->stdout_buf);
1075                 process_release(p);
1076                 return NULL;
1077         }
1078
1079         buf = p->stdout_buf;
1080         /* If a route is found, ip-route output will be of the form
1081          * "... dev DEVNAME ... " */
1082         tok = strtok(buf, delim);
1083         while (tok) {
1084                 if (!strcmp(tok, "dev")) {
1085                         tok = strtok(NULL, delim);
1086                         dev = talloc_strdup(ctx, tok);
1087                         break;
1088                 }
1089                 tok = strtok(NULL, delim);
1090         }
1091
1092         process_release(p);
1093         if (dev)
1094                 pb_debug("%s: Found interface '%s'\n", __func__,dev);
1095         return dev;
1096 }
1097
1098 void device_handler_process_url(struct device_handler *handler,
1099                 const char *url, const char *mac, const char *ip)
1100 {
1101         struct discover_context *ctx;
1102         struct discover_device *dev;
1103         struct boot_status *status;
1104         struct pb_url *pb_url;
1105         struct event *event;
1106         struct param *param;
1107
1108         status = talloc(handler, struct boot_status);
1109
1110         status->type = BOOT_STATUS_ERROR;
1111         status->progress = 0;
1112         status->detail = talloc_asprintf(status,
1113                         _("Received config URL %s"), url);
1114
1115         if (!handler->network) {
1116                 status->message = talloc_asprintf(handler,
1117                                         _("No network configured"));
1118                 goto msg;
1119         }
1120
1121         event = talloc(handler, struct event);
1122         event->type = EVENT_TYPE_USER;
1123         event->action = EVENT_ACTION_CONF;
1124
1125         if (url[strlen(url) - 1] == '/') {
1126                 event->params = talloc_array(event, struct param, 3);
1127                 param = &event->params[0];
1128                 param->name = talloc_strdup(event, "pxepathprefix");
1129                 param->value = talloc_strdup(event, url);
1130                 param = &event->params[1];
1131                 param->name = talloc_strdup(event, "mac");
1132                 param->value = talloc_strdup(event, mac);
1133                 param = &event->params[2];
1134                 param->name = talloc_strdup(event, "ip");
1135                 param->value = talloc_strdup(event, ip);
1136                 event->n_params = 3;
1137         } else {
1138                 event->params = talloc_array(event, struct param, 1);
1139                 param = &event->params[0];
1140                 param->name = talloc_strdup(event, "pxeconffile");
1141                 param->value = talloc_strdup(event, url);
1142                 event->n_params = 1;
1143         }
1144
1145         pb_url = pb_url_parse(event, event->params->value);
1146         if (!pb_url || (pb_url->scheme != pb_url_file && !pb_url->host)) {
1147                 status->message = talloc_asprintf(handler,
1148                                         _("Invalid config URL!"));
1149                 goto msg;
1150         }
1151
1152         if (pb_url->scheme == pb_url_file)
1153                 event->device = talloc_asprintf(event, "local");
1154         else
1155                 event->device = device_from_addr(event, pb_url);
1156
1157         if (!event->device) {
1158                 status->message = talloc_asprintf(status,
1159                                         _("Unable to route to host %s"),
1160                                         pb_url->host);
1161                 goto msg;
1162         }
1163
1164         dev = discover_device_create(handler, event->device);
1165         if (pb_url->scheme == pb_url_file)
1166                 dev->device->type = DEVICE_TYPE_ANY;
1167         ctx = device_handler_discover_context_create(handler, dev);
1168         ctx->event = event;
1169
1170         iterate_parsers(ctx);
1171
1172         device_handler_discover_context_commit(handler, ctx);
1173
1174         talloc_unlink(handler, ctx);
1175
1176         status->type = BOOT_STATUS_INFO;
1177         status->message = talloc_asprintf(status, _("Config file %s parsed"),
1178                                         pb_url->file);
1179 msg:
1180         device_handler_boot_status(handler, status);
1181         talloc_free(status);
1182 }
1183
1184 #ifndef PETITBOOT_TEST
1185
1186 /**
1187  * context_commit - Commit a temporary discovery context to the handler,
1188  * and notify the clients about any new options / devices
1189  */
1190 void device_handler_discover_context_commit(struct device_handler *handler,
1191                 struct discover_context *ctx)
1192 {
1193         struct discover_device *dev = ctx->device;
1194         struct discover_boot_option *opt, *tmp;
1195
1196         if (!device_lookup_by_id(handler, dev->device->id))
1197                 device_handler_add_device(handler, dev);
1198
1199         /* move boot options from the context to the device */
1200         list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
1201                 list_remove(&opt->list);
1202
1203                 /* All boot options need at least a kernel image */
1204                 if (!opt->boot_image || !opt->boot_image->url) {
1205                         pb_log("boot option %s is missing boot image, ignoring\n",
1206                                 opt->option->id);
1207                         talloc_free(opt);
1208                         continue;
1209                 }
1210
1211                 if (boot_option_resolve(opt, handler)) {
1212                         pb_log("boot option %s is resolved, "
1213                                         "sending to clients\n",
1214                                         opt->option->id);
1215                         list_add_tail(&dev->boot_options, &opt->list);
1216                         talloc_steal(dev, opt);
1217                         boot_option_finalise(handler, opt);
1218                         notify_boot_option(handler, opt);
1219                 } else {
1220                         if (!opt->source->resolve_resource) {
1221                                 pb_log("parser %s gave us an unresolved "
1222                                         "resource (%s), but no way to "
1223                                         "resolve it\n",
1224                                         opt->source->name, opt->option->id);
1225                                 talloc_free(opt);
1226                         } else {
1227                                 pb_log("boot option %s is unresolved, "
1228                                                 "adding to queue\n",
1229                                                 opt->option->id);
1230                                 list_add(&handler->unresolved_boot_options,
1231                                                 &opt->list);
1232                                 talloc_steal(handler, opt);
1233                         }
1234                 }
1235         }
1236 }
1237
1238 static void device_handler_update_lang(const char *lang)
1239 {
1240         const char *cur_lang;
1241
1242         if (!lang)
1243                 return;
1244
1245         cur_lang = setlocale(LC_ALL, NULL);
1246         if (cur_lang && !strcmp(cur_lang, lang))
1247                 return;
1248
1249         setlocale(LC_ALL, lang);
1250 }
1251
1252 static int device_handler_init_sources(struct device_handler *handler)
1253 {
1254         /* init our device sources: udev, network and user events */
1255         handler->udev = udev_init(handler, handler->waitset);
1256         if (!handler->udev)
1257                 return -1;
1258
1259         handler->network = network_init(handler, handler->waitset,
1260                         handler->dry_run);
1261         if (!handler->network)
1262                 return -1;
1263
1264         handler->user_event = user_event_init(handler, handler->waitset);
1265         if (!handler->user_event)
1266                 return -1;
1267
1268         return 0;
1269 }
1270
1271 static void device_handler_reinit_sources(struct device_handler *handler)
1272 {
1273         /* if we haven't initialised sources previously (becuase we started in
1274          * safe mode), then init once here. */
1275         if (!(handler->udev || handler->network || handler->user_event)) {
1276                 device_handler_init_sources(handler);
1277                 return;
1278         }
1279
1280         udev_reinit(handler->udev);
1281
1282         network_shutdown(handler->network);
1283         handler->network = network_init(handler, handler->waitset,
1284                         handler->dry_run);
1285 }
1286
1287 static inline const char *get_device_path(struct discover_device *dev)
1288 {
1289         return dev->ramdisk ? dev->ramdisk->snapshot : dev->device_path;
1290 }
1291
1292 static char *check_subvols(struct discover_device *dev)
1293 {
1294         const char *fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1295         struct stat sb;
1296         char *path;
1297         int rc;
1298
1299         if (strncmp(fstype, "btrfs", strlen("btrfs")))
1300                 return dev->mount_path;
1301
1302         /* On btrfs a device's root may be under a subvolume path */
1303         path = join_paths(dev, dev->mount_path, "@");
1304         rc = stat(path, &sb);
1305         if (!rc && S_ISDIR(sb.st_mode)) {
1306                 pb_debug("Using '%s' for btrfs root path\n", path);
1307                 return path;
1308         }
1309
1310         talloc_free(path);
1311         return dev->mount_path;
1312 }
1313
1314 static bool check_existing_mount(struct discover_device *dev)
1315 {
1316         struct stat devstat, mntstat;
1317         const char *device_path;
1318         struct mntent *mnt;
1319         FILE *fp;
1320         int rc;
1321
1322         device_path = get_device_path(dev);
1323
1324         rc = stat(device_path, &devstat);
1325         if (rc) {
1326                 pb_debug("%s: stat failed: %s\n", __func__, strerror(errno));
1327                 return false;
1328         }
1329
1330         if (!S_ISBLK(devstat.st_mode)) {
1331                 pb_debug("%s: %s isn't a block device?\n", __func__,
1332                                 dev->device_path);
1333                 return false;
1334         }
1335
1336         fp = fopen("/proc/self/mounts", "r");
1337
1338         for (;;) {
1339                 mnt = getmntent(fp);
1340                 if (!mnt)
1341                         break;
1342
1343                 if (!mnt->mnt_fsname || mnt->mnt_fsname[0] != '/')
1344                         continue;
1345
1346                 rc = stat(mnt->mnt_fsname, &mntstat);
1347                 if (rc)
1348                         continue;
1349
1350                 if (!S_ISBLK(mntstat.st_mode))
1351                         continue;
1352
1353                 if (mntstat.st_rdev == devstat.st_rdev) {
1354                         dev->mount_path = talloc_strdup(dev, mnt->mnt_dir);
1355                         dev->root_path = check_subvols(dev);
1356                         dev->mounted_rw = !!hasmntopt(mnt, "rw");
1357                         dev->mounted = true;
1358                         dev->unmount = false;
1359
1360                         pb_debug("%s: %s is already mounted (r%c) at %s\n",
1361                                         __func__, dev->device_path,
1362                                         dev->mounted_rw ? 'w' : 'o',
1363                                         mnt->mnt_dir);
1364                         break;
1365                 }
1366         }
1367
1368         fclose(fp);
1369
1370         return mnt != NULL;
1371 }
1372
1373 /*
1374  * Attempt to mount a filesystem safely, while handling certain filesytem-
1375  * specific options
1376  */
1377 static int try_mount(const char *device_path, const char *mount_path,
1378                              const char *fstype, unsigned long flags,
1379                              bool have_snapshot)
1380 {
1381         const char *fs, *safe_opts;
1382         int rc;
1383
1384         /* Mount ext3 as ext4 instead so 'norecovery' can be used */
1385         if (strncmp(fstype, "ext3", strlen("ext3")) == 0) {
1386                 pb_debug("Mounting ext3 filesystem as ext4\n");
1387                 fs = "ext4";
1388         } else
1389                 fs = fstype;
1390
1391         if (strncmp(fs, "xfs", strlen("xfs")) == 0 ||
1392             strncmp(fs, "ext4", strlen("ext4")) == 0)
1393                 safe_opts = "norecovery";
1394         else
1395                 safe_opts = NULL;
1396
1397         errno = 0;
1398         /* If no snapshot is available don't attempt recovery */
1399         if (!have_snapshot)
1400                 return mount(device_path, mount_path, fs, flags, safe_opts);
1401
1402         rc = mount(device_path, mount_path, fs, flags, NULL);
1403
1404         if (!rc)
1405                 return rc;
1406
1407         /* Mounting failed; some filesystems will fail to mount if a recovery
1408          * journal exists (eg. cross-endian XFS), so try again with norecovery
1409          * where that option is available.
1410          * If mounting read-write just return the error as norecovery is not a
1411          * valid option */
1412         if ((flags & MS_RDONLY) != MS_RDONLY || !safe_opts)
1413                 return rc;
1414
1415         errno = 0;
1416         return mount(device_path, mount_path, fs, flags, safe_opts);
1417 }
1418
1419 static int mount_device(struct discover_device *dev)
1420 {
1421         const char *fstype, *device_path;
1422         int rc;
1423
1424         if (!dev->device_path)
1425                 return -1;
1426
1427         if (dev->mounted)
1428                 return 0;
1429
1430         if (check_existing_mount(dev))
1431                 return 0;
1432
1433         fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1434         if (!fstype)
1435                 return 0;
1436
1437         dev->mount_path = join_paths(dev, mount_base(),
1438                                         dev->device_path);
1439
1440         if (pb_mkdir_recursive(dev->mount_path)) {
1441                 pb_log("couldn't create mount directory %s: %s\n",
1442                                 dev->mount_path, strerror(errno));
1443                 goto err_free;
1444         }
1445
1446         device_path = get_device_path(dev);
1447
1448         pb_log("mounting device %s read-only\n", dev->device_path);
1449         rc = try_mount(device_path, dev->mount_path, fstype,
1450                        MS_RDONLY | MS_SILENT, dev->ramdisk);
1451
1452         if (!rc) {
1453                 dev->mounted = true;
1454                 dev->mounted_rw = false;
1455                 dev->unmount = true;
1456                 dev->root_path = check_subvols(dev);
1457                 return 0;
1458         }
1459
1460         pb_log("couldn't mount device %s: mount failed: %s\n",
1461                         device_path, strerror(errno));
1462
1463         /* If mount fails clean up any snapshot */
1464         devmapper_destroy_snapshot(dev);
1465
1466         pb_rmdir_recursive(mount_base(), dev->mount_path);
1467 err_free:
1468         talloc_free(dev->mount_path);
1469         dev->mount_path = NULL;
1470         return -1;
1471 }
1472
1473 static int umount_device(struct discover_device *dev)
1474 {
1475         const char *device_path;
1476         int rc;
1477
1478         if (!dev->mounted || !dev->unmount)
1479                 return 0;
1480
1481         device_path = get_device_path(dev);
1482
1483         pb_log("unmounting device %s\n", device_path);
1484         rc = umount(dev->mount_path);
1485         if (rc)
1486                 return -1;
1487
1488         dev->mounted = false;
1489         devmapper_destroy_snapshot(dev);
1490
1491         pb_rmdir_recursive(mount_base(), dev->mount_path);
1492
1493         talloc_free(dev->mount_path);
1494         dev->mount_path = NULL;
1495         dev->root_path = NULL;
1496
1497         return 0;
1498 }
1499
1500 int device_request_write(struct discover_device *dev, bool *release)
1501 {
1502         const char *fstype, *device_path;
1503         const struct config *config;
1504         int rc;
1505
1506         *release = false;
1507
1508         config = config_get();
1509         if (!config->allow_writes)
1510                 return -1;
1511
1512         if (!dev->mounted)
1513                 return -1;
1514
1515         if (dev->mounted_rw)
1516                 return 0;
1517
1518         fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1519
1520         device_path = get_device_path(dev);
1521
1522         pb_log("remounting device %s read-write\n", device_path);
1523
1524         rc = umount(dev->mount_path);
1525         if (rc) {
1526                 pb_log("Failed to unmount %s: %s\n",
1527                        dev->mount_path, strerror(errno));
1528                 return -1;
1529         }
1530
1531         rc = try_mount(device_path, dev->mount_path, fstype,
1532                        MS_SILENT, dev->ramdisk);
1533         if (rc)
1534                 goto mount_ro;
1535
1536         dev->mounted_rw = true;
1537         *release = true;
1538         return 0;
1539
1540 mount_ro:
1541         pb_log("Unable to remount device %s read-write: %s\n",
1542                device_path, strerror(errno));
1543         rc = try_mount(device_path, dev->mount_path, fstype,
1544                        MS_RDONLY | MS_SILENT, dev->ramdisk);
1545         if (rc)
1546                 pb_log("Unable to recover mount for %s: %s\n",
1547                        device_path, strerror(errno));
1548         return -1;
1549 }
1550
1551 void device_release_write(struct discover_device *dev, bool release)
1552 {
1553         const char *fstype, *device_path;
1554
1555         if (!release)
1556                 return;
1557
1558         device_path = get_device_path(dev);
1559
1560         fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1561
1562         pb_log("remounting device %s read-only\n", device_path);
1563
1564         if (umount(dev->mount_path)) {
1565                 pb_log("Failed to unmount %s\n", dev->mount_path);
1566                 return;
1567         }
1568         dev->mounted_rw = dev->mounted = false;
1569
1570         if (dev->ramdisk) {
1571                 devmapper_merge_snapshot(dev);
1572                 /* device_path becomes stale after merge */
1573                 device_path = get_device_path(dev);
1574         }
1575
1576         if (try_mount(device_path, dev->mount_path, fstype,
1577                        MS_RDONLY | MS_SILENT, dev->ramdisk))
1578                 pb_log("Failed to remount %s read-only: %s\n",
1579                        device_path, strerror(errno));
1580         else
1581                 dev->mounted = true;
1582 }
1583
1584 #else
1585
1586 void device_handler_discover_context_commit(
1587                 struct device_handler *handler __attribute__((unused)),
1588                 struct discover_context *ctx __attribute__((unused)))
1589 {
1590         pb_log("%s stubbed out for test cases\n", __func__);
1591 }
1592
1593 static void device_handler_update_lang(const char *lang __attribute__((unused)))
1594 {
1595 }
1596
1597 static int device_handler_init_sources(
1598                 struct device_handler *handler __attribute__((unused)))
1599 {
1600         return 0;
1601 }
1602
1603 static void device_handler_reinit_sources(
1604                 struct device_handler *handler __attribute__((unused)))
1605 {
1606 }
1607
1608 static int umount_device(struct discover_device *dev __attribute__((unused)))
1609 {
1610         return 0;
1611 }
1612
1613 static int __attribute__((unused)) mount_device(
1614                 struct discover_device *dev __attribute__((unused)))
1615 {
1616         return 0;
1617 }
1618
1619 int device_request_write(struct discover_device *dev __attribute__((unused)),
1620                 bool *release)
1621 {
1622         *release = true;
1623         return 0;
1624 }
1625
1626 void device_release_write(struct discover_device *dev __attribute__((unused)),
1627         bool release __attribute__((unused)))
1628 {
1629 }
1630
1631 #endif