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.

48 lines
1.5 KiB

  1. /*
  2. * Copyright (c) 1999-2004 Damien Miller <djm@mindrot.org>
  3. * Copyright (c) 2014 Brent Cook <bcook@openbsd.org>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <mach/mach_time.h>
  18. #include <sys/time.h>
  19. #include <stdint.h>
  20. #include <time.h>
  21. int
  22. clock_gettime(clockid_t clk_id, struct timespec *ts)
  23. {
  24. static uint64_t timebase_scale = 0;
  25. mach_timebase_info_data_t timebase_info;
  26. uint64_t nsec;
  27. /*
  28. * Only CLOCK_MONOTONIC is needed by ntpd
  29. */
  30. if (clk_id != CLOCK_MONOTONIC)
  31. return -1;
  32. if (timebase_scale == 0) {
  33. if (mach_timebase_info(&timebase_info))
  34. return -1;
  35. timebase_scale = timebase_info.numer / timebase_info.denom;
  36. }
  37. nsec = mach_absolute_time() * timebase_scale;
  38. ts->tv_sec = nsec / 1000000000UL;
  39. ts->tv_nsec = nsec % 1000000000UL;
  40. return 0;
  41. }