xref: /titanic_53/usr/src/cmd/dfs.cmds/dfshares/dfshares.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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
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 /*
34*7c478bd9Sstevel@tonic-gate  *	generic interface to dfshares, dfmounts.
35*7c478bd9Sstevel@tonic-gate  *
36*7c478bd9Sstevel@tonic-gate  *	usage:	dfshares [-F fstype] [-o fs_options] [-h] [ args ]
37*7c478bd9Sstevel@tonic-gate  *
38*7c478bd9Sstevel@tonic-gate  *	exec's /usr/lib/fs/<fstype>/<cmd>
39*7c478bd9Sstevel@tonic-gate  *	<cmd> is the basename of the command.
40*7c478bd9Sstevel@tonic-gate  *
41*7c478bd9Sstevel@tonic-gate  *	if -F is missing, fstype is the first entry in /etc/dfs/fstypes
42*7c478bd9Sstevel@tonic-gate  */
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
46*7c478bd9Sstevel@tonic-gate #include <ctype.h>
47*7c478bd9Sstevel@tonic-gate #include <stdio.h>
48*7c478bd9Sstevel@tonic-gate #include <dirent.h>
49*7c478bd9Sstevel@tonic-gate #include <string.h>
50*7c478bd9Sstevel@tonic-gate #include <errno.h>
51*7c478bd9Sstevel@tonic-gate #include <unistd.h>
52*7c478bd9Sstevel@tonic-gate #include <wait.h>
53*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate #define	DFSTYPES	"/etc/dfs/fstypes"		/* dfs list */
56*7c478bd9Sstevel@tonic-gate #define	FSCMD		"/usr/lib/fs/%s/%s"
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate /*
59*7c478bd9Sstevel@tonic-gate  * non-[arg...] elements in new argv list:
60*7c478bd9Sstevel@tonic-gate  * cmd name, , -h, -o, opts, (char *)0 terminator
61*7c478bd9Sstevel@tonic-gate  */
62*7c478bd9Sstevel@tonic-gate #define	ARGVPAD		5
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate static char *getfs(FILE *);
65*7c478bd9Sstevel@tonic-gate static int invalid(const char *, FILE *);
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
68*7c478bd9Sstevel@tonic-gate {
69*7c478bd9Sstevel@tonic-gate 	FILE *dfp;		/* fp for dfs list */
70*7c478bd9Sstevel@tonic-gate 	int c, err = 0;
71*7c478bd9Sstevel@tonic-gate 	char subcmd[BUFSIZ];	/* fs specific command */
72*7c478bd9Sstevel@tonic-gate 	char *cmd;		/* basename of this command */
73*7c478bd9Sstevel@tonic-gate 	char *fsname = NULL;	/* file system name */
74*7c478bd9Sstevel@tonic-gate 	char *opts = NULL;	/* -o options */
75*7c478bd9Sstevel@tonic-gate 	char **nargv;		/* new argv list */
76*7c478bd9Sstevel@tonic-gate 	int hflag = 0;
77*7c478bd9Sstevel@tonic-gate 	int nargc = 0;		/* new argc */
78*7c478bd9Sstevel@tonic-gate 	pid_t pid;		/* pid for fork */
79*7c478bd9Sstevel@tonic-gate 	int retval;		/* exit status from exec'd commad */
80*7c478bd9Sstevel@tonic-gate 	int showall = (argc <= 1);	/* show all resources */
81*7c478bd9Sstevel@tonic-gate 	static char usage[] =
82*7c478bd9Sstevel@tonic-gate 		"usage: %s [-F fstype] [-h] [-o fs_options ] [arg ...]\n";
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	cmd = strrchr(argv[0], '/');	/* find the basename */
85*7c478bd9Sstevel@tonic-gate 	if (cmd)
86*7c478bd9Sstevel@tonic-gate 		++cmd;
87*7c478bd9Sstevel@tonic-gate 	else
88*7c478bd9Sstevel@tonic-gate 		cmd = argv[0];
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "hF:o:")) != -1)
91*7c478bd9Sstevel@tonic-gate 		switch (c) {
92*7c478bd9Sstevel@tonic-gate 		case 'h':
93*7c478bd9Sstevel@tonic-gate 			hflag = 1;	/* no header ... pass to subcommand */
94*7c478bd9Sstevel@tonic-gate 			break;
95*7c478bd9Sstevel@tonic-gate 		case 'F':
96*7c478bd9Sstevel@tonic-gate 			err |= (fsname != NULL);	/* at most one -F */
97*7c478bd9Sstevel@tonic-gate 			fsname = optarg;
98*7c478bd9Sstevel@tonic-gate 			break;
99*7c478bd9Sstevel@tonic-gate 		case 'o':		/* fs specific options */
100*7c478bd9Sstevel@tonic-gate 			err |= (opts != NULL);	/* at most one -o */
101*7c478bd9Sstevel@tonic-gate 			opts = optarg;
102*7c478bd9Sstevel@tonic-gate 			break;
103*7c478bd9Sstevel@tonic-gate 		case '?':
104*7c478bd9Sstevel@tonic-gate 			err = 1;
105*7c478bd9Sstevel@tonic-gate 			break;
106*7c478bd9Sstevel@tonic-gate 		}
107*7c478bd9Sstevel@tonic-gate 	if (err) {
108*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, usage, cmd);
109*7c478bd9Sstevel@tonic-gate 		exit(1);
110*7c478bd9Sstevel@tonic-gate 	}
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	if ((dfp = fopen(DFSTYPES, "r")) == NULL) {
113*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: cannot open %s\n", cmd, DFSTYPES);
114*7c478bd9Sstevel@tonic-gate 		exit(1);
115*7c478bd9Sstevel@tonic-gate 	}
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 	/* allocate a block for the new argv list */
118*7c478bd9Sstevel@tonic-gate 	if (!(nargv = (char **)malloc(sizeof (char *)*(argc-optind+ARGVPAD)))) {
119*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: malloc failed.\n", cmd);
120*7c478bd9Sstevel@tonic-gate 		exit(1);
121*7c478bd9Sstevel@tonic-gate 	}
122*7c478bd9Sstevel@tonic-gate 	nargv[nargc++] = cmd;
123*7c478bd9Sstevel@tonic-gate 	if (hflag)
124*7c478bd9Sstevel@tonic-gate 		nargv[nargc++] = "-h";
125*7c478bd9Sstevel@tonic-gate 	if (opts) {
126*7c478bd9Sstevel@tonic-gate 		nargv[nargc++] = "-o";
127*7c478bd9Sstevel@tonic-gate 		nargv[nargc++] = opts;
128*7c478bd9Sstevel@tonic-gate 	}
129*7c478bd9Sstevel@tonic-gate 	for (; optind <= argc; ++optind)	/* this copies the last NULL */
130*7c478bd9Sstevel@tonic-gate 		nargv[nargc++] = argv[optind];
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 	if (showall) {		/* command with no args -- show all dfs's */
133*7c478bd9Sstevel@tonic-gate 		while (fsname = getfs(dfp)) {
134*7c478bd9Sstevel@tonic-gate 			(void) snprintf(subcmd, sizeof (subcmd),
135*7c478bd9Sstevel@tonic-gate 			    FSCMD, fsname, cmd);
136*7c478bd9Sstevel@tonic-gate 			switch (pid = fork()) {		/* do the subcommand */
137*7c478bd9Sstevel@tonic-gate 			case 0:
138*7c478bd9Sstevel@tonic-gate 				(void) execvp(subcmd, nargv);
139*7c478bd9Sstevel@tonic-gate 				if (errno != ENOENT)
140*7c478bd9Sstevel@tonic-gate 					perror(subcmd);
141*7c478bd9Sstevel@tonic-gate 				_exit(1);
142*7c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
143*7c478bd9Sstevel@tonic-gate 			default:
144*7c478bd9Sstevel@tonic-gate 				while (wait(&retval) != pid)
145*7c478bd9Sstevel@tonic-gate 					;
146*7c478bd9Sstevel@tonic-gate 				/* take exit status into account */
147*7c478bd9Sstevel@tonic-gate 				err |= (retval & 0xff00) >> 8;
148*7c478bd9Sstevel@tonic-gate 				break;
149*7c478bd9Sstevel@tonic-gate 			case -1:
150*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
151*7c478bd9Sstevel@tonic-gate 					"%s: fork failed - try again later.\n",
152*7c478bd9Sstevel@tonic-gate 					cmd);
153*7c478bd9Sstevel@tonic-gate 				exit(1);
154*7c478bd9Sstevel@tonic-gate 			}
155*7c478bd9Sstevel@tonic-gate 		}
156*7c478bd9Sstevel@tonic-gate 		(void) fclose(dfp);
157*7c478bd9Sstevel@tonic-gate 		if (pid == 0) {		/* we never got into the loop! */
158*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
159*7c478bd9Sstevel@tonic-gate 				    "%s: no file systems in %s\n",
160*7c478bd9Sstevel@tonic-gate 				    cmd, DFSTYPES);
161*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, usage, cmd);
162*7c478bd9Sstevel@tonic-gate 			exit(1);
163*7c478bd9Sstevel@tonic-gate 		}
164*7c478bd9Sstevel@tonic-gate 		else
165*7c478bd9Sstevel@tonic-gate 			exit(err);
166*7c478bd9Sstevel@tonic-gate 	}
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 	if (fsname) {		/* generate fs specific command name */
169*7c478bd9Sstevel@tonic-gate 		if (invalid(fsname, dfp)) {	/* valid ? */
170*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
171*7c478bd9Sstevel@tonic-gate 				    "%s: invalid file system name\n", cmd);
172*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, usage, cmd);
173*7c478bd9Sstevel@tonic-gate 			exit(1);
174*7c478bd9Sstevel@tonic-gate 		}
175*7c478bd9Sstevel@tonic-gate 		else
176*7c478bd9Sstevel@tonic-gate 			(void) snprintf(subcmd, sizeof (subcmd),
177*7c478bd9Sstevel@tonic-gate 			    FSCMD, fsname, cmd);
178*7c478bd9Sstevel@tonic-gate 	} else if (fsname = getfs(dfp))		/* use 1st line in dfstypes */
179*7c478bd9Sstevel@tonic-gate 		(void) snprintf(subcmd, sizeof (subcmd), FSCMD, fsname, cmd);
180*7c478bd9Sstevel@tonic-gate 	else {
181*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
182*7c478bd9Sstevel@tonic-gate 			    "%s: no file systems in %s\n", cmd, DFSTYPES);
183*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, usage, cmd);
184*7c478bd9Sstevel@tonic-gate 		exit(1);
185*7c478bd9Sstevel@tonic-gate 	}
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate 	(void) execvp(subcmd, nargv);
188*7c478bd9Sstevel@tonic-gate 	perror(subcmd);				/* execvp failed */
189*7c478bd9Sstevel@tonic-gate 	return (1);
190*7c478bd9Sstevel@tonic-gate }
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate /*
194*7c478bd9Sstevel@tonic-gate  *	invalid(name, f)  -  return non-zero if name is not in
195*7c478bd9Sstevel@tonic-gate  *			     the list of fs names in file f
196*7c478bd9Sstevel@tonic-gate  */
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate static int
199*7c478bd9Sstevel@tonic-gate invalid(const char *name,	/* file system name */
200*7c478bd9Sstevel@tonic-gate 	FILE *f)		/* file of list of file system types */
201*7c478bd9Sstevel@tonic-gate {
202*7c478bd9Sstevel@tonic-gate 	char *s;
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate 	while (s = getfs(f))	/* while there's still hope ... */
205*7c478bd9Sstevel@tonic-gate 		if (strcmp(s, name) == 0)
206*7c478bd9Sstevel@tonic-gate 			return (0);	/* we got it! */
207*7c478bd9Sstevel@tonic-gate 	return (1);
208*7c478bd9Sstevel@tonic-gate }
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate /*
212*7c478bd9Sstevel@tonic-gate  *   getfs(fp) - get the next file system name from fp
213*7c478bd9Sstevel@tonic-gate  *               ignoring lines starting with a #.
214*7c478bd9Sstevel@tonic-gate  *               All leading whitespace is discarded.
215*7c478bd9Sstevel@tonic-gate  */
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate static char buf[BUFSIZ];
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate static char *
220*7c478bd9Sstevel@tonic-gate getfs(FILE *fp)
221*7c478bd9Sstevel@tonic-gate {
222*7c478bd9Sstevel@tonic-gate 	register char *s;
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 	while (s = fgets(buf, BUFSIZ, fp)) {
225*7c478bd9Sstevel@tonic-gate 		while (isspace(*s))	/* leading whitespace doesn't count */
226*7c478bd9Sstevel@tonic-gate 			++s;
227*7c478bd9Sstevel@tonic-gate 		if (*s != '#') {	/* not a comment */
228*7c478bd9Sstevel@tonic-gate 			char *t = s;
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate 			while (!isspace(*t))	/* get the token */
231*7c478bd9Sstevel@tonic-gate 				++t;
232*7c478bd9Sstevel@tonic-gate 			*t = '\0';		/* ignore rest of line */
233*7c478bd9Sstevel@tonic-gate 			return (s);
234*7c478bd9Sstevel@tonic-gate 		}
235*7c478bd9Sstevel@tonic-gate 	}
236*7c478bd9Sstevel@tonic-gate 	return (NULL);	/* that's all, folks! */
237*7c478bd9Sstevel@tonic-gate }
238