trace-agent-ctl.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Controller of read/write threads for virtio-trace
  4. *
  5. * Copyright (C) 2012 Hitachi, Ltd.
  6. * Created by Yoshihiro Yunomae <yoshihiro.yunomae.ez@hitachi.com>
  7. * Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
  8. */
  9. #define _GNU_SOURCE
  10. #include <fcntl.h>
  11. #include <poll.h>
  12. #include <signal.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <unistd.h>
  16. #include "trace-agent.h"
  17. #define HOST_MSG_SIZE 256
  18. #define EVENT_WAIT_MSEC 100
  19. static volatile sig_atomic_t global_signal_val;
  20. bool global_sig_receive; /* default false */
  21. bool global_run_operation; /* default false*/
  22. /* Handle SIGTERM/SIGINT/SIGQUIT to exit */
  23. static void signal_handler(int sig)
  24. {
  25. global_signal_val = sig;
  26. }
  27. int rw_ctl_init(const char *ctl_path)
  28. {
  29. int ctl_fd;
  30. ctl_fd = open(ctl_path, O_RDONLY);
  31. if (ctl_fd == -1) {
  32. pr_err("Cannot open ctl_fd\n");
  33. goto error;
  34. }
  35. return ctl_fd;
  36. error:
  37. exit(EXIT_FAILURE);
  38. }
  39. static int wait_order(int ctl_fd)
  40. {
  41. struct pollfd poll_fd;
  42. int ret = 0;
  43. while (!global_sig_receive) {
  44. poll_fd.fd = ctl_fd;
  45. poll_fd.events = POLLIN;
  46. ret = poll(&poll_fd, 1, EVENT_WAIT_MSEC);
  47. if (global_signal_val) {
  48. global_sig_receive = true;
  49. pr_info("Receive interrupt %d\n", global_signal_val);
  50. /* Wakes rw-threads when they are sleeping */
  51. if (!global_run_operation)
  52. pthread_cond_broadcast(&cond_wakeup);
  53. ret = -1;
  54. break;
  55. }
  56. if (ret < 0) {
  57. pr_err("Polling error\n");
  58. goto error;
  59. }
  60. if (ret)
  61. break;
  62. };
  63. return ret;
  64. error:
  65. exit(EXIT_FAILURE);
  66. }
  67. /*
  68. * contol read/write threads by handling global_run_operation
  69. */
  70. void *rw_ctl_loop(int ctl_fd)
  71. {
  72. ssize_t rlen;
  73. char buf[HOST_MSG_SIZE];
  74. int ret;
  75. /* Setup signal handlers */
  76. signal(SIGTERM, signal_handler);
  77. signal(SIGINT, signal_handler);
  78. signal(SIGQUIT, signal_handler);
  79. while (!global_sig_receive) {
  80. ret = wait_order(ctl_fd);
  81. if (ret < 0)
  82. break;
  83. rlen = read(ctl_fd, buf, sizeof(buf));
  84. if (rlen < 0) {
  85. pr_err("read data error in ctl thread\n");
  86. goto error;
  87. }
  88. if (rlen == 2 && buf[0] == '1') {
  89. /*
  90. * If host writes '1' to a control path,
  91. * this controller wakes all read/write threads.
  92. */
  93. global_run_operation = true;
  94. pthread_cond_broadcast(&cond_wakeup);
  95. pr_debug("Wake up all read/write threads\n");
  96. } else if (rlen == 2 && buf[0] == '0') {
  97. /*
  98. * If host writes '0' to a control path, read/write
  99. * threads will wait for notification from Host.
  100. */
  101. global_run_operation = false;
  102. pr_debug("Stop all read/write threads\n");
  103. } else
  104. pr_info("Invalid host notification: %s\n", buf);
  105. }
  106. return NULL;
  107. error:
  108. exit(EXIT_FAILURE);
  109. }