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