xref: /titanic_51/usr/src/cmd/dfs.cmds/dfshares/dfshares.c (revision 8c332a0dd4225c1d5b651b04a5df22040d918bf7)
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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
237c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
27*8c332a0dSja97890  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
287c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate  *	generic interface to dfshares, dfmounts.
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  *	usage:	dfshares [-F fstype] [-o fs_options] [-h] [ args ]
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  *	exec's /usr/lib/fs/<fstype>/<cmd>
397c478bd9Sstevel@tonic-gate  *	<cmd> is the basename of the command.
407c478bd9Sstevel@tonic-gate  *
417c478bd9Sstevel@tonic-gate  *	if -F is missing, fstype is the first entry in /etc/dfs/fstypes
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include <sys/types.h>
457c478bd9Sstevel@tonic-gate #include <sys/stat.h>
467c478bd9Sstevel@tonic-gate #include <ctype.h>
477c478bd9Sstevel@tonic-gate #include <stdio.h>
487c478bd9Sstevel@tonic-gate #include <dirent.h>
497c478bd9Sstevel@tonic-gate #include <string.h>
507c478bd9Sstevel@tonic-gate #include <errno.h>
517c478bd9Sstevel@tonic-gate #include <unistd.h>
527c478bd9Sstevel@tonic-gate #include <wait.h>
537c478bd9Sstevel@tonic-gate #include <stdlib.h>
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #define	DFSTYPES	"/etc/dfs/fstypes"		/* dfs list */
567c478bd9Sstevel@tonic-gate #define	FSCMD		"/usr/lib/fs/%s/%s"
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate /*
597c478bd9Sstevel@tonic-gate  * non-[arg...] elements in new argv list:
607c478bd9Sstevel@tonic-gate  * cmd name, , -h, -o, opts, (char *)0 terminator
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate #define	ARGVPAD		5
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate static char *getfs(FILE *);
657c478bd9Sstevel@tonic-gate static int invalid(const char *, FILE *);
667c478bd9Sstevel@tonic-gate 
67*8c332a0dSja97890 int
687c478bd9Sstevel@tonic-gate main(int argc, char **argv)
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate 	FILE *dfp;		/* fp for dfs list */
717c478bd9Sstevel@tonic-gate 	int c, err = 0;
727c478bd9Sstevel@tonic-gate 	char subcmd[BUFSIZ];	/* fs specific command */
737c478bd9Sstevel@tonic-gate 	char *cmd;		/* basename of this command */
747c478bd9Sstevel@tonic-gate 	char *fsname = NULL;	/* file system name */
757c478bd9Sstevel@tonic-gate 	char *opts = NULL;	/* -o options */
767c478bd9Sstevel@tonic-gate 	char **nargv;		/* new argv list */
777c478bd9Sstevel@tonic-gate 	int hflag = 0;
787c478bd9Sstevel@tonic-gate 	int nargc = 0;		/* new argc */
797c478bd9Sstevel@tonic-gate 	pid_t pid;		/* pid for fork */
807c478bd9Sstevel@tonic-gate 	int retval;		/* exit status from exec'd commad */
817c478bd9Sstevel@tonic-gate 	int showall = (argc <= 1);	/* show all resources */
827c478bd9Sstevel@tonic-gate 	static char usage[] =
837c478bd9Sstevel@tonic-gate 		"usage: %s [-F fstype] [-h] [-o fs_options ] [arg ...]\n";
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	cmd = strrchr(argv[0], '/');	/* find the basename */
867c478bd9Sstevel@tonic-gate 	if (cmd)
877c478bd9Sstevel@tonic-gate 		++cmd;
887c478bd9Sstevel@tonic-gate 	else
897c478bd9Sstevel@tonic-gate 		cmd = argv[0];
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "hF:o:")) != -1)
927c478bd9Sstevel@tonic-gate 		switch (c) {
937c478bd9Sstevel@tonic-gate 		case 'h':
947c478bd9Sstevel@tonic-gate 			hflag = 1;	/* no header ... pass to subcommand */
957c478bd9Sstevel@tonic-gate 			break;
967c478bd9Sstevel@tonic-gate 		case 'F':
977c478bd9Sstevel@tonic-gate 			err |= (fsname != NULL);	/* at most one -F */
987c478bd9Sstevel@tonic-gate 			fsname = optarg;
997c478bd9Sstevel@tonic-gate 			break;
1007c478bd9Sstevel@tonic-gate 		case 'o':		/* fs specific options */
1017c478bd9Sstevel@tonic-gate 			err |= (opts != NULL);	/* at most one -o */
1027c478bd9Sstevel@tonic-gate 			opts = optarg;
1037c478bd9Sstevel@tonic-gate 			break;
1047c478bd9Sstevel@tonic-gate 		case '?':
1057c478bd9Sstevel@tonic-gate 			err = 1;
1067c478bd9Sstevel@tonic-gate 			break;
1077c478bd9Sstevel@tonic-gate 		}
1087c478bd9Sstevel@tonic-gate 	if (err) {
1097c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, usage, cmd);
1107c478bd9Sstevel@tonic-gate 		exit(1);
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	if ((dfp = fopen(DFSTYPES, "r")) == NULL) {
1147c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: cannot open %s\n", cmd, DFSTYPES);
1157c478bd9Sstevel@tonic-gate 		exit(1);
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	/* allocate a block for the new argv list */
1197c478bd9Sstevel@tonic-gate 	if (!(nargv = (char **)malloc(sizeof (char *)*(argc-optind+ARGVPAD)))) {
1207c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: malloc failed.\n", cmd);
1217c478bd9Sstevel@tonic-gate 		exit(1);
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 	nargv[nargc++] = cmd;
1247c478bd9Sstevel@tonic-gate 	if (hflag)
1257c478bd9Sstevel@tonic-gate 		nargv[nargc++] = "-h";
1267c478bd9Sstevel@tonic-gate 	if (opts) {
1277c478bd9Sstevel@tonic-gate 		nargv[nargc++] = "-o";
1287c478bd9Sstevel@tonic-gate 		nargv[nargc++] = opts;
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 	for (; optind <= argc; ++optind)	/* this copies the last NULL */
1317c478bd9Sstevel@tonic-gate 		nargv[nargc++] = argv[optind];
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	if (showall) {		/* command with no args -- show all dfs's */
1347c478bd9Sstevel@tonic-gate 		while (fsname = getfs(dfp)) {
1357c478bd9Sstevel@tonic-gate 			(void) snprintf(subcmd, sizeof (subcmd),
1367c478bd9Sstevel@tonic-gate 			    FSCMD, fsname, cmd);
1377c478bd9Sstevel@tonic-gate 			switch (pid = fork()) {		/* do the subcommand */
1387c478bd9Sstevel@tonic-gate 			case 0:
1397c478bd9Sstevel@tonic-gate 				(void) execvp(subcmd, nargv);
1407c478bd9Sstevel@tonic-gate 				if (errno != ENOENT)
1417c478bd9Sstevel@tonic-gate 					perror(subcmd);
1427c478bd9Sstevel@tonic-gate 				_exit(1);
1437c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
1447c478bd9Sstevel@tonic-gate 			default:
1457c478bd9Sstevel@tonic-gate 				while (wait(&retval) != pid)
1467c478bd9Sstevel@tonic-gate 					;
1477c478bd9Sstevel@tonic-gate 				/* take exit status into account */
1487c478bd9Sstevel@tonic-gate 				err |= (retval & 0xff00) >> 8;
1497c478bd9Sstevel@tonic-gate 				break;
1507c478bd9Sstevel@tonic-gate 			case -1:
1517c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1527c478bd9Sstevel@tonic-gate 					"%s: fork failed - try again later.\n",
1537c478bd9Sstevel@tonic-gate 					cmd);
1547c478bd9Sstevel@tonic-gate 				exit(1);
1557c478bd9Sstevel@tonic-gate 			}
1567c478bd9Sstevel@tonic-gate 		}
1577c478bd9Sstevel@tonic-gate 		(void) fclose(dfp);
1587c478bd9Sstevel@tonic-gate 		if (pid == 0) {		/* we never got into the loop! */
1597c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1607c478bd9Sstevel@tonic-gate 				    "%s: no file systems in %s\n",
1617c478bd9Sstevel@tonic-gate 				    cmd, DFSTYPES);
1627c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, usage, cmd);
1637c478bd9Sstevel@tonic-gate 			exit(1);
1647c478bd9Sstevel@tonic-gate 		}
1657c478bd9Sstevel@tonic-gate 		else
1667c478bd9Sstevel@tonic-gate 			exit(err);
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	if (fsname) {		/* generate fs specific command name */
1707c478bd9Sstevel@tonic-gate 		if (invalid(fsname, dfp)) {	/* valid ? */
1717c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1727c478bd9Sstevel@tonic-gate 				    "%s: invalid file system name\n", cmd);
1737c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, usage, cmd);
1747c478bd9Sstevel@tonic-gate 			exit(1);
1757c478bd9Sstevel@tonic-gate 		}
1767c478bd9Sstevel@tonic-gate 		else
1777c478bd9Sstevel@tonic-gate 			(void) snprintf(subcmd, sizeof (subcmd),
1787c478bd9Sstevel@tonic-gate 			    FSCMD, fsname, cmd);
1797c478bd9Sstevel@tonic-gate 	} else if (fsname = getfs(dfp))		/* use 1st line in dfstypes */
1807c478bd9Sstevel@tonic-gate 		(void) snprintf(subcmd, sizeof (subcmd), FSCMD, fsname, cmd);
1817c478bd9Sstevel@tonic-gate 	else {
1827c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1837c478bd9Sstevel@tonic-gate 			    "%s: no file systems in %s\n", cmd, DFSTYPES);
1847c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, usage, cmd);
1857c478bd9Sstevel@tonic-gate 		exit(1);
1867c478bd9Sstevel@tonic-gate 	}
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	(void) execvp(subcmd, nargv);
1897c478bd9Sstevel@tonic-gate 	perror(subcmd);				/* execvp failed */
1907c478bd9Sstevel@tonic-gate 	return (1);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate /*
1957c478bd9Sstevel@tonic-gate  *	invalid(name, f)  -  return non-zero if name is not in
1967c478bd9Sstevel@tonic-gate  *			     the list of fs names in file f
1977c478bd9Sstevel@tonic-gate  */
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate static int
2007c478bd9Sstevel@tonic-gate invalid(const char *name,	/* file system name */
2017c478bd9Sstevel@tonic-gate 	FILE *f)		/* file of list of file system types */
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate 	char *s;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	while (s = getfs(f))	/* while there's still hope ... */
2067c478bd9Sstevel@tonic-gate 		if (strcmp(s, name) == 0)
2077c478bd9Sstevel@tonic-gate 			return (0);	/* we got it! */
2087c478bd9Sstevel@tonic-gate 	return (1);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate /*
2137c478bd9Sstevel@tonic-gate  *   getfs(fp) - get the next file system name from fp
2147c478bd9Sstevel@tonic-gate  *               ignoring lines starting with a #.
2157c478bd9Sstevel@tonic-gate  *               All leading whitespace is discarded.
2167c478bd9Sstevel@tonic-gate  */
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate static char buf[BUFSIZ];
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate static char *
2217c478bd9Sstevel@tonic-gate getfs(FILE *fp)
2227c478bd9Sstevel@tonic-gate {
2237c478bd9Sstevel@tonic-gate 	register char *s;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	while (s = fgets(buf, BUFSIZ, fp)) {
2267c478bd9Sstevel@tonic-gate 		while (isspace(*s))	/* leading whitespace doesn't count */
2277c478bd9Sstevel@tonic-gate 			++s;
2287c478bd9Sstevel@tonic-gate 		if (*s != '#') {	/* not a comment */
2297c478bd9Sstevel@tonic-gate 			char *t = s;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 			while (!isspace(*t))	/* get the token */
2327c478bd9Sstevel@tonic-gate 				++t;
2337c478bd9Sstevel@tonic-gate 			*t = '\0';		/* ignore rest of line */
2347c478bd9Sstevel@tonic-gate 			return (s);
2357c478bd9Sstevel@tonic-gate 		}
2367c478bd9Sstevel@tonic-gate 	}
2377c478bd9Sstevel@tonic-gate 	return (NULL);	/* that's all, folks! */
2387c478bd9Sstevel@tonic-gate }
239