leapcrash.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* Demo leapsecond deadlock
  2. * by: John Stultz ([email protected])
  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 possible
  8. * on kernels from 2.6.26 to 3.3.
  9. *
  10. * WARNING: THIS WILL LIKELY HARD HANG 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. #include "../kselftest.h"
  23. /* clear NTP time_status & time_state */
  24. int clear_time_state(void)
  25. {
  26. struct timex tx;
  27. int ret;
  28. /*
  29. * We have to call adjtime twice here, as kernels
  30. * prior to 6b1859dba01c7 (included in 3.5 and
  31. * -stable), had an issue with the state machine
  32. * and wouldn't clear the STA_INS/DEL flag directly.
  33. */
  34. tx.modes = ADJ_STATUS;
  35. tx.status = STA_PLL;
  36. ret = adjtimex(&tx);
  37. tx.modes = ADJ_STATUS;
  38. tx.status = 0;
  39. ret = adjtimex(&tx);
  40. return ret;
  41. }
  42. /* Make sure we cleanup on ctrl-c */
  43. void handler(int unused)
  44. {
  45. clear_time_state();
  46. exit(0);
  47. }
  48. int main(void)
  49. {
  50. struct timex tx;
  51. struct timespec ts;
  52. time_t next_leap;
  53. int count = 0;
  54. setbuf(stdout, NULL);
  55. signal(SIGINT, handler);
  56. signal(SIGKILL, handler);
  57. printf("This runs for a few minutes. Press ctrl-c to stop\n");
  58. clear_time_state();
  59. /* Get the current time */
  60. clock_gettime(CLOCK_REALTIME, &ts);
  61. /* Calculate the next possible leap second 23:59:60 GMT */
  62. next_leap = ts.tv_sec;
  63. next_leap += 86400 - (next_leap % 86400);
  64. for (count = 0; count < 20; count++) {
  65. struct timeval tv;
  66. /* set the time to 2 seconds before the leap */
  67. tv.tv_sec = next_leap - 2;
  68. tv.tv_usec = 0;
  69. if (settimeofday(&tv, NULL)) {
  70. printf("Error: You're likely not running with proper (ie: root) permissions\n");
  71. return ksft_exit_fail();
  72. }
  73. tx.modes = 0;
  74. adjtimex(&tx);
  75. /* hammer on adjtime w/ STA_INS */
  76. while (tx.time.tv_sec < next_leap + 1) {
  77. /* Set the leap second insert flag */
  78. tx.modes = ADJ_STATUS;
  79. tx.status = STA_INS;
  80. adjtimex(&tx);
  81. }
  82. clear_time_state();
  83. printf(".");
  84. fflush(stdout);
  85. }
  86. printf("[OK]\n");
  87. return ksft_exit_pass();
  88. }