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