set-tai.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* Set tai offset
  2. * by: John Stultz <john.stultz@linaro.org>
  3. * (C) Copyright Linaro 2013
  4. * Licensed under the GPLv2
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <time.h>
  19. #include <sys/time.h>
  20. #include <sys/timex.h>
  21. #include <string.h>
  22. #include <signal.h>
  23. #include <unistd.h>
  24. #ifdef KTEST
  25. #include "../kselftest.h"
  26. #else
  27. static inline int ksft_exit_pass(void)
  28. {
  29. exit(0);
  30. }
  31. static inline int ksft_exit_fail(void)
  32. {
  33. exit(1);
  34. }
  35. #endif
  36. int set_tai(int offset)
  37. {
  38. struct timex tx;
  39. memset(&tx, 0, sizeof(tx));
  40. tx.modes = ADJ_TAI;
  41. tx.constant = offset;
  42. return adjtimex(&tx);
  43. }
  44. int get_tai(void)
  45. {
  46. struct timex tx;
  47. memset(&tx, 0, sizeof(tx));
  48. adjtimex(&tx);
  49. return tx.tai;
  50. }
  51. int main(int argc, char **argv)
  52. {
  53. int i, ret;
  54. ret = get_tai();
  55. printf("tai offset started at %i\n", ret);
  56. printf("Checking tai offsets can be properly set: ");
  57. for (i = 1; i <= 60; i++) {
  58. ret = set_tai(i);
  59. ret = get_tai();
  60. if (ret != i) {
  61. printf("[FAILED] expected: %i got %i\n", i, ret);
  62. return ksft_exit_fail();
  63. }
  64. }
  65. printf("[OK]\n");
  66. return ksft_exit_pass();
  67. }