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