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.
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.
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]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include <sys/types.h>
37 #include <sys/mount.h>
38 #include <sys/mntent.h>
39 #include <sys/fs/sdev_impl.h>
49 static char typename
[64], *myname
;
50 static char fstype
[] = MNTTYPE_DEV
;
58 static struct sdev_mountargs mountargs
;
60 static char *myopts
[] = {
61 #define SUBOPT_READONLY 0
63 #define SUBOPT_READWRITE 1
65 #define SUBOPT_ATTRIBDIR 2
67 #define SUBOPT_REMOUNT 3
76 (void) fprintf(stderr
, gettext(
77 "%s usage:\n%s [-F %s] [-r] [-o specific_options]"
78 " {special | mount_point}\n%s [-F %s] [-r] [-o specific_options]"
79 " special mount_point\n"), fstype
, myname
, fstype
, myname
, fstype
);
89 if (readflag
== READFLAG_RO
)
96 if (mount(special
, mountpt
, flags
, fstype
, &mountargs
,
97 sizeof (mountargs
), NULL
, 0)) {
100 (void) fprintf(stderr
, gettext("%s: not super user\n"),
104 (void) fprintf(stderr
, gettext("%s: %s no such "
105 "device\n"), typename
, special
);
108 (void) fprintf(stderr
, gettext("%s: %s "
110 "\tor a component of %s is not a directory\n"),
111 typename
, mountpt
, special
);
114 (void) fprintf(stderr
, gettext("%s: %s or %s, no such "
115 "file or directory\n"),
116 typename
, special
, mountpt
);
119 (void) fprintf(stderr
, gettext("%s: %s is not this "
120 "filesystem type.\n"), typename
, special
);
123 (void) fprintf(stderr
, gettext("%s: %s "
124 "is already mounted, %s is busy,\n"
125 "\tor allowable number of mount points exceeded\n"),
126 typename
, special
, mountpt
);
129 (void) fprintf(stderr
, gettext("%s: %s not a block "
130 "device\n"), typename
, special
);
133 (void) fprintf(stderr
, gettext("%s: %s read-only "
134 "filesystem\n"), typename
, special
);
137 (void) fprintf(stderr
, gettext("%s: the state of %s "
139 "\tand read/write mount was attempted\n"),
143 (void) fprintf(stderr
, gettext("%s: cannot mount %s: "
144 "%s\n"), typename
, special
, strerror(errno
));
154 * Wrapper around strdup().
157 do_strdup(const char *s1
)
163 (void) fprintf(stderr
, gettext("%s: strdup failed: %s\n"),
164 typename
, strerror(errno
));
171 * Wrapper around stat().
174 do_stat(const char *path
, struct stat
*buf
)
178 ret
= stat(path
, buf
);
180 (void) fprintf(stderr
, gettext("%s: can't stat %s: %s\n"),
181 typename
, path
, strerror(errno
));
188 * Wraper around realpath()
191 do_realpath(const char *path
, char *resolved_path
)
195 ret
= realpath(path
, resolved_path
);
197 (void) fprintf(stderr
, gettext("%s: realpath %s failed: %s\n"),
198 typename
, path
, strerror(errno
));
205 parse_subopts(char *subopts
)
208 char path
[PATH_MAX
+ 1];
210 while (*subopts
!= '\0') {
211 switch (getsubopt(&subopts
, myopts
, &value
)) {
212 case SUBOPT_READONLY
:
213 if (readflag
== READFLAG_RW
) {
214 (void) fprintf(stderr
, gettext("%s: both "
215 "read-only and read-write options "
216 "specified\n"), typename
);
219 readflag
= READFLAG_RO
;
222 case SUBOPT_READWRITE
:
223 if (readflag
== READFLAG_RO
) {
224 (void) fprintf(stderr
, gettext("%s: both "
225 "read-only and read-write options "
226 "specified\n"), typename
);
229 readflag
= READFLAG_RW
;
232 case SUBOPT_ATTRIBDIR
:
234 (void) fprintf(stderr
, gettext("%s: no "
235 "attribute directory\n"), typename
);
238 if (do_realpath(value
, path
) == NULL
)
240 mountargs
.sdev_attrdir
=
241 (uint64_t)(uintptr_t)do_strdup(path
);
242 if (mountargs
.sdev_attrdir
== 0)
252 (void) fprintf(stderr
, gettext("%s: illegal -o "
253 "suboption: %s\n"), typename
, value
);
262 main(int argc
, char **argv
)
265 char mntpath
[PATH_MAX
+ 1];
268 (void) setlocale(LC_ALL
, "");
270 #if !defined(TEXT_DOMAIN)
271 #define TEXT_DOMAIN "SYS_TEST"
273 (void) textdomain(TEXT_DOMAIN
);
275 if (myname
= strrchr(argv
[0], '/'))
279 (void) snprintf(typename
, sizeof (typename
), "%s %s", fstype
, myname
);
282 while ((cc
= getopt(argc
, argv
, "?o:rmO")) != -1) {
285 if (readflag
== READFLAG_RW
) {
286 (void) fprintf(stderr
, gettext("%s: both "
287 "read-only and read-write options "
288 "specified\n"), typename
);
291 readflag
= READFLAG_RO
;
299 if (parse_subopts(optarg
))
310 * There must be at least 2 more arguments, the
311 * special file and the directory.
313 if ((argc
- optind
) != 2)
316 special
= argv
[optind
++];
318 if (do_realpath(argv
[optind
++], mntpath
) == NULL
)
323 if (do_stat(mountpt
, &st
) < 0)
325 if (! S_ISDIR(st
.st_mode
)) {
326 (void) fprintf(stderr
, gettext("%s: %s is not a "
327 "directory\n"), typename
, mountpt
);
332 if (mountargs
.sdev_attrdir
) {
333 if (do_stat((const char *)(uintptr_t)mountargs
.sdev_attrdir
,
336 if (! S_ISDIR(st
.st_mode
)) {
337 (void) fprintf(stderr
, gettext("%s: %s is not a "
338 "directory\n"), typename
, mountargs
.sdev_attrdir
);
343 /* Special checks if /dev is the mount point */
344 /* Remount of /dev requires an attribute directory */
345 if (strcmp(mountpt
, "/dev") == 0 && remount
&&
346 mountargs
.sdev_attrdir
== 0) {
347 (void) fprintf(stderr
, gettext("%s: missing attribute "
348 "directory\n"), typename
);
352 (void) signal(SIGHUP
, SIG_IGN
);
353 (void) signal(SIGQUIT
, SIG_IGN
);
354 (void) signal(SIGINT
, SIG_IGN
);
356 /* Perform the mount */