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