discover: Add DEVICE_TYPE_ANY for matching any device
[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 <talloc/talloc.h>
12 #include <list/list.h>
13 #include <log/log.h>
14 #include <types/types.h>
15 #include <system/system.h>
16 #include <process/process.h>
17 #include <url/url.h>
18
19 #include "device-handler.h"
20 #include "discover-server.h"
21 #include "platform.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         struct discover_boot_option *opt, *tmp;
283         unsigned int i;
284
285         for (i = 0; i < handler->n_devices; i++)
286                 if (handler->devices[i] == device)
287                         break;
288
289         if (i == handler->n_devices) {
290                 talloc_free(device);
291                 return;
292         }
293
294         /* Free any unresolved options, as they're currently allocated
295          * against the handler */
296         list_for_each_entry_safe(&handler->unresolved_boot_options,
297                         opt, tmp, list) {
298                 if (opt->device != device)
299                         continue;
300                 list_remove(&opt->list);
301                 talloc_free(opt);
302         }
303
304         handler->n_devices--;
305         memmove(&handler->devices[i], &handler->devices[i + 1],
306                 (handler->n_devices - i) * sizeof(handler->devices[0]));
307         handler->devices = talloc_realloc(handler, handler->devices,
308                 struct discover_device *, handler->n_devices);
309
310         if (device->notified)
311                 discover_server_notify_device_remove(handler->server,
312                                                         device->device);
313
314         talloc_free(device);
315 }
316
317 static void boot_status(void *arg, struct boot_status *status)
318 {
319         struct device_handler *handler = arg;
320
321         discover_server_notify_boot_status(handler->server, status);
322 }
323
324 static void countdown_status(struct device_handler *handler,
325                 struct discover_boot_option *opt, unsigned int sec)
326 {
327         struct boot_status status;
328
329         status.type = BOOT_STATUS_INFO;
330         status.progress = -1;
331         status.detail = NULL;
332         status.message = talloc_asprintf(handler,
333                         "Booting %s in %u sec", opt->option->name, sec);
334
335         discover_server_notify_boot_status(handler->server, &status);
336
337         talloc_free(status.message);
338 }
339
340 static int default_timeout(void *arg)
341 {
342         struct device_handler *handler = arg;
343         struct discover_boot_option *opt;
344
345         if (!handler->default_boot_option)
346                 return 0;
347
348         if (handler->pending_boot)
349                 return 0;
350
351         opt = handler->default_boot_option;
352
353         if (handler->sec_to_boot) {
354                 countdown_status(handler, opt, handler->sec_to_boot);
355                 handler->sec_to_boot--;
356                 handler->timeout_waiter = waiter_register_timeout(
357                                                 handler->waitset, 1000,
358                                                 default_timeout, handler);
359                 return 0;
360         }
361
362         handler->timeout_waiter = NULL;
363
364         pb_log("Timeout expired, booting default option %s\n", opt->option->id);
365
366         handler->pending_boot = boot(handler, handler->default_boot_option,
367                         NULL, handler->dry_run, boot_status, handler);
368         handler->pending_boot_is_default = true;
369         return 0;
370 }
371
372 static bool priority_match(struct boot_priority *prio,
373                 struct discover_boot_option *opt)
374 {
375         return prio->type == opt->device->device->type ||
376                 prio->type == DEVICE_TYPE_ANY;
377 }
378
379 static int default_option_priority(struct discover_boot_option *opt)
380 {
381         const struct config *config;
382         struct boot_priority *prio;
383         unsigned int i;
384
385         config = config_get();
386
387         for (i = 0; i < config->n_boot_priorities; i++) {
388                 prio = &config->boot_priorities[i];
389                 if (priority_match(prio, opt))
390                         return prio->priority;
391         }
392
393         return 0;
394 }
395
396 static void set_default(struct device_handler *handler,
397                 struct discover_boot_option *opt)
398 {
399         int new_prio;
400
401         if (!handler->autoboot_enabled)
402                 return;
403
404         new_prio = default_option_priority(opt);
405
406         /* A negative priority indicates that we don't want to boot this device
407          * by default */
408         if (new_prio < 0)
409                 return;
410
411         /* Resolve any conflicts: if we have a new default option, it only
412          * replaces the current if it has a higher priority. */
413         if (handler->default_boot_option) {
414                 int cur_prio;
415
416                 cur_prio = default_option_priority(
417                                         handler->default_boot_option);
418
419                 if (new_prio > cur_prio) {
420                         handler->default_boot_option = opt;
421                         /* extend the timeout a little, so the user sees some
422                          * indication of the change */
423                         handler->sec_to_boot += 2;
424                 }
425
426                 return;
427         }
428
429         handler->sec_to_boot = config_get()->autoboot_timeout_sec;
430         handler->default_boot_option = opt;
431
432         pb_log("Boot option %s set as default, timeout %u sec.\n",
433                opt->option->id, handler->sec_to_boot);
434
435         default_timeout(handler);
436 }
437
438 static bool resource_is_resolved(struct resource *res)
439 {
440         return !res || res->resolved;
441 }
442
443 /* We only use this in an assert, which will disappear if we're compiling
444  * with NDEBUG, so we need the 'used' attribute for these builds */
445 static bool __attribute__((used)) boot_option_is_resolved(
446                 struct discover_boot_option *opt)
447 {
448         return resource_is_resolved(opt->boot_image) &&
449                 resource_is_resolved(opt->initrd) &&
450                 resource_is_resolved(opt->dtb) &&
451                 resource_is_resolved(opt->icon);
452 }
453
454 static bool resource_resolve(struct resource *res, const char *name,
455                 struct discover_boot_option *opt,
456                 struct device_handler *handler)
457 {
458         struct parser *parser = opt->source;
459
460         if (resource_is_resolved(res))
461                 return true;
462
463         pb_debug("Attempting to resolve resource %s->%s with parser %s\n",
464                         opt->option->id, name, parser->name);
465         parser->resolve_resource(handler, res);
466
467         return res->resolved;
468 }
469
470 static bool boot_option_resolve(struct discover_boot_option *opt,
471                 struct device_handler *handler)
472 {
473         return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
474                 resource_resolve(opt->initrd, "initrd", opt, handler) &&
475                 resource_resolve(opt->dtb, "dtb", opt, handler) &&
476                 resource_resolve(opt->icon, "icon", opt, handler);
477 }
478
479 static void boot_option_finalise(struct device_handler *handler,
480                 struct discover_boot_option *opt)
481 {
482         assert(boot_option_is_resolved(opt));
483
484         /* check that the parsers haven't set any of the final data */
485         assert(!opt->option->boot_image_file);
486         assert(!opt->option->initrd_file);
487         assert(!opt->option->dtb_file);
488         assert(!opt->option->icon_file);
489         assert(!opt->option->device_id);
490
491         if (opt->boot_image)
492                 opt->option->boot_image_file = opt->boot_image->url->full;
493         if (opt->initrd)
494                 opt->option->initrd_file = opt->initrd->url->full;
495         if (opt->dtb)
496                 opt->option->dtb_file = opt->dtb->url->full;
497         if (opt->icon)
498                 opt->option->icon_file = opt->icon->url->full;
499
500         opt->option->device_id = opt->device->device->id;
501
502         if (opt->option->is_default)
503                 set_default(handler, opt);
504 }
505
506 static void notify_boot_option(struct device_handler *handler,
507                 struct discover_boot_option *opt)
508 {
509         struct discover_device *dev = opt->device;
510
511         if (!dev->notified)
512                 discover_server_notify_device_add(handler->server,
513                                                   opt->device->device);
514         dev->notified = true;
515         discover_server_notify_boot_option_add(handler->server, opt->option);
516 }
517
518 static void process_boot_option_queue(struct device_handler *handler)
519 {
520         struct discover_boot_option *opt, *tmp;
521
522         list_for_each_entry_safe(&handler->unresolved_boot_options,
523                         opt, tmp, list) {
524
525                 pb_debug("queue: attempting resolution for %s\n",
526                                 opt->option->id);
527
528                 if (!boot_option_resolve(opt, handler))
529                         continue;
530
531                 pb_debug("\tresolved!\n");
532
533                 list_remove(&opt->list);
534                 list_add_tail(&opt->device->boot_options, &opt->list);
535                 talloc_steal(opt->device, opt);
536                 boot_option_finalise(handler, opt);
537                 notify_boot_option(handler, opt);
538         }
539 }
540
541 struct discover_context *device_handler_discover_context_create(
542                 struct device_handler *handler,
543                 struct discover_device *device)
544 {
545         struct discover_context *ctx;
546
547         ctx = talloc_zero(handler, struct discover_context);
548         ctx->device = device;
549         list_init(&ctx->boot_options);
550
551         return ctx;
552 }
553
554 /**
555  * context_commit - Commit a temporary discovery context to the handler,
556  * and notify the clients about any new options / devices
557  */
558 void device_handler_discover_context_commit(struct device_handler *handler,
559                 struct discover_context *ctx)
560 {
561         struct discover_device *dev = ctx->device;
562         struct discover_boot_option *opt, *tmp;
563
564         if (!device_lookup_by_id(handler, dev->device->id))
565                 device_handler_add_device(handler, dev);
566
567         /* move boot options from the context to the device */
568         list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
569                 list_remove(&opt->list);
570
571                 if (boot_option_resolve(opt, handler)) {
572                         pb_log("boot option %s is resolved, "
573                                         "sending to clients\n",
574                                         opt->option->id);
575                         list_add_tail(&dev->boot_options, &opt->list);
576                         talloc_steal(dev, opt);
577                         boot_option_finalise(handler, opt);
578                         notify_boot_option(handler, opt);
579                 } else {
580                         if (!opt->source->resolve_resource) {
581                                 pb_log("parser %s gave us an unresolved "
582                                         "resource (%s), but no way to "
583                                         "resolve it\n",
584                                         opt->source->name, opt->option->id);
585                                 talloc_free(opt);
586                         } else {
587                                 pb_log("boot option %s is unresolved, "
588                                                 "adding to queue\n",
589                                                 opt->option->id);
590                                 list_add(&handler->unresolved_boot_options,
591                                                 &opt->list);
592                                 talloc_steal(handler, opt);
593                         }
594                 }
595         }
596 }
597
598 void device_handler_add_device(struct device_handler *handler,
599                 struct discover_device *device)
600 {
601         handler->n_devices++;
602         handler->devices = talloc_realloc(handler, handler->devices,
603                                 struct discover_device *, handler->n_devices);
604         handler->devices[handler->n_devices - 1] = device;
605
606 }
607
608 /* Start discovery on a hotplugged device. The device will be in our devices
609  * array, but has only just been initialised by the hotplug source.
610  */
611 int device_handler_discover(struct device_handler *handler,
612                 struct discover_device *dev)
613 {
614         struct discover_context *ctx;
615         int rc;
616
617         process_boot_option_queue(handler);
618
619         /* create our context */
620         ctx = device_handler_discover_context_create(handler, dev);
621
622         rc = mount_device(dev);
623         if (rc)
624                 goto out;
625
626         /* add this device to our system info */
627         system_info_register_blockdev(dev->device->id, dev->uuid,
628                         dev->mount_path);
629
630         /* run the parsers. This will populate the ctx's boot_option list. */
631         iterate_parsers(ctx);
632
633         /* add discovered stuff to the handler */
634         device_handler_discover_context_commit(handler, ctx);
635
636 out:
637         talloc_free(ctx);
638
639         return 0;
640 }
641
642 /* Incoming dhcp event */
643 int device_handler_dhcp(struct device_handler *handler,
644                 struct discover_device *dev, struct event *event)
645 {
646         struct discover_context *ctx;
647
648         /* create our context */
649         ctx = device_handler_discover_context_create(handler, dev);
650         ctx->event = event;
651
652         iterate_parsers(ctx);
653
654         device_handler_discover_context_commit(handler, ctx);
655
656         talloc_free(ctx);
657
658         return 0;
659 }
660
661 /* incoming conf event */
662 int device_handler_conf(struct device_handler *handler,
663                 struct discover_device *dev, struct pb_url *url)
664 {
665         struct discover_context *ctx;
666
667         /* create our context */
668         ctx = device_handler_discover_context_create(handler, dev);
669         ctx->conf_url = url;
670
671         iterate_parsers(ctx);
672
673         device_handler_discover_context_commit(handler, ctx);
674
675         talloc_free(ctx);
676
677         return 0;
678 }
679
680 static struct discover_boot_option *find_boot_option_by_id(
681                 struct device_handler *handler, const char *id)
682 {
683         unsigned int i;
684
685         for (i = 0; i < handler->n_devices; i++) {
686                 struct discover_device *dev = handler->devices[i];
687                 struct discover_boot_option *opt;
688
689                 list_for_each_entry(&dev->boot_options, opt, list)
690                         if (!strcmp(opt->option->id, id))
691                                 return opt;
692         }
693
694         return NULL;
695 }
696
697 void device_handler_boot(struct device_handler *handler,
698                 struct boot_command *cmd)
699 {
700         struct discover_boot_option *opt = NULL;
701
702         if (cmd->option_id && strlen(cmd->option_id))
703                 opt = find_boot_option_by_id(handler, cmd->option_id);
704
705         if (handler->pending_boot)
706                 boot_cancel(handler->pending_boot);
707         handler->pending_boot = boot(handler, opt, cmd, handler->dry_run,
708                         boot_status, handler);
709         handler->pending_boot_is_default = false;
710 }
711
712 void device_handler_cancel_default(struct device_handler *handler)
713 {
714         struct boot_status status;
715
716         if (handler->timeout_waiter)
717                 waiter_remove(handler->timeout_waiter);
718
719         handler->timeout_waiter = NULL;
720         handler->autoboot_enabled = false;
721
722         /* we only send status if we had a default boot option queued */
723         if (!handler->default_boot_option)
724                 return;
725
726         pb_log("Cancelling default boot option\n");
727
728         if (handler->pending_boot && handler->pending_boot_is_default) {
729                 boot_cancel(handler->pending_boot);
730                 handler->pending_boot = NULL;
731                 handler->pending_boot_is_default = false;
732         }
733
734         handler->default_boot_option = NULL;
735
736         status.type = BOOT_STATUS_INFO;
737         status.progress = -1;
738         status.detail = NULL;
739         status.message = "Default boot cancelled";
740
741         discover_server_notify_boot_status(handler->server, &status);
742 }
743
744 void device_handler_update_config(struct device_handler *handler,
745                 struct config *config)
746 {
747         config_set(config);
748         discover_server_notify_config(handler->server, config);
749 }
750
751 #ifndef PETITBOOT_TEST
752 static bool check_existing_mount(struct discover_device *dev)
753 {
754         struct stat devstat, mntstat;
755         struct mntent *mnt;
756         FILE *fp;
757         int rc;
758
759         rc = stat(dev->device_path, &devstat);
760         if (rc) {
761                 pb_debug("%s: stat failed: %s\n", __func__, strerror(errno));
762                 return false;
763         }
764
765         if (!S_ISBLK(devstat.st_mode)) {
766                 pb_debug("%s: %s isn't a block device?\n", __func__,
767                                 dev->device_path);
768                 return false;
769         }
770
771         fp = fopen("/proc/self/mounts", "r");
772
773         for (;;) {
774                 mnt = getmntent(fp);
775                 if (!mnt)
776                         break;
777
778                 if (!mnt->mnt_fsname || mnt->mnt_fsname[0] != '/')
779                         continue;
780
781                 rc = stat(mnt->mnt_fsname, &mntstat);
782                 if (rc)
783                         continue;
784
785                 if (!S_ISBLK(mntstat.st_mode))
786                         continue;
787
788                 if (mntstat.st_rdev == devstat.st_rdev) {
789                         dev->mount_path = talloc_strdup(dev, mnt->mnt_dir);
790                         dev->mounted_rw = !!hasmntopt(mnt, "rw");
791                         dev->mounted = true;
792                         dev->unmount = false;
793
794                         pb_debug("%s: %s is already mounted (r%c) at %s\n",
795                                         __func__, dev->device_path,
796                                         dev->mounted_rw ? 'w' : 'o',
797                                         mnt->mnt_dir);
798                         break;
799                 }
800         }
801
802         fclose(fp);
803
804         return mnt != NULL;
805 }
806
807 static int mount_device(struct discover_device *dev)
808 {
809         int rc;
810
811         if (!dev->device_path)
812                 return -1;
813
814         if (dev->mounted)
815                 return 0;
816
817         if (check_existing_mount(dev))
818                 return 0;
819
820         dev->mount_path = join_paths(dev, mount_base(),
821                                         dev->device_path);
822
823         if (pb_mkdir_recursive(dev->mount_path)) {
824                 pb_log("couldn't create mount directory %s: %s\n",
825                                 dev->mount_path, strerror(errno));
826                 goto err_free;
827         }
828
829         rc = process_run_simple(dev, pb_system_apps.mount,
830                         dev->device_path, dev->mount_path,
831                         "-o", "ro", NULL);
832         if (!rc) {
833                 dev->mounted = true;
834                 dev->mounted_rw = false;
835                 dev->unmount = true;
836                 return 0;
837         }
838
839         /* Retry mount without ro option. */
840         rc = process_run_simple(dev, pb_system_apps.mount,
841                         dev->device_path, dev->mount_path, NULL);
842
843         if (!rc) {
844                 dev->mounted = true;
845                 dev->mounted_rw = true;
846                 dev->unmount = true;
847                 return 0;
848         }
849
850         pb_rmdir_recursive(mount_base(), dev->mount_path);
851 err_free:
852         talloc_free(dev->mount_path);
853         dev->mount_path = NULL;
854         return -1;
855 }
856
857 static int umount_device(struct discover_device *dev)
858 {
859         int status;
860
861         if (!dev->mounted || !dev->unmount)
862                 return 0;
863
864         status = process_run_simple(dev, pb_system_apps.umount,
865                         dev->mount_path, NULL);
866
867         if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
868                 return -1;
869
870         dev->mounted = false;
871
872         pb_rmdir_recursive(mount_base(), dev->mount_path);
873
874         talloc_free(dev->mount_path);
875         dev->mount_path = NULL;
876
877         return 0;
878 }
879
880 int device_request_write(struct discover_device *dev, bool *release)
881 {
882         int rc;
883
884         *release = false;
885
886         if (!dev->mounted)
887                 return -1;
888
889         if (dev->mounted_rw)
890                 return 0;
891
892         rc = process_run_simple(dev, pb_system_apps.mount, dev->mount_path,
893                         "-o", "remount,rw", NULL);
894         if (rc)
895                 return -1;
896
897         dev->mounted_rw = true;
898         *release = true;
899         return 0;
900 }
901
902 void device_release_write(struct discover_device *dev, bool release)
903 {
904         if (!release)
905                 return;
906
907         process_run_simple(dev, pb_system_apps.mount, dev->mount_path,
908                         "-o", "remount,ro", NULL);
909         dev->mounted_rw = false;
910 }
911
912 #else
913
914 static int umount_device(struct discover_device *dev __attribute__((unused)))
915 {
916         return 0;
917 }
918
919 static int __attribute__((unused)) mount_device(
920                 struct discover_device *dev __attribute__((unused)))
921 {
922         return 0;
923 }
924
925 int device_request_write(struct discover_device *dev __attribute__((unused)),
926                 bool *release)
927 {
928         *release = true;
929         return 0;
930 }
931
932 void device_release_write(struct discover_device *dev __attribute__((unused)),
933         bool release __attribute__((unused)))
934 {
935 }
936
937 #endif
938