2 * Copyright (c) 1997-2006 Erez Zadok
3 * Copyright (c) 1990 Jan-Simon Pendry
4 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
5 * Copyright (c) 1990 The Regents of the University of California.
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry at Imperial College, London.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgment:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * File: am-utils/amd/get_args.c
50 #endif /* HAVE_CONFIG_H */
54 /* include auto-generated version file */
55 #include <build_version.h>
57 char *amu_conf_file = "/etc/amd.conf"; /* default amd configuration file */
58 char *conf_tag = NULL; /* default conf file tags to use */
60 int use_conf_file = 0; /* default don't use amd.conf file */
61 char *mnttab_file_name = NULL; /* symbol must be available always */
65 * Return the version string (dynamic buffer)
68 get_version_string(void)
74 size_t len; /* max allocated length (to avoid buf overflow) */
77 * First get dynamic string listing all known networks.
78 * This could be a long list, if host has lots of interfaces.
80 wire_buf = print_wires();
82 wire_buf_len = strlen(wire_buf);
84 len = 2048 + wire_buf_len;
86 xsnprintf(vers, len, "%s\n%s\n%s\n%s\n",
87 "Copyright (c) 1997-2006 Erez Zadok",
88 "Copyright (c) 1990 Jan-Simon Pendry",
89 "Copyright (c) 1990 Imperial College of Science, Technology & Medicine",
90 "Copyright (c) 1990 The Regents of the University of California.");
91 xsnprintf(tmpbuf, sizeof(tmpbuf), "%s version %s (build %d).\n",
92 PACKAGE_NAME, PACKAGE_VERSION, AMU_BUILD_VERSION);
93 strlcat(vers, tmpbuf, len);
94 xsnprintf(tmpbuf, sizeof(tmpbuf), "Report bugs to %s.\n", PACKAGE_BUGREPORT);
95 strlcat(vers, tmpbuf, len);
96 xsnprintf(tmpbuf, sizeof(tmpbuf), "cpu=%s (%s-endian), arch=%s, karch=%s.\n",
97 cpu, endian, gopt.arch, gopt.karch);
98 strlcat(vers, tmpbuf, len);
99 xsnprintf(tmpbuf, sizeof(tmpbuf), "full_os=%s, os=%s, osver=%s, vendor=%s, distro=%s.\n",
100 gopt.op_sys_full, gopt.op_sys, gopt.op_sys_ver, gopt.op_sys_vendor, DISTRO_NAME);
101 strlcat(vers, tmpbuf, len);
102 xsnprintf(tmpbuf, sizeof(tmpbuf), "domain=%s, host=%s, hostd=%s.\n",
103 hostdomain, am_get_hostname(), hostd);
104 strlcat(vers, tmpbuf, len);
106 strlcat(vers, "Map support for: ", len);
107 mapc_showtypes(tmpbuf, sizeof(tmpbuf));
108 strlcat(vers, tmpbuf, len);
109 strlcat(vers, ".\nAMFS: ", len);
110 ops_showamfstypes(tmpbuf, sizeof(tmpbuf));
111 strlcat(vers, tmpbuf, len);
112 strlcat(vers, ", inherit.\nFS: ", len); /* hack: "show" that we support type:=inherit */
113 ops_showfstypes(tmpbuf, sizeof(tmpbuf));
114 strlcat(vers, tmpbuf, len);
116 /* append list of networks if available */
118 strlcat(vers, wire_buf, len);
130 "Usage: %s [-nprvHS] [-a mount_point] [-c cache_time] [-d domain]\n\
131 \t[-k kernel_arch] [-l logfile%s\n\
132 \t[-t timeout.retrans] [-w wait_timeout] [-A arch] [-C cluster_name]\n\
133 \t[-o op_sys_ver] [-O op_sys_name]\n\
134 \t[-F conf_file] [-T conf_tag]", am_get_progname(),
137 "|\"syslog[:facility]\"]"
138 # else /* not LOG_DAEMON */
140 # endif /* not LOG_DAEMON */
141 #else /* not HAVE_SYSLOG */
143 #endif /* not HAVE_SYSLOG */
147 fputs(" [-y nis-domain]\n", stderr);
148 #else /* not HAVE_MAP_NIS */
150 #endif /* HAVE_MAP_NIS */
152 show_opts('x', xlog_opt);
154 show_opts('D', dbg_opt);
156 fprintf(stderr, "\t[directory mapname [-map_options]] ...\n");
161 get_args(int argc, char *argv[])
165 char getopt_arguments[] = "+nprvSa:c:d:k:l:o:t:w:x:y:C:D:F:T:O:HA:";
167 int print_version = 0; /* 1 means we should print version info */
169 #ifdef HAVE_GNU_GETOPT
170 getopt_args = getopt_arguments;
171 #else /* ! HAVE_GNU_GETOPT */
172 getopt_args = &getopt_arguments[1];
173 #endif /* HAVE_GNU_GETOPT */
175 /* if no arguments were passed, try to use /etc/amd.conf file */
179 while ((opt_ch = getopt(argc, argv, getopt_args)) != -1)
183 if (*optarg != '/') {
184 fprintf(stderr, "%s: -a option must begin with a '/'\n",
188 gopt.auto_dir = optarg;
192 gopt.am_timeo = atoi(optarg);
193 if (gopt.am_timeo <= 0)
194 gopt.am_timeo = AM_TTL;
198 gopt.sub_domain = optarg;
206 gopt.logfile = optarg;
210 gopt.flags |= CFM_NORMALIZE_HOSTNAMES;
214 gopt.op_sys_ver = optarg;
218 gopt.flags |= CFM_PRINT_PID;
222 gopt.flags |= CFM_RESTART_EXISTING_MOUNTS;
226 /* timeo.retrans (also affects toplvl mounts) */
228 char *dot = strchr(optarg, '.');
233 for (i=0; i<AMU_TYPE_MAX; ++i)
234 gopt.amfs_auto_timeo[i] = atoi(optarg);
237 for (i=0; i<AMU_TYPE_MAX; ++i)
238 gopt.amfs_auto_retrans[i] = atoi(dot + 1);
246 * defer to print version info after every variable had been
253 gopt.am_timeo_w = atoi(optarg);
254 if (gopt.am_timeo_w <= 0)
255 gopt.am_timeo_w = AM_TTL_W;
259 usage += switch_option(optarg);
264 gopt.nis_domain = optarg;
265 #else /* not HAVE_MAP_NIS */
266 plog(XLOG_USER, "-y: option ignored. No NIS support available.");
267 #endif /* not HAVE_MAP_NIS */
275 gopt.cluster = optarg;
280 usage += debug_option(optarg);
281 #else /* not DEBUG */
282 fprintf(stderr, "%s: not compiled with DEBUG option -- sorry.\n",
284 #endif /* not DEBUG */
288 amu_conf_file = optarg;
298 gopt.op_sys = optarg;
302 gopt.flags &= ~CFM_PROCESS_LOCK; /* turn process locking off */
315 * amd.conf file: if not command-line arguments were used, or if -F was
316 * specified, then use that amd.conf file. If the file cannot be opened,
317 * abort amd. If it can be found, open it, parse it, and then close it.
319 if (use_conf_file && amu_conf_file) {
320 fp = fopen(amu_conf_file, "r");
323 xsnprintf(buf, sizeof(buf), "Amd configuration file (%s)",
331 if (process_all_regular_maps() != 0)
335 /* make sure there are some default options defined */
336 if (xlog_level_init == ~0) {
340 usage += switch_option("debug");
343 /* log information regarding amd.conf file */
344 if (use_conf_file && amu_conf_file)
345 plog(XLOG_INFO, "using configuration file %s", amu_conf_file);
348 /* ensure that if ldap_base is specified, that also ldap_hostports is */
349 if (gopt.ldap_hostports && !gopt.ldap_base) {
350 fprintf(stderr, "must specify both ldap_hostports and ldap_base\n");
353 #endif /* HAVE_MAP_LDAP */
360 while (optind <= argc - 2) {
361 char *dir = argv[optind++];
362 char *map = argv[optind++];
364 if (argv[optind] && *argv[optind] == '-')
365 opts = &argv[optind++][1];
367 root_newmap(dir, opts, map, NULL);
370 if (optind == argc) {
372 * Append domain name to hostname.
373 * sub_domain overrides hostdomain
377 hostdomain = gopt.sub_domain;
378 if (*hostdomain == '.')
380 xstrlcat(hostd, ".", sizeof(hostd));
381 xstrlcat(hostd, hostdomain, sizeof(hostd));
383 #ifdef MOUNT_TABLE_ON_FILE
384 if (amuDebug(D_MTAB))
385 if (gopt.debug_mtab_file)
386 mnttab_file_name = gopt.debug_mtab_file; /* user supplied debug mtab path */
388 mnttab_file_name = DEBUG_MNTTAB_FILE; /* default debug mtab path */
390 mnttab_file_name = MNTTAB_FILE_NAME;
391 #else /* not MOUNT_TABLE_ON_FILE */
392 if (amuDebug(D_MTAB))
393 dlog("-D mtab option ignored");
394 # ifdef MNTTAB_FILE_NAME
395 mnttab_file_name = MNTTAB_FILE_NAME;
396 # endif /* MNTTAB_FILE_NAME */
397 #endif /* not MOUNT_TABLE_ON_FILE */
400 * If the kernel architecture was not specified
401 * then use the machine architecture.
403 if (gopt.karch == NULL)
404 gopt.karch = gopt.arch;
406 if (gopt.cluster == NULL)
407 gopt.cluster = hostdomain;
409 /* sanity checking, normalize values just in case (toplvl too) */
410 for (i=0; i<AMU_TYPE_MAX; ++i) {
411 if (gopt.amfs_auto_timeo[i] == 0)
412 gopt.amfs_auto_timeo[i] = AMFS_AUTO_TIMEO;
413 if (gopt.amfs_auto_retrans[i] == 0)
414 gopt.amfs_auto_retrans[i] = AMFS_AUTO_RETRANS(i);
415 if (gopt.amfs_auto_retrans[i] == 0)
416 gopt.amfs_auto_retrans[i] = 3; /* under very unusual circumstances, could be zero */
420 /* finally print version string and exit, if asked for */
422 fputs(get_version_string(), stderr);
426 if (switch_to_logfile(gopt.logfile, orig_umask,
427 (gopt.flags & CFM_TRUNCATE_LOG)) != 0)
428 plog(XLOG_USER, "Cannot switch logfile");