1 /*
2 * Copyright (C) 2022 Intel Corporation.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6 
7 #include <stdio.h>
8 #include <fcntl.h>
9 #include <signal.h>
10 
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 
14 #include "ivshmemlib.h"
15 
16 #define BUFFERSIZE 256
17 
18 //Used for reading output from cyclictest
19 int data_pipe = -1;
20 
21 //Used for handling signals
22 void sig_handler(int);
23 
main(void)24 int main(void)
25 {
26 
27 	//First make sure the user is root
28 	if (geteuid() != 0) {
29 
30 		printf("You need to run this program as root!!!\n");
31 		return failure;
32 
33 	}
34 
35 	//Used for holding the data from cyclictest
36 	char data_buffer[BUFFERSIZE] = {0};
37 
38 	//Used for sanitizing the data
39 	char *start_stat = NULL;
40 
41 	//Set up signal handler for when we get interrupt
42 	signal(SIGINT, sig_handler);
43 
44 	//Used for reading output from cyclictest
45 	data_pipe = open("data_pipe", O_RDWR);
46 	if (data_pipe == failure) {
47 
48 		perror("Failed to open a fifo pipe");
49 		return failure;
50 
51 	}
52 
53 	//Open the shared memory region
54 	if (setup_ivshmem_region("/sys/class/uio/uio0/device/resource2") == failure) {
55 
56 		perror("Failed to open the shared memory region");
57 		close(data_pipe);
58 		return failure;
59 
60 	}
61 
62 	//Loop forever, reading and writing the data from cyclictest to the uservm
63 	while (1) {
64 
65 		//Read the data
66 		bzero(data_buffer, BUFFERSIZE);
67 		read(data_pipe, data_buffer, BUFFERSIZE - 1);
68 
69 		//Get the sample stat
70 		start_stat = strstr(data_buffer, "T:");
71 		if (start_stat != NULL)
72 
73 			//Send the data
74 			write_ivshmem_region(start_stat, BUFFERSIZE);
75 
76 	}
77 
78 	//Close the shared memory region and the data pipe now that we don't need them
79 	close_ivshmem_region();
80 	close(data_pipe);
81 
82 	return success;
83 }
84 
85 /*
86 void sig_handler(int signum)
87 input: int - the signal value
88 
89 This function will get run when a signal is sent to the process and will gracefully
90 shut down the program
91 */
sig_handler(int signum)92 void sig_handler(int signum)
93 {
94 
95 	fprintf(stderr, "Received signal: %d\n", signum);
96 
97 	//Close the shared memory region and the data pipe now that we don't need them
98 	close_ivshmem_region();
99 	if (data_pipe != -1)
100 		close(data_pipe);
101 
102 	exit(-1);
103 
104 }
105