1 // SPDX-License-Identifier: GPL-2.0-only
3 * Controller of read/write threads for virtio-trace
5 * Copyright (C) 2012 Hitachi, Ltd.
6 * Created by Yoshihiro Yunomae <yoshihiro.yunomae.ez@hitachi.com>
7 * Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
17 #include "trace-agent.h"
19 #define HOST_MSG_SIZE 256
20 #define EVENT_WAIT_MSEC 100
22 static volatile sig_atomic_t global_signal_val
;
23 bool global_sig_receive
; /* default false */
24 bool global_run_operation
; /* default false*/
26 /* Handle SIGTERM/SIGINT/SIGQUIT to exit */
27 static void signal_handler(int sig
)
29 global_signal_val
= sig
;
32 int rw_ctl_init(const char *ctl_path
)
36 ctl_fd
= open(ctl_path
, O_RDONLY
);
38 pr_err("Cannot open ctl_fd\n");
48 static int wait_order(int ctl_fd
)
50 struct pollfd poll_fd
;
53 while (!global_sig_receive
) {
55 poll_fd
.events
= POLLIN
;
57 ret
= poll(&poll_fd
, 1, EVENT_WAIT_MSEC
);
59 if (global_signal_val
) {
60 global_sig_receive
= true;
61 pr_info("Receive interrupt %d\n", global_signal_val
);
63 /* Wakes rw-threads when they are sleeping */
64 if (!global_run_operation
)
65 pthread_cond_broadcast(&cond_wakeup
);
72 pr_err("Polling error\n");
87 * contol read/write threads by handling global_run_operation
89 void *rw_ctl_loop(int ctl_fd
)
92 char buf
[HOST_MSG_SIZE
];
95 /* Setup signal handlers */
96 signal(SIGTERM
, signal_handler
);
97 signal(SIGINT
, signal_handler
);
98 signal(SIGQUIT
, signal_handler
);
100 while (!global_sig_receive
) {
102 ret
= wait_order(ctl_fd
);
106 rlen
= read(ctl_fd
, buf
, sizeof(buf
));
108 pr_err("read data error in ctl thread\n");
112 if (rlen
== 2 && buf
[0] == '1') {
114 * If host writes '1' to a control path,
115 * this controller wakes all read/write threads.
117 global_run_operation
= true;
118 pthread_cond_broadcast(&cond_wakeup
);
119 pr_debug("Wake up all read/write threads\n");
120 } else if (rlen
== 2 && buf
[0] == '0') {
122 * If host writes '0' to a control path, read/write
123 * threads will wait for notification from Host.
125 global_run_operation
= false;
126 pr_debug("Stop all read/write threads\n");
128 pr_info("Invalid host notification: %s\n", buf
);