aboutsummaryrefslogtreecommitdiff
path: root/networking/ntpd.c
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2009-11-23 16:27:16 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2009-11-23 16:27:16 +0100
commit8d580c72f4eaf5bb6cfec21ef473723c74c3382d (patch)
tree12ce4f7273ff993981549d1a0b7450b20878a1e2 /networking/ntpd.c
parent74f8e7810bbe61bdeffd1dea6adb08426f479705 (diff)
downloadbusybox-w32-8d580c72f4eaf5bb6cfec21ef473723c74c3382d.tar.gz
busybox-w32-8d580c72f4eaf5bb6cfec21ef473723c74c3382d.tar.bz2
busybox-w32-8d580c72f4eaf5bb6cfec21ef473723c74c3382d.zip
ntpd: add -q option. By Adam Tkac.
function old new delta settime 347 375 +28 packed_usage 26681 26693 +12 ntpd_main 3282 3251 -31 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 2/1 up/down: 40/-31) Total: 9 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'networking/ntpd.c')
-rw-r--r--networking/ntpd.c58
1 files changed, 28 insertions, 30 deletions
diff --git a/networking/ntpd.c b/networking/ntpd.c
index 62a00a5bf..8ee42e627 100644
--- a/networking/ntpd.c
+++ b/networking/ntpd.c
@@ -152,6 +152,16 @@ typedef struct {
152 uint8_t trustlevel; 152 uint8_t trustlevel;
153} ntp_peer_t; 153} ntp_peer_t;
154 154
155enum {
156 OPT_n = (1 << 0),
157 OPT_g = (1 << 1),
158 OPT_q = (1 << 2),
159 /* Insert new options above this line. */
160 /* Non-compat options: */
161 OPT_p = (1 << 3),
162 OPT_l = (1 << 4),
163};
164
155 165
156struct globals { 166struct globals {
157 unsigned verbose; 167 unsigned verbose;
@@ -164,7 +174,6 @@ struct globals {
164 uint8_t settime; 174 uint8_t settime;
165 uint8_t firstadj; 175 uint8_t firstadj;
166 smallint peer_cnt; 176 smallint peer_cnt;
167
168}; 177};
169#define G (*ptr_to_globals) 178#define G (*ptr_to_globals)
170 179
@@ -468,14 +477,14 @@ settime(double offset)
468 char buf[80]; 477 char buf[80];
469 time_t tval; 478 time_t tval;
470 479
471#if 0
472 if (!G.settime) 480 if (!G.settime)
473 return; 481 goto bail;
474#endif 482
483 G.settime = 0;
475 484
476 /* if the offset is small, don't call settimeofday */ 485 /* if the offset is small, don't call settimeofday */
477 if (offset < SETTIME_MIN_OFFSET && offset > -SETTIME_MIN_OFFSET) 486 if (offset < SETTIME_MIN_OFFSET && offset > -SETTIME_MIN_OFFSET)
478 return; 487 goto bail;
479 488
480 gettimeofday(&curtime, NULL); /* never fails */ 489 gettimeofday(&curtime, NULL); /* never fails */
481 490
@@ -486,11 +495,9 @@ settime(double offset)
486 495
487 if (settimeofday(&curtime, NULL) == -1) { 496 if (settimeofday(&curtime, NULL) == -1) {
488 bb_error_msg("settimeofday"); 497 bb_error_msg("settimeofday");
489 return; 498 goto bail;
490 } 499 }
491 500
492 G.settime = 0;
493
494 tval = curtime.tv_sec; 501 tval = curtime.tv_sec;
495 strftime(buf, sizeof(buf), "%a %b %e %H:%M:%S %Z %Y", localtime(&tval)); 502 strftime(buf, sizeof(buf), "%a %b %e %H:%M:%S %Z %Y", localtime(&tval));
496 503
@@ -504,6 +511,10 @@ settime(double offset)
504 if (p->deadline) 511 if (p->deadline)
505 p->deadline -= offset; 512 p->deadline -= offset;
506 } 513 }
514
515 bail:
516 if (option_mask32 & OPT_q)
517 exit(0);
507} 518}
508 519
509static void 520static void
@@ -669,8 +680,7 @@ client_dispatch(ntp_peer_t *p)
669 offset->offset, offset->delay, (int) interval); 680 offset->offset, offset->delay, (int) interval);
670 681
671 client_update(p); 682 client_update(p);
672 if (!G.settime) 683 settime(offset->offset);
673 settime(offset->offset);
674 684
675 if (++p->shift >= OFFSET_ARRAY_SIZE) 685 if (++p->shift >= OFFSET_ARRAY_SIZE)
676 p->shift = 0; 686 p->shift = 0;
@@ -817,13 +827,6 @@ server_dispatch(int fd)
817 * Note: The kernel time discipline is disabled with this option. 827 * Note: The kernel time discipline is disabled with this option.
818 */ 828 */
819 829
820enum {
821 OPT_n = (1 << 0),
822 OPT_g = (1 << 1),
823 OPT_p = (1 << 2),
824 OPT_l = (1 << 3),
825};
826
827/* By doing init in a separate function we decrease stack usage 830/* By doing init in a separate function we decrease stack usage
828 * in main loop. 831 * in main loop.
829 */ 832 */
@@ -840,7 +843,7 @@ static NOINLINE void ntp_init(char **argv)
840 peers = NULL; 843 peers = NULL;
841 opt_complementary = "dd:p::"; /* d: counter, p: list */ 844 opt_complementary = "dd:p::"; /* d: counter, p: list */
842 opts = getopt32(argv, 845 opts = getopt32(argv,
843 "ng" /* compat */ 846 "ngq" /* compat */
844 "p:"IF_FEATURE_NTPD_SERVER("l") /* NOT compat */ 847 "p:"IF_FEATURE_NTPD_SERVER("l") /* NOT compat */
845 "d" /* compat */ 848 "d" /* compat */
846 "46aAbLNx", /* compat, ignored */ 849 "46aAbLNx", /* compat, ignored */
@@ -891,7 +894,6 @@ int ntpd_main(int argc UNUSED_PARAM, char **argv) MAIN_EXTERNALLY_VISIBLE;
891int ntpd_main(int argc UNUSED_PARAM, char **argv) 894int ntpd_main(int argc UNUSED_PARAM, char **argv)
892{ 895{
893 struct globals g; 896 struct globals g;
894 unsigned new_cnt;
895 struct pollfd *pfd; 897 struct pollfd *pfd;
896 ntp_peer_t **idx2peer; 898 ntp_peer_t **idx2peer;
897 899
@@ -900,13 +902,12 @@ int ntpd_main(int argc UNUSED_PARAM, char **argv)
900 902
901 ntp_init(argv); 903 ntp_init(argv);
902 904
903 new_cnt = g.peer_cnt; 905 {
904 idx2peer = xzalloc(sizeof(void *) * new_cnt); 906 unsigned new_cnt = g.peer_cnt;
905#if ENABLE_FEATURE_NTPD_SERVER 907 idx2peer = xzalloc(sizeof(void *) * new_cnt);
906 if (g.listen_fd != -1) 908 /* if ENABLE_FEATURE_NTPD_SERVER, + 1 for listen_fd: */
907 new_cnt++; 909 pfd = xzalloc(sizeof(pfd[0]) * (new_cnt + ENABLE_FEATURE_NTPD_SERVER));
908#endif 910 }
909 pfd = xzalloc(sizeof(pfd[0]) * new_cnt);
910 911
911 while (!bb_got_signal) { 912 while (!bb_got_signal) {
912 llist_t *item; 913 llist_t *item;
@@ -964,11 +965,8 @@ int ntpd_main(int argc UNUSED_PARAM, char **argv)
964 } 965 }
965 } 966 }
966 967
967 if (g.settime 968 if ((trial_cnt > 0 && sent_cnt == 0) || g.peer_cnt == 0)
968 && ((trial_cnt > 0 && sent_cnt == 0) || g.peer_cnt == 0)
969 ) {
970 settime(0); /* no good peers, don't wait */ 969 settime(0); /* no good peers, don't wait */
971 }
972 970
973 timeout = nextaction - time(NULL); 971 timeout = nextaction - time(NULL);
974 if (timeout < 0) 972 if (timeout < 0)