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