xref: /openbsd/usr.sbin/amd/amd/get_args.c (revision 10a7b758)
1 /*
2  * Copyright (c) 1990 Jan-Simon Pendry
3  * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
4  * Copyright (c) 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Jan-Simon Pendry at Imperial College, London.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	from: @(#)get_args.c	8.1 (Berkeley) 6/6/93
35  *	$Id: get_args.c,v 1.16 2021/11/15 15:14:24 millert Exp $
36  */
37 
38 /*
39  * Argument decode
40  */
41 
42 #include "am.h"
43 #include <syslog.h>
44 #include <sys/stat.h>
45 #include <unistd.h>
46 
47 #if defined(DEBUG) && defined(PARANOID)
48 char **gargv;
49 #endif /* defined(DEBUG) && defined(PARANOID) */
50 int restart_existing_mounts;
51 int print_pid;
52 int normalize_hosts;
53 char *karch;			/* Kernel architecture */
54 char *cluster;			/* Cluster name */
55 char *domain;			/* YP domain */
56 int afs_timeo = -1;
57 int afs_retrans = -1;
58 int am_timeo = AM_TTL;
59 int am_timeo_w = AM_TTL_W;
60 
61 #ifdef DEBUG
62 /*
63  * List of debug options.
64  */
65 static struct opt_tab dbg_opt[] = {
66 	{ "all", D_ALL },		/* All */
67 	{ "amq", D_AMQ },		/* Register for AMQ program */
68 	{ "daemon", D_DAEMON },		/* Enter daemon mode */
69 	{ "full", D_FULL },		/* Program trace */
70 	{ "mem", D_MEM },		/* Trace memory allocations */
71 	{ "mtab", D_MTAB },		/* Use local mtab file */
72 	{ "str", D_STR },		/* Debug string munging */
73 	{ "test", D_TEST },		/* Full debug - but no daemon */
74 	{ "trace", D_TRACE },		/* Protocol trace */
75 	{ 0, 0 }
76 };
77 
78 int debug_flags = D_AMQ			/* Register AMQ */
79 		 |D_DAEMON		/* Enter daemon mode */
80 		 ;
81 
82 /*
83  * Switch on/off debug options
84  */
85 int
debug_option(char * opt)86 debug_option(char *opt)
87 {
88 	return cmdoption(opt, dbg_opt, &debug_flags);
89 }
90 #endif /* DEBUG */
91 
92 void
get_args(int c,char * v[])93 get_args(int c, char *v[])
94 {
95 	int opt_ch;
96 	int usage = 0;
97 	char *logfile = "syslog";
98 	char *sub_domain = 0;
99 
100 	while ((opt_ch = getopt(c, v, "mnprva:c:d:h:k:l:t:w:x:y:C:D:")) != -1)
101 		switch (opt_ch) {
102 		case 'a':
103 			if (*optarg != '/') {
104 				fprintf(stderr, "%s: -a option must begin with a '/'\n",
105 				    __progname);
106 				exit(1);
107 			}
108 			auto_dir = optarg;
109 			break;
110 
111 		case 'c':
112 			am_timeo = atoi(optarg);
113 			if (am_timeo <= 0)
114 				am_timeo = AM_TTL;
115 			break;
116 
117 		case 'd':
118 			sub_domain = optarg;
119 			break;
120 
121 		case 'h':
122 #if defined(HAS_HOST) && defined(HOST_EXEC)
123 			host_helper = optarg;
124 #else
125 			plog(XLOG_USER, "-h: option ignored.  HOST_EXEC is not enabled.");
126 			break;
127 #endif /* defined(HAS_HOST) && defined(HOST_EXEC) */
128 
129 		case 'k':
130 			karch = optarg;
131 			break;
132 
133 		case 'l':
134 			logfile = optarg;
135 			break;
136 
137 		case 'm':
138 			plog(XLOG_USER, "The -m option is no longer supported.");
139 			plog(XLOG_USER, "... Use `ypcat -k am.master` on the command line instead");
140 			break;
141 
142 		case 'n':
143 			normalize_hosts = 1;
144 			break;
145 
146 		case 'p':
147 			print_pid = 1;
148 			break;
149 
150 		case 'r':
151 			restart_existing_mounts = 1;
152 			break;
153 
154 		case 't':
155 			/* timeo.retrans */
156 			{ char *dot = strchr(optarg, '.');
157 			  if (dot) *dot = '\0';
158 			  if (*optarg) {
159 				afs_timeo = atoi(optarg);
160 			  }
161 			  if (dot) {
162 				afs_retrans = atoi(dot+1);
163 				*dot = '.';
164 			  }
165 			}
166 			break;
167 
168 		case 'v':
169 			fputs("Map support for: ", stderr);
170 			mapc_showtypes(stderr);
171 			fputs(".\nFS: ", stderr);
172 			ops_showfstypes(stderr);
173 			fputs(".\n", stderr);
174 			fprintf(stderr, "Primary network is %s.\n", wire);
175 			exit(0);
176 			break;
177 
178 		case 'w':
179 			am_timeo_w = atoi(optarg);
180 			if (am_timeo_w <= 0)
181 				am_timeo_w = AM_TTL_W;
182 			break;
183 
184 		case 'x':
185 			usage += switch_option(optarg);
186 			break;
187 
188 		case 'y':
189 			domain = optarg;
190 			break;
191 
192 		case 'C':
193 			cluster = optarg;
194 			break;
195 
196 		case 'D':
197 #ifdef DEBUG
198 			usage += debug_option(optarg);
199 #else
200 			fprintf(stderr, "%s: not compiled with DEBUG option -- sorry.\n",
201 			    __progname);
202 #endif /* DEBUG */
203 			break;
204 
205 		default:
206 			usage = 1;
207 			break;
208 		}
209 
210 	if (xlog_level_init == ~0) {
211 		(void) switch_option("");
212 #ifdef DEBUG
213 		usage += switch_option("debug");
214 #endif /* DEBUG */
215 	} else {
216 #ifdef DEBUG
217 		usage += switch_option("debug");
218 #endif /* DEBUG */
219 	}
220 
221 	if (usage)
222 		goto show_usage;
223 
224 	while (optind <= c-2) {
225 		char *dir = v[optind++];
226 		char *map = v[optind++];
227 		char *opts = "";
228 		if (v[optind] && *v[optind] == '-')
229 			opts = &v[optind++][1];
230 
231 		root_newmap(dir, opts, map);
232 	}
233 
234 	if (optind == c) {
235 
236 		/*
237 		 * Append domain name to hostname.
238 		 * sub_domain overrides hostdomain
239 		 * if given.
240 		 */
241 		if (sub_domain)
242 			hostdomain = sub_domain;
243 		if (*hostdomain == '.')
244 			hostdomain++;
245 		strlcat(hostd,  ".", 2 * (HOST_NAME_MAX+1));
246 		strlcat(hostd, hostdomain, 2 * (HOST_NAME_MAX+1));
247 
248 #ifdef DEBUG
249 		{ if (debug_flags & D_MTAB) {
250 			dlog("-D mtab option ignored");
251 		} }
252 #endif /* DEBUG */
253 
254 		if (switch_to_logfile(logfile) != 0)
255 			plog(XLOG_USER, "Cannot switch logfile");
256 
257 		/*
258 		 * If the kernel architecture was not specified
259 		 * then use the machine architecture.
260 		 */
261 		if (karch == 0)
262 			karch = arch;
263 
264 		if (cluster == 0)
265 			cluster = hostdomain;
266 
267 		if (afs_timeo <= 0)
268 			afs_timeo = AFS_TIMEO;
269 		if (afs_retrans <= 0)
270 			afs_retrans = AFS_RETRANS;
271 		if (afs_retrans <= 0)
272 			afs_retrans = 3;	/* XXX */
273 		return;
274 	}
275 
276 show_usage:
277 	fprintf(stderr,
278 	    "usage: %s [-nprv] [-a mount_point] [-C cluster] "
279 	    "[-c duration] [-D option]\n"
280 	    "\t[-d domain] [-k kernel-arch] [-l logfile] "
281 	    "[-t interval.interval]\n"
282 	    "\t[-w interval]", __progname);
283 
284 #if defined(HAS_HOST) && defined(HOST_EXEC)
285 	fputs(" [-h host_helper]\n", stderr);
286 #endif /* defined(HAS_HOST) && defined(HOST_EXEC) */
287 
288 	fputs(" [-y YP-domain]\n", stderr);
289 
290 	show_opts('x', xlog_opt);
291 #ifdef DEBUG
292 	show_opts('D', dbg_opt);
293 #endif /* DEBUG */
294 	fprintf(stderr, "\t[directory mapname [-map-options]] ...\n");
295 	exit(1);
296 }
297