Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <signal.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <fcntl.h>
- void sigusr1_handler(int signum) {
- // Read and print the contents of text.txt
- char buffer[1024];
- int file_descriptor = open("text.txt", O_RDONLY);
- ssize_t bytes_read;
- while ((bytes_read = read(file_descriptor, buffer, sizeof(buffer))) > 0) {
- write(STDOUT_FILENO, buffer, bytes_read);
- }
- close(file_descriptor);
- }
- void sigusr2_handler(int signum) {
- // Print a termination message and exit
- printf("Process terminating...\n");
- exit(0);
- }
- void sigterm_handler(int signum) {
- // Print a termination message and exit
- printf("Process terminating...\n");
- exit(0);
- }
- int main() {
- // Create the agent.pid file and write the PID into it
- FILE* pid_file = fopen("/var/run/agent.pid", "w");
- if (pid_file == NULL) {
- perror("Error creating agent.pid file");
- exit(1);
- }
- fprintf(pid_file, "%d", getpid());
- fclose(pid_file);
- // Register signal handlers for SIGUSR1, SIGUSR2, and SIGTERM
- signal(SIGUSR1, sigusr1_handler);
- signal(SIGUSR2, sigusr2_handler);
- signal(SIGTERM, sigterm_handler);
- // Read and print the initial contents of text.txt
- char buffer[1024];
- int file_descriptor = open("text.txt", O_RDONLY);
- ssize_t bytes_read;
- while ((bytes_read = read(file_descriptor, buffer, sizeof(buffer))) > 0) {
- write(STDOUT_FILENO, buffer, bytes_read);
- }
- close(file_descriptor);
- // Sleep indefinitely
- while (1) {
- sleep(3600); // Sleep for a long time (1 hour)
- }
- return 0;
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <signal.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- int agent_pid;
- void sigint_handler(int signum) {
- // Send SIGTERM to the agent and exit
- if (agent_pid > 0) {
- kill(agent_pid, SIGTERM);
- }
- exit(0);
- }
- int main() {
- // Check if the agent.pid file exists
- struct stat stat_buffer;
- if (stat("/var/run/agent.pid", &stat_buffer) == -1) {
- perror("Error: No agent found.");
- exit(1);
- }
- // Try to open the agent.pid file to get the agent's PID
- FILE* pid_file = fopen("/var/run/agent.pid", "r");
- if (pid_file == NULL) {
- perror("Error reading agent PID.");
- exit(1);
- }
- fscanf(pid_file, "%d", &agent_pid);
- fclose(pid_file);
- printf("Agent found.\n");
- // Register the Ctrl+C signal handler
- signal(SIGINT, sigint_handler);
- while (1) {
- printf("Choose a command {\"read\", \"exit\", \"stop\", \"continue\"}: ");
- char command[20];
- scanf("%s", command);
- if (strcmp(command, "read") == 0) {
- // Send SIGUSR1 to the agent
- kill(agent_pid, SIGUSR1);
- } else if (strcmp(command, "exit") == 0) {
- // Send SIGUSR2 to the agent and exit
- kill(agent_pid, SIGUSR2);
- exit(0);
- } else if (strcmp(command, "stop") == 0) {
- // Send SIGSTOP to the agent
- kill(agent_pid, SIGSTOP);
- } else if (strcmp(command, "continue") == 0) {
- // Send SIGCONT to the agent
- kill(agent_pid, SIGCONT);
- } else {
- printf("Invalid command.\n");
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement