Advertisement
J3st3rs_j0k3

pr7_zaebalsya

Dec 17th, 2023
628
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <signal.h>
  4. #include <unistd.h>
  5.  
  6. #define LOG_FILE "/user/home/prg.log"
  7.  
  8. int hup_count = 0;
  9.  
  10. void sighup_handler(int signum) {
  11.     hup_count++;
  12.     FILE *file = fopen(LOG_FILE, "a");
  13.     if (file == NULL) {
  14.         perror("Error opening file");
  15.         exit(EXIT_FAILURE);
  16.     }
  17.     fprintf(file, "%d\n", hup_count);
  18.     fclose(file);
  19. }
  20.  
  21. void sigusr1_handler(int signum) {
  22.     FILE *file = fopen(LOG_FILE, "r");
  23.     if (file == NULL) {
  24.         perror("Error opening file");
  25.         exit(EXIT_FAILURE);
  26.     }
  27.     int value;
  28.     while (fscanf(file, "%d", &value) != EOF) {
  29.         printf("%d\n", value);
  30.     }
  31.     fclose(file);
  32. }
  33.  
  34. void sigterm_handler(int signum) {
  35.     if (remove(LOG_FILE) == -1) {
  36.         perror("Error removing file");
  37.         exit(EXIT_FAILURE);
  38.     }
  39.     exit(EXIT_SUCCESS);
  40. }
  41.  
  42. int main() {
  43.     signal(SIGHUP, sighup_handler);
  44.     signal(SIGUSR1, sigusr1_handler);
  45.     signal(SIGTERM, sigterm_handler);
  46.     while (1) {
  47.         sleep(1);
  48.     }
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement