new copyright notice
[unix-history] / usr / src / sbin / mount / mount.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 1980, 1989 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * %sccs.include.redist.c%
6 */
7
8#ifndef lint
9char copyright[] =
10"@(#) Copyright (c) 1980, 1989 The Regents of the University of California.\n\
11 All rights reserved.\n";
12#endif /* not lint */
13
14#ifndef lint
15static char sccsid[] = "@(#)mount.c 5.37 (Berkeley) %G%";
16#endif /* not lint */
17
18#include "pathnames.h"
19#include <sys/param.h>
20#include <sys/file.h>
21#include <sys/time.h>
22#include <sys/wait.h>
23#include <fstab.h>
24#include <errno.h>
25#include <stdio.h>
26#include <signal.h>
27#include <string.h>
28#include <sys/mount.h>
29#ifdef NFS
30#include <sys/socket.h>
31#include <sys/socketvar.h>
32#include <netdb.h>
33#include <rpc/rpc.h>
34#include <rpc/pmap_clnt.h>
35#include <rpc/pmap_prot.h>
36#include <nfs/rpcv2.h>
37#include <nfs/nfsv2.h>
38#include <nfs/nfs.h>
39#endif
40
41#define DEFAULT_ROOTUID -2
42
43#define BADTYPE(type) \
44 (strcmp(type, FSTAB_RO) && strcmp(type, FSTAB_RW) && \
45 strcmp(type, FSTAB_RQ))
46#define SETTYPE(type) \
47 (!strcmp(type, FSTAB_RW) || !strcmp(type, FSTAB_RQ))
48
49int fake, verbose, updateflg, mnttype;
50char *mntname, **envp;
51char **vfslist, **makevfslist();
52
53#ifdef NFS
54int xdr_dir(), xdr_fh();
55char *getnfsargs();
56struct nfs_args nfsdefargs = {
57 (struct sockaddr *)0,
58 SOCK_DGRAM,
59 0,
60 (nfsv2fh_t *)0,
61 0,
62 NFS_WSIZE,
63 NFS_RSIZE,
64 NFS_TIMEO,
65 NFS_RETRANS,
66 (char *)0,
67};
68
69struct nfhret {
70 u_long stat;
71 nfsv2fh_t nfh;
72};
73#define DEF_RETRY 10000
74int retrycnt;
75#define BGRND 1
76#define ISBGRND 2
77int opflags = 0;
78#endif
79
80main(argc, argv, arge)
81 int argc;
82 char **argv;
83 char **arge;
84{
85 extern char *optarg;
86 extern int optind;
87 register struct fstab *fs;
88 int all, ch, rval, flags, ret, pid, i;
89 long mntsize;
90 struct statfs *mntbuf, *getmntpt();
91 char *type, *options = NULL;
92 FILE *pidfile;
93
94 envp = arge;
95 all = 0;
96 type = NULL;
97 mnttype = MOUNT_UFS;
98 mntname = "ufs";
99 while ((ch = getopt(argc, argv, "afrwuvt:o:")) != EOF)
100 switch((char)ch) {
101 case 'a':
102 all = 1;
103 break;
104 case 'f':
105 fake = 1;
106 break;
107 case 'r':
108 type = FSTAB_RO;
109 break;
110 case 'u':
111 updateflg = MNT_UPDATE;
112 break;
113 case 'v':
114 verbose = 1;
115 break;
116 case 'w':
117 type = FSTAB_RW;
118 break;
119 case 'o':
120 options = optarg;
121 break;
122 case 't':
123 vfslist = makevfslist(optarg);
124 mnttype = getmnttype(optarg);
125 break;
126 case '?':
127 default:
128 usage();
129 /* NOTREACHED */
130 }
131 argc -= optind;
132 argv += optind;
133
134 /* NOSTRICT */
135
136 if (all) {
137 rval = 0;
138 while (fs = getfsent()) {
139 if (BADTYPE(fs->fs_type))
140 continue;
141 if (badvfsname(fs->fs_vfstype, vfslist))
142 continue;
143 /* `/' is special, it's always mounted */
144 if (!strcmp(fs->fs_file, "/"))
145 flags = MNT_UPDATE;
146 else
147 flags = updateflg;
148 mnttype = getmnttype(fs->fs_vfstype);
149 rval |= mountfs(fs->fs_spec, fs->fs_file, flags,
150 type, options, fs->fs_mntops);
151 }
152 exit(rval);
153 }
154
155 if (argc == 0) {
156 if (verbose || fake || type)
157 usage();
158 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
159 fprintf(stderr,
160 "mount: cannot get mount information\n");
161 exit(1);
162 }
163 for (i = 0; i < mntsize; i++) {
164 if (badvfstype(mntbuf[i].f_type, vfslist))
165 continue;
166 prmount(mntbuf[i].f_mntfromname, mntbuf[i].f_mntonname,
167 mntbuf[i].f_flags);
168 }
169 exit(0);
170 }
171
172 if (argc == 1 && updateflg) {
173 if ((mntbuf = getmntpt(*argv)) == NULL) {
174 fprintf(stderr,
175 "mount: unknown special file or file system %s.\n",
176 *argv);
177 exit(1);
178 }
179 mnttype = mntbuf->f_type;
180 if (!strcmp(mntbuf->f_mntfromname, "root_device")) {
181 fs = getfsfile("/");
182 strcpy(mntbuf->f_mntfromname, fs->fs_spec);
183 }
184 ret = mountfs(mntbuf->f_mntfromname, mntbuf->f_mntonname,
185 updateflg, type, options, (char *)NULL);
186 } else if (argc == 1) {
187 if (!(fs = getfsfile(*argv)) && !(fs = getfsspec(*argv))) {
188 fprintf(stderr,
189 "mount: unknown special file or file system %s.\n",
190 *argv);
191 exit(1);
192 }
193 if (BADTYPE(fs->fs_type)) {
194 fprintf(stderr,
195 "mount: %s has unknown file system type.\n", *argv);
196 exit(1);
197 }
198 mnttype = getmnttype(fs->fs_vfstype);
199 ret = mountfs(fs->fs_spec, fs->fs_file, updateflg,
200 type, options, fs->fs_mntops);
201 } else if (argc != 2) {
202 usage();
203 ret = 1;
204 } else {
205 ret = mountfs(argv[0], argv[1], updateflg, type, options,
206 (char *)NULL);
207 }
208 if ((pidfile = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
209 pid = 0;
210 fscanf(pidfile, "%d", &pid);
211 fclose(pidfile);
212 if (pid > 0)
213 kill(pid, SIGHUP);
214 }
215 exit (ret);
216}
217
218mountfs(spec, name, flags, type, options, mntopts)
219 char *spec, *name, *type, *options, *mntopts;
220 int flags;
221{
222 extern int errno;
223 union wait status;
224 pid_t pid;
225 int argc, i;
226 struct ufs_args args;
227 struct nfs_args nfsargs;
228 char *argp, *argv[50];
229 char execname[MAXPATHLEN + 1], flagval[12];
230
231 nfsargs = nfsdefargs;
232 if (mntopts)
233 getstdopts(mntopts, &flags);
234 if (options)
235 getstdopts(options, &flags);
236 if (type)
237 getstdopts(type, &flags);
238 switch (mnttype) {
239 case MOUNT_UFS:
240 if (mntopts)
241 getufsopts(mntopts, &flags);
242 if (options)
243 getufsopts(options, &flags);
244 args.fspec = spec;
245 args.exroot = DEFAULT_ROOTUID;
246 if (flags & MNT_RDONLY)
247 args.exflags = MNT_EXRDONLY;
248 else
249 args.exflags = 0;
250 argp = (caddr_t)&args;
251 break;
252
253#ifdef NFS
254 case MOUNT_NFS:
255 retrycnt = DEF_RETRY;
256 if (mntopts)
257 getnfsopts(mntopts, &nfsargs, &opflags, &retrycnt);
258 if (options)
259 getnfsopts(options, &nfsargs, &opflags, &retrycnt);
260 if (argp = getnfsargs(spec, &nfsargs))
261 break;
262 return (1);
263#endif /* NFS */
264
265 case MOUNT_MFS:
266 default:
267 argv[0] = mntname;
268 argc = 1;
269 if (flags) {
270 argv[argc++] = "-F";
271 sprintf(flagval, "%d", flags);
272 argv[argc++] = flagval;
273 }
274 if (mntopts)
275 argc += getexecopts(mntopts, &argv[argc]);
276 if (options)
277 argc += getexecopts(options, &argv[argc]);
278 argv[argc++] = spec;
279 argv[argc++] = name;
280 argv[argc++] = NULL;
281 sprintf(execname, "%s/mount_%s", _PATH_EXECDIR, mntname);
282 if (verbose) {
283 (void)printf("exec: %s", execname);
284 for (i = 1; i < argc - 1; i++)
285 (void)printf(" %s", argv[i]);
286 (void)printf("\n");
287 }
288 if (fake)
289 break;
290 if (pid = vfork()) {
291 if (pid == -1) {
292 perror("mount: vfork starting file system");
293 return (1);
294 }
295 if (waitpid(pid, &status, 0) != -1 &&
296 WIFEXITED(status) &&
297 WEXITSTATUS(status) != 0)
298 return (WEXITSTATUS(status));
299 spec = mntname;
300 goto out;
301 }
302 execve(execname, argv, envp);
303 fprintf(stderr, "mount: cannot exec %s for %s: ",
304 execname, name);
305 perror((char *)NULL);
306 exit (1);
307 /* NOTREACHED */
308
309 }
310 if (!fake && mount(mnttype, name, flags, argp)) {
311 if (opflags & ISBGRND)
312 exit(1);
313 fprintf(stderr, "%s on %s: ", spec, name);
314 switch (errno) {
315 case EMFILE:
316 fprintf(stderr, "Mount table full\n");
317 break;
318 case EINVAL:
319 if (flags & MNT_UPDATE)
320 fprintf(stderr, "Specified device does %s\n",
321 "not match mounted device");
322 else
323 fprintf(stderr, "Bogus super block\n");
324 break;
325 case EOPNOTSUPP:
326 fprintf(stderr, "Operation not supported\n");
327 break;
328 default:
329 perror((char *)NULL);
330 break;
331 }
332 return(1);
333 }
334
335out:
336 if (verbose)
337 prmount(spec, name, flags);
338
339 if (opflags & ISBGRND)
340 exit(1);
341 return(0);
342}
343
344static
345prmount(spec, name, flags)
346 char *spec, *name;
347 register short flags;
348{
349 register int first;
350
351 if (opflags & ISBGRND)
352 return;
353 (void)printf("%s on %s", spec, name);
354 if (!(flags & MNT_VISFLAGMASK)) {
355 (void)printf("\n");
356 return;
357 }
358 first = 0;
359#define PR(msg) (void)printf("%s%s", !first++ ? " (" : ", ", msg)
360 if (flags & MNT_RDONLY)
361 PR("read-only");
362 if (flags & MNT_NOEXEC)
363 PR("noexec");
364 if (flags & MNT_NOSUID)
365 PR("nosuid");
366 if (flags & MNT_NODEV)
367 PR("nodev");
368 if (flags & MNT_SYNCHRONOUS)
369 PR("synchronous");
370 if (flags & MNT_QUOTA)
371 PR("with quotas");
372 if (flags & MNT_LOCAL)
373 PR("local");
374 if (flags & MNT_EXPORTED)
375 if (flags & MNT_EXRDONLY)
376 PR("NFS exported read-only");
377 else
378 PR("NFS exported");
379 (void)printf(")\n");
380}
381
382getmnttype(fstype)
383 char *fstype;
384{
385
386 mntname = fstype;
387 if (!strcmp(fstype, "ufs"))
388 return (MOUNT_UFS);
389 if (!strcmp(fstype, "nfs"))
390 return (MOUNT_NFS);
391 if (!strcmp(fstype, "mfs"))
392 return (MOUNT_MFS);
393 return (0);
394}
395
396usage()
397{
398
399 fprintf(stderr, "usage:\n mount %s %s\n mount %s\n mount %s\n",
400 "[ -frwu ] [ -t nfs | ufs | external_type ]",
401 "[ -o options ] special node",
402 "[ -afrwu ] [ -t nfs | ufs | external_type ]",
403 "[ -frwu ] special | node");
404 exit(1);
405}
406
407getstdopts(options, flagp)
408 char *options;
409 int *flagp;
410{
411 register char *opt;
412 int negative;
413 char optbuf[BUFSIZ];
414
415 (void)strcpy(optbuf, options);
416 for (opt = strtok(optbuf, ","); opt; opt = strtok((char *)NULL, ",")) {
417 if (opt[0] == 'n' && opt[1] == 'o') {
418 negative++;
419 opt += 2;
420 } else {
421 negative = 0;
422 }
423 if (!negative && !strcasecmp(opt, FSTAB_RO)) {
424 *flagp |= MNT_RDONLY;
425 continue;
426 }
427 if (!negative && !strcasecmp(opt, FSTAB_RW)) {
428 *flagp &= ~MNT_RDONLY;
429 continue;
430 }
431 if (!strcasecmp(opt, "exec")) {
432 if (negative)
433 *flagp |= MNT_NOEXEC;
434 else
435 *flagp &= ~MNT_NOEXEC;
436 continue;
437 }
438 if (!strcasecmp(opt, "suid")) {
439 if (negative)
440 *flagp |= MNT_NOSUID;
441 else
442 *flagp &= ~MNT_NOSUID;
443 continue;
444 }
445 if (!strcasecmp(opt, "dev")) {
446 if (negative)
447 *flagp |= MNT_NODEV;
448 else
449 *flagp &= ~MNT_NODEV;
450 continue;
451 }
452 if (!strcasecmp(opt, "synchronous")) {
453 if (!negative)
454 *flagp |= MNT_SYNCHRONOUS;
455 else
456 *flagp &= ~MNT_SYNCHRONOUS;
457 continue;
458 }
459 }
460}
461
462/* ARGSUSED */
463getufsopts(options, flagp)
464 char *options;
465 int *flagp;
466{
467 return;
468}
469
470getexecopts(options, argv)
471 char *options;
472 char **argv;
473{
474 register int argc = 0;
475 register char *opt;
476
477 for (opt = strtok(options, ","); opt; opt = strtok((char *)NULL, ",")) {
478 if (opt[0] != '-')
479 continue;
480 argv[argc++] = opt;
481 if (opt[2] == '\0' || opt[2] != '=')
482 continue;
483 opt[2] = '\0';
484 argv[argc++] = &opt[3];
485 }
486 return (argc);
487}
488
489struct statfs *
490getmntpt(name)
491 char *name;
492{
493 long mntsize;
494 register long i;
495 struct statfs *mntbuf;
496
497 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
498 for (i = 0; i < mntsize; i++) {
499 if (!strcmp(mntbuf[i].f_mntfromname, name) ||
500 !strcmp(mntbuf[i].f_mntonname, name))
501 return (&mntbuf[i]);
502 }
503 return ((struct statfs *)0);
504}
505
506static int skipvfs;
507
508badvfstype(vfstype, vfslist)
509 short vfstype;
510 char **vfslist;
511{
512
513 if (vfslist == 0)
514 return(0);
515 while (*vfslist) {
516 if (vfstype == getmnttype(*vfslist))
517 return(skipvfs);
518 vfslist++;
519 }
520 return (!skipvfs);
521}
522
523badvfsname(vfsname, vfslist)
524 char *vfsname;
525 char **vfslist;
526{
527
528 if (vfslist == 0)
529 return(0);
530 while (*vfslist) {
531 if (strcmp(vfsname, *vfslist) == 0)
532 return(skipvfs);
533 vfslist++;
534 }
535 return (!skipvfs);
536}
537
538char **
539makevfslist(fslist)
540 char *fslist;
541{
542 register char **av, *nextcp;
543 register int i;
544
545 if (fslist == NULL)
546 return (NULL);
547 if (fslist[0] == 'n' && fslist[1] == 'o') {
548 fslist += 2;
549 skipvfs = 1;
550 }
551 for (i = 0, nextcp = fslist; *nextcp; nextcp++)
552 if (*nextcp == ',')
553 i++;
554 av = (char **)malloc((size_t)(i+2) * sizeof(char *));
555 if (av == NULL)
556 return (NULL);
557 nextcp = fslist;
558 i = 0;
559 av[i++] = nextcp;
560 while (nextcp = index(nextcp, ',')) {
561 *nextcp++ = '\0';
562 av[i++] = nextcp;
563 }
564 av[i++] = 0;
565 return (av);
566}
567
568#ifdef NFS
569/*
570 * Handle the getoption arg.
571 * Essentially update "opflags", "retrycnt" and "nfsargs"
572 */
573getnfsopts(optarg, nfsargsp, opflagsp, retrycntp)
574 char *optarg;
575 struct nfs_args *nfsargsp;
576 int *opflagsp;
577 int *retrycntp;
578{
579 register char *cp, *nextcp;
580 int num;
581 char *nump;
582
583 cp = optarg;
584 while (cp != NULL && *cp != '\0') {
585 if ((nextcp = index(cp, ',')) != NULL)
586 *nextcp++ = '\0';
587 if ((nump = index(cp, '=')) != NULL) {
588 *nump++ = '\0';
589 num = atoi(nump);
590 } else
591 num = -1;
592 /*
593 * Just test for a string match and do it
594 */
595 if (!strcmp(cp, "bg")) {
596 *opflagsp |= BGRND;
597 } else if (!strcmp(cp, "soft")) {
598 nfsargsp->flags |= NFSMNT_SOFT;
599 } else if (!strcmp(cp, "intr")) {
600 nfsargsp->flags |= NFSMNT_INT;
601 } else if (!strcmp(cp, "tcp")) {
602 nfsargsp->sotype = SOCK_STREAM;
603 } else if (!strcmp(cp, "noconn")) {
604 nfsargsp->flags |= NFSMNT_NOCONN;
605 } else if (!strcmp(cp, "retry") && num > 0) {
606 *retrycntp = num;
607 } else if (!strcmp(cp, "rsize") && num > 0) {
608 nfsargsp->rsize = num;
609 nfsargsp->flags |= NFSMNT_RSIZE;
610 } else if (!strcmp(cp, "wsize") && num > 0) {
611 nfsargsp->wsize = num;
612 nfsargsp->flags |= NFSMNT_WSIZE;
613 } else if (!strcmp(cp, "timeo") && num > 0) {
614 nfsargsp->timeo = num;
615 nfsargsp->flags |= NFSMNT_TIMEO;
616 } else if (!strcmp(cp, "retrans") && num > 0) {
617 nfsargsp->retrans = num;
618 nfsargsp->flags |= NFSMNT_RETRANS;
619 }
620 cp = nextcp;
621 }
622}
623
624char *
625getnfsargs(spec, nfsargsp)
626 char *spec;
627 struct nfs_args *nfsargsp;
628{
629 extern int errno;
630 register CLIENT *clp;
631 struct hostent *hp;
632 static struct sockaddr_in saddr;
633 struct timeval pertry, try;
634 enum clnt_stat clnt_stat;
635 int so = RPC_ANYSOCK;
636 char *hostp, *delimp;
637 u_short tport;
638 static struct nfhret nfhret;
639 static char nam[MNAMELEN + 1];
640
641 strncpy(nam, spec, MNAMELEN);
642 nam[MNAMELEN] = '\0';
643 if ((delimp = index(spec, '@')) != NULL) {
644 hostp = delimp + 1;
645 } else if ((delimp = index(spec, ':')) != NULL) {
646 hostp = spec;
647 spec = delimp + 1;
648 } else {
649 fprintf(stderr,
650 "No <host>:<dirpath> or <dirpath>@<host> spec\n");
651 return (0);
652 }
653 *delimp = '\0';
654 if ((hp = gethostbyname(hostp)) == NULL) {
655 fprintf(stderr, "Can't get net id for host\n");
656 return (0);
657 }
658 bcopy(hp->h_addr, (caddr_t)&saddr.sin_addr, hp->h_length);
659 nfhret.stat = EACCES; /* Mark not yet successful */
660 while (retrycnt > 0) {
661 saddr.sin_family = AF_INET;
662 saddr.sin_port = htons(PMAPPORT);
663 if ((tport = pmap_getport(&saddr, RPCPROG_NFS,
664 NFS_VER2, IPPROTO_UDP)) == 0) {
665 if ((opflags & ISBGRND) == 0)
666 clnt_pcreateerror("NFS Portmap");
667 } else {
668 saddr.sin_port = 0;
669 pertry.tv_sec = 10;
670 pertry.tv_usec = 0;
671 if ((clp = clntudp_create(&saddr, RPCPROG_MNT,
672 RPCMNT_VER1, pertry, &so)) == NULL) {
673 if ((opflags & ISBGRND) == 0)
674 clnt_pcreateerror("Cannot MNT PRC");
675 } else {
676 clp->cl_auth = authunix_create_default();
677 try.tv_sec = 10;
678 try.tv_usec = 0;
679 clnt_stat = clnt_call(clp, RPCMNT_MOUNT,
680 xdr_dir, spec, xdr_fh, &nfhret, try);
681 if (clnt_stat != RPC_SUCCESS) {
682 if ((opflags & ISBGRND) == 0)
683 clnt_perror(clp, "Bad MNT RPC");
684 } else {
685 auth_destroy(clp->cl_auth);
686 clnt_destroy(clp);
687 retrycnt = 0;
688 }
689 }
690 }
691 if (--retrycnt > 0) {
692 if (opflags & BGRND) {
693 opflags &= ~BGRND;
694 if (fork())
695 return (0);
696 else
697 opflags |= ISBGRND;
698 }
699 sleep(10);
700 }
701 }
702 if (nfhret.stat) {
703 if (opflags & ISBGRND)
704 exit(1);
705 fprintf(stderr, "Can't access %s: ", spec);
706 errno = nfhret.stat;
707 perror((char *)NULL);
708 return (0);
709 }
710 saddr.sin_port = htons(tport);
711 nfsargsp->addr = (struct sockaddr *) &saddr;
712 nfsargsp->fh = &nfhret.nfh;
713 nfsargsp->hostname = nam;
714 return ((caddr_t)nfsargsp);
715}
716
717/*
718 * xdr routines for mount rpc's
719 */
720xdr_dir(xdrsp, dirp)
721 XDR *xdrsp;
722 char *dirp;
723{
724 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
725}
726
727xdr_fh(xdrsp, np)
728 XDR *xdrsp;
729 struct nfhret *np;
730{
731 if (!xdr_u_long(xdrsp, &(np->stat)))
732 return (0);
733 if (np->stat)
734 return (1);
735 return (xdr_opaque(xdrsp, (caddr_t)&(np->nfh), NFSX_FH));
736}
737#endif /* NFS */