]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/amd/amd/get_args.c
Update mandoc to 1.13.4 release
[FreeBSD/FreeBSD.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), "cpu=%s (%s-endian), arch=%s, karch=%s.\n",
97             cpu, endian, gopt.arch, gopt.karch);
98   strlcat(vers, tmpbuf, len);
99   xsnprintf(tmpbuf, sizeof(tmpbuf), "full_os=%s, os=%s, osver=%s, vendor=%s, distro=%s.\n",
100             gopt.op_sys_full, gopt.op_sys, gopt.op_sys_ver, gopt.op_sys_vendor, DISTRO_NAME);
101   strlcat(vers, tmpbuf, len);
102   xsnprintf(tmpbuf, sizeof(tmpbuf), "domain=%s, host=%s, hostd=%s.\n",
103             hostdomain, am_get_hostname(), hostd);
104   strlcat(vers, tmpbuf, len);
105
106   strlcat(vers, "Map support for: ", len);
107   mapc_showtypes(tmpbuf, sizeof(tmpbuf));
108   strlcat(vers, tmpbuf, len);
109   strlcat(vers, ".\nAMFS: ", len);
110   ops_showamfstypes(tmpbuf, sizeof(tmpbuf));
111   strlcat(vers, tmpbuf, len);
112   strlcat(vers, ", inherit.\nFS: ", len); /* hack: "show" that we support type:=inherit */
113   ops_showfstypes(tmpbuf, sizeof(tmpbuf));
114   strlcat(vers, tmpbuf, len);
115
116   /* append list of networks if available */
117   if (wire_buf) {
118     strlcat(vers, wire_buf, len);
119     XFREE(wire_buf);
120   }
121
122   return vers;
123 }
124
125
126 static void
127 show_usage(void)
128 {
129   fprintf(stderr,
130           "Usage: %s [-nprvHS] [-a mount_point] [-c cache_time] [-d domain]\n\
131 \t[-k kernel_arch] [-l logfile%s\n\
132 \t[-t timeout.retrans] [-w wait_timeout] [-A arch] [-C cluster_name]\n\
133 \t[-o op_sys_ver] [-O op_sys_name]\n\
134 \t[-F conf_file] [-T conf_tag]", am_get_progname(),
135 #ifdef HAVE_SYSLOG
136 # ifdef LOG_DAEMON
137           "|\"syslog[:facility]\"]"
138 # else /* not LOG_DAEMON */
139           "|\"syslog\"]"
140 # endif /* not LOG_DAEMON */
141 #else /* not HAVE_SYSLOG */
142           "]"
143 #endif /* not HAVE_SYSLOG */
144           );
145
146 #ifdef HAVE_MAP_NIS
147   fputs(" [-y nis-domain]\n", stderr);
148 #else /* not HAVE_MAP_NIS */
149   fputc('\n', stderr);
150 #endif /* HAVE_MAP_NIS */
151
152   show_opts('x', xlog_opt);
153 #ifdef DEBUG
154   show_opts('D', dbg_opt);
155 #endif /* DEBUG */
156   fprintf(stderr, "\t[directory mapname [-map_options]] ...\n");
157 }
158
159
160 void
161 get_args(int argc, char *argv[])
162 {
163   int opt_ch, i;
164   FILE *fp = stdin;
165   char getopt_arguments[] = "+nprvSa:c:d:k:l:o:t:w:x:y:C:D:F:T:O:HA:";
166   char *getopt_args;
167   int print_version = 0;        /* 1 means we should print version info */
168
169 #ifdef HAVE_GNU_GETOPT
170   getopt_args = getopt_arguments;
171 #else /* ! HAVE_GNU_GETOPT */
172   getopt_args = &getopt_arguments[1];
173 #endif /* HAVE_GNU_GETOPT */
174
175   /* if no arguments were passed, try to use /etc/amd.conf file */
176   if (argc <= 1)
177     use_conf_file = 1;
178
179   while ((opt_ch = getopt(argc, argv, getopt_args)) != -1)
180     switch (opt_ch) {
181
182     case 'a':
183       if (*optarg != '/') {
184         fprintf(stderr, "%s: -a option must begin with a '/'\n",
185                 am_get_progname());
186         exit(1);
187       }
188       gopt.auto_dir = optarg;
189       break;
190
191     case 'c':
192       gopt.am_timeo = atoi(optarg);
193       if (gopt.am_timeo <= 0)
194         gopt.am_timeo = AM_TTL;
195       break;
196
197     case 'd':
198       gopt.sub_domain = optarg;
199       break;
200
201     case 'k':
202       gopt.karch = optarg;
203       break;
204
205     case 'l':
206       gopt.logfile = optarg;
207       break;
208
209     case 'n':
210       gopt.flags |= CFM_NORMALIZE_HOSTNAMES;
211       break;
212
213     case 'o':
214       gopt.op_sys_ver = optarg;
215       break;
216
217     case 'p':
218      gopt.flags |= CFM_PRINT_PID;
219       break;
220
221     case 'r':
222       gopt.flags |= CFM_RESTART_EXISTING_MOUNTS;
223       break;
224
225     case 't':
226       /* timeo.retrans (also affects toplvl mounts) */
227       {
228         char *dot = strchr(optarg, '.');
229         int i;
230         if (dot)
231           *dot = '\0';
232         if (*optarg) {
233           for (i=0; i<AMU_TYPE_MAX; ++i)
234             gopt.amfs_auto_timeo[i] = atoi(optarg);
235         }
236         if (dot) {
237           for (i=0; i<AMU_TYPE_MAX; ++i)
238             gopt.amfs_auto_retrans[i] = atoi(dot + 1);
239           *dot = '.';
240         }
241       }
242       break;
243
244     case 'v':
245       /*
246        * defer to print version info after every variable had been
247        * initialized.
248        */
249       print_version++;
250       break;
251
252     case 'w':
253       gopt.am_timeo_w = atoi(optarg);
254       if (gopt.am_timeo_w <= 0)
255         gopt.am_timeo_w = AM_TTL_W;
256       break;
257
258     case 'x':
259       usage += switch_option(optarg);
260       break;
261
262     case 'y':
263 #ifdef HAVE_MAP_NIS
264       gopt.nis_domain = optarg;
265 #else /* not HAVE_MAP_NIS */
266       plog(XLOG_USER, "-y: option ignored.  No NIS support available.");
267 #endif /* not HAVE_MAP_NIS */
268       break;
269
270     case 'A':
271       gopt.arch = optarg;
272       break;
273
274     case 'C':
275       gopt.cluster = optarg;
276       break;
277
278     case 'D':
279 #ifdef DEBUG
280       usage += debug_option(optarg);
281 #else /* not DEBUG */
282       fprintf(stderr, "%s: not compiled with DEBUG option -- sorry.\n",
283               am_get_progname());
284 #endif /* not DEBUG */
285       break;
286
287     case 'F':
288       amu_conf_file = optarg;
289       use_conf_file = 1;
290       break;
291
292     case 'H':
293       show_usage();
294       exit(1);
295       break;
296
297     case 'O':
298       gopt.op_sys = optarg;
299       break;
300
301     case 'S':
302       gopt.flags &= ~CFM_PROCESS_LOCK; /* turn process locking off */
303       break;
304
305     case 'T':
306       conf_tag = optarg;
307       break;
308
309     default:
310       usage = 1;
311       break;
312     }
313
314   /*
315    * amd.conf file: if not command-line arguments were used, or if -F was
316    * specified, then use that amd.conf file.  If the file cannot be opened,
317    * abort amd.  If it can be found, open it, parse it, and then close it.
318    */
319   if (use_conf_file && amu_conf_file) {
320     fp = fopen(amu_conf_file, "r");
321     if (!fp) {
322       char buf[128];
323       xsnprintf(buf, sizeof(buf), "Amd configuration file (%s)",
324                 amu_conf_file);
325       perror(buf);
326       exit(1);
327     }
328     yyin = fp;
329     yyparse();
330     fclose(fp);
331     if (process_all_regular_maps() != 0)
332       exit(1);
333   }
334
335   /* make sure there are some default options defined */
336   if (xlog_level_init == ~0) {
337     switch_option("");
338   }
339 #ifdef DEBUG
340   usage += switch_option("debug");
341 #endif /* DEBUG */
342
343   /* log information regarding amd.conf file */
344   if (use_conf_file && amu_conf_file)
345     plog(XLOG_INFO, "using configuration file %s", amu_conf_file);
346
347 #ifdef HAVE_MAP_LDAP
348   /* ensure that if ldap_base is specified, that also ldap_hostports is */
349   if (gopt.ldap_hostports && !gopt.ldap_base) {
350     fprintf(stderr, "must specify both ldap_hostports and ldap_base\n");
351     exit(1);
352   }
353 #endif /* HAVE_MAP_LDAP */
354
355   if (usage) {
356     show_usage();
357     exit(1);
358   }
359
360   while (optind <= argc - 2) {
361     char *dir = argv[optind++];
362     char *map = argv[optind++];
363     char *opts = "";
364     if (argv[optind] && *argv[optind] == '-')
365       opts = &argv[optind++][1];
366
367     root_newmap(dir, opts, map, NULL);
368   }
369
370   if (optind == argc) {
371     /*
372      * Append domain name to hostname.
373      * sub_domain overrides hostdomain
374      * if given.
375      */
376     if (gopt.sub_domain)
377       hostdomain = gopt.sub_domain;
378     if (*hostdomain == '.')
379       hostdomain++;
380     xstrlcat(hostd, ".", sizeof(hostd));
381     xstrlcat(hostd, hostdomain, sizeof(hostd));
382
383 #ifdef MOUNT_TABLE_ON_FILE
384     if (amuDebug(D_MTAB))
385       if (gopt.debug_mtab_file)
386         mnttab_file_name = gopt.debug_mtab_file; /* user supplied debug mtab path */
387       else
388         mnttab_file_name = DEBUG_MNTTAB_FILE; /* default debug mtab path */
389     else
390       mnttab_file_name = MNTTAB_FILE_NAME;
391 #else /* not MOUNT_TABLE_ON_FILE */
392     if (amuDebug(D_MTAB))
393       dlog("-D mtab option ignored");
394 # ifdef MNTTAB_FILE_NAME
395     mnttab_file_name = MNTTAB_FILE_NAME;
396 # endif /* MNTTAB_FILE_NAME */
397 #endif /* not MOUNT_TABLE_ON_FILE */
398
399     /*
400      * If the kernel architecture was not specified
401      * then use the machine architecture.
402      */
403     if (gopt.karch == NULL)
404       gopt.karch = gopt.arch;
405
406     if (gopt.cluster == NULL)
407       gopt.cluster = hostdomain;
408
409     /* sanity checking, normalize values just in case (toplvl too) */
410     for (i=0; i<AMU_TYPE_MAX; ++i) {
411       if (gopt.amfs_auto_timeo[i] == 0)
412         gopt.amfs_auto_timeo[i] = AMFS_AUTO_TIMEO;
413       if (gopt.amfs_auto_retrans[i] == 0)
414         gopt.amfs_auto_retrans[i] = AMFS_AUTO_RETRANS(i);
415       if (gopt.amfs_auto_retrans[i] == 0)
416         gopt.amfs_auto_retrans[i] = 3;  /* under very unusual circumstances, could be zero */
417     }
418   }
419
420   /* finally print version string and exit, if asked for */
421   if (print_version) {
422     fputs(get_version_string(), stderr);
423     exit(0);
424   }
425
426   if (switch_to_logfile(gopt.logfile, orig_umask,
427                         (gopt.flags & CFM_TRUNCATE_LOG)) != 0)
428     plog(XLOG_USER, "Cannot switch logfile");
429
430   return;
431 }