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.

76 lines
2.0 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/timex.h>
  18. #include <unistd.h>
  19. #include <ntp.h>
  20. #include <ntpd.h>
  21. /*
  22. * adjfreq (old)freq = nanosec. per seconds shifted left 32 bits
  23. * timex.freq is ppm / left shifted by SHIFT_USEC (16 bits), defined in timex.h
  24. */
  25. int
  26. adjfreq(const int64_t *freq, int64_t *oldfreq)
  27. {
  28. struct timex txc = { 0 };
  29. int64_t newfreq;
  30. if (freq != NULL) {
  31. txc.modes = ADJ_FREQUENCY;
  32. txc.freq = *freq / 1e3 / (1LL << 16);
  33. if ((adjtimex(&txc)) == -1)
  34. log_warn("adjtimex failed");
  35. log_debug("adjtimex adjusted frequency by %fppm",
  36. ((txc.freq * 1e3) * (1LL<<16) / 1e3 / (1LL << 32)));
  37. }
  38. if (oldfreq != NULL) {
  39. txc.modes = 0;
  40. if ((adjtimex(&txc)) == -1) {
  41. log_warn("adjtimex failed");
  42. return -1;
  43. }
  44. newfreq = (txc.freq * 1e3) * (1LL<<16);
  45. log_debug("adjtimex returns frequency of %fppm",
  46. newfreq / 1e3 / (1LL << 32));
  47. *oldfreq = newfreq;
  48. }
  49. return 0;
  50. }
  51. /*
  52. * The RTC is only updated if the clock is not marked as unsynced.
  53. */
  54. void
  55. update_time_sync_status(int synced)
  56. {
  57. struct timex txc = { 0 };
  58. txc.modes = MOD_STATUS;
  59. if (!synced)
  60. txc.status = STA_UNSYNC;
  61. if (adjtimex(&txc) == -1)
  62. log_warn("ntp_adjtime (3) failed");
  63. return;
  64. }