xref: /titanic_50/usr/src/cmd/fs.d/lofs/mount/mount.c (revision a38ddfee9c8c6b6c5a2947ff52fd2338362a4444)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*a38ddfeeSTim Haley  * Common Development and Distribution License (the "License").
6*a38ddfeeSTim Haley  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate /*
23*a38ddfeeSTim Haley  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #define	LOFS
287c478bd9Sstevel@tonic-gate #define	MNTTYPE_LOFS "lofs"
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <libintl.h>
337c478bd9Sstevel@tonic-gate #include <errno.h>
347c478bd9Sstevel@tonic-gate #include <sys/fstyp.h>
357c478bd9Sstevel@tonic-gate #include <sys/fsid.h>
367c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
377c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
387c478bd9Sstevel@tonic-gate #include <sys/mount.h>
397c478bd9Sstevel@tonic-gate #include <sys/signal.h>
407c478bd9Sstevel@tonic-gate #include <sys/stat.h>
417c478bd9Sstevel@tonic-gate #include <fslib.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #define	RET_OK		0
44*a38ddfeeSTim Haley /*
45*a38ddfeeSTim Haley  * /sbin/mount and the fs-local method understand this exit code to
46*a38ddfeeSTim Haley  * mean that all the mount failures were related to lofs mounts. Since
47*a38ddfeeSTim Haley  * this program only attempts to mount lofs file systems, when it fails
48*a38ddfeeSTim Haley  * it returns this exit status.
49*a38ddfeeSTim Haley  */
50*a38ddfeeSTim Haley #define	RET_ERR		111
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate static void usage(void);
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate static char  optbuf[MAX_MNTOPT_STR] = { '\0', };
557c478bd9Sstevel@tonic-gate static int   optsize = 0;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate static char fstype[] = MNTTYPE_LOFS;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * usage: mount [-Ormq] [-o options] special mountp
617c478bd9Sstevel@tonic-gate  *
627c478bd9Sstevel@tonic-gate  * This mount program is exec'ed by /usr/sbin/mount if '-F lofs' is
637c478bd9Sstevel@tonic-gate  * specified.
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])667c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	int c;
697c478bd9Sstevel@tonic-gate 	char *special;		/* Entity being mounted */
707c478bd9Sstevel@tonic-gate 	char *mountp;		/* Entity being mounted on */
717c478bd9Sstevel@tonic-gate 	char *savedoptbuf;
727c478bd9Sstevel@tonic-gate 	char *myname;
737c478bd9Sstevel@tonic-gate 	char typename[64];
747c478bd9Sstevel@tonic-gate 	int flags = 0;
757c478bd9Sstevel@tonic-gate 	int errflag = 0;
767c478bd9Sstevel@tonic-gate 	int qflg = 0;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	myname = strrchr(argv[0], '/');
797c478bd9Sstevel@tonic-gate 	myname = myname ? myname+1 : argv[0];
807c478bd9Sstevel@tonic-gate 	(void) snprintf(typename, sizeof (typename), "%s %s", fstype, myname);
817c478bd9Sstevel@tonic-gate 	argv[0] = typename;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "o:rmOq")) != EOF) {
847c478bd9Sstevel@tonic-gate 		switch (c) {
857c478bd9Sstevel@tonic-gate 		case '?':
867c478bd9Sstevel@tonic-gate 			errflag++;
877c478bd9Sstevel@tonic-gate 			break;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 		case 'o':
907c478bd9Sstevel@tonic-gate 			if (strlcpy(optbuf, optarg, sizeof (optbuf)) >=
917c478bd9Sstevel@tonic-gate 			    sizeof (optbuf)) {
927c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
937c478bd9Sstevel@tonic-gate 				    gettext("%s: Invalid argument: %s\n"),
947c478bd9Sstevel@tonic-gate 				    myname, optarg);
957c478bd9Sstevel@tonic-gate 				return (2);
967c478bd9Sstevel@tonic-gate 			}
977c478bd9Sstevel@tonic-gate 			optsize = strlen(optbuf);
987c478bd9Sstevel@tonic-gate 			break;
997c478bd9Sstevel@tonic-gate 		case 'O':
1007c478bd9Sstevel@tonic-gate 			flags |= MS_OVERLAY;
1017c478bd9Sstevel@tonic-gate 			break;
1027c478bd9Sstevel@tonic-gate 		case 'r':
1037c478bd9Sstevel@tonic-gate 			flags |= MS_RDONLY;
1047c478bd9Sstevel@tonic-gate 			break;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 		case 'm':
1077c478bd9Sstevel@tonic-gate 			flags |= MS_NOMNTTAB;
1087c478bd9Sstevel@tonic-gate 			break;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 		case 'q':
1117c478bd9Sstevel@tonic-gate 			qflg = 1;
1127c478bd9Sstevel@tonic-gate 			break;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 		default:
1157c478bd9Sstevel@tonic-gate 			usage();
1167c478bd9Sstevel@tonic-gate 		}
1177c478bd9Sstevel@tonic-gate 	}
1187c478bd9Sstevel@tonic-gate 	if ((argc - optind != 2) || errflag) {
1197c478bd9Sstevel@tonic-gate 		usage();
1207c478bd9Sstevel@tonic-gate 	}
1217c478bd9Sstevel@tonic-gate 	special = argv[argc - 2];
1227c478bd9Sstevel@tonic-gate 	mountp = argv[argc - 1];
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	if ((savedoptbuf = strdup(optbuf)) == NULL) {
1257c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: out of memory\n"),
1267c478bd9Sstevel@tonic-gate 		    myname);
1277c478bd9Sstevel@tonic-gate 		exit(2);
1287c478bd9Sstevel@tonic-gate 	}
1297c478bd9Sstevel@tonic-gate 	if (mount(special, mountp, flags | MS_OPTIONSTR, fstype, NULL, 0,
1307c478bd9Sstevel@tonic-gate 	    optbuf, MAX_MNTOPT_STR)) {
1317c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "mount: ");
1327c478bd9Sstevel@tonic-gate 		perror(special);
1337c478bd9Sstevel@tonic-gate 		exit(RET_ERR);
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 	if (optsize && !qflg)
1367c478bd9Sstevel@tonic-gate 		cmp_requested_to_actual_options(savedoptbuf, optbuf,
1377c478bd9Sstevel@tonic-gate 		    special, mountp);
1387c478bd9Sstevel@tonic-gate 	return (0);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate void
usage(void)1427c478bd9Sstevel@tonic-gate usage(void)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
1457c478bd9Sstevel@tonic-gate 	    "Usage: mount [-Ormq] [-o options] special mountpoint\n");
1467c478bd9Sstevel@tonic-gate 	exit(RET_ERR);
1477c478bd9Sstevel@tonic-gate }
148