c21eb286d2bd8b47d361fad1c537c883e50fdbbc
[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 <sys/stat.h>
9 #include <sys/wait.h>
10 #include <sys/mount.h>
11
12 #include <talloc/talloc.h>
13 #include <list/list.h>
14 #include <log/log.h>
15 #include <types/types.h>
16 #include <system/system.h>
17 #include <process/process.h>
18 #include <url/url.h>
19
20 #include "device-handler.h"
21 #include "discover-server.h"
22 #include "user-event.h"
23 #include "platform.h"
24 #include "event.h"
25 #include "parser.h"
26 #include "resource.h"
27 #include "paths.h"
28 #include "sysinfo.h"
29 #include "boot.h"
30 #include "udev.h"
31 #include "network.h"
32
33 struct device_handler {
34         struct discover_server  *server;
35         int                     dry_run;
36
37         struct pb_udev          *udev;
38         struct network          *network;
39         struct user_event       *user_event;
40
41         struct discover_device  **devices;
42         unsigned int            n_devices;
43
44         struct waitset          *waitset;
45         struct waiter           *timeout_waiter;
46         bool                    autoboot_enabled;
47         unsigned int            sec_to_boot;
48
49         struct discover_boot_option *default_boot_option;
50         struct list             unresolved_boot_options;
51
52         struct boot_task        *pending_boot;
53         bool                    pending_boot_is_default;
54 };
55
56 static int mount_device(struct discover_device *dev);
57 static int umount_device(struct discover_device *dev);
58
59 static int device_handler_init_sources(struct device_handler *handler);
60 static void device_handler_reinit_sources(struct device_handler *handler);
61
62 void discover_context_add_boot_option(struct discover_context *ctx,
63                 struct discover_boot_option *boot_option)
64 {
65         boot_option->source = ctx->parser;
66         list_add_tail(&ctx->boot_options, &boot_option->list);
67         talloc_steal(ctx, boot_option);
68 }
69
70 /**
71  * device_handler_get_device_count - Get the count of current handler devices.
72  */
73
74 int device_handler_get_device_count(const struct device_handler *handler)
75 {
76         return handler->n_devices;
77 }
78
79 /**
80  * device_handler_get_device - Get a handler device by index.
81  */
82
83 const struct discover_device *device_handler_get_device(
84         const struct device_handler *handler, unsigned int index)
85 {
86         if (index >= handler->n_devices) {
87                 assert(0 && "bad index");
88                 return NULL;
89         }
90
91         return handler->devices[index];
92 }
93
94 struct discover_boot_option *discover_boot_option_create(
95                 struct discover_context *ctx,
96                 struct discover_device *device)
97 {
98         struct discover_boot_option *opt;
99
100         opt = talloc_zero(ctx, struct discover_boot_option);
101         opt->option = talloc_zero(opt, struct boot_option);
102         opt->device = device;
103
104         return opt;
105 }
106
107 static int device_match_uuid(struct discover_device *dev, const char *uuid)
108 {
109         return dev->uuid && !strcmp(dev->uuid, uuid);
110 }
111
112 static int device_match_label(struct discover_device *dev, const char *label)
113 {
114         return dev->label && !strcmp(dev->label, label);
115 }
116
117 static int device_match_id(struct discover_device *dev, const char *id)
118 {
119         return !strcmp(dev->device->id, id);
120 }
121
122 static int device_match_serial(struct discover_device *dev, const char *serial)
123 {
124         const char *val = discover_device_get_param(dev, "ID_SERIAL");
125         return val && !strcmp(val, serial);
126 }
127
128 static struct discover_device *device_lookup(
129                 struct device_handler *device_handler,
130                 int (match_fn)(struct discover_device *, const char *),
131                 const char *str)
132 {
133         struct discover_device *dev;
134         unsigned int i;
135
136         if (!str)
137                 return NULL;
138
139         for (i = 0; i < device_handler->n_devices; i++) {
140                 dev = device_handler->devices[i];
141
142                 if (match_fn(dev, str))
143                         return dev;
144         }
145
146         return NULL;
147 }
148
149 struct discover_device *device_lookup_by_name(struct device_handler *handler,
150                 const char *name)
151 {
152         if (!strncmp(name, "/dev/", strlen("/dev/")))
153                 name += strlen("/dev/");
154
155         return device_lookup_by_id(handler, name);
156 }
157
158 struct discover_device *device_lookup_by_uuid(
159                 struct device_handler *device_handler,
160                 const char *uuid)
161 {
162         return device_lookup(device_handler, device_match_uuid, uuid);
163 }
164
165 struct discover_device *device_lookup_by_label(
166                 struct device_handler *device_handler,
167                 const char *label)
168 {
169         return device_lookup(device_handler, device_match_label, label);
170 }
171
172 struct discover_device *device_lookup_by_id(
173                 struct device_handler *device_handler,
174                 const char *id)
175 {
176         return device_lookup(device_handler, device_match_id, id);
177 }
178
179 struct discover_device *device_lookup_by_serial(
180                 struct device_handler *device_handler,
181                 const char *serial)
182 {
183         return device_lookup(device_handler, device_match_serial, serial);
184 }
185
186 void device_handler_destroy(struct device_handler *handler)
187 {
188         talloc_free(handler);
189 }
190
191 static int destroy_device(void *arg)
192 {
193         struct discover_device *dev = arg;
194
195         umount_device(dev);
196
197         return 0;
198 }
199
200 struct discover_device *discover_device_create(struct device_handler *handler,
201                 const char *id)
202 {
203         struct discover_device *dev;
204
205         dev = device_lookup_by_id(handler, id);
206         if (dev)
207                 return dev;
208
209         dev = talloc_zero(handler, struct discover_device);
210         dev->device = talloc_zero(dev, struct device);
211         dev->device->id = talloc_strdup(dev->device, id);
212         list_init(&dev->params);
213         list_init(&dev->boot_options);
214
215         talloc_set_destructor(dev, destroy_device);
216
217         return dev;
218 }
219
220 struct discover_device_param {
221         char                    *name;
222         char                    *value;
223         struct list_item        list;
224 };
225
226 void discover_device_set_param(struct discover_device *device,
227                 const char *name, const char *value)
228 {
229         struct discover_device_param *param;
230         bool found = false;
231
232         list_for_each_entry(&device->params, param, list) {
233                 if (!strcmp(param->name, name)) {
234                         found = true;
235                         break;
236                 }
237         }
238
239         if (!found) {
240                 if (!value)
241                         return;
242                 param = talloc(device, struct discover_device_param);
243                 param->name = talloc_strdup(param, name);
244                 list_add(&device->params, &param->list);
245         } else {
246                 if (!value) {
247                         list_remove(&param->list);
248                         talloc_free(param);
249                         return;
250                 }
251                 talloc_free(param->value);
252         }
253
254         param->value = talloc_strdup(param, value);
255 }
256
257 const char *discover_device_get_param(struct discover_device *device,
258                 const char *name)
259 {
260         struct discover_device_param *param;
261
262         list_for_each_entry(&device->params, param, list) {
263                 if (!strcmp(param->name, name))
264                         return param->value;
265         }
266         return NULL;
267 }
268
269 struct device_handler *device_handler_init(struct discover_server *server,
270                 struct waitset *waitset, int dry_run)
271 {
272         struct device_handler *handler;
273         int rc;
274
275         handler = talloc_zero(NULL, struct device_handler);
276         handler->server = server;
277         handler->waitset = waitset;
278         handler->dry_run = dry_run;
279         handler->autoboot_enabled = config_get()->autoboot_enabled;
280
281         list_init(&handler->unresolved_boot_options);
282
283         /* set up our mount point base */
284         pb_mkdir_recursive(mount_base());
285
286         parser_init();
287
288         rc = device_handler_init_sources(handler);
289         if (rc) {
290                 talloc_free(handler);
291                 return NULL;
292         }
293
294         return handler;
295 }
296
297 void device_handler_reinit(struct device_handler *handler)
298 {
299         struct discover_boot_option *opt, *tmp;
300         unsigned int i;
301
302         device_handler_cancel_default(handler);
303
304         /* free unresolved boot options */
305         list_for_each_entry_safe(&handler->unresolved_boot_options,
306                         opt, tmp, list)
307                 talloc_free(opt);
308         list_init(&handler->unresolved_boot_options);
309
310         /* drop all devices */
311         for (i = 0; i < handler->n_devices; i++)
312                 discover_server_notify_device_remove(handler->server,
313                                 handler->devices[i]->device);
314
315         talloc_free(handler->devices);
316         handler->devices = NULL;
317         handler->n_devices = 0;
318
319         device_handler_reinit_sources(handler);
320 }
321
322 void device_handler_remove(struct device_handler *handler,
323                 struct discover_device *device)
324 {
325         struct discover_boot_option *opt, *tmp;
326         unsigned int i;
327
328         for (i = 0; i < handler->n_devices; i++)
329                 if (handler->devices[i] == device)
330                         break;
331
332         if (i == handler->n_devices) {
333                 talloc_free(device);
334                 return;
335         }
336
337         /* Free any unresolved options, as they're currently allocated
338          * against the handler */
339         list_for_each_entry_safe(&handler->unresolved_boot_options,
340                         opt, tmp, list) {
341                 if (opt->device != device)
342                         continue;
343                 list_remove(&opt->list);
344                 talloc_free(opt);
345         }
346
347         handler->n_devices--;
348         memmove(&handler->devices[i], &handler->devices[i + 1],
349                 (handler->n_devices - i) * sizeof(handler->devices[0]));
350         handler->devices = talloc_realloc(handler, handler->devices,
351                 struct discover_device *, handler->n_devices);
352
353         if (device->notified)
354                 discover_server_notify_device_remove(handler->server,
355                                                         device->device);
356
357         talloc_free(device);
358 }
359
360 static void boot_status(void *arg, struct boot_status *status)
361 {
362         struct device_handler *handler = arg;
363
364         discover_server_notify_boot_status(handler->server, status);
365 }
366
367 static void countdown_status(struct device_handler *handler,
368                 struct discover_boot_option *opt, unsigned int sec)
369 {
370         struct boot_status status;
371
372         status.type = BOOT_STATUS_INFO;
373         status.progress = -1;
374         status.detail = NULL;
375         status.message = talloc_asprintf(handler,
376                         "Booting in %d sec: %s", sec, opt->option->name);
377
378         discover_server_notify_boot_status(handler->server, &status);
379
380         talloc_free(status.message);
381 }
382
383 static int default_timeout(void *arg)
384 {
385         struct device_handler *handler = arg;
386         struct discover_boot_option *opt;
387
388         if (!handler->default_boot_option)
389                 return 0;
390
391         if (handler->pending_boot)
392                 return 0;
393
394         opt = handler->default_boot_option;
395
396         if (handler->sec_to_boot) {
397                 countdown_status(handler, opt, handler->sec_to_boot);
398                 handler->sec_to_boot--;
399                 handler->timeout_waiter = waiter_register_timeout(
400                                                 handler->waitset, 1000,
401                                                 default_timeout, handler);
402                 return 0;
403         }
404
405         handler->timeout_waiter = NULL;
406
407         pb_log("Timeout expired, booting default option %s\n", opt->option->id);
408
409         handler->pending_boot = boot(handler, handler->default_boot_option,
410                         NULL, handler->dry_run, boot_status, handler);
411         handler->pending_boot_is_default = true;
412         return 0;
413 }
414
415 static bool priority_match(struct boot_priority *prio,
416                 struct discover_boot_option *opt)
417 {
418         return prio->type == opt->device->device->type ||
419                 prio->type == DEVICE_TYPE_ANY;
420 }
421
422 static int default_option_priority(struct discover_boot_option *opt)
423 {
424         const struct config *config;
425         struct boot_priority *prio;
426         unsigned int i;
427
428         config = config_get();
429
430         for (i = 0; i < config->n_boot_priorities; i++) {
431                 prio = &config->boot_priorities[i];
432                 if (priority_match(prio, opt))
433                         return prio->priority;
434         }
435
436         return 0;
437 }
438
439 static void set_default(struct device_handler *handler,
440                 struct discover_boot_option *opt)
441 {
442         int new_prio;
443
444         if (!handler->autoboot_enabled)
445                 return;
446
447         new_prio = default_option_priority(opt);
448
449         /* A negative priority indicates that we don't want to boot this device
450          * by default */
451         if (new_prio < 0)
452                 return;
453
454         /* Resolve any conflicts: if we have a new default option, it only
455          * replaces the current if it has a higher priority. */
456         if (handler->default_boot_option) {
457                 int cur_prio;
458
459                 cur_prio = default_option_priority(
460                                         handler->default_boot_option);
461
462                 if (new_prio > cur_prio) {
463                         handler->default_boot_option = opt;
464                         /* extend the timeout a little, so the user sees some
465                          * indication of the change */
466                         handler->sec_to_boot += 2;
467                 }
468
469                 return;
470         }
471
472         handler->sec_to_boot = config_get()->autoboot_timeout_sec;
473         handler->default_boot_option = opt;
474
475         pb_log("Boot option %s set as default, timeout %u sec.\n",
476                opt->option->id, handler->sec_to_boot);
477
478         default_timeout(handler);
479 }
480
481 static bool resource_is_resolved(struct resource *res)
482 {
483         return !res || res->resolved;
484 }
485
486 /* We only use this in an assert, which will disappear if we're compiling
487  * with NDEBUG, so we need the 'used' attribute for these builds */
488 static bool __attribute__((used)) boot_option_is_resolved(
489                 struct discover_boot_option *opt)
490 {
491         return resource_is_resolved(opt->boot_image) &&
492                 resource_is_resolved(opt->initrd) &&
493                 resource_is_resolved(opt->dtb) &&
494                 resource_is_resolved(opt->icon);
495 }
496
497 static bool resource_resolve(struct resource *res, const char *name,
498                 struct discover_boot_option *opt,
499                 struct device_handler *handler)
500 {
501         struct parser *parser = opt->source;
502
503         if (resource_is_resolved(res))
504                 return true;
505
506         pb_debug("Attempting to resolve resource %s->%s with parser %s\n",
507                         opt->option->id, name, parser->name);
508         parser->resolve_resource(handler, res);
509
510         return res->resolved;
511 }
512
513 static bool boot_option_resolve(struct discover_boot_option *opt,
514                 struct device_handler *handler)
515 {
516         return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
517                 resource_resolve(opt->initrd, "initrd", opt, handler) &&
518                 resource_resolve(opt->dtb, "dtb", opt, handler) &&
519                 resource_resolve(opt->icon, "icon", opt, handler);
520 }
521
522 static void boot_option_finalise(struct device_handler *handler,
523                 struct discover_boot_option *opt)
524 {
525         assert(boot_option_is_resolved(opt));
526
527         /* check that the parsers haven't set any of the final data */
528         assert(!opt->option->boot_image_file);
529         assert(!opt->option->initrd_file);
530         assert(!opt->option->dtb_file);
531         assert(!opt->option->icon_file);
532         assert(!opt->option->device_id);
533
534         if (opt->boot_image)
535                 opt->option->boot_image_file = opt->boot_image->url->full;
536         if (opt->initrd)
537                 opt->option->initrd_file = opt->initrd->url->full;
538         if (opt->dtb)
539                 opt->option->dtb_file = opt->dtb->url->full;
540         if (opt->icon)
541                 opt->option->icon_file = opt->icon->url->full;
542
543         opt->option->device_id = opt->device->device->id;
544
545         if (opt->option->is_default)
546                 set_default(handler, opt);
547 }
548
549 static void notify_boot_option(struct device_handler *handler,
550                 struct discover_boot_option *opt)
551 {
552         struct discover_device *dev = opt->device;
553
554         if (!dev->notified)
555                 discover_server_notify_device_add(handler->server,
556                                                   opt->device->device);
557         dev->notified = true;
558         discover_server_notify_boot_option_add(handler->server, opt->option);
559 }
560
561 static void process_boot_option_queue(struct device_handler *handler)
562 {
563         struct discover_boot_option *opt, *tmp;
564
565         list_for_each_entry_safe(&handler->unresolved_boot_options,
566                         opt, tmp, list) {
567
568                 pb_debug("queue: attempting resolution for %s\n",
569                                 opt->option->id);
570
571                 if (!boot_option_resolve(opt, handler))
572                         continue;
573
574                 pb_debug("\tresolved!\n");
575
576                 list_remove(&opt->list);
577                 list_add_tail(&opt->device->boot_options, &opt->list);
578                 talloc_steal(opt->device, opt);
579                 boot_option_finalise(handler, opt);
580                 notify_boot_option(handler, opt);
581         }
582 }
583
584 struct discover_context *device_handler_discover_context_create(
585                 struct device_handler *handler,
586                 struct discover_device *device)
587 {
588         struct discover_context *ctx;
589
590         ctx = talloc_zero(handler, struct discover_context);
591         ctx->device = device;
592         list_init(&ctx->boot_options);
593
594         return ctx;
595 }
596
597 /**
598  * context_commit - Commit a temporary discovery context to the handler,
599  * and notify the clients about any new options / devices
600  */
601 void device_handler_discover_context_commit(struct device_handler *handler,
602                 struct discover_context *ctx)
603 {
604         struct discover_device *dev = ctx->device;
605         struct discover_boot_option *opt, *tmp;
606
607         if (!device_lookup_by_id(handler, dev->device->id))
608                 device_handler_add_device(handler, dev);
609
610         /* move boot options from the context to the device */
611         list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
612                 list_remove(&opt->list);
613
614                 if (boot_option_resolve(opt, handler)) {
615                         pb_log("boot option %s is resolved, "
616                                         "sending to clients\n",
617                                         opt->option->id);
618                         list_add_tail(&dev->boot_options, &opt->list);
619                         talloc_steal(dev, opt);
620                         boot_option_finalise(handler, opt);
621                         notify_boot_option(handler, opt);
622                 } else {
623                         if (!opt->source->resolve_resource) {
624                                 pb_log("parser %s gave us an unresolved "
625                                         "resource (%s), but no way to "
626                                         "resolve it\n",
627                                         opt->source->name, opt->option->id);
628                                 talloc_free(opt);
629                         } else {
630                                 pb_log("boot option %s is unresolved, "
631                                                 "adding to queue\n",
632                                                 opt->option->id);
633                                 list_add(&handler->unresolved_boot_options,
634                                                 &opt->list);
635                                 talloc_steal(handler, opt);
636                         }
637                 }
638         }
639 }
640
641 void device_handler_add_device(struct device_handler *handler,
642                 struct discover_device *device)
643 {
644         handler->n_devices++;
645         handler->devices = talloc_realloc(handler, handler->devices,
646                                 struct discover_device *, handler->n_devices);
647         handler->devices[handler->n_devices - 1] = device;
648
649 }
650
651 /* Start discovery on a hotplugged device. The device will be in our devices
652  * array, but has only just been initialised by the hotplug source.
653  */
654 int device_handler_discover(struct device_handler *handler,
655                 struct discover_device *dev)
656 {
657         struct discover_context *ctx;
658         int rc;
659
660         process_boot_option_queue(handler);
661
662         /* create our context */
663         ctx = device_handler_discover_context_create(handler, dev);
664
665         rc = mount_device(dev);
666         if (rc)
667                 goto out;
668
669         /* add this device to our system info */
670         system_info_register_blockdev(dev->device->id, dev->uuid,
671                         dev->mount_path);
672
673         /* run the parsers. This will populate the ctx's boot_option list. */
674         iterate_parsers(ctx);
675
676         /* add discovered stuff to the handler */
677         device_handler_discover_context_commit(handler, ctx);
678
679 out:
680         talloc_free(ctx);
681
682         return 0;
683 }
684
685 /* Incoming dhcp event */
686 int device_handler_dhcp(struct device_handler *handler,
687                 struct discover_device *dev, struct event *event)
688 {
689         struct discover_context *ctx;
690
691         /* create our context */
692         ctx = device_handler_discover_context_create(handler, dev);
693         ctx->event = event;
694
695         iterate_parsers(ctx);
696
697         device_handler_discover_context_commit(handler, ctx);
698
699         talloc_free(ctx);
700
701         return 0;
702 }
703
704 /* incoming conf event */
705 int device_handler_conf(struct device_handler *handler,
706                 struct discover_device *dev, struct pb_url *url)
707 {
708         struct discover_context *ctx;
709
710         /* create our context */
711         ctx = device_handler_discover_context_create(handler, dev);
712         ctx->conf_url = url;
713
714         iterate_parsers(ctx);
715
716         device_handler_discover_context_commit(handler, ctx);
717
718         talloc_free(ctx);
719
720         return 0;
721 }
722
723 static struct discover_boot_option *find_boot_option_by_id(
724                 struct device_handler *handler, const char *id)
725 {
726         unsigned int i;
727
728         for (i = 0; i < handler->n_devices; i++) {
729                 struct discover_device *dev = handler->devices[i];
730                 struct discover_boot_option *opt;
731
732                 list_for_each_entry(&dev->boot_options, opt, list)
733                         if (!strcmp(opt->option->id, id))
734                                 return opt;
735         }
736
737         return NULL;
738 }
739
740 void device_handler_boot(struct device_handler *handler,
741                 struct boot_command *cmd)
742 {
743         struct discover_boot_option *opt = NULL;
744
745         if (cmd->option_id && strlen(cmd->option_id))
746                 opt = find_boot_option_by_id(handler, cmd->option_id);
747
748         if (handler->pending_boot)
749                 boot_cancel(handler->pending_boot);
750         handler->pending_boot = boot(handler, opt, cmd, handler->dry_run,
751                         boot_status, handler);
752         handler->pending_boot_is_default = false;
753 }
754
755 void device_handler_cancel_default(struct device_handler *handler)
756 {
757         struct boot_status status;
758
759         if (handler->timeout_waiter)
760                 waiter_remove(handler->timeout_waiter);
761
762         handler->timeout_waiter = NULL;
763         handler->autoboot_enabled = false;
764
765         /* we only send status if we had a default boot option queued */
766         if (!handler->default_boot_option)
767                 return;
768
769         pb_log("Cancelling default boot option\n");
770
771         if (handler->pending_boot && handler->pending_boot_is_default) {
772                 boot_cancel(handler->pending_boot);
773                 handler->pending_boot = NULL;
774                 handler->pending_boot_is_default = false;
775         }
776
777         handler->default_boot_option = NULL;
778
779         status.type = BOOT_STATUS_INFO;
780         status.progress = -1;
781         status.detail = NULL;
782         status.message = "Default boot cancelled";
783
784         discover_server_notify_boot_status(handler->server, &status);
785 }
786
787 void device_handler_update_config(struct device_handler *handler,
788                 struct config *config)
789 {
790         config_set(config);
791         discover_server_notify_config(handler->server, config);
792         device_handler_reinit(handler);
793 }
794
795 #ifndef PETITBOOT_TEST
796
797 static int device_handler_init_sources(struct device_handler *handler)
798 {
799         /* init our device sources: udev, network and user events */
800         handler->udev = udev_init(handler, handler->waitset);
801         if (!handler->udev)
802                 return -1;
803
804         handler->network = network_init(handler, handler->waitset,
805                         handler->dry_run);
806         if (!handler->network)
807                 return -1;
808
809         handler->user_event = user_event_init(handler, handler->waitset);
810         if (!handler->user_event)
811                 return -1;
812
813         return 0;
814 }
815
816 static void device_handler_reinit_sources(struct device_handler *handler)
817 {
818         udev_reinit(handler->udev);
819
820         network_shutdown(handler->network);
821         handler->network = network_init(handler, handler->waitset,
822                         handler->dry_run);
823 }
824
825 static bool check_existing_mount(struct discover_device *dev)
826 {
827         struct stat devstat, mntstat;
828         struct mntent *mnt;
829         FILE *fp;
830         int rc;
831
832         rc = stat(dev->device_path, &devstat);
833         if (rc) {
834                 pb_debug("%s: stat failed: %s\n", __func__, strerror(errno));
835                 return false;
836         }
837
838         if (!S_ISBLK(devstat.st_mode)) {
839                 pb_debug("%s: %s isn't a block device?\n", __func__,
840                                 dev->device_path);
841                 return false;
842         }
843
844         fp = fopen("/proc/self/mounts", "r");
845
846         for (;;) {
847                 mnt = getmntent(fp);
848                 if (!mnt)
849                         break;
850
851                 if (!mnt->mnt_fsname || mnt->mnt_fsname[0] != '/')
852                         continue;
853
854                 rc = stat(mnt->mnt_fsname, &mntstat);
855                 if (rc)
856                         continue;
857
858                 if (!S_ISBLK(mntstat.st_mode))
859                         continue;
860
861                 if (mntstat.st_rdev == devstat.st_rdev) {
862                         dev->mount_path = talloc_strdup(dev, mnt->mnt_dir);
863                         dev->mounted_rw = !!hasmntopt(mnt, "rw");
864                         dev->mounted = true;
865                         dev->unmount = false;
866
867                         pb_debug("%s: %s is already mounted (r%c) at %s\n",
868                                         __func__, dev->device_path,
869                                         dev->mounted_rw ? 'w' : 'o',
870                                         mnt->mnt_dir);
871                         break;
872                 }
873         }
874
875         fclose(fp);
876
877         return mnt != NULL;
878 }
879
880 static int mount_device(struct discover_device *dev)
881 {
882         const char *fstype;
883         int rc;
884
885         if (!dev->device_path)
886                 return -1;
887
888         if (dev->mounted)
889                 return 0;
890
891         if (check_existing_mount(dev))
892                 return 0;
893
894         fstype = discover_device_get_param(dev, "ID_FS_TYPE");
895         if (!fstype)
896                 return 0;
897
898         dev->mount_path = join_paths(dev, mount_base(),
899                                         dev->device_path);
900
901         if (pb_mkdir_recursive(dev->mount_path)) {
902                 pb_log("couldn't create mount directory %s: %s\n",
903                                 dev->mount_path, strerror(errno));
904                 goto err_free;
905         }
906
907         pb_log("mounting device %s read-only\n", dev->device_path);
908         errno = 0;
909         rc = mount(dev->device_path, dev->mount_path, fstype,
910                         MS_RDONLY | MS_SILENT, "");
911         if (!rc) {
912                 dev->mounted = true;
913                 dev->mounted_rw = false;
914                 dev->unmount = true;
915                 return 0;
916         }
917
918         pb_log("couldn't mount device %s: mount failed: %s\n",
919                         dev->device_path, strerror(errno));
920
921         pb_rmdir_recursive(mount_base(), dev->mount_path);
922 err_free:
923         talloc_free(dev->mount_path);
924         dev->mount_path = NULL;
925         return -1;
926 }
927
928 static int umount_device(struct discover_device *dev)
929 {
930         int rc;
931
932         if (!dev->mounted || !dev->unmount)
933                 return 0;
934
935         pb_log("unmounting device %s\n", dev->device_path);
936         rc = umount(dev->mount_path);
937         if (rc)
938                 return -1;
939
940         dev->mounted = false;
941
942         pb_rmdir_recursive(mount_base(), dev->mount_path);
943
944         talloc_free(dev->mount_path);
945         dev->mount_path = NULL;
946
947         return 0;
948 }
949
950 int device_request_write(struct discover_device *dev, bool *release)
951 {
952         int rc;
953
954         *release = false;
955
956         if (!dev->mounted)
957                 return -1;
958
959         if (dev->mounted_rw)
960                 return 0;
961
962         pb_log("remounting device %s read-write\n", dev->device_path);
963         rc = mount(dev->device_path, dev->mount_path, "",
964                         MS_REMOUNT | MS_SILENT, "");
965         if (rc)
966                 return -1;
967
968         dev->mounted_rw = true;
969         *release = true;
970         return 0;
971 }
972
973 void device_release_write(struct discover_device *dev, bool release)
974 {
975         if (!release)
976                 return;
977
978         pb_log("remounting device %s read-only\n", dev->device_path);
979         mount(dev->device_path, dev->mount_path, "",
980                         MS_REMOUNT | MS_RDONLY | MS_SILENT, "");
981         dev->mounted_rw = false;
982 }
983
984 #else
985
986 static int device_handler_init_sources(
987                 struct device_handler *handler __attribute__((unused)))
988 {
989         return 0;
990 }
991
992 static void device_handler_reinit_sources(
993                 struct device_handler *handler __attribute__((unused)))
994 {
995 }
996
997 static int umount_device(struct discover_device *dev __attribute__((unused)))
998 {
999         return 0;
1000 }
1001
1002 static int __attribute__((unused)) mount_device(
1003                 struct discover_device *dev __attribute__((unused)))
1004 {
1005         return 0;
1006 }
1007
1008 int device_request_write(struct discover_device *dev __attribute__((unused)),
1009                 bool *release)
1010 {
1011         *release = true;
1012         return 0;
1013 }
1014
1015 void device_release_write(struct discover_device *dev __attribute__((unused)),
1016         bool release __attribute__((unused)))
1017 {
1018 }
1019
1020 #endif
1021