2 * Copyright (c) 1997-2014 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. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * File: am-utils/amd/get_args.c
46 #endif /* HAVE_CONFIG_H */
50 /* include auto-generated version file */
51 #include <build_version.h>
53 char *amu_conf_file = "/etc/amd.conf"; /* default amd configuration file */
54 char *conf_tag = NULL; /* default conf file tags to use */
56 int use_conf_file = 0; /* default don't use amd.conf file */
57 char *mnttab_file_name = NULL; /* symbol must be available always */
61 * Return the version string (dynamic buffer)
64 get_version_string(void)
70 size_t len; /* max allocated length (to avoid buf overflow) */
73 * First get dynamic string listing all known networks.
74 * This could be a long list, if host has lots of interfaces.
76 wire_buf = print_wires();
78 wire_buf_len = strlen(wire_buf);
80 len = 2048 + wire_buf_len;
82 xsnprintf(vers, len, "%s\n%s\n%s\n%s\n",
83 "Copyright (c) 1997-2014 Erez Zadok",
84 "Copyright (c) 1990 Jan-Simon Pendry",
85 "Copyright (c) 1990 Imperial College of Science, Technology & Medicine",
86 "Copyright (c) 1990 The Regents of the University of California.");
87 xsnprintf(tmpbuf, sizeof(tmpbuf), "%s version %s (build %d).\n",
88 PACKAGE_NAME, PACKAGE_VERSION, AMU_BUILD_VERSION);
89 xstrlcat(vers, tmpbuf, len);
90 xsnprintf(tmpbuf, sizeof(tmpbuf), "Report bugs to %s.\n", PACKAGE_BUGREPORT);
91 xstrlcat(vers, tmpbuf, len);
94 * XXX This block (between from the #if 0 to #endif was in the
95 * XXX original was in the original merge however in the interest
96 * XXX of reproduceable builds and the fact that this is redundant
97 * XXX information, it is effectively removed.
99 xsnprintf(tmpbuf, sizeof(tmpbuf), "Configured by %s@%s on date %s.\n",
100 USER_NAME, HOST_NAME, CONFIG_DATE);
101 xstrlcat(vers, tmpbuf, len);
102 xsnprintf(tmpbuf, sizeof(tmpbuf), "Built by %s@%s on date %s.\n",
103 BUILD_USER, BUILD_HOST, BUILD_DATE);
104 xstrlcat(vers, tmpbuf, len);
106 xsnprintf(tmpbuf, sizeof(tmpbuf), "cpu=%s (%s-endian), arch=%s, karch=%s.\n",
107 cpu, endian, gopt.arch, gopt.karch);
108 xstrlcat(vers, tmpbuf, len);
109 xsnprintf(tmpbuf, sizeof(tmpbuf), "full_os=%s, os=%s, osver=%s, vendor=%s, distro=%s.\n",
110 gopt.op_sys_full, gopt.op_sys, gopt.op_sys_ver, gopt.op_sys_vendor, DISTRO_NAME);
111 xstrlcat(vers, tmpbuf, len);
112 xsnprintf(tmpbuf, sizeof(tmpbuf), "domain=%s, host=%s, hostd=%s.\n",
113 hostdomain, am_get_hostname(), hostd);
114 xstrlcat(vers, tmpbuf, len);
116 xstrlcat(vers, "Map support for: ", len);
117 mapc_showtypes(tmpbuf, sizeof(tmpbuf));
118 xstrlcat(vers, tmpbuf, len);
119 xstrlcat(vers, ".\nAMFS: ", len);
120 ops_showamfstypes(tmpbuf, sizeof(tmpbuf));
121 xstrlcat(vers, tmpbuf, len);
122 xstrlcat(vers, ", inherit.\nFS: ", len); /* hack: "show" that we support type:=inherit */
123 ops_showfstypes(tmpbuf, sizeof(tmpbuf));
124 xstrlcat(vers, tmpbuf, len);
126 /* append list of networks if available */
128 xstrlcat(vers, wire_buf, len);
140 "Usage: %s [-nprvHS] [-a mount_point] [-c cache_time] [-d domain]\n\
141 \t[-k kernel_arch] [-l logfile%s\n\
142 \t[-t timeout.retrans] [-w wait_timeout] [-A arch] [-C cluster_name]\n\
143 \t[-o op_sys_ver] [-O op_sys_name]\n\
144 \t[-F conf_file] [-T conf_tag]", am_get_progname(),
147 "|\"syslog[:facility]\"]"
148 # else /* not LOG_DAEMON */
150 # endif /* not LOG_DAEMON */
151 #else /* not HAVE_SYSLOG */
153 #endif /* not HAVE_SYSLOG */
157 fputs(" [-y nis-domain]\n", stderr);
158 #else /* not HAVE_MAP_NIS */
160 #endif /* HAVE_MAP_NIS */
162 show_opts('x', xlog_opt);
164 show_opts('D', dbg_opt);
166 fprintf(stderr, "\t[directory mapname [-map_options]] ...\n");
171 get_args(int argc, char *argv[])
175 char getopt_arguments[] = "+nprvSa:c:d:k:l:o:t:w:x:y:C:D:F:T:O:HA:";
177 int print_version = 0; /* 1 means we should print version info */
179 #ifdef HAVE_GNU_GETOPT
180 getopt_args = getopt_arguments;
181 #else /* ! HAVE_GNU_GETOPT */
182 getopt_args = &getopt_arguments[1];
183 #endif /* HAVE_GNU_GETOPT */
185 /* if no arguments were passed, try to use /etc/amd.conf file */
189 while ((opt_ch = getopt(argc, argv, getopt_args)) != -1)
193 if (*optarg != '/') {
194 fprintf(stderr, "%s: -a option must begin with a '/'\n",
198 gopt.auto_dir = optarg;
202 gopt.am_timeo = atoi(optarg);
203 if (gopt.am_timeo <= 0)
204 gopt.am_timeo = AM_TTL;
208 gopt.sub_domain = optarg;
216 gopt.logfile = optarg;
220 gopt.flags |= CFM_NORMALIZE_HOSTNAMES;
224 gopt.op_sys_ver = optarg;
228 gopt.flags |= CFM_PRINT_PID;
232 gopt.flags |= CFM_RESTART_EXISTING_MOUNTS;
236 /* timeo.retrans (also affects toplvl mounts) */
238 char *dot = strchr(optarg, '.');
243 for (i=0; i<AMU_TYPE_MAX; ++i)
244 gopt.amfs_auto_timeo[i] = atoi(optarg);
247 for (i=0; i<AMU_TYPE_MAX; ++i)
248 gopt.amfs_auto_retrans[i] = atoi(dot + 1);
256 * defer to print version info after every variable had been
263 gopt.am_timeo_w = atoi(optarg);
264 if (gopt.am_timeo_w <= 0)
265 gopt.am_timeo_w = AM_TTL_W;
269 usage += switch_option(optarg);
274 gopt.nis_domain = optarg;
275 #else /* not HAVE_MAP_NIS */
276 plog(XLOG_USER, "-y: option ignored. No NIS support available.");
277 #endif /* not HAVE_MAP_NIS */
285 gopt.cluster = optarg;
290 usage += debug_option(optarg);
291 #else /* not DEBUG */
292 fprintf(stderr, "%s: not compiled with DEBUG option -- sorry.\n",
294 #endif /* not DEBUG */
298 amu_conf_file = optarg;
308 gopt.op_sys = optarg;
312 gopt.flags &= ~CFM_PROCESS_LOCK; /* turn process locking off */
325 * amd.conf file: if not command-line arguments were used, or if -F was
326 * specified, then use that amd.conf file. If the file cannot be opened,
327 * abort amd. If it can be found, open it, parse it, and then close it.
329 if (use_conf_file && amu_conf_file) {
330 fp = fopen(amu_conf_file, "r");
333 xsnprintf(buf, sizeof(buf), "Amd configuration file (%s)",
341 if (process_all_regular_maps() != 0)
346 usage += switch_option("debug");
347 /* initialize debug options */
349 debug_flags = D_CONTROL; /* CONTROL = "daemon,amq,fork" */
352 /* log information regarding amd.conf file */
353 if (use_conf_file && amu_conf_file)
354 plog(XLOG_INFO, "using configuration file %s", amu_conf_file);
357 /* ensure that if ldap_base is specified, that also ldap_hostports is */
358 if (gopt.ldap_hostports && !gopt.ldap_base) {
359 fprintf(stderr, "must specify both ldap_hostports and ldap_base\n");
362 #endif /* HAVE_MAP_LDAP */
369 while (optind <= argc - 2) {
370 char *dir = argv[optind++];
371 char *map = argv[optind++];
373 if (argv[optind] && *argv[optind] == '-')
374 opts = &argv[optind++][1];
376 root_newmap(dir, opts, map, NULL);
379 if (optind == argc) {
381 * Append domain name to hostname.
382 * sub_domain overrides hostdomain
386 hostdomain = gopt.sub_domain;
387 if (*hostdomain == '.')
389 xstrlcat(hostd, ".", sizeof(hostd));
390 xstrlcat(hostd, hostdomain, sizeof(hostd));
392 #ifdef MOUNT_TABLE_ON_FILE
393 if (amuDebug(D_MTAB))
394 if (gopt.debug_mtab_file)
395 mnttab_file_name = gopt.debug_mtab_file; /* user supplied debug mtab path */
397 mnttab_file_name = DEBUG_MNTTAB_FILE; /* default debug mtab path */
399 mnttab_file_name = MNTTAB_FILE_NAME;
400 #else /* not MOUNT_TABLE_ON_FILE */
401 if (amuDebug(D_MTAB))
402 dlog("-D mtab option ignored");
403 # ifdef MNTTAB_FILE_NAME
404 mnttab_file_name = MNTTAB_FILE_NAME;
405 # endif /* MNTTAB_FILE_NAME */
406 #endif /* not MOUNT_TABLE_ON_FILE */
409 * If the kernel architecture was not specified
410 * then use the machine architecture.
412 if (gopt.karch == NULL)
413 gopt.karch = gopt.arch;
415 if (gopt.cluster == NULL)
416 gopt.cluster = hostdomain;
418 /* sanity checking, normalize values just in case (toplvl too) */
419 for (i=0; i<AMU_TYPE_MAX; ++i) {
420 if (gopt.amfs_auto_timeo[i] == 0)
421 gopt.amfs_auto_timeo[i] = AMFS_AUTO_TIMEO;
422 if (gopt.amfs_auto_retrans[i] == 0)
423 gopt.amfs_auto_retrans[i] = AMFS_AUTO_RETRANS(i);
424 if (gopt.amfs_auto_retrans[i] == 0)
425 gopt.amfs_auto_retrans[i] = 3; /* under very unusual circumstances, could be zero */
429 /* finally print version string and exit, if asked for */
431 fputs(get_version_string(), stderr);
435 if (switch_to_logfile(gopt.logfile, orig_umask,
436 (gopt.flags & CFM_TRUNCATE_LOG)) != 0)
437 plog(XLOG_USER, "Cannot switch logfile");