xref: /csrg-svn/usr.sbin/amd/amq/amq.c (revision 62549)
144628Smckusick /*
244628Smckusick  * Copyright (c) 1990 Jan-Simon Pendry
344628Smckusick  * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
4*62549Sbostic  * Copyright (c) 1990, 1993
5*62549Sbostic  *	The Regents of the University of California.  All rights reserved.
644628Smckusick  *
744628Smckusick  * This code is derived from software contributed to Berkeley by
844628Smckusick  * Jan-Simon Pendry at Imperial College, London.
944628Smckusick  *
1044628Smckusick  * %sccs.include.redist.c%
1149686Spendry  *
12*62549Sbostic  *	@(#)amq.c	8.1 (Berkeley) 06/07/93
1349686Spendry  *
1452453Spendry  * $Id: amq.c,v 5.2.2.1 1992/02/09 15:09:16 jsp beta $
1549686Spendry  *
1644628Smckusick  */
1744628Smckusick 
1844628Smckusick /*
1944628Smckusick  * Automounter query tool
2044628Smckusick  */
2144628Smckusick 
2244628Smckusick #ifndef lint
2344628Smckusick char copyright[] = "\
2444628Smckusick @(#)Copyright (c) 1990 Jan-Simon Pendry\n\
2544628Smckusick @(#)Copyright (c) 1990 Imperial College of Science, Technology & Medicine\n\
26*62549Sbostic @(#)Copyright (c) 1990, 1993\n\
27*62549Sbostic 	The Regents of the University of California.  All rights reserved.\n";
2844628Smckusick #endif /* not lint */
2944628Smckusick 
3044628Smckusick #ifndef lint
3152453Spendry static char rcsid[] = "$Id: amq.c,v 5.2.2.1 1992/02/09 15:09:16 jsp beta $";
32*62549Sbostic static char sccsid[] = "@(#)amq.c	8.1 (Berkeley) 06/07/93";
3344628Smckusick #endif /* not lint */
3444628Smckusick 
3544628Smckusick #include "am.h"
3644628Smckusick #include "amq.h"
3744628Smckusick #include <stdio.h>
3844628Smckusick #include <fcntl.h>
3944628Smckusick #include <netdb.h>
4044628Smckusick 
4147529Spendry static int privsock();
4247529Spendry 
4344628Smckusick char *progname;
4444628Smckusick static int flush_flag;
4544628Smckusick static int minfo_flag;
4644628Smckusick static int unmount_flag;
4744628Smckusick static int stats_flag;
4847529Spendry static int getvers_flag;
4944628Smckusick static char *debug_opts;
5044628Smckusick static char *logfile;
5147529Spendry static char *mount_map;
5247529Spendry static char *xlog_optstr;
5344628Smckusick static char localhost[] = "localhost";
5444628Smckusick static char *def_server = localhost;
5544628Smckusick 
5644628Smckusick extern int optind;
5744628Smckusick extern char *optarg;
5844628Smckusick 
5944628Smckusick static struct timeval tmo = { 10, 0 };
6044628Smckusick #define	TIMEOUT tmo
6144628Smckusick 
6244628Smckusick enum show_opt { Full, Stats, Calc, Short, ShowDone };
6344628Smckusick 
6444628Smckusick /*
6544628Smckusick  * If (e) is Calc then just calculate the sizes
6644628Smckusick  * Otherwise display the mount node on stdout
6744628Smckusick  */
show_mti(mt,e,mwid,dwid,twid)6844628Smckusick static void show_mti(mt, e, mwid, dwid, twid)
6944628Smckusick amq_mount_tree *mt;
7044628Smckusick enum show_opt e;
7144628Smckusick int *mwid;
7244628Smckusick int *dwid;
7344628Smckusick int *twid;
7444628Smckusick {
7544628Smckusick 	switch (e) {
7644628Smckusick 	case Calc: {
7744628Smckusick 		int mw = strlen(mt->mt_mountinfo);
7844628Smckusick 		int dw = strlen(mt->mt_directory);
7944628Smckusick 		int tw = strlen(mt->mt_type);
8044628Smckusick 		if (mw > *mwid) *mwid = mw;
8144628Smckusick 		if (dw > *dwid) *dwid = dw;
8244628Smckusick 		if (tw > *twid) *twid = tw;
8344628Smckusick 	} break;
8444628Smckusick 
8544628Smckusick 	case Full: {
8647529Spendry 		struct tm *tp = localtime((time_t *) &mt->mt_mounttime);
8744628Smckusick printf("%-*.*s %-*.*s %-*.*s %s\n\t%-5d %-7d %-6d %-7d %-7d %-6d %02d/%02d/%02d %02d:%02d:%02d\n",
8844628Smckusick 			*dwid, *dwid,
8944628Smckusick 			*mt->mt_directory ? mt->mt_directory : "/",	/* XXX */
9044628Smckusick 			*twid, *twid,
9144628Smckusick 			mt->mt_type,
9244628Smckusick 			*mwid, *mwid,
9344628Smckusick 			mt->mt_mountinfo,
9444628Smckusick 			mt->mt_mountpoint,
9544628Smckusick 
9644628Smckusick 			mt->mt_mountuid,
9744628Smckusick 			mt->mt_getattr,
9844628Smckusick 			mt->mt_lookup,
9944628Smckusick 			mt->mt_readdir,
10044628Smckusick 			mt->mt_readlink,
10144628Smckusick 			mt->mt_statfs,
10244628Smckusick 
10344628Smckusick 			tp->tm_year > 99 ? tp->tm_year - 100 : tp->tm_year,
10444628Smckusick 			tp->tm_mon+1, tp->tm_mday,
10544628Smckusick 			tp->tm_hour, tp->tm_min, tp->tm_sec);
10644628Smckusick 	} break;
10744628Smckusick 
10844628Smckusick 	case Stats: {
10947529Spendry 		struct tm *tp = localtime((time_t *) &mt->mt_mounttime);
11044628Smckusick printf("%-*.*s %-5d %-7d %-6d %-7d %-7d %-6d %02d/%02d/%02d %02d:%02d:%02d\n",
11144628Smckusick 			*dwid, *dwid,
11244628Smckusick 			*mt->mt_directory ? mt->mt_directory : "/",	/* XXX */
11344628Smckusick 
11444628Smckusick 			mt->mt_mountuid,
11544628Smckusick 			mt->mt_getattr,
11644628Smckusick 			mt->mt_lookup,
11744628Smckusick 			mt->mt_readdir,
11844628Smckusick 			mt->mt_readlink,
11944628Smckusick 			mt->mt_statfs,
12044628Smckusick 
12144628Smckusick 			tp->tm_year > 99 ? tp->tm_year - 100 : tp->tm_year,
12244628Smckusick 			tp->tm_mon+1, tp->tm_mday,
12344628Smckusick 			tp->tm_hour, tp->tm_min, tp->tm_sec);
12444628Smckusick 	} break;
12544628Smckusick 
12644628Smckusick 	case Short: {
12744628Smckusick 		printf("%-*.*s %-*.*s %-*.*s %s\n",
12844628Smckusick 			*dwid, *dwid,
12944628Smckusick 			*mt->mt_directory ? mt->mt_directory : "/",
13044628Smckusick 			*twid, *twid,
13144628Smckusick 			mt->mt_type,
13244628Smckusick 			*mwid, *mwid,
13344628Smckusick 			mt->mt_mountinfo,
13444628Smckusick 			mt->mt_mountpoint);
13544628Smckusick 	} break;
13644628Smckusick 	}
13744628Smckusick }
13844628Smckusick 
13944628Smckusick /*
14044628Smckusick  * Display a mount tree.
14144628Smckusick  */
show_mt(mt,e,mwid,dwid,pwid)14244628Smckusick static void show_mt(mt, e, mwid, dwid, pwid)
14344628Smckusick amq_mount_tree *mt;
14444628Smckusick enum show_opt e;
14544628Smckusick int *mwid;
14644628Smckusick int *dwid;
14744628Smckusick int *pwid;
14844628Smckusick {
14944628Smckusick 	while (mt) {
15044628Smckusick 		show_mti(mt, e, mwid, dwid, pwid);
15144628Smckusick 		show_mt(mt->mt_next, e, mwid, dwid, pwid);
15244628Smckusick 		mt = mt->mt_child;
15344628Smckusick 	}
15444628Smckusick }
15544628Smckusick 
show_mi(ml,e,mwid,dwid,twid)15644628Smckusick static void show_mi(ml, e, mwid, dwid, twid)
15744628Smckusick amq_mount_info_list *ml;
15844628Smckusick enum show_opt e;
15944628Smckusick int *mwid;
16044628Smckusick int *dwid;
16144628Smckusick int *twid;
16244628Smckusick {
16344628Smckusick 	int i;
16444628Smckusick 	switch (e) {
16544628Smckusick 	case Calc: {
16644628Smckusick 		for (i = 0; i < ml->amq_mount_info_list_len; i++) {
16744628Smckusick 			amq_mount_info *mi = &ml->amq_mount_info_list_val[i];
16844628Smckusick 			int mw = strlen(mi->mi_mountinfo);
16944628Smckusick 			int dw = strlen(mi->mi_mountpt);
17044628Smckusick 			int tw = strlen(mi->mi_type);
17144628Smckusick 			if (mw > *mwid) *mwid = mw;
17244628Smckusick 			if (dw > *dwid) *dwid = dw;
17344628Smckusick 			if (tw > *twid) *twid = tw;
17444628Smckusick 		}
17544628Smckusick 	} break;
17644628Smckusick 
17744628Smckusick 	case Full: {
17844628Smckusick 		for (i = 0; i < ml->amq_mount_info_list_len; i++) {
17944628Smckusick 			amq_mount_info *mi = &ml->amq_mount_info_list_val[i];
18044628Smckusick 			printf("%-*.*s %-*.*s %-*.*s %-3d %s is %s",
18144628Smckusick 						*mwid, *mwid, mi->mi_mountinfo,
18244628Smckusick 						*dwid, *dwid, mi->mi_mountpt,
18344628Smckusick 						*twid, *twid, mi->mi_type,
18444628Smckusick 						mi->mi_refc, mi->mi_fserver,
18544628Smckusick 						mi->mi_up > 0 ? "up" :
18644628Smckusick 						mi->mi_up < 0 ? "starting" : "down");
18744628Smckusick 			if (mi->mi_error > 0) {
18860883Spendry #ifdef HAS_STRERROR
18960883Spendry 				printf(" (%s)", strerror(mi->mi_error));
19060883Spendry #else
19144628Smckusick 				extern char *sys_errlist[];
19244628Smckusick 				extern int sys_nerr;
19344628Smckusick 				if (mi->mi_error < sys_nerr)
19444628Smckusick 					printf(" (%s)", sys_errlist[mi->mi_error]);
19544628Smckusick 				else
19644628Smckusick 					printf(" (Error %d)", mi->mi_error);
19760883Spendry #endif
19844628Smckusick 			} else if (mi->mi_error < 0) {
19944628Smckusick 				fputs(" (in progress)", stdout);
20044628Smckusick 			}
20144628Smckusick 			fputc('\n', stdout);
20244628Smckusick 		}
20344628Smckusick 	} break;
20444628Smckusick 	}
20544628Smckusick }
20644628Smckusick 
20744628Smckusick /*
20844628Smckusick  * Display general mount statistics
20944628Smckusick  */
show_ms(ms)21044628Smckusick static void show_ms(ms)
21144628Smckusick amq_mount_stats *ms;
21244628Smckusick {
21344628Smckusick 	printf("\
21444628Smckusick requests  stale     mount     mount     unmount\n\
21544628Smckusick deferred  fhandles  ok        failed    failed\n\
21644628Smckusick %-9d %-9d %-9d %-9d %-9d\n",
21744628Smckusick 	ms->as_drops, ms->as_stale, ms->as_mok, ms->as_merr, ms->as_uerr);
21844628Smckusick }
21944628Smckusick 
22044628Smckusick static bool_t
xdr_pri_free(xdr_args,args_ptr)22144628Smckusick xdr_pri_free(xdr_args, args_ptr)
22244628Smckusick xdrproc_t xdr_args;
22344628Smckusick caddr_t args_ptr;
22444628Smckusick {
22544628Smckusick 	XDR xdr;
22644628Smckusick 	xdr.x_op = XDR_FREE;
22744628Smckusick 	return ((*xdr_args)(&xdr, args_ptr));
22844628Smckusick }
22944628Smckusick 
23044628Smckusick #ifdef hpux
23144628Smckusick #include <cluster.h>
cluster_server()23244628Smckusick static char *cluster_server()
23344628Smckusick {
23444628Smckusick 	struct cct_entry *cp;
23544628Smckusick 
23644628Smckusick 	if (cnodeid() == 0) {
23744628Smckusick 		/*
23844628Smckusick 		 * Not clustered
23944628Smckusick 		 */
24044628Smckusick 		return def_server;
24144628Smckusick 	}
24244628Smckusick 
24344628Smckusick 	while (cp = getccent())
24444628Smckusick 		if (cp->cnode_type == 'r')
24544628Smckusick 			return cp->cnode_name;
24644628Smckusick 
24744628Smckusick 
24844628Smckusick 	return def_server;
24944628Smckusick }
25044628Smckusick #endif /* hpux */
25144628Smckusick 
25244628Smckusick /*
25344628Smckusick  * MAIN
25444628Smckusick  */
main(argc,argv)25544628Smckusick main(argc, argv)
25644628Smckusick int argc;
25744628Smckusick char *argv[];
25844628Smckusick {
25944628Smckusick 	int opt_ch;
26044628Smckusick 	int errs = 0;
26144628Smckusick 	char *server;
26244628Smckusick 	struct sockaddr_in server_addr;
26347529Spendry 
26447529Spendry 	/* In order to pass the Amd security check, we must use a priv port. */
26547529Spendry 	int s;
26647529Spendry 
26744628Smckusick 	CLIENT *clnt;
26844628Smckusick 	struct hostent *hp;
26944628Smckusick 	int nodefault = 0;
27044628Smckusick 
27144628Smckusick 	/*
27244628Smckusick 	 * Compute program name
27344628Smckusick 	 */
27444628Smckusick 	if (argv[0]) {
27544628Smckusick 		progname = strrchr(argv[0], '/');
27644628Smckusick 		if (progname && progname[1])
27744628Smckusick 			progname++;
27844628Smckusick 		else
27944628Smckusick 			progname = argv[0];
28044628Smckusick 	}
28144628Smckusick 	if (!progname)
28244628Smckusick 		progname = "amq";
28344628Smckusick 
28444628Smckusick 	/*
28544628Smckusick 	 * Parse arguments
28644628Smckusick 	 */
28747529Spendry 	while ((opt_ch = getopt(argc, argv, "fh:l:msuvx:D:M:")) != EOF)
28844628Smckusick 	switch (opt_ch) {
28944628Smckusick 	case 'f':
29044628Smckusick 		flush_flag = 1;
29147529Spendry 		nodefault = 1;
29244628Smckusick 		break;
29344628Smckusick 
29444628Smckusick 	case 'h':
29544628Smckusick 		def_server = optarg;
29644628Smckusick 		break;
29744628Smckusick 
29844628Smckusick 	case 'l':
29944628Smckusick 		logfile = optarg;
30044628Smckusick 		nodefault = 1;
30144628Smckusick 		break;
30244628Smckusick 
30344628Smckusick 	case 'm':
30444628Smckusick 		minfo_flag = 1;
30544628Smckusick 		nodefault = 1;
30644628Smckusick 		break;
30744628Smckusick 
30844628Smckusick 	case 's':
30944628Smckusick 		stats_flag = 1;
31047529Spendry 		nodefault = 1;
31144628Smckusick 		break;
31244628Smckusick 
31344628Smckusick 	case 'u':
31444628Smckusick 		unmount_flag = 1;
31547529Spendry 		nodefault = 1;
31644628Smckusick 		break;
31744628Smckusick 
31847529Spendry 	case 'v':
31947529Spendry 		getvers_flag = 1;
32047529Spendry 		nodefault = 1;
32147529Spendry 		break;
32247529Spendry 
32344628Smckusick 	case 'x':
32447529Spendry 		xlog_optstr = optarg;
32544628Smckusick 		nodefault = 1;
32644628Smckusick 		break;
32744628Smckusick 
32844628Smckusick 	case 'D':
32944628Smckusick 		debug_opts = optarg;
33044628Smckusick 		nodefault = 1;
33144628Smckusick 		break;
33244628Smckusick 
33347529Spendry 	case 'M':
33447529Spendry 		mount_map = optarg;
33547529Spendry 		nodefault = 1;
33647529Spendry 		break;
33747529Spendry 
33844628Smckusick 	default:
33944628Smckusick 		errs = 1;
34044628Smckusick 		break;
34144628Smckusick 	}
34244628Smckusick 
34347529Spendry 	if (optind == argc) {
34447529Spendry 		if (unmount_flag)
34547529Spendry 			errs = 1;
34647529Spendry 	}
34747529Spendry 
34844628Smckusick 	if (errs) {
34944628Smckusick show_usage:
35044628Smckusick 		fprintf(stderr, "\
35147529Spendry Usage: %s [-h host] [[-f] [-m] [-v] [-s]] | [[-u] directory ...]] |\n\
35247529Spendry \t[-l logfile|\"syslog\"] [-x log_flags] [-D dbg_opts] [-M mapent]\n", progname);
35344628Smckusick 		exit(1);
35444628Smckusick 	}
35544628Smckusick 
35644628Smckusick #ifdef hpux
35744628Smckusick 	/*
35844628Smckusick 	 * Figure out root server of cluster
35944628Smckusick 	 */
36044628Smckusick 	if (def_server == localhost)
36144628Smckusick 		server = cluster_server();
36244628Smckusick 	else
36344628Smckusick #endif /* hpux */
36444628Smckusick 	server = def_server;
36544628Smckusick 
36644628Smckusick 	/*
36744628Smckusick 	 * Get address of server
36844628Smckusick 	 */
36949686Spendry 	if ((hp = gethostbyname(server)) == 0 && strcmp(server, localhost) != 0) {
37044628Smckusick 		fprintf(stderr, "%s: Can't get address of %s\n", progname, server);
37144628Smckusick 		exit(1);
37244628Smckusick 	}
37344628Smckusick 	bzero(&server_addr, sizeof server_addr);
37444628Smckusick 	server_addr.sin_family = AF_INET;
37549686Spendry 	if (hp) {
37649686Spendry 		bcopy((voidp) hp->h_addr, (voidp) &server_addr.sin_addr,
37749686Spendry 			sizeof(server_addr.sin_addr));
37849686Spendry 	} else {
37949686Spendry 		/* fake "localhost" */
38049686Spendry 		server_addr.sin_addr.s_addr = htonl(0x7f000001);
38149686Spendry 	}
38244628Smckusick 
38344628Smckusick 	/*
38444628Smckusick 	 * Create RPC endpoint
38544628Smckusick 	 */
38652453Spendry 	s = privsock(SOCK_STREAM);
38752453Spendry 	clnt = clnttcp_create(&server_addr, AMQ_PROGRAM, AMQ_VERSION, &s, 0, 0);
38844628Smckusick 	if (clnt == 0) {
38952453Spendry 		close(s);
39052453Spendry 		s = privsock(SOCK_DGRAM);
39152453Spendry 		clnt = clntudp_create(&server_addr, AMQ_PROGRAM, AMQ_VERSION, TIMEOUT, &s);
39252453Spendry 	}
39352453Spendry 	if (clnt == 0) {
39444628Smckusick 		fprintf(stderr, "%s: ", progname);
39544628Smckusick 		clnt_pcreateerror(server);
39644628Smckusick 		exit(1);
39744628Smckusick 	}
39844628Smckusick 
39944628Smckusick 	/*
40044628Smckusick 	 * Control debugging
40144628Smckusick 	 */
40244628Smckusick 	if (debug_opts) {
40344628Smckusick 		int *rc;
40444628Smckusick 		amq_setopt opt;
40544628Smckusick 		opt.as_opt = AMOPT_DEBUG;
40644628Smckusick 		opt.as_str = debug_opts;
40744628Smckusick 		rc = amqproc_setopt_1(&opt, clnt);
40844628Smckusick 		if (rc && *rc < 0) {
40944628Smckusick 			fprintf(stderr, "%s: daemon not compiled for debug", progname);
41044628Smckusick 			errs = 1;
41144628Smckusick 		} else if (!rc || *rc > 0) {
41244628Smckusick 			fprintf(stderr, "%s: debug setting for \"%s\" failed\n", progname, debug_opts);
41344628Smckusick 			errs = 1;
41444628Smckusick 		}
41544628Smckusick 	}
41644628Smckusick 
41744628Smckusick 	/*
41844628Smckusick 	 * Control logging
41944628Smckusick 	 */
42047529Spendry 	if (xlog_optstr) {
42144628Smckusick 		int *rc;
42244628Smckusick 		amq_setopt opt;
42344628Smckusick 		opt.as_opt = AMOPT_XLOG;
42447529Spendry 		opt.as_str = xlog_optstr;
42544628Smckusick 		rc = amqproc_setopt_1(&opt, clnt);
42644628Smckusick 		if (!rc || *rc) {
42747529Spendry 			fprintf(stderr, "%s: setting log level to \"%s\" failed\n", progname, xlog_optstr);
42844628Smckusick 			errs = 1;
42944628Smckusick 		}
43044628Smckusick 	}
43144628Smckusick 
43244628Smckusick 	/*
43344628Smckusick 	 * Control log file
43444628Smckusick 	 */
43544628Smckusick 	if (logfile) {
43644628Smckusick 		int *rc;
43744628Smckusick 		amq_setopt opt;
43844628Smckusick 		opt.as_opt = AMOPT_LOGFILE;
43944628Smckusick 		opt.as_str = logfile;
44044628Smckusick 		rc = amqproc_setopt_1(&opt, clnt);
44144628Smckusick 		if (!rc || *rc) {
44244628Smckusick 			fprintf(stderr, "%s: setting logfile to \"%s\" failed\n", progname, logfile);
44344628Smckusick 			errs = 1;
44444628Smckusick 		}
44544628Smckusick 	}
44644628Smckusick 
44744628Smckusick 	/*
44844628Smckusick 	 * Flush map cache
44944628Smckusick 	 */
45047529Spendry 	if (flush_flag) {
45144628Smckusick 		int *rc;
45244628Smckusick 		amq_setopt opt;
45344628Smckusick 		opt.as_opt = AMOPT_FLUSHMAPC;
45444628Smckusick 		opt.as_str = "";
45544628Smckusick 		rc = amqproc_setopt_1(&opt, clnt);
45644628Smckusick 		if (!rc || *rc) {
45744628Smckusick 			fprintf(stderr, "%s: amd on %s cannot flush the map cache\n", progname, server);
45844628Smckusick 			errs = 1;
45944628Smckusick 		}
46044628Smckusick 	}
46144628Smckusick 
46244628Smckusick 	/*
46344628Smckusick 	 * Mount info
46444628Smckusick 	 */
46544628Smckusick 	if (minfo_flag) {
46644628Smckusick 		int dummy;
46744628Smckusick 		amq_mount_info_list *ml = amqproc_getmntfs_1(&dummy, clnt);
46844628Smckusick 		if (ml) {
46944628Smckusick 			int mwid = 0, dwid = 0, twid = 0;
47044628Smckusick 			show_mi(ml, Calc, &mwid, &dwid, &twid);
47144628Smckusick 			mwid++; dwid++; twid++;
47244628Smckusick 			show_mi(ml, Full, &mwid, &dwid, &twid);
47344628Smckusick 
47444628Smckusick 		} else {
47544628Smckusick 			fprintf(stderr, "%s: amd on %s cannot provide mount info\n", progname, server);
47644628Smckusick 		}
47744628Smckusick 	}
47844628Smckusick 
47944628Smckusick 	/*
48047529Spendry 	 * Mount map
48147529Spendry 	 */
48247529Spendry 	if (mount_map) {
48347529Spendry 		int *rc;
48447529Spendry 		do {
48547529Spendry 			rc = amqproc_mount_1(&mount_map, clnt);
48647529Spendry 		} while (rc && *rc < 0);
48747529Spendry 		if (!rc || *rc > 0) {
48847529Spendry 			if (rc)
48947529Spendry 				errno = *rc;
49047529Spendry 			else
49147529Spendry 				errno = ETIMEDOUT;
49247529Spendry 			fprintf(stderr, "%s: could not start new ", progname);
49347529Spendry 			perror("autmount point");
49447529Spendry 		}
49547529Spendry 	}
49647529Spendry 
49747529Spendry 	/*
49847529Spendry 	 * Get Version
49947529Spendry 	 */
50047529Spendry 	if (getvers_flag) {
50147529Spendry 		amq_string *spp = amqproc_getvers_1((voidp) 0, clnt);
50247529Spendry 		if (spp && *spp) {
50347529Spendry 			printf("%s.\n", *spp);
50447529Spendry 			free(*spp);
50547529Spendry 		} else {
50649686Spendry 			fprintf(stderr, "%s: failed to get version information\n", progname);
50747529Spendry 			errs = 1;
50847529Spendry 		}
50947529Spendry 	}
51047529Spendry 
51147529Spendry 	/*
51244628Smckusick 	 * Apply required operation to all remaining arguments
51344628Smckusick 	 */
51444628Smckusick 	if (optind < argc) {
51544628Smckusick 		do {
51644628Smckusick 			char *fs = argv[optind++];
51744628Smckusick 			if (unmount_flag) {
51844628Smckusick 				/*
51944628Smckusick 				 * Unmount request
52044628Smckusick 				 */
52144628Smckusick 				amqproc_umnt_1(&fs, clnt);
52244628Smckusick 			} else {
52344628Smckusick 				/*
52444628Smckusick 				 * Stats request
52544628Smckusick 				 */
52644628Smckusick 				amq_mount_tree_p *mtp = amqproc_mnttree_1(&fs, clnt);
52744628Smckusick 				if (mtp) {
52844628Smckusick 					amq_mount_tree *mt = *mtp;
52944628Smckusick 					if (mt) {
53044628Smckusick 						int mwid = 0, dwid = 0, twid = 0;
53144628Smckusick 						show_mt(mt, Calc, &mwid, &dwid, &twid);
53244628Smckusick 						mwid++; dwid++, twid++;
53344628Smckusick 		printf("%-*.*s Uid   Getattr Lookup RdDir   RdLnk   Statfs Mounted@\n",
53444628Smckusick 			dwid, dwid, "What");
53544628Smckusick 						show_mt(mt, Stats, &mwid, &dwid, &twid);
53644628Smckusick 					} else {
53744628Smckusick 						fprintf(stderr, "%s: %s not automounted\n", progname, fs);
53844628Smckusick 					}
53944628Smckusick 					xdr_pri_free(xdr_amq_mount_tree_p, (caddr_t) mtp);
54044628Smckusick 				} else {
54144628Smckusick 					fprintf(stderr, "%s: ", progname);
54244628Smckusick 					clnt_perror(clnt, server);
54344628Smckusick 					errs = 1;
54444628Smckusick 				}
54544628Smckusick 			}
54644628Smckusick 		} while (optind < argc);
54744628Smckusick 	} else if (unmount_flag) {
54844628Smckusick 		goto show_usage;
54944628Smckusick 	} else if (stats_flag) {
55044628Smckusick 		amq_mount_stats *ms = amqproc_stats_1((voidp) 0, clnt);
55144628Smckusick 		if (ms) {
55244628Smckusick 			show_ms(ms);
55344628Smckusick 		} else {
55444628Smckusick 			fprintf(stderr, "%s: ", progname);
55544628Smckusick 			clnt_perror(clnt, server);
55644628Smckusick 			errs = 1;
55744628Smckusick 		}
55844628Smckusick 	} else if (!nodefault) {
55944628Smckusick 		amq_mount_tree_list *mlp = amqproc_export_1((voidp) 0, clnt);
56044628Smckusick 		if (mlp) {
56144628Smckusick 			enum show_opt e = Calc;
56244628Smckusick 			int mwid = 0, dwid = 0, pwid = 0;
56344628Smckusick 			while (e != ShowDone) {
56444628Smckusick 				int i;
56544628Smckusick 				for (i = 0; i < mlp->amq_mount_tree_list_len; i++) {
56644628Smckusick 					show_mt(mlp->amq_mount_tree_list_val[i],
56744628Smckusick 						 e, &mwid, &dwid, &pwid);
56844628Smckusick 				}
56944628Smckusick 				mwid++; dwid++, pwid++;
57044628Smckusick 				if (e == Calc) e = Short;
57144628Smckusick 				else if (e == Short) e = ShowDone;
57244628Smckusick 			}
57344628Smckusick 		} else {
57444628Smckusick 			fprintf(stderr, "%s: ", progname);
57544628Smckusick 			clnt_perror(clnt, server);
57644628Smckusick 			errs = 1;
57744628Smckusick 		}
57844628Smckusick 	}
57944628Smckusick 
58044628Smckusick 	exit(errs);
58144628Smckusick }
58244628Smckusick 
58347529Spendry /*
58447529Spendry  * udpresport creates a datagram socket and attempts to bind it to a
58547529Spendry  * secure port.
58647529Spendry  * returns: The bound socket, or -1 to indicate an error.
58747529Spendry  */
inetresport(ty)58852453Spendry static int inetresport(ty)
58952453Spendry int ty;
59047529Spendry {
59147529Spendry 	int alport;
59247529Spendry 	struct sockaddr_in addr;
59347529Spendry 	int sock;
59447529Spendry 
59547529Spendry 	/* Use internet address family */
59647529Spendry 	addr.sin_family = AF_INET;
59747529Spendry 	addr.sin_addr.s_addr = INADDR_ANY;
59852453Spendry 	if ((sock = socket(AF_INET, ty, 0)) < 0)
59947529Spendry 		return -1;
60047529Spendry 	for (alport = IPPORT_RESERVED-1; alport > IPPORT_RESERVED/2 + 1; alport--) {
60147529Spendry 		addr.sin_port = htons((u_short)alport);
60247529Spendry 		if (bind(sock, (struct sockaddr *)&addr, sizeof (addr)) >= 0)
60347529Spendry 			return sock;
60447529Spendry 		if (errno != EADDRINUSE) {
60547529Spendry 			close(sock);
60647529Spendry 			return -1;
60747529Spendry 		}
60847529Spendry 	}
60947529Spendry 	close(sock);
61047529Spendry 	errno = EAGAIN;
61147529Spendry 	return -1;
61247529Spendry }
61347529Spendry 
61447529Spendry /*
61552453Spendry  * Privsock() calls inetresport() to attempt to bind a socket to a secure
61652453Spendry  * port.  If inetresport() fails, privsock returns a magic socket number which
61747529Spendry  * indicates to RPC that it should make its own socket.
61847529Spendry  * returns: A privileged socket # or RPC_ANYSOCK.
61947529Spendry  */
privsock(ty)62052453Spendry static int privsock(ty)
62152453Spendry int ty;
62247529Spendry {
62352453Spendry 	int sock = inetresport(ty);
62447529Spendry 
62547529Spendry 	if (sock < 0) {
62647529Spendry 		errno = 0;
62747529Spendry 		/* Couldn't get a secure port, let RPC make an insecure one */
62847529Spendry 		sock = RPC_ANYSOCK;
62947529Spendry 	}
63047529Spendry 	return sock;
63147529Spendry }
63247529Spendry 
63344628Smckusick #ifdef DEBUG
xfree(f,l,p)63444628Smckusick xfree(f, l, p)
63544628Smckusick char *f, *l;
63644628Smckusick voidp p;
63744628Smckusick {
63844628Smckusick 	free(p);
63944628Smckusick }
64044628Smckusick #endif /* DEBUG */
641