xref: /illumos-gate/usr/src/cmd/fs.d/objfs/mount.c (revision 2a8bcb4e)
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*a237e38eSth  * Common Development and Distribution License (the "License").
6*a237e38eSth  * 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*a237e38eSth  * 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 #include <stdio.h>
277c478bd9Sstevel@tonic-gate #include <stdlib.h>
287c478bd9Sstevel@tonic-gate #include <string.h>
297c478bd9Sstevel@tonic-gate #include <libintl.h>
307c478bd9Sstevel@tonic-gate #include <errno.h>
317c478bd9Sstevel@tonic-gate #include <sys/fstyp.h>
327c478bd9Sstevel@tonic-gate #include <sys/fsid.h>
337c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
347c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
357c478bd9Sstevel@tonic-gate #include <sys/mount.h>
367c478bd9Sstevel@tonic-gate #include <sys/signal.h>
377c478bd9Sstevel@tonic-gate #include <sys/stat.h>
387c478bd9Sstevel@tonic-gate #include <fslib.h>
39*a237e38eSth #include <locale.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #define	RET_OK		0
427c478bd9Sstevel@tonic-gate #define	RET_ERR		33
43*a237e38eSth #define	EXIT_MAGIC	2
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate static void usage(void);
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate static char  optbuf[MAX_MNTOPT_STR] = { '\0', };
487c478bd9Sstevel@tonic-gate static int   optsize = 0;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate static char fstype[] = "objfs";
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate  * usage: mount [-Ormq] [-o options] special mountp
547c478bd9Sstevel@tonic-gate  *
557c478bd9Sstevel@tonic-gate  * This mount program is exec'ed by /usr/sbin/mount if '-F objfs' is
567c478bd9Sstevel@tonic-gate  * specified.
577c478bd9Sstevel@tonic-gate  */
587c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])597c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate 	int c;
627c478bd9Sstevel@tonic-gate 	char *special;		/* Entity being mounted */
637c478bd9Sstevel@tonic-gate 	char *mountp;		/* Entity being mounted on */
647c478bd9Sstevel@tonic-gate 	char *savedoptbuf;
657c478bd9Sstevel@tonic-gate 	char *myname;
66*a237e38eSth 	char *typename;
677c478bd9Sstevel@tonic-gate 	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 
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	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);
937c478bd9Sstevel@tonic-gate 	argv[0] = typename;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "o:rmOq")) != EOF) {
967c478bd9Sstevel@tonic-gate 		switch (c) {
977c478bd9Sstevel@tonic-gate 		case '?':
98*a237e38eSth 			error_flag = 1;
997c478bd9Sstevel@tonic-gate 			break;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 		case 'o':
1027c478bd9Sstevel@tonic-gate 			if (strlcpy(optbuf, optarg, sizeof (optbuf)) >=
1037c478bd9Sstevel@tonic-gate 			    sizeof (optbuf)) {
1047c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1057c478bd9Sstevel@tonic-gate 				    gettext("%s: Invalid argument: %s\n"),
1067c478bd9Sstevel@tonic-gate 				    myname, optarg);
107*a237e38eSth 				free(typename);
108*a237e38eSth 				return (EXIT_MAGIC);
1097c478bd9Sstevel@tonic-gate 			}
1107c478bd9Sstevel@tonic-gate 			optsize = strlen(optbuf);
1117c478bd9Sstevel@tonic-gate 			break;
1127c478bd9Sstevel@tonic-gate 		case 'O':
1137c478bd9Sstevel@tonic-gate 			flags |= MS_OVERLAY;
1147c478bd9Sstevel@tonic-gate 			break;
1157c478bd9Sstevel@tonic-gate 		case 'r':
1167c478bd9Sstevel@tonic-gate 			flags |= MS_RDONLY;
1177c478bd9Sstevel@tonic-gate 			break;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 		case 'm':
1207c478bd9Sstevel@tonic-gate 			flags |= MS_NOMNTTAB;
1217c478bd9Sstevel@tonic-gate 			break;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 		case 'q':
124*a237e38eSth 			q_flag = 1;
1257c478bd9Sstevel@tonic-gate 			break;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 		default:
1287c478bd9Sstevel@tonic-gate 			usage();
1297c478bd9Sstevel@tonic-gate 		}
1307c478bd9Sstevel@tonic-gate 	}
131*a237e38eSth 	if ((argc - optind != 2) || error_flag) {
1327c478bd9Sstevel@tonic-gate 		usage();
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate 	special = argv[argc - 2];
1357c478bd9Sstevel@tonic-gate 	mountp = argv[argc - 1];
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	if ((savedoptbuf = strdup(optbuf)) == NULL) {
1387c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: out of memory\n"),
1397c478bd9Sstevel@tonic-gate 		    myname);
140*a237e38eSth 		free(typename);
141*a237e38eSth 		exit(EXIT_MAGIC);
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 	if (mount(special, mountp, flags | MS_OPTIONSTR, fstype, NULL, 0,
1447c478bd9Sstevel@tonic-gate 	    optbuf, MAX_MNTOPT_STR)) {
1457c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "mount: ");
1467c478bd9Sstevel@tonic-gate 		perror(special);
147*a237e38eSth 		free(typename);
1487c478bd9Sstevel@tonic-gate 		exit(RET_ERR);
1497c478bd9Sstevel@tonic-gate 	}
150*a237e38eSth 	if (optsize && !q_flag)
1517c478bd9Sstevel@tonic-gate 		cmp_requested_to_actual_options(savedoptbuf, optbuf,
1527c478bd9Sstevel@tonic-gate 		    special, mountp);
153*a237e38eSth 	free(typename);
154*a237e38eSth 	return (RET_OK);
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate 
157*a237e38eSth static void
usage(void)1587c478bd9Sstevel@tonic-gate usage(void)
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
161*a237e38eSth 	    gettext("Usage: mount [-Ormq] [-o options] special mountpoint\n"));
1627c478bd9Sstevel@tonic-gate 	exit(RET_ERR);
1637c478bd9Sstevel@tonic-gate }
164