2 * Copyright (c) 2005 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/usr.sbin/dntpd/main.c,v 1.9 2005/04/27 00:42:13 dillon Exp $
39 static void usage(const char *av0);
40 static void dotest(const char *target);
41 static void add_server(const char *target);
42 static void process_config_file(const char *path);
43 static pid_t check_pid(void);
44 static void set_pid(const char *av0);
45 static void sigint_handler(int signo __unused);
47 static struct server_info **servers;
49 static int maxservers;
53 int debug_level = -1; /* (set to default later) */
54 int quickset_opt = 0; /* immediately set time of day on startup */
55 int no_update_opt = 0; /* do not make any actual updates */
56 int min_sleep_opt = 5; /* 5 seconds minimum poll interval */
57 int nom_sleep_opt = 300; /* 5 minutes nominal poll interval */
58 int max_sleep_opt = 1800; /* 30 minutes maximum poll interval */
59 const char *config_opt; /* config file */
60 const char *pid_opt = "/var/run/dntpd.pid";
63 main(int ac, char **av)
80 while ((ch = getopt(ac, av, "df:l:np:qstFL:QST:")) != -1) {
87 if (config_opt == NULL)
88 config_opt = "/dev/null";
97 debug_level = strtol(optarg, NULL, 0);
117 if (config_opt == NULL)
118 config_opt = "/dev/null";
124 max_sleep_opt = strtol(optarg, NULL, 0);
127 nom_sleep_opt = strtol(optarg, NULL, 0);
128 if (nom_sleep_opt < 1) {
129 fprintf(stderr, "Warning: nominal poll interval too small, "
130 "limiting to 1 second\n");
133 if (nom_sleep_opt > 24 * 60 * 60) {
134 fprintf(stderr, "Warning: nominal poll interval too large, "
135 "limiting to 24 hours\n");
136 nom_sleep_opt = 24 * 60 * 60;
138 if (min_sleep_opt > nom_sleep_opt)
139 min_sleep_opt = nom_sleep_opt;
140 if (max_sleep_opt < nom_sleep_opt * 5)
141 max_sleep_opt = nom_sleep_opt * 5;
144 if ((pid = check_pid()) != 0) {
145 fprintf(stderr, "%s: killing old daemon\n", av[0]);
153 fprintf(stderr, "%s: Unable to kill running daemon.\n", av[0]);
155 fprintf(stderr, "%s: Running daemon has been terminated.\n", av[0]);
158 fprintf(stderr, "%s: There is no daemon running to kill.\n", av[0]);
168 if (config_opt == NULL) {
170 config_opt = "/dev/null";
172 config_opt = "/etc/dntpd.conf";
178 process_config_file(config_opt);
181 openlog("dntpd", LOG_CONS|LOG_PID, LOG_DAEMON);
184 if (optind != ac - 1)
191 * Add additional hosts.
193 for (i = optind; i < ac; ++i) {
202 * Do an initial course time setting if requested using the first
203 * host successfully polled.
208 * Daemonize, stop logging to stderr.
211 if ((pid = check_pid()) != 0) {
212 logerrstr("%s: NOTE: killing old daemon and starting a new one",
221 logerrstr("%s: Unable to kill running daemon, exiting", av[0]);
226 } else if (check_pid() != 0) {
227 logerrstr("%s: A background dntpd is running, you must kill it first",
231 if (debug_opt == 0) {
234 signal(SIGINT, sigint_handler);
235 logdebug(0, "dntpd version %s started\n", DNTPD_VERSION);
241 sysntp_clear_alternative_corrections();
243 rc = client_main(servers, nservers);
249 usage(const char *av0)
251 fprintf(stderr, "%s [-dnqstFSQ] [-f config_file] [-l log_level] [-T poll_interval] [-L poll_limit] [additional_targets]\n", av0);
253 "\t-d\tDebugging mode, implies -F, -l 99, and logs to stderr\n"
254 "\t-f file\tSpecify the config file (/etc/dntpd.conf)\n"
255 "\t-l int\tSet log level (0-4), default 1\n"
256 "\t-n\tNo-update mode. No offset or frequency corrections are made\n"
257 "\t-q\tQuiet-mode, same as -L 0\n"
258 "\t-s\tSet the time immediately on startup\n"
259 "\t-t\tTest mode, implies -F, -l 99, -n, logs to stderr\n"
260 "\t-F\tRun in foreground (log still goes to syslog)\n"
261 "\t-L int\tMaximum polling interval\n"
262 "\t-S\tDo not set the time immediately on startup\n"
263 "\t-T int\tNominal polling interval\n"
264 "\t-Q\tTerminate any running background daemon\n"
266 "\t\tNOTE: in debug and test modes -f must be specified if\n"
267 "\t\tyou want to use a config file.\n"
274 dotest(const char *target)
276 struct server_info info;
278 bzero(&info, sizeof(info));
279 info.fd = udp_socket(target, 123);
281 logerrstr("unable to create UDP socket for %s", target);
284 info.target = strdup(target);
288 "Will run %d-second polls until interrupted.\n", nom_sleep_opt);
291 client_poll(&info, nom_sleep_opt, 1);
292 sleep(nom_sleep_opt);
298 add_server(const char *target)
302 if (nservers == maxservers) {
304 servers = realloc(servers, maxservers * sizeof(server_info_t));
305 assert(servers != NULL);
307 info = malloc(sizeof(struct server_info));
308 servers[nservers] = info;
309 bzero(info, sizeof(struct server_info));
310 info->fd = udp_socket(target, 123);
312 logerrstr("Unable to add server %s", target);
314 info->target = strdup(target);
320 process_config_file(const char *path)
322 const char *ws = " \t\r\n";
329 if ((fi = fopen(path, "r")) != NULL) {
331 while (fgets(buf, sizeof(buf), fi) != NULL) {
332 if (strchr(buf, '#'))
333 *strchr(buf, '#') = 0;
334 if ((keyword = strtok(buf, ws)) != NULL) {
335 data = strtok(NULL, ws);
336 if (strcmp(keyword, "server") == 0) {
338 logerr("%s:%d server missing host specification",
344 logerr("%s:%d unknown keyword %s", path, line, keyword);
351 logerr("Unable to open %s", path);
365 if ((fi = fopen(pid_opt, "r")) != NULL) {
366 if (fgets(buf, sizeof(buf), fi) != NULL) {
367 pid = strtol(buf, NULL, 0);
368 if (kill(pid, 0) != 0)
378 set_pid(const char *av0)
384 if ((fo = fopen(pid_opt, "w")) != NULL) {
385 fprintf(fo, "%d\n", (int)pid);
388 logerr("%s: Unable to create %s, continuing anyway.", av0, pid_opt);
394 sigint_handler(int signo __unused)
397 /* dangerous, but we are exiting anyway so pray... */
398 logdebug(0, "dntpd version %s stopped\n", DNTPD_VERSION);