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