xref: /illumos-gate/usr/src/cmd/fs.d/lofs/mount/mount.c (revision 942c5e3c2dd127463517e5cc1694ee94ca45e021)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 #pragma ident	"%Z%%M%	%I%	%E% SMI"
23 
24 /*
25  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
26  * Use is subject to license terms.
27  */
28 
29 #define	LOFS
30 #define	MNTTYPE_LOFS "lofs"
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <libintl.h>
35 #include <errno.h>
36 #include <sys/fstyp.h>
37 #include <sys/fsid.h>
38 #include <sys/mntent.h>
39 #include <sys/mnttab.h>
40 #include <sys/mount.h>
41 #include <sys/signal.h>
42 #include <sys/stat.h>
43 #include <fslib.h>
44 
45 #define	RET_OK		0
46 #define	RET_ERR		33
47 
48 static void usage(void);
49 
50 static char  optbuf[MAX_MNTOPT_STR] = { '\0', };
51 static int   optsize = 0;
52 
53 static char fstype[] = MNTTYPE_LOFS;
54 
55 /*
56  * usage: mount [-Ormq] [-o options] special mountp
57  *
58  * This mount program is exec'ed by /usr/sbin/mount if '-F lofs' is
59  * specified.
60  */
61 int
62 main(int argc, char *argv[])
63 {
64 	int c;
65 	char *special;		/* Entity being mounted */
66 	char *mountp;		/* Entity being mounted on */
67 	char *savedoptbuf;
68 	char *myname;
69 	char typename[64];
70 	int flags = 0;
71 	int errflag = 0;
72 	int qflg = 0;
73 
74 	myname = strrchr(argv[0], '/');
75 	myname = myname ? myname+1 : argv[0];
76 	(void) snprintf(typename, sizeof (typename), "%s %s", fstype, myname);
77 	argv[0] = typename;
78 
79 	while ((c = getopt(argc, argv, "o:rmOq")) != EOF) {
80 		switch (c) {
81 		case '?':
82 			errflag++;
83 			break;
84 
85 		case 'o':
86 			if (strlcpy(optbuf, optarg, sizeof (optbuf)) >=
87 			    sizeof (optbuf)) {
88 				(void) fprintf(stderr,
89 				    gettext("%s: Invalid argument: %s\n"),
90 				    myname, optarg);
91 				return (2);
92 			}
93 			optsize = strlen(optbuf);
94 			break;
95 		case 'O':
96 			flags |= MS_OVERLAY;
97 			break;
98 		case 'r':
99 			flags |= MS_RDONLY;
100 			break;
101 
102 		case 'm':
103 			flags |= MS_NOMNTTAB;
104 			break;
105 
106 		case 'q':
107 			qflg = 1;
108 			break;
109 
110 		default:
111 			usage();
112 		}
113 	}
114 	if ((argc - optind != 2) || errflag) {
115 		usage();
116 	}
117 	special = argv[argc - 2];
118 	mountp = argv[argc - 1];
119 
120 	if ((savedoptbuf = strdup(optbuf)) == NULL) {
121 		(void) fprintf(stderr, gettext("%s: out of memory\n"),
122 		    myname);
123 		exit(2);
124 	}
125 	if (mount(special, mountp, flags | MS_OPTIONSTR, fstype, NULL, 0,
126 	    optbuf, MAX_MNTOPT_STR)) {
127 		(void) fprintf(stderr, "mount: ");
128 		perror(special);
129 		exit(RET_ERR);
130 	}
131 	if (optsize && !qflg)
132 		cmp_requested_to_actual_options(savedoptbuf, optbuf,
133 		    special, mountp);
134 	return (0);
135 }
136 
137 void
138 usage(void)
139 {
140 	(void) fprintf(stderr,
141 	    "Usage: mount [-Ormq] [-o options] special mountpoint\n");
142 	exit(RET_ERR);
143 }
144