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