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