daemon.c
changeset 138 dff18d1ac2af
parent 131 b5ad49852adc
equal deleted inserted replaced
137:d8a0984b72fc 138:dff18d1ac2af
    14 #include <sys/types.h>
    14 #include <sys/types.h>
    15 #include <signal.h>
    15 #include <signal.h>
    16 
    16 
    17 #define      MAXLEN 80
    17 #define      MAXLEN 80
    18 #define    TIME_OUT 60
    18 #define    TIME_OUT 60
       
    19 
    19 #define  INPUT_FILE "/proc/meminfo"
    20 #define  INPUT_FILE "/proc/meminfo"
    20 #define OUTPUT_FILE "/tmp/meminfo.log"
    21 #define OUTPUT_FILE "/tmp/meminfo.log"
    21 #define   LOCK_FILE "/tmp/.daemon.lock"
    22 #define   LOCK_FILE "/tmp/.daemon.lock"
    22 
    23 
    23 void check (int fd, char *buffer)
    24 void check (int fd, char *buffer)
    46 void sanity_check (int argc, char **argv)
    47 void sanity_check (int argc, char **argv)
    47 {
    48 {
    48     struct stat st;
    49     struct stat st;
    49     int fd;
    50     int fd;
    50 
    51 
       
    52     if (stat (INPUT_FILE, &st) != 0) {
       
    53         perror (INPUT_FILE);
       
    54         exit (EXIT_FAILURE);
       
    55     }
       
    56 
    51     if (argc == 2) {
    57     if (argc == 2) {
    52         if (!strcmp (argv[1], "-h")) {
    58         if (!strcmp (argv[1], "-h")) {
    53             printf ("Usage: %s [-f|-h]\n", argv[0]);
    59             printf ("Usage: %s [-f|-h]\n", argv[0]);
    54             printf ("Report bugs to mbroeker@largo.homelinux.org\n");
    60             printf ("Report bugs to mbroeker@largo.homelinux.org\n");
    55             exit (EXIT_SUCCESS);
    61             exit (EXIT_SUCCESS);
    61             exit (EXIT_FAILURE);
    67             exit (EXIT_FAILURE);
    62         }
    68         }
    63     }
    69     }
    64 
    70 
    65     if (stat (LOCK_FILE, &st) < 0) {
    71     if (stat (LOCK_FILE, &st) < 0) {
    66         fd = open (LOCK_FILE, O_WRONLY | O_CREAT, 0);
    72         if ((fd = open (LOCK_FILE, O_WRONLY | O_CREAT, 0)) < 0) {
       
    73             perror (LOCK_FILE);
       
    74             exit (EXIT_FAILURE);
       
    75         }
    67         close (fd);
    76         close (fd);
    68     } else {
    77     } else {
    69         fprintf (stderr, "%s is already running\n", argv[0]);
    78         fprintf (stderr, "%s is already running\n", argv[0]);
    70         exit (EXIT_FAILURE);
    79         exit (EXIT_FAILURE);
    71     }
    80     }