get_args.c revision 38494
1/*
2 * Copyright (c) 1997-1998 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.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry at Imperial College, London.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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 acknowledgement:
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.
26 *
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
37 * SUCH DAMAGE.
38 *
39 *      %W% (Berkeley) %G%
40 *
41 * $Id: get_args.c,v 5.2.2.1 1992/02/09 15:08:23 jsp beta $
42 *
43 */
44
45/*
46 * Argument decode
47 */
48
49#ifdef HAVE_CONFIG_H
50# include <config.h>
51#endif /* HAVE_CONFIG_H */
52#include <am_defs.h>
53#include <amd.h>
54
55/* include auto-generated version file */
56#include <build_version.h>
57
58char *conf_file = "/etc/amd.conf"; /* default amd configuration file */
59char *conf_tag = NULL;		/* default conf file tags to use */
60int usage = 0;
61int use_conf_file = 0;		/* default don't use amd.conf file */
62char *mnttab_file_name = NULL;	/* symbol must be available always */
63#ifdef DEBUG
64int debug_flags = D_AMQ		/* Register AMQ */
65		| D_DAEMON;	/* Enter daemon mode */
66#endif /* DEBUG */
67
68
69/*
70 * Return the version string (dynamic buffer)
71 */
72char *
73get_version_string(void)
74{
75  static char *vers = NULL;
76  char tmpbuf[1024];
77  char *wire_buf;
78  int wire_buf_len = 0;
79
80  /* first get dynamic string listing all known networks */
81  wire_buf = print_wires();
82  if (wire_buf)
83    wire_buf_len = strlen(wire_buf);
84
85  vers = xmalloc(2048 + wire_buf_len);
86  sprintf(vers, "%s\n%s\n%s\n%s\n",
87	  "Copyright (c) 1997-1998 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  sprintf(tmpbuf, "%s version %s (build %d).\n",
92	  PACKAGE, VERSION, AMU_BUILD_VERSION);
93  strcat(vers, tmpbuf);
94  sprintf(tmpbuf, "Built by %s@%s on date %s.\n",
95	  USER_NAME, HOST_NAME, CONFIG_DATE);
96  strcat(vers, tmpbuf);
97  sprintf(tmpbuf, "cpu=%s (%s-endian), arch=%s, karch=%s.\n",
98	  cpu, endian, gopt.arch, gopt.karch);
99  strcat(vers, tmpbuf);
100  sprintf(tmpbuf, "full_os=%s, os=%s, osver=%s, vendor=%s.\n",
101	  HOST_OS, gopt.op_sys, gopt.op_sys_ver, HOST_VENDOR);
102  strcat(vers, tmpbuf);
103
104  strcat(vers, "Map support for: ");
105  mapc_showtypes(tmpbuf);
106  strcat(vers, tmpbuf);
107  strcat(vers, ".\nAMFS: ");
108  ops_showamfstypes(tmpbuf);
109  strcat(vers, tmpbuf);
110  strcat(vers, ".\nFS: ");
111  ops_showfstypes(tmpbuf);
112  strcat(vers, tmpbuf);
113
114  /* append list of networks if available */
115  if (wire_buf) {
116    strcat(vers, wire_buf);
117    XFREE(wire_buf);
118  }
119
120  return vers;
121}
122
123
124void
125get_args(int argc, char *argv[])
126{
127  int opt_ch;
128  FILE *fp = stdin;
129
130  /* if no arguments were passed, try to use /etc/amd.conf file */
131  if (argc <= 1)
132    use_conf_file = 1;
133
134  while ((opt_ch = getopt(argc, argv, "nprvSa:c:d:k:l:o:t:w:x:y:C:D:F:T:O:H")) != EOF)
135    switch (opt_ch) {
136
137    case 'a':
138      if (*optarg != '/') {
139	fprintf(stderr, "%s: -a option must begin with a '/'\n",
140		progname);
141	exit(1);
142      }
143      gopt.auto_dir = optarg;
144      break;
145
146    case 'c':
147      gopt.am_timeo = atoi(optarg);
148      if (gopt.am_timeo <= 0)
149	gopt.am_timeo = AM_TTL;
150      break;
151
152    case 'd':
153      gopt.sub_domain = optarg;
154      break;
155
156    case 'k':
157      gopt.karch = optarg;
158      break;
159
160    case 'l':
161      gopt.logfile = optarg;
162      break;
163
164    case 'n':
165      gopt.flags |= CFM_NORMALIZE_HOSTNAMES;
166      break;
167
168    case 'o':
169      gopt.op_sys_ver = optarg;
170      break;
171
172    case 'p':
173     gopt.flags |= CFM_PRINT_PID;
174      break;
175
176    case 'r':
177      gopt.flags |= CFM_RESTART_EXISTING_MOUNTS;
178      break;
179
180    case 't':
181      /* timeo.retrans */
182      {
183	char *dot = strchr(optarg, '.');
184	if (dot)
185	  *dot = '\0';
186	if (*optarg) {
187	  gopt.amfs_auto_timeo = atoi(optarg);
188	}
189	if (dot) {
190	  gopt.amfs_auto_retrans = atoi(dot + 1);
191	  *dot = '.';
192	}
193      }
194      break;
195
196    case 'v':
197      fputs(get_version_string(), stderr);
198      exit(0);
199      break;
200
201    case 'w':
202      gopt.am_timeo_w = atoi(optarg);
203      if (gopt.am_timeo_w <= 0)
204	gopt.am_timeo_w = AM_TTL_W;
205      break;
206
207    case 'x':
208      usage += switch_option(optarg);
209      break;
210
211    case 'y':
212#ifdef HAVE_MAP_NIS
213      gopt.nis_domain = optarg;
214#else /* not HAVE_MAP_NIS */
215      plog(XLOG_USER, "-y: option ignored.  No NIS support available.");
216#endif /* not HAVE_MAP_NIS */
217      break;
218
219    case 'C':
220      gopt.cluster = optarg;
221      break;
222
223    case 'D':
224#ifdef DEBUG
225      usage += debug_option(optarg);
226#else /* not DEBUG */
227      fprintf(stderr, "%s: not compiled with DEBUG option -- sorry.\n", progname);
228#endif /* not DEBUG */
229      break;
230
231    case 'F':
232      conf_file = optarg;
233      use_conf_file = 1;
234      break;
235
236    case 'H':
237      goto show_usage;
238      break;
239
240    case 'O':
241      gopt.op_sys = optarg;
242      break;
243
244    case 'S':
245      gopt.flags &= ~CFM_PROCESS_LOCK; /* turn process locking off */
246      break;
247
248    case 'T':
249      conf_tag = optarg;
250      break;
251
252    default:
253      usage = 1;
254      break;
255    }
256
257  /*
258   * amd.conf file: if not command-line arguments were used, or if -F was
259   * specified, then use that amd.conf file.  If the file cannot be opened,
260   * abort amd.  If it can be found, open it, parse it, and then close it.
261   */
262  if (use_conf_file && conf_file) {
263    fp = fopen(conf_file, "r");
264    if (!fp) {
265      char buf[128];
266      sprintf(buf, "Amd configuration file (%s)", conf_file);
267      perror(buf);
268      exit(1);
269    }
270    yyin = fp;
271    yyparse();
272    fclose(fp);
273    if (process_last_regular_map() != 0)
274      exit(1);
275  }
276
277  /* make sure there are some default options defined */
278  if (xlog_level_init == ~0) {
279    switch_option("");
280  }
281#ifdef DEBUG
282  usage += switch_option("debug");
283#endif /* DEBUG */
284
285  /* log information regarding amd.conf file */
286  if (use_conf_file && conf_file)
287    plog(XLOG_INFO, "using configuration file %s", conf_file);
288
289#ifdef HAVE_MAP_LDAP
290  /* ensure that if ldap_base is specified, that also ldap_hostports is */
291  if (gopt.ldap_hostports && !gopt.ldap_base) {
292    fprintf(stderr, "must specify both ldap_hostports and ldap_base\n");
293    exit(1);
294  }
295#endif /* HAVE_MAP_LDAP */
296
297  if (usage)
298    goto show_usage;
299
300  while (optind <= argc - 2) {
301    char *dir = argv[optind++];
302    char *map = argv[optind++];
303    char *opts = "";
304    if (argv[optind] && *argv[optind] == '-')
305      opts = &argv[optind++][1];
306
307    root_newmap(dir, opts, map, NULL);
308  }
309
310  if (optind == argc) {
311    /*
312     * Append domain name to hostname.
313     * sub_domain overrides hostdomain
314     * if given.
315     */
316    if (gopt.sub_domain)
317      hostdomain = gopt.sub_domain;
318    if (*hostdomain == '.')
319      hostdomain++;
320    strcat(hostd, ".");
321    strcat(hostd, hostdomain);
322
323#ifdef MOUNT_TABLE_ON_FILE
324# ifdef DEBUG
325    if (debug_flags & D_MTAB)
326      mnttab_file_name = DEBUG_MNTTAB;
327    else
328# endif /* DEBUG */
329      mnttab_file_name = MNTTAB_FILE_NAME;
330#else /* not MOUNT_TABLE_ON_FILE */
331# ifdef DEBUG
332    if (debug_flags & D_MTAB)
333      dlog("-D mtab option ignored");
334# endif /* DEBUG */
335#endif /* not MOUNT_TABLE_ON_FILE */
336
337    if (switch_to_logfile(gopt.logfile) != 0)
338      plog(XLOG_USER, "Cannot switch logfile");
339
340    /*
341     * If the kernel architecture was not specified
342     * then use the machine architecture.
343     */
344    if (gopt.karch == 0)
345      gopt.karch = gopt.arch;
346
347    if (gopt.cluster == 0)
348      gopt.cluster = hostdomain;
349
350    if (gopt.amfs_auto_timeo <= 0)
351      gopt.amfs_auto_timeo = AMFS_AUTO_TIMEO;
352    if (gopt.amfs_auto_retrans <= 0)
353      gopt.amfs_auto_retrans = AMFS_AUTO_RETRANS;
354    if (gopt.amfs_auto_retrans <= 0)
355      gopt.amfs_auto_retrans = 3;	/* XXX */
356    return;
357  }
358
359show_usage:
360  fprintf(stderr,
361	  "Usage: %s [-nprvHS] [-a mount_point] [-c cache_time] [-d domain]\n\
362\t[-k kernel_arch] [-l logfile%s\n\
363\t[-t timeout.retrans] [-w wait_timeout] [-C cluster_name]\n\
364\t[-o op_sys_ver] [-O op_sys_name]\n\
365\t[-F conf_file] [-T conf_tag]", progname,
366#ifdef HAVE_SYSLOG
367# ifdef LOG_DAEMON
368	  "|\"syslog[:facility]\"]"
369# else /* not LOG_DAEMON */
370	  "|\"syslog\"]"
371# endif /* not LOG_DAEMON */
372#else /* not HAVE_SYSLOG */
373	  "]"
374#endif /* not HAVE_SYSLOG */
375	  );
376
377#ifdef HAVE_MAP_NIS
378  fputs(" [-y nis-domain]\n", stderr);
379#else /* not HAVE_MAP_NIS */
380  fputc('\n', stderr);
381#endif /* HAVE_MAP_NIS */
382
383  show_opts('x', xlog_opt);
384#ifdef DEBUG
385  show_opts('D', dbg_opt);
386#endif /* DEBUG */
387  fprintf(stderr, "\t[directory mapname [-map_options]] ...\n");
388  exit(1);
389}
390