]> git.ozlabs.org Git - petitboot/blob - discover/device-handler.c
discover/powerpc: Parse & save default boot device parameter
[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         /* if this is a network device, we have to unregister it from the
348          * network code */
349         if (device->device->type == DEVICE_TYPE_NETWORK)
350                 network_unregister_device(handler->network, device);
351
352         handler->n_devices--;
353         memmove(&handler->devices[i], &handler->devices[i + 1],
354                 (handler->n_devices - i) * sizeof(handler->devices[0]));
355         handler->devices = talloc_realloc(handler, handler->devices,
356                 struct discover_device *, handler->n_devices);
357
358         if (device->notified)
359                 discover_server_notify_device_remove(handler->server,
360                                                         device->device);
361
362         talloc_free(device);
363 }
364
365 static void boot_status(void *arg, struct boot_status *status)
366 {
367         struct device_handler *handler = arg;
368
369         discover_server_notify_boot_status(handler->server, status);
370 }
371
372 static void countdown_status(struct device_handler *handler,
373                 struct discover_boot_option *opt, unsigned int sec)
374 {
375         struct boot_status status;
376
377         status.type = BOOT_STATUS_INFO;
378         status.progress = -1;
379         status.detail = NULL;
380         status.message = talloc_asprintf(handler,
381                         "Booting in %d sec: %s", sec, opt->option->name);
382
383         discover_server_notify_boot_status(handler->server, &status);
384
385         talloc_free(status.message);
386 }
387
388 static int default_timeout(void *arg)
389 {
390         struct device_handler *handler = arg;
391         struct discover_boot_option *opt;
392
393         if (!handler->default_boot_option)
394                 return 0;
395
396         if (handler->pending_boot)
397                 return 0;
398
399         opt = handler->default_boot_option;
400
401         if (handler->sec_to_boot) {
402                 countdown_status(handler, opt, handler->sec_to_boot);
403                 handler->sec_to_boot--;
404                 handler->timeout_waiter = waiter_register_timeout(
405                                                 handler->waitset, 1000,
406                                                 default_timeout, handler);
407                 return 0;
408         }
409
410         handler->timeout_waiter = NULL;
411
412         pb_log("Timeout expired, booting default option %s\n", opt->option->id);
413
414         handler->pending_boot = boot(handler, handler->default_boot_option,
415                         NULL, handler->dry_run, boot_status, handler);
416         handler->pending_boot_is_default = true;
417         return 0;
418 }
419
420 static bool priority_match(struct boot_priority *prio,
421                 struct discover_boot_option *opt)
422 {
423         return prio->type == opt->device->device->type ||
424                 prio->type == DEVICE_TYPE_ANY;
425 }
426
427 static int default_option_priority(struct discover_boot_option *opt)
428 {
429         const struct config *config;
430         struct boot_priority *prio;
431         unsigned int i;
432
433         config = config_get();
434
435         for (i = 0; i < config->n_boot_priorities; i++) {
436                 prio = &config->boot_priorities[i];
437                 if (priority_match(prio, opt))
438                         return prio->priority;
439         }
440
441         return 0;
442 }
443
444 static bool device_allows_default(struct discover_device *dev)
445 {
446         const char *dev_str;
447
448         dev_str = config_get()->boot_device;
449
450         if (!dev_str || !strlen(dev_str))
451                 return true;
452
453         /* default devices are specified by UUIDs at present */
454         if (strcmp(dev->uuid, dev_str))
455                 return false;
456
457         return true;
458 }
459
460 static void set_default(struct device_handler *handler,
461                 struct discover_boot_option *opt)
462 {
463         int new_prio;
464
465         if (!handler->autoboot_enabled)
466                 return;
467
468         /* do we allow default-booting from this device? */
469         if (!device_allows_default(opt->device))
470                 return;
471
472         new_prio = default_option_priority(opt);
473
474         /* A negative priority indicates that we don't want to boot this device
475          * by default */
476         if (new_prio < 0)
477                 return;
478
479         /* Resolve any conflicts: if we have a new default option, it only
480          * replaces the current if it has a higher priority. */
481         if (handler->default_boot_option) {
482                 int cur_prio;
483
484                 cur_prio = default_option_priority(
485                                         handler->default_boot_option);
486
487                 if (new_prio > cur_prio) {
488                         handler->default_boot_option = opt;
489                         /* extend the timeout a little, so the user sees some
490                          * indication of the change */
491                         handler->sec_to_boot += 2;
492                 }
493
494                 return;
495         }
496
497         handler->sec_to_boot = config_get()->autoboot_timeout_sec;
498         handler->default_boot_option = opt;
499
500         pb_log("Boot option %s set as default, timeout %u sec.\n",
501                opt->option->id, handler->sec_to_boot);
502
503         default_timeout(handler);
504 }
505
506 static bool resource_is_resolved(struct resource *res)
507 {
508         return !res || res->resolved;
509 }
510
511 /* We only use this in an assert, which will disappear if we're compiling
512  * with NDEBUG, so we need the 'used' attribute for these builds */
513 static bool __attribute__((used)) boot_option_is_resolved(
514                 struct discover_boot_option *opt)
515 {
516         return resource_is_resolved(opt->boot_image) &&
517                 resource_is_resolved(opt->initrd) &&
518                 resource_is_resolved(opt->dtb) &&
519                 resource_is_resolved(opt->icon);
520 }
521
522 static bool resource_resolve(struct resource *res, const char *name,
523                 struct discover_boot_option *opt,
524                 struct device_handler *handler)
525 {
526         struct parser *parser = opt->source;
527
528         if (resource_is_resolved(res))
529                 return true;
530
531         pb_debug("Attempting to resolve resource %s->%s with parser %s\n",
532                         opt->option->id, name, parser->name);
533         parser->resolve_resource(handler, res);
534
535         return res->resolved;
536 }
537
538 static bool boot_option_resolve(struct discover_boot_option *opt,
539                 struct device_handler *handler)
540 {
541         return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
542                 resource_resolve(opt->initrd, "initrd", opt, handler) &&
543                 resource_resolve(opt->dtb, "dtb", opt, handler) &&
544                 resource_resolve(opt->icon, "icon", opt, handler);
545 }
546
547 static void boot_option_finalise(struct device_handler *handler,
548                 struct discover_boot_option *opt)
549 {
550         assert(boot_option_is_resolved(opt));
551
552         /* check that the parsers haven't set any of the final data */
553         assert(!opt->option->boot_image_file);
554         assert(!opt->option->initrd_file);
555         assert(!opt->option->dtb_file);
556         assert(!opt->option->icon_file);
557         assert(!opt->option->device_id);
558
559         if (opt->boot_image)
560                 opt->option->boot_image_file = opt->boot_image->url->full;
561         if (opt->initrd)
562                 opt->option->initrd_file = opt->initrd->url->full;
563         if (opt->dtb)
564                 opt->option->dtb_file = opt->dtb->url->full;
565         if (opt->icon)
566                 opt->option->icon_file = opt->icon->url->full;
567
568         opt->option->device_id = opt->device->device->id;
569
570         if (opt->option->is_default)
571                 set_default(handler, opt);
572 }
573
574 static void notify_boot_option(struct device_handler *handler,
575                 struct discover_boot_option *opt)
576 {
577         struct discover_device *dev = opt->device;
578
579         if (!dev->notified)
580                 discover_server_notify_device_add(handler->server,
581                                                   opt->device->device);
582         dev->notified = true;
583         discover_server_notify_boot_option_add(handler->server, opt->option);
584 }
585
586 static void process_boot_option_queue(struct device_handler *handler)
587 {
588         struct discover_boot_option *opt, *tmp;
589
590         list_for_each_entry_safe(&handler->unresolved_boot_options,
591                         opt, tmp, list) {
592
593                 pb_debug("queue: attempting resolution for %s\n",
594                                 opt->option->id);
595
596                 if (!boot_option_resolve(opt, handler))
597                         continue;
598
599                 pb_debug("\tresolved!\n");
600
601                 list_remove(&opt->list);
602                 list_add_tail(&opt->device->boot_options, &opt->list);
603                 talloc_steal(opt->device, opt);
604                 boot_option_finalise(handler, opt);
605                 notify_boot_option(handler, opt);
606         }
607 }
608
609 struct discover_context *device_handler_discover_context_create(
610                 struct device_handler *handler,
611                 struct discover_device *device)
612 {
613         struct discover_context *ctx;
614
615         ctx = talloc_zero(handler, struct discover_context);
616         ctx->device = device;
617         list_init(&ctx->boot_options);
618
619         return ctx;
620 }
621
622 /**
623  * context_commit - Commit a temporary discovery context to the handler,
624  * and notify the clients about any new options / devices
625  */
626 void device_handler_discover_context_commit(struct device_handler *handler,
627                 struct discover_context *ctx)
628 {
629         struct discover_device *dev = ctx->device;
630         struct discover_boot_option *opt, *tmp;
631
632         if (!device_lookup_by_id(handler, dev->device->id))
633                 device_handler_add_device(handler, dev);
634
635         /* move boot options from the context to the device */
636         list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
637                 list_remove(&opt->list);
638
639                 if (boot_option_resolve(opt, handler)) {
640                         pb_log("boot option %s is resolved, "
641                                         "sending to clients\n",
642                                         opt->option->id);
643                         list_add_tail(&dev->boot_options, &opt->list);
644                         talloc_steal(dev, opt);
645                         boot_option_finalise(handler, opt);
646                         notify_boot_option(handler, opt);
647                 } else {
648                         if (!opt->source->resolve_resource) {
649                                 pb_log("parser %s gave us an unresolved "
650                                         "resource (%s), but no way to "
651                                         "resolve it\n",
652                                         opt->source->name, opt->option->id);
653                                 talloc_free(opt);
654                         } else {
655                                 pb_log("boot option %s is unresolved, "
656                                                 "adding to queue\n",
657                                                 opt->option->id);
658                                 list_add(&handler->unresolved_boot_options,
659                                                 &opt->list);
660                                 talloc_steal(handler, opt);
661                         }
662                 }
663         }
664 }
665
666 void device_handler_add_device(struct device_handler *handler,
667                 struct discover_device *device)
668 {
669         handler->n_devices++;
670         handler->devices = talloc_realloc(handler, handler->devices,
671                                 struct discover_device *, handler->n_devices);
672         handler->devices[handler->n_devices - 1] = device;
673
674         if (device->device->type == DEVICE_TYPE_NETWORK)
675                 network_register_device(handler->network, device);
676 }
677
678 /* Start discovery on a hotplugged device. The device will be in our devices
679  * array, but has only just been initialised by the hotplug source.
680  */
681 int device_handler_discover(struct device_handler *handler,
682                 struct discover_device *dev)
683 {
684         struct discover_context *ctx;
685         int rc;
686
687         process_boot_option_queue(handler);
688
689         /* create our context */
690         ctx = device_handler_discover_context_create(handler, dev);
691
692         rc = mount_device(dev);
693         if (rc)
694                 goto out;
695
696         /* add this device to our system info */
697         system_info_register_blockdev(dev->device->id, dev->uuid,
698                         dev->mount_path);
699
700         /* run the parsers. This will populate the ctx's boot_option list. */
701         iterate_parsers(ctx);
702
703         /* add discovered stuff to the handler */
704         device_handler_discover_context_commit(handler, ctx);
705
706 out:
707         talloc_free(ctx);
708
709         return 0;
710 }
711
712 /* Incoming dhcp event */
713 int device_handler_dhcp(struct device_handler *handler,
714                 struct discover_device *dev, struct event *event)
715 {
716         struct discover_context *ctx;
717
718         /* create our context */
719         ctx = device_handler_discover_context_create(handler, dev);
720         ctx->event = event;
721
722         iterate_parsers(ctx);
723
724         device_handler_discover_context_commit(handler, ctx);
725
726         talloc_free(ctx);
727
728         return 0;
729 }
730
731 /* incoming conf event */
732 int device_handler_conf(struct device_handler *handler,
733                 struct discover_device *dev, struct pb_url *url)
734 {
735         struct discover_context *ctx;
736
737         /* create our context */
738         ctx = device_handler_discover_context_create(handler, dev);
739         ctx->conf_url = url;
740
741         iterate_parsers(ctx);
742
743         device_handler_discover_context_commit(handler, ctx);
744
745         talloc_free(ctx);
746
747         return 0;
748 }
749
750 static struct discover_boot_option *find_boot_option_by_id(
751                 struct device_handler *handler, const char *id)
752 {
753         unsigned int i;
754
755         for (i = 0; i < handler->n_devices; i++) {
756                 struct discover_device *dev = handler->devices[i];
757                 struct discover_boot_option *opt;
758
759                 list_for_each_entry(&dev->boot_options, opt, list)
760                         if (!strcmp(opt->option->id, id))
761                                 return opt;
762         }
763
764         return NULL;
765 }
766
767 void device_handler_boot(struct device_handler *handler,
768                 struct boot_command *cmd)
769 {
770         struct discover_boot_option *opt = NULL;
771
772         if (cmd->option_id && strlen(cmd->option_id))
773                 opt = find_boot_option_by_id(handler, cmd->option_id);
774
775         if (handler->pending_boot)
776                 boot_cancel(handler->pending_boot);
777         handler->pending_boot = boot(handler, opt, cmd, handler->dry_run,
778                         boot_status, handler);
779         handler->pending_boot_is_default = false;
780 }
781
782 void device_handler_cancel_default(struct device_handler *handler)
783 {
784         struct boot_status status;
785
786         if (handler->timeout_waiter)
787                 waiter_remove(handler->timeout_waiter);
788
789         handler->timeout_waiter = NULL;
790         handler->autoboot_enabled = false;
791
792         /* we only send status if we had a default boot option queued */
793         if (!handler->default_boot_option)
794                 return;
795
796         pb_log("Cancelling default boot option\n");
797
798         if (handler->pending_boot && handler->pending_boot_is_default) {
799                 boot_cancel(handler->pending_boot);
800                 handler->pending_boot = NULL;
801                 handler->pending_boot_is_default = false;
802         }
803
804         handler->default_boot_option = NULL;
805
806         status.type = BOOT_STATUS_INFO;
807         status.progress = -1;
808         status.detail = NULL;
809         status.message = "Default boot cancelled";
810
811         discover_server_notify_boot_status(handler->server, &status);
812 }
813
814 void device_handler_update_config(struct device_handler *handler,
815                 struct config *config)
816 {
817         config_set(config);
818         discover_server_notify_config(handler->server, config);
819         device_handler_reinit(handler);
820 }
821
822 #ifndef PETITBOOT_TEST
823
824 static int device_handler_init_sources(struct device_handler *handler)
825 {
826         /* init our device sources: udev, network and user events */
827         handler->udev = udev_init(handler, handler->waitset);
828         if (!handler->udev)
829                 return -1;
830
831         handler->network = network_init(handler, handler->waitset,
832                         handler->dry_run);
833         if (!handler->network)
834                 return -1;
835
836         handler->user_event = user_event_init(handler, handler->waitset);
837         if (!handler->user_event)
838                 return -1;
839
840         return 0;
841 }
842
843 static void device_handler_reinit_sources(struct device_handler *handler)
844 {
845         udev_reinit(handler->udev);
846
847         network_shutdown(handler->network);
848         handler->network = network_init(handler, handler->waitset,
849                         handler->dry_run);
850 }
851
852 static bool check_existing_mount(struct discover_device *dev)
853 {
854         struct stat devstat, mntstat;
855         struct mntent *mnt;
856         FILE *fp;
857         int rc;
858
859         rc = stat(dev->device_path, &devstat);
860         if (rc) {
861                 pb_debug("%s: stat failed: %s\n", __func__, strerror(errno));
862                 return false;
863         }
864
865         if (!S_ISBLK(devstat.st_mode)) {
866                 pb_debug("%s: %s isn't a block device?\n", __func__,
867                                 dev->device_path);
868                 return false;
869         }
870
871         fp = fopen("/proc/self/mounts", "r");
872
873         for (;;) {
874                 mnt = getmntent(fp);
875                 if (!mnt)
876                         break;
877
878                 if (!mnt->mnt_fsname || mnt->mnt_fsname[0] != '/')
879                         continue;
880
881                 rc = stat(mnt->mnt_fsname, &mntstat);
882                 if (rc)
883                         continue;
884
885                 if (!S_ISBLK(mntstat.st_mode))
886                         continue;
887
888                 if (mntstat.st_rdev == devstat.st_rdev) {
889                         dev->mount_path = talloc_strdup(dev, mnt->mnt_dir);
890                         dev->mounted_rw = !!hasmntopt(mnt, "rw");
891                         dev->mounted = true;
892                         dev->unmount = false;
893
894                         pb_debug("%s: %s is already mounted (r%c) at %s\n",
895                                         __func__, dev->device_path,
896                                         dev->mounted_rw ? 'w' : 'o',
897                                         mnt->mnt_dir);
898                         break;
899                 }
900         }
901
902         fclose(fp);
903
904         return mnt != NULL;
905 }
906
907 static int mount_device(struct discover_device *dev)
908 {
909         const char *fstype;
910         int rc;
911
912         if (!dev->device_path)
913                 return -1;
914
915         if (dev->mounted)
916                 return 0;
917
918         if (check_existing_mount(dev))
919                 return 0;
920
921         fstype = discover_device_get_param(dev, "ID_FS_TYPE");
922         if (!fstype)
923                 return 0;
924
925         dev->mount_path = join_paths(dev, mount_base(),
926                                         dev->device_path);
927
928         if (pb_mkdir_recursive(dev->mount_path)) {
929                 pb_log("couldn't create mount directory %s: %s\n",
930                                 dev->mount_path, strerror(errno));
931                 goto err_free;
932         }
933
934         pb_log("mounting device %s read-only\n", dev->device_path);
935         errno = 0;
936         rc = mount(dev->device_path, dev->mount_path, fstype,
937                         MS_RDONLY | MS_SILENT, "");
938         if (!rc) {
939                 dev->mounted = true;
940                 dev->mounted_rw = false;
941                 dev->unmount = true;
942                 return 0;
943         }
944
945         pb_log("couldn't mount device %s: mount failed: %s\n",
946                         dev->device_path, strerror(errno));
947
948         pb_rmdir_recursive(mount_base(), dev->mount_path);
949 err_free:
950         talloc_free(dev->mount_path);
951         dev->mount_path = NULL;
952         return -1;
953 }
954
955 static int umount_device(struct discover_device *dev)
956 {
957         int rc;
958
959         if (!dev->mounted || !dev->unmount)
960                 return 0;
961
962         pb_log("unmounting device %s\n", dev->device_path);
963         rc = umount(dev->mount_path);
964         if (rc)
965                 return -1;
966
967         dev->mounted = false;
968
969         pb_rmdir_recursive(mount_base(), dev->mount_path);
970
971         talloc_free(dev->mount_path);
972         dev->mount_path = NULL;
973
974         return 0;
975 }
976
977 int device_request_write(struct discover_device *dev, bool *release)
978 {
979         int rc;
980
981         *release = false;
982
983         if (!dev->mounted)
984                 return -1;
985
986         if (dev->mounted_rw)
987                 return 0;
988
989         pb_log("remounting device %s read-write\n", dev->device_path);
990         rc = mount(dev->device_path, dev->mount_path, "",
991                         MS_REMOUNT | MS_SILENT, "");
992         if (rc)
993                 return -1;
994
995         dev->mounted_rw = true;
996         *release = true;
997         return 0;
998 }
999
1000 void device_release_write(struct discover_device *dev, bool release)
1001 {
1002         if (!release)
1003                 return;
1004
1005         pb_log("remounting device %s read-only\n", dev->device_path);
1006         mount(dev->device_path, dev->mount_path, "",
1007                         MS_REMOUNT | MS_RDONLY | MS_SILENT, "");
1008         dev->mounted_rw = false;
1009 }
1010
1011 #else
1012
1013 static int device_handler_init_sources(
1014                 struct device_handler *handler __attribute__((unused)))
1015 {
1016         return 0;
1017 }
1018
1019 static void device_handler_reinit_sources(
1020                 struct device_handler *handler __attribute__((unused)))
1021 {
1022 }
1023
1024 static int umount_device(struct discover_device *dev __attribute__((unused)))
1025 {
1026         return 0;
1027 }
1028
1029 static int __attribute__((unused)) mount_device(
1030                 struct discover_device *dev __attribute__((unused)))
1031 {
1032         return 0;
1033 }
1034
1035 int device_request_write(struct discover_device *dev __attribute__((unused)),
1036                 bool *release)
1037 {
1038         *release = true;
1039         return 0;
1040 }
1041
1042 void device_release_write(struct discover_device *dev __attribute__((unused)),
1043         bool release __attribute__((unused)))
1044 {
1045 }
1046
1047 #endif
1048