The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [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 | |
| 10 | // FIXME - only one loop mount is supported at a time |
| 11 | #define LOOP_DEVICE "/dev/block/loop0" |
| 12 | |
| 13 | static int is_loop_mount(const char* path) |
| 14 | { |
| 15 | FILE* f; |
| 16 | int count; |
| 17 | char device[256]; |
| 18 | char mount_path[256]; |
| 19 | char rest[256]; |
| 20 | int result = 0; |
| 21 | int path_length = strlen(path); |
| 22 | |
| 23 | f = fopen("/proc/mounts", "r"); |
| 24 | if (!f) { |
| 25 | fprintf(stdout, "could not open /proc/mounts\n"); |
| 26 | return -1; |
| 27 | } |
| 28 | |
| 29 | do { |
| 30 | count = fscanf(f, "%255s %255s %255s\n", device, mount_path, rest); |
| 31 | if (count == 3) { |
| 32 | if (strcmp(LOOP_DEVICE, device) == 0 && strcmp(path, mount_path) == 0) { |
| 33 | result = 1; |
| 34 | break; |
| 35 | } |
| 36 | } |
| 37 | } while (count == 3); |
| 38 | |
| 39 | fclose(f); |
| 40 | return result; |
| 41 | } |
| 42 | |
| 43 | int umount_main(int argc, char *argv[]) |
| 44 | { |
| 45 | int loop, loop_fd; |
| 46 | |
| 47 | if(argc != 2) { |
| 48 | fprintf(stderr,"umount <path>\n"); |
| 49 | return 1; |
| 50 | } |
| 51 | |
| 52 | loop = is_loop_mount(argv[1]); |
| 53 | if(umount(argv[1])){ |
| 54 | fprintf(stderr,"failed.\n"); |
| 55 | return 1; |
| 56 | } |
| 57 | |
| 58 | if (loop) { |
| 59 | // free the loop device |
| 60 | loop_fd = open(LOOP_DEVICE, O_RDONLY); |
| 61 | if (loop_fd < -1) { |
| 62 | perror("open loop device failed"); |
| 63 | return 1; |
| 64 | } |
| 65 | if (ioctl(loop_fd, LOOP_CLR_FD, 0) < 0) { |
| 66 | perror("ioctl LOOP_CLR_FD failed"); |
| 67 | return 1; |
| 68 | } |
| 69 | |
| 70 | close(loop_fd); |
| 71 | } |
| 72 | |
| 73 | return 0; |
| 74 | } |