xref: /illumos-gate/usr/src/cmd/fs.d/ufs/lockfs/lockfs.c (revision 2a8bcb4e)
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
5*d1a180b0Smaheshvs  * Common Development and Distribution License (the "License").
6*d1a180b0Smaheshvs  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*d1a180b0Smaheshvs  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * lockfs
287c478bd9Sstevel@tonic-gate  *	user interface to lockfs functionality
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <unistd.h>
347c478bd9Sstevel@tonic-gate #include <fcntl.h>
357c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
367c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
377c478bd9Sstevel@tonic-gate #include <errno.h>
387c478bd9Sstevel@tonic-gate #include <sys/lockfs.h>
397c478bd9Sstevel@tonic-gate #include <sys/filio.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #define	bzero(s, n)	memset(s, 0, n);
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  * command line processing
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate extern char	*optarg;
477c478bd9Sstevel@tonic-gate extern int	optind;
487c478bd9Sstevel@tonic-gate extern int	opterr;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate extern void exit();
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate static void exitusage();
53*d1a180b0Smaheshvs static void printstatusline(char *, char *, char *);
54*d1a180b0Smaheshvs static void printstatus(char *);
55*d1a180b0Smaheshvs static void flushfs(char *);
56*d1a180b0Smaheshvs static void lockfs(char *);
577c478bd9Sstevel@tonic-gate static void getmntnames();
58*d1a180b0Smaheshvs static void getcmdnames(int, char **, int);
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate  * -a = all
627c478bd9Sstevel@tonic-gate  * -v = verbose
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate int all		= 0;
657c478bd9Sstevel@tonic-gate int verbose	= 0;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * exitstatus
697c478bd9Sstevel@tonic-gate  *	0 all ok
707c478bd9Sstevel@tonic-gate  *	1 internal error
717c478bd9Sstevel@tonic-gate  *	2 system call error
727c478bd9Sstevel@tonic-gate  */
737c478bd9Sstevel@tonic-gate int exitstatus	= 0;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * list of filenames
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate struct filename {
797c478bd9Sstevel@tonic-gate 	struct filename	*fn_next;
807c478bd9Sstevel@tonic-gate 	char		*fn_name;
817c478bd9Sstevel@tonic-gate };
827c478bd9Sstevel@tonic-gate struct filename	*fnanchor	= 0;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate  * default request is `file system lock status'
867c478bd9Sstevel@tonic-gate  * default lock type is `unlock'
877c478bd9Sstevel@tonic-gate  * -wnduhfe changes them
887c478bd9Sstevel@tonic-gate  */
897c478bd9Sstevel@tonic-gate int request	= _FIOLFSS;
90*d1a180b0Smaheshvs ushort_t	lock	= LOCKFS_ULOCK;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /*
937c478bd9Sstevel@tonic-gate  * default comment is null
947c478bd9Sstevel@tonic-gate  *	-c changes it
957c478bd9Sstevel@tonic-gate  */
967c478bd9Sstevel@tonic-gate caddr_t comment	= 0;
97*d1a180b0Smaheshvs ulong_t	comlen	= 0;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /*
1007c478bd9Sstevel@tonic-gate  * for prettyprint
1017c478bd9Sstevel@tonic-gate  */
1027c478bd9Sstevel@tonic-gate int firsttime	= 0;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * no unlocks printed
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate int no_unlocks_printed	= 0;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate  * file system was modified during hlock/wlock/elock
1117c478bd9Sstevel@tonic-gate  */
1127c478bd9Sstevel@tonic-gate #define	LOCKWARN(FN, S)	\
1137c478bd9Sstevel@tonic-gate { \
1147c478bd9Sstevel@tonic-gate 	if (verbose) \
1157c478bd9Sstevel@tonic-gate 		printf("WARNING: %s was modified while %s locked\n", FN, S); \
1167c478bd9Sstevel@tonic-gate 	exitstatus = 2; \
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate /*
1207c478bd9Sstevel@tonic-gate  * forward reference
1217c478bd9Sstevel@tonic-gate  */
1227c478bd9Sstevel@tonic-gate char	*malloc();
1237c478bd9Sstevel@tonic-gate 
124*d1a180b0Smaheshvs int
main(int argc,char * argv[])125*d1a180b0Smaheshvs main(int argc, char *argv[])
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate 	int		c;
1287c478bd9Sstevel@tonic-gate 	struct filename	*fnp;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	exitstatus = 0;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	/*
1337c478bd9Sstevel@tonic-gate 	 * process command line
1347c478bd9Sstevel@tonic-gate 	 */
1357c478bd9Sstevel@tonic-gate 	opterr = 0;
1367c478bd9Sstevel@tonic-gate 	optarg = 0;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "vfwnduheac:")) != -1)
1397c478bd9Sstevel@tonic-gate 		switch (c) {
1407c478bd9Sstevel@tonic-gate 		case 'v':
1417c478bd9Sstevel@tonic-gate 			verbose = 1;
1427c478bd9Sstevel@tonic-gate 			break;
1437c478bd9Sstevel@tonic-gate 		case 'f':
1447c478bd9Sstevel@tonic-gate 			request = _FIOFFS;
1457c478bd9Sstevel@tonic-gate 			break;
1467c478bd9Sstevel@tonic-gate 		case 'w':
1477c478bd9Sstevel@tonic-gate 			lock    = LOCKFS_WLOCK;
1487c478bd9Sstevel@tonic-gate 			request = _FIOLFS;
1497c478bd9Sstevel@tonic-gate 			break;
1507c478bd9Sstevel@tonic-gate 		case 'n':
1517c478bd9Sstevel@tonic-gate 			lock    = LOCKFS_NLOCK;
1527c478bd9Sstevel@tonic-gate 			request = _FIOLFS;
1537c478bd9Sstevel@tonic-gate 			break;
1547c478bd9Sstevel@tonic-gate 		case 'd':
1557c478bd9Sstevel@tonic-gate 			lock    = LOCKFS_DLOCK;
1567c478bd9Sstevel@tonic-gate 			request = _FIOLFS;
1577c478bd9Sstevel@tonic-gate 			break;
1587c478bd9Sstevel@tonic-gate 		case 'h':
1597c478bd9Sstevel@tonic-gate 			lock    = LOCKFS_HLOCK;
1607c478bd9Sstevel@tonic-gate 			request = _FIOLFS;
1617c478bd9Sstevel@tonic-gate 			break;
1627c478bd9Sstevel@tonic-gate 		case 'e':
1637c478bd9Sstevel@tonic-gate 			lock	= LOCKFS_ELOCK;
1647c478bd9Sstevel@tonic-gate 			request = _FIOLFS;
1657c478bd9Sstevel@tonic-gate 			break;
1667c478bd9Sstevel@tonic-gate 		case 'u':
1677c478bd9Sstevel@tonic-gate 			lock    = LOCKFS_ULOCK;
1687c478bd9Sstevel@tonic-gate 			request = _FIOLFS;
1697c478bd9Sstevel@tonic-gate 			break;
1707c478bd9Sstevel@tonic-gate 		case 'a':
1717c478bd9Sstevel@tonic-gate 			all = 1;
1727c478bd9Sstevel@tonic-gate 			break;
1737c478bd9Sstevel@tonic-gate 		case 'c':
1747c478bd9Sstevel@tonic-gate 			comment = optarg;
1757c478bd9Sstevel@tonic-gate 			comlen  = strlen(optarg)+1;
1767c478bd9Sstevel@tonic-gate 			request = _FIOLFS;
1777c478bd9Sstevel@tonic-gate 			break;
1787c478bd9Sstevel@tonic-gate 		default:
1797c478bd9Sstevel@tonic-gate 			exitusage();
1807c478bd9Sstevel@tonic-gate 			break;
1817c478bd9Sstevel@tonic-gate 		}
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	if (argc == 1) {
1847c478bd9Sstevel@tonic-gate 		no_unlocks_printed = 1;
1857c478bd9Sstevel@tonic-gate 		all = 1;
1867c478bd9Sstevel@tonic-gate 	}
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	if (all)
1897c478bd9Sstevel@tonic-gate 		/*
1907c478bd9Sstevel@tonic-gate 		 * use /etc/mtab
1917c478bd9Sstevel@tonic-gate 		 */
1927c478bd9Sstevel@tonic-gate 		getmntnames();
1937c478bd9Sstevel@tonic-gate 	else
1947c478bd9Sstevel@tonic-gate 		/*
1957c478bd9Sstevel@tonic-gate 		 * use command line
1967c478bd9Sstevel@tonic-gate 		 */
1977c478bd9Sstevel@tonic-gate 		getcmdnames(argc, argv, optind);
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	/*
2007c478bd9Sstevel@tonic-gate 	 * for each filename, doit
2017c478bd9Sstevel@tonic-gate 	 */
2027c478bd9Sstevel@tonic-gate 	for (fnp = fnanchor; fnp; fnp = fnp->fn_next) {
2037c478bd9Sstevel@tonic-gate 		switch (request) {
2047c478bd9Sstevel@tonic-gate 		case _FIOLFSS:
2057c478bd9Sstevel@tonic-gate 			printstatus(fnp->fn_name);
2067c478bd9Sstevel@tonic-gate 			break;
2077c478bd9Sstevel@tonic-gate 		case _FIOLFS:
2087c478bd9Sstevel@tonic-gate 			lockfs(fnp->fn_name);
2097c478bd9Sstevel@tonic-gate 			break;
2107c478bd9Sstevel@tonic-gate 		case _FIOFFS:
2117c478bd9Sstevel@tonic-gate 			flushfs(fnp->fn_name);
2127c478bd9Sstevel@tonic-gate 			break;
2137c478bd9Sstevel@tonic-gate 		default:
2147c478bd9Sstevel@tonic-gate 			break;
2157c478bd9Sstevel@tonic-gate 		}
2167c478bd9Sstevel@tonic-gate 	}
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	/*
2197c478bd9Sstevel@tonic-gate 	 * all done
2207c478bd9Sstevel@tonic-gate 	 */
221*d1a180b0Smaheshvs 	return (exitstatus);
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate /*
2247c478bd9Sstevel@tonic-gate  * exitusage
2257c478bd9Sstevel@tonic-gate  *	bad command line, give hint
2267c478bd9Sstevel@tonic-gate  */
2277c478bd9Sstevel@tonic-gate void
exitusage()2287c478bd9Sstevel@tonic-gate exitusage()
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate 	printf("usage: lockfs [-dfhnuw] [-c string] [-a] [file system ...]\n");
2317c478bd9Sstevel@tonic-gate 	exit(1);
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate /*
2347c478bd9Sstevel@tonic-gate  * printstatusline
2357c478bd9Sstevel@tonic-gate  * 	prettyprint the status line
2367c478bd9Sstevel@tonic-gate  */
2377c478bd9Sstevel@tonic-gate void
printstatusline(char * fn,char * locktype,char * comment)238*d1a180b0Smaheshvs printstatusline(char *fn, char *locktype, char *comment)
2397c478bd9Sstevel@tonic-gate {
2407c478bd9Sstevel@tonic-gate 	if (firsttime++ == 0)
2417c478bd9Sstevel@tonic-gate 		printf("%-20s %-10s %s\n", "Filesystem", "Locktype", "Comment");
2427c478bd9Sstevel@tonic-gate 	printf("%-20s %-10s %s\n", fn, locktype, comment);
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate /*
2457c478bd9Sstevel@tonic-gate  * printstatus
2467c478bd9Sstevel@tonic-gate  *	get and prettyprint file system lock status
2477c478bd9Sstevel@tonic-gate  */
2487c478bd9Sstevel@tonic-gate void
printstatus(char * fn)249*d1a180b0Smaheshvs printstatus(char *fn)
2507c478bd9Sstevel@tonic-gate {
2517c478bd9Sstevel@tonic-gate 	int		fd;
2527c478bd9Sstevel@tonic-gate 	int		fsmod	= 0;
2537c478bd9Sstevel@tonic-gate 	char		*locktype;
2547c478bd9Sstevel@tonic-gate 	char		commentbuffer[LOCKFS_MAXCOMMENTLEN+1];
2557c478bd9Sstevel@tonic-gate 	struct lockfs	lf;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	fd = open64(fn, O_RDONLY);
2587c478bd9Sstevel@tonic-gate 	if (fd == -1) {
2597c478bd9Sstevel@tonic-gate 		if (errno == EIO)
2607c478bd9Sstevel@tonic-gate 			printstatusline(fn, "EIO", "May be hard locked");
2617c478bd9Sstevel@tonic-gate 		else
2627c478bd9Sstevel@tonic-gate 			perror(fn);
2637c478bd9Sstevel@tonic-gate 		exitstatus = 2;
2647c478bd9Sstevel@tonic-gate 		return;
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	bzero((caddr_t)&lf, sizeof (struct lockfs));
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	lf.lf_flags   = LOCKFS_MOD;
2707c478bd9Sstevel@tonic-gate 	lf.lf_comlen  = LOCKFS_MAXCOMMENTLEN;
2717c478bd9Sstevel@tonic-gate 	lf.lf_comment = commentbuffer;
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	if (ioctl(fd, _FIOLFSS, &lf) == -1) {
2747c478bd9Sstevel@tonic-gate 		perror(fn);
2757c478bd9Sstevel@tonic-gate 		close(fd);
2767c478bd9Sstevel@tonic-gate 		exitstatus = 2;
2777c478bd9Sstevel@tonic-gate 		return;
2787c478bd9Sstevel@tonic-gate 	}
2797c478bd9Sstevel@tonic-gate 	switch (lf.lf_lock) {
2807c478bd9Sstevel@tonic-gate 	case LOCKFS_ULOCK:
2817c478bd9Sstevel@tonic-gate 		if (no_unlocks_printed)
2827c478bd9Sstevel@tonic-gate 			goto out;
2837c478bd9Sstevel@tonic-gate 		if (LOCKFS_IS_BUSY(&lf))
2847c478bd9Sstevel@tonic-gate 			locktype = "(unlock)";
2857c478bd9Sstevel@tonic-gate 		else
2867c478bd9Sstevel@tonic-gate 			locktype = "unlock";
2877c478bd9Sstevel@tonic-gate 		break;
2887c478bd9Sstevel@tonic-gate 	case LOCKFS_WLOCK:
2897c478bd9Sstevel@tonic-gate 		if (LOCKFS_IS_BUSY(&lf))
2907c478bd9Sstevel@tonic-gate 			locktype = "(write)";
2917c478bd9Sstevel@tonic-gate 		else {
2927c478bd9Sstevel@tonic-gate 			locktype = "write";
2937c478bd9Sstevel@tonic-gate 			fsmod = LOCKFS_IS_MOD(&lf);
2947c478bd9Sstevel@tonic-gate 		}
2957c478bd9Sstevel@tonic-gate 		break;
2967c478bd9Sstevel@tonic-gate 	case LOCKFS_NLOCK:
2977c478bd9Sstevel@tonic-gate 		if (LOCKFS_IS_BUSY(&lf))
2987c478bd9Sstevel@tonic-gate 			locktype = "(name)";
2997c478bd9Sstevel@tonic-gate 		else
3007c478bd9Sstevel@tonic-gate 			locktype = "name";
3017c478bd9Sstevel@tonic-gate 		break;
3027c478bd9Sstevel@tonic-gate 	case LOCKFS_DLOCK:
3037c478bd9Sstevel@tonic-gate 		locktype = "delete";
3047c478bd9Sstevel@tonic-gate 		if (LOCKFS_IS_BUSY(&lf))
3057c478bd9Sstevel@tonic-gate 			locktype = "(delete)";
3067c478bd9Sstevel@tonic-gate 		else
3077c478bd9Sstevel@tonic-gate 			locktype = "delete";
3087c478bd9Sstevel@tonic-gate 		break;
3097c478bd9Sstevel@tonic-gate 	case LOCKFS_HLOCK:
3107c478bd9Sstevel@tonic-gate 		if (LOCKFS_IS_BUSY(&lf))
3117c478bd9Sstevel@tonic-gate 			locktype = "(hard)";
3127c478bd9Sstevel@tonic-gate 		else {
3137c478bd9Sstevel@tonic-gate 			locktype = "hard";
3147c478bd9Sstevel@tonic-gate 			fsmod = LOCKFS_IS_MOD(&lf);
3157c478bd9Sstevel@tonic-gate 		}
3167c478bd9Sstevel@tonic-gate 		break;
3177c478bd9Sstevel@tonic-gate 	case LOCKFS_ELOCK:
3187c478bd9Sstevel@tonic-gate 		if (LOCKFS_IS_BUSY(&lf))
3197c478bd9Sstevel@tonic-gate 			locktype = "(error)";
3207c478bd9Sstevel@tonic-gate 		else {
3217c478bd9Sstevel@tonic-gate 			locktype = "error";
3227c478bd9Sstevel@tonic-gate 			fsmod = LOCKFS_IS_MOD(&lf);
3237c478bd9Sstevel@tonic-gate 		}
3247c478bd9Sstevel@tonic-gate 		break;
3257c478bd9Sstevel@tonic-gate 	default:
3267c478bd9Sstevel@tonic-gate 		if (LOCKFS_IS_BUSY(&lf))
3277c478bd9Sstevel@tonic-gate 			locktype = "(unknown)";
3287c478bd9Sstevel@tonic-gate 		else
3297c478bd9Sstevel@tonic-gate 			locktype = "unknown";
3307c478bd9Sstevel@tonic-gate 		break;
3317c478bd9Sstevel@tonic-gate 	}
3327c478bd9Sstevel@tonic-gate 	lf.lf_comment[lf.lf_comlen] = '\0';
3337c478bd9Sstevel@tonic-gate 	printstatusline(fn, locktype, lf.lf_comment);
3347c478bd9Sstevel@tonic-gate 	if (fsmod)
3357c478bd9Sstevel@tonic-gate 		LOCKWARN(fn, locktype);
3367c478bd9Sstevel@tonic-gate out:
3377c478bd9Sstevel@tonic-gate 	close(fd);
3387c478bd9Sstevel@tonic-gate }
3397c478bd9Sstevel@tonic-gate /*
3407c478bd9Sstevel@tonic-gate  * flushfs
3417c478bd9Sstevel@tonic-gate  *	push and invalidate at least the data that is *currently* dirty
3427c478bd9Sstevel@tonic-gate  */
3437c478bd9Sstevel@tonic-gate void
flushfs(char * fn)344*d1a180b0Smaheshvs flushfs(char *fn)
3457c478bd9Sstevel@tonic-gate {
3467c478bd9Sstevel@tonic-gate 	int		fd;
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	fd = open64(fn, O_RDONLY);
3497c478bd9Sstevel@tonic-gate 	if (fd == -1) {
3507c478bd9Sstevel@tonic-gate 		perror(fn);
3517c478bd9Sstevel@tonic-gate 		exitstatus = 2;
3527c478bd9Sstevel@tonic-gate 		return;
3537c478bd9Sstevel@tonic-gate 	}
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	if (ioctl(fd, _FIOFFS, NULL) == -1) {
3567c478bd9Sstevel@tonic-gate 		perror(fn);
3577c478bd9Sstevel@tonic-gate 		close(fd);
3587c478bd9Sstevel@tonic-gate 		exitstatus = 2;
3597c478bd9Sstevel@tonic-gate 		return;
3607c478bd9Sstevel@tonic-gate 	}
3617c478bd9Sstevel@tonic-gate 	close(fd);
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate /*
3647c478bd9Sstevel@tonic-gate  * lockfs
3657c478bd9Sstevel@tonic-gate  *	lock the file system
3667c478bd9Sstevel@tonic-gate  */
3677c478bd9Sstevel@tonic-gate void
lockfs(char * fn)368*d1a180b0Smaheshvs lockfs(char *fn)
3697c478bd9Sstevel@tonic-gate {
3707c478bd9Sstevel@tonic-gate 	int		fd;
3717c478bd9Sstevel@tonic-gate 	struct lockfs	lf;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	fd = open64(fn, O_RDONLY);
3747c478bd9Sstevel@tonic-gate 	if (fd == -1) {
3757c478bd9Sstevel@tonic-gate 		perror(fn);
3767c478bd9Sstevel@tonic-gate 		exitstatus = 2;
3777c478bd9Sstevel@tonic-gate 		return;
3787c478bd9Sstevel@tonic-gate 	}
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	bzero((caddr_t)&lf, sizeof (struct lockfs));
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	lf.lf_flags = LOCKFS_MOD;
3837c478bd9Sstevel@tonic-gate 	if (ioctl(fd, _FIOLFSS, &lf) == -1) {
3847c478bd9Sstevel@tonic-gate 		perror(fn);
3857c478bd9Sstevel@tonic-gate 		close(fd);
3867c478bd9Sstevel@tonic-gate 		exitstatus = 2;
3877c478bd9Sstevel@tonic-gate 		return;
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	if (!LOCKFS_IS_BUSY(&lf) && LOCKFS_IS_MOD(&lf)) {
3917c478bd9Sstevel@tonic-gate 		if (LOCKFS_IS_HLOCK(&lf))
3927c478bd9Sstevel@tonic-gate 			LOCKWARN(fn, "hard");
3937c478bd9Sstevel@tonic-gate 		if (LOCKFS_IS_ELOCK(&lf))
3947c478bd9Sstevel@tonic-gate 			LOCKWARN(fn, "error");
3957c478bd9Sstevel@tonic-gate 		if (LOCKFS_IS_WLOCK(&lf))
3967c478bd9Sstevel@tonic-gate 			LOCKWARN(fn, "write");
3977c478bd9Sstevel@tonic-gate 	}
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	lf.lf_lock	= lock;
4007c478bd9Sstevel@tonic-gate 	lf.lf_flags	= 0;
4017c478bd9Sstevel@tonic-gate 	lf.lf_key	= lf.lf_key;
4027c478bd9Sstevel@tonic-gate 	lf.lf_comment	= comment;
4037c478bd9Sstevel@tonic-gate 	lf.lf_comlen	= (comment) ? strlen(comment)+1 : 0;
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	if (ioctl(fd, _FIOLFS, &lf) == -1) {
4067c478bd9Sstevel@tonic-gate 		perror(fn);
4077c478bd9Sstevel@tonic-gate 		close(fd);
4087c478bd9Sstevel@tonic-gate 		exitstatus = 2;
4097c478bd9Sstevel@tonic-gate 		return;
4107c478bd9Sstevel@tonic-gate 	}
4117c478bd9Sstevel@tonic-gate 	close(fd);
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate /*
4147c478bd9Sstevel@tonic-gate  * getmntnames
4157c478bd9Sstevel@tonic-gate  *	file names from /etc/mtab
4167c478bd9Sstevel@tonic-gate  */
4177c478bd9Sstevel@tonic-gate void
getmntnames()4187c478bd9Sstevel@tonic-gate getmntnames()
4197c478bd9Sstevel@tonic-gate {
4207c478bd9Sstevel@tonic-gate 	int		fnlen;
4217c478bd9Sstevel@tonic-gate 	struct filename	*fnp;
4227c478bd9Sstevel@tonic-gate 	struct filename	*fnpc;
4237c478bd9Sstevel@tonic-gate 	FILE		*mnttab;
4247c478bd9Sstevel@tonic-gate 	struct mnttab	mnt, *mntp = &mnt;
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	fnpc = fnanchor;
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	if ((mnttab = fopen(MNTTAB, "r")) == NULL) {
4297c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Can't open %s\n", MNTTAB);
4307c478bd9Sstevel@tonic-gate 		perror(MNTTAB);
4317c478bd9Sstevel@tonic-gate 		exit(32);
4327c478bd9Sstevel@tonic-gate 	}
4337c478bd9Sstevel@tonic-gate 	while ((getmntent(mnttab, mntp)) == 0) {
4347c478bd9Sstevel@tonic-gate 		if (strcmp(mntp->mnt_fstype, MNTTYPE_UFS) != 0)
4357c478bd9Sstevel@tonic-gate 			continue;
4367c478bd9Sstevel@tonic-gate 		fnlen = strlen(mntp->mnt_mountp) + 1;
4377c478bd9Sstevel@tonic-gate 		fnp = (struct filename *)malloc(sizeof (struct filename));
438*d1a180b0Smaheshvs 		fnp->fn_name = malloc((uint_t)fnlen);
4397c478bd9Sstevel@tonic-gate 		strcpy(fnp->fn_name, mntp->mnt_mountp);
4407c478bd9Sstevel@tonic-gate 		fnp->fn_next = NULL;
4417c478bd9Sstevel@tonic-gate 		if (fnpc)
4427c478bd9Sstevel@tonic-gate 			fnpc->fn_next = fnp;
4437c478bd9Sstevel@tonic-gate 		else
4447c478bd9Sstevel@tonic-gate 			fnanchor = fnp;
4457c478bd9Sstevel@tonic-gate 		fnpc = fnp;
4467c478bd9Sstevel@tonic-gate 	}
4477c478bd9Sstevel@tonic-gate 	fclose(mnttab);
4487c478bd9Sstevel@tonic-gate }
4497c478bd9Sstevel@tonic-gate /*
4507c478bd9Sstevel@tonic-gate  * getcmdnames
4517c478bd9Sstevel@tonic-gate  *	file names from command line
4527c478bd9Sstevel@tonic-gate  */
4537c478bd9Sstevel@tonic-gate void
getcmdnames(int argc,char ** argv,int i)454*d1a180b0Smaheshvs getcmdnames(int argc, char **argv, int i)
4557c478bd9Sstevel@tonic-gate {
4567c478bd9Sstevel@tonic-gate 	struct filename	*fnp;
4577c478bd9Sstevel@tonic-gate 	struct filename	*fnpc;
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	for (fnpc = fnanchor; i < argc; ++i) {
4607c478bd9Sstevel@tonic-gate 		fnp = (struct filename *)malloc(sizeof (struct filename));
4617c478bd9Sstevel@tonic-gate 		fnp->fn_name = *(argv+i);
4627c478bd9Sstevel@tonic-gate 		fnp->fn_next = NULL;
4637c478bd9Sstevel@tonic-gate 		if (fnpc)
4647c478bd9Sstevel@tonic-gate 			fnpc->fn_next = fnp;
4657c478bd9Sstevel@tonic-gate 		else
4667c478bd9Sstevel@tonic-gate 			fnanchor = fnp;
4677c478bd9Sstevel@tonic-gate 		fnpc = fnp;
4687c478bd9Sstevel@tonic-gate 	}
4697c478bd9Sstevel@tonic-gate }
470