leapcrash.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* Demo leapsecond deadlock
  2. * by: John Stultz (john.stultz@linaro.org)
  3. * (C) Copyright IBM 2012
  4. * (C) Copyright 2013, 2015 Linaro Limited
  5. * Licensed under the GPL
  6. *
  7. * This test demonstrates leapsecond deadlock that is possibe
  8. * on kernels from 2.6.26 to 3.3.
  9. *
  10. * WARNING: THIS WILL LIKELY HARDHANG SYSTEMS AND MAY LOSE DATA
  11. * RUN AT YOUR OWN RISK!
  12. * To build:
  13. * $ gcc leapcrash.c -o leapcrash -lrt
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <time.h>
  18. #include <sys/time.h>
  19. #include <sys/timex.h>
  20. #include <string.h>
  21. #include <signal.h>
  22. #ifdef KTEST
  23. #include "../kselftest.h"
  24. #else
  25. static inline int ksft_exit_pass(void)
  26. {
  27. exit(0);
  28. }
  29. static inline int ksft_exit_fail(void)
  30. {
  31. exit(1);
  32. }
  33. #endif
  34. /* clear NTP time_status & time_state */
  35. int clear_time_state(void)
  36. {
  37. struct timex tx;
  38. int ret;
  39. /*
  40. * We have to call adjtime twice here, as kernels
  41. * prior to 6b1859dba01c7 (included in 3.5 and
  42. * -stable), had an issue with the state machine
  43. * and wouldn't clear the STA_INS/DEL flag directly.
  44. */
  45. tx.modes = ADJ_STATUS;
  46. tx.status = STA_PLL;
  47. ret = adjtimex(&tx);
  48. tx.modes = ADJ_STATUS;
  49. tx.status = 0;
  50. ret = adjtimex(&tx);
  51. return ret;
  52. }
  53. /* Make sure we cleanup on ctrl-c */
  54. void handler(int unused)
  55. {
  56. clear_time_state();
  57. exit(0);
  58. }
  59. int main(void)
  60. {
  61. struct timex tx;
  62. struct timespec ts;
  63. time_t next_leap;
  64. int count = 0;
  65. setbuf(stdout, NULL);
  66. signal(SIGINT, handler);
  67. signal(SIGKILL, handler);
  68. printf("This runs for a few minutes. Press ctrl-c to stop\n");
  69. clear_time_state();
  70. /* Get the current time */
  71. clock_gettime(CLOCK_REALTIME, &ts);
  72. /* Calculate the next possible leap second 23:59:60 GMT */
  73. next_leap = ts.tv_sec;
  74. next_leap += 86400 - (next_leap % 86400);
  75. for (count = 0; count < 20; count++) {
  76. struct timeval tv;
  77. /* set the time to 2 seconds before the leap */
  78. tv.tv_sec = next_leap - 2;
  79. tv.tv_usec = 0;
  80. if (settimeofday(&tv, NULL)) {
  81. printf("Error: You're likely not running with proper (ie: root) permissions\n");
  82. return ksft_exit_fail();
  83. }
  84. tx.modes = 0;
  85. adjtimex(&tx);
  86. /* hammer on adjtime w/ STA_INS */
  87. while (tx.time.tv_sec < next_leap + 1) {
  88. /* Set the leap second insert flag */
  89. tx.modes = ADJ_STATUS;
  90. tx.status = STA_INS;
  91. adjtimex(&tx);
  92. }
  93. clear_time_state();
  94. printf(".");
  95. }
  96. printf("[OK]\n");
  97. return ksft_exit_pass();
  98. }