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