2 * cmd_args.c = command-line argument processing
9 #include "ntp_stdlib.h"
10 #include "ntp_cmdargs.h"
17 * Definitions of things either imported from or exported to outside
19 extern char const *progname;
20 int listen_to_virtual_ips = 1;
23 extern BOOL NoWinService;
26 static const char *ntp_options = "aAbB:c:C:dD:f:gi:k:l:LmnNO:p:P:qr:s:S:t:T:W:u:v:V:xY:Z:-:";
29 extern int check_netinfo;
34 * getstartup - search through the options looking for a debugging flag
43 extern int priority_done;
47 debug = 0; /* no debugging by default */
51 * This is a big hack. We don't really want to read command line
52 * configuration until everything else is initialized, since
53 * the ability to configure the system may depend on storage
54 * and the like having been initialized. Except that we also
55 * don't want to initialize anything until after detaching from
56 * the terminal, but we won't know to do that until we've
57 * parsed the command line. Do that now, crudely, and do it
58 * again later. Our ntp_getopt() is explicitly reusable, by the
59 * way. Your own mileage may vary.
61 * This hack is even called twice (to allow complete logging to file)
67 * Decode argument list
69 while ((c = ntp_getopt(argc, argv, ntp_options)) != EOF)
76 debug = (int)atol(ntp_optarg);
77 printf("Debug1: %s -> %x = %d\n", ntp_optarg, debug, debug);
82 msyslog(LOG_ERR, "ntpd not compiled with -DDEBUG option - no DEBUG support");
83 fprintf(stderr, "ntpd not compiled with -DDEBUG option - no DEBUG support\n");
88 listen_to_virtual_ips = 0;
94 if(strcmp(ntp_optarg, "stderr") == 0)
96 else if(strcmp(ntp_optarg, "stdout") == 0)
99 new_file = fopen(ntp_optarg, "a");
100 if (new_file != NULL) {
102 msyslog(LOG_NOTICE, "logging to file %s", ntp_optarg);
103 if (syslog_file != NULL &&
104 fileno(syslog_file) != fileno(new_file))
105 (void)fclose(syslog_file);
107 syslog_file = new_file;
112 "Cannot open log file %s",
134 if ( ! strcmp(ntp_optarg, "version") ) {
135 printf("%.80s: %.80s\n", progname, Version);
137 } else if ( ! strcmp(ntp_optarg, "help") ) {
141 } else if ( ! strcmp(ntp_optarg, "copyright") ) {
145 fprintf(stderr, "%.80s: Error unknown argument '--%.80s'\n",
156 if (errflg || ntp_optind != argc) {
157 (void) fprintf(stderr, "usage: %s [ -abdgmnqx ] [ -c config_file ] [ -e e_delay ]\n", progname);
158 (void) fprintf(stderr, "\t\t[ -f freq_file ] [ -k key_file ] [ -l log_file ]\n");
159 (void) fprintf(stderr, "\t\t[ -p pid_file ] [ -r broad_delay ] [ -s statdir ]\n");
160 (void) fprintf(stderr, "\t\t[ -t trust_key ] [ -v sys_var ] [ -V default_sysvar ]\n");
161 #if defined(HAVE_SCHED_SETSCHEDULER)
162 (void) fprintf(stderr, "\t\t[ -P fixed_process_priority ]\n");
165 (void) fprintf(stderr, "\t\t[ -u user[:group] ] [ -i chrootdir ]\n");
169 ntp_optind = 0; /* reset ntp_optind to restart ntp_getopt */
174 static char buf[BUFSIZ];
175 setvbuf(stdout, buf, _IOLBF, BUFSIZ);
184 * getCmdOpts - get command line options
192 extern char *config_file;
193 struct sockaddr_in inaddrntp;
198 * Initialize, initialize
208 * Decode argument list
210 while ((c = ntp_getopt(argc, argv, ntp_options)) != EOF) {
213 proto_config(PROTO_AUTHENTICATE, 1, 0., NULL);
217 proto_config(PROTO_AUTHENTICATE, 0, 0., NULL);
221 proto_config(PROTO_BROADCLIENT, 1, 0., NULL);
225 config_file = ntp_optarg;
241 debug = (int)atol(ntp_optarg);
242 printf("Debug2: %s -> %x = %d\n", ntp_optarg, debug, debug);
249 stats_config(STATS_FREQ_FILE, ntp_optarg);
261 chrootdir = ntp_optarg;
267 getauthkeys(ntp_optarg);
270 case 'L': /* already done at pre-scan */
271 case 'l': /* already done at pre-scan */
275 inaddrntp.sin_family = AF_INET;
276 inaddrntp.sin_port = htons(NTP_PORT);
277 inaddrntp.sin_addr.s_addr = htonl(INADDR_NTP);
278 proto_config(PROTO_MULTICAST_ADD, 0, 0., (struct sockaddr_storage*)&inaddrntp);
282 case 'n': /* already done at pre-scan */
285 case 'N': /* already done at pre-scan */
289 stats_config(STATS_PID_FILE, ntp_optarg);
293 #if defined(HAVE_SCHED_SETSCHEDULER)
294 config_priority = (int)atol(ntp_optarg);
295 config_priority_override = 1;
309 if (sscanf(ntp_optarg, "%lf", &tmp) != 1) {
311 "command line broadcast delay value %s undecodable",
314 proto_config(PROTO_BROADDELAY, 0, tmp, NULL);
321 user = malloc(strlen(ntp_optarg) + 1);
322 if ((user == NULL) || (ntp_optarg == NULL))
324 (void)strncpy(user, ntp_optarg, strlen(ntp_optarg) + 1);
325 group = rindex(user, ':');
327 *group++ = '\0'; /* get rid of the ':' */
333 stats_config(STATS_STATSDIR, ntp_optarg);
340 tkey = (int)atol(ntp_optarg);
341 if (tkey <= 0 || tkey > NTP_MAXKEY) {
343 "command line trusted key %s is invalid",
353 set_sys_var(ntp_optarg, strlen(ntp_optarg)+1,
354 (u_short) (RW | ((c == 'V') ? DEF : 0)));
362 sscanf(ntp_optarg, "%lf", &ntp_node.bdly);
366 sscanf(ntp_optarg, "%lf", &ntp_node.snse);
370 sscanf(ntp_optarg, "%lf", &ntp_node.slew);
374 sscanf(ntp_optarg, "%lf", &ntp_node.clk_time);
378 sscanf(ntp_optarg, "%lf", &ntp_node.sim_time);
382 sscanf(ntp_optarg, "%lf", &ntp_node.ferr);
386 sscanf(ntp_optarg, "%lf", &ntp_node.fnse);
390 sscanf(ntp_optarg, "%lf", &ntp_node.ndly);
394 sscanf(ntp_optarg, "%lf", &ntp_node.pdly);
404 if (errflg || ntp_optind != argc) {
405 (void) fprintf(stderr, "usage: %s [ -abdgmnx ] [ -c config_file ] [ -e e_delay ]\n", progname);
406 (void) fprintf(stderr, "\t\t[ -f freq_file ] [ -k key_file ] [ -l log_file ]\n");
407 (void) fprintf(stderr, "\t\t[ -p pid_file ] [ -r broad_delay ] [ -s statdir ]\n");
408 (void) fprintf(stderr, "\t\t[ -t trust_key ] [ -v sys_var ] [ -V default_sysvar ]\n");
409 #if defined(HAVE_SCHED_SETSCHEDULER)
410 (void) fprintf(stderr, "\t\t[ -P fixed_process_priority ]\n");
413 (void) fprintf(stderr, "\t\t[ -u user[:group] ] [ -i chrootdir ]\n");