diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2009-11-24 14:04:15 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2009-11-24 14:04:15 +0100 |
commit | 363e89b1ee7a70e1328370e7c0db2852d06c15bf (patch) | |
tree | b1c636f02961b2601c9bdb9fa6fb52f07b6477e1 /networking/ntpd.c | |
parent | ca6c7e42f93eb2b0ce246eafd8b0e4ac27414f6b (diff) | |
download | busybox-w32-363e89b1ee7a70e1328370e7c0db2852d06c15bf.tar.gz busybox-w32-363e89b1ee7a70e1328370e7c0db2852d06c15bf.tar.bz2 busybox-w32-363e89b1ee7a70e1328370e7c0db2852d06c15bf.zip |
ntpd: use MSG_DONTWAIT; better readability
function old new delta
gettime_fp - 39 +39
ntpd_main 3214 3199 -15
gettime 47 - -47
------------------------------------------------------------------------------
(add/remove: 1/1 grow/shrink: 0/1 up/down: 39/-62) Total: -23 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'networking/ntpd.c')
-rw-r--r-- | networking/ntpd.c | 209 |
1 files changed, 108 insertions, 101 deletions
diff --git a/networking/ntpd.c b/networking/ntpd.c index 4e661ce98..fb82fd309 100644 --- a/networking/ntpd.c +++ b/networking/ntpd.c | |||
@@ -16,7 +16,7 @@ | |||
16 | #define INTERVAL_QUERY_PATHETIC 60 | 16 | #define INTERVAL_QUERY_PATHETIC 60 |
17 | #define INTERVAL_QUERY_AGRESSIVE 5 | 17 | #define INTERVAL_QUERY_AGRESSIVE 5 |
18 | 18 | ||
19 | #define TRUSTLEVEL_BADPEER 6 | 19 | #define TRUSTLEVEL_BADPEER 6 /* bad if *less than* TRUSTLEVEL_BADPEER */ |
20 | #define TRUSTLEVEL_PATHETIC 2 | 20 | #define TRUSTLEVEL_PATHETIC 2 |
21 | #define TRUSTLEVEL_AGRESSIVE 8 | 21 | #define TRUSTLEVEL_AGRESSIVE 8 |
22 | #define TRUSTLEVEL_MAX 10 | 22 | #define TRUSTLEVEL_MAX 10 |
@@ -214,14 +214,13 @@ add_peers(const char *s) | |||
214 | } | 214 | } |
215 | 215 | ||
216 | static double | 216 | static double |
217 | gettime(void) | 217 | gettime_fp(void) |
218 | { | 218 | { |
219 | struct timeval tv; | 219 | struct timeval tv; |
220 | gettimeofday(&tv, NULL); /* never fails */ | 220 | gettimeofday(&tv, NULL); /* never fails */ |
221 | return (tv.tv_sec + JAN_1970 + 1.0e-6 * tv.tv_usec); | 221 | return (tv.tv_sec + 1.0e-6 * tv.tv_usec + JAN_1970); |
222 | } | 222 | } |
223 | 223 | ||
224 | |||
225 | static void | 224 | static void |
226 | d_to_tv(double d, struct timeval *tv) | 225 | d_to_tv(double d, struct timeval *tv) |
227 | { | 226 | { |
@@ -298,9 +297,9 @@ sendmsg_wrap(int fd, | |||
298 | 297 | ||
299 | errno = 0; | 298 | errno = 0; |
300 | if (!from) { | 299 | if (!from) { |
301 | ret = sendto(fd, msg, len, 0, to, addrlen); | 300 | ret = sendto(fd, msg, len, MSG_DONTWAIT, to, addrlen); |
302 | } else { | 301 | } else { |
303 | ret = send_to_from(fd, msg, len, 0, to, from, addrlen); | 302 | ret = send_to_from(fd, msg, len, MSG_DONTWAIT, to, from, addrlen); |
304 | } | 303 | } |
305 | if (ret != len) { | 304 | if (ret != len) { |
306 | bb_perror_msg("send failed"); | 305 | bb_perror_msg("send failed"); |
@@ -310,7 +309,7 @@ sendmsg_wrap(int fd, | |||
310 | } | 309 | } |
311 | 310 | ||
312 | static int | 311 | static int |
313 | client_query(ntp_peer_t *p) | 312 | send_query_to_peer(ntp_peer_t *p) |
314 | { | 313 | { |
315 | if (p->query.fd == -1) { | 314 | if (p->query.fd == -1) { |
316 | p->query.fd = xsocket(p->lsa->u.sa.sa_family, SOCK_DGRAM, 0); | 315 | p->query.fd = xsocket(p->lsa->u.sa.sa_family, SOCK_DGRAM, 0); |
@@ -336,7 +335,7 @@ client_query(ntp_peer_t *p) | |||
336 | 335 | ||
337 | p->query.msg.xmttime.int_partl = random(); | 336 | p->query.msg.xmttime.int_partl = random(); |
338 | p->query.msg.xmttime.fractionl = random(); | 337 | p->query.msg.xmttime.fractionl = random(); |
339 | p->query.xmttime = gettime(); | 338 | p->query.xmttime = gettime_fp(); |
340 | 339 | ||
341 | if (sendmsg_wrap(p->query.fd, /*from:*/ NULL, /*to:*/ &p->lsa->u.sa, /*addrlen:*/ p->lsa->len, | 340 | if (sendmsg_wrap(p->query.fd, /*from:*/ NULL, /*to:*/ &p->lsa->u.sa, /*addrlen:*/ p->lsa->len, |
342 | &p->query.msg, NTP_MSGSIZE_NOAUTH) == -1) { | 341 | &p->query.msg, NTP_MSGSIZE_NOAUTH) == -1) { |
@@ -377,6 +376,7 @@ adjtime_wrap(void) | |||
377 | { | 376 | { |
378 | ntp_peer_t *p; | 377 | ntp_peer_t *p; |
379 | unsigned offset_cnt; | 378 | unsigned offset_cnt; |
379 | unsigned middle; | ||
380 | int i = 0; | 380 | int i = 0; |
381 | ntp_peer_t **peers; | 381 | ntp_peer_t **peers; |
382 | double offset_median; | 382 | double offset_median; |
@@ -394,7 +394,10 @@ adjtime_wrap(void) | |||
394 | offset_cnt++; | 394 | offset_cnt++; |
395 | } | 395 | } |
396 | 396 | ||
397 | peers = xzalloc(sizeof(ntp_peer_t *) * offset_cnt); | 397 | if (offset_cnt == 0) |
398 | goto clear_good; | ||
399 | |||
400 | peers = xzalloc(sizeof(peers[0]) * offset_cnt); | ||
398 | for (item = G.ntp_peers; item != NULL; item = item->link) { | 401 | for (item = G.ntp_peers; item != NULL; item = item->link) { |
399 | p = (ntp_peer_t *) item->data; | 402 | p = (ntp_peer_t *) item->data; |
400 | if (p->trustlevel < TRUSTLEVEL_BADPEER) | 403 | if (p->trustlevel < TRUSTLEVEL_BADPEER) |
@@ -402,62 +405,55 @@ adjtime_wrap(void) | |||
402 | peers[i++] = p; | 405 | peers[i++] = p; |
403 | } | 406 | } |
404 | 407 | ||
405 | qsort(peers, offset_cnt, sizeof(ntp_peer_t *), offset_compare); | 408 | qsort(peers, offset_cnt, sizeof(peers[0]), offset_compare); |
406 | |||
407 | if (offset_cnt != 0) { | ||
408 | if ((offset_cnt & 1) == 0) { | ||
409 | //TODO: try offset_cnt /= 2... | ||
410 | offset_median = | ||
411 | (peers[offset_cnt / 2 - 1]->update.offset + | ||
412 | peers[offset_cnt / 2]->update.offset) / 2; | ||
413 | G.status.rootdelay = | ||
414 | (peers[offset_cnt / 2 - 1]->update.delay + | ||
415 | peers[offset_cnt / 2]->update.delay) / 2; | ||
416 | G.status.stratum = MAX( | ||
417 | peers[offset_cnt / 2 - 1]->update.status.stratum, | ||
418 | peers[offset_cnt / 2]->update.status.stratum); | ||
419 | } else { | ||
420 | offset_median = peers[offset_cnt / 2]->update.offset; | ||
421 | G.status.rootdelay = peers[offset_cnt / 2]->update.delay; | ||
422 | G.status.stratum = peers[offset_cnt / 2]->update.status.stratum; | ||
423 | } | ||
424 | G.status.leap = peers[offset_cnt / 2]->update.status.leap; | ||
425 | 409 | ||
426 | bb_info_msg("adjusting local clock by %fs", offset_median); | 410 | middle = offset_cnt / 2; |
411 | if ((offset_cnt & 1) == 0) { | ||
412 | offset_median = (peers[middle-1]->update.offset + peers[middle]->update.offset) / 2; | ||
413 | G.status.rootdelay = (peers[middle-1]->update.delay + peers[middle]->update.delay) / 2; | ||
414 | G.status.stratum = MAX(peers[middle-1]->update.status.stratum, peers[middle]->update.status.stratum); | ||
415 | } else { | ||
416 | offset_median = peers[middle]->update.offset; | ||
417 | G.status.rootdelay = peers[middle]->update.delay; | ||
418 | G.status.stratum = peers[middle]->update.status.stratum; | ||
419 | } | ||
420 | G.status.leap = peers[middle]->update.status.leap; | ||
427 | 421 | ||
428 | d_to_tv(offset_median, &tv); | 422 | bb_info_msg("adjusting local clock by %fs", offset_median); |
429 | if (adjtime(&tv, &olddelta) == -1) | ||
430 | bb_error_msg("adjtime failed"); | ||
431 | else if (!G.firstadj | ||
432 | && olddelta.tv_sec == 0 | ||
433 | && olddelta.tv_usec == 0 | ||
434 | && !G.status.synced | ||
435 | ) { | ||
436 | bb_info_msg("clock synced"); | ||
437 | G.status.synced = 1; | ||
438 | } else if (G.status.synced) { | ||
439 | bb_info_msg("clock unsynced"); | ||
440 | G.status.synced = 0; | ||
441 | } | ||
442 | 423 | ||
443 | G.firstadj = 0; | 424 | d_to_tv(offset_median, &tv); |
444 | G.status.reftime = gettime(); | 425 | if (adjtime(&tv, &olddelta) == -1) |
445 | G.status.stratum++; /* one more than selected peer */ | 426 | bb_error_msg("adjtime failed"); |
446 | G.scale = updated_scale(offset_median); | 427 | else if (!G.firstadj |
428 | && olddelta.tv_sec == 0 | ||
429 | && olddelta.tv_usec == 0 | ||
430 | && !G.status.synced | ||
431 | ) { | ||
432 | bb_info_msg("clock synced"); | ||
433 | G.status.synced = 1; | ||
434 | } else if (G.status.synced) { | ||
435 | bb_info_msg("clock unsynced"); | ||
436 | G.status.synced = 0; | ||
437 | } | ||
438 | |||
439 | G.firstadj = 0; | ||
440 | G.status.reftime = gettime_fp(); | ||
441 | G.status.stratum++; /* one more than selected peer */ | ||
442 | G.scale = updated_scale(offset_median); | ||
447 | 443 | ||
448 | G.status.refid4 = peers[offset_cnt / 2]->update.status.refid4; | 444 | G.status.refid4 = peers[middle]->update.status.refid4; |
449 | 445 | ||
450 | lsa = peers[offset_cnt / 2]->lsa; | 446 | lsa = peers[middle]->lsa; |
451 | G.status.refid = | 447 | G.status.refid = |
452 | #if ENABLE_FEATURE_IPV6 | 448 | #if ENABLE_FEATURE_IPV6 |
453 | lsa->u.sa.sa_family != AF_INET ? | 449 | lsa->u.sa.sa_family != AF_INET ? |
454 | G.status.refid4 : | 450 | G.status.refid4 : |
455 | #endif | 451 | #endif |
456 | lsa->u.sin.sin_addr.s_addr; | 452 | lsa->u.sin.sin_addr.s_addr; |
457 | } | ||
458 | 453 | ||
459 | free(peers); | 454 | free(peers); |
460 | 455 | ||
456 | clear_good: | ||
461 | for (item = G.ntp_peers; item != NULL; item = item->link) { | 457 | for (item = G.ntp_peers; item != NULL; item = item->link) { |
462 | p = (ntp_peer_t *) item->data; | 458 | p = (ntp_peer_t *) item->data; |
463 | p->update.good = 0; | 459 | p->update.good = 0; |
@@ -514,7 +510,7 @@ settime(double offset) | |||
514 | } | 510 | } |
515 | 511 | ||
516 | static void | 512 | static void |
517 | client_update(ntp_peer_t *p) | 513 | update_peer_data(ntp_peer_t *p) |
518 | { | 514 | { |
519 | int i, best = 0, good = 0; | 515 | int i, best = 0, good = 0; |
520 | 516 | ||
@@ -561,7 +557,7 @@ scale_interval(time_t requested) | |||
561 | } | 557 | } |
562 | 558 | ||
563 | static void | 559 | static void |
564 | client_dispatch(ntp_peer_t *p) | 560 | recv_and_process_peer_pkt(ntp_peer_t *p) |
565 | { | 561 | { |
566 | char *addr; | 562 | char *addr; |
567 | ssize_t size; | 563 | ssize_t size; |
@@ -572,13 +568,13 @@ client_dispatch(ntp_peer_t *p) | |||
572 | 568 | ||
573 | addr = xmalloc_sockaddr2dotted_noport(&p->lsa->u.sa); | 569 | addr = xmalloc_sockaddr2dotted_noport(&p->lsa->u.sa); |
574 | 570 | ||
575 | //TODO: use MSG_DONTWAIT flag? | 571 | size = recv(p->query.fd, &msg, sizeof(msg), MSG_DONTWAIT); |
576 | size = recv(p->query.fd, &msg, sizeof(msg), 0); | ||
577 | if (size == -1) { | 572 | if (size == -1) { |
578 | bb_perror_msg("recv(%s) error", addr); | 573 | bb_perror_msg("recv(%s) error", addr); |
579 | if (errno == EHOSTUNREACH || errno == EHOSTDOWN | 574 | if (errno == EHOSTUNREACH || errno == EHOSTDOWN |
580 | || errno == ENETUNREACH || errno == ENETDOWN | 575 | || errno == ENETUNREACH || errno == ENETDOWN |
581 | || errno == ECONNREFUSED || errno == EADDRNOTAVAIL | 576 | || errno == ECONNREFUSED || errno == EADDRNOTAVAIL |
577 | || errno == EAGAIN | ||
582 | ) { | 578 | ) { |
583 | //TODO: always do this? | 579 | //TODO: always do this? |
584 | set_next(p, error_interval()); | 580 | set_next(p, error_interval()); |
@@ -587,7 +583,7 @@ client_dispatch(ntp_peer_t *p) | |||
587 | xfunc_die(); | 583 | xfunc_die(); |
588 | } | 584 | } |
589 | 585 | ||
590 | T4 = gettime(); | 586 | T4 = gettime_fp(); |
591 | 587 | ||
592 | if (size != NTP_MSGSIZE_NOAUTH && size != NTP_MSGSIZE) { | 588 | if (size != NTP_MSGSIZE_NOAUTH && size != NTP_MSGSIZE) { |
593 | bb_error_msg("malformed packet received from %s", addr); | 589 | bb_error_msg("malformed packet received from %s", addr); |
@@ -639,6 +635,7 @@ client_dispatch(ntp_peer_t *p) | |||
639 | goto bail; | 635 | goto bail; |
640 | } | 636 | } |
641 | offset->error = (T2 - T1) - (T3 - T4); | 637 | offset->error = (T2 - T1) - (T3 - T4); |
638 | // Can we use (T4 - JAN_1970) instead of time(NULL)? | ||
642 | offset->rcvd = time(NULL); | 639 | offset->rcvd = time(NULL); |
643 | offset->good = 1; | 640 | offset->good = 1; |
644 | 641 | ||
@@ -664,21 +661,19 @@ client_dispatch(ntp_peer_t *p) | |||
664 | 661 | ||
665 | /* every received reply which we do not discard increases trust */ | 662 | /* every received reply which we do not discard increases trust */ |
666 | if (p->trustlevel < TRUSTLEVEL_MAX) { | 663 | if (p->trustlevel < TRUSTLEVEL_MAX) { |
667 | if (p->trustlevel < TRUSTLEVEL_BADPEER | ||
668 | && p->trustlevel + 1 >= TRUSTLEVEL_BADPEER | ||
669 | ) { | ||
670 | bb_info_msg("peer %s now valid", addr); | ||
671 | } | ||
672 | p->trustlevel++; | 664 | p->trustlevel++; |
665 | if (p->trustlevel == TRUSTLEVEL_BADPEER) | ||
666 | bb_info_msg("peer %s now valid", addr); | ||
673 | } | 667 | } |
674 | 668 | ||
675 | bb_info_msg("reply from %s: offset %f delay %f, next query %ds", addr, | 669 | bb_info_msg("reply from %s: offset %f delay %f, next query %ds", addr, |
676 | offset->offset, offset->delay, (int) interval); | 670 | offset->offset, offset->delay, (int) interval); |
677 | 671 | ||
678 | client_update(p); | 672 | update_peer_data(p); |
679 | settime(offset->offset); | 673 | settime(offset->offset); |
680 | 674 | ||
681 | if (++p->shift >= OFFSET_ARRAY_SIZE) | 675 | p->shift++; |
676 | if (p->shift >= OFFSET_ARRAY_SIZE) | ||
682 | p->shift = 0; | 677 | p->shift = 0; |
683 | 678 | ||
684 | bail: | 679 | bail: |
@@ -687,7 +682,7 @@ client_dispatch(ntp_peer_t *p) | |||
687 | 682 | ||
688 | #if ENABLE_FEATURE_NTPD_SERVER | 683 | #if ENABLE_FEATURE_NTPD_SERVER |
689 | static void | 684 | static void |
690 | server_dispatch(int fd) | 685 | recv_and_process_client_pkt(void /*int fd*/) |
691 | { | 686 | { |
692 | ssize_t size; | 687 | ssize_t size; |
693 | uint8_t version; | 688 | uint8_t version; |
@@ -702,12 +697,14 @@ server_dispatch(int fd) | |||
702 | to = get_sock_lsa(G.listen_fd); | 697 | to = get_sock_lsa(G.listen_fd); |
703 | from = xzalloc(to->len); | 698 | from = xzalloc(to->len); |
704 | 699 | ||
705 | //TODO: use MGS_DONTWAIT flag? | 700 | size = recv_from_to(G.listen_fd, &msg, sizeof(msg), MSG_DONTWAIT, from, &to->u.sa, to->len); |
706 | size = recv_from_to(fd, &msg, sizeof(msg), 0, from, &to->u.sa, to->len); | ||
707 | if (size != NTP_MSGSIZE_NOAUTH && size != NTP_MSGSIZE) { | 701 | if (size != NTP_MSGSIZE_NOAUTH && size != NTP_MSGSIZE) { |
708 | char *addr; | 702 | char *addr; |
709 | if (size < 0) | 703 | if (size < 0) { |
710 | bb_error_msg_and_die("recv_from_to"); | 704 | if (errno == EAGAIN) |
705 | goto bail; | ||
706 | bb_perror_msg_and_die("recv_from_to"); | ||
707 | } | ||
711 | addr = xmalloc_sockaddr2dotted_noport(from); | 708 | addr = xmalloc_sockaddr2dotted_noport(from); |
712 | bb_error_msg("malformed packet received from %s", addr); | 709 | bb_error_msg("malformed packet received from %s", addr); |
713 | free(addr); | 710 | free(addr); |
@@ -727,18 +724,18 @@ server_dispatch(int fd) | |||
727 | msg.stratum = G.status.stratum; | 724 | msg.stratum = G.status.stratum; |
728 | msg.ppoll = query_ppoll; | 725 | msg.ppoll = query_ppoll; |
729 | msg.precision = G.status.precision; | 726 | msg.precision = G.status.precision; |
730 | rectime = gettime(); | 727 | rectime = gettime_fp(); |
731 | msg.xmttime = msg.rectime = d_to_lfp(rectime); | 728 | msg.xmttime = msg.rectime = d_to_lfp(rectime); |
732 | msg.reftime = d_to_lfp(G.status.reftime); | 729 | msg.reftime = d_to_lfp(G.status.reftime); |
733 | //msg.xmttime = d_to_lfp(gettime()); // = msg.rectime | 730 | //msg.xmttime = d_to_lfp(gettime_fp()); // = msg.rectime |
734 | msg.orgtime = query_xmttime; | 731 | msg.orgtime = query_xmttime; |
735 | msg.rootdelay = d_to_sfp(G.status.rootdelay); | 732 | msg.rootdelay = d_to_sfp(G.status.rootdelay); |
736 | version = (query_status & VERSION_MASK); /* ... >> VERSION_SHIFT - done below instead */ | 733 | version = (query_status & VERSION_MASK); /* ... >> VERSION_SHIFT - done below instead */ |
737 | msg.refid = (version > (3 << VERSION_SHIFT)) ? G.status.refid4 : G.status.refid; | 734 | msg.refid = (version > (3 << VERSION_SHIFT)) ? G.status.refid4 : G.status.refid; |
738 | 735 | ||
739 | /* We reply from the address packet was sent to, | 736 | /* We reply from the local address packet was sent to, |
740 | * this makes to/from look swapped here: */ | 737 | * this makes to/from look swapped here: */ |
741 | sendmsg_wrap(fd, | 738 | sendmsg_wrap(G.listen_fd, |
742 | /*from:*/ &to->u.sa, /*to:*/ from, /*addrlen:*/ to->len, | 739 | /*from:*/ &to->u.sa, /*to:*/ from, /*addrlen:*/ to->len, |
743 | &msg, size); | 740 | &msg, size); |
744 | 741 | ||
@@ -910,20 +907,23 @@ int ntpd_main(int argc UNUSED_PARAM, char **argv) | |||
910 | ntp_init(argv); | 907 | ntp_init(argv); |
911 | 908 | ||
912 | { | 909 | { |
913 | unsigned new_cnt = g.peer_cnt; | 910 | unsigned cnt = g.peer_cnt; |
914 | idx2peer = xzalloc(sizeof(void *) * new_cnt); | ||
915 | /* if ENABLE_FEATURE_NTPD_SERVER, + 1 for listen_fd: */ | 911 | /* if ENABLE_FEATURE_NTPD_SERVER, + 1 for listen_fd: */ |
916 | pfd = xzalloc(sizeof(pfd[0]) * (new_cnt + ENABLE_FEATURE_NTPD_SERVER)); | 912 | idx2peer = xzalloc(sizeof(void *) * (cnt + ENABLE_FEATURE_NTPD_SERVER)); |
913 | pfd = xzalloc(sizeof(pfd[0]) * (cnt + ENABLE_FEATURE_NTPD_SERVER)); | ||
917 | } | 914 | } |
918 | 915 | ||
919 | while (!bb_got_signal) { | 916 | while (!bb_got_signal) { |
920 | llist_t *item; | 917 | llist_t *item; |
921 | unsigned i, j, first_peer_idx; | 918 | unsigned i, j; |
922 | unsigned sent_cnt, trial_cnt; | 919 | unsigned sent_cnt, trial_cnt; |
923 | int nfds, timeout; | 920 | int nfds, timeout; |
924 | time_t nextaction; | 921 | time_t cur_time, nextaction; |
925 | 922 | ||
926 | nextaction = time(NULL) + 3600; | 923 | /* Nothing between here and poll() blocks for any significant time */ |
924 | |||
925 | cur_time = time(NULL); | ||
926 | nextaction = cur_time + 3600; | ||
927 | 927 | ||
928 | i = 0; | 928 | i = 0; |
929 | #if ENABLE_FEATURE_NTPD_SERVER | 929 | #if ENABLE_FEATURE_NTPD_SERVER |
@@ -933,22 +933,19 @@ int ntpd_main(int argc UNUSED_PARAM, char **argv) | |||
933 | i++; | 933 | i++; |
934 | } | 934 | } |
935 | #endif | 935 | #endif |
936 | first_peer_idx = i; | 936 | /* Pass over peer list, send requests, time out on receives */ |
937 | sent_cnt = trial_cnt = 0; | 937 | sent_cnt = trial_cnt = 0; |
938 | for (item = g.ntp_peers; item != NULL; item = item->link) { | 938 | for (item = g.ntp_peers; item != NULL; item = item->link) { |
939 | ntp_peer_t *p = (ntp_peer_t *) item->data; | 939 | ntp_peer_t *p = (ntp_peer_t *) item->data; |
940 | 940 | ||
941 | if (p->next > 0 && p->next <= time(NULL)) { | 941 | if (p->next != 0 && p->next <= cur_time) { |
942 | /* Time to send new req */ | ||
942 | trial_cnt++; | 943 | trial_cnt++; |
943 | if (client_query(p) == 0) | 944 | if (send_query_to_peer(p) == 0) |
944 | sent_cnt++; | 945 | sent_cnt++; |
945 | } | 946 | } |
946 | if (p->next > 0 && p->next < nextaction) | 947 | if (p->deadline != 0 && p->deadline <= cur_time) { |
947 | nextaction = p->next; | 948 | /* Timed out waiting for reply */ |
948 | if (p->deadline > 0 && p->deadline < nextaction) | ||
949 | nextaction = p->deadline; | ||
950 | |||
951 | if (p->deadline > 0 && p->deadline <= time(NULL)) { | ||
952 | char *addr = xmalloc_sockaddr2dotted_noport(&p->lsa->u.sa); | 949 | char *addr = xmalloc_sockaddr2dotted_noport(&p->lsa->u.sa); |
953 | 950 | ||
954 | timeout = error_interval(); | 951 | timeout = error_interval(); |
@@ -964,39 +961,49 @@ int ntpd_main(int argc UNUSED_PARAM, char **argv) | |||
964 | set_next(p, timeout); | 961 | set_next(p, timeout); |
965 | } | 962 | } |
966 | 963 | ||
964 | if (p->next != 0 && p->next < nextaction) | ||
965 | nextaction = p->next; | ||
966 | if (p->deadline != 0 && p->deadline < nextaction) | ||
967 | nextaction = p->deadline; | ||
968 | |||
967 | if (p->state == STATE_QUERY_SENT) { | 969 | if (p->state == STATE_QUERY_SENT) { |
970 | /* Wait for reply from this peer */ | ||
968 | pfd[i].fd = p->query.fd; | 971 | pfd[i].fd = p->query.fd; |
969 | pfd[i].events = POLLIN; | 972 | pfd[i].events = POLLIN; |
970 | idx2peer[i - first_peer_idx] = p; | 973 | idx2peer[i] = p; |
971 | i++; | 974 | i++; |
972 | } | 975 | } |
973 | } | 976 | } |
974 | 977 | ||
975 | if ((trial_cnt > 0 && sent_cnt == 0) || g.peer_cnt == 0) | 978 | if ((trial_cnt > 0 && sent_cnt == 0) || g.peer_cnt == 0) |
976 | settime(0); /* no good peers, don't wait */ | 979 | settime(0); /* no good peers, don't wait */ |
977 | 980 | ||
978 | timeout = nextaction - time(NULL); | 981 | timeout = nextaction - cur_time; |
979 | if (timeout < 0) | 982 | if (timeout < 0) |
980 | timeout = 0; | 983 | timeout = 0; |
981 | 984 | ||
985 | /* Here we may block */ | ||
982 | if (g.verbose) | 986 | if (g.verbose) |
983 | bb_error_msg("entering poll %u secs", timeout); | 987 | bb_error_msg("entering poll %u secs", timeout); |
984 | nfds = poll(pfd, i, timeout * 1000); | 988 | nfds = poll(pfd, i, timeout * 1000); |
989 | if (nfds <= 0) | ||
990 | continue; | ||
985 | 991 | ||
992 | /* Process any received packets */ | ||
986 | j = 0; | 993 | j = 0; |
987 | #if ENABLE_FEATURE_NTPD_SERVER | 994 | #if ENABLE_FEATURE_NTPD_SERVER |
988 | //TODO: simplify. There is only one server fd! | 995 | if (g.listen_fd != -1) { |
989 | for (; nfds > 0 && j < first_peer_idx; j++) { | 996 | if (pfd[0].revents /* & (POLLIN|POLLERR)*/) { |
990 | if (pfd[j].revents & (POLLIN|POLLERR)) { | ||
991 | nfds--; | 997 | nfds--; |
992 | server_dispatch(pfd[j].fd); | 998 | recv_and_process_client_pkt(/*g.listen_fd*/); |
993 | } | 999 | } |
1000 | j = 1; | ||
994 | } | 1001 | } |
995 | #endif | 1002 | #endif |
996 | for (; nfds > 0 && j < i; j++) { | 1003 | for (; nfds != 0 && j < i; j++) { |
997 | if (pfd[j].revents & (POLLIN|POLLERR)) { | 1004 | if (pfd[j].revents /* & (POLLIN|POLLERR)*/) { |
998 | nfds--; | 1005 | nfds--; |
999 | client_dispatch(idx2peer[j - first_peer_idx]); | 1006 | recv_and_process_peer_pkt(idx2peer[j]); |
1000 | } | 1007 | } |
1001 | } | 1008 | } |
1002 | } /* while (!bb_got_signal) */ | 1009 | } /* while (!bb_got_signal) */ |