2 * Controller of read/write threads for virtio-trace
4 * Copyright (C) 2012 Hitachi, Ltd.
5 * Created by Yoshihiro Yunomae <yoshihiro.yunomae.ez@hitachi.com>
6 * Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
8 * Licensed under GPL version 2 only.
19 #include "trace-agent.h"
21 #define HOST_MSG_SIZE 256
22 #define EVENT_WAIT_MSEC 100
24 static volatile sig_atomic_t global_signal_val
;
25 bool global_sig_receive
; /* default false */
26 bool global_run_operation
; /* default false*/
28 /* Handle SIGTERM/SIGINT/SIGQUIT to exit */
29 static void signal_handler(int sig
)
31 global_signal_val
= sig
;
34 int rw_ctl_init(const char *ctl_path
)
38 ctl_fd
= open(ctl_path
, O_RDONLY
);
40 pr_err("Cannot open ctl_fd\n");
50 static int wait_order(int ctl_fd
)
52 struct pollfd poll_fd
;
55 while (!global_sig_receive
) {
57 poll_fd
.events
= POLLIN
;
59 ret
= poll(&poll_fd
, 1, EVENT_WAIT_MSEC
);
61 if (global_signal_val
) {
62 global_sig_receive
= true;
63 pr_info("Receive interrupt %d\n", global_signal_val
);
65 /* Wakes rw-threads when they are sleeping */
66 if (!global_run_operation
)
67 pthread_cond_broadcast(&cond_wakeup
);
74 pr_err("Polling error\n");
89 * contol read/write threads by handling global_run_operation
91 void *rw_ctl_loop(int ctl_fd
)
94 char buf
[HOST_MSG_SIZE
];
97 /* Setup signal handlers */
98 signal(SIGTERM
, signal_handler
);
99 signal(SIGINT
, signal_handler
);
100 signal(SIGQUIT
, signal_handler
);
102 while (!global_sig_receive
) {
104 ret
= wait_order(ctl_fd
);
108 rlen
= read(ctl_fd
, buf
, sizeof(buf
));
110 pr_err("read data error in ctl thread\n");
114 if (rlen
== 2 && buf
[0] == '1') {
116 * If host writes '1' to a control path,
117 * this controller wakes all read/write threads.
119 global_run_operation
= true;
120 pthread_cond_broadcast(&cond_wakeup
);
121 pr_debug("Wake up all read/write threads\n");
122 } else if (rlen
== 2 && buf
[0] == '0') {
124 * If host writes '0' to a control path, read/write
125 * threads will wait for notification from Host.
127 global_run_operation
= false;
128 pr_debug("Stop all read/write threads\n");
130 pr_info("Invalid host notification: %s\n", buf
);