xref: /titanic_52/usr/src/cmd/fs.d/objfs/mount.c (revision a237e38e9161f0acd6451439d4a7dd597e66291d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*a237e38eSth199096  * Common Development and Distribution License (the "License").
6*a237e38eSth199096  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*a237e38eSth199096  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate #include <libintl.h>
327c478bd9Sstevel@tonic-gate #include <errno.h>
337c478bd9Sstevel@tonic-gate #include <sys/fstyp.h>
347c478bd9Sstevel@tonic-gate #include <sys/fsid.h>
357c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
367c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
377c478bd9Sstevel@tonic-gate #include <sys/mount.h>
387c478bd9Sstevel@tonic-gate #include <sys/signal.h>
397c478bd9Sstevel@tonic-gate #include <sys/stat.h>
407c478bd9Sstevel@tonic-gate #include <fslib.h>
41*a237e38eSth199096 #include <locale.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #define	RET_OK		0
447c478bd9Sstevel@tonic-gate #define	RET_ERR		33
45*a237e38eSth199096 #define	EXIT_MAGIC	2
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate static void usage(void);
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate static char  optbuf[MAX_MNTOPT_STR] = { '\0', };
507c478bd9Sstevel@tonic-gate static int   optsize = 0;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate static char fstype[] = "objfs";
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /*
557c478bd9Sstevel@tonic-gate  * usage: mount [-Ormq] [-o options] special mountp
567c478bd9Sstevel@tonic-gate  *
577c478bd9Sstevel@tonic-gate  * This mount program is exec'ed by /usr/sbin/mount if '-F objfs' is
587c478bd9Sstevel@tonic-gate  * specified.
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate int
617c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate 	int c;
647c478bd9Sstevel@tonic-gate 	char *special;		/* Entity being mounted */
657c478bd9Sstevel@tonic-gate 	char *mountp;		/* Entity being mounted on */
667c478bd9Sstevel@tonic-gate 	char *savedoptbuf;
677c478bd9Sstevel@tonic-gate 	char *myname;
68*a237e38eSth199096 	char *typename;
697c478bd9Sstevel@tonic-gate 	int flags = 0;
70*a237e38eSth199096 	int error_flag = 0;
71*a237e38eSth199096 	int q_flag = 0;
72*a237e38eSth199096 
73*a237e38eSth199096 	int len;
74*a237e38eSth199096 
75*a237e38eSth199096 	(void) setlocale(LC_ALL, "");
76*a237e38eSth199096 
77*a237e38eSth199096 #if !defined(TEXT_DOMAIN)
78*a237e38eSth199096 #define	TEXT_DOMAIN "SYS_TEST"
79*a237e38eSth199096 #endif
80*a237e38eSth199096 	(void) textdomain(TEXT_DOMAIN);
81*a237e38eSth199096 
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	myname = strrchr(argv[0], '/');
847c478bd9Sstevel@tonic-gate 	myname = myname ? myname + 1 : argv[0];
85*a237e38eSth199096 
86*a237e38eSth199096 	len = strlen(fstype) + 1 + strlen(myname);
87*a237e38eSth199096 	typename = malloc(len + 1);
88*a237e38eSth199096 	if (!typename) {
89*a237e38eSth199096 		(void) fprintf(stderr, gettext("%s: out of memory\n"),
90*a237e38eSth199096 		    myname);
91*a237e38eSth199096 		return (EXIT_MAGIC);
92*a237e38eSth199096 	}
93*a237e38eSth199096 
94*a237e38eSth199096 	(void) snprintf(typename, len, "%s %s", fstype, myname);
957c478bd9Sstevel@tonic-gate 	argv[0] = typename;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "o:rmOq")) != EOF) {
987c478bd9Sstevel@tonic-gate 		switch (c) {
997c478bd9Sstevel@tonic-gate 		case '?':
100*a237e38eSth199096 			error_flag = 1;
1017c478bd9Sstevel@tonic-gate 			break;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 		case 'o':
1047c478bd9Sstevel@tonic-gate 			if (strlcpy(optbuf, optarg, sizeof (optbuf)) >=
1057c478bd9Sstevel@tonic-gate 			    sizeof (optbuf)) {
1067c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1077c478bd9Sstevel@tonic-gate 				    gettext("%s: Invalid argument: %s\n"),
1087c478bd9Sstevel@tonic-gate 				    myname, optarg);
109*a237e38eSth199096 				free(typename);
110*a237e38eSth199096 				return (EXIT_MAGIC);
1117c478bd9Sstevel@tonic-gate 			}
1127c478bd9Sstevel@tonic-gate 			optsize = strlen(optbuf);
1137c478bd9Sstevel@tonic-gate 			break;
1147c478bd9Sstevel@tonic-gate 		case 'O':
1157c478bd9Sstevel@tonic-gate 			flags |= MS_OVERLAY;
1167c478bd9Sstevel@tonic-gate 			break;
1177c478bd9Sstevel@tonic-gate 		case 'r':
1187c478bd9Sstevel@tonic-gate 			flags |= MS_RDONLY;
1197c478bd9Sstevel@tonic-gate 			break;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 		case 'm':
1227c478bd9Sstevel@tonic-gate 			flags |= MS_NOMNTTAB;
1237c478bd9Sstevel@tonic-gate 			break;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 		case 'q':
126*a237e38eSth199096 			q_flag = 1;
1277c478bd9Sstevel@tonic-gate 			break;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 		default:
1307c478bd9Sstevel@tonic-gate 			usage();
1317c478bd9Sstevel@tonic-gate 		}
1327c478bd9Sstevel@tonic-gate 	}
133*a237e38eSth199096 	if ((argc - optind != 2) || error_flag) {
1347c478bd9Sstevel@tonic-gate 		usage();
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 	special = argv[argc - 2];
1377c478bd9Sstevel@tonic-gate 	mountp = argv[argc - 1];
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	if ((savedoptbuf = strdup(optbuf)) == NULL) {
1407c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: out of memory\n"),
1417c478bd9Sstevel@tonic-gate 		    myname);
142*a237e38eSth199096 		free(typename);
143*a237e38eSth199096 		exit(EXIT_MAGIC);
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate 	if (mount(special, mountp, flags | MS_OPTIONSTR, fstype, NULL, 0,
1467c478bd9Sstevel@tonic-gate 	    optbuf, MAX_MNTOPT_STR)) {
1477c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "mount: ");
1487c478bd9Sstevel@tonic-gate 		perror(special);
149*a237e38eSth199096 		free(typename);
1507c478bd9Sstevel@tonic-gate 		exit(RET_ERR);
1517c478bd9Sstevel@tonic-gate 	}
152*a237e38eSth199096 	if (optsize && !q_flag)
1537c478bd9Sstevel@tonic-gate 		cmp_requested_to_actual_options(savedoptbuf, optbuf,
1547c478bd9Sstevel@tonic-gate 		    special, mountp);
155*a237e38eSth199096 	free(typename);
156*a237e38eSth199096 	return (RET_OK);
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate 
159*a237e38eSth199096 static void
1607c478bd9Sstevel@tonic-gate usage(void)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
163*a237e38eSth199096 	    gettext("Usage: mount [-Ormq] [-o options] special mountpoint\n"));
1647c478bd9Sstevel@tonic-gate 	exit(RET_ERR);
1657c478bd9Sstevel@tonic-gate }
166