xref: /onnv-gate/usr/src/cmd/fs.d/lofs/mount/mount.c (revision 8155:508fa30fdb35)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*8155STim.Haley@Sun.COM  * Common Development and Distribution License (the "License").
6*8155STim.Haley@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate 
220Sstevel@tonic-gate /*
23*8155STim.Haley@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #define	LOFS
280Sstevel@tonic-gate #define	MNTTYPE_LOFS "lofs"
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <stdlib.h>
310Sstevel@tonic-gate #include <string.h>
320Sstevel@tonic-gate #include <libintl.h>
330Sstevel@tonic-gate #include <errno.h>
340Sstevel@tonic-gate #include <sys/fstyp.h>
350Sstevel@tonic-gate #include <sys/fsid.h>
360Sstevel@tonic-gate #include <sys/mntent.h>
370Sstevel@tonic-gate #include <sys/mnttab.h>
380Sstevel@tonic-gate #include <sys/mount.h>
390Sstevel@tonic-gate #include <sys/signal.h>
400Sstevel@tonic-gate #include <sys/stat.h>
410Sstevel@tonic-gate #include <fslib.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #define	RET_OK		0
44*8155STim.Haley@Sun.COM /*
45*8155STim.Haley@Sun.COM  * /sbin/mount and the fs-local method understand this exit code to
46*8155STim.Haley@Sun.COM  * mean that all the mount failures were related to lofs mounts. Since
47*8155STim.Haley@Sun.COM  * this program only attempts to mount lofs file systems, when it fails
48*8155STim.Haley@Sun.COM  * it returns this exit status.
49*8155STim.Haley@Sun.COM  */
50*8155STim.Haley@Sun.COM #define	RET_ERR		111
510Sstevel@tonic-gate 
520Sstevel@tonic-gate static void usage(void);
530Sstevel@tonic-gate 
540Sstevel@tonic-gate static char  optbuf[MAX_MNTOPT_STR] = { '\0', };
550Sstevel@tonic-gate static int   optsize = 0;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate static char fstype[] = MNTTYPE_LOFS;
580Sstevel@tonic-gate 
590Sstevel@tonic-gate /*
600Sstevel@tonic-gate  * usage: mount [-Ormq] [-o options] special mountp
610Sstevel@tonic-gate  *
620Sstevel@tonic-gate  * This mount program is exec'ed by /usr/sbin/mount if '-F lofs' is
630Sstevel@tonic-gate  * specified.
640Sstevel@tonic-gate  */
650Sstevel@tonic-gate int
main(int argc,char * argv[])660Sstevel@tonic-gate main(int argc, char *argv[])
670Sstevel@tonic-gate {
680Sstevel@tonic-gate 	int c;
690Sstevel@tonic-gate 	char *special;		/* Entity being mounted */
700Sstevel@tonic-gate 	char *mountp;		/* Entity being mounted on */
710Sstevel@tonic-gate 	char *savedoptbuf;
720Sstevel@tonic-gate 	char *myname;
730Sstevel@tonic-gate 	char typename[64];
740Sstevel@tonic-gate 	int flags = 0;
750Sstevel@tonic-gate 	int errflag = 0;
760Sstevel@tonic-gate 	int qflg = 0;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	myname = strrchr(argv[0], '/');
790Sstevel@tonic-gate 	myname = myname ? myname+1 : argv[0];
800Sstevel@tonic-gate 	(void) snprintf(typename, sizeof (typename), "%s %s", fstype, myname);
810Sstevel@tonic-gate 	argv[0] = typename;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "o:rmOq")) != EOF) {
840Sstevel@tonic-gate 		switch (c) {
850Sstevel@tonic-gate 		case '?':
860Sstevel@tonic-gate 			errflag++;
870Sstevel@tonic-gate 			break;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 		case 'o':
900Sstevel@tonic-gate 			if (strlcpy(optbuf, optarg, sizeof (optbuf)) >=
910Sstevel@tonic-gate 			    sizeof (optbuf)) {
920Sstevel@tonic-gate 				(void) fprintf(stderr,
930Sstevel@tonic-gate 				    gettext("%s: Invalid argument: %s\n"),
940Sstevel@tonic-gate 				    myname, optarg);
950Sstevel@tonic-gate 				return (2);
960Sstevel@tonic-gate 			}
970Sstevel@tonic-gate 			optsize = strlen(optbuf);
980Sstevel@tonic-gate 			break;
990Sstevel@tonic-gate 		case 'O':
1000Sstevel@tonic-gate 			flags |= MS_OVERLAY;
1010Sstevel@tonic-gate 			break;
1020Sstevel@tonic-gate 		case 'r':
1030Sstevel@tonic-gate 			flags |= MS_RDONLY;
1040Sstevel@tonic-gate 			break;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 		case 'm':
1070Sstevel@tonic-gate 			flags |= MS_NOMNTTAB;
1080Sstevel@tonic-gate 			break;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 		case 'q':
1110Sstevel@tonic-gate 			qflg = 1;
1120Sstevel@tonic-gate 			break;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 		default:
1150Sstevel@tonic-gate 			usage();
1160Sstevel@tonic-gate 		}
1170Sstevel@tonic-gate 	}
1180Sstevel@tonic-gate 	if ((argc - optind != 2) || errflag) {
1190Sstevel@tonic-gate 		usage();
1200Sstevel@tonic-gate 	}
1210Sstevel@tonic-gate 	special = argv[argc - 2];
1220Sstevel@tonic-gate 	mountp = argv[argc - 1];
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	if ((savedoptbuf = strdup(optbuf)) == NULL) {
1250Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: out of memory\n"),
1260Sstevel@tonic-gate 		    myname);
1270Sstevel@tonic-gate 		exit(2);
1280Sstevel@tonic-gate 	}
1290Sstevel@tonic-gate 	if (mount(special, mountp, flags | MS_OPTIONSTR, fstype, NULL, 0,
1300Sstevel@tonic-gate 	    optbuf, MAX_MNTOPT_STR)) {
1310Sstevel@tonic-gate 		(void) fprintf(stderr, "mount: ");
1320Sstevel@tonic-gate 		perror(special);
1330Sstevel@tonic-gate 		exit(RET_ERR);
1340Sstevel@tonic-gate 	}
1350Sstevel@tonic-gate 	if (optsize && !qflg)
1360Sstevel@tonic-gate 		cmp_requested_to_actual_options(savedoptbuf, optbuf,
1370Sstevel@tonic-gate 		    special, mountp);
1380Sstevel@tonic-gate 	return (0);
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate void
usage(void)1420Sstevel@tonic-gate usage(void)
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate 	(void) fprintf(stderr,
1450Sstevel@tonic-gate 	    "Usage: mount [-Ormq] [-o options] special mountpoint\n");
1460Sstevel@tonic-gate 	exit(RET_ERR);
1470Sstevel@tonic-gate }
148