author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Fri, 05 Mar 2010 22:03:16 +0100 | |
changeset 112 | 9bfb339a8156 |
parent 79 | 1499176750b9 |
child 131 | b5ad49852adc |
permissions | -rw-r--r-- |
76
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
1 |
/** |
77 | 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 |
|
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
35 |
write (fd, response, len); |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
36 |
} |
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 |
void sigproc () |
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 |
if (unlink (LOCK_FILE) == 0) |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
41 |
exit (EXIT_SUCCESS); |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
42 |
exit (EXIT_FAILURE); |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
43 |
} |
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 |
void sanity_check (int argc, char **argv) |
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 |
struct stat st; |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
48 |
int fd; |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
49 |
|
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
50 |
if (argc == 2) { |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
51 |
if (!strcmp (argv[1], "-h")) { |
79
1499176750b9
Little Typo in daemon.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
52 |
printf ("Usage: %s [-f|-h]\n", argv[0]); |
76
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
53 |
printf ("Report bugs to mbroeker@largo.homelinux.org\n"); |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
54 |
exit (EXIT_SUCCESS); |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
55 |
} else if (!strcmp (argv[1], "-f")) { |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
56 |
if (unlink (LOCK_FILE) != 0) |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
57 |
exit (EXIT_FAILURE); |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
58 |
} else { |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
59 |
fprintf (stderr, "Unknown Option %s\n", argv[1]); |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
60 |
exit (EXIT_FAILURE); |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
61 |
} |
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 |
if (stat (LOCK_FILE, &st) < 0) { |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
65 |
fd = open (LOCK_FILE, O_WRONLY | O_CREAT, 0); |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
66 |
close (fd); |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
67 |
} else { |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
68 |
fprintf (stderr, "%s is already running\n", argv[0]); |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
69 |
exit (EXIT_FAILURE); |
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 |
|
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
72 |
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
|
73 |
} |
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 |
int main (int argc, char **argv) |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
76 |
{ |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
77 |
FILE *fin; |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
78 |
static char buffer[MAXLEN]; |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
79 |
int fd; |
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 |
sanity_check (argc, argv); |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
82 |
|
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
83 |
daemon (0, 0); |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
84 |
signal (SIGTERM, sigproc); |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
85 |
|
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
86 |
for (;;) { |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
87 |
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
|
88 |
return EXIT_FAILURE; |
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 |
if ((fin = fopen (INPUT_FILE, "r")) == NULL) |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
91 |
return EXIT_FAILURE; |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
92 |
|
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
93 |
while (fgets (buffer, sizeof (buffer), fin)) |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
94 |
check (fd, buffer); |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
95 |
|
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
96 |
fclose (fin); |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
97 |
close (fd); |
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 |
sleep (TIME_OUT); |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
100 |
} |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
101 |
|
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
102 |
return EXIT_SUCCESS; |
c46968bddef5
A nice system daemon...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
103 |
} |