daemon.c
author Markus Bröker<broeker.markus@googlemail.com>
Fri, 20 Oct 2017 06:41:27 +0200
changeset 168 dfb60716880c
parent 138 dff18d1ac2af
permissions -rw-r--r--
Broken OSX Demo added
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
138
dff18d1ac2af Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents: 131
diff changeset
    19
76
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
#define  INPUT_FILE "/proc/meminfo"
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
#define OUTPUT_FILE "/tmp/meminfo.log"
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
#define   LOCK_FILE "/tmp/.daemon.lock"
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
void check (int fd, char *buffer)
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
{
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
    char response[1024];
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
    char time_stamp[40];
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
    int len;
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
    time_t t;
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
    t = time (NULL);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
    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
    34
    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
    35
131
b5ad49852adc check for the return values
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 79
diff changeset
    36
    if (write (fd, response, len) == -1)
b5ad49852adc check for the return values
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 79
diff changeset
    37
        perror ("write");
76
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
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
void sigproc ()
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
{
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
    if (unlink (LOCK_FILE) == 0)
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
        exit (EXIT_SUCCESS);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
    exit (EXIT_FAILURE);
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
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
void sanity_check (int argc, char **argv)
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    48
{
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    49
    struct stat st;
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
    int fd;
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    51
138
dff18d1ac2af Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents: 131
diff changeset
    52
    if (stat (INPUT_FILE, &st) != 0) {
dff18d1ac2af Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents: 131
diff changeset
    53
        perror (INPUT_FILE);
dff18d1ac2af Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents: 131
diff changeset
    54
        exit (EXIT_FAILURE);
dff18d1ac2af Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents: 131
diff changeset
    55
    }
dff18d1ac2af Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents: 131
diff changeset
    56
76
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    57
    if (argc == 2) {
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    58
        if (!strcmp (argv[1], "-h")) {
79
1499176750b9 Little Typo in daemon.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    59
            printf ("Usage: %s [-f|-h]\n", argv[0]);
76
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    60
            printf ("Report bugs to mbroeker@largo.homelinux.org\n");
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    61
            exit (EXIT_SUCCESS);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    62
        } else if (!strcmp (argv[1], "-f")) {
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    63
            if (unlink (LOCK_FILE) != 0)
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    64
                exit (EXIT_FAILURE);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    65
        } else {
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    66
            fprintf (stderr, "Unknown Option %s\n", argv[1]);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    67
            exit (EXIT_FAILURE);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    68
        }
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    69
    }
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    70
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    71
    if (stat (LOCK_FILE, &st) < 0) {
138
dff18d1ac2af Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents: 131
diff changeset
    72
        if ((fd = open (LOCK_FILE, O_WRONLY | O_CREAT, 0)) < 0) {
dff18d1ac2af Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents: 131
diff changeset
    73
            perror (LOCK_FILE);
dff18d1ac2af Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents: 131
diff changeset
    74
            exit (EXIT_FAILURE);
dff18d1ac2af Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents: 131
diff changeset
    75
        }
76
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    76
        close (fd);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    77
    } else {
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    78
        fprintf (stderr, "%s is already running\n", argv[0]);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    79
        exit (EXIT_FAILURE);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    80
    }
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
    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
    83
}
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    84
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    85
int main (int argc, char **argv)
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    86
{
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    87
    FILE *fin;
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    88
    static char buffer[MAXLEN];
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    89
    int fd;
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    90
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    91
    sanity_check (argc, argv);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    92
131
b5ad49852adc check for the return values
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 79
diff changeset
    93
    if (daemon (0, 0) == -1) {
b5ad49852adc check for the return values
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 79
diff changeset
    94
        perror ("daemon");
b5ad49852adc check for the return values
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 79
diff changeset
    95
        return EXIT_FAILURE;
b5ad49852adc check for the return values
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 79
diff changeset
    96
    }
76
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    97
    signal (SIGTERM, sigproc);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    98
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    99
    for (;;) {
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   100
        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
   101
            return EXIT_FAILURE;
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
        if ((fin = fopen (INPUT_FILE, "r")) == NULL)
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   104
            return EXIT_FAILURE;
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
        while (fgets (buffer, sizeof (buffer), fin))
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   107
            check (fd, buffer);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   108
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   109
        fclose (fin);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   110
        close (fd);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   111
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   112
        sleep (TIME_OUT);
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   113
    }
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   114
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   115
    return EXIT_SUCCESS;
c46968bddef5 A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   116
}