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