rtc_user.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020 Intel Corporation
  4. * Author: Johannes Berg <[email protected]>
  5. */
  6. #include <stdbool.h>
  7. #include <os.h>
  8. #include <errno.h>
  9. #include <sched.h>
  10. #include <unistd.h>
  11. #include <kern_util.h>
  12. #include <sys/select.h>
  13. #include <stdio.h>
  14. #include <sys/timerfd.h>
  15. #include "rtc.h"
  16. static int uml_rtc_irq_fds[2];
  17. void uml_rtc_send_timetravel_alarm(void)
  18. {
  19. unsigned long long c = 1;
  20. CATCH_EINTR(write(uml_rtc_irq_fds[1], &c, sizeof(c)));
  21. }
  22. int uml_rtc_start(bool timetravel)
  23. {
  24. int err;
  25. if (timetravel) {
  26. int err = os_pipe(uml_rtc_irq_fds, 1, 1);
  27. if (err)
  28. goto fail;
  29. } else {
  30. uml_rtc_irq_fds[0] = timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC);
  31. if (uml_rtc_irq_fds[0] < 0) {
  32. err = -errno;
  33. goto fail;
  34. }
  35. /* apparently timerfd won't send SIGIO, use workaround */
  36. sigio_broken(uml_rtc_irq_fds[0]);
  37. err = add_sigio_fd(uml_rtc_irq_fds[0]);
  38. if (err < 0) {
  39. close(uml_rtc_irq_fds[0]);
  40. goto fail;
  41. }
  42. }
  43. return uml_rtc_irq_fds[0];
  44. fail:
  45. uml_rtc_stop(timetravel);
  46. return err;
  47. }
  48. int uml_rtc_enable_alarm(unsigned long long delta_seconds)
  49. {
  50. struct itimerspec it = {
  51. .it_value = {
  52. .tv_sec = delta_seconds,
  53. },
  54. };
  55. if (timerfd_settime(uml_rtc_irq_fds[0], 0, &it, NULL))
  56. return -errno;
  57. return 0;
  58. }
  59. void uml_rtc_disable_alarm(void)
  60. {
  61. uml_rtc_enable_alarm(0);
  62. }
  63. void uml_rtc_stop(bool timetravel)
  64. {
  65. if (timetravel)
  66. os_close_file(uml_rtc_irq_fds[1]);
  67. else
  68. ignore_sigio_fd(uml_rtc_irq_fds[0]);
  69. os_close_file(uml_rtc_irq_fds[0]);
  70. }