Print this page
6198 Let's EOL cachefs
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/fs.d/mount.c
+++ new/usr/src/cmd/fs.d/mount.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
↓ open down ↓ |
19 lines elided |
↑ open up ↑ |
20 20 */
21 21 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
22 22 /* All Rights Reserved */
23 23
24 24
25 25 /*
26 26 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
27 27 * Use is subject to license terms.
28 28 */
29 29 /*
30 - * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
30 + * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
31 31 */
32 32
33 33 #include <stdio.h>
34 34 #include <stdio_ext.h>
35 35 #include <limits.h>
36 36 #include <fcntl.h>
37 37 #include <unistd.h>
38 38 #include <stdlib.h>
39 39 #include <string.h>
40 40 #include <stdarg.h>
41 41 #include <sys/types.h>
42 42 #include <sys/stat.h>
43 43 #include <sys/statvfs.h>
44 44 #include <errno.h>
45 45 #include <sys/mnttab.h>
46 46 #include <sys/mntent.h>
47 47 #include <sys/mount.h>
48 48 #include <sys/vfstab.h>
49 49 #include <sys/param.h>
50 50 #include <sys/wait.h>
51 51 #include <sys/signal.h>
52 52 #include <sys/resource.h>
53 53 #include <stropts.h>
54 54 #include <sys/conf.h>
55 55 #include <locale.h>
56 56 #include "fslib.h"
57 57
58 58 #define VFS_PATH "/usr/lib/fs"
59 59 #define ALT_PATH "/etc/fs"
60 60 #define REMOTE "/etc/dfs/fstypes"
61 61
62 62 #define ARGV_MAX 16
63 63 #define TIME_MAX 50
64 64 #define FSTYPE_MAX 8
65 65 #define REMOTE_MAX 64
66 66
67 67 #define OLD 0
68 68 #define NEW 1
69 69
70 70 #define READONLY 0
71 71 #define READWRITE 1
72 72 #define SUID 2
73 73 #define NOSUID 3
74 74 #define SETUID 4
75 75 #define NOSETUID 5
76 76 #define DEVICES 6
77 77 #define NODEVICES 7
78 78
79 79 #define FORMAT "%a %b %e %H:%M:%S %Y\n" /* date time format */
80 80 /* a - abbreviated weekday name */
81 81 /* b - abbreviated month name */
82 82 /* e - day of month */
83 83 /* H - hour */
84 84 /* M - minute */
85 85 /* S - second */
86 86 /* Y - Year */
87 87 /* n - newline */
88 88
89 89 /*
90 90 * The fs-local method understands this exit code to mean that one or
91 91 * more failures occurred and that all the failures were of attempted
92 92 * lofs mounts.
93 93 */
94 94 #define ALL_LOFS_FAILURES 111
95 95
96 96 extern int optind;
97 97 extern char *optarg;
98 98
99 99 extern void usage(void);
100 100 extern char *flags(char *, int);
101 101 extern char *remote(char *, FILE *);
102 102 extern char *default_fstype(char *);
103 103
104 104 char *myopts[] = {
105 105 MNTOPT_RO,
106 106 MNTOPT_RW,
107 107 MNTOPT_SUID,
108 108 MNTOPT_NOSUID,
109 109 MNTOPT_SETUID,
110 110 MNTOPT_NOSETUID,
111 111 MNTOPT_DEVICES,
112 112 MNTOPT_NODEVICES,
113 113 NULL
114 114 };
115 115
116 116 static char *myname; /* point to argv[0] */
117 117
118 118 /*
119 119 * Set the limit to double the number of characters a user should be allowed to
120 120 * type in one line.
121 121 * This should cover the different shells, which don't use POSIX_MAX_INPUT,
122 122 * and should cover the case where a long option string can be in
123 123 * the /etc/vfstab file.
124 124 */
125 125 char mntflags[(_POSIX_MAX_INPUT+1) * 2];
126 126
127 127 char realdir[MAXPATHLEN]; /* buffer for realpath() calls */
128 128 char *vfstab = VFSTAB;
129 129 char *mnttab = MNTTAB;
130 130 char *specific_opts; /* holds specific mount options */
131 131 char *generic_opts; /* holds generic mount options */
↓ open down ↓ |
91 lines elided |
↑ open up ↑ |
132 132 int maxrun;
133 133 int nrun;
134 134 int failcnt; /* total count of failures */
135 135 int lofscnt; /* presence of lofs prohibits parallel */
136 136 /* mounting */
137 137 int lofsfail; /* count of failures of lofs mounts */
138 138 int exitcode;
139 139 int aflg, cflg, fflg, Fflg, gflg, oflg, pflg, rflg, vflg, Vflg, mflg, Oflg,
140 140 dashflg, questflg, dflg, qflg;
141 141
142 -/*
143 - * Currently, mounting cachefs instances simultaneously uncovers various
144 - * problems. For the short term, we serialize cachefs activity while we fix
145 - * these cachefs bugs.
146 - */
147 -#define CACHEFS_BUG
148 -#ifdef CACHEFS_BUG
149 -int cachefs_running; /* parallel cachefs not supported yet */
150 -#endif
151 142
152 143 /*
153 144 * Each vfsent_t describes a vfstab entry. It is used to manage and cleanup
154 145 * each child that performs the particular mount for the entry.
155 146 */
156 147
157 148 typedef struct vfsent {
158 149 struct vfstab v; /* the vfstab entry */
159 150 char *rpath; /* resolved pathname so far */
160 151 int mlevel; /* how deep is this mount point */
161 152 int order; /* vfstab serial order of this vfs */
162 153 int flag;
163 154 pid_t pid; /* the pid of this mount process */
164 155 int exitcode; /* process's exitcode */
165 156 #define RDPIPE 0
166 157 #define WRPIPE 1
167 158 int sopipe[2]; /* pipe attached to child's stdout */
168 159 int sepipe[2]; /* pipe attached to child's stderr */
169 160 struct vfsent *next; /* used when in linked list */
170 161 } vfsent_t;
171 162
172 163 #define VRPFAILED 0x01 /* most recent realpath failed on */
173 164 /* this mount point */
174 165 #define VNOTMOUNTED 0x02 /* mount point could not be mounted */
175 166
176 167 vfsent_t *vfsll, *vfslltail; /* head and tail of the global */
177 168 /* linked list of vfstab entries */
178 169 vfsent_t **vfsarray; /* global array of vfsent_t's */
179 170 int vfsarraysize; /* length of the list */
180 171
181 172 /*
182 173 * This structure is used to build a linked list of
183 174 * mnttab structures from /etc/mnttab.
184 175 */
185 176 typedef struct mountent {
186 177 struct extmnttab *ment;
187 178 int flag;
188 179 struct mountent *next;
189 180 } mountent_t;
190 181
191 182 #define MSORTED 0x1
192 183
193 184 static vfsent_t **make_vfsarray(char **, int);
194 185 static vfsent_t *new_vfsent(struct vfstab *, int);
195 186 static vfsent_t *getvfsall(char *, int);
196 187
197 188 static void doexec(char *, char **);
198 189 static void nomem();
199 190 static void cleanup(int);
200 191 static char *setrpath(vfsent_t *);
201 192 static int dowait();
202 193 static int setup_iopipe(vfsent_t *);
203 194 static void setup_output(vfsent_t *);
204 195 static void doio(vfsent_t *);
205 196 static void do_mounts();
206 197 static int parmount(char **, int, char *);
207 198 static int mlevelcmp(const void *, const void *);
208 199 static int mordercmp(const void *, const void *);
209 200 static int check_fields(char *, char *);
210 201 static int cleanupkid(pid_t, int);
211 202 static void print_mnttab(int, int);
212 203 static void vfserror(int, char *);
213 204 static void mnterror(int);
214 205 static int ignore(char *);
215 206
216 207 /*
217 208 * This is /usr/sbin/mount: the generic command that in turn
218 209 * execs the appropriate /usr/lib/fs/{fstype}/mount.
219 210 * The -F flag and argument are NOT passed.
220 211 * If the usr file system is not mounted a duplicate copy
221 212 * can be found in /sbin and this version execs the
222 213 * appropriate /etc/fs/{fstype}/mount
223 214 *
224 215 * If the -F fstype, special or directory are missing,
225 216 * /etc/vfstab is searched to fill in the missing arguments.
226 217 *
227 218 * -V will print the built command on the stdout.
228 219 * It isn't passed either.
229 220 */
230 221 int
231 222 main(int argc, char *argv[])
232 223 {
233 224 char *special, /* argument of special/resource */
234 225 *mountp, /* argument of mount directory */
235 226 *fstype, /* wherein the fstype name is filled */
236 227 *newargv[ARGV_MAX], /* arg list for specific command */
237 228 *farg = NULL, *Farg = NULL;
238 229 int ii, ret, cc, fscnt;
239 230 struct stat64 stbuf;
240 231 struct vfstab vget, vref;
241 232 mode_t mode;
242 233 FILE *fd;
243 234
244 235 (void) setlocale(LC_ALL, "");
245 236
246 237 #if !defined(TEXT_DOMAIN)
247 238 #define TEXT_DOMAIN "SYS_TEST"
248 239 #endif
249 240 (void) textdomain(TEXT_DOMAIN);
250 241
251 242 myname = strrchr(argv[0], '/');
252 243 if (myname)
253 244 myname++;
254 245 else
255 246 myname = argv[0];
256 247 if (myname == 0) myname = "path unknown";
257 248
258 249 /* Process the args. */
259 250
260 251 while ((cc = getopt(argc, argv, "?acd:f:F:gmno:pqrvVO")) != -1)
261 252 switch (cc) {
262 253 case 'a':
263 254 aflg++;
264 255 break;
265 256 case 'c':
266 257 cflg++;
267 258 break;
268 259
269 260 #ifdef DEBUG
270 261 case 'd':
271 262 dflg = atoi(optarg);
272 263 break;
273 264 #endif
274 265
275 266 case 'f':
276 267 fflg++;
277 268 farg = optarg;
278 269 break;
279 270 case 'F':
280 271 Fflg++;
281 272 Farg = optarg;
282 273 break;
283 274 case 'g':
284 275 gflg++;
285 276 break;
286 277 case 'm':
287 278 mflg++;
288 279 break; /* do not update /etc/mnttab */
289 280 case 'o':
290 281 oflg++;
291 282 if ((specific_opts = strdup(optarg)) == NULL)
292 283 nomem();
293 284 break; /* fstype dependent options */
294 285 case 'O':
295 286 Oflg++;
296 287 break;
297 288 case 'p':
298 289 pflg++;
299 290 break;
300 291 case 'q':
301 292 qflg++;
302 293 break;
303 294 case 'r':
304 295 rflg++;
305 296 generic_opts = "ro";
306 297 break;
307 298 case 'v':
308 299 vflg++;
309 300 break;
310 301 case 'V':
311 302 Vflg++;
312 303 break;
313 304 case '?':
314 305 questflg++;
315 306 break;
316 307 }
317 308
318 309 /* copy '--' to specific */
319 310 if (strcmp(argv[optind-1], "--") == 0)
320 311 dashflg++;
321 312
322 313 /* option checking */
323 314 /* more than two args not allowed if !aflg */
324 315 if (!aflg && (argc - optind > 2))
325 316 usage();
326 317
327 318 /* pv mututally exclusive */
328 319 if (pflg + vflg + aflg > 1) {
329 320 fprintf(stderr, gettext
330 321 ("%s: -a, -p, and -v are mutually exclusive\n"),
331 322 myname);
332 323 usage();
333 324 }
334 325
335 326 /*
336 327 * Can't have overlaying mounts on the same mount point during
337 328 * a parallel mount.
338 329 */
339 330 if (aflg && Oflg) {
340 331 fprintf(stderr, gettext
341 332 ("%s: -a and -O are mutually exclusive\n"), myname);
342 333 usage();
343 334 }
344 335
345 336 /* dfF mutually exclusive */
346 337 if (fflg + Fflg > 1) {
347 338 fprintf(stderr, gettext
348 339 ("%s: More than one FSType specified\n"), myname);
349 340 usage();
350 341 }
351 342
352 343 /* no arguments, only allow p,v,V or [F]? */
353 344 if (!aflg && optind == argc) {
354 345 if (cflg || fflg || mflg || oflg || rflg || qflg)
355 346 usage();
356 347
357 348 if (Fflg && !questflg)
358 349 usage();
359 350
360 351 if (questflg) {
361 352 if (Fflg) {
362 353 newargv[2] = "-?";
363 354 newargv[3] = NULL;
364 355 doexec(Farg, newargv);
365 356 }
366 357 usage();
367 358 }
368 359 }
369 360
370 361 if (questflg)
371 362 usage();
372 363
373 364 /* one or two args, allow any but p,v */
374 365 if (optind != argc && (pflg || vflg)) {
375 366 fprintf(stderr,
376 367 gettext("%s: Cannot use -p and -v with arguments\n"), myname);
377 368 usage();
378 369 }
379 370
380 371
381 372 /* if only reporting mnttab, generic prints mnttab and exits */
382 373 if (!aflg && optind == argc) {
383 374 if (Vflg) {
384 375 printf("%s", myname);
385 376 if (pflg)
386 377 printf(" -p");
387 378 if (vflg)
388 379 printf(" -v");
389 380 printf("\n");
390 381 exit(0);
391 382 }
392 383
393 384 print_mnttab(vflg, pflg);
394 385 exit(0);
395 386 }
396 387
397 388 /*
398 389 * Get filesystem type here. If "-F FStype" is specified, use
399 390 * that fs type. Otherwise, determine the fs type from /etc/vfstab
400 391 * if the entry exists. Otherwise, determine the local or remote
401 392 * fs type from /etc/default/df or /etc/dfs/fstypes respectively.
402 393 */
403 394 if (fflg) {
404 395 if ((strcmp(farg, "S51K") != 0) &&
405 396 (strcmp(farg, "S52K") != 0)) {
406 397 fstype = farg;
407 398 }
408 399 else
409 400 fstype = "ufs";
410 401 } else /* if (Fflg) */
411 402 fstype = Farg;
412 403
413 404 fscnt = argc - optind;
414 405 if (aflg && (fscnt != 1))
415 406 exit(parmount(argv + optind, fscnt, fstype));
416 407
417 408 /*
418 409 * Then don't bother with the parallel over head. Everything
419 410 * from this point is simple/normal single execution.
420 411 */
421 412 aflg = 0;
422 413
423 414 /* get special and/or mount-point from arg(s) */
424 415 if (fscnt == 2)
425 416 special = argv[optind++];
426 417 else
427 418 special = NULL;
428 419 if (optind < argc)
429 420 mountp = argv[optind++];
430 421 else
431 422 mountp = NULL;
432 423
433 424 /* lookup only if we need to */
434 425 if (fstype == NULL || specific_opts == NULL || special == NULL ||
435 426 mountp == NULL) {
436 427 if ((fd = fopen(vfstab, "r")) == NULL) {
437 428 if (fstype == NULL || special == NULL ||
438 429 mountp == NULL) {
439 430 fprintf(stderr, gettext(
440 431 "%s: Cannot open %s\n"),
441 432 myname, vfstab);
442 433 exit(1);
443 434 } else {
444 435 /*
445 436 * No vfstab, but we know what we want
446 437 * to mount.
447 438 */
448 439 goto out;
449 440 }
450 441 }
451 442 vfsnull(&vref);
452 443 vref.vfs_special = special;
453 444 vref.vfs_mountp = mountp;
454 445 vref.vfs_fstype = fstype;
455 446
456 447 /* get a vfstab entry matching mountp or special */
457 448 while ((ret = getvfsany(fd, &vget, &vref)) > 0)
458 449 vfserror(ret, vget.vfs_special);
459 450
460 451 /* if no entry and there was only one argument */
461 452 /* then the argument could be the special */
462 453 /* and not mount point as we thought earlier */
463 454 if (ret == -1 && special == NULL) {
464 455 rewind(fd);
465 456 special = vref.vfs_special = mountp;
466 457 mountp = vref.vfs_mountp = NULL;
467 458 /* skip erroneous lines; they were reported above */
468 459 while ((ret = getvfsany(fd, &vget, &vref)) > 0)
469 460 ;
470 461 }
471 462
472 463 fclose(fd);
473 464
474 465 if (ret == 0) {
475 466 if (fstype == NULL)
476 467 fstype = vget.vfs_fstype;
477 468 if (special == NULL)
478 469 special = vget.vfs_special;
479 470 if (mountp == NULL)
480 471 mountp = vget.vfs_mountp;
481 472 if (oflg == 0 && vget.vfs_mntopts) {
482 473 oflg++;
483 474 specific_opts = vget.vfs_mntopts;
484 475 }
485 476 } else if (special == NULL) {
486 477 if (stat64(mountp, &stbuf) == -1) {
487 478 fprintf(stderr, gettext("%s: cannot stat %s\n"),
488 479 myname, mountp);
489 480 exit(2);
490 481 }
491 482 if (((mode = (stbuf.st_mode & S_IFMT)) == S_IFBLK) ||
492 483 (mode == S_IFCHR)) {
493 484 fprintf(stderr,
494 485 gettext("%s: mount point cannot be determined\n"),
495 486 myname);
496 487 exit(1);
497 488 } else
498 489 {
499 490 fprintf(stderr,
500 491 gettext("%s: special cannot be determined\n"),
501 492 myname);
502 493 exit(1);
503 494 }
504 495 } else if (fstype == NULL)
505 496 fstype = default_fstype(special);
506 497 }
507 498
508 499 out:
509 500 if (realpath(mountp, realdir) == NULL) {
510 501 (void) fprintf(stderr, "mount: ");
511 502 perror(mountp);
512 503 exit(1);
513 504 }
514 505
515 506 if ((mountp = strdup(realdir)) == NULL)
516 507 nomem();
517 508
518 509 if (check_fields(fstype, mountp))
519 510 exit(1);
520 511
521 512 /* create the new arg list, and end the list with a null pointer */
522 513 ii = 2;
523 514 if (cflg)
524 515 newargv[ii++] = "-c";
525 516 if (gflg)
526 517 newargv[ii++] = "-g";
527 518 if (mflg)
528 519 newargv[ii++] = "-m";
529 520 /*
530 521 * The q option needs to go before the -o option as some
531 522 * filesystems complain during first pass option parsing.
532 523 */
533 524 if (qflg)
534 525 newargv[ii++] = "-q";
535 526 if (oflg) {
536 527 newargv[ii++] = "-o";
537 528 newargv[ii++] = specific_opts;
538 529 }
539 530 if (Oflg)
540 531 newargv[ii++] = "-O";
541 532 if (rflg)
542 533 newargv[ii++] = "-r";
543 534 if (dashflg)
544 535 newargv[ii++] = "--";
545 536 newargv[ii++] = special;
546 537 newargv[ii++] = mountp;
547 538 newargv[ii] = NULL;
548 539
549 540 doexec(fstype, newargv);
550 541 return (0);
551 542 }
552 543
553 544 void
554 545 usage(void)
555 546 {
556 547 fprintf(stderr, gettext("Usage:\n%s [-v | -p]\n"), myname);
557 548 fprintf(stderr, gettext(
558 549 "%s [-F FSType] [-V] [current_options] [-o specific_options]"),
559 550 myname);
560 551 fprintf(stderr, gettext("\n\t{special | mount_point}\n"));
561 552
562 553 fprintf(stderr, gettext(
563 554 "%s [-F FSType] [-V] [current_options] [-o specific_options]"),
564 555 myname);
565 556 fprintf(stderr, gettext("\n\tspecial mount_point\n"));
566 557
567 558 fprintf(stderr, gettext(
568 559 "%s -a [-F FSType ] [-V] [current_options] [-o specific_options]\n"),
569 560 myname);
570 561 fprintf(stderr, gettext("\t[mount_point ...]\n"));
571 562
572 563 exit(1);
573 564 }
574 565
575 566 /*
576 567 * Get rid of "dev=[hex string]" clause, if any. It's not legal
577 568 * when printing in vfstab format.
578 569 */
579 570 void
580 571 elide_dev(char *mntopts)
581 572 {
582 573 char *dev, *other;
583 574
584 575 if (mntopts != NULL) {
585 576 dev = strstr(mntopts, "dev=");
586 577 if (dev != NULL) {
587 578 other = strpbrk(dev, ",");
588 579 if (other == NULL) {
589 580 /* last option */
590 581 if (dev != mntopts) {
591 582 *--dev = '\0';
592 583 } else {
593 584 *dev = '\0';
594 585 }
595 586 } else {
596 587 /* first or intermediate option */
597 588 memmove(dev, other+1, strlen(other+1)+1);
598 589 }
599 590 }
600 591 }
601 592 }
602 593
603 594 void
604 595 print_mnttab(int vflg, int pflg)
605 596 {
606 597 FILE *fd;
607 598 FILE *rfp; /* this will be NULL if fopen fails */
608 599 int ret;
609 600 char time_buf[TIME_MAX]; /* array to hold date and time */
610 601 struct extmnttab mget;
611 602 time_t ltime;
612 603
613 604 if ((fd = fopen(mnttab, "r")) == NULL) {
614 605 fprintf(stderr, gettext("%s: Cannot open mnttab\n"), myname);
615 606 exit(1);
616 607 }
617 608 rfp = fopen(REMOTE, "r");
618 609 while ((ret = getextmntent(fd, &mget, sizeof (struct extmnttab)))
619 610 == 0) {
620 611 if (ignore(mget.mnt_mntopts))
621 612 continue;
622 613 if (mget.mnt_special && mget.mnt_mountp &&
623 614 mget.mnt_fstype && mget.mnt_time) {
624 615 ltime = atol(mget.mnt_time);
625 616 cftime(time_buf, FORMAT, <ime);
626 617 if (pflg) {
627 618 elide_dev(mget.mnt_mntopts);
628 619 printf("%s - %s %s - no %s\n",
629 620 mget.mnt_special,
630 621 mget.mnt_mountp,
631 622 mget.mnt_fstype,
632 623 mget.mnt_mntopts != NULL ?
633 624 mget.mnt_mntopts : "-");
634 625 } else if (vflg) {
635 626 printf("%s on %s type %s %s%s on %s",
636 627 mget.mnt_special,
637 628 mget.mnt_mountp,
638 629 mget.mnt_fstype,
639 630 remote(mget.mnt_fstype, rfp),
640 631 flags(mget.mnt_mntopts, NEW),
641 632 time_buf);
642 633 } else
643 634 printf("%s on %s %s%s on %s",
644 635 mget.mnt_mountp,
645 636 mget.mnt_special,
646 637 remote(mget.mnt_fstype, rfp),
647 638 flags(mget.mnt_mntopts, OLD),
648 639 time_buf);
649 640 }
650 641 }
651 642 if (ret > 0)
652 643 mnterror(ret);
653 644 }
654 645
655 646 char *
656 647 flags(char *mntopts, int flag)
657 648 {
658 649 char opts[sizeof (mntflags)];
659 650 char *value;
660 651 int rdwr = 1;
661 652 int suid = 1;
662 653 int devices = 1;
663 654 int setuid = 1;
664 655
665 656 if (mntopts == NULL || *mntopts == '\0')
666 657 return ("read/write/setuid/devices");
667 658
668 659 strcpy(opts, "");
669 660 while (*mntopts != '\0') {
670 661 switch (getsubopt(&mntopts, myopts, &value)) {
671 662 case READONLY:
672 663 rdwr = 0;
673 664 break;
674 665 case READWRITE:
675 666 rdwr = 1;
676 667 break;
677 668 case SUID:
678 669 suid = 1;
679 670 break;
680 671 case NOSUID:
681 672 suid = 0;
682 673 break;
683 674 case SETUID:
684 675 setuid = 1;
685 676 break;
686 677 case NOSETUID:
687 678 setuid = 0;
688 679 break;
689 680 case DEVICES:
690 681 devices = 1;
691 682 break;
692 683 case NODEVICES:
693 684 devices = 0;
694 685 break;
695 686 default:
696 687 /* cat '/' separator to mntflags */
697 688 if (*opts != '\0' && value != NULL)
698 689 strcat(opts, "/");
699 690 strcat(opts, value);
700 691 break;
701 692 }
702 693 }
703 694
704 695 strcpy(mntflags, "");
705 696 if (rdwr)
706 697 strcat(mntflags, "read/write");
707 698 else if (flag == OLD)
708 699 strcat(mntflags, "read only");
709 700 else
710 701 strcat(mntflags, "read-only");
711 702 if (suid) {
712 703 if (setuid)
713 704 strcat(mntflags, "/setuid");
714 705 else
715 706 strcat(mntflags, "/nosetuid");
716 707 if (devices)
717 708 strcat(mntflags, "/devices");
718 709 else
719 710 strcat(mntflags, "/nodevices");
720 711 } else {
721 712 strcat(mntflags, "/nosetuid/nodevices");
722 713 }
723 714 if (*opts != '\0') {
724 715 strcat(mntflags, "/");
725 716 strcat(mntflags, opts);
726 717 }
727 718
728 719 /*
729 720 * The assumed assertion
730 721 * assert (strlen(mntflags) < sizeof mntflags);
731 722 * is valid at this point in the code. Note that a call to "assert"
732 723 * is not appropriate in production code since it halts the program.
733 724 */
734 725 return (mntflags);
735 726 }
736 727
737 728 char *
738 729 remote(char *fstype, FILE *rfp)
739 730 {
740 731 char buf[BUFSIZ];
741 732 char *fs;
742 733 extern char *strtok();
743 734
744 735 if (rfp == NULL || fstype == NULL ||
745 736 strlen(fstype) > (size_t)FSTYPE_MAX)
746 737 return (""); /* not a remote */
747 738 rewind(rfp);
748 739 while (fgets(buf, sizeof (buf), rfp) != NULL) {
749 740 fs = strtok(buf, " \t\n");
750 741 if (strcmp(fstype, fs) == 0)
751 742 return ("remote/"); /* is a remote fs */
752 743 }
753 744 return (""); /* not a remote */
754 745 }
755 746
756 747
757 748 void
758 749 vfserror(int flag, char *special)
759 750 {
760 751 if (special == NULL)
761 752 special = "<null>";
762 753 switch (flag) {
763 754 case VFS_TOOLONG:
764 755 fprintf(stderr,
765 756 gettext("%s: Warning: Line in vfstab for \"%s\" exceeds %d characters\n"),
766 757 myname, special, VFS_LINE_MAX-1);
767 758 break;
768 759 case VFS_TOOFEW:
769 760 fprintf(stderr,
770 761 gettext("%s: Warning: Line for \"%s\" in vfstab has too few entries\n"),
771 762 myname, special);
772 763 break;
773 764 case VFS_TOOMANY:
774 765 fprintf(stderr,
775 766 gettext("%s: Warning: Line for \"%s\" in vfstab has too many entries\n"),
776 767 myname, special);
777 768 break;
778 769 default:
779 770 fprintf(stderr, gettext(
780 771 "%s: Warning: Error in line for \"%s\" in vfstab\n"),
781 772 myname, special);
782 773 }
783 774 }
784 775
785 776 void
786 777 mnterror(int flag)
787 778 {
788 779 switch (flag) {
789 780 case MNT_TOOLONG:
790 781 fprintf(stderr,
791 782 gettext("%s: Line in mnttab exceeds %d characters\n"),
792 783 myname, MNT_LINE_MAX-2);
793 784 break;
794 785 case MNT_TOOFEW:
795 786 fprintf(stderr,
796 787 gettext("%s: Line in mnttab has too few entries\n"),
797 788 myname);
798 789 break;
799 790 case MNT_TOOMANY:
800 791 fprintf(stderr,
801 792 gettext("%s: Line in mnttab has too many entries\n"),
802 793 myname);
803 794 break;
804 795 }
805 796 exit(1);
806 797 }
807 798
808 799 void
809 800 doexec(char *fstype, char *newargv[])
810 801 {
811 802 char full_path[PATH_MAX];
812 803 char alter_path[PATH_MAX];
813 804 char *vfs_path = VFS_PATH;
814 805 char *alt_path = ALT_PATH;
815 806 int i;
816 807
817 808 /* build the full pathname of the fstype dependent command. */
818 809 sprintf(full_path, "%s/%s/%s", vfs_path, fstype, myname);
819 810 sprintf(alter_path, "%s/%s/%s", alt_path, fstype, myname);
820 811 newargv[1] = myname;
821 812
822 813 if (Vflg) {
823 814 printf("%s -F %s", newargv[1], fstype);
824 815 for (i = 2; newargv[i]; i++)
825 816 printf(" %s", newargv[i]);
826 817 printf("\n");
827 818 fflush(stdout);
828 819 exit(0);
829 820 }
830 821
831 822 /*
832 823 * Try to exec the fstype dependent portion of the mount.
833 824 * See if the directory is there before trying to exec dependent
834 825 * portion. This is only useful for eliminating the
835 826 * '..mount: not found' message when '/usr' is mounted
836 827 */
837 828 if (access(full_path, 0) == 0) {
838 829 execv(full_path, &newargv[1]);
839 830 if (errno == EACCES) {
840 831 fprintf(stderr,
841 832 gettext("%s: Cannot execute %s - permission denied\n"),
842 833 myname, full_path);
843 834 }
844 835 if (errno == ENOEXEC) {
845 836 newargv[0] = "sh";
846 837 newargv[1] = full_path;
847 838 execv("/sbin/sh", &newargv[0]);
848 839 }
849 840 }
850 841 execv(alter_path, &newargv[1]);
851 842 if (errno == EACCES) {
852 843 fprintf(stderr, gettext(
853 844 "%s: Cannot execute %s - permission denied\n"),
854 845 myname, alter_path);
855 846 exit(1);
856 847 }
857 848 if (errno == ENOEXEC) {
858 849 newargv[0] = "sh";
859 850 newargv[1] = alter_path;
860 851 execv("/sbin/sh", &newargv[0]);
861 852 }
862 853 fprintf(stderr,
863 854 gettext("%s: Operation not applicable to FSType %s\n"),
864 855 myname, fstype);
865 856 exit(1);
866 857 }
867 858
868 859 char *mntopts[] = { MNTOPT_IGNORE, NULL };
869 860 #define IGNORE 0
870 861
871 862 /*
872 863 * Return 1 if "ignore" appears in the options string
873 864 */
874 865 int
875 866 ignore(char *opts)
876 867 {
877 868 char *value;
878 869 char *saveptr, *my_opts;
879 870 int rval = 0;
880 871
881 872 if (opts == NULL || *opts == NULL)
882 873 return (0);
883 874
884 875 /*
885 876 * we make a copy of the option string to pass to getsubopt(),
886 877 * because getsubopt() modifies the string. We also save
887 878 * the original pointer returned by strdup, because getsubopt
888 879 * changes the pointer passed into it. If strdup fails (unlikely),
889 880 * we act as if the "ignore" option isn't set rather than fail.
890 881 */
891 882
892 883 if ((saveptr = my_opts = strdup(opts)) == NULL)
893 884 nomem();
894 885
895 886 while (*my_opts != '\0') {
896 887 if (getsubopt(&my_opts, mntopts, &value) == IGNORE)
897 888 rval = 1;
898 889 }
899 890
900 891 free(saveptr);
901 892
902 893 return (rval);
903 894 }
904 895
905 896 /*
906 897 * Perform the parallel version of mount. If count == 0, mount all
907 898 * vfstab filesystems with the automnt field == "yes". Use fstype if
908 899 * supplied. If mntlist supplied, then attempt to only mount those.
909 900 */
910 901
911 902 int
912 903 parmount(char **mntlist, int count, char *fstype)
913 904 {
914 905 int maxfd = OPEN_MAX;
915 906 struct rlimit rl;
916 907 vfsent_t **vl, *vp;
917 908
918 909 /*
919 910 * Process scaling. After running a series
920 911 * of tests based on the number of simultaneous processes and
921 912 * processors available, optimum performance was achieved near or
922 913 * at (PROCN * 2).
923 914 */
924 915 if ((maxrun = sysconf(_SC_NPROCESSORS_ONLN)) == -1)
925 916 maxrun = 4;
926 917 else
927 918 maxrun = maxrun * 2 + 1;
928 919
929 920 if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
930 921 rl.rlim_cur = rl.rlim_max;
931 922 if (setrlimit(RLIMIT_NOFILE, &rl) == 0)
932 923 maxfd = (int)rl.rlim_cur;
933 924 }
934 925 (void) enable_extended_FILE_stdio(-1, -1);
935 926
936 927 /*
937 928 * The parent needs to maintain 3 of its own fd's, plus 2 for
938 929 * each child (the stdout and stderr pipes).
939 930 */
940 931 maxfd = (maxfd / 2) - 6; /* 6 takes care of temporary */
941 932 /* periods of open fds */
942 933 if (maxfd < maxrun)
943 934 maxrun = maxfd;
944 935 if (maxrun < 4)
945 936 maxrun = 4; /* sanity check */
946 937
947 938 if (count == 0)
948 939 mntlist = NULL; /* used as a flag later */
949 940 else
950 941 fstype = NULL; /* mount points supplied: */
951 942 /* ignore fstype */
952 943 /*
953 944 * Read the whole vfstab into a linked list for quick processing.
954 945 * On average, this is the most efficient way to collect and
955 946 * manipulate the vfstab data.
956 947 */
957 948 vfsll = getvfsall(fstype, mntlist == NULL);
958 949
959 950 /*
960 951 * Make an array out of the vfs linked list for sorting purposes.
961 952 */
962 953 if (vfsll == NULL ||
963 954 (vfsarray = make_vfsarray(mntlist, count)) == NULL) {
964 955 if (mntlist == NULL) /* not an error - just none found */
965 956 return (0);
966 957
967 958 fprintf(stderr, gettext("%s: No valid entries found in %s\n"),
968 959 myname, vfstab);
969 960 return (1);
970 961 }
971 962
972 963 /*
973 964 * Sort the entries based on their resolved path names
974 965 *
975 966 * If an lofs is encountered, then the original order of the vfstab
976 967 * file needs to be maintained until we are done mounting lofs's.
977 968 */
978 969 if (!lofscnt)
979 970 qsort((void *)vfsarray, vfsarraysize, sizeof (vfsent_t *),
980 971 mlevelcmp);
981 972
982 973 /*
983 974 * Shrink the vfsll linked list down to the new list. This will
984 975 * speed up the pid search in cleanupkid() later.
985 976 */
986 977 vfsll = vfsarray[0];
987 978 for (vl = vfsarray; vp = *vl; )
988 979 vp->next = *++vl;
989 980
990 981 /*
991 982 * Try to handle interrupts in a reasonable way.
992 983 */
993 984 sigset(SIGHUP, cleanup);
994 985 sigset(SIGQUIT, cleanup);
995 986 sigset(SIGINT, cleanup);
996 987
997 988 do_mounts(); /* do the mounts */
998 989
999 990 if (failcnt > 0 && failcnt == lofsfail)
1000 991 return (ALL_LOFS_FAILURES);
1001 992
1002 993 return (exitcode);
1003 994 }
1004 995
1005 996 /*
1006 997 * Read all vstab (fp) entries into memory if fstype == NULL.
1007 998 * If fstype is specified, than read all those that match it.
1008 999 *
1009 1000 * Returns a linked list.
1010 1001 */
1011 1002 vfsent_t *
1012 1003 getvfsall(char *fstype, int takeall)
1013 1004 {
1014 1005 vfsent_t *vhead, *vtail;
1015 1006 struct vfstab vget;
1016 1007 FILE *fp;
1017 1008 int cnt = 0, ret;
1018 1009
1019 1010 if ((fp = fopen(vfstab, "r")) == NULL) {
1020 1011 fprintf(stderr, gettext("%s: Cannot open %s\n"),
1021 1012 myname, vfstab);
1022 1013 exit(1);
1023 1014 }
1024 1015
1025 1016 vhead = vtail = NULL;
1026 1017
1027 1018 while ((ret = getvfsent(fp, &vget)) != -1) {
1028 1019 vfsent_t *vp;
1029 1020
1030 1021 if (ret > 0) {
1031 1022 vfserror(ret, vget.vfs_mountp);
1032 1023 continue;
1033 1024 }
1034 1025
1035 1026 /*
1036 1027 * If mount points were not specified, then we ignore
1037 1028 * entries that aren't marked "yes".
1038 1029 */
1039 1030 if (takeall &&
1040 1031 (vget.vfs_automnt == NULL ||
1041 1032 strcmp(vget.vfs_automnt, "yes")))
1042 1033 continue;
1043 1034
1044 1035 if (fstype && vget.vfs_fstype &&
1045 1036 strcmp(fstype, vget.vfs_fstype))
1046 1037 continue;
1047 1038
1048 1039 if (vget.vfs_mountp == NULL ||
1049 1040 (vget.vfs_fstype && (strcmp(vget.vfs_fstype, "swap") == 0)))
1050 1041 continue;
1051 1042
1052 1043 if (check_fields(vget.vfs_fstype, vget.vfs_mountp)) {
1053 1044 exitcode = 1;
1054 1045 continue;
1055 1046 }
1056 1047
1057 1048 vp = new_vfsent(&vget, cnt); /* create new vfs entry */
1058 1049 if (vhead == NULL)
1059 1050 vhead = vp;
1060 1051 else
1061 1052 vtail->next = vp;
1062 1053 vtail = vp;
1063 1054 cnt++;
1064 1055 }
1065 1056 fclose(fp);
1066 1057 if (vtail == NULL) {
1067 1058 vfsarraysize = 0;
1068 1059 vfslltail = NULL;
1069 1060 return (NULL);
1070 1061 }
1071 1062 vtail->next = NULL;
1072 1063 vfslltail = vtail; /* save it in the global variable */
1073 1064 vfsarraysize = cnt;
1074 1065 return (vhead);
1075 1066 }
1076 1067
1077 1068
1078 1069 /*
1079 1070 * Returns an array of vfsent_t's based on vfsll & mntlist.
1080 1071 */
1081 1072 vfsent_t **
1082 1073 make_vfsarray(char **mntlist, int count)
1083 1074 {
1084 1075 vfsent_t *vp, *vmark, *vpprev, **vpp;
1085 1076 int ndx, found;
1086 1077
1087 1078 if (vfsll == NULL)
1088 1079 return (NULL);
1089 1080
1090 1081 if (count > 0)
1091 1082 vfsarraysize = count;
1092 1083
1093 1084 vpp = (vfsent_t **)malloc(sizeof (*vpp) * (vfsarraysize + 1));
1094 1085 if (vpp == NULL)
1095 1086 nomem();
1096 1087
1097 1088 if (mntlist == NULL) {
1098 1089 /*
1099 1090 * No mount list specified: take all vfstab mount points.
1100 1091 */
1101 1092 for (ndx = 0, vp = vfsll; vp; vp = vp->next) {
1102 1093 (void) setrpath(vp);
1103 1094 /*
1104 1095 * Sigh. lofs entries can complicate matters so much
1105 1096 * that the best way to avoid problems is to
1106 1097 * stop parallel mounting when an lofs is
1107 1098 * encountered, so we keep a count of how many
1108 1099 * there are.
1109 1100 * Fortunately this is rare.
1110 1101 */
1111 1102 if (vp->v.vfs_fstype &&
1112 1103 (strcmp(vp->v.vfs_fstype, MNTTYPE_LOFS) == 0))
1113 1104 lofscnt++;
1114 1105
1115 1106 vpp[ndx++] = vp;
1116 1107 }
1117 1108 vpp[ndx] = NULL;
1118 1109 return (vpp);
1119 1110 }
1120 1111
1121 1112 /*
1122 1113 * A list of mount points was specified on the command line
1123 1114 * and we need to search for each one.
1124 1115 */
1125 1116 vpprev = vfslltail;
1126 1117 vpprev->next = vfsll; /* make a circle out of it */
1127 1118 vmark = vp = vfsll;
1128 1119 /*
1129 1120 * For each specified mount point:
1130 1121 */
1131 1122 for (ndx = 0; *mntlist; mntlist++) {
1132 1123 found = 0;
1133 1124 /*
1134 1125 * Circle our entire linked list, looking for *mntlist.
1135 1126 */
1136 1127 while (vp) {
1137 1128 if (strcmp(*mntlist, vp->v.vfs_mountp) == 0) {
1138 1129 vpp[ndx++] = vp; /* found it. */
1139 1130 (void) setrpath(vp);
1140 1131 if (vp->v.vfs_fstype &&
1141 1132 (strcmp(vp->v.vfs_fstype,
1142 1133 MNTTYPE_LOFS) == 0))
1143 1134 lofscnt++;
1144 1135
1145 1136 if (vp == vpprev) { /* list exhausted */
1146 1137 vp = NULL;
1147 1138 found++;
1148 1139 break;
1149 1140 }
1150 1141 /*
1151 1142 * Remove it from the circular list. vpprev
1152 1143 * remains unchanged.
1153 1144 */
1154 1145 vp = vp->next;
1155 1146 vpprev->next->next = NULL;
1156 1147 vpprev->next = vp;
1157 1148 /*
1158 1149 * Set vmark to the first elem that we check
1159 1150 * each time.
1160 1151 */
1161 1152 vmark = vp;
1162 1153 found++;
1163 1154 break;
1164 1155 }
1165 1156 vpprev = vp;
1166 1157 vp = vp->next;
1167 1158 if (vp == vmark) /* break out if we completed */
1168 1159 /* the circle */
1169 1160 break;
1170 1161 }
1171 1162
1172 1163 if (!found) {
1173 1164 fprintf(stderr, gettext(
1174 1165 "%s: Warning: %s not found in %s\n"),
1175 1166 myname, *mntlist, vfstab);
1176 1167 exitcode = 1;
1177 1168 }
1178 1169 }
1179 1170 if (ndx == 0)
1180 1171 return (NULL);
1181 1172
1182 1173 vpp[ndx] = NULL; /* null terminate the list */
1183 1174 vfsarraysize = ndx; /* adjust vfsarraysize */
1184 1175 return (vpp);
1185 1176 }
1186 1177
1187 1178 /*
1188 1179 * Performs the exec argument processing, all of the child forking and
1189 1180 * execing, and child cleanup.
1190 1181 * Sets exitcode to non-zero if any errors occurred.
1191 1182 */
1192 1183 void
1193 1184 do_mounts(void)
1194 1185 {
1195 1186 int i, isave, cnt;
1196 1187 vfsent_t *vp, *vpprev, **vl;
1197 1188 char *newargv[ARGV_MAX];
1198 1189 pid_t child;
1199 1190
1200 1191 /*
1201 1192 * create the arg list once; the only differences among
1202 1193 * the calls are the options, special and mountp fields.
1203 1194 */
1204 1195 i = 2;
1205 1196 if (cflg)
1206 1197 newargv[i++] = "-c";
1207 1198 if (gflg)
1208 1199 newargv[i++] = "-g";
1209 1200 if (mflg)
1210 1201 newargv[i++] = "-m";
1211 1202 if (Oflg)
1212 1203 newargv[i++] = "-O";
1213 1204 if (qflg)
1214 1205 newargv[i++] = "-q";
1215 1206 if (rflg)
1216 1207 newargv[i++] = "-r";
1217 1208 if (dashflg)
1218 1209 newargv[i++] = "--";
1219 1210 if (oflg) {
1220 1211 newargv[i++] = "-o";
1221 1212 newargv[i++] = specific_opts;
1222 1213 }
1223 1214 isave = i;
1224 1215
1225 1216 /*
1226 1217 * Main loop for the mount processes
1227 1218 */
1228 1219 vl = vfsarray;
1229 1220 cnt = vfsarraysize;
1230 1221 for (vpprev = *vl; vp = *vl; vpprev = vp, vl++, cnt--) {
1231 1222 /*
1232 1223 * Check to see if we cross a mount level: e.g.,
1233 1224 * /a/b -> /a/b/c. If so, we need to wait for all current
1234 1225 * mounts to finish, rerun realpath on the remaining mount
1235 1226 * points, and resort the list.
1236 1227 *
1237 1228 * Also, we mount serially as long as there are lofs's
1238 1229 * to mount to avoid improper mount ordering.
1239 1230 */
1240 1231 if (vp->mlevel > vpprev->mlevel || lofscnt > 0) {
1241 1232 vfsent_t **vlp;
1242 1233
1243 1234 while (nrun > 0 && (dowait() != -1))
1244 1235 ;
1245 1236 /*
1246 1237 * Gads! It's possible for real path mounts points to
1247 1238 * change after mounts are done at a lower mount
1248 1239 * level.
1249 1240 * Thus, we need to recalculate mount levels and
1250 1241 * resort the list from this point.
1251 1242 */
1252 1243 for (vlp = vl; *vlp; vlp++)
1253 1244 (void) setrpath(*vlp);
1254 1245 /*
1255 1246 * Sort the remaining entries based on their newly
1256 1247 * resolved path names.
1257 1248 * Do not sort if we still have lofs's to mount.
1258 1249 */
1259 1250 if (lofscnt == 0) {
1260 1251 qsort((void *)vl, cnt, sizeof (vfsent_t *),
1261 1252 mlevelcmp);
1262 1253 vp = *vl;
1263 1254 }
1264 1255 }
1265 1256
1266 1257 if (vp->flag & VRPFAILED) {
1267 1258 fprintf(stderr, gettext(
1268 1259 "%s: Nonexistent mount point: %s\n"),
1269 1260 myname, vp->v.vfs_mountp);
1270 1261 vp->flag |= VNOTMOUNTED;
1271 1262 exitcode = 1;
1272 1263 continue;
1273 1264 }
1274 1265
1275 1266 /*
1276 1267 * If mount options were not specified on the command
1277 1268 * line, then use the ones found in the vfstab entry,
1278 1269 * if any.
1279 1270 */
1280 1271 i = isave;
1281 1272 if (!oflg && vp->v.vfs_mntopts) {
1282 1273 newargv[i++] = "-o";
1283 1274 newargv[i++] = vp->v.vfs_mntopts;
1284 1275 }
1285 1276 newargv[i++] = vp->v.vfs_special;
1286 1277 newargv[i++] = vp->rpath;
1287 1278 newargv[i] = NULL;
↓ open down ↓ |
1127 lines elided |
↑ open up ↑ |
1288 1279
1289 1280 /*
1290 1281 * This should never really fail.
1291 1282 */
1292 1283 while (setup_iopipe(vp) == -1 && (dowait() != -1))
1293 1284 ;
1294 1285
1295 1286 while (nrun >= maxrun && (dowait() != -1)) /* throttle */
1296 1287 ;
1297 1288
1298 -#ifdef CACHEFS_BUG
1299 - if (vp->v.vfs_fstype &&
1300 - (strcmp(vp->v.vfs_fstype, "cachefs") == 0)) {
1301 - while (cachefs_running && (dowait() != -1))
1302 - ;
1303 - cachefs_running = 1;
1304 - }
1305 -#endif
1306 -
1307 1289 if ((child = fork()) == -1) {
1308 1290 perror("fork");
1309 1291 cleanup(-1);
1310 1292 /* not reached */
1311 1293 }
1312 1294 if (child == 0) { /* child */
1313 1295 signal(SIGHUP, SIG_IGN);
1314 1296 signal(SIGQUIT, SIG_IGN);
1315 1297 signal(SIGINT, SIG_IGN);
1316 1298 setup_output(vp);
1317 1299 doexec(vp->v.vfs_fstype, newargv);
1318 1300 perror("exec");
1319 1301 exit(1);
1320 1302 }
1321 1303
1322 1304 /* parent */
1323 1305 (void) close(vp->sopipe[WRPIPE]);
1324 1306 (void) close(vp->sepipe[WRPIPE]);
1325 1307 vp->pid = child;
1326 1308 nrun++;
1327 1309 }
1328 1310 /*
1329 1311 * Mostly done by now - wait and clean up the stragglers.
1330 1312 */
1331 1313 cleanup(0);
1332 1314 }
1333 1315
1334 1316
1335 1317 /*
1336 1318 * Setup stdout and stderr pipes for the children's output.
1337 1319 */
1338 1320 int
1339 1321 setup_iopipe(vfsent_t *mp)
1340 1322 {
1341 1323 /*
1342 1324 * Make a stdout and stderr pipe. This should never fail.
1343 1325 */
1344 1326 if (pipe(mp->sopipe) == -1)
1345 1327 return (-1);
1346 1328 if (pipe(mp->sepipe) == -1) {
1347 1329 (void) close(mp->sopipe[RDPIPE]);
1348 1330 (void) close(mp->sopipe[WRPIPE]);
1349 1331 return (-1);
1350 1332 }
1351 1333 /*
1352 1334 * Don't block on an empty pipe.
1353 1335 */
1354 1336 (void) fcntl(mp->sopipe[RDPIPE], F_SETFL, O_NDELAY|O_NONBLOCK);
1355 1337 (void) fcntl(mp->sepipe[RDPIPE], F_SETFL, O_NDELAY|O_NONBLOCK);
1356 1338 /*
1357 1339 * Don't pass extra fds into children.
1358 1340 */
1359 1341 (void) fcntl(mp->sopipe[RDPIPE], F_SETFD, FD_CLOEXEC);
1360 1342 (void) fcntl(mp->sepipe[RDPIPE], F_SETFD, FD_CLOEXEC);
1361 1343
1362 1344 return (0);
1363 1345 }
1364 1346
1365 1347 /*
1366 1348 * Called by a child to attach its stdout and stderr to the write side of
1367 1349 * the pipes.
1368 1350 */
1369 1351 void
1370 1352 setup_output(vfsent_t *vp)
1371 1353 {
1372 1354
1373 1355 (void) close(fileno(stdout));
1374 1356 (void) dup(vp->sopipe[WRPIPE]);
1375 1357 (void) close(vp->sopipe[WRPIPE]);
1376 1358
1377 1359 (void) close(fileno(stderr));
1378 1360 (void) dup(vp->sepipe[WRPIPE]);
1379 1361 (void) close(vp->sepipe[WRPIPE]);
1380 1362 }
1381 1363
1382 1364 /*
1383 1365 * Parent uses this to print any stdout or stderr output issued by
1384 1366 * the child.
1385 1367 */
1386 1368 static void
1387 1369 doio(vfsent_t *vp)
1388 1370 {
1389 1371 int bytes;
1390 1372 char ibuf[BUFSIZ];
1391 1373
1392 1374 while ((bytes = read(vp->sepipe[RDPIPE], ibuf, sizeof (ibuf))) > 0)
1393 1375 write(fileno(stderr), ibuf, bytes);
1394 1376 while ((bytes = read(vp->sopipe[RDPIPE], ibuf, sizeof (ibuf))) > 0)
1395 1377 write(fileno(stdout), ibuf, bytes);
1396 1378
1397 1379 (void) close(vp->sopipe[RDPIPE]);
1398 1380 (void) close(vp->sepipe[RDPIPE]);
1399 1381 }
1400 1382
1401 1383 /*
1402 1384 * Waits for 1 child to die.
1403 1385 *
1404 1386 * Returns -1 if no children are left to wait for.
1405 1387 * Returns 0 if a child died without an error.
1406 1388 * Returns 1 if a child died with an error.
1407 1389 */
1408 1390 int
1409 1391 dowait(void)
1410 1392 {
1411 1393 int child, wstat;
1412 1394
1413 1395 if ((child = wait(&wstat)) == -1)
1414 1396 return (-1);
1415 1397 nrun--;
1416 1398 return (cleanupkid(child, wstat) != 0);
1417 1399 }
1418 1400
1419 1401 /*
1420 1402 * Locates the child mount process represented by pid, outputs any io
1421 1403 * it may have, and returns its exit code.
1422 1404 * Sets the global exitcode if an error occurred.
1423 1405 */
1424 1406 int
1425 1407 cleanupkid(pid_t pid, int wstat)
1426 1408 {
1427 1409 vfsent_t *vp, *prevp;
1428 1410 int ret;
1429 1411
1430 1412 if (WIFEXITED(wstat)) /* this should always be true */
1431 1413 ret = WEXITSTATUS(wstat);
1432 1414 else
1433 1415 ret = 1; /* assume some kind of error */
1434 1416 if (ret) {
1435 1417 exitcode = 1;
1436 1418 failcnt++;
1437 1419 }
1438 1420
1439 1421 /*
1440 1422 * Find our child.
1441 1423 * This search gets smaller and smaller as children are cleaned
1442 1424 * up.
1443 1425 */
1444 1426 for (prevp = NULL, vp = vfsll; vp; vp = vp->next) {
1445 1427 if (vp->pid != pid) {
1446 1428 prevp = vp;
1447 1429 continue;
1448 1430 }
1449 1431 /*
1450 1432 * Found: let's remove it from this linked list.
1451 1433 */
1452 1434 if (prevp) {
1453 1435 prevp->next = vp->next;
1454 1436 vp->next = NULL;
1455 1437 }
1456 1438 break;
1457 1439 }
1458 1440
1459 1441 if (vp == NULL) {
1460 1442 /*
1461 1443 * This should never happen.
1462 1444 */
1463 1445 fprintf(stderr, gettext(
1464 1446 "%s: Unknown child %d\n"), myname, pid);
1465 1447 exitcode = 1;
1466 1448 return (ret);
↓ open down ↓ |
150 lines elided |
↑ open up ↑ |
1467 1449 }
1468 1450 doio(vp); /* Any output? */
1469 1451
1470 1452 if (vp->v.vfs_fstype &&
1471 1453 (strcmp(vp->v.vfs_fstype, MNTTYPE_LOFS) == 0)) {
1472 1454 lofscnt--;
1473 1455 if (ret)
1474 1456 lofsfail++;
1475 1457 }
1476 1458
1477 -#ifdef CACHEFS_BUG
1478 - if (vp->v.vfs_fstype && (strcmp(vp->v.vfs_fstype, "cachefs") == 0))
1479 - cachefs_running = 0;
1480 -#endif
1481 -
1482 1459 vp->exitcode = ret;
1483 1460 return (ret);
1484 1461 }
1485 1462
1486 1463
1487 1464 static vfsent_t zvmount = { 0 };
1488 1465
1489 1466 vfsent_t *
1490 1467 new_vfsent(struct vfstab *vin, int order)
1491 1468 {
1492 1469 vfsent_t *new;
1493 1470
1494 1471 new = (vfsent_t *)malloc(sizeof (*new));
1495 1472 if (new == NULL)
1496 1473 nomem();
1497 1474
1498 1475 *new = zvmount;
1499 1476 if (vin->vfs_special &&
1500 1477 (new->v.vfs_special = strdup(vin->vfs_special)) == NULL)
1501 1478 nomem();
1502 1479 if (vin->vfs_mountp &&
1503 1480 (new->v.vfs_mountp = strdup(vin->vfs_mountp)) == NULL)
1504 1481 nomem();
1505 1482 if (vin->vfs_fstype &&
1506 1483 (new->v.vfs_fstype = strdup(vin->vfs_fstype)) == NULL)
1507 1484 nomem();
1508 1485 /*
1509 1486 * If specific mount options were specified on the command
1510 1487 * line, then use those. Else, use the ones on the vfstab
1511 1488 * line, if any. In other words, specific options on the
1512 1489 * command line override those in /etc/vfstab.
1513 1490 */
1514 1491 if (oflg) {
1515 1492 if ((new->v.vfs_mntopts = strdup(specific_opts)) == NULL)
1516 1493 nomem();
1517 1494 } else if (vin->vfs_mntopts &&
1518 1495 (new->v.vfs_mntopts = strdup(vin->vfs_mntopts)) == NULL)
1519 1496 nomem();
1520 1497
1521 1498 new->order = order;
1522 1499 return (new);
1523 1500 }
1524 1501
1525 1502 /*
1526 1503 * Runs realpath on vp's mount point, records success or failure,
1527 1504 * resets the mount level based on the new realpath, and returns
1528 1505 * realpath()'s return value.
1529 1506 */
1530 1507 char *
1531 1508 setrpath(vfsent_t *vp)
1532 1509 {
1533 1510 char *rp;
1534 1511
1535 1512 if ((rp = realpath(vp->v.vfs_mountp, realdir)) == NULL)
1536 1513 vp->flag |= VRPFAILED;
1537 1514 else
1538 1515 vp->flag &= ~VRPFAILED;
1539 1516
1540 1517 if (vp->rpath)
1541 1518 free(vp->rpath);
1542 1519 if ((vp->rpath = strdup(realdir)) == NULL)
1543 1520 nomem();
1544 1521 vp->mlevel = fsgetmlevel(vp->rpath);
1545 1522 return (rp);
1546 1523 }
1547 1524
1548 1525
1549 1526 /*
1550 1527 * sort first by mlevel (1...N), then by vfstab order.
1551 1528 */
1552 1529 int
1553 1530 mlevelcmp(const void *a, const void *b)
1554 1531 {
1555 1532 vfsent_t *a1, *b1;
1556 1533 int lcmp;
1557 1534
1558 1535 a1 = *(vfsent_t **)a;
1559 1536 b1 = *(vfsent_t **)b;
1560 1537
1561 1538 lcmp = a1->mlevel - b1->mlevel;
1562 1539 if (lcmp == 0)
1563 1540 lcmp = a1->order - b1->order;
1564 1541 return (lcmp);
1565 1542 }
1566 1543
1567 1544 /* sort by vfstab order. 0..N */
1568 1545 static int
1569 1546 mordercmp(const void *a, const void *b)
1570 1547 {
1571 1548 vfsent_t *a1, *b1;
1572 1549
1573 1550 a1 = *(vfsent_t **)a;
1574 1551 b1 = *(vfsent_t **)b;
1575 1552 return (a1->order - b1->order);
1576 1553 }
1577 1554
1578 1555 /*
1579 1556 * cleanup the existing children and exit with an error
1580 1557 * if asig != 0.
1581 1558 */
1582 1559 void
1583 1560 cleanup(int asig)
1584 1561 {
1585 1562 while (nrun > 0 && (dowait() != -1))
1586 1563 ;
1587 1564
1588 1565 if (asig != 0)
1589 1566 exit(1);
1590 1567 }
1591 1568
1592 1569
1593 1570 int
1594 1571 check_fields(char *fstype, char *mountp)
1595 1572 {
1596 1573 struct stat64 stbuf;
1597 1574
1598 1575 if (strlen(fstype) > (size_t)FSTYPE_MAX) {
1599 1576 fprintf(stderr,
1600 1577 gettext("%s: FSType %s exceeds %d characters\n"),
1601 1578 myname, fstype, FSTYPE_MAX);
1602 1579 return (1);
1603 1580 }
1604 1581
1605 1582 if (mountp == NULL) {
1606 1583 fprintf(stderr,
1607 1584 gettext("%s: Mount point cannot be determined\n"),
1608 1585 myname);
1609 1586 return (1);
1610 1587 }
1611 1588 if (*mountp != '/') {
1612 1589 fprintf(stderr, gettext(
1613 1590 "%s: Mount point %s is not an absolute pathname.\n"),
1614 1591 myname, mountp);
1615 1592 return (1);
1616 1593 }
1617 1594 /*
1618 1595 * Don't do some of these checks if aflg because a mount point may
1619 1596 * not exist now, but will be mounted before we get to it.
1620 1597 * This is one of the quirks of "secondary mounting".
1621 1598 */
1622 1599 if (!aflg && stat64(mountp, &stbuf) < 0) {
1623 1600 if (errno == ENOENT || errno == ENOTDIR)
1624 1601 fprintf(stderr,
1625 1602 gettext("%s: Mount point %s does not exist.\n"),
1626 1603 myname, mountp);
1627 1604 else {
1628 1605 fprintf(stderr,
1629 1606 gettext("%s: Cannot stat mount point %s.\n"),
1630 1607 myname, mountp);
1631 1608 perror(myname);
1632 1609 }
1633 1610 return (1);
1634 1611 }
1635 1612 return (0);
1636 1613 }
1637 1614
1638 1615 void
1639 1616 nomem(void)
1640 1617 {
1641 1618 fprintf(stderr, gettext("%s: Out of memory\n"), myname);
1642 1619 while (nrun > 0 && (dowait() != -1))
1643 1620 ;
1644 1621 exit(1);
1645 1622 }
↓ open down ↓ |
154 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX