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), "Configured by %s@%s on date %s.\n",
97 USER_NAME, HOST_NAME, CONFIG_DATE);
98 strlcat(vers, tmpbuf, len);
99 xsnprintf(tmpbuf, sizeof(tmpbuf), "Built by %s@%s.\n",
100 BUILD_USER, BUILD_HOST);
101 strlcat(vers, tmpbuf, len);
102 xsnprintf(tmpbuf, sizeof(tmpbuf), "cpu=%s (%s-endian), arch=%s, karch=%s.\n",
103 cpu, endian, gopt.arch, gopt.karch);
104 strlcat(vers, tmpbuf, len);
105 xsnprintf(tmpbuf, sizeof(tmpbuf), "full_os=%s, os=%s, osver=%s, vendor=%s, distro=%s.\n",
106 gopt.op_sys_full, gopt.op_sys, gopt.op_sys_ver, gopt.op_sys_vendor, DISTRO_NAME);
107 strlcat(vers, tmpbuf, len);
108 xsnprintf(tmpbuf, sizeof(tmpbuf), "domain=%s, host=%s, hostd=%s.\n",
109 hostdomain, am_get_hostname(), hostd);
110 strlcat(vers, tmpbuf, len);
112 strlcat(vers, "Map support for: ", len);
113 mapc_showtypes(tmpbuf, sizeof(tmpbuf));
114 strlcat(vers, tmpbuf, len);
115 strlcat(vers, ".\nAMFS: ", len);
116 ops_showamfstypes(tmpbuf, sizeof(tmpbuf));
117 strlcat(vers, tmpbuf, len);
118 strlcat(vers, ", inherit.\nFS: ", len); /* hack: "show" that we support type:=inherit */
119 ops_showfstypes(tmpbuf, sizeof(tmpbuf));
120 strlcat(vers, tmpbuf, len);
122 /* append list of networks if available */
124 strlcat(vers, wire_buf, len);
136 "Usage: %s [-nprvHS] [-a mount_point] [-c cache_time] [-d domain]\n\
137 \t[-k kernel_arch] [-l logfile%s\n\
138 \t[-t timeout.retrans] [-w wait_timeout] [-A arch] [-C cluster_name]\n\
139 \t[-o op_sys_ver] [-O op_sys_name]\n\
140 \t[-F conf_file] [-T conf_tag]", am_get_progname(),
143 "|\"syslog[:facility]\"]"
144 # else /* not LOG_DAEMON */
146 # endif /* not LOG_DAEMON */
147 #else /* not HAVE_SYSLOG */
149 #endif /* not HAVE_SYSLOG */
153 fputs(" [-y nis-domain]\n", stderr);
154 #else /* not HAVE_MAP_NIS */
156 #endif /* HAVE_MAP_NIS */
158 show_opts('x', xlog_opt);
160 show_opts('D', dbg_opt);
162 fprintf(stderr, "\t[directory mapname [-map_options]] ...\n");
167 get_args(int argc, char *argv[])
171 char getopt_arguments[] = "+nprvSa:c:d:k:l:o:t:w:x:y:C:D:F:T:O:HA:";
173 int print_version = 0; /* 1 means we should print version info */
175 #ifdef HAVE_GNU_GETOPT
176 getopt_args = getopt_arguments;
177 #else /* ! HAVE_GNU_GETOPT */
178 getopt_args = &getopt_arguments[1];
179 #endif /* HAVE_GNU_GETOPT */
181 /* if no arguments were passed, try to use /etc/amd.conf file */
185 while ((opt_ch = getopt(argc, argv, getopt_args)) != -1)
189 if (*optarg != '/') {
190 fprintf(stderr, "%s: -a option must begin with a '/'\n",
194 gopt.auto_dir = optarg;
198 gopt.am_timeo = atoi(optarg);
199 if (gopt.am_timeo <= 0)
200 gopt.am_timeo = AM_TTL;
204 gopt.sub_domain = optarg;
212 gopt.logfile = optarg;
216 gopt.flags |= CFM_NORMALIZE_HOSTNAMES;
220 gopt.op_sys_ver = optarg;
224 gopt.flags |= CFM_PRINT_PID;
228 gopt.flags |= CFM_RESTART_EXISTING_MOUNTS;
232 /* timeo.retrans (also affects toplvl mounts) */
234 char *dot = strchr(optarg, '.');
239 for (i=0; i<AMU_TYPE_MAX; ++i)
240 gopt.amfs_auto_timeo[i] = atoi(optarg);
243 for (i=0; i<AMU_TYPE_MAX; ++i)
244 gopt.amfs_auto_retrans[i] = atoi(dot + 1);
252 * defer to print version info after every variable had been
259 gopt.am_timeo_w = atoi(optarg);
260 if (gopt.am_timeo_w <= 0)
261 gopt.am_timeo_w = AM_TTL_W;
265 usage += switch_option(optarg);
270 gopt.nis_domain = optarg;
271 #else /* not HAVE_MAP_NIS */
272 plog(XLOG_USER, "-y: option ignored. No NIS support available.");
273 #endif /* not HAVE_MAP_NIS */
281 gopt.cluster = optarg;
286 usage += debug_option(optarg);
287 #else /* not DEBUG */
288 fprintf(stderr, "%s: not compiled with DEBUG option -- sorry.\n",
290 #endif /* not DEBUG */
294 amu_conf_file = optarg;
304 gopt.op_sys = optarg;
308 gopt.flags &= ~CFM_PROCESS_LOCK; /* turn process locking off */
321 * amd.conf file: if not command-line arguments were used, or if -F was
322 * specified, then use that amd.conf file. If the file cannot be opened,
323 * abort amd. If it can be found, open it, parse it, and then close it.
325 if (use_conf_file && amu_conf_file) {
326 fp = fopen(amu_conf_file, "r");
329 xsnprintf(buf, sizeof(buf), "Amd configuration file (%s)",
337 if (process_all_regular_maps() != 0)
341 /* make sure there are some default options defined */
342 if (xlog_level_init == ~0) {
346 usage += switch_option("debug");
349 /* log information regarding amd.conf file */
350 if (use_conf_file && amu_conf_file)
351 plog(XLOG_INFO, "using configuration file %s", amu_conf_file);
354 /* ensure that if ldap_base is specified, that also ldap_hostports is */
355 if (gopt.ldap_hostports && !gopt.ldap_base) {
356 fprintf(stderr, "must specify both ldap_hostports and ldap_base\n");
359 #endif /* HAVE_MAP_LDAP */
366 while (optind <= argc - 2) {
367 char *dir = argv[optind++];
368 char *map = argv[optind++];
370 if (argv[optind] && *argv[optind] == '-')
371 opts = &argv[optind++][1];
373 root_newmap(dir, opts, map, NULL);
376 if (optind == argc) {
378 * Append domain name to hostname.
379 * sub_domain overrides hostdomain
383 hostdomain = gopt.sub_domain;
384 if (*hostdomain == '.')
386 xstrlcat(hostd, ".", sizeof(hostd));
387 xstrlcat(hostd, hostdomain, sizeof(hostd));
389 #ifdef MOUNT_TABLE_ON_FILE
390 if (amuDebug(D_MTAB))
391 if (gopt.debug_mtab_file)
392 mnttab_file_name = gopt.debug_mtab_file; /* user supplied debug mtab path */
394 mnttab_file_name = DEBUG_MNTTAB_FILE; /* default debug mtab path */
396 mnttab_file_name = MNTTAB_FILE_NAME;
397 #else /* not MOUNT_TABLE_ON_FILE */
398 if (amuDebug(D_MTAB))
399 dlog("-D mtab option ignored");
400 # ifdef MNTTAB_FILE_NAME
401 mnttab_file_name = MNTTAB_FILE_NAME;
402 # endif /* MNTTAB_FILE_NAME */
403 #endif /* not MOUNT_TABLE_ON_FILE */
406 * If the kernel architecture was not specified
407 * then use the machine architecture.
409 if (gopt.karch == NULL)
410 gopt.karch = gopt.arch;
412 if (gopt.cluster == NULL)
413 gopt.cluster = hostdomain;
415 /* sanity checking, normalize values just in case (toplvl too) */
416 for (i=0; i<AMU_TYPE_MAX; ++i) {
417 if (gopt.amfs_auto_timeo[i] == 0)
418 gopt.amfs_auto_timeo[i] = AMFS_AUTO_TIMEO;
419 if (gopt.amfs_auto_retrans[i] == 0)
420 gopt.amfs_auto_retrans[i] = AMFS_AUTO_RETRANS(i);
421 if (gopt.amfs_auto_retrans[i] == 0)
422 gopt.amfs_auto_retrans[i] = 3; /* under very unusual circumstances, could be zero */
426 /* finally print version string and exit, if asked for */
428 fputs(get_version_string(), stderr);
432 if (switch_to_logfile(gopt.logfile, orig_umask,
433 (gopt.flags & CFM_TRUNCATE_LOG)) != 0)
434 plog(XLOG_USER, "Cannot switch logfile");