trace-agent-ctl.c 2.6 KB

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