xref: /illumos-gate/usr/src/cmd/fs.d/ctfs/mount.c (revision 9164a50bf932130cbb5097a16f6986873ce0e6e5)
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 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <libintl.h>
30 #include <errno.h>
31 #include <sys/fstyp.h>
32 #include <sys/fsid.h>
33 #include <sys/mntent.h>
34 #include <sys/mnttab.h>
35 #include <sys/mount.h>
36 #include <sys/signal.h>
37 #include <sys/stat.h>
38 #include <fslib.h>
39 #include <locale.h>
40 
41 #define	RET_OK		0
42 #define	RET_ERR		33
43 #define	EXIT_MAGIC	2
44 
45 static void usage(void);
46 
47 static char  optbuf[MAX_MNTOPT_STR] = { '\0', };
48 static int   optsize = 0;
49 
50 static char fstype[] = "ctfs";
51 
52 /*
53  * usage: mount [-Ormq] [-o options] special mountp
54  *
55  * This mount program is exec'ed by /usr/sbin/mount if '-F ctfs' is
56  * specified.
57  */
58 int
59 main(int argc, char *argv[])
60 {
61 	int c;
62 	char *special;		/* Entity being mounted */
63 	char *mountp;		/* Entity being mounted on */
64 	char *savedoptbuf;
65 	char *myname;
66 	char *typename;
67 	int flags = 0;
68 	int error_flag = 0;
69 	int q_flag = 0;
70 
71 	int len;
72 
73 	(void) setlocale(LC_ALL, "");
74 
75 #if !defined(TEXT_DOMAIN)
76 #define	TEXT_DOMAIN "SYS_TEST"
77 #endif
78 	(void) textdomain(TEXT_DOMAIN);
79 
80 
81 	myname = strrchr(argv[0], '/');
82 	myname = myname ? myname + 1 : argv[0];
83 
84 	len = strlen(fstype) + 1 + strlen(myname);
85 	typename = malloc(len + 1);
86 	if (!typename) {
87 		(void) fprintf(stderr, gettext("%s: out of memory\n"),
88 		    myname);
89 		return (EXIT_MAGIC);
90 	}
91 
92 	(void) snprintf(typename, len, "%s %s", fstype, myname);
93 	argv[0] = typename;
94 
95 	while ((c = getopt(argc, argv, "o:rmOq")) != EOF) {
96 		switch (c) {
97 		case '?':
98 			error_flag = 1;
99 			break;
100 
101 		case 'o':
102 			if (strlcpy(optbuf, optarg, sizeof (optbuf)) >=
103 			    sizeof (optbuf)) {
104 				(void) fprintf(stderr,
105 				    gettext("%s: Invalid argument: %s\n"),
106 				    myname, optarg);
107 				free(typename);
108 				return (EXIT_MAGIC);
109 			}
110 			optsize = strlen(optbuf);
111 			break;
112 		case 'O':
113 			flags |= MS_OVERLAY;
114 			break;
115 		case 'r':
116 			flags |= MS_RDONLY;
117 			break;
118 
119 		case 'm':
120 			flags |= MS_NOMNTTAB;
121 			break;
122 
123 		case 'q':
124 			q_flag = 1;
125 			break;
126 
127 		default:
128 			usage();
129 		}
130 	}
131 	if ((argc - optind != 2) || error_flag) {
132 		usage();
133 	}
134 	special = argv[argc - 2];
135 	mountp = argv[argc - 1];
136 
137 	if ((savedoptbuf = strdup(optbuf)) == NULL) {
138 		(void) fprintf(stderr, gettext("%s: out of memory\n"),
139 		    myname);
140 		free(typename);
141 		exit(EXIT_MAGIC);
142 	}
143 	if (mount(special, mountp, flags | MS_OPTIONSTR, fstype, NULL, 0,
144 	    optbuf, MAX_MNTOPT_STR)) {
145 		(void) fprintf(stderr, "mount: ");
146 		perror(special);
147 		free(typename);
148 		exit(RET_ERR);
149 	}
150 	if (optsize && !q_flag)
151 		cmp_requested_to_actual_options(savedoptbuf, optbuf,
152 		    special, mountp);
153 	free(typename);
154 	return (RET_OK);
155 }
156 
157 static void
158 usage(void)
159 {
160 	(void) fprintf(stderr,
161 	    gettext("Usage: mount [-Ormq] [-o options] special mountpoint\n"));
162 	exit(RET_ERR);
163 }
164