The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | |
| 2 | #include <sys/mount.h> |
| 3 | #include <sys/stat.h> |
| 4 | #include <fcntl.h> |
| 5 | #include <stdio.h> |
| 6 | #include <string.h> |
| 7 | #include <unistd.h> |
| 8 | #include <linux/loop.h> |
Jeff Brown | baf6b6b | 2011-07-13 22:12:18 -0700 | [diff] [blame] | 9 | #include <errno.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 10 | |
Ken Sumrall | 940c810 | 2011-07-12 19:47:06 -0700 | [diff] [blame] | 11 | #define LOOPDEV_MAXLEN 64 |
| 12 | #define LOOP_MAJOR 7 |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 13 | |
Ken Sumrall | 940c810 | 2011-07-12 19:47:06 -0700 | [diff] [blame] | 14 | static int is_loop(char *dev) |
| 15 | { |
| 16 | struct stat st; |
| 17 | int ret = 0; |
| 18 | |
| 19 | if (stat(dev, &st) == 0) { |
| 20 | if (S_ISBLK(st.st_mode) && (major(st.st_rdev) == LOOP_MAJOR)) { |
| 21 | ret = 1; |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | return ret; |
| 26 | } |
| 27 | |
| 28 | static int is_loop_mount(const char* path, char *loopdev) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 29 | { |
| 30 | FILE* f; |
| 31 | int count; |
| 32 | char device[256]; |
| 33 | char mount_path[256]; |
| 34 | char rest[256]; |
| 35 | int result = 0; |
| 36 | int path_length = strlen(path); |
| 37 | |
| 38 | f = fopen("/proc/mounts", "r"); |
| 39 | if (!f) { |
Jeff Brown | baf6b6b | 2011-07-13 22:12:18 -0700 | [diff] [blame] | 40 | fprintf(stdout, "could not open /proc/mounts: %s\n", strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 41 | return -1; |
| 42 | } |
| 43 | |
| 44 | do { |
| 45 | count = fscanf(f, "%255s %255s %255s\n", device, mount_path, rest); |
| 46 | if (count == 3) { |
Ken Sumrall | 940c810 | 2011-07-12 19:47:06 -0700 | [diff] [blame] | 47 | if (is_loop(device) && strcmp(path, mount_path) == 0) { |
| 48 | strlcpy(loopdev, device, LOOPDEV_MAXLEN); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 49 | result = 1; |
| 50 | break; |
| 51 | } |
| 52 | } |
| 53 | } while (count == 3); |
| 54 | |
| 55 | fclose(f); |
| 56 | return result; |
| 57 | } |
| 58 | |
| 59 | int umount_main(int argc, char *argv[]) |
| 60 | { |
| 61 | int loop, loop_fd; |
Ken Sumrall | 940c810 | 2011-07-12 19:47:06 -0700 | [diff] [blame] | 62 | char loopdev[LOOPDEV_MAXLEN]; |
| 63 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 64 | if(argc != 2) { |
| 65 | fprintf(stderr,"umount <path>\n"); |
| 66 | return 1; |
| 67 | } |
| 68 | |
Ken Sumrall | 940c810 | 2011-07-12 19:47:06 -0700 | [diff] [blame] | 69 | loop = is_loop_mount(argv[1], loopdev); |
Jeff Brown | baf6b6b | 2011-07-13 22:12:18 -0700 | [diff] [blame] | 70 | if (umount(argv[1])) { |
| 71 | fprintf(stderr, "failed: %s\n", strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | if (loop) { |
| 76 | // free the loop device |
Ken Sumrall | 940c810 | 2011-07-12 19:47:06 -0700 | [diff] [blame] | 77 | loop_fd = open(loopdev, O_RDONLY); |
| 78 | if (loop_fd < 0) { |
Jeff Brown | baf6b6b | 2011-07-13 22:12:18 -0700 | [diff] [blame] | 79 | fprintf(stderr, "open loop device failed: %s\n", strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 80 | return 1; |
| 81 | } |
| 82 | if (ioctl(loop_fd, LOOP_CLR_FD, 0) < 0) { |
Jeff Brown | baf6b6b | 2011-07-13 22:12:18 -0700 | [diff] [blame] | 83 | fprintf(stderr, "ioctl LOOP_CLR_FD failed: %s\n", strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 84 | return 1; |
| 85 | } |
| 86 | |
| 87 | close(loop_fd); |
| 88 | } |
| 89 | |
| 90 | return 0; |
| 91 | } |