xref: /titanic_51/usr/src/cmd/fs.d/volcopy.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 /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
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	<errno.h>
36*7c478bd9Sstevel@tonic-gate #include	<stdarg.h>
37*7c478bd9Sstevel@tonic-gate #include	<sys/vfstab.h>
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #include	<locale.h>
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate static int perr(const char *fmt, ...);
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate #define	ARGV_MAX	1024
44*7c478bd9Sstevel@tonic-gate #define	FSTYPE_MAX	8
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate #define	VFS_PATH	"/usr/lib/fs"
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate #define	EQ(X,Y,Z)	!strncmp(X,Y,Z)
49*7c478bd9Sstevel@tonic-gate #define	NEWARG()\
50*7c478bd9Sstevel@tonic-gate 	(nargv[nargc++] = &argv[1][0],\
51*7c478bd9Sstevel@tonic-gate 	 nargc == ARGV_MAX ? perr("volcopy:  too many arguments.\n") : 1)
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate extern char	*default_fstype();
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate char	*nargv[ARGV_MAX];
56*7c478bd9Sstevel@tonic-gate int	nargc = 2;
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate char	vfstab[] = VFSTAB;
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate main(argc, argv)
61*7c478bd9Sstevel@tonic-gate 	int	argc;
62*7c478bd9Sstevel@tonic-gate 	char	**argv;
63*7c478bd9Sstevel@tonic-gate {
64*7c478bd9Sstevel@tonic-gate 	register char	cc;
65*7c478bd9Sstevel@tonic-gate 	register int	ii, Vflg = 0, Fflg = 0;
66*7c478bd9Sstevel@tonic-gate 	register char	*fstype = NULL;
67*7c478bd9Sstevel@tonic-gate 	register FILE	*fd;
68*7c478bd9Sstevel@tonic-gate 	struct vfstab	vget, vref;
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL,"");
71*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
72*7c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
73*7c478bd9Sstevel@tonic-gate #endif
74*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 	while (argc > 1 && argv[1][0] == '-') {
77*7c478bd9Sstevel@tonic-gate 		if (EQ(argv[1], "-a", 2)) {
78*7c478bd9Sstevel@tonic-gate 			NEWARG();
79*7c478bd9Sstevel@tonic-gate 		} else if (EQ(argv[1], "-e", 2)) {
80*7c478bd9Sstevel@tonic-gate 			NEWARG();
81*7c478bd9Sstevel@tonic-gate 		} else if (EQ(argv[1], "-s", 2)) {
82*7c478bd9Sstevel@tonic-gate 			NEWARG();
83*7c478bd9Sstevel@tonic-gate 		} else if (EQ(argv[1], "-y", 2)) {
84*7c478bd9Sstevel@tonic-gate 			NEWARG();
85*7c478bd9Sstevel@tonic-gate 		} else if (EQ(argv[1], "-buf", 4)) {
86*7c478bd9Sstevel@tonic-gate 			NEWARG();
87*7c478bd9Sstevel@tonic-gate 		} else if (EQ(argv[1], "-bpi", 4)) {
88*7c478bd9Sstevel@tonic-gate 			NEWARG();
89*7c478bd9Sstevel@tonic-gate 			if ((cc = argv[1][4]) < '0' || cc > '9') {
90*7c478bd9Sstevel@tonic-gate 				++argv;
91*7c478bd9Sstevel@tonic-gate 				--argc;
92*7c478bd9Sstevel@tonic-gate 				NEWARG();
93*7c478bd9Sstevel@tonic-gate 			}
94*7c478bd9Sstevel@tonic-gate 		} else if (EQ(argv[1], "-feet", 5)) {
95*7c478bd9Sstevel@tonic-gate 			NEWARG();
96*7c478bd9Sstevel@tonic-gate 			if ((cc = argv[1][5]) < '0' || cc > '9') {
97*7c478bd9Sstevel@tonic-gate 				++argv;
98*7c478bd9Sstevel@tonic-gate 				--argc;
99*7c478bd9Sstevel@tonic-gate 				NEWARG();
100*7c478bd9Sstevel@tonic-gate 			}
101*7c478bd9Sstevel@tonic-gate 		} else if (EQ(argv[1], "-reel", 5)) {
102*7c478bd9Sstevel@tonic-gate 			NEWARG();
103*7c478bd9Sstevel@tonic-gate 			if ((cc = argv[1][5]) < '0' || cc > '9') {
104*7c478bd9Sstevel@tonic-gate 				++argv;
105*7c478bd9Sstevel@tonic-gate 				--argc;
106*7c478bd9Sstevel@tonic-gate 				NEWARG();
107*7c478bd9Sstevel@tonic-gate 			}
108*7c478bd9Sstevel@tonic-gate 		} else if (EQ(argv[1], "-r", 2)) { /* 3b15 only */
109*7c478bd9Sstevel@tonic-gate 			NEWARG();
110*7c478bd9Sstevel@tonic-gate 			if ((cc = argv[1][2]) < '0' || cc > '9') {
111*7c478bd9Sstevel@tonic-gate 				++argv;
112*7c478bd9Sstevel@tonic-gate 				--argc;
113*7c478bd9Sstevel@tonic-gate 				NEWARG();
114*7c478bd9Sstevel@tonic-gate 			}
115*7c478bd9Sstevel@tonic-gate 		} else if (EQ(argv[1], "-block", 6)) { /* 3b15 only */
116*7c478bd9Sstevel@tonic-gate 			NEWARG();
117*7c478bd9Sstevel@tonic-gate 			if ((cc = argv[1][6]) < '0' || cc > '9') {
118*7c478bd9Sstevel@tonic-gate 				++argv;
119*7c478bd9Sstevel@tonic-gate 				--argc;
120*7c478bd9Sstevel@tonic-gate 				NEWARG();
121*7c478bd9Sstevel@tonic-gate 			}
122*7c478bd9Sstevel@tonic-gate 		} else if (EQ(argv[1], "-V", 2)) {
123*7c478bd9Sstevel@tonic-gate 			Vflg++;
124*7c478bd9Sstevel@tonic-gate 		} else if (EQ(argv[1], "-F", 2)) {
125*7c478bd9Sstevel@tonic-gate 			if (Fflg)
126*7c478bd9Sstevel@tonic-gate 				perr("volcopy: More than one FSType specified.\nUsage:\nvolcopy [-F FSType] [-V] [current_options] [-o specific_options] operands\n");
127*7c478bd9Sstevel@tonic-gate 			Fflg++;
128*7c478bd9Sstevel@tonic-gate 			if (argv[1][2] == '\0') {
129*7c478bd9Sstevel@tonic-gate 				++argv;
130*7c478bd9Sstevel@tonic-gate 				--argc;
131*7c478bd9Sstevel@tonic-gate 				if (argc == 1)
132*7c478bd9Sstevel@tonic-gate 					perr("Usage:\nvolcopy [-F FSType] [-V] [current_options] [-o specific_options] operands\n");
133*7c478bd9Sstevel@tonic-gate 				fstype = &argv[1][0];
134*7c478bd9Sstevel@tonic-gate 			} else
135*7c478bd9Sstevel@tonic-gate 				fstype = &argv[1][2];
136*7c478bd9Sstevel@tonic-gate 			if (strlen(fstype) > FSTYPE_MAX)
137*7c478bd9Sstevel@tonic-gate 				perr("volcopy: FSType %s exceeds %d characters\n", fstype, FSTYPE_MAX);
138*7c478bd9Sstevel@tonic-gate 		} else if (EQ(argv[1], "-o", 2)) {
139*7c478bd9Sstevel@tonic-gate 			NEWARG();
140*7c478bd9Sstevel@tonic-gate 			if (argv[1][2] == '\0') {
141*7c478bd9Sstevel@tonic-gate 				++argv;
142*7c478bd9Sstevel@tonic-gate 				--argc;
143*7c478bd9Sstevel@tonic-gate 				NEWARG();
144*7c478bd9Sstevel@tonic-gate 			}
145*7c478bd9Sstevel@tonic-gate 			if (Fflg && strlen(fstype) > FSTYPE_MAX)
146*7c478bd9Sstevel@tonic-gate 				perr("volcopy: FSType %s exceeds %d characters.\nUsage:\nvolcopy [-F FSType] [-V] [current_options] [-o specific_options] operands\n",fstype, FSTYPE_MAX);
147*7c478bd9Sstevel@tonic-gate 		} else if (EQ(argv[1], "-nosh", 5)) { /* 3b15 only */
148*7c478bd9Sstevel@tonic-gate 			NEWARG();
149*7c478bd9Sstevel@tonic-gate 		} else if (EQ(argv[1], "-?", 2)) {
150*7c478bd9Sstevel@tonic-gate 			if (Fflg) {
151*7c478bd9Sstevel@tonic-gate 				nargv[2] = "-?";
152*7c478bd9Sstevel@tonic-gate 				doexec(fstype, nargv);
153*7c478bd9Sstevel@tonic-gate 			}
154*7c478bd9Sstevel@tonic-gate 			else {
155*7c478bd9Sstevel@tonic-gate 				perr("Usage:\nvolcopy [-F FSType] [-V] [current_options] [-o specific_options] operands\n");
156*7c478bd9Sstevel@tonic-gate 		}
157*7c478bd9Sstevel@tonic-gate 		} else
158*7c478bd9Sstevel@tonic-gate 			perr("<%s> invalid option\nUsage:\nvolcopy [-F FSType] [-V] [current_options] [-o specific_options] operands\n",argv[1]);
159*7c478bd9Sstevel@tonic-gate 		++argv;
160*7c478bd9Sstevel@tonic-gate 		--argc;
161*7c478bd9Sstevel@tonic-gate 	} /* argv[1][0] == '-' */
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 	if (argc != 6) /* if mandatory fields not present */
164*7c478bd9Sstevel@tonic-gate 		perr("Usage:\nvolcopy [-F FSType] [-V] [current_options] [-o specific_options] operands\n");
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate 	if (nargc + 5 >= ARGV_MAX)
167*7c478bd9Sstevel@tonic-gate 		perr("volcopy: too many arguments.\n");
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 	for (ii = 0; ii < 5; ii++)
170*7c478bd9Sstevel@tonic-gate 		nargv[nargc++] = argv[ii+1];
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate 	if (fstype == NULL) {
173*7c478bd9Sstevel@tonic-gate 		if ((fd = fopen(vfstab, "r")) == NULL)
174*7c478bd9Sstevel@tonic-gate 			perr("volcopy: cannot open %s.\n", vfstab);
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 		vfsnull(&vref);
177*7c478bd9Sstevel@tonic-gate 		vref.vfs_special = argv[2];
178*7c478bd9Sstevel@tonic-gate 		ii = getvfsany(fd, &vget, &vref);
179*7c478bd9Sstevel@tonic-gate 		if (ii == -1) {
180*7c478bd9Sstevel@tonic-gate 			rewind(fd);
181*7c478bd9Sstevel@tonic-gate 			vfsnull(&vref);
182*7c478bd9Sstevel@tonic-gate 			vref.vfs_fsckdev = argv[2];
183*7c478bd9Sstevel@tonic-gate 			ii = getvfsany(fd, &vget, &vref);
184*7c478bd9Sstevel@tonic-gate 		}
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 		fclose(fd);
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 		switch (ii) {
189*7c478bd9Sstevel@tonic-gate 		case -1:
190*7c478bd9Sstevel@tonic-gate 			fstype = default_fstype(argv[2]);
191*7c478bd9Sstevel@tonic-gate 			break;
192*7c478bd9Sstevel@tonic-gate 		case 0:
193*7c478bd9Sstevel@tonic-gate 			fstype = vget.vfs_fstype;
194*7c478bd9Sstevel@tonic-gate 			break;
195*7c478bd9Sstevel@tonic-gate 		case VFS_TOOLONG:
196*7c478bd9Sstevel@tonic-gate 			perr("volcopy: line in vfstab exceeds %d characters\n", VFS_LINE_MAX-2);
197*7c478bd9Sstevel@tonic-gate 			break;
198*7c478bd9Sstevel@tonic-gate 		case VFS_TOOFEW:
199*7c478bd9Sstevel@tonic-gate 			perr("volcopy: line in vfstab has too few entries\n");
200*7c478bd9Sstevel@tonic-gate 			break;
201*7c478bd9Sstevel@tonic-gate 		case VFS_TOOMANY:
202*7c478bd9Sstevel@tonic-gate 			perr("volcopy: line in vfstab has too many entries\n");
203*7c478bd9Sstevel@tonic-gate 			break;
204*7c478bd9Sstevel@tonic-gate 		}
205*7c478bd9Sstevel@tonic-gate 	}
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate 	if (Vflg) {
208*7c478bd9Sstevel@tonic-gate 		printf("volcopy -F %s", fstype);
209*7c478bd9Sstevel@tonic-gate 		for (ii = 2; nargv[ii]; ii++)
210*7c478bd9Sstevel@tonic-gate 			printf(" %s", nargv[ii]);
211*7c478bd9Sstevel@tonic-gate 		printf("\n");
212*7c478bd9Sstevel@tonic-gate 		exit(0);
213*7c478bd9Sstevel@tonic-gate 	}
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate 	doexec(fstype, nargv);
216*7c478bd9Sstevel@tonic-gate }
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate doexec(fstype, nargv)
219*7c478bd9Sstevel@tonic-gate 	char	*fstype, *nargv[];
220*7c478bd9Sstevel@tonic-gate {
221*7c478bd9Sstevel@tonic-gate 	char	full_path[PATH_MAX];
222*7c478bd9Sstevel@tonic-gate 	char	*vfs_path = VFS_PATH;
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 	/* build the full pathname of the fstype dependent command. */
225*7c478bd9Sstevel@tonic-gate 	sprintf(full_path, "%s/%s/volcopy", vfs_path, fstype);
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 	/* set the new argv[0] to the filename */
228*7c478bd9Sstevel@tonic-gate 	nargv[1] = "volcopy";
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate 	/* Try to exec the fstype dependent portion of the mount. */
231*7c478bd9Sstevel@tonic-gate 	execv(full_path, &nargv[1]);
232*7c478bd9Sstevel@tonic-gate 	if (errno == EACCES) {
233*7c478bd9Sstevel@tonic-gate 		perr("volcopy: cannot execute %s - permission denied\n", full_path);
234*7c478bd9Sstevel@tonic-gate 		exit(1);
235*7c478bd9Sstevel@tonic-gate 	}
236*7c478bd9Sstevel@tonic-gate 	if (errno == ENOEXEC) {
237*7c478bd9Sstevel@tonic-gate 		nargv[0] = "sh";
238*7c478bd9Sstevel@tonic-gate 		nargv[1] = full_path;
239*7c478bd9Sstevel@tonic-gate 		execv("/sbin/sh", &nargv[0]);
240*7c478bd9Sstevel@tonic-gate 	}
241*7c478bd9Sstevel@tonic-gate 	perr("volcopy: Operation not applicable for FSType %s\n", fstype);
242*7c478bd9Sstevel@tonic-gate 	exit(1);
243*7c478bd9Sstevel@tonic-gate }
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate /*
246*7c478bd9Sstevel@tonic-gate  * perr:  Print error messages.
247*7c478bd9Sstevel@tonic-gate  */
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate static int
250*7c478bd9Sstevel@tonic-gate perr(const char *fmt, ...)
251*7c478bd9Sstevel@tonic-gate {
252*7c478bd9Sstevel@tonic-gate 	va_list ap;
253*7c478bd9Sstevel@tonic-gate 
254*7c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
255*7c478bd9Sstevel@tonic-gate 	(void)vfprintf(stderr, gettext(fmt), ap);
256*7c478bd9Sstevel@tonic-gate 	va_end(ap);
257*7c478bd9Sstevel@tonic-gate 	exit(1);
258*7c478bd9Sstevel@tonic-gate 	return (0);
259*7c478bd9Sstevel@tonic-gate }
260