| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * mount.c, by rmk | 
|  | 3 | */ | 
|  | 4 |  | 
|  | 5 | #include <sys/mount.h> | 
|  | 6 | #include <sys/stat.h> | 
|  | 7 | #include <fcntl.h> | 
|  | 8 | #include <errno.h> | 
|  | 9 | #include <stdio.h> | 
|  | 10 | #include <stdlib.h> | 
|  | 11 | #include <string.h> | 
|  | 12 | #include <unistd.h> | 
|  | 13 |  | 
|  | 14 | #include <linux/loop.h> | 
|  | 15 |  | 
|  | 16 | #define ARRAY_SIZE(x)	(sizeof(x) / sizeof(x[0])) | 
|  | 17 |  | 
| Ken Sumrall | 940c810 | 2011-07-12 19:47:06 -0700 | [diff] [blame] | 18 | #define DEFAULT_LOOP_DEVICE "/dev/block/loop0" | 
|  | 19 | #define LOOPDEV_MAXLEN 64 | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 20 |  | 
|  | 21 | struct mount_opts { | 
|  | 22 | const char str[8]; | 
|  | 23 | unsigned long rwmask; | 
|  | 24 | unsigned long rwset; | 
|  | 25 | unsigned long rwnoset; | 
|  | 26 | }; | 
|  | 27 |  | 
|  | 28 | struct extra_opts { | 
|  | 29 | char *str; | 
|  | 30 | char *end; | 
|  | 31 | int used_size; | 
|  | 32 | int alloc_size; | 
|  | 33 | }; | 
|  | 34 |  | 
|  | 35 | /* | 
|  | 36 | * These options define the function of "mount(2)". | 
|  | 37 | */ | 
|  | 38 | #define MS_TYPE	(MS_REMOUNT|MS_BIND|MS_MOVE) | 
|  | 39 |  | 
|  | 40 |  | 
|  | 41 | static const struct mount_opts options[] = { | 
|  | 42 | /* name		mask		set		noset		*/ | 
|  | 43 | { "async",	MS_SYNCHRONOUS,	0,		MS_SYNCHRONOUS	}, | 
|  | 44 | { "atime",	MS_NOATIME,	0,		MS_NOATIME	}, | 
|  | 45 | { "bind",	MS_TYPE,	MS_BIND,	0,		}, | 
|  | 46 | { "dev",	MS_NODEV,	0,		MS_NODEV	}, | 
|  | 47 | { "diratime",	MS_NODIRATIME,	0,		MS_NODIRATIME	}, | 
|  | 48 | { "dirsync",	MS_DIRSYNC,	MS_DIRSYNC,	0		}, | 
|  | 49 | { "exec",	MS_NOEXEC,	0,		MS_NOEXEC	}, | 
|  | 50 | { "move",	MS_TYPE,	MS_MOVE,	0		}, | 
|  | 51 | { "recurse",	MS_REC,		MS_REC,		0		}, | 
|  | 52 | { "remount",	MS_TYPE,	MS_REMOUNT,	0		}, | 
|  | 53 | { "ro",		MS_RDONLY,	MS_RDONLY,	0		}, | 
|  | 54 | { "rw",		MS_RDONLY,	0,		MS_RDONLY	}, | 
|  | 55 | { "suid",	MS_NOSUID,	0,		MS_NOSUID	}, | 
|  | 56 | { "sync",	MS_SYNCHRONOUS,	MS_SYNCHRONOUS,	0		}, | 
|  | 57 | { "verbose",	MS_VERBOSE,	MS_VERBOSE,	0		}, | 
|  | 58 | }; | 
|  | 59 |  | 
|  | 60 | static void add_extra_option(struct extra_opts *extra, char *s) | 
|  | 61 | { | 
|  | 62 | int len = strlen(s); | 
|  | 63 | int newlen = extra->used_size + len; | 
|  | 64 |  | 
|  | 65 | if (extra->str) | 
|  | 66 | len++;			/* +1 for ',' */ | 
|  | 67 |  | 
|  | 68 | if (newlen >= extra->alloc_size) { | 
|  | 69 | char *new; | 
|  | 70 |  | 
|  | 71 | new = realloc(extra->str, newlen + 1);	/* +1 for NUL */ | 
|  | 72 | if (!new) | 
|  | 73 | return; | 
|  | 74 |  | 
|  | 75 | extra->str = new; | 
|  | 76 | extra->end = extra->str + extra->used_size; | 
|  | 77 | extra->alloc_size = newlen; | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | if (extra->used_size) { | 
|  | 81 | *extra->end = ','; | 
|  | 82 | extra->end++; | 
|  | 83 | } | 
|  | 84 | strcpy(extra->end, s); | 
|  | 85 | extra->used_size += len; | 
|  | 86 |  | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | static unsigned long | 
| Ken Sumrall | 940c810 | 2011-07-12 19:47:06 -0700 | [diff] [blame] | 90 | parse_mount_options(char *arg, unsigned long rwflag, struct extra_opts *extra, int* loop, char *loopdev) | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 91 | { | 
|  | 92 | char *s; | 
|  | 93 |  | 
|  | 94 | *loop = 0; | 
|  | 95 | while ((s = strsep(&arg, ",")) != NULL) { | 
|  | 96 | char *opt = s; | 
|  | 97 | unsigned int i; | 
|  | 98 | int res, no = s[0] == 'n' && s[1] == 'o'; | 
|  | 99 |  | 
|  | 100 | if (no) | 
|  | 101 | s += 2; | 
|  | 102 |  | 
| Ken Sumrall | 940c810 | 2011-07-12 19:47:06 -0700 | [diff] [blame] | 103 | if (strncmp(s, "loop=", 5) == 0) { | 
|  | 104 | *loop = 1; | 
|  | 105 | strlcpy(loopdev, s + 5, LOOPDEV_MAXLEN); | 
|  | 106 | continue; | 
|  | 107 | } | 
|  | 108 |  | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 109 | if (strcmp(s, "loop") == 0) { | 
|  | 110 | *loop = 1; | 
| Ken Sumrall | 940c810 | 2011-07-12 19:47:06 -0700 | [diff] [blame] | 111 | strlcpy(loopdev, DEFAULT_LOOP_DEVICE, LOOPDEV_MAXLEN); | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 112 | continue; | 
|  | 113 | } | 
|  | 114 | for (i = 0, res = 1; i < ARRAY_SIZE(options); i++) { | 
|  | 115 | res = strcmp(s, options[i].str); | 
|  | 116 |  | 
|  | 117 | if (res == 0) { | 
|  | 118 | rwflag &= ~options[i].rwmask; | 
|  | 119 | if (no) | 
|  | 120 | rwflag |= options[i].rwnoset; | 
|  | 121 | else | 
|  | 122 | rwflag |= options[i].rwset; | 
|  | 123 | } | 
|  | 124 | if (res <= 0) | 
|  | 125 | break; | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | if (res != 0 && s[0]) | 
|  | 129 | add_extra_option(extra, opt); | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | return rwflag; | 
|  | 133 | } | 
|  | 134 |  | 
|  | 135 | static char *progname; | 
|  | 136 |  | 
|  | 137 | static struct extra_opts extra; | 
|  | 138 | static unsigned long rwflag; | 
|  | 139 |  | 
|  | 140 | static int | 
| Ken Sumrall | 940c810 | 2011-07-12 19:47:06 -0700 | [diff] [blame] | 141 | do_mount(char *dev, char *dir, char *type, unsigned long rwflag, void *data, int loop, | 
|  | 142 | char *loopdev) | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 143 | { | 
|  | 144 | char *s; | 
|  | 145 | int error = 0; | 
|  | 146 |  | 
|  | 147 | if (loop) { | 
|  | 148 | int file_fd, device_fd; | 
| Jay Freeman (saurik) | bc7b0cb | 2008-11-16 13:25:10 +0000 | [diff] [blame] | 149 | int flags; | 
|  | 150 |  | 
|  | 151 | flags = (rwflag & MS_RDONLY) ? O_RDONLY : O_RDWR; | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 152 |  | 
| Jay Freeman (saurik) | bc7b0cb | 2008-11-16 13:25:10 +0000 | [diff] [blame] | 153 | file_fd = open(dev, flags); | 
| Ken Sumrall | 940c810 | 2011-07-12 19:47:06 -0700 | [diff] [blame] | 154 | if (file_fd < 0) { | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 155 | perror("open backing file failed"); | 
|  | 156 | return 1; | 
|  | 157 | } | 
| Ken Sumrall | 940c810 | 2011-07-12 19:47:06 -0700 | [diff] [blame] | 158 | device_fd = open(loopdev, flags); | 
|  | 159 | if (device_fd < 0) { | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 160 | perror("open loop device failed"); | 
|  | 161 | close(file_fd); | 
|  | 162 | return 1; | 
|  | 163 | } | 
|  | 164 | if (ioctl(device_fd, LOOP_SET_FD, file_fd) < 0) { | 
|  | 165 | perror("ioctl LOOP_SET_FD failed"); | 
|  | 166 | close(file_fd); | 
|  | 167 | close(device_fd); | 
|  | 168 | return 1; | 
|  | 169 | } | 
|  | 170 |  | 
|  | 171 | close(file_fd); | 
|  | 172 | close(device_fd); | 
| Ken Sumrall | 940c810 | 2011-07-12 19:47:06 -0700 | [diff] [blame] | 173 | dev = loopdev; | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 174 | } | 
|  | 175 |  | 
|  | 176 | while ((s = strsep(&type, ",")) != NULL) { | 
|  | 177 | retry: | 
|  | 178 | if (mount(dev, dir, s, rwflag, data) == -1) { | 
|  | 179 | error = errno; | 
|  | 180 | /* | 
|  | 181 | * If the filesystem is not found, or the | 
|  | 182 | * superblock is invalid, try the next. | 
|  | 183 | */ | 
|  | 184 | if (error == ENODEV || error == EINVAL) | 
|  | 185 | continue; | 
|  | 186 |  | 
|  | 187 | /* | 
|  | 188 | * If we get EACCESS, and we're trying to | 
|  | 189 | * mount readwrite and this isn't a remount, | 
|  | 190 | * try read only. | 
|  | 191 | */ | 
|  | 192 | if (error == EACCES && | 
|  | 193 | (rwflag & (MS_REMOUNT|MS_RDONLY)) == 0) { | 
|  | 194 | rwflag |= MS_RDONLY; | 
|  | 195 | goto retry; | 
|  | 196 | } | 
|  | 197 | break; | 
|  | 198 | } | 
|  | 199 | } | 
|  | 200 |  | 
|  | 201 | if (error) { | 
|  | 202 | errno = error; | 
|  | 203 | perror("mount"); | 
|  | 204 | return 255; | 
|  | 205 | } | 
|  | 206 |  | 
|  | 207 | return 0; | 
|  | 208 | } | 
|  | 209 |  | 
|  | 210 | static int print_mounts() | 
|  | 211 | { | 
|  | 212 | FILE* f; | 
|  | 213 | int length; | 
|  | 214 | char buffer[100]; | 
|  | 215 |  | 
|  | 216 | f = fopen("/proc/mounts", "r"); | 
|  | 217 | if (!f) { | 
|  | 218 | fprintf(stdout, "could not open /proc/mounts\n"); | 
|  | 219 | return -1; | 
|  | 220 | } | 
|  | 221 |  | 
|  | 222 | do { | 
|  | 223 | length = fread(buffer, 1, 100, f); | 
|  | 224 | if (length > 0) | 
|  | 225 | fwrite(buffer, 1, length, stdout); | 
|  | 226 | } while (length > 0); | 
|  | 227 |  | 
|  | 228 | fclose(f); | 
|  | 229 | return 0; | 
|  | 230 | } | 
|  | 231 |  | 
| Colin Cross | 4e7dd3d | 2010-05-06 16:33:30 -0700 | [diff] [blame] | 232 | static int get_mounts_dev_dir(const char *arg, char **dev, char **dir) | 
|  | 233 | { | 
|  | 234 | FILE *f; | 
|  | 235 | char mount_dev[256]; | 
|  | 236 | char mount_dir[256]; | 
|  | 237 | char mount_type[256]; | 
|  | 238 | char mount_opts[256]; | 
|  | 239 | int mount_freq; | 
|  | 240 | int mount_passno; | 
|  | 241 | int match; | 
|  | 242 |  | 
|  | 243 | f = fopen("/proc/mounts", "r"); | 
|  | 244 | if (!f) { | 
|  | 245 | fprintf(stdout, "could not open /proc/mounts\n"); | 
|  | 246 | return -1; | 
|  | 247 | } | 
|  | 248 |  | 
|  | 249 | do { | 
|  | 250 | match = fscanf(f, "%255s %255s %255s %255s %d %d\n", | 
|  | 251 | mount_dev, mount_dir, mount_type, | 
|  | 252 | mount_opts, &mount_freq, &mount_passno); | 
|  | 253 | mount_dev[255] = 0; | 
|  | 254 | mount_dir[255] = 0; | 
|  | 255 | mount_type[255] = 0; | 
|  | 256 | mount_opts[255] = 0; | 
|  | 257 | if (match == 6 && | 
|  | 258 | (strcmp(arg, mount_dev) == 0 || | 
|  | 259 | strcmp(arg, mount_dir) == 0)) { | 
|  | 260 | *dev = strdup(mount_dev); | 
|  | 261 | *dir = strdup(mount_dir); | 
|  | 262 | fclose(f); | 
|  | 263 | return 0; | 
|  | 264 | } | 
|  | 265 | } while (match != EOF); | 
|  | 266 |  | 
|  | 267 | fclose(f); | 
|  | 268 | return -1; | 
|  | 269 | } | 
|  | 270 |  | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 271 | int mount_main(int argc, char *argv[]) | 
|  | 272 | { | 
|  | 273 | char *type = NULL; | 
| Colin Cross | 4e7dd3d | 2010-05-06 16:33:30 -0700 | [diff] [blame] | 274 | char *dev = NULL; | 
|  | 275 | char *dir = NULL; | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 276 | int c; | 
| Dima Zavin | 383688b | 2009-06-29 15:50:20 -0700 | [diff] [blame] | 277 | int loop = 0; | 
| Ken Sumrall | 940c810 | 2011-07-12 19:47:06 -0700 | [diff] [blame] | 278 | char loopdev[LOOPDEV_MAXLEN]; | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 279 |  | 
|  | 280 | progname = argv[0]; | 
|  | 281 | rwflag = MS_VERBOSE; | 
|  | 282 |  | 
|  | 283 | // mount with no arguments is equivalent to "cat /proc/mounts" | 
|  | 284 | if (argc == 1) return print_mounts(); | 
|  | 285 |  | 
|  | 286 | do { | 
|  | 287 | c = getopt(argc, argv, "o:rt:w"); | 
|  | 288 | if (c == EOF) | 
|  | 289 | break; | 
|  | 290 | switch (c) { | 
|  | 291 | case 'o': | 
| Ken Sumrall | 940c810 | 2011-07-12 19:47:06 -0700 | [diff] [blame] | 292 | rwflag = parse_mount_options(optarg, rwflag, &extra, &loop, loopdev); | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 293 | break; | 
|  | 294 | case 'r': | 
|  | 295 | rwflag |= MS_RDONLY; | 
|  | 296 | break; | 
|  | 297 | case 't': | 
|  | 298 | type = optarg; | 
|  | 299 | break; | 
|  | 300 | case 'w': | 
|  | 301 | rwflag &= ~MS_RDONLY; | 
|  | 302 | break; | 
|  | 303 | case '?': | 
|  | 304 | fprintf(stderr, "%s: invalid option -%c\n", | 
|  | 305 | progname, optopt); | 
|  | 306 | exit(1); | 
|  | 307 | } | 
|  | 308 | } while (1); | 
|  | 309 |  | 
|  | 310 | /* | 
|  | 311 | * If remount, bind or move was specified, then we don't | 
|  | 312 | * have a "type" as such.  Use the dummy "none" type. | 
|  | 313 | */ | 
|  | 314 | if (rwflag & MS_TYPE) | 
|  | 315 | type = "none"; | 
|  | 316 |  | 
| Colin Cross | 4e7dd3d | 2010-05-06 16:33:30 -0700 | [diff] [blame] | 317 | if (optind + 2 == argc) { | 
|  | 318 | dev = argv[optind]; | 
|  | 319 | dir = argv[optind + 1]; | 
|  | 320 | } else if (optind + 1 == argc && rwflag & MS_REMOUNT) { | 
|  | 321 | get_mounts_dev_dir(argv[optind], &dev, &dir); | 
|  | 322 | } | 
|  | 323 |  | 
|  | 324 | if (dev == NULL || dir == NULL || type == NULL) { | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 325 | fprintf(stderr, "Usage: %s [-r] [-w] [-o options] [-t type] " | 
|  | 326 | "device directory\n", progname); | 
|  | 327 | exit(1); | 
|  | 328 | } | 
|  | 329 |  | 
| Ken Sumrall | 940c810 | 2011-07-12 19:47:06 -0700 | [diff] [blame] | 330 | return do_mount(dev, dir, type, rwflag, extra.str, loop, loopdev); | 
| Colin Cross | 4e7dd3d | 2010-05-06 16:33:30 -0700 | [diff] [blame] | 331 | /* We leak dev and dir in some cases, but we're about to exit */ | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 332 | } |