A nice system daemon...
authorMarkus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:50:39 +0200
changeset 76 c46968bddef5
parent 75 a2c32869d4d5
child 77 49e0babccb23
A nice system daemon... Daemon monitors /proc/meminfo every 60 seconds... committer: Markus Bröker <mbroeker@largo.homelinux.org>
Makefile
daemon.c
--- a/Makefile
+++ b/Makefile
@@ -63,6 +63,7 @@
 TARGET += unicode
 TARGET += fts
 TARGET += clplaner
+TARGET += daemon
 
 .SUFFIXES: .c .cc .asm
 
@@ -291,20 +292,20 @@
 	@$(CC) -o $@ $<
 
 unicode: unicode.c
-	@echo Compiling $<...
-	@$(CC) -c $(CFLAGS) -std=c99 $<
-	@echo Linking $< ...
-	@$(CC) -std=c99 -o $@ $<
+	@echo Linking $<...
+	@$(CC) $(CFLAGS) -std=c99 -o $@ $<
 
 fts: fts.c
-	@echo Compiling \(NON-ANSI\) $<...
-	@$(CC) -c -Wall -O2 $<
 	@echo Linking $< ...
+	@$(CC) -Wall -O2 $< -o $@
+
+clplaner: clplaner.o
+	@echo Linking $<...
 	@$(CC) -o $@ $<
 
-clplaner: clplaner.o
-	@echo Compiling $<...
-	@$(CC) -o $@ $<
+daemon: daemon.c
+	@echo Linking $<...
+	@$(CC) -Wall -O2 -g -ggdb $< -o $@
 
 .PHONY: beauty clean uninstall
 
new file mode 100644
--- /dev/null
+++ b/daemon.c
@@ -0,0 +1,103 @@
+/**
+ * test/demos/daemon.c
+ * Copyright (C) 2008 Markus Broeker
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <signal.h>
+
+#define      MAXLEN 80
+#define    TIME_OUT 60
+#define  INPUT_FILE "/proc/meminfo"
+#define OUTPUT_FILE "/tmp/meminfo.log"
+#define   LOCK_FILE "/tmp/.daemon.lock"
+
+void check (int fd, char *buffer)
+{
+    char response[1024];
+    char time_stamp[40];
+    int len;
+
+    time_t t;
+
+    t = time (NULL);
+    strftime (time_stamp, sizeof (time_stamp), "%a %d %T", localtime (&t));
+    len = snprintf (response, sizeof (response), "%s %s", time_stamp, buffer);
+
+    write (fd, response, len);
+}
+
+void sigproc ()
+{
+    if (unlink (LOCK_FILE) == 0)
+        exit (EXIT_SUCCESS);
+    exit (EXIT_FAILURE);
+}
+
+void sanity_check (int argc, char **argv)
+{
+    struct stat st;
+    int fd;
+
+    if (argc == 2) {
+        if (!strcmp (argv[1], "-h")) {
+            printf ("Usage: %s [-f|]\n", argv[0]);
+            printf ("Report bugs to mbroeker@largo.homelinux.org\n");
+            exit (EXIT_SUCCESS);
+        } else if (!strcmp (argv[1], "-f")) {
+            if (unlink (LOCK_FILE) != 0)
+                exit (EXIT_FAILURE);
+        } else {
+            fprintf (stderr, "Unknown Option %s\n", argv[1]);
+            exit (EXIT_FAILURE);
+        }
+    }
+
+    if (stat (LOCK_FILE, &st) < 0) {
+        fd = open (LOCK_FILE, O_WRONLY | O_CREAT, 0);
+        close (fd);
+    } else {
+        fprintf (stderr, "%s is already running\n", argv[0]);
+        exit (EXIT_FAILURE);
+    }
+
+    fprintf (stderr, "%s monitors %s every %d seconds into %s\n", argv[0], INPUT_FILE, TIME_OUT, OUTPUT_FILE);
+}
+
+int main (int argc, char **argv)
+{
+    FILE *fin;
+    static char buffer[MAXLEN];
+    int fd;
+
+    sanity_check (argc, argv);
+
+    daemon (0, 0);
+    signal (SIGTERM, sigproc);
+
+    for (;;) {
+        if ((fd = open (OUTPUT_FILE, O_WRONLY | O_CREAT, 0644)) < 0)
+            return EXIT_FAILURE;
+
+        if ((fin = fopen (INPUT_FILE, "r")) == NULL)
+            return EXIT_FAILURE;
+
+        while (fgets (buffer, sizeof (buffer), fin))
+            check (fd, buffer);
+
+        fclose (fin);
+        close (fd);
+
+        sleep (TIME_OUT);
+    }
+
+    return EXIT_SUCCESS;
+}