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