xref: /titanic_54/usr/src/cmd/fs.d/switchout.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright 1996-2003 Sun Microsystems, Inc.  All rights reserved.
28*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include	<stdio.h>
34*7c478bd9Sstevel@tonic-gate #include 	<limits.h>
35*7c478bd9Sstevel@tonic-gate #include	<locale.h>
36*7c478bd9Sstevel@tonic-gate #include	<libintl.h>
37*7c478bd9Sstevel@tonic-gate #include	<sys/fstyp.h>
38*7c478bd9Sstevel@tonic-gate #include	<errno.h>
39*7c478bd9Sstevel@tonic-gate #include	<sys/vfstab.h>
40*7c478bd9Sstevel@tonic-gate #include	<sys/types.h>
41*7c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
42*7c478bd9Sstevel@tonic-gate #include	<fcntl.h>
43*7c478bd9Sstevel@tonic-gate #include	<string.h>
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate #define	FSTYPE_MAX	8
46*7c478bd9Sstevel@tonic-gate #define	ARGV_MAX	1024
47*7c478bd9Sstevel@tonic-gate #define	VFS_PATH	"/usr/lib/fs"
48*7c478bd9Sstevel@tonic-gate #define	ALT_PATH	"/etc/fs"
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate extern char	*default_fstype();
51*7c478bd9Sstevel@tonic-gate void	stat_snap(char *, char *, char *);
52*7c478bd9Sstevel@tonic-gate char	*special = NULL;  /*  device special name  */
53*7c478bd9Sstevel@tonic-gate char	*fstype = NULL;	  /*  fstype name is filled in here  */
54*7c478bd9Sstevel@tonic-gate char	*cbasename;	  /* name of command */
55*7c478bd9Sstevel@tonic-gate char	*newargv[ARGV_MAX]; 	/* args for the fstype specific command  */
56*7c478bd9Sstevel@tonic-gate char	vfstab[] = VFSTAB;
57*7c478bd9Sstevel@tonic-gate int	newargc = 2;
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate /*
60*7c478bd9Sstevel@tonic-gate  * TRANSLATION_NOTE - the usage strings in the c_usgstr[] of the
61*7c478bd9Sstevel@tonic-gate  * following structures should be given a translation; the call to gettext
62*7c478bd9Sstevel@tonic-gate  * is in the usage() function. The strings are the ones containing
63*7c478bd9Sstevel@tonic-gate  * "[-F FSType]".
64*7c478bd9Sstevel@tonic-gate  */
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate struct commands {
67*7c478bd9Sstevel@tonic-gate 	char *c_basename;
68*7c478bd9Sstevel@tonic-gate 	char *c_optstr;
69*7c478bd9Sstevel@tonic-gate 	char *c_usgstr[4]; /* make sure as large as largest array size */
70*7c478bd9Sstevel@tonic-gate } cmd_data[] = {
71*7c478bd9Sstevel@tonic-gate 	"clri", "F:o:?V",
72*7c478bd9Sstevel@tonic-gate 	{
73*7c478bd9Sstevel@tonic-gate 		"[-F FSType] [-V] special inumber ...",
74*7c478bd9Sstevel@tonic-gate 		NULL
75*7c478bd9Sstevel@tonic-gate 	},
76*7c478bd9Sstevel@tonic-gate 	"mkfs", "F:o:mb:?V",
77*7c478bd9Sstevel@tonic-gate 	{
78*7c478bd9Sstevel@tonic-gate 		"[-F FSType] [-V] [-m] [-o specific_options] special ",
79*7c478bd9Sstevel@tonic-gate 		"[operands]", NULL
80*7c478bd9Sstevel@tonic-gate 	},
81*7c478bd9Sstevel@tonic-gate 	"dcopy", "F:o:?V",
82*7c478bd9Sstevel@tonic-gate 	{
83*7c478bd9Sstevel@tonic-gate 		"[-F FSType] [-V] special inumber ...",
84*7c478bd9Sstevel@tonic-gate 		NULL
85*7c478bd9Sstevel@tonic-gate 	},
86*7c478bd9Sstevel@tonic-gate 	"fsdb", "F:o:z:?V",
87*7c478bd9Sstevel@tonic-gate 	{
88*7c478bd9Sstevel@tonic-gate 		"[-F FSType] [-V] [-o specific_options] special",
89*7c478bd9Sstevel@tonic-gate 		NULL
90*7c478bd9Sstevel@tonic-gate 	},
91*7c478bd9Sstevel@tonic-gate 	"fssnap", "F:dio:?V",
92*7c478bd9Sstevel@tonic-gate 	{
93*7c478bd9Sstevel@tonic-gate 		"[-F FSType] [-V] -o special_options  /mount/point",
94*7c478bd9Sstevel@tonic-gate 		"-d [-F FSType] [-V] /mount/point | dev",
95*7c478bd9Sstevel@tonic-gate 		"-i [-F FSType] [-V] [-o special-options] [/mount/point | dev]",
96*7c478bd9Sstevel@tonic-gate 		NULL
97*7c478bd9Sstevel@tonic-gate 	},
98*7c478bd9Sstevel@tonic-gate 	"labelit", "F:o:?nV",
99*7c478bd9Sstevel@tonic-gate 	{
100*7c478bd9Sstevel@tonic-gate 		"[-F FSType] [-V] [-o specific_options] special [operands]",
101*7c478bd9Sstevel@tonic-gate 		NULL
102*7c478bd9Sstevel@tonic-gate 	},
103*7c478bd9Sstevel@tonic-gate 	NULL, "F:o:?V",
104*7c478bd9Sstevel@tonic-gate 	{
105*7c478bd9Sstevel@tonic-gate 		"[-F FSType] [-V] [-o specific_options] special [operands]",
106*7c478bd9Sstevel@tonic-gate 		NULL
107*7c478bd9Sstevel@tonic-gate 	}
108*7c478bd9Sstevel@tonic-gate };
109*7c478bd9Sstevel@tonic-gate struct 	commands *c_ptr;
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate main(argc, argv)
112*7c478bd9Sstevel@tonic-gate int	argc;
113*7c478bd9Sstevel@tonic-gate char	*argv[];
114*7c478bd9Sstevel@tonic-gate {
115*7c478bd9Sstevel@tonic-gate 	register char *ptr;
116*7c478bd9Sstevel@tonic-gate 	char	full_path[PATH_MAX];
117*7c478bd9Sstevel@tonic-gate 	char	*vfs_path = VFS_PATH;
118*7c478bd9Sstevel@tonic-gate 	char	*alt_path = ALT_PATH;
119*7c478bd9Sstevel@tonic-gate 	int	i;
120*7c478bd9Sstevel@tonic-gate 	int	verbose = 0;		/* set if -V is specified */
121*7c478bd9Sstevel@tonic-gate 	int	F_flg = 0;
122*7c478bd9Sstevel@tonic-gate 	int	mflag = 0;
123*7c478bd9Sstevel@tonic-gate 	char	*oopts = NULL;
124*7c478bd9Sstevel@tonic-gate 	int	iflag = 0;
125*7c478bd9Sstevel@tonic-gate 	int	usgflag = 0;
126*7c478bd9Sstevel@tonic-gate 	int	arg;			/* argument from getopt() */
127*7c478bd9Sstevel@tonic-gate 	extern	char *optarg;		/* getopt specific */
128*7c478bd9Sstevel@tonic-gate 	extern	int optind;
129*7c478bd9Sstevel@tonic-gate 	extern	int opterr;
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
134*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
135*7c478bd9Sstevel@tonic-gate #endif
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	cbasename = ptr = argv[0];
140*7c478bd9Sstevel@tonic-gate 	while (*ptr) {
141*7c478bd9Sstevel@tonic-gate 		if (*ptr++ == '/')
142*7c478bd9Sstevel@tonic-gate 			cbasename = ptr;
143*7c478bd9Sstevel@tonic-gate 	}
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 	if (argc == 1) {
147*7c478bd9Sstevel@tonic-gate 		for (c_ptr = cmd_data; ((c_ptr->c_basename != NULL) &&
148*7c478bd9Sstevel@tonic-gate 		    (strcmp(c_ptr->c_basename, cbasename) != 0)); c_ptr++)
149*7c478bd9Sstevel@tonic-gate 		;
150*7c478bd9Sstevel@tonic-gate 		usage(cbasename, c_ptr->c_usgstr);
151*7c478bd9Sstevel@tonic-gate 		exit(2);
152*7c478bd9Sstevel@tonic-gate 	}
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 	for (c_ptr = cmd_data; ((c_ptr->c_basename != NULL) &&
155*7c478bd9Sstevel@tonic-gate 	    (strcmp(c_ptr->c_basename, cbasename) != 0));  c_ptr++)
156*7c478bd9Sstevel@tonic-gate 		;
157*7c478bd9Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, c_ptr->c_optstr)) != -1) {
158*7c478bd9Sstevel@tonic-gate 			switch (arg) {
159*7c478bd9Sstevel@tonic-gate 			case 'V':	/* echo complete command line */
160*7c478bd9Sstevel@tonic-gate 				verbose = 1;
161*7c478bd9Sstevel@tonic-gate 				break;
162*7c478bd9Sstevel@tonic-gate 			case 'F':	/* FSType specified */
163*7c478bd9Sstevel@tonic-gate 				F_flg++;
164*7c478bd9Sstevel@tonic-gate 				fstype = optarg;
165*7c478bd9Sstevel@tonic-gate 				break;
166*7c478bd9Sstevel@tonic-gate 			case 'o':	/* FSType specific arguments */
167*7c478bd9Sstevel@tonic-gate 				newargv[newargc++] = "-o";
168*7c478bd9Sstevel@tonic-gate 				newargv[newargc++] = optarg;
169*7c478bd9Sstevel@tonic-gate 				oopts = optarg;
170*7c478bd9Sstevel@tonic-gate 				break;
171*7c478bd9Sstevel@tonic-gate 			case '?':	/* print usage message */
172*7c478bd9Sstevel@tonic-gate 				newargv[newargc++] = "-?";
173*7c478bd9Sstevel@tonic-gate 				usgflag = 1;
174*7c478bd9Sstevel@tonic-gate 				break;
175*7c478bd9Sstevel@tonic-gate 			case 'm':	/* FSType specific arguments */
176*7c478bd9Sstevel@tonic-gate 				mflag = 1;
177*7c478bd9Sstevel@tonic-gate 				newargv[newargc] = (char *)malloc(3);
178*7c478bd9Sstevel@tonic-gate 				sprintf(newargv[newargc++], "-%c", arg);
179*7c478bd9Sstevel@tonic-gate 				if (optarg)
180*7c478bd9Sstevel@tonic-gate 					newargv[newargc++] = optarg;
181*7c478bd9Sstevel@tonic-gate 				break;
182*7c478bd9Sstevel@tonic-gate 			case 'i': /* fssnap only */
183*7c478bd9Sstevel@tonic-gate 				iflag = 1;
184*7c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
185*7c478bd9Sstevel@tonic-gate 			default:
186*7c478bd9Sstevel@tonic-gate 				newargv[newargc] = (char *)malloc(3);
187*7c478bd9Sstevel@tonic-gate 				sprintf(newargv[newargc++], "-%c", arg);
188*7c478bd9Sstevel@tonic-gate 				if (optarg)
189*7c478bd9Sstevel@tonic-gate 					newargv[newargc++] = optarg;
190*7c478bd9Sstevel@tonic-gate 				break;
191*7c478bd9Sstevel@tonic-gate 			}
192*7c478bd9Sstevel@tonic-gate 			optarg = NULL;
193*7c478bd9Sstevel@tonic-gate 	}
194*7c478bd9Sstevel@tonic-gate 	if (F_flg > 1) {
195*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: more than one FSType specified\n"),
196*7c478bd9Sstevel@tonic-gate 			cbasename);
197*7c478bd9Sstevel@tonic-gate 		usage(cbasename, c_ptr->c_usgstr);
198*7c478bd9Sstevel@tonic-gate 		exit(2);
199*7c478bd9Sstevel@tonic-gate 	}
200*7c478bd9Sstevel@tonic-gate 	if (fstype != NULL) {
201*7c478bd9Sstevel@tonic-gate 		if (strlen(fstype) > FSTYPE_MAX) {
202*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,
203*7c478bd9Sstevel@tonic-gate 			    gettext("%s: FSType %s exceeds %d characters\n"),
204*7c478bd9Sstevel@tonic-gate 			    cbasename, fstype, FSTYPE_MAX);
205*7c478bd9Sstevel@tonic-gate 			exit(2);
206*7c478bd9Sstevel@tonic-gate 		}
207*7c478bd9Sstevel@tonic-gate 	}
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 	/*  perform a lookup if fstype is not specified  */
210*7c478bd9Sstevel@tonic-gate 	special = argv[optind];
211*7c478bd9Sstevel@tonic-gate 	optind++;
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate 	/* handle -i (fssnap command only) */
214*7c478bd9Sstevel@tonic-gate 	if (iflag) {
215*7c478bd9Sstevel@tonic-gate 		int diff = argc - optind;
216*7c478bd9Sstevel@tonic-gate 		/*
217*7c478bd9Sstevel@tonic-gate 		 * There is no reason to ever call a file system specific
218*7c478bd9Sstevel@tonic-gate 		 * version since its all in kstats.
219*7c478bd9Sstevel@tonic-gate 		 */
220*7c478bd9Sstevel@tonic-gate 		if (diff > 0) /* gave more than one mountpoint or device */
221*7c478bd9Sstevel@tonic-gate 			usage(cbasename, c_ptr->c_usgstr);
222*7c478bd9Sstevel@tonic-gate 		stat_snap(cbasename, diff == 0 ? argv[argc-1] : NULL, oopts);
223*7c478bd9Sstevel@tonic-gate 		exit(0);
224*7c478bd9Sstevel@tonic-gate 	}
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate 	if ((special == NULL) && (!usgflag)) {
227*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: special not specified\n"),
228*7c478bd9Sstevel@tonic-gate 		    cbasename);
229*7c478bd9Sstevel@tonic-gate 		usage(cbasename, c_ptr->c_usgstr);
230*7c478bd9Sstevel@tonic-gate 		exit(2);
231*7c478bd9Sstevel@tonic-gate 	}
232*7c478bd9Sstevel@tonic-gate 	if ((fstype == NULL) && (usgflag))
233*7c478bd9Sstevel@tonic-gate 		usage(cbasename, c_ptr->c_usgstr);
234*7c478bd9Sstevel@tonic-gate 	if (fstype == NULL)
235*7c478bd9Sstevel@tonic-gate 		lookup();
236*7c478bd9Sstevel@tonic-gate 	if (fstype == NULL) {
237*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: FSType cannot be identified\n"),
238*7c478bd9Sstevel@tonic-gate 			cbasename);
239*7c478bd9Sstevel@tonic-gate 		usage(cbasename, c_ptr->c_usgstr);
240*7c478bd9Sstevel@tonic-gate 		exit(2);
241*7c478bd9Sstevel@tonic-gate 	}
242*7c478bd9Sstevel@tonic-gate 	newargv[newargc++] = special;
243*7c478bd9Sstevel@tonic-gate 	for (; optind < argc; optind++)
244*7c478bd9Sstevel@tonic-gate 		newargv[newargc++] = argv[optind];
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate 	/*  build the full pathname of the fstype dependent command  */
247*7c478bd9Sstevel@tonic-gate 	sprintf(full_path, "%s/%s/%s", vfs_path, fstype, cbasename);
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate 	newargv[1] = cbasename;
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate 	if (verbose) {
252*7c478bd9Sstevel@tonic-gate 		printf("%s -F %s ", cbasename, fstype);
253*7c478bd9Sstevel@tonic-gate 		for (i = 2; newargv[i]; i++)
254*7c478bd9Sstevel@tonic-gate 			printf("%s ", newargv[i]);
255*7c478bd9Sstevel@tonic-gate 		printf("\n");
256*7c478bd9Sstevel@tonic-gate 		exit(0);
257*7c478bd9Sstevel@tonic-gate 	}
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate 	/*
260*7c478bd9Sstevel@tonic-gate 	 *  Execute the FSType specific command.
261*7c478bd9Sstevel@tonic-gate 	 */
262*7c478bd9Sstevel@tonic-gate 	execv(full_path, &newargv[1]);
263*7c478bd9Sstevel@tonic-gate 	if ((errno == ENOENT) || (errno == EACCES)) {
264*7c478bd9Sstevel@tonic-gate 		/*  build the alternate pathname */
265*7c478bd9Sstevel@tonic-gate 		sprintf(full_path, "%s/%s/%s", alt_path, fstype, cbasename);
266*7c478bd9Sstevel@tonic-gate 		if (verbose) {
267*7c478bd9Sstevel@tonic-gate 			printf("%s -F %s ", cbasename, fstype);
268*7c478bd9Sstevel@tonic-gate 			for (i = 2; newargv[i]; i++)
269*7c478bd9Sstevel@tonic-gate 				printf("%s ", newargv[i]);
270*7c478bd9Sstevel@tonic-gate 			printf("\n");
271*7c478bd9Sstevel@tonic-gate 			exit(0);
272*7c478bd9Sstevel@tonic-gate 		}
273*7c478bd9Sstevel@tonic-gate 		execv(full_path, &newargv[1]);
274*7c478bd9Sstevel@tonic-gate 	}
275*7c478bd9Sstevel@tonic-gate 	if (errno == ENOEXEC) {
276*7c478bd9Sstevel@tonic-gate 		newargv[0] = "sh";
277*7c478bd9Sstevel@tonic-gate 		newargv[1] = full_path;
278*7c478bd9Sstevel@tonic-gate 		execv("/sbin/sh", &newargv[0]);
279*7c478bd9Sstevel@tonic-gate 	}
280*7c478bd9Sstevel@tonic-gate 	if (errno != ENOENT) {
281*7c478bd9Sstevel@tonic-gate 		perror(cbasename);
282*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: cannot execute %s\n"), cbasename,
283*7c478bd9Sstevel@tonic-gate 		    full_path);
284*7c478bd9Sstevel@tonic-gate 		exit(2);
285*7c478bd9Sstevel@tonic-gate 	}
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate 	if (sysfs(GETFSIND, fstype) == (-1)) {
288*7c478bd9Sstevel@tonic-gate 		fprintf(stderr,
289*7c478bd9Sstevel@tonic-gate 		    gettext("%s: FSType %s not installed in the kernel\n"),
290*7c478bd9Sstevel@tonic-gate 		    cbasename, fstype);
291*7c478bd9Sstevel@tonic-gate 		exit(2);
292*7c478bd9Sstevel@tonic-gate 	}
293*7c478bd9Sstevel@tonic-gate 	fprintf(stderr,
294*7c478bd9Sstevel@tonic-gate 	    gettext("%s: Operation not applicable for FSType %s \n"),
295*7c478bd9Sstevel@tonic-gate 	    cbasename, fstype);
296*7c478bd9Sstevel@tonic-gate 	exit(2);
297*7c478bd9Sstevel@tonic-gate }
298*7c478bd9Sstevel@tonic-gate 
299*7c478bd9Sstevel@tonic-gate usage(cmd, usg)
300*7c478bd9Sstevel@tonic-gate char *cmd;
301*7c478bd9Sstevel@tonic-gate char **usg;
302*7c478bd9Sstevel@tonic-gate {
303*7c478bd9Sstevel@tonic-gate 	int i;
304*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext("Usage:\n"));
305*7c478bd9Sstevel@tonic-gate 	for (i = 0; usg[i] != NULL; i++)
306*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s %s\n", gettext(cmd), gettext(usg[i]));
307*7c478bd9Sstevel@tonic-gate 	exit(2);
308*7c478bd9Sstevel@tonic-gate }
309*7c478bd9Sstevel@tonic-gate 
310*7c478bd9Sstevel@tonic-gate 
311*7c478bd9Sstevel@tonic-gate /*
312*7c478bd9Sstevel@tonic-gate  *  This looks up the /etc/vfstab entry given the device 'special'.
313*7c478bd9Sstevel@tonic-gate  *  It is called when the fstype is not specified on the command line.
314*7c478bd9Sstevel@tonic-gate  *
315*7c478bd9Sstevel@tonic-gate  *  The following global variables are used:
316*7c478bd9Sstevel@tonic-gate  *	special, fstype
317*7c478bd9Sstevel@tonic-gate  */
318*7c478bd9Sstevel@tonic-gate 
319*7c478bd9Sstevel@tonic-gate lookup()
320*7c478bd9Sstevel@tonic-gate {
321*7c478bd9Sstevel@tonic-gate 	FILE	*fd;
322*7c478bd9Sstevel@tonic-gate 	int	ret;
323*7c478bd9Sstevel@tonic-gate 	struct vfstab	vget, vref;
324*7c478bd9Sstevel@tonic-gate 
325*7c478bd9Sstevel@tonic-gate 	if ((fd = fopen(vfstab, "r")) == NULL) {
326*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: cannot open vfstab\n"), cbasename);
327*7c478bd9Sstevel@tonic-gate 		exit(1);
328*7c478bd9Sstevel@tonic-gate 	}
329*7c478bd9Sstevel@tonic-gate 	vfsnull(&vref);
330*7c478bd9Sstevel@tonic-gate 	vref.vfs_special = special;
331*7c478bd9Sstevel@tonic-gate 	ret = getvfsany(fd, &vget, &vref);
332*7c478bd9Sstevel@tonic-gate 	if (ret == -1) {
333*7c478bd9Sstevel@tonic-gate 		rewind(fd);
334*7c478bd9Sstevel@tonic-gate 		vfsnull(&vref);
335*7c478bd9Sstevel@tonic-gate 		vref.vfs_fsckdev = special;
336*7c478bd9Sstevel@tonic-gate 		ret = getvfsany(fd, &vget, &vref);
337*7c478bd9Sstevel@tonic-gate 	}
338*7c478bd9Sstevel@tonic-gate 	fclose(fd);
339*7c478bd9Sstevel@tonic-gate 
340*7c478bd9Sstevel@tonic-gate 	switch (ret) {
341*7c478bd9Sstevel@tonic-gate 	case -1:
342*7c478bd9Sstevel@tonic-gate 		fstype = default_fstype(special);
343*7c478bd9Sstevel@tonic-gate 		break;
344*7c478bd9Sstevel@tonic-gate 	case 0:
345*7c478bd9Sstevel@tonic-gate 		fstype = vget.vfs_fstype;
346*7c478bd9Sstevel@tonic-gate 		break;
347*7c478bd9Sstevel@tonic-gate 	case VFS_TOOLONG:
348*7c478bd9Sstevel@tonic-gate 		fprintf(stderr,
349*7c478bd9Sstevel@tonic-gate 		    gettext("%s: line in vfstab exceeds %d characters\n"),
350*7c478bd9Sstevel@tonic-gate 		    cbasename, VFS_LINE_MAX-2);
351*7c478bd9Sstevel@tonic-gate 		exit(1);
352*7c478bd9Sstevel@tonic-gate 		break;
353*7c478bd9Sstevel@tonic-gate 	case VFS_TOOFEW:
354*7c478bd9Sstevel@tonic-gate 		fprintf(stderr,
355*7c478bd9Sstevel@tonic-gate 		    gettext("%s: line in vfstab has too few entries\n"),
356*7c478bd9Sstevel@tonic-gate 		    cbasename);
357*7c478bd9Sstevel@tonic-gate 		exit(1);
358*7c478bd9Sstevel@tonic-gate 		break;
359*7c478bd9Sstevel@tonic-gate 	}
360*7c478bd9Sstevel@tonic-gate }
361*7c478bd9Sstevel@tonic-gate 
362*7c478bd9Sstevel@tonic-gate void
363*7c478bd9Sstevel@tonic-gate stat_snap(cmd, mountpoint, opts)
364*7c478bd9Sstevel@tonic-gate char *cmd;
365*7c478bd9Sstevel@tonic-gate char *mountpoint;
366*7c478bd9Sstevel@tonic-gate char *opts;
367*7c478bd9Sstevel@tonic-gate {
368*7c478bd9Sstevel@tonic-gate 	int fd; /* check mount point if given */
369*7c478bd9Sstevel@tonic-gate 	int en;
370*7c478bd9Sstevel@tonic-gate 	char *errstr;
371*7c478bd9Sstevel@tonic-gate 
372*7c478bd9Sstevel@tonic-gate 	if (mountpoint) {
373*7c478bd9Sstevel@tonic-gate 		if ((fd = open(mountpoint, O_RDONLY)) < 0) {
374*7c478bd9Sstevel@tonic-gate 			en = errno;
375*7c478bd9Sstevel@tonic-gate 			errstr = strerror(errno);
376*7c478bd9Sstevel@tonic-gate 			if (errstr == NULL)
377*7c478bd9Sstevel@tonic-gate 				errstr = gettext("Unknown error");
378*7c478bd9Sstevel@tonic-gate 
379*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("%s: %s: error %d: %s\n"),
380*7c478bd9Sstevel@tonic-gate 				cmd, mountpoint, en, errstr);
381*7c478bd9Sstevel@tonic-gate 
382*7c478bd9Sstevel@tonic-gate 			exit(2);
383*7c478bd9Sstevel@tonic-gate 		}
384*7c478bd9Sstevel@tonic-gate 		close(fd);
385*7c478bd9Sstevel@tonic-gate 	}
386*7c478bd9Sstevel@tonic-gate 	fssnap_show_status(mountpoint, opts, 1, (opts ? 0 : 1));
387*7c478bd9Sstevel@tonic-gate }
388