Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <signal.h>
- void sigusr1_handler(int signum) {
- FILE *file = fopen("text.txt", "r");
- if (file == NULL) {
- perror("Failed to open text.txt");
- exit(1);
- }
- char buffer[1024];
- while (fgets(buffer, sizeof(buffer), file)) {
- fputs(buffer, stdout);
- }
- fclose(file);
- }
- void sigusr2_handler(int signum) {
- printf("Process terminating...\n");
- exit(0);
- }
- int main() {
- // Create and write the PID to /var/run/agent.pid
- FILE *pid_file = fopen("/var/run/agent.pid", "w");
- if (pid_file == NULL) {
- perror("Failed to create /var/run/agent.pid");
- exit(1);
- }
- fprintf(pid_file, "%d", getpid());
- fclose(pid_file);
- // Set up signal handlers
- signal(SIGUSR1, sigusr1_handler);
- signal(SIGUSR2, sigusr2_handler);
- // Read and print the contents of text.txt
- FILE *text_file = fopen("text.txt", "r");
- if (text_file == NULL) {
- perror("Failed to open text.txt");
- exit(1);
- }
- char buffer[1024];
- while (fgets(buffer, sizeof(buffer), text_file)) {
- fputs(buffer, stdout);
- }
- fclose(text_file);
- // Sleep indefinitely
- while (1) {
- sleep(1);
- }
- return 0;
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <signal.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- int main() {
- // Check if the agent exists by reading its PID from /var/run/agent.pid
- int pid_file = open("/var/run/agent.pid", O_RDONLY);
- if (pid_file == -1) {
- printf("Error: No agent found.\n");
- exit(1);
- }
- char pid_buffer[10];
- ssize_t bytes_read = read(pid_file, pid_buffer, sizeof(pid_buffer));
- if (bytes_read <= 0) {
- perror("Failed to read agent PID");
- exit(1);
- }
- close(pid_file);
- int agent_pid = atoi(pid_buffer);
- printf("Agent found.\n");
- while (1) {
- char choice[20];
- printf("Choose a command {\"read\", \"exit\", \"stop\", \"continue\"} to send to the agent: ");
- scanf("%s", choice);
- if (strcmp(choice, "read") == 0) {
- kill(agent_pid, SIGUSR1);
- } else if (strcmp(choice, "exit") == 0) {
- kill(agent_pid, SIGUSR2);
- exit(0);
- } else if (strcmp(choice, "stop") == 0) {
- kill(agent_pid, SIGSTOP);
- } else if (strcmp(choice, "continue") == 0) {
- kill(agent_pid, SIGCONT);
- } else {
- printf("Invalid command.\n");
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement