diff --git a/cronjob/chart/Makefile b/cronjob/chart/Makefile index 80ff0f8..691a01c 100644 --- a/cronjob/chart/Makefile +++ b/cronjob/chart/Makefile @@ -6,7 +6,7 @@ LDFLAS = -o INCL = -I$$(pg_config --includedir) BIN_NAME = chart -OBJS = chart.o config.o drawing/process_image.o +OBJS = chart.o config.o common.o image_file/image_file.o CONF_NAME = chart.conf @@ -29,9 +29,20 @@ $(BIN_NAME): $(OBJS) @ echo Binary $(BIN_NAME) ist fertig! # Abhängigkeiten -chart.o: chart.c definitions.h config.h chart.h drawing/process_image.h -config.o: config.c config.h definitions.h chart.h -drawing/process_image.o: drawing/process_image.c +chart.o: chart.c \ + definitions.h \ + config.h \ + chart.h \ + common.h \ + image_file/image_file.h +common.o: common.c \ + common.h +config.o: config.c \ + config.h \ + definitions.h \ + common.h +image_file/image_file.o: image_file/image_file.c \ + image_file/image_file.h # Compillieren $(OBJS): diff --git a/cronjob/chart/chart.c b/cronjob/chart/chart.c index fc94b49..7fb2388 100644 --- a/cronjob/chart/chart.c +++ b/cronjob/chart/chart.c @@ -25,23 +25,20 @@ #include #include #include -#include #include -#include #include "definitions.h" #include "config.h" #include "chart.h" -#include "drawing/process_image.h" +#include "common.h" +#include "image_file/image_file.h" static int walk_image_cfg_list(); static void wait_for_childs(); -static void exit_sig_handler(int); config global_opts; - static int walk_image_cfg_list(){ int has_forked = 0; pid_t pid = 0; @@ -50,22 +47,26 @@ static int walk_image_cfg_list(){ for(; tmp_ptr; tmp_ptr = tmp_ptr->next){ if(global_opts.fork){ if((pid = fork()) == 0){ - process_image(tmp_ptr->image_cfg_file); + clear_clean(); + process_image_cfg(tmp_ptr->image_cfg_file); exit(EXIT_SUCCESS); } else if (pid == -1){ exit_error(ERROR_FORK); } + if(!has_forked){ + add_clean(wait_for_childs, NULL); + } has_forked++; DEBUGOUT2("Prozess %d angelegt\n",pid); } else { - process_image(tmp_ptr->image_cfg_file); + process_image_cfg(tmp_ptr->image_cfg_file); } } return has_forked; } -static void wait_for_childs(){ +static void wait_for_childs(void *dummy){ int ret_val; pid_t pid; while((pid = wait(&ret_val)) != -1){ @@ -73,7 +74,6 @@ static void wait_for_childs(){ } } - int main(int argc, char *argv[]){ DEBUGOUT1("Programm gestartet\n"); @@ -104,37 +104,9 @@ int main(int argc, char *argv[]){ DEBUGOUT2(" Datenbank = %s\n",global_opts.pg_database); if(walk_image_cfg_list()) - wait_for_childs(); + wait_for_childs(NULL); return EXIT_SUCCESS; } -/* Diese Funktion beendet das Programm mit einer Fehlermeldung. */ -void exit_error(char* err){ - DEBUGOUT1("\nEtwas unschoenes ist passiert\n"); - if(errno != 0){ - perror("Fehler"); - } - write(STDOUT_FILENO, err, strlen(err)); - exit(1); -} - -/* Wird bei Beendigungssignalen ausgefuehrt */ -static void exit_sig_handler(int signr){ - #ifdef DEBUG - DEBUGOUT1("\n"); - switch (signr){ - case SIGABRT: - DEBUGOUT1("SIGABRT Interupt erhalten!\n"); - case SIGTERM: - DEBUGOUT1("SIGTERM Interupt erhalten!\n"); - case SIGQUIT: - DEBUGOUT1("SIGQUIT Interupt erhalten!\n"); - case SIGINT: - DEBUGOUT1("SIGINT Interupt erhalten!\n"); - } - #endif - DEBUGOUT1("Beende Programm...\n"); - exit(0); -} diff --git a/cronjob/chart/chart.h b/cronjob/chart/chart.h index 451465b..1254037 100644 --- a/cronjob/chart/chart.h +++ b/cronjob/chart/chart.h @@ -20,27 +20,5 @@ */ -typedef struct image_cfg_list_t *image_cfg_list_ptr; -typedef struct image_cfg_list_t { - char *image_cfg_file; - image_cfg_list_ptr next; -} image_cfg_list; - -typedef struct config_t { - char *pg_host; - char *pg_database; - char *pg_user; - char *pg_pass; - char *image_cfg_location; - image_cfg_list_ptr image_cfg; - int fork; -} config; - -extern config global_opts; -/* Funktionen -------------------------------------------------------------*/ - -/* Programm mit einem Fatalem Fehler beenden. - * Argument: Fehlermeldung */ -void exit_error(char*); diff --git a/cronjob/chart/common.c b/cronjob/chart/common.c new file mode 100644 index 0000000..04f7d00 --- /dev/null +++ b/cronjob/chart/common.c @@ -0,0 +1,112 @@ +/* + + common.c -- Part of Chart-generator for the weatherstation + + Copyright (C) 2006 Jan Losinski + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + + +#include +#include +#include +#include +#include +#include /* STDOUT_FILENO */ +#include "common.h" +#include "definitions.h" + +static void clean(); + + +clean_struct_ptr clean_ptr = NULL; + +/* Ein neues Clean-Element anfuegen. Ein Clean-Element ist eine Datenstruktur, die + * einen Pointer auf eine Funktion vom Typ + * void func(void *data), + * einen Zeiger auf beliebige Daten und einen Zeiger auf das naechste Element haelt. + * Die Funktionen werden beim regulaeren beenden des Programmes aufgerufen um zum bsp. + * datenbankverbindungen zu schließen, etc. */ +void add_clean(clean_func_t func, void *data){ + clean_struct_ptr temp = (clean_struct_ptr) malloc(sizeof(clean_data)); + + temp->data = data; + temp->func = func; + temp->next = NULL; + + if(clean_ptr == NULL){ + clean_ptr = temp; + } else { + temp->next = clean_ptr; + clean_ptr = temp; + } +} + +void remove_clean(){ + clean_struct_ptr temp = clean_ptr; + if (temp != NULL){ + clean_ptr = temp->next; + free(temp); + } +} + +void clear_clean(){ + while(clean_ptr != NULL){ + remove_clean(); + } +} + +/* 'Saeuberung' ausfuehren */ +static void clean(){ + DEBUGOUT1("\nRaeume auf...\n"); + clean_struct_ptr p = clean_ptr; + while(p != NULL){ + p->func(p->data); + p = p->next; + } +} + +/* Diese Funktion beendet das Programm mit einer Fehlermeldung. */ +void exit_error(char* err){ + DEBUGOUT1("\nEtwas unschoenes ist passiert\n"); + clean(); + if(errno != 0){ + perror("Fehler"); + } + write(STDOUT_FILENO, err, strlen(err)); + exit(1); +} + +/* Wird bei Beendigungssignalen ausgefuehrt */ +void exit_sig_handler(int signr){ + #ifdef DEBUG + DEBUGOUT1("\n"); + switch (signr){ + case SIGABRT: + DEBUGOUT1("SIGABRT Interupt erhalten!\n"); + case SIGTERM: + DEBUGOUT1("SIGTERM Interupt erhalten!\n"); + case SIGQUIT: + DEBUGOUT1("SIGQUIT Interupt erhalten!\n"); + case SIGINT: + DEBUGOUT1("SIGINT Interupt erhalten!\n"); + } + #endif + clean(); + DEBUGOUT1("Beende Programm...\n"); + exit(0); +} diff --git a/cronjob/chart/common.h b/cronjob/chart/common.h new file mode 100644 index 0000000..4310d1e --- /dev/null +++ b/cronjob/chart/common.h @@ -0,0 +1,68 @@ +/* + + common.h -- Part of Chart-generator for the weatherstation + + Copyright (C) 2006 Jan Losinski + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +typedef struct image_cfg_list_t *image_cfg_list_ptr; +typedef struct image_cfg_list_t { + char *image_cfg_file; + image_cfg_list_ptr next; +} image_cfg_list; + +typedef struct config_t { + char *pg_host; + char *pg_database; + char *pg_user; + char *pg_pass; + char *image_cfg_location; + image_cfg_list_ptr image_cfg; + int fork; +} config; + +typedef void (*clean_func_t)(void *data); + +typedef struct clean_struct *clean_struct_ptr; +typedef struct clean_struct { + void *data; + clean_func_t func; + clean_struct_ptr next; +} clean_data; + + + +extern config global_opts; + +/* Funktionen -------------------------------------------------------------*/ + +/* Programm mit einem Fatalem Fehler beenden. + * Argument: Fehlermeldung */ +void exit_error(char*); + +/* Signal-handler-Funktion zum beenden des Programmes */ +void exit_sig_handler(int); + +/* Eine (neuste) Clean-Funktion entfernen */ +void remove_clean(); + +/* Eine Clean-Funktion hinzufügen */ +void add_clean(clean_func_t , void *); + +/* Alle clean-Funktionen entfernen */ +void clear_clean(); diff --git a/cronjob/chart/config.c b/cronjob/chart/config.c index 8f3d654..e7f225f 100644 --- a/cronjob/chart/config.c +++ b/cronjob/chart/config.c @@ -32,7 +32,7 @@ #include "config.h" #include "definitions.h" -#include "chart.h" +#include "common.h" /* Funktionsdefinitionen */ static int read_int(const char *, void *); diff --git a/cronjob/chart/image_file/image_file.c b/cronjob/chart/image_file/image_file.c index e9220af..1a56084 100644 --- a/cronjob/chart/image_file/image_file.c +++ b/cronjob/chart/image_file/image_file.c @@ -21,9 +21,9 @@ */ #include #include -#include "process_image.h" +#include "image_file.h" -void process_image(char *image_cfg_file){ +void process_image_cfg(char *image_cfg_file){ printf("%s\n",image_cfg_file); sleep(3); } diff --git a/cronjob/chart/image_file/image_file.h b/cronjob/chart/image_file/image_file.h index 6f8e449..8f950fb 100644 --- a/cronjob/chart/image_file/image_file.h +++ b/cronjob/chart/image_file/image_file.h @@ -20,4 +20,4 @@ */ -void process_image(char *); +void process_image_cfg(char *); diff --git a/cronjob/chart/main.c b/cronjob/chart/main.c deleted file mode 100644 index a701ff3..0000000 --- a/cronjob/chart/main.c +++ /dev/null @@ -1,23 +0,0 @@ -#include -#include -#include -#include -#include - -int main(){ - int i; - pid_t p; - for(i=0; i<2; i++){ - if((p = fork()) == 0){ - sleep(10); - exit(0); - } - sleep(1); - printf("Process %i created\n",p); - } - while((p = wait(&i))!=-1){ - printf("pid: %i\n",p); - } -printf("alle Prozesse beendet\n"); - exit(0); -}