daemon.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Mon, 21 Jun 2010 01:09:18 +0200
changeset 135 f837cf975e95
parent 131 b5ad49852adc
child 138 dff18d1ac2af
permissions -rw-r--r--
DBClient: update, query and metadata committer: Markus Bröker <mbroeker@largo.homelinux.org>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
76
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     1
/**
77
49e0babccb23 HEADER TAGS
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 76
diff changeset
     2
 * daemon.c
76
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     3
 * Copyright (C) 2008 Markus Broeker
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
 */
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
#include <stdio.h>
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
#include <stdlib.h>
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
#include <string.h>
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
#include <time.h>
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
#include <fcntl.h>
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
#include <unistd.h>
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
#include <sys/stat.h>
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
#include <sys/types.h>
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
#include <signal.h>
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
#define      MAXLEN 80
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
#define    TIME_OUT 60
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
#define  INPUT_FILE "/proc/meminfo"
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
#define OUTPUT_FILE "/tmp/meminfo.log"
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
#define   LOCK_FILE "/tmp/.daemon.lock"
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
void check (int fd, char *buffer)
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
{
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
    char response[1024];
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
    char time_stamp[40];
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
    int len;
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
    time_t t;
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
    t = time (NULL);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
    strftime (time_stamp, sizeof (time_stamp), "%a %d %T", localtime (&t));
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
    len = snprintf (response, sizeof (response), "%s %s", time_stamp, buffer);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
131
b5ad49852adc check for the return values
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 79
diff changeset
    35
    if (write (fd, response, len) == -1)
b5ad49852adc check for the return values
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 79
diff changeset
    36
        perror ("write");
76
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
}
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
void sigproc ()
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
{
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
    if (unlink (LOCK_FILE) == 0)
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
        exit (EXIT_SUCCESS);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
    exit (EXIT_FAILURE);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
}
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
void sanity_check (int argc, char **argv)
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
{
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    48
    struct stat st;
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    49
    int fd;
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    51
    if (argc == 2) {
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    52
        if (!strcmp (argv[1], "-h")) {
79
1499176750b9 Little Typo in daemon.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    53
            printf ("Usage: %s [-f|-h]\n", argv[0]);
76
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    54
            printf ("Report bugs to mbroeker@largo.homelinux.org\n");
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
            exit (EXIT_SUCCESS);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    56
        } else if (!strcmp (argv[1], "-f")) {
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    57
            if (unlink (LOCK_FILE) != 0)
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    58
                exit (EXIT_FAILURE);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    59
        } else {
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    60
            fprintf (stderr, "Unknown Option %s\n", argv[1]);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    61
            exit (EXIT_FAILURE);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    62
        }
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    63
    }
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    64
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    65
    if (stat (LOCK_FILE, &st) < 0) {
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    66
        fd = open (LOCK_FILE, O_WRONLY | O_CREAT, 0);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    67
        close (fd);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    68
    } else {
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    69
        fprintf (stderr, "%s is already running\n", argv[0]);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    70
        exit (EXIT_FAILURE);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    71
    }
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    72
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    73
    fprintf (stderr, "%s monitors %s every %d seconds into %s\n", argv[0], INPUT_FILE, TIME_OUT, OUTPUT_FILE);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    74
}
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    75
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    76
int main (int argc, char **argv)
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    77
{
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    78
    FILE *fin;
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    79
    static char buffer[MAXLEN];
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    80
    int fd;
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    81
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    82
    sanity_check (argc, argv);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    83
131
b5ad49852adc check for the return values
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 79
diff changeset
    84
    if (daemon (0, 0) == -1) {
b5ad49852adc check for the return values
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 79
diff changeset
    85
        perror ("daemon");
b5ad49852adc check for the return values
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 79
diff changeset
    86
        return EXIT_FAILURE;
b5ad49852adc check for the return values
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 79
diff changeset
    87
    }
76
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    88
    signal (SIGTERM, sigproc);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    89
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    90
    for (;;) {
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    91
        if ((fd = open (OUTPUT_FILE, O_WRONLY | O_CREAT, 0644)) < 0)
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    92
            return EXIT_FAILURE;
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    93
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    94
        if ((fin = fopen (INPUT_FILE, "r")) == NULL)
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    95
            return EXIT_FAILURE;
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    96
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    97
        while (fgets (buffer, sizeof (buffer), fin))
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    98
            check (fd, buffer);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    99
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   100
        fclose (fin);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   101
        close (fd);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   102
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   103
        sleep (TIME_OUT);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   104
    }
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   105
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   106
    return EXIT_SUCCESS;
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   107
}