Portable build framework for OpenNTPD
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.0 KiB

4 years ago
4 years ago
4 years ago
4 years ago
  1. From f212e683592026fdfe40e24eca9afd21da54eafb Mon Sep 17 00:00:00 2001
  2. From: Brent Cook <busterb@gmail.com>
  3. Date: Sat, 19 Jan 2019 04:25:44 -0600
  4. Subject: [PATCH 14/18] use adjtimex over adjtime, check return value and
  5. adjust offset as needed
  6. ---
  7. src/usr.sbin/ntpd/ntpd.c | 32 +++++++++++++++++++++++++++++---
  8. 1 file changed, 29 insertions(+), 3 deletions(-)
  9. diff --git a/src/usr.sbin/ntpd/ntpd.c b/src/usr.sbin/ntpd/ntpd.c
  10. index daa077dcda..5817fff78a 100644
  11. --- a/src/usr.sbin/ntpd/ntpd.c
  12. +++ b/src/usr.sbin/ntpd/ntpd.c
  13. @@ -17,6 +17,9 @@
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. +#ifdef HAVE_ADJTIMEX
  17. +#include <sys/timex.h>
  18. +#endif
  19. #include <sys/types.h>
  20. #include <sys/resource.h>
  21. #include <sys/socket.h>
  22. @@ -510,21 +513,44 @@ reset_adjtime(void)
  23. int
  24. ntpd_adjtime(double d)
  25. {
  26. - struct timeval tv, olddelta;
  27. int synced = 0;
  28. static int firstadj = 1;
  29. + double threshold = (double)LOG_NEGLIGIBLE_ADJTIME / 1000;
  30. d += getoffset();
  31. - if (d >= (double)LOG_NEGLIGIBLE_ADJTIME / 1000 ||
  32. - d <= -1 * (double)LOG_NEGLIGIBLE_ADJTIME / 1000)
  33. + if (d >= threshold || d <= -1 * threshold)
  34. log_info("adjusting local clock by %fs", d);
  35. else
  36. log_debug("adjusting local clock by %fs", d);
  37. +
  38. +#ifdef HAVE_ADJTIMEX
  39. + int rc;
  40. +
  41. + long offset = d * 1000000;
  42. + struct timex tx = { 0 };
  43. + tx.offset = offset;
  44. + tx.modes = ADJ_OFFSET_SINGLESHOT | ADJ_STATUS;
  45. +
  46. + do {
  47. + rc = adjtimex(&tx);
  48. + } while (rc == TIME_ERROR && (tx.offset /= 2) > threshold);
  49. +
  50. + if (rc == TIME_ERROR) {
  51. + if ((tx.status & ~STA_UNSYNC))
  52. + log_warn("adjtimex returned TIME_ERROR");
  53. + } else if (rc < 0) {
  54. + log_warn("adjtimex failed");
  55. + } else if (!firstadj && tx.offset == offset) {
  56. + synced = 1;
  57. + }
  58. +#else
  59. + struct timeval tv, olddelta;
  60. d_to_tv(d, &tv);
  61. if (adjtime(&tv, &olddelta) == -1)
  62. log_warn("adjtime failed");
  63. else if (!firstadj && olddelta.tv_sec == 0 && olddelta.tv_usec == 0)
  64. synced = 1;
  65. +#endif
  66. firstadj = 0;
  67. update_time_sync_status(synced);
  68. return (synced);
  69. --
  70. 2.27.0