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