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