xref: /freebsd/contrib/ntp/ntpd/cmd_args.c (revision 6fd05b64b5b65dd4ba9b86482a0634a5f0b96c29)
1 /*
2  * cmd_args.c = command-line argument processing
3  */
4 #ifdef HAVE_CONFIG_H
5 # include <config.h>
6 #endif
7 
8 #include "ntpd.h"
9 #include "ntp_stdlib.h"
10 #include "ntp_cmdargs.h"
11 
12 /*
13  * Definitions of things either imported from or exported to outside
14  */
15 extern char const *progname;
16 int	listen_to_virtual_ips = 0;
17 
18 static const char *ntp_options = "aAbc:dD:f:gk:l:LmnN:p:P:qr:s:t:v:V:x";
19 
20 #ifdef HAVE_NETINFO
21 extern int	check_netinfo;
22 #endif
23 
24 
25 /*
26  * getstartup - search through the options looking for a debugging flag
27  */
28 void
29 getstartup(
30 	int argc,
31 	char *argv[]
32 	)
33 {
34 	int errflg;
35 	extern int priority_done;
36 	int c;
37 
38 #ifdef DEBUG
39 	debug = 0;		/* no debugging by default */
40 #endif
41 
42 	/*
43 	 * This is a big hack.	We don't really want to read command line
44 	 * configuration until everything else is initialized, since
45 	 * the ability to configure the system may depend on storage
46 	 * and the like having been initialized.  Except that we also
47 	 * don't want to initialize anything until after detaching from
48 	 * the terminal, but we won't know to do that until we've
49 	 * parsed the command line.  Do that now, crudely, and do it
50 	 * again later.  Our ntp_getopt() is explicitly reusable, by the
51 	 * way.  Your own mileage may vary.
52 	 *
53 	 * This hack is even called twice (to allow complete logging to file)
54 	 */
55 	errflg = 0;
56 	progname = argv[0];
57 
58 	/*
59 	 * Decode argument list
60 	 */
61 	while ((c = ntp_getopt(argc, argv, ntp_options)) != EOF)
62 	    switch (c) {
63 #ifdef DEBUG
64 		case 'd':
65 		    ++debug;
66 		    break;
67 		case 'D':
68 		    debug = (int)atol(ntp_optarg);
69 		    printf("Debug1: %s -> %x = %d\n", ntp_optarg, debug, debug);
70 		    break;
71 #else
72 		case 'd':
73 		case 'D':
74 		    msyslog(LOG_ERR, "ntpd not compiled with -DDEBUG option - no DEBUG support");
75 		    fprintf(stderr, "ntpd not compiled with -DDEBUG option - no DEBUG support");
76 		    ++errflg;
77 		    break;
78 #endif
79 		case 'L':
80 		    listen_to_virtual_ips = 1;
81 		    break;
82 		case 'l':
83 			{
84 				FILE *new_file;
85 
86 				new_file = fopen(ntp_optarg, "a");
87 				if (new_file != NULL) {
88 					NLOG(NLOG_SYSINFO)
89 						msyslog(LOG_NOTICE, "logging to file %s", ntp_optarg);
90 					if (syslog_file != NULL &&
91 						fileno(syslog_file) != fileno(new_file))
92 						(void)fclose(syslog_file);
93 
94 					syslog_file = new_file;
95 					syslogit = 0;
96 				}
97 				else
98 					msyslog(LOG_ERR,
99 						"Cannot open log file %s",
100 						ntp_optarg);
101 			}
102 			break;
103 
104 		case 'n':
105 		case 'q':
106 		    ++nofork;
107 		    break;
108 
109 		case 'N':
110 		    priority_done = strcmp(ntp_optarg, "high");
111 		    break;
112 
113 		case '?':
114 		    ++errflg;
115 		    break;
116 
117 		default:
118 			break;
119 		}
120 
121 	if (errflg || ntp_optind != argc) {
122 		(void) fprintf(stderr, "usage: %s [ -abdgmnqx ] [ -c config_file ] [ -e e_delay ]\n", progname);
123 		(void) fprintf(stderr, "\t\t[ -f freq_file ] [ -k key_file ] [ -l log_file ]\n");
124 		(void) fprintf(stderr, "\t\t[ -p pid_file ] [ -r broad_delay ] [ -s statdir ]\n");
125 		(void) fprintf(stderr, "\t\t[ -t trust_key ] [ -v sys_var ] [ -V default_sysvar ]\n");
126 #if defined(HAVE_SCHED_SETSCHEDULER)
127 		(void) fprintf(stderr, "\t\t[ -P fixed_process_priority ]\n");
128 #endif
129 		exit(2);
130 	}
131 	ntp_optind = 0;	/* reset ntp_optind to restart ntp_getopt */
132 
133 #ifdef DEBUG
134 	if (debug) {
135 #ifdef HAVE_SETVBUF
136 		static char buf[BUFSIZ];
137 		setvbuf(stdout, buf, _IOLBF, BUFSIZ);
138 #else
139 		setlinebuf(stdout);
140 #endif
141 	}
142 #endif
143 }
144 
145 /*
146  * getCmdOpts - get command line options
147  */
148 void
149 getCmdOpts(
150 	int argc,
151 	char *argv[]
152 	)
153 {
154 	extern char *config_file;
155 	int errflg;
156 	int c;
157 
158 	/*
159 	 * Initialize, initialize
160 	 */
161 	errflg = 0;
162 #ifdef DEBUG
163 	debug = 0;
164 #endif	/* DEBUG */
165 
166 	progname = argv[0];
167 
168 	/*
169 	 * Decode argument list
170 	 */
171 	while ((c = ntp_getopt(argc, argv, ntp_options)) != EOF) {
172 		switch (c) {
173 		    case 'a':
174 			proto_config(PROTO_AUTHENTICATE, 1, 0.);
175 			break;
176 
177 		    case 'A':
178 			proto_config(PROTO_AUTHENTICATE, 0, 0.);
179 			break;
180 
181 		    case 'b':
182 			proto_config(PROTO_BROADCLIENT, 1, 0.);
183 			break;
184 
185 		    case 'c':
186 			config_file = ntp_optarg;
187 #ifdef HAVE_NETINFO
188 			check_netinfo = 0;
189 #endif
190 			break;
191 
192 		    case 'd':
193 #ifdef DEBUG
194 			debug++;
195 #else
196 			errflg++;
197 #endif	/* DEBUG */
198 			break;
199 
200 		    case 'D':
201 #ifdef DEBUG
202 			debug = (int)atol(ntp_optarg);
203 			printf("Debug2: %s -> %x = %d\n", ntp_optarg, debug, debug);
204 #else
205 			errflg++;
206 #endif	/* DEBUG */
207 			break;
208 
209 		    case 'f':
210 			stats_config(STATS_FREQ_FILE, ntp_optarg);
211 			break;
212 
213 		    case 'g':
214 			allow_panic = TRUE;
215 			break;
216 
217 		    case 'k':
218 			getauthkeys(ntp_optarg);
219 			break;
220 
221 		    case 'L':   /* already done at pre-scan */
222 		    case 'l':   /* already done at pre-scan */
223 			break;
224 
225 		    case 'm':
226 			proto_config(PROTO_MULTICAST_ADD, htonl(INADDR_NTP), 0.);
227 			sys_bclient = 1;
228 			break;
229 
230 		    case 'n':	/* already done at pre-scan */
231 			break;
232 
233 		    case 'N':	/* already done at pre-scan */
234 			break;
235 
236 		    case 'p':
237 			stats_config(STATS_PID_FILE, ntp_optarg);
238 			break;
239 
240 		    case 'P':
241 #if defined(HAVE_SCHED_SETSCHEDULER)
242 			config_priority = (int)atol(ntp_optarg);
243 			config_priority_override = 1;
244 #else
245 			errflg++;
246 #endif
247 			break;
248 
249 		    case 'q':
250 			mode_ntpdate = TRUE;
251 			break;
252 
253 		    case 'r':
254 			do {
255 				double tmp;
256 
257 				if (sscanf(ntp_optarg, "%lf", &tmp) != 1) {
258 					msyslog(LOG_ERR,
259 						"command line broadcast delay value %s undecodable",
260 						ntp_optarg);
261 				} else {
262 					proto_config(PROTO_BROADDELAY, 0, tmp);
263 				}
264 			} while (0);
265 			break;
266 
267 		    case 's':
268 			stats_config(STATS_STATSDIR, ntp_optarg);
269 			break;
270 
271 		    case 't':
272 			do {
273 				u_long tkey;
274 
275 				tkey = (int)atol(ntp_optarg);
276 				if (tkey <= 0 || tkey > NTP_MAXKEY) {
277 					msyslog(LOG_ERR,
278 					    "command line trusted key %s is invalid",
279 					    ntp_optarg);
280 				} else {
281 					authtrust(tkey, 1);
282 				}
283 			} while (0);
284 			break;
285 
286 		    case 'v':
287 		    case 'V':
288 			set_sys_var(ntp_optarg, strlen(ntp_optarg)+1,
289 			    RW | ((c == 'V') ? DEF : 0));
290 			break;
291 
292 		    case 'x':
293 			allow_step = FALSE;
294 			break;
295 
296 		    default:
297 			errflg++;
298 			break;
299 		}
300 	}
301 
302 	if (errflg || ntp_optind != argc) {
303 		(void) fprintf(stderr, "usage: %s [ -abdgmnx ] [ -c config_file ] [ -e e_delay ]\n", progname);
304 		(void) fprintf(stderr, "\t\t[ -f freq_file ] [ -k key_file ] [ -l log_file ]\n");
305 		(void) fprintf(stderr, "\t\t[ -p pid_file ] [ -r broad_delay ] [ -s statdir ]\n");
306 		(void) fprintf(stderr, "\t\t[ -t trust_key ] [ -v sys_var ] [ -V default_sysvar ]\n");
307 #if defined(HAVE_SCHED_SETSCHEDULER)
308 		(void) fprintf(stderr, "\t\t[ -P fixed_process_priority ]\n");
309 #endif
310 		exit(2);
311 	}
312 	return;
313 }
314