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.

90 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. /* RCSID("$Id$"); */
  17. #include <sys/types.h>
  18. #include <unistd.h>
  19. #ifdef HAVE_SYS_TIMEX_H
  20. # include <sys/timex.h>
  21. #endif
  22. #ifdef adjfreq
  23. # undef adjfreq
  24. #endif
  25. #include "ntp.h"
  26. #include "ntpd.h"
  27. #ifndef NTP_ADJTIME
  28. #include <errno.h>
  29. /*
  30. * Some sad but very popular platforms do not appear to provide a mechanism to
  31. * adjust the time frequency at all!
  32. */
  33. int
  34. adjfreq(const int64_t *freq, int64_t *oldfreq)
  35. {
  36. errno = ENOSYS;
  37. return -1;
  38. }
  39. #else
  40. /*
  41. * adjfreq (old)freq = nanosec. per seconds shifted left 32 bits
  42. * timex.freq is ppm / left shifted by SHIFT_USEC (16 bits), defined in timex.h
  43. */
  44. int
  45. adjfreq(const int64_t *freq, int64_t *oldfreq)
  46. {
  47. struct timex txc;
  48. int64_t newfreq;
  49. if (freq != NULL) {
  50. #if defined(__linux__)
  51. txc.modes = ADJ_FREQUENCY;
  52. #else
  53. txc.modes = MOD_FREQUENCY;
  54. #endif
  55. txc.freq = *freq / 1e3 / (1LL << 16);
  56. if ((ntp_adjtime(&txc)) == -1)
  57. log_warn("ntp_adjtime (2) failed");
  58. log_debug("ntp_adjtime adjusted frequency by %fppm",
  59. ((txc.freq * 1e3) * (1LL<<16) / 1e3 / (1LL << 32)));
  60. }
  61. if (oldfreq != NULL) {
  62. txc.modes = 0;
  63. if ((ntp_adjtime(&txc)) == -1) {
  64. log_warn("ntp_adjtime (1) failed");
  65. return -1;
  66. }
  67. newfreq = (txc.freq * 1e3) * (1LL<<16);
  68. log_debug("ntp_adjtime returns frequency of %fppm",
  69. newfreq / 1e3 / (1LL << 32));
  70. *oldfreq = newfreq;
  71. }
  72. return 0;
  73. }
  74. #endif