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