]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/amd/amd/get_args.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / amd / amd / get_args.c
1 /*
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.
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  *
40  * File: am-utils/amd/get_args.c
41  *
42  */
43
44 /*
45  * Argument decode
46  */
47
48 #ifdef HAVE_CONFIG_H
49 # include <config.h>
50 #endif /* HAVE_CONFIG_H */
51 #include <am_defs.h>
52 #include <amd.h>
53
54 /* include auto-generated version file */
55 #include <build_version.h>
56
57 char *amu_conf_file = "/etc/amd.conf"; /* default amd configuration file */
58 char *conf_tag = NULL;          /* default conf file tags to use */
59 int usage = 0;
60 int use_conf_file = 0;          /* default don't use amd.conf file */
61 char *mnttab_file_name = NULL;  /* symbol must be available always */
62
63
64 /*
65  * Return the version string (dynamic buffer)
66  */
67 char *
68 get_version_string(void)
69 {
70   char *vers = NULL;
71   char tmpbuf[1024];
72   char *wire_buf;
73   int wire_buf_len = 0;
74   size_t len;             /* max allocated length (to avoid buf overflow) */
75
76   /*
77    * First get dynamic string listing all known networks.
78    * This could be a long list, if host has lots of interfaces.
79    */
80   wire_buf = print_wires();
81   if (wire_buf)
82     wire_buf_len = strlen(wire_buf);
83
84   len = 2048 + wire_buf_len;
85   vers = xmalloc(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);
111
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);
121
122   /* append list of networks if available */
123   if (wire_buf) {
124     strlcat(vers, wire_buf, len);
125     XFREE(wire_buf);
126   }
127
128   return vers;
129 }
130
131
132 static void
133 show_usage(void)
134 {
135   fprintf(stderr,
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(),
141 #ifdef HAVE_SYSLOG
142 # ifdef LOG_DAEMON
143           "|\"syslog[:facility]\"]"
144 # else /* not LOG_DAEMON */
145           "|\"syslog\"]"
146 # endif /* not LOG_DAEMON */
147 #else /* not HAVE_SYSLOG */
148           "]"
149 #endif /* not HAVE_SYSLOG */
150           );
151
152 #ifdef HAVE_MAP_NIS
153   fputs(" [-y nis-domain]\n", stderr);
154 #else /* not HAVE_MAP_NIS */
155   fputc('\n', stderr);
156 #endif /* HAVE_MAP_NIS */
157
158   show_opts('x', xlog_opt);
159 #ifdef DEBUG
160   show_opts('D', dbg_opt);
161 #endif /* DEBUG */
162   fprintf(stderr, "\t[directory mapname [-map_options]] ...\n");
163 }
164
165
166 void
167 get_args(int argc, char *argv[])
168 {
169   int opt_ch, i;
170   FILE *fp = stdin;
171   char getopt_arguments[] = "+nprvSa:c:d:k:l:o:t:w:x:y:C:D:F:T:O:HA:";
172   char *getopt_args;
173   int print_version = 0;        /* 1 means we should print version info */
174
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 */
180
181   /* if no arguments were passed, try to use /etc/amd.conf file */
182   if (argc <= 1)
183     use_conf_file = 1;
184
185   while ((opt_ch = getopt(argc, argv, getopt_args)) != -1)
186     switch (opt_ch) {
187
188     case 'a':
189       if (*optarg != '/') {
190         fprintf(stderr, "%s: -a option must begin with a '/'\n",
191                 am_get_progname());
192         exit(1);
193       }
194       gopt.auto_dir = optarg;
195       break;
196
197     case 'c':
198       gopt.am_timeo = atoi(optarg);
199       if (gopt.am_timeo <= 0)
200         gopt.am_timeo = AM_TTL;
201       break;
202
203     case 'd':
204       gopt.sub_domain = optarg;
205       break;
206
207     case 'k':
208       gopt.karch = optarg;
209       break;
210
211     case 'l':
212       gopt.logfile = optarg;
213       break;
214
215     case 'n':
216       gopt.flags |= CFM_NORMALIZE_HOSTNAMES;
217       break;
218
219     case 'o':
220       gopt.op_sys_ver = optarg;
221       break;
222
223     case 'p':
224      gopt.flags |= CFM_PRINT_PID;
225       break;
226
227     case 'r':
228       gopt.flags |= CFM_RESTART_EXISTING_MOUNTS;
229       break;
230
231     case 't':
232       /* timeo.retrans (also affects toplvl mounts) */
233       {
234         char *dot = strchr(optarg, '.');
235         int i;
236         if (dot)
237           *dot = '\0';
238         if (*optarg) {
239           for (i=0; i<AMU_TYPE_MAX; ++i)
240             gopt.amfs_auto_timeo[i] = atoi(optarg);
241         }
242         if (dot) {
243           for (i=0; i<AMU_TYPE_MAX; ++i)
244             gopt.amfs_auto_retrans[i] = atoi(dot + 1);
245           *dot = '.';
246         }
247       }
248       break;
249
250     case 'v':
251       /*
252        * defer to print version info after every variable had been
253        * initialized.
254        */
255       print_version++;
256       break;
257
258     case 'w':
259       gopt.am_timeo_w = atoi(optarg);
260       if (gopt.am_timeo_w <= 0)
261         gopt.am_timeo_w = AM_TTL_W;
262       break;
263
264     case 'x':
265       usage += switch_option(optarg);
266       break;
267
268     case 'y':
269 #ifdef HAVE_MAP_NIS
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 */
274       break;
275
276     case 'A':
277       gopt.arch = optarg;
278       break;
279
280     case 'C':
281       gopt.cluster = optarg;
282       break;
283
284     case 'D':
285 #ifdef DEBUG
286       usage += debug_option(optarg);
287 #else /* not DEBUG */
288       fprintf(stderr, "%s: not compiled with DEBUG option -- sorry.\n",
289               am_get_progname());
290 #endif /* not DEBUG */
291       break;
292
293     case 'F':
294       amu_conf_file = optarg;
295       use_conf_file = 1;
296       break;
297
298     case 'H':
299       show_usage();
300       exit(1);
301       break;
302
303     case 'O':
304       gopt.op_sys = optarg;
305       break;
306
307     case 'S':
308       gopt.flags &= ~CFM_PROCESS_LOCK; /* turn process locking off */
309       break;
310
311     case 'T':
312       conf_tag = optarg;
313       break;
314
315     default:
316       usage = 1;
317       break;
318     }
319
320   /*
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.
324    */
325   if (use_conf_file && amu_conf_file) {
326     fp = fopen(amu_conf_file, "r");
327     if (!fp) {
328       char buf[128];
329       xsnprintf(buf, sizeof(buf), "Amd configuration file (%s)",
330                 amu_conf_file);
331       perror(buf);
332       exit(1);
333     }
334     yyin = fp;
335     yyparse();
336     fclose(fp);
337     if (process_all_regular_maps() != 0)
338       exit(1);
339   }
340
341   /* make sure there are some default options defined */
342   if (xlog_level_init == ~0) {
343     switch_option("");
344   }
345 #ifdef DEBUG
346   usage += switch_option("debug");
347 #endif /* DEBUG */
348
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);
352
353 #ifdef HAVE_MAP_LDAP
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");
357     exit(1);
358   }
359 #endif /* HAVE_MAP_LDAP */
360
361   if (usage) {
362     show_usage();
363     exit(1);
364   }
365
366   while (optind <= argc - 2) {
367     char *dir = argv[optind++];
368     char *map = argv[optind++];
369     char *opts = "";
370     if (argv[optind] && *argv[optind] == '-')
371       opts = &argv[optind++][1];
372
373     root_newmap(dir, opts, map, NULL);
374   }
375
376   if (optind == argc) {
377     /*
378      * Append domain name to hostname.
379      * sub_domain overrides hostdomain
380      * if given.
381      */
382     if (gopt.sub_domain)
383       hostdomain = gopt.sub_domain;
384     if (*hostdomain == '.')
385       hostdomain++;
386     xstrlcat(hostd, ".", sizeof(hostd));
387     xstrlcat(hostd, hostdomain, sizeof(hostd));
388
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 */
393       else
394         mnttab_file_name = DEBUG_MNTTAB_FILE; /* default debug mtab path */
395     else
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 */
404
405     /*
406      * If the kernel architecture was not specified
407      * then use the machine architecture.
408      */
409     if (gopt.karch == NULL)
410       gopt.karch = gopt.arch;
411
412     if (gopt.cluster == NULL)
413       gopt.cluster = hostdomain;
414
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 */
423     }
424   }
425
426   /* finally print version string and exit, if asked for */
427   if (print_version) {
428     fputs(get_version_string(), stderr);
429     exit(0);
430   }
431
432   if (switch_to_logfile(gopt.logfile, orig_umask,
433                         (gopt.flags & CFM_TRUNCATE_LOG)) != 0)
434     plog(XLOG_USER, "Cannot switch logfile");
435
436   return;
437 }