set-tai.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* Set tai offset
  2. * by: John Stultz <[email protected]>
  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. #include "../kselftest.h"
  25. int set_tai(int offset)
  26. {
  27. struct timex tx;
  28. memset(&tx, 0, sizeof(tx));
  29. tx.modes = ADJ_TAI;
  30. tx.constant = offset;
  31. return adjtimex(&tx);
  32. }
  33. int get_tai(void)
  34. {
  35. struct timex tx;
  36. memset(&tx, 0, sizeof(tx));
  37. adjtimex(&tx);
  38. return tx.tai;
  39. }
  40. int main(int argc, char **argv)
  41. {
  42. int i, ret;
  43. ret = get_tai();
  44. printf("tai offset started at %i\n", ret);
  45. printf("Checking tai offsets can be properly set: ");
  46. fflush(stdout);
  47. for (i = 1; i <= 60; i++) {
  48. ret = set_tai(i);
  49. ret = get_tai();
  50. if (ret != i) {
  51. printf("[FAILED] expected: %i got %i\n", i, ret);
  52. return ksft_exit_fail();
  53. }
  54. }
  55. printf("[OK]\n");
  56. return ksft_exit_pass();
  57. }