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.

80 lines
2.1 KiB

  1. /*
  2. * Copyright (c) 2007 Sebastian Benoit <benoit-lists@fb12.de>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <sys/types.h>
  17. #include <sys/time.h>
  18. #include <sys/timex.h>
  19. #include <unistd.h>
  20. #include <ntp.h>
  21. #include <ntpd.h>
  22. /*
  23. * adjfreq (old)freq = nanosec. per seconds shifted left 32 bits
  24. * timex.freq is ppm / left shifted by SHIFT_USEC (16 bits), defined in timex.h
  25. */
  26. int
  27. adjfreq(const int64_t *freq, int64_t *oldfreq)
  28. {
  29. struct timex txc = { 0 };
  30. int64_t newfreq;
  31. if (freq != NULL) {
  32. txc.modes = MOD_FREQUENCY;
  33. txc.freq = *freq / 1e3 / (1LL << 16);
  34. if ((ntp_adjtime(&txc)) == -1)
  35. log_warn("ntp_adjtime (2) failed");
  36. log_debug("ntp_adjtime adjusted frequency by %fppm",
  37. ((txc.freq * 1e3) * (1LL<<16) / 1e3 / (1LL << 32)));
  38. }
  39. if (oldfreq != NULL) {
  40. txc.modes = 0;
  41. if ((ntp_adjtime(&txc)) == -1) {
  42. log_warn("ntp_adjtime (1) failed");
  43. return -1;
  44. }
  45. newfreq = (txc.freq * 1e3) * (1LL<<16);
  46. log_debug("ntp_adjtime returns frequency of %fppm",
  47. newfreq / 1e3 / (1LL << 32));
  48. *oldfreq = newfreq;
  49. }
  50. return 0;
  51. }
  52. /*
  53. * The RTC is only updated if the clock is not marked as unsynced.
  54. */
  55. void
  56. update_time_sync_status(int synced)
  57. {
  58. struct timex txc = { 0 };
  59. txc.modes = MOD_STATUS;
  60. if (synced) {
  61. txc.modes |= MOD_MAXERROR;
  62. txc.maxerror = 0;
  63. } else
  64. txc.status = STA_UNSYNC;
  65. if (ntp_adjtime(&txc) == -1)
  66. log_warn("ntp_adjtime (3) failed");
  67. return;
  68. }