daemon.c
changeset 76 c46968bddef5
child 77 49e0babccb23
equal deleted inserted replaced
75:a2c32869d4d5 76:c46968bddef5
       
     1 /**
       
     2  * test/demos/daemon.c
       
     3  * Copyright (C) 2008 Markus Broeker
       
     4  */
       
     5 
       
     6 #include <stdio.h>
       
     7 #include <stdlib.h>
       
     8 #include <string.h>
       
     9 #include <time.h>
       
    10 
       
    11 #include <fcntl.h>
       
    12 #include <unistd.h>
       
    13 #include <sys/stat.h>
       
    14 #include <sys/types.h>
       
    15 #include <signal.h>
       
    16 
       
    17 #define      MAXLEN 80
       
    18 #define    TIME_OUT 60
       
    19 #define  INPUT_FILE "/proc/meminfo"
       
    20 #define OUTPUT_FILE "/tmp/meminfo.log"
       
    21 #define   LOCK_FILE "/tmp/.daemon.lock"
       
    22 
       
    23 void check (int fd, char *buffer)
       
    24 {
       
    25     char response[1024];
       
    26     char time_stamp[40];
       
    27     int len;
       
    28 
       
    29     time_t t;
       
    30 
       
    31     t = time (NULL);
       
    32     strftime (time_stamp, sizeof (time_stamp), "%a %d %T", localtime (&t));
       
    33     len = snprintf (response, sizeof (response), "%s %s", time_stamp, buffer);
       
    34 
       
    35     write (fd, response, len);
       
    36 }
       
    37 
       
    38 void sigproc ()
       
    39 {
       
    40     if (unlink (LOCK_FILE) == 0)
       
    41         exit (EXIT_SUCCESS);
       
    42     exit (EXIT_FAILURE);
       
    43 }
       
    44 
       
    45 void sanity_check (int argc, char **argv)
       
    46 {
       
    47     struct stat st;
       
    48     int fd;
       
    49 
       
    50     if (argc == 2) {
       
    51         if (!strcmp (argv[1], "-h")) {
       
    52             printf ("Usage: %s [-f|]\n", argv[0]);
       
    53             printf ("Report bugs to mbroeker@largo.homelinux.org\n");
       
    54             exit (EXIT_SUCCESS);
       
    55         } else if (!strcmp (argv[1], "-f")) {
       
    56             if (unlink (LOCK_FILE) != 0)
       
    57                 exit (EXIT_FAILURE);
       
    58         } else {
       
    59             fprintf (stderr, "Unknown Option %s\n", argv[1]);
       
    60             exit (EXIT_FAILURE);
       
    61         }
       
    62     }
       
    63 
       
    64     if (stat (LOCK_FILE, &st) < 0) {
       
    65         fd = open (LOCK_FILE, O_WRONLY | O_CREAT, 0);
       
    66         close (fd);
       
    67     } else {
       
    68         fprintf (stderr, "%s is already running\n", argv[0]);
       
    69         exit (EXIT_FAILURE);
       
    70     }
       
    71 
       
    72     fprintf (stderr, "%s monitors %s every %d seconds into %s\n", argv[0], INPUT_FILE, TIME_OUT, OUTPUT_FILE);
       
    73 }
       
    74 
       
    75 int main (int argc, char **argv)
       
    76 {
       
    77     FILE *fin;
       
    78     static char buffer[MAXLEN];
       
    79     int fd;
       
    80 
       
    81     sanity_check (argc, argv);
       
    82 
       
    83     daemon (0, 0);
       
    84     signal (SIGTERM, sigproc);
       
    85 
       
    86     for (;;) {
       
    87         if ((fd = open (OUTPUT_FILE, O_WRONLY | O_CREAT, 0644)) < 0)
       
    88             return EXIT_FAILURE;
       
    89 
       
    90         if ((fin = fopen (INPUT_FILE, "r")) == NULL)
       
    91             return EXIT_FAILURE;
       
    92 
       
    93         while (fgets (buffer, sizeof (buffer), fin))
       
    94             check (fd, buffer);
       
    95 
       
    96         fclose (fin);
       
    97         close (fd);
       
    98 
       
    99         sleep (TIME_OUT);
       
   100     }
       
   101 
       
   102     return EXIT_SUCCESS;
       
   103 }