+static void check_filesystem(struct blkid_struct_probe *pr)
+{
+ pid_t pid;
+ struct stat statbuf;
+ char *e2fsck = "/usr/sbin/e2fsck";
+
+ if (strncmp(pr->id->name, "ext", 3)) {
+ fprintf(stderr, "check_filesystem: %s is not supported\n", pr->id->name);
+ return;
+ }
+
+ if (stat(e2fsck, &statbuf) < 0) {
+ fprintf(stderr, "check_filesystem: %s not found\n", e2fsck);
+ return;
+ }
+
+ pid = fork();
+ if (!pid) {
+ execl(e2fsck, e2fsck, "-p", pr->dev, NULL);
+ exit(-1);
+ } else if (pid > 0) {
+ int status;
+
+ waitpid(pid, &status, 0);
+ if (WEXITSTATUS(status))
+ fprintf(stderr, "check_filesystem: %s returned %d\n", e2fsck, WEXITSTATUS(status));
+ }
+}
+
+static int mount_device(struct blkid_struct_probe *pr, int hotplug)
+{
+ struct mount *m;
+ char *device;
+
+ if (!pr)
+ return -1;
+
+ device = basename(pr->dev);
+
+ if (!strcmp(pr->id->name, "swap")) {
+ if (hotplug && !auto_swap)
+ return -1;
+ m = find_swap(pr->uuid, device);
+ if (m || anon_swap)
+ swapon(pr->dev, (m) ? (m->prio) : (0));
+
+ return 0;
+ }
+
+ if (hotplug && !auto_mount)
+ return -1;
+
+ if (find_mount_point(pr->dev)) {
+ fprintf(stderr, "%s is already mounted\n", pr->dev);
+ return -1;
+ }
+
+ m = find_block(pr->uuid, pr->label, device, NULL);
+ if (m && m->extroot)
+ return -1;
+
+ if (m) {
+ char *target = m->target;
+ char _target[32];
+ int err = 0;
+
+ if (!target) {
+ snprintf(_target, sizeof(_target), "/mnt/%s", device);
+ target = _target;
+ }
+ mkdir_p(target);
+
+ if (check_fs)
+ check_filesystem(pr);
+
+ err = mount(pr->dev, target, pr->id->name, 0, (m->options) ? (m->options) : (""));
+ if (err)
+ fprintf(stderr, "mounting %s (%s) as %s failed (%d) - %s\n",
+ pr->dev, pr->id->name, target, err, strerror(err));
+ return err;
+ }
+
+ if (anon_mount) {
+ char target[] = "/mnt/mmcblk123";
+ int err = 0;
+
+ snprintf(target, sizeof(target), "/mnt/%s", device);
+ mkdir_p(target);
+
+ if (check_fs)
+ check_filesystem(pr);
+
+ err = mount(pr->dev, target, pr->id->name, 0, "");
+ if (err)
+ fprintf(stderr, "mounting %s (%s) as %s failed (%d) - %s\n",
+ pr->dev, pr->id->name, target, err, strerror(err));
+ return err;
+ }
+
+ return 0;
+}
+
+static int umount_device(struct blkid_struct_probe *pr)