mount.c (12851B)
1 /* see LICENSE file for copyright and license details */ 2 3 #if FEATURE_LINUX_MOUNT 4 #ifndef _DEFAULT_SOURCE 5 #define _DEFAULT_SOURCE 6 #endif 7 #endif 8 #include <sys/mount.h> 9 #include <sys/stat.h> 10 #include <sys/types.h> 11 #include <sys/wait.h> 12 13 #include <errno.h> 14 #include <limits.h> 15 #include <mntent.h> 16 #include <stdio.h> 17 #include <stdlib.h> 18 #include <string.h> 19 #include <strings.h> 20 #include <unistd.h> 21 22 #include "diskutil.h" 23 #include "paths.h" 24 #include "text.h" 25 #include "util.h" 26 #include "wexec.h" 27 28 #define FSOPTS_MAXLEN 512 29 30 struct { 31 const char *opt; 32 const char *notopt; 33 unsigned long v; 34 } optnames[] = { 35 {"defaults", NULL, 0}, 36 {"remount", NULL, MS_REMOUNT}, 37 {"ro", "rw", MS_RDONLY}, 38 {"sync", "async", MS_SYNCHRONOUS}, 39 {"dirsync", NULL, MS_DIRSYNC}, 40 {"nodev", "dev", MS_NODEV}, 41 {"noatime", "atime", MS_NOATIME}, 42 {"noauto", "auto", 0}, 43 {"nofail", NULL, 0}, 44 {"nodiratime", "diratime", MS_NODIRATIME}, 45 {"noexec", "exec", MS_NOEXEC}, 46 {"nosuid", "suid", MS_NOSUID}, 47 {"mand", "nomand", MS_MANDLOCK}, 48 {"relatime", "norelatime", MS_RELATIME}, 49 {"bind", NULL, MS_BIND}, 50 {NULL, NULL, 0} 51 }; 52 53 static unsigned long argflags = 0; 54 static char fsopts[FSOPTS_MAXLEN] = ""; 55 static int nofail_opt = 0; 56 57 static char * 58 findtype(const char *types, const char *t) 59 { 60 const char *p; 61 size_t len; 62 63 for (len = strlen(t); (p = strstr(types, t)); types = p + len) { 64 if (!strncmp(p, t, len) && (p[len] == '\0' || p[len] == ',')) 65 return (char *)p; 66 } 67 return NULL; 68 } 69 70 static void 71 parseopts(const char *popts, unsigned long *flags, char *data, size_t datasiz) 72 { 73 unsigned int i, validopt; 74 size_t optlen, dlen = 0; 75 const char *name, *e; 76 77 name = popts; 78 data[0] = '\0'; 79 do { 80 if ((e = strstr(name, ","))) 81 optlen = e - name; 82 else 83 optlen = strlen(name); 84 85 validopt = 0; 86 if (optlen == 6 && !strncmp(name, "nofail", 6)) { 87 nofail_opt = 1; 88 validopt = 1; 89 } 90 for (i = 0; !validopt && optnames[i].opt; i++) { 91 if (optnames[i].opt && !strncmp(name, optnames[i].opt, optlen)) { 92 *flags |= optnames[i].v; 93 validopt = 1; 94 break; 95 } 96 if (optnames[i].notopt && !strncmp(name, optnames[i].notopt, optlen)) { 97 *flags &= ~optnames[i].v; 98 validopt = 1; 99 break; 100 } 101 } 102 103 if (!validopt && optlen > 0) { 104 /* unknown option, pass as data option to mount() */ 105 if (dlen + optlen + 2 >= datasiz) 106 return; /* prevent overflow */ 107 if (dlen) 108 data[dlen++] = ','; 109 memcpy(&data[dlen], name, optlen); 110 dlen += optlen; 111 data[dlen] = '\0'; 112 } 113 name = e + 1; 114 } while (e); 115 } 116 117 static int 118 mounthelper(const char *fsname, const char *dir, const char *fstype) 119 { 120 pid_t pid; 121 char eprog[PATH_MAX]; 122 char const *eargv[10]; 123 char *found; 124 int status, i; 125 126 if (!fstype || strcmp(fstype, "auto") == 0) 127 return 1; 128 129 snprintf(eprog, sizeof(eprog), "mount.%s", fstype); 130 if (strcmp(argv0, eprog) == 0) 131 return 1; 132 133 /* no helper for this type: mount(2) does it, and nothing is printed */ 134 if (!wexec_is_builtin(eprog)) { 135 if (!(found = wwhich(eprog))) 136 return 1; 137 free(found); 138 } 139 140 pid = fork(); 141 switch (pid) { 142 case -1: 143 break; 144 case 0: 145 i = 0; 146 eargv[i++] = eprog; 147 if (argflags & MS_BIND) 148 eargv[i++] = "-B"; 149 if (argflags & MS_MOVE) 150 eargv[i++] = "-M"; 151 if (argflags & MS_REC) 152 eargv[i++] = "-R"; 153 154 if (fsopts[0]) { 155 eargv[i++] = "-o"; 156 eargv[i++] = fsopts; 157 } 158 eargv[i++] = fsname; 159 eargv[i++] = dir; 160 eargv[i] = NULL; 161 162 wexecvp_self(eprog, (char *const *)eargv); 163 default: 164 if (waitpid(pid, &status, 0) < 0) { 165 weprintf("waitpid:"); 166 return -1; 167 } 168 if (WIFEXITED(status)) 169 return WEXITSTATUS(status); 170 else if (WIFSIGNALED(status)) 171 return 1; 172 break; 173 } 174 return 0; 175 } 176 177 static const char * 178 resolve_source(const char *src, char *buf, size_t bufsz) 179 { 180 struct BlockDevInfo *list, *cur; 181 struct BlockDev dev; 182 char dev_type[32], dev_label[128], dev_uuid[128]; 183 const char *target_val; 184 int is_label; 185 186 if (!src) 187 return NULL; 188 189 if (strncmp(src, "LABEL=", 6) == 0) { 190 target_val = src + 6; 191 is_label = 1; 192 } else if (strncmp(src, "UUID=", 5) == 0) { 193 target_val = src + 5; 194 is_label = 0; 195 } else { 196 return src; 197 } 198 199 list = blockdev_get_list(); 200 if (!list) 201 return NULL; 202 203 /* the disks, and after each the partitions it holds: a filesystem is 204 * mostly on a partition, and only a disc or an unpartitioned stick is a 205 * whole disk */ 206 for (cur = list; cur; cur = cur->next) { 207 struct BlockDevInfo *part; 208 209 for (part = cur->parts; part; part = part->next) { 210 if (blockdev_open(&dev, part->path, 0) != 0) 211 continue; 212 if (blockdev_detect_fs( 213 &dev, 214 dev_type, 215 sizeof(dev_type), 216 dev_label, 217 sizeof(dev_label), 218 dev_uuid, 219 sizeof(dev_uuid) 220 ) == 0 221 && ((is_label && strcmp(dev_label, target_val) == 0) 222 || (!is_label && strcasecmp(dev_uuid, target_val) == 0))) { 223 blockdev_close(&dev); 224 estrlcpy(buf, part->path, bufsz); 225 blockdev_free_list(list); 226 return buf; 227 } 228 blockdev_close(&dev); 229 } 230 if (blockdev_open(&dev, cur->path, 0) == 0) { 231 if (blockdev_detect_fs( 232 &dev, 233 dev_type, 234 sizeof(dev_type), 235 dev_label, 236 sizeof(dev_label), 237 dev_uuid, 238 sizeof(dev_uuid) 239 ) 240 == 0) { 241 if (is_label && strcmp(dev_label, target_val) == 0) { 242 blockdev_close(&dev); 243 estrlcpy(buf, cur->path, bufsz); 244 blockdev_free_list(list); 245 return buf; 246 } 247 if (!is_label && strcasecmp(dev_uuid, target_val) == 0) { 248 blockdev_close(&dev); 249 estrlcpy(buf, cur->path, bufsz); 250 blockdev_free_list(list); 251 return buf; 252 } 253 } 254 blockdev_close(&dev); 255 } 256 } 257 blockdev_free_list(list); 258 return NULL; 259 } 260 261 static const char * 262 detect_fstype(const char *devpath, char *buf, size_t bufsz) 263 { 264 struct BlockDev dev; 265 char label[128], uuid[128]; 266 267 if (!devpath) 268 return NULL; 269 if (blockdev_open(&dev, devpath, 0) == 0) { 270 if (blockdev_detect_fs(&dev, buf, bufsz, label, sizeof(label), uuid, sizeof(uuid)) == 0) { 271 blockdev_close(&dev); 272 return buf; 273 } 274 blockdev_close(&dev); 275 } 276 return NULL; 277 } 278 279 static int 280 mounted(const char *dir) 281 { 282 FILE *fp; 283 struct mntent *me, mebuf; 284 struct stat st1, st2; 285 char linebuf[256]; 286 287 if (stat(dir, &st1) < 0) { 288 weprintf("stat %s:", dir); 289 return 0; 290 } 291 if (!(fp = setmntent(ARUU_LINUX_PATH_PROC_MOUNTS, "r"))) 292 eprintf("setmntent %s:", ARUU_LINUX_PATH_PROC_MOUNTS); 293 294 while ((me = getmntent_r(fp, &mebuf, linebuf, sizeof(linebuf)))) { 295 if (stat(me->mnt_dir, &st2) < 0) { 296 weprintf("stat %s:", me->mnt_dir); 297 continue; 298 } 299 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino) 300 return 1; 301 } 302 endmntent(fp); 303 304 return 0; 305 } 306 307 static void 308 usage(void) 309 { 310 eprintf( 311 "usage: %s [-BMRan] [-t fstype] [-o options] [source] " 312 "[target]\n", 313 argv0 314 ); 315 } 316 317 // ?man mount: mount a filesystem 318 // ?man arguments: source [target] 319 // ?man mount a filesystem to the directory tree 320 int 321 main(int argc, char *argv[]) 322 { 323 char *types = NULL, data[FSOPTS_MAXLEN] = "", *resolvpath = NULL; 324 char *files[] = {ARUU_LINUX_PATH_PROC_MOUNTS, "/etc/fstab", NULL}; 325 const char *source, *target; 326 struct mntent *me = NULL; 327 int aflag = 0, status = 0, i, r; 328 unsigned long flags = 0; 329 FILE *fp; 330 331 ARGBEGIN 332 { 333 // ?man -B: specify option flag 334 case 'B': 335 argflags |= MS_BIND; 336 break; 337 // ?man -M: specify option flag 338 case 'M': 339 argflags |= MS_MOVE; 340 break; 341 // ?man -R: operate recursively on directories 342 case 'R': 343 argflags |= MS_REC; 344 break; 345 // ?man -a: print or show all entries 346 case 'a': 347 aflag = 1; 348 break; 349 // ?man -o:str: specify output file 350 case 'o': 351 estrlcat(fsopts, EARGF(usage()), sizeof(fsopts)); 352 parseopts(fsopts, &flags, data, sizeof(data)); 353 break; 354 // ?man -t:str: sort or specify timestamp 355 case 't': 356 types = EARGF(usage()); 357 break; 358 // ?man -n: print line numbers or counts 359 case 'n': 360 break; 361 default: 362 usage(); 363 } 364 ARGEND; 365 366 if (!types && strncmp(argv0, "mount.", 6) == 0) 367 types = argv0 + 6; 368 369 if (argc < 1 && aflag == 0) { 370 if (!(fp = fopen(files[0], "r"))) 371 eprintf("fopen %s:", files[0]); 372 fconcat(fp, files[0], stdout, "<stdout>"); 373 fclose(fp); 374 return 0; 375 } 376 377 if (aflag == 1) 378 goto mountall; 379 380 source = argv[0]; 381 target = argv[1]; 382 383 if (!target) { 384 target = argv[0]; 385 source = NULL; 386 if (strcmp(target, "/") != 0) { 387 if (!(resolvpath = realpath(target, NULL))) 388 eprintf("realpath %s:", target); 389 target = resolvpath; 390 } 391 } 392 393 for (i = 0; files[i]; i++) { 394 if (!(fp = setmntent(files[i], "r"))) { 395 if (strcmp(files[i], ARUU_LINUX_PATH_PROC_MOUNTS) != 0) 396 weprintf("setmntent %s:", files[i]); 397 continue; 398 } 399 while ((me = getmntent(fp))) { 400 if (strcmp(me->mnt_dir, target) == 0 || strcmp(me->mnt_fsname, target) == 0 401 || (source && strcmp(me->mnt_dir, source) == 0) 402 || (source && strcmp(me->mnt_fsname, source) == 0)) { 403 if (!source) { 404 target = me->mnt_dir; 405 source = me->mnt_fsname; 406 } 407 if (!fsopts[0]) { 408 estrlcat(fsopts, me->mnt_opts, sizeof(fsopts)); 409 parseopts(fsopts, &flags, data, sizeof(data)); 410 } 411 if (!types) 412 types = me->mnt_type; 413 goto mountsingle; 414 } 415 } 416 endmntent(fp); 417 fp = NULL; 418 } 419 if (!source) 420 eprintf("can't find %s in /etc/fstab\n", target); 421 422 mountsingle: 423 { 424 char resbuf[PATH_MAX]; 425 char typebuf[32]; 426 const char *realsrc; 427 428 realsrc = resolve_source(source, resbuf, sizeof(resbuf)); 429 if (!realsrc) { 430 if (nofail_opt) { 431 if (fp) 432 endmntent(fp); 433 free(resolvpath); 434 return 0; 435 } 436 eprintf("mount: unable to resolve %s\n", source); 437 } 438 source = realsrc; 439 440 if (!types || strcmp(types, "auto") == 0) { 441 const char *det = detect_fstype(source, typebuf, sizeof(typebuf)); 442 if (det) 443 types = (char *)det; 444 } 445 446 r = mounthelper(source, target, types); 447 if (r == -1) 448 status = 1; 449 if (r > 0 && mount(source, target, types, argflags | flags, data) < 0) { 450 if (!nofail_opt) { 451 weprintf("mount: %s:", source); 452 status = 1; 453 } 454 } 455 if (fp) 456 endmntent(fp); 457 free(resolvpath); 458 return status; 459 } 460 461 mountall: 462 if (!(fp = setmntent("/etc/fstab", "r"))) 463 eprintf("setmntent %s:", "/etc/fstab"); 464 while ((me = getmntent(fp))) { 465 char resbuf[PATH_MAX]; 466 char typebuf[32]; 467 const char *realsrc; 468 const char *mtype = me->mnt_type; 469 470 /* has "noauto" option or already mounted: skip. swap has no 471 * directory to mount on, swapon -a takes it */ 472 if (!strcmp(me->mnt_type, MNTTYPE_SWAP) || hasmntopt(me, MNTOPT_NOAUTO) || mounted(me->mnt_dir)) 473 continue; 474 flags = 0; 475 fsopts[0] = '\0'; 476 nofail_opt = 0; 477 if (strlcat(fsopts, me->mnt_opts, sizeof(fsopts)) >= sizeof(fsopts)) { 478 weprintf("%s: option string too long\n", me->mnt_dir); 479 status = 1; 480 continue; 481 } 482 parseopts(fsopts, &flags, data, sizeof(data)); 483 /* if -t types specified: 484 * if non-match, skip 485 * if match and prefixed with "no", skip */ 486 if (types 487 && ((types[0] == 'n' && types[1] == 'o' && findtype(types + 2, me->mnt_type)) 488 || (!findtype(types, me->mnt_type)))) 489 continue; 490 491 realsrc = resolve_source(me->mnt_fsname, resbuf, sizeof(resbuf)); 492 if (!realsrc) { 493 if (nofail_opt) 494 continue; 495 weprintf("mount: unable to resolve %s\n", me->mnt_fsname); 496 status = 1; 497 continue; 498 } 499 500 if (!mtype || strcmp(mtype, "auto") == 0) { 501 const char *det = detect_fstype(realsrc, typebuf, sizeof(typebuf)); 502 if (det) 503 mtype = det; 504 } 505 506 r = mounthelper(realsrc, me->mnt_dir, mtype); 507 if (r > 0 && mount(realsrc, me->mnt_dir, mtype, argflags | flags, data) < 0) { 508 if (!nofail_opt) { 509 weprintf("mount: %s:", realsrc); 510 status = 1; 511 } 512 } 513 } 514 endmntent(fp); 515 516 return status; 517 }