xref: /titanic_51/usr/src/cmd/fs.d/volcopy.c (revision 081901271249c8ffce8241a6035a9f13fb1c0aa9)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*08190127Sdh145677  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include	<stdio.h>
347c478bd9Sstevel@tonic-gate #include 	<limits.h>
357c478bd9Sstevel@tonic-gate #include	<errno.h>
367c478bd9Sstevel@tonic-gate #include	<stdarg.h>
377c478bd9Sstevel@tonic-gate #include	<sys/vfstab.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include	<locale.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate static int perr(const char *fmt, ...);
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #define	ARGV_MAX	1024
447c478bd9Sstevel@tonic-gate #define	FSTYPE_MAX	8
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #define	VFS_PATH	"/usr/lib/fs"
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #define	EQ(X, Y, Z)	!strncmp(X, Y, Z)
497c478bd9Sstevel@tonic-gate #define	NEWARG()\
507c478bd9Sstevel@tonic-gate 	(nargv[nargc++] = &argv[1][0],\
517c478bd9Sstevel@tonic-gate 		nargc == ARGV_MAX ? perr("volcopy:  too many arguments.\n") : 1)
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate extern char	*default_fstype();
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate char	*nargv[ARGV_MAX];
567c478bd9Sstevel@tonic-gate int	nargc = 2;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate char	vfstab[] = VFSTAB;
597c478bd9Sstevel@tonic-gate 
60*08190127Sdh145677 static void doexec(char *fstype, char *nargv[]);
61*08190127Sdh145677 
62*08190127Sdh145677 int
63*08190127Sdh145677 main(int argc, char **argv)
647c478bd9Sstevel@tonic-gate {
65*08190127Sdh145677 	char	cc;
66*08190127Sdh145677 	int	ii, Vflg = 0, Fflg = 0;
67*08190127Sdh145677 	char	*fstype = NULL;
68*08190127Sdh145677 	FILE	*fd;
697c478bd9Sstevel@tonic-gate 	struct vfstab	vget, vref;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
727c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
737c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
747c478bd9Sstevel@tonic-gate #endif
757c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	while (argc > 1 && argv[1][0] == '-') {
787c478bd9Sstevel@tonic-gate 		if (EQ(argv[1], "-a", 2)) {
797c478bd9Sstevel@tonic-gate 			NEWARG();
807c478bd9Sstevel@tonic-gate 		} else if (EQ(argv[1], "-e", 2)) {
817c478bd9Sstevel@tonic-gate 			NEWARG();
827c478bd9Sstevel@tonic-gate 		} else if (EQ(argv[1], "-s", 2)) {
837c478bd9Sstevel@tonic-gate 			NEWARG();
847c478bd9Sstevel@tonic-gate 		} else if (EQ(argv[1], "-y", 2)) {
857c478bd9Sstevel@tonic-gate 			NEWARG();
867c478bd9Sstevel@tonic-gate 		} else if (EQ(argv[1], "-buf", 4)) {
877c478bd9Sstevel@tonic-gate 			NEWARG();
887c478bd9Sstevel@tonic-gate 		} else if (EQ(argv[1], "-bpi", 4)) {
897c478bd9Sstevel@tonic-gate 			NEWARG();
907c478bd9Sstevel@tonic-gate 			if ((cc = argv[1][4]) < '0' || cc > '9') {
917c478bd9Sstevel@tonic-gate 				++argv;
927c478bd9Sstevel@tonic-gate 				--argc;
937c478bd9Sstevel@tonic-gate 				NEWARG();
947c478bd9Sstevel@tonic-gate 			}
957c478bd9Sstevel@tonic-gate 		} else if (EQ(argv[1], "-feet", 5)) {
967c478bd9Sstevel@tonic-gate 			NEWARG();
977c478bd9Sstevel@tonic-gate 			if ((cc = argv[1][5]) < '0' || cc > '9') {
987c478bd9Sstevel@tonic-gate 				++argv;
997c478bd9Sstevel@tonic-gate 				--argc;
1007c478bd9Sstevel@tonic-gate 				NEWARG();
1017c478bd9Sstevel@tonic-gate 			}
1027c478bd9Sstevel@tonic-gate 		} else if (EQ(argv[1], "-reel", 5)) {
1037c478bd9Sstevel@tonic-gate 			NEWARG();
1047c478bd9Sstevel@tonic-gate 			if ((cc = argv[1][5]) < '0' || cc > '9') {
1057c478bd9Sstevel@tonic-gate 				++argv;
1067c478bd9Sstevel@tonic-gate 				--argc;
1077c478bd9Sstevel@tonic-gate 				NEWARG();
1087c478bd9Sstevel@tonic-gate 			}
1097c478bd9Sstevel@tonic-gate 		} else if (EQ(argv[1], "-r", 2)) { /* 3b15 only */
1107c478bd9Sstevel@tonic-gate 			NEWARG();
1117c478bd9Sstevel@tonic-gate 			if ((cc = argv[1][2]) < '0' || cc > '9') {
1127c478bd9Sstevel@tonic-gate 				++argv;
1137c478bd9Sstevel@tonic-gate 				--argc;
1147c478bd9Sstevel@tonic-gate 				NEWARG();
1157c478bd9Sstevel@tonic-gate 			}
1167c478bd9Sstevel@tonic-gate 		} else if (EQ(argv[1], "-block", 6)) { /* 3b15 only */
1177c478bd9Sstevel@tonic-gate 			NEWARG();
1187c478bd9Sstevel@tonic-gate 			if ((cc = argv[1][6]) < '0' || cc > '9') {
1197c478bd9Sstevel@tonic-gate 				++argv;
1207c478bd9Sstevel@tonic-gate 				--argc;
1217c478bd9Sstevel@tonic-gate 				NEWARG();
1227c478bd9Sstevel@tonic-gate 			}
1237c478bd9Sstevel@tonic-gate 		} else if (EQ(argv[1], "-V", 2)) {
1247c478bd9Sstevel@tonic-gate 			Vflg++;
1257c478bd9Sstevel@tonic-gate 		} else if (EQ(argv[1], "-F", 2)) {
1267c478bd9Sstevel@tonic-gate 			if (Fflg)
127*08190127Sdh145677 				perr("volcopy: More than one"
128*08190127Sdh145677 					"FSType specified.\n"
129*08190127Sdh145677 					"Usage:\nvolcopy [-F FSType] [-V]"
130*08190127Sdh145677 					" [current_options] [-o "
131*08190127Sdh145677 					"specific_options] operands\n");
1327c478bd9Sstevel@tonic-gate 			Fflg++;
1337c478bd9Sstevel@tonic-gate 			if (argv[1][2] == '\0') {
1347c478bd9Sstevel@tonic-gate 				++argv;
1357c478bd9Sstevel@tonic-gate 				--argc;
1367c478bd9Sstevel@tonic-gate 				if (argc == 1)
137*08190127Sdh145677 					perr("Usage:\nvolcopy [-F FSType] [-V]"
138*08190127Sdh145677 						" [current_options] [-o "
139*08190127Sdh145677 						"specific_options] operands\n");
1407c478bd9Sstevel@tonic-gate 				fstype = &argv[1][0];
1417c478bd9Sstevel@tonic-gate 			} else
1427c478bd9Sstevel@tonic-gate 				fstype = &argv[1][2];
1437c478bd9Sstevel@tonic-gate 			if (strlen(fstype) > FSTYPE_MAX)
144*08190127Sdh145677 				perr("volcopy: FSType %s exceeds %d"
145*08190127Sdh145677 					" characters\n", fstype, FSTYPE_MAX);
1467c478bd9Sstevel@tonic-gate 		} else if (EQ(argv[1], "-o", 2)) {
1477c478bd9Sstevel@tonic-gate 			NEWARG();
1487c478bd9Sstevel@tonic-gate 			if (argv[1][2] == '\0') {
1497c478bd9Sstevel@tonic-gate 				++argv;
1507c478bd9Sstevel@tonic-gate 				--argc;
1517c478bd9Sstevel@tonic-gate 				NEWARG();
1527c478bd9Sstevel@tonic-gate 			}
1537c478bd9Sstevel@tonic-gate 			if (Fflg && strlen(fstype) > FSTYPE_MAX)
154*08190127Sdh145677 				perr("volcopy: FSType %s exceeds %d "
155*08190127Sdh145677 					"characters.\nUsage:\nvolcopy "
156*08190127Sdh145677 					"[-F FSType] [-V] [current_options] "
157*08190127Sdh145677 					"[-o specific_options] "
158*08190127Sdh145677 					"operands\n", fstype, FSTYPE_MAX);
1597c478bd9Sstevel@tonic-gate 		} else if (EQ(argv[1], "-nosh", 5)) { /* 3b15 only */
1607c478bd9Sstevel@tonic-gate 			NEWARG();
1617c478bd9Sstevel@tonic-gate 		} else if (EQ(argv[1], "-?", 2)) {
1627c478bd9Sstevel@tonic-gate 			if (Fflg) {
1637c478bd9Sstevel@tonic-gate 				nargv[2] = "-?";
1647c478bd9Sstevel@tonic-gate 				doexec(fstype, nargv);
165*08190127Sdh145677 			} else {
166*08190127Sdh145677 				perr("Usage:\nvolcopy [-F FSType] [-V] "
167*08190127Sdh145677 					"[current_options] [-o "
168*08190127Sdh145677 					"specific_options] operands\n");
1697c478bd9Sstevel@tonic-gate 		}
1707c478bd9Sstevel@tonic-gate 		} else
171*08190127Sdh145677 			perr("<%s> invalid option\nUsage:\n"
172*08190127Sdh145677 				"volcopy [-F FSType] [-V] "
173*08190127Sdh145677 				"[current_options] [-o "
174*08190127Sdh145677 				"specific_options] operands\n", argv[1]);
1757c478bd9Sstevel@tonic-gate 		++argv;
1767c478bd9Sstevel@tonic-gate 		--argc;
1777c478bd9Sstevel@tonic-gate 	} /* argv[1][0] == '-' */
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	if (argc != 6) /* if mandatory fields not present */
180*08190127Sdh145677 		perr("Usage:\nvolcopy [-F FSType] [-V] "
181*08190127Sdh145677 			"[current_options] [-o "
182*08190127Sdh145677 			"specific_options] operands\n");
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	if (nargc + 5 >= ARGV_MAX)
1857c478bd9Sstevel@tonic-gate 		perr("volcopy: too many arguments.\n");
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	for (ii = 0; ii < 5; ii++)
1887c478bd9Sstevel@tonic-gate 		nargv[nargc++] = argv[ii+1];
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	if (fstype == NULL) {
1917c478bd9Sstevel@tonic-gate 		if ((fd = fopen(vfstab, "r")) == NULL)
1927c478bd9Sstevel@tonic-gate 			perr("volcopy: cannot open %s.\n", vfstab);
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 		vfsnull(&vref);
1957c478bd9Sstevel@tonic-gate 		vref.vfs_special = argv[2];
1967c478bd9Sstevel@tonic-gate 		ii = getvfsany(fd, &vget, &vref);
1977c478bd9Sstevel@tonic-gate 		if (ii == -1) {
1987c478bd9Sstevel@tonic-gate 			rewind(fd);
1997c478bd9Sstevel@tonic-gate 			vfsnull(&vref);
2007c478bd9Sstevel@tonic-gate 			vref.vfs_fsckdev = argv[2];
2017c478bd9Sstevel@tonic-gate 			ii = getvfsany(fd, &vget, &vref);
2027c478bd9Sstevel@tonic-gate 		}
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 		fclose(fd);
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 		switch (ii) {
2077c478bd9Sstevel@tonic-gate 		case -1:
2087c478bd9Sstevel@tonic-gate 			fstype = default_fstype(argv[2]);
2097c478bd9Sstevel@tonic-gate 			break;
2107c478bd9Sstevel@tonic-gate 		case 0:
2117c478bd9Sstevel@tonic-gate 			fstype = vget.vfs_fstype;
2127c478bd9Sstevel@tonic-gate 			break;
2137c478bd9Sstevel@tonic-gate 		case VFS_TOOLONG:
214*08190127Sdh145677 			perr("volcopy: line in vfstab exceeds "
215*08190127Sdh145677 				"%d characters\n", VFS_LINE_MAX-2);
2167c478bd9Sstevel@tonic-gate 			break;
2177c478bd9Sstevel@tonic-gate 		case VFS_TOOFEW:
2187c478bd9Sstevel@tonic-gate 			perr("volcopy: line in vfstab has too few entries\n");
2197c478bd9Sstevel@tonic-gate 			break;
2207c478bd9Sstevel@tonic-gate 		case VFS_TOOMANY:
2217c478bd9Sstevel@tonic-gate 			perr("volcopy: line in vfstab has too many entries\n");
2227c478bd9Sstevel@tonic-gate 			break;
223*08190127Sdh145677 		default:
224*08190127Sdh145677 			break;
2257c478bd9Sstevel@tonic-gate 		}
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	if (Vflg) {
2297c478bd9Sstevel@tonic-gate 		printf("volcopy -F %s", fstype);
2307c478bd9Sstevel@tonic-gate 		for (ii = 2; nargv[ii]; ii++)
2317c478bd9Sstevel@tonic-gate 			printf(" %s", nargv[ii]);
2327c478bd9Sstevel@tonic-gate 		printf("\n");
2337c478bd9Sstevel@tonic-gate 		exit(0);
2347c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	doexec(fstype, nargv);
237*08190127Sdh145677 	return (0);
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate 
240*08190127Sdh145677 static void
241*08190127Sdh145677 doexec(char *fstype, char *nargv[])
2427c478bd9Sstevel@tonic-gate {
2437c478bd9Sstevel@tonic-gate 	char	full_path[PATH_MAX];
2447c478bd9Sstevel@tonic-gate 	char	*vfs_path = VFS_PATH;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	/* build the full pathname of the fstype dependent command. */
2477c478bd9Sstevel@tonic-gate 	sprintf(full_path, "%s/%s/volcopy", vfs_path, fstype);
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	/* set the new argv[0] to the filename */
2507c478bd9Sstevel@tonic-gate 	nargv[1] = "volcopy";
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	/* Try to exec the fstype dependent portion of the mount. */
2537c478bd9Sstevel@tonic-gate 	execv(full_path, &nargv[1]);
2547c478bd9Sstevel@tonic-gate 	if (errno == EACCES) {
255*08190127Sdh145677 		perr("volcopy: cannot execute %s"
256*08190127Sdh145677 			" - permission denied\n", full_path);
2577c478bd9Sstevel@tonic-gate 		exit(1);
2587c478bd9Sstevel@tonic-gate 	}
2597c478bd9Sstevel@tonic-gate 	if (errno == ENOEXEC) {
2607c478bd9Sstevel@tonic-gate 		nargv[0] = "sh";
2617c478bd9Sstevel@tonic-gate 		nargv[1] = full_path;
2627c478bd9Sstevel@tonic-gate 		execv("/sbin/sh", &nargv[0]);
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate 	perr("volcopy: Operation not applicable for FSType %s\n", fstype);
2657c478bd9Sstevel@tonic-gate 	exit(1);
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate /*
2697c478bd9Sstevel@tonic-gate  * perr:  Print error messages.
2707c478bd9Sstevel@tonic-gate  */
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate static int
2737c478bd9Sstevel@tonic-gate perr(const char *fmt, ...)
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate 	va_list ap;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
2787c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, gettext(fmt), ap);
2797c478bd9Sstevel@tonic-gate 	va_end(ap);
2807c478bd9Sstevel@tonic-gate 	exit(1);
2817c478bd9Sstevel@tonic-gate 	return (0);
2827c478bd9Sstevel@tonic-gate }
283