xref: /csrg-svn/usr.sbin/amd/amq/amq.c (revision 47529)
144628Smckusick /*
2*47529Spendry  * $Id: amq.c,v 5.2.1.3 91/03/17 17:33:42 jsp Alpha $
344628Smckusick  *
444628Smckusick  * Copyright (c) 1990 Jan-Simon Pendry
544628Smckusick  * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
644628Smckusick  * Copyright (c) 1990 The Regents of the University of California.
744628Smckusick  * All rights reserved.
844628Smckusick  *
944628Smckusick  * This code is derived from software contributed to Berkeley by
1044628Smckusick  * Jan-Simon Pendry at Imperial College, London.
1144628Smckusick  *
1244628Smckusick  * %sccs.include.redist.c%
1344628Smckusick  */
1444628Smckusick 
1544628Smckusick /*
1644628Smckusick  * Automounter query tool
1744628Smckusick  */
1844628Smckusick 
1944628Smckusick #ifndef lint
2044628Smckusick char copyright[] = "\
2144628Smckusick @(#)Copyright (c) 1990 Jan-Simon Pendry\n\
2244628Smckusick @(#)Copyright (c) 1990 Imperial College of Science, Technology & Medicine\n\
2344628Smckusick @(#)Copyright (c) 1990 The Regents of the University of California.\n\
2444628Smckusick @(#)All rights reserved.\n";
2544628Smckusick #endif /* not lint */
2644628Smckusick 
2744628Smckusick #ifndef lint
28*47529Spendry static char rcsid[] = "$Id: amq.c,v 5.2.1.3 91/03/17 17:33:42 jsp Alpha $";
29*47529Spendry static char sccsid[] = "@(#)amq.c	5.2 (Berkeley) 03/17/91";
3044628Smckusick #endif /* not lint */
3144628Smckusick 
3244628Smckusick #include "am.h"
3344628Smckusick #include "amq.h"
3444628Smckusick #include <stdio.h>
3544628Smckusick #include <fcntl.h>
3644628Smckusick #include <netdb.h>
3744628Smckusick 
38*47529Spendry static int privsock();
39*47529Spendry 
4044628Smckusick char *progname;
4144628Smckusick static int flush_flag;
4244628Smckusick static int minfo_flag;
4344628Smckusick static int unmount_flag;
4444628Smckusick static int stats_flag;
45*47529Spendry static int getvers_flag;
4644628Smckusick static char *debug_opts;
4744628Smckusick static char *logfile;
48*47529Spendry static char *mount_map;
49*47529Spendry static char *xlog_optstr;
5044628Smckusick static char localhost[] = "localhost";
5144628Smckusick static char *def_server = localhost;
5244628Smckusick 
5344628Smckusick extern int optind;
5444628Smckusick extern char *optarg;
5544628Smckusick 
5644628Smckusick static struct timeval tmo = { 10, 0 };
5744628Smckusick #define	TIMEOUT tmo
5844628Smckusick 
5944628Smckusick enum show_opt { Full, Stats, Calc, Short, ShowDone };
6044628Smckusick 
6144628Smckusick /*
6244628Smckusick  * If (e) is Calc then just calculate the sizes
6344628Smckusick  * Otherwise display the mount node on stdout
6444628Smckusick  */
6544628Smckusick static void show_mti(mt, e, mwid, dwid, twid)
6644628Smckusick amq_mount_tree *mt;
6744628Smckusick enum show_opt e;
6844628Smckusick int *mwid;
6944628Smckusick int *dwid;
7044628Smckusick int *twid;
7144628Smckusick {
7244628Smckusick 	switch (e) {
7344628Smckusick 	case Calc: {
7444628Smckusick 		int mw = strlen(mt->mt_mountinfo);
7544628Smckusick 		int dw = strlen(mt->mt_directory);
7644628Smckusick 		int tw = strlen(mt->mt_type);
7744628Smckusick 		if (mw > *mwid) *mwid = mw;
7844628Smckusick 		if (dw > *dwid) *dwid = dw;
7944628Smckusick 		if (tw > *twid) *twid = tw;
8044628Smckusick 	} break;
8144628Smckusick 
8244628Smckusick 	case Full: {
83*47529Spendry 		struct tm *tp = localtime((time_t *) &mt->mt_mounttime);
8444628Smckusick printf("%-*.*s %-*.*s %-*.*s %s\n\t%-5d %-7d %-6d %-7d %-7d %-6d %02d/%02d/%02d %02d:%02d:%02d\n",
8544628Smckusick 			*dwid, *dwid,
8644628Smckusick 			*mt->mt_directory ? mt->mt_directory : "/",	/* XXX */
8744628Smckusick 			*twid, *twid,
8844628Smckusick 			mt->mt_type,
8944628Smckusick 			*mwid, *mwid,
9044628Smckusick 			mt->mt_mountinfo,
9144628Smckusick 			mt->mt_mountpoint,
9244628Smckusick 
9344628Smckusick 			mt->mt_mountuid,
9444628Smckusick 			mt->mt_getattr,
9544628Smckusick 			mt->mt_lookup,
9644628Smckusick 			mt->mt_readdir,
9744628Smckusick 			mt->mt_readlink,
9844628Smckusick 			mt->mt_statfs,
9944628Smckusick 
10044628Smckusick 			tp->tm_year > 99 ? tp->tm_year - 100 : tp->tm_year,
10144628Smckusick 			tp->tm_mon+1, tp->tm_mday,
10244628Smckusick 			tp->tm_hour, tp->tm_min, tp->tm_sec);
10344628Smckusick 	} break;
10444628Smckusick 
10544628Smckusick 	case Stats: {
106*47529Spendry 		struct tm *tp = localtime((time_t *) &mt->mt_mounttime);
10744628Smckusick printf("%-*.*s %-5d %-7d %-6d %-7d %-7d %-6d %02d/%02d/%02d %02d:%02d:%02d\n",
10844628Smckusick 			*dwid, *dwid,
10944628Smckusick 			*mt->mt_directory ? mt->mt_directory : "/",	/* XXX */
11044628Smckusick 
11144628Smckusick 			mt->mt_mountuid,
11244628Smckusick 			mt->mt_getattr,
11344628Smckusick 			mt->mt_lookup,
11444628Smckusick 			mt->mt_readdir,
11544628Smckusick 			mt->mt_readlink,
11644628Smckusick 			mt->mt_statfs,
11744628Smckusick 
11844628Smckusick 			tp->tm_year > 99 ? tp->tm_year - 100 : tp->tm_year,
11944628Smckusick 			tp->tm_mon+1, tp->tm_mday,
12044628Smckusick 			tp->tm_hour, tp->tm_min, tp->tm_sec);
12144628Smckusick 	} break;
12244628Smckusick 
12344628Smckusick 	case Short: {
12444628Smckusick 		printf("%-*.*s %-*.*s %-*.*s %s\n",
12544628Smckusick 			*dwid, *dwid,
12644628Smckusick 			*mt->mt_directory ? mt->mt_directory : "/",
12744628Smckusick 			*twid, *twid,
12844628Smckusick 			mt->mt_type,
12944628Smckusick 			*mwid, *mwid,
13044628Smckusick 			mt->mt_mountinfo,
13144628Smckusick 			mt->mt_mountpoint);
13244628Smckusick 	} break;
13344628Smckusick 	}
13444628Smckusick }
13544628Smckusick 
13644628Smckusick /*
13744628Smckusick  * Display a mount tree.
13844628Smckusick  */
13944628Smckusick static void show_mt(mt, e, mwid, dwid, pwid)
14044628Smckusick amq_mount_tree *mt;
14144628Smckusick enum show_opt e;
14244628Smckusick int *mwid;
14344628Smckusick int *dwid;
14444628Smckusick int *pwid;
14544628Smckusick {
14644628Smckusick 	while (mt) {
14744628Smckusick 		show_mti(mt, e, mwid, dwid, pwid);
14844628Smckusick 		show_mt(mt->mt_next, e, mwid, dwid, pwid);
14944628Smckusick 		mt = mt->mt_child;
15044628Smckusick 	}
15144628Smckusick }
15244628Smckusick 
15344628Smckusick static void show_mi(ml, e, mwid, dwid, twid)
15444628Smckusick amq_mount_info_list *ml;
15544628Smckusick enum show_opt e;
15644628Smckusick int *mwid;
15744628Smckusick int *dwid;
15844628Smckusick int *twid;
15944628Smckusick {
16044628Smckusick 	int i;
16144628Smckusick 	switch (e) {
16244628Smckusick 	case Calc: {
16344628Smckusick 		for (i = 0; i < ml->amq_mount_info_list_len; i++) {
16444628Smckusick 			amq_mount_info *mi = &ml->amq_mount_info_list_val[i];
16544628Smckusick 			int mw = strlen(mi->mi_mountinfo);
16644628Smckusick 			int dw = strlen(mi->mi_mountpt);
16744628Smckusick 			int tw = strlen(mi->mi_type);
16844628Smckusick 			if (mw > *mwid) *mwid = mw;
16944628Smckusick 			if (dw > *dwid) *dwid = dw;
17044628Smckusick 			if (tw > *twid) *twid = tw;
17144628Smckusick 		}
17244628Smckusick 	} break;
17344628Smckusick 
17444628Smckusick 	case Full: {
17544628Smckusick 		for (i = 0; i < ml->amq_mount_info_list_len; i++) {
17644628Smckusick 			amq_mount_info *mi = &ml->amq_mount_info_list_val[i];
17744628Smckusick 			printf("%-*.*s %-*.*s %-*.*s %-3d %s is %s",
17844628Smckusick 						*mwid, *mwid, mi->mi_mountinfo,
17944628Smckusick 						*dwid, *dwid, mi->mi_mountpt,
18044628Smckusick 						*twid, *twid, mi->mi_type,
18144628Smckusick 						mi->mi_refc, mi->mi_fserver,
18244628Smckusick 						mi->mi_up > 0 ? "up" :
18344628Smckusick 						mi->mi_up < 0 ? "starting" : "down");
18444628Smckusick 			if (mi->mi_error > 0) {
18544628Smckusick 				extern char *sys_errlist[];
18644628Smckusick 				extern int sys_nerr;
18744628Smckusick 				if (mi->mi_error < sys_nerr)
18844628Smckusick 					printf(" (%s)", sys_errlist[mi->mi_error]);
18944628Smckusick 				else
19044628Smckusick 					printf(" (Error %d)", mi->mi_error);
19144628Smckusick 			} else if (mi->mi_error < 0) {
19244628Smckusick 				fputs(" (in progress)", stdout);
19344628Smckusick 			}
19444628Smckusick 			fputc('\n', stdout);
19544628Smckusick 		}
19644628Smckusick 	} break;
19744628Smckusick 	}
19844628Smckusick }
19944628Smckusick 
20044628Smckusick /*
20144628Smckusick  * Display general mount statistics
20244628Smckusick  */
20344628Smckusick static void show_ms(ms)
20444628Smckusick amq_mount_stats *ms;
20544628Smckusick {
20644628Smckusick 	printf("\
20744628Smckusick requests  stale     mount     mount     unmount\n\
20844628Smckusick deferred  fhandles  ok        failed    failed\n\
20944628Smckusick %-9d %-9d %-9d %-9d %-9d\n",
21044628Smckusick 	ms->as_drops, ms->as_stale, ms->as_mok, ms->as_merr, ms->as_uerr);
21144628Smckusick }
21244628Smckusick 
21344628Smckusick static bool_t
21444628Smckusick xdr_pri_free(xdr_args, args_ptr)
21544628Smckusick xdrproc_t xdr_args;
21644628Smckusick caddr_t args_ptr;
21744628Smckusick {
21844628Smckusick 	XDR xdr;
21944628Smckusick 	xdr.x_op = XDR_FREE;
22044628Smckusick 	return ((*xdr_args)(&xdr, args_ptr));
22144628Smckusick }
22244628Smckusick 
22344628Smckusick #ifdef hpux
22444628Smckusick #include <cluster.h>
22544628Smckusick static char *cluster_server()
22644628Smckusick {
22744628Smckusick 	struct cct_entry *cp;
22844628Smckusick 
22944628Smckusick 	if (cnodeid() == 0) {
23044628Smckusick 		/*
23144628Smckusick 		 * Not clustered
23244628Smckusick 		 */
23344628Smckusick 		return def_server;
23444628Smckusick 	}
23544628Smckusick 
23644628Smckusick 	while (cp = getccent())
23744628Smckusick 		if (cp->cnode_type == 'r')
23844628Smckusick 			return cp->cnode_name;
23944628Smckusick 
24044628Smckusick 
24144628Smckusick 	return def_server;
24244628Smckusick }
24344628Smckusick #endif /* hpux */
24444628Smckusick 
24544628Smckusick /*
24644628Smckusick  * MAIN
24744628Smckusick  */
24844628Smckusick main(argc, argv)
24944628Smckusick int argc;
25044628Smckusick char *argv[];
25144628Smckusick {
25244628Smckusick 	int opt_ch;
25344628Smckusick 	int errs = 0;
25444628Smckusick 	char *server;
25544628Smckusick 	struct sockaddr_in server_addr;
256*47529Spendry 
257*47529Spendry 	/* In order to pass the Amd security check, we must use a priv port. */
258*47529Spendry 	int s;
259*47529Spendry 
26044628Smckusick 	CLIENT *clnt;
26144628Smckusick 	struct hostent *hp;
26244628Smckusick 	int nodefault = 0;
26344628Smckusick 
26444628Smckusick 	/*
26544628Smckusick 	 * Compute program name
26644628Smckusick 	 */
26744628Smckusick 	if (argv[0]) {
26844628Smckusick 		progname = strrchr(argv[0], '/');
26944628Smckusick 		if (progname && progname[1])
27044628Smckusick 			progname++;
27144628Smckusick 		else
27244628Smckusick 			progname = argv[0];
27344628Smckusick 	}
27444628Smckusick 	if (!progname)
27544628Smckusick 		progname = "amq";
27644628Smckusick 
27744628Smckusick 	/*
27844628Smckusick 	 * Parse arguments
27944628Smckusick 	 */
280*47529Spendry 	while ((opt_ch = getopt(argc, argv, "fh:l:msuvx:D:M:")) != EOF)
28144628Smckusick 	switch (opt_ch) {
28244628Smckusick 	case 'f':
28344628Smckusick 		flush_flag = 1;
284*47529Spendry 		nodefault = 1;
28544628Smckusick 		break;
28644628Smckusick 
28744628Smckusick 	case 'h':
28844628Smckusick 		def_server = optarg;
28944628Smckusick 		break;
29044628Smckusick 
29144628Smckusick 	case 'l':
29244628Smckusick 		logfile = optarg;
29344628Smckusick 		nodefault = 1;
29444628Smckusick 		break;
29544628Smckusick 
29644628Smckusick 	case 'm':
29744628Smckusick 		minfo_flag = 1;
29844628Smckusick 		nodefault = 1;
29944628Smckusick 		break;
30044628Smckusick 
30144628Smckusick 	case 's':
30244628Smckusick 		stats_flag = 1;
303*47529Spendry 		nodefault = 1;
30444628Smckusick 		break;
30544628Smckusick 
30644628Smckusick 	case 'u':
30744628Smckusick 		unmount_flag = 1;
308*47529Spendry 		nodefault = 1;
30944628Smckusick 		break;
31044628Smckusick 
311*47529Spendry 	case 'v':
312*47529Spendry 		getvers_flag = 1;
313*47529Spendry 		nodefault = 1;
314*47529Spendry 		break;
315*47529Spendry 
31644628Smckusick 	case 'x':
317*47529Spendry 		xlog_optstr = optarg;
31844628Smckusick 		nodefault = 1;
31944628Smckusick 		break;
32044628Smckusick 
32144628Smckusick 	case 'D':
32244628Smckusick 		debug_opts = optarg;
32344628Smckusick 		nodefault = 1;
32444628Smckusick 		break;
32544628Smckusick 
326*47529Spendry 	case 'M':
327*47529Spendry 		mount_map = optarg;
328*47529Spendry 		nodefault = 1;
329*47529Spendry 		break;
330*47529Spendry 
33144628Smckusick 	default:
33244628Smckusick 		errs = 1;
33344628Smckusick 		break;
33444628Smckusick 	}
33544628Smckusick 
336*47529Spendry 	if (optind == argc) {
337*47529Spendry 		if (unmount_flag)
338*47529Spendry 			errs = 1;
339*47529Spendry 	}
340*47529Spendry 
34144628Smckusick 	if (errs) {
34244628Smckusick show_usage:
34344628Smckusick 		fprintf(stderr, "\
344*47529Spendry Usage: %s [-h host] [[-f] [-m] [-v] [-s]] | [[-u] directory ...]] |\n\
345*47529Spendry \t[-l logfile|\"syslog\"] [-x log_flags] [-D dbg_opts] [-M mapent]\n", progname);
34644628Smckusick 		exit(1);
34744628Smckusick 	}
34844628Smckusick 
34944628Smckusick #ifdef hpux
35044628Smckusick 	/*
35144628Smckusick 	 * Figure out root server of cluster
35244628Smckusick 	 */
35344628Smckusick 	if (def_server == localhost)
35444628Smckusick 		server = cluster_server();
35544628Smckusick 	else
35644628Smckusick #endif /* hpux */
35744628Smckusick 	server = def_server;
35844628Smckusick 
35944628Smckusick 	/*
36044628Smckusick 	 * Get address of server
36144628Smckusick 	 */
36244628Smckusick 	if ((hp = gethostbyname(server)) == 0) {
36344628Smckusick 		fprintf(stderr, "%s: Can't get address of %s\n", progname, server);
36444628Smckusick 		exit(1);
36544628Smckusick 	}
36644628Smckusick 	bzero(&server_addr, sizeof server_addr);
36744628Smckusick 	server_addr.sin_family = AF_INET;
368*47529Spendry 	bcopy((voidp) hp->h_addr, (voidp) &server_addr.sin_addr, sizeof(server_addr.sin_addr));
36944628Smckusick 
37044628Smckusick 	/*
37144628Smckusick 	 * Create RPC endpoint
37244628Smckusick 	 */
373*47529Spendry 	s = privsock();
37444628Smckusick 	clnt = clntudp_create(&server_addr, AMQ_PROGRAM, AMQ_VERSION, TIMEOUT, &s);
37544628Smckusick 	if (clnt == 0) {
37644628Smckusick 		fprintf(stderr, "%s: ", progname);
37744628Smckusick 		clnt_pcreateerror(server);
37844628Smckusick 		exit(1);
37944628Smckusick 	}
38044628Smckusick 
38144628Smckusick 	/*
38244628Smckusick 	 * Control debugging
38344628Smckusick 	 */
38444628Smckusick 	if (debug_opts) {
38544628Smckusick 		int *rc;
38644628Smckusick 		amq_setopt opt;
38744628Smckusick 		opt.as_opt = AMOPT_DEBUG;
38844628Smckusick 		opt.as_str = debug_opts;
38944628Smckusick 		rc = amqproc_setopt_1(&opt, clnt);
39044628Smckusick 		if (rc && *rc < 0) {
39144628Smckusick 			fprintf(stderr, "%s: daemon not compiled for debug", progname);
39244628Smckusick 			errs = 1;
39344628Smckusick 		} else if (!rc || *rc > 0) {
39444628Smckusick 			fprintf(stderr, "%s: debug setting for \"%s\" failed\n", progname, debug_opts);
39544628Smckusick 			errs = 1;
39644628Smckusick 		}
39744628Smckusick 	}
39844628Smckusick 
39944628Smckusick 	/*
40044628Smckusick 	 * Control logging
40144628Smckusick 	 */
402*47529Spendry 	if (xlog_optstr) {
40344628Smckusick 		int *rc;
40444628Smckusick 		amq_setopt opt;
40544628Smckusick 		opt.as_opt = AMOPT_XLOG;
406*47529Spendry 		opt.as_str = xlog_optstr;
40744628Smckusick 		rc = amqproc_setopt_1(&opt, clnt);
40844628Smckusick 		if (!rc || *rc) {
409*47529Spendry 			fprintf(stderr, "%s: setting log level to \"%s\" failed\n", progname, xlog_optstr);
41044628Smckusick 			errs = 1;
41144628Smckusick 		}
41244628Smckusick 	}
41344628Smckusick 
41444628Smckusick 	/*
41544628Smckusick 	 * Control log file
41644628Smckusick 	 */
41744628Smckusick 	if (logfile) {
41844628Smckusick 		int *rc;
41944628Smckusick 		amq_setopt opt;
42044628Smckusick 		opt.as_opt = AMOPT_LOGFILE;
42144628Smckusick 		opt.as_str = logfile;
42244628Smckusick 		rc = amqproc_setopt_1(&opt, clnt);
42344628Smckusick 		if (!rc || *rc) {
42444628Smckusick 			fprintf(stderr, "%s: setting logfile to \"%s\" failed\n", progname, logfile);
42544628Smckusick 			errs = 1;
42644628Smckusick 		}
42744628Smckusick 	}
42844628Smckusick 
42944628Smckusick 	/*
43044628Smckusick 	 * Flush map cache
43144628Smckusick 	 */
432*47529Spendry 	if (flush_flag) {
43344628Smckusick 		int *rc;
43444628Smckusick 		amq_setopt opt;
43544628Smckusick 		opt.as_opt = AMOPT_FLUSHMAPC;
43644628Smckusick 		opt.as_str = "";
43744628Smckusick 		rc = amqproc_setopt_1(&opt, clnt);
43844628Smckusick 		if (!rc || *rc) {
43944628Smckusick 			fprintf(stderr, "%s: amd on %s cannot flush the map cache\n", progname, server);
44044628Smckusick 			errs = 1;
44144628Smckusick 		}
44244628Smckusick 	}
44344628Smckusick 
44444628Smckusick 	/*
44544628Smckusick 	 * Mount info
44644628Smckusick 	 */
44744628Smckusick 	if (minfo_flag) {
44844628Smckusick 		int dummy;
44944628Smckusick 		amq_mount_info_list *ml = amqproc_getmntfs_1(&dummy, clnt);
45044628Smckusick 		if (ml) {
45144628Smckusick 			int mwid = 0, dwid = 0, twid = 0;
45244628Smckusick 			show_mi(ml, Calc, &mwid, &dwid, &twid);
45344628Smckusick 			mwid++; dwid++; twid++;
45444628Smckusick 			show_mi(ml, Full, &mwid, &dwid, &twid);
45544628Smckusick 
45644628Smckusick 		} else {
45744628Smckusick 			fprintf(stderr, "%s: amd on %s cannot provide mount info\n", progname, server);
45844628Smckusick 		}
45944628Smckusick 	}
46044628Smckusick 
46144628Smckusick 	/*
462*47529Spendry 	 * Mount map
463*47529Spendry 	 */
464*47529Spendry 	if (mount_map) {
465*47529Spendry 		int *rc;
466*47529Spendry 		do {
467*47529Spendry 			rc = amqproc_mount_1(&mount_map, clnt);
468*47529Spendry 		} while (rc && *rc < 0);
469*47529Spendry 		if (!rc || *rc > 0) {
470*47529Spendry 			if (rc)
471*47529Spendry 				errno = *rc;
472*47529Spendry 			else
473*47529Spendry 				errno = ETIMEDOUT;
474*47529Spendry 			fprintf(stderr, "%s: could not start new ", progname);
475*47529Spendry 			perror("autmount point");
476*47529Spendry 		}
477*47529Spendry 	}
478*47529Spendry 
479*47529Spendry 	/*
480*47529Spendry 	 * Get Version
481*47529Spendry 	 */
482*47529Spendry 	if (getvers_flag) {
483*47529Spendry 		amq_string *spp = amqproc_getvers_1((voidp) 0, clnt);
484*47529Spendry 		if (spp && *spp) {
485*47529Spendry 			printf("%s.\n", *spp);
486*47529Spendry 			free(*spp);
487*47529Spendry 		} else {
488*47529Spendry 			fprintf(stderr, "%s: failed to get version infromation\n", progname);
489*47529Spendry 			errs = 1;
490*47529Spendry 		}
491*47529Spendry 	}
492*47529Spendry 
493*47529Spendry 	/*
49444628Smckusick 	 * Apply required operation to all remaining arguments
49544628Smckusick 	 */
49644628Smckusick 	if (optind < argc) {
49744628Smckusick 		do {
49844628Smckusick 			char *fs = argv[optind++];
49944628Smckusick 			if (unmount_flag) {
50044628Smckusick 				/*
50144628Smckusick 				 * Unmount request
50244628Smckusick 				 */
50344628Smckusick 				amqproc_umnt_1(&fs, clnt);
50444628Smckusick 			} else {
50544628Smckusick 				/*
50644628Smckusick 				 * Stats request
50744628Smckusick 				 */
50844628Smckusick 				amq_mount_tree_p *mtp = amqproc_mnttree_1(&fs, clnt);
50944628Smckusick 				if (mtp) {
51044628Smckusick 					amq_mount_tree *mt = *mtp;
51144628Smckusick 					if (mt) {
51244628Smckusick 						int mwid = 0, dwid = 0, twid = 0;
51344628Smckusick 						show_mt(mt, Calc, &mwid, &dwid, &twid);
51444628Smckusick 						mwid++; dwid++, twid++;
51544628Smckusick 		printf("%-*.*s Uid   Getattr Lookup RdDir   RdLnk   Statfs Mounted@\n",
51644628Smckusick 			dwid, dwid, "What");
51744628Smckusick 						show_mt(mt, Stats, &mwid, &dwid, &twid);
51844628Smckusick 					} else {
51944628Smckusick 						fprintf(stderr, "%s: %s not automounted\n", progname, fs);
52044628Smckusick 					}
52144628Smckusick 					xdr_pri_free(xdr_amq_mount_tree_p, (caddr_t) mtp);
52244628Smckusick 				} else {
52344628Smckusick 					fprintf(stderr, "%s: ", progname);
52444628Smckusick 					clnt_perror(clnt, server);
52544628Smckusick 					errs = 1;
52644628Smckusick 				}
52744628Smckusick 			}
52844628Smckusick 		} while (optind < argc);
52944628Smckusick 	} else if (unmount_flag) {
53044628Smckusick 		goto show_usage;
53144628Smckusick 	} else if (stats_flag) {
53244628Smckusick 		amq_mount_stats *ms = amqproc_stats_1((voidp) 0, clnt);
53344628Smckusick 		if (ms) {
53444628Smckusick 			show_ms(ms);
53544628Smckusick 		} else {
53644628Smckusick 			fprintf(stderr, "%s: ", progname);
53744628Smckusick 			clnt_perror(clnt, server);
53844628Smckusick 			errs = 1;
53944628Smckusick 		}
54044628Smckusick 	} else if (!nodefault) {
54144628Smckusick 		amq_mount_tree_list *mlp = amqproc_export_1((voidp) 0, clnt);
54244628Smckusick 		if (mlp) {
54344628Smckusick 			enum show_opt e = Calc;
54444628Smckusick 			int mwid = 0, dwid = 0, pwid = 0;
54544628Smckusick 			while (e != ShowDone) {
54644628Smckusick 				int i;
54744628Smckusick 				for (i = 0; i < mlp->amq_mount_tree_list_len; i++) {
54844628Smckusick 					show_mt(mlp->amq_mount_tree_list_val[i],
54944628Smckusick 						 e, &mwid, &dwid, &pwid);
55044628Smckusick 				}
55144628Smckusick 				mwid++; dwid++, pwid++;
55244628Smckusick 				if (e == Calc) e = Short;
55344628Smckusick 				else if (e == Short) e = ShowDone;
55444628Smckusick 			}
55544628Smckusick 		} else {
55644628Smckusick 			fprintf(stderr, "%s: ", progname);
55744628Smckusick 			clnt_perror(clnt, server);
55844628Smckusick 			errs = 1;
55944628Smckusick 		}
56044628Smckusick 	}
56144628Smckusick 
56244628Smckusick 	exit(errs);
56344628Smckusick }
56444628Smckusick 
565*47529Spendry /*
566*47529Spendry  * udpresport creates a datagram socket and attempts to bind it to a
567*47529Spendry  * secure port.
568*47529Spendry  * returns: The bound socket, or -1 to indicate an error.
569*47529Spendry  */
570*47529Spendry static int udpresport()
571*47529Spendry {
572*47529Spendry 	int alport;
573*47529Spendry 	struct sockaddr_in addr;
574*47529Spendry 	int sock;
575*47529Spendry 
576*47529Spendry 	/* Use internet address family */
577*47529Spendry 	addr.sin_family = AF_INET;
578*47529Spendry 	addr.sin_addr.s_addr = INADDR_ANY;
579*47529Spendry 	if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
580*47529Spendry 		return -1;
581*47529Spendry 	for (alport = IPPORT_RESERVED-1; alport > IPPORT_RESERVED/2 + 1; alport--) {
582*47529Spendry 		addr.sin_port = htons((u_short)alport);
583*47529Spendry 		if (bind(sock, (struct sockaddr *)&addr, sizeof (addr)) >= 0)
584*47529Spendry 			return sock;
585*47529Spendry 		if (errno != EADDRINUSE) {
586*47529Spendry 			close(sock);
587*47529Spendry 			return -1;
588*47529Spendry 		}
589*47529Spendry 	}
590*47529Spendry 	close(sock);
591*47529Spendry 	errno = EAGAIN;
592*47529Spendry 	return -1;
593*47529Spendry }
594*47529Spendry 
595*47529Spendry /*
596*47529Spendry  * Privsock() calls udpresport() to attempt to bind a socket to a secure
597*47529Spendry  * port.  If udpresport() fails, privsock returns a magic socket number which
598*47529Spendry  * indicates to RPC that it should make its own socket.
599*47529Spendry  * returns: A privileged socket # or RPC_ANYSOCK.
600*47529Spendry  */
601*47529Spendry static int privsock()
602*47529Spendry {
603*47529Spendry 	int sock = udpresport();
604*47529Spendry 
605*47529Spendry 	if (sock < 0) {
606*47529Spendry 		errno = 0;
607*47529Spendry 		/* Couldn't get a secure port, let RPC make an insecure one */
608*47529Spendry 		sock = RPC_ANYSOCK;
609*47529Spendry 	}
610*47529Spendry 	return sock;
611*47529Spendry }
612*47529Spendry 
61344628Smckusick #ifdef DEBUG
61444628Smckusick xfree(f, l, p)
61544628Smckusick char *f, *l;
61644628Smckusick voidp p;
61744628Smckusick {
61844628Smckusick 	free(p);
61944628Smckusick }
62044628Smckusick #endif /* DEBUG */
621