Advertisement
EBobkunov

ex1 w6

Oct 16th, 2023
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <signal.h>
  5.  
  6. void sigusr1_handler(int signum) {
  7.     FILE *file = fopen("text.txt", "r");
  8.     if (file == NULL) {
  9.         perror("Failed to open text.txt");
  10.         exit(1);
  11.     }
  12.     char buffer[1024];
  13.     while (fgets(buffer, sizeof(buffer), file)) {
  14.         fputs(buffer, stdout);
  15.     }
  16.     fclose(file);
  17. }
  18.  
  19. void sigusr2_handler(int signum) {
  20.     printf("Process terminating...\n");
  21.     exit(0);
  22. }
  23.  
  24. int main() {
  25.     // Create and write the PID to /var/run/agent.pid
  26.     FILE *pid_file = fopen("/var/run/agent.pid", "w");
  27.     if (pid_file == NULL) {
  28.         perror("Failed to create /var/run/agent.pid");
  29.         exit(1);
  30.     }
  31.     fprintf(pid_file, "%d", getpid());
  32.     fclose(pid_file);
  33.  
  34.     // Set up signal handlers
  35.     signal(SIGUSR1, sigusr1_handler);
  36.     signal(SIGUSR2, sigusr2_handler);
  37.  
  38.     // Read and print the contents of text.txt
  39.     FILE *text_file = fopen("text.txt", "r");
  40.     if (text_file == NULL) {
  41.         perror("Failed to open text.txt");
  42.         exit(1);
  43.     }
  44.     char buffer[1024];
  45.     while (fgets(buffer, sizeof(buffer), text_file)) {
  46.         fputs(buffer, stdout);
  47.     }
  48.     fclose(text_file);
  49.  
  50.     // Sleep indefinitely
  51.     while (1) {
  52.         sleep(1);
  53.     }
  54.  
  55.     return 0;
  56. }
  57.  
  58.  
  59. #include <stdio.h>
  60. #include <stdlib.h>
  61. #include <unistd.h>
  62. #include <signal.h>
  63. #include <sys/types.h>
  64. #include <sys/stat.h>
  65. #include <fcntl.h>
  66.  
  67. int main() {
  68.     // Check if the agent exists by reading its PID from /var/run/agent.pid
  69.     int pid_file = open("/var/run/agent.pid", O_RDONLY);
  70.     if (pid_file == -1) {
  71.         printf("Error: No agent found.\n");
  72.         exit(1);
  73.     }
  74.    
  75.     char pid_buffer[10];
  76.     ssize_t bytes_read = read(pid_file, pid_buffer, sizeof(pid_buffer));
  77.     if (bytes_read <= 0) {
  78.         perror("Failed to read agent PID");
  79.         exit(1);
  80.     }
  81.     close(pid_file);
  82.     int agent_pid = atoi(pid_buffer);
  83.  
  84.     printf("Agent found.\n");
  85.  
  86.     while (1) {
  87.         char choice[20];
  88.         printf("Choose a command {\"read\", \"exit\", \"stop\", \"continue\"} to send to the agent: ");
  89.         scanf("%s", choice);
  90.  
  91.         if (strcmp(choice, "read") == 0) {
  92.             kill(agent_pid, SIGUSR1);
  93.         } else if (strcmp(choice, "exit") == 0) {
  94.             kill(agent_pid, SIGUSR2);
  95.             exit(0);
  96.         } else if (strcmp(choice, "stop") == 0) {
  97.             kill(agent_pid, SIGSTOP);
  98.         } else if (strcmp(choice, "continue") == 0) {
  99.             kill(agent_pid, SIGCONT);
  100.         } else {
  101.             printf("Invalid command.\n");
  102.         }
  103.     }
  104.  
  105.     return 0;
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement