[Sysfence-commit] sysfence sysfence.c,1.12,1.13
Status: Alpha
Brought to you by:
emes
|
From: mkoperto <mko...@us...> - 2004-05-20 14:29:38
|
Update of /cvsroot/sysfence/sysfence In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21551/sysfence Modified Files: sysfence.c Log Message: change threads -> processes Index: sysfence.c =================================================================== RCS file: /cvsroot/sysfence/sysfence/sysfence.c,v retrieving revision 1.12 retrieving revision 1.13 diff -u -d -r1.12 -r1.13 --- sysfence.c 1 Mar 2004 17:28:10 -0000 1.12 +++ sysfence.c 20 May 2004 14:29:28 -0000 1.13 @@ -15,9 +15,12 @@ /* $Id$ */ #include <stdio.h> +#include <stdlib.h> #include <unistd.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <assert.h> #include "xalloc.h" -#include <pthread.h> #include "exit.h" #include "parseopt/lex.h" #include "getstats.h" @@ -26,6 +29,9 @@ #include "parseopt/confread.h" #include "mainloop.h" #include "log.h" +#include "communication.h" +#include "sighandlers.h" +#include "cp2memory.h" const char *usage = "sysfence v0.11, 01-03-2004\n" @@ -65,12 +71,14 @@ int main (int argc, char *argv[]) { - ruleset **ruletab; - int rulecount = 0; + ruleset **ruletab, **ruleshm; int i; + int ruletabsize; + + int pid; + + void *shm; - pthread_t *threads; - #ifdef DEBUG ruleset *rl; #endif @@ -82,15 +90,18 @@ if (*ruletab == NULL) bail_out (EXIT_NORULE, NULL); /* count rules */ + rulecount = 0; while (*(ruletab + rulecount) != NULL) rulecount ++; log_start (rulecount); #ifdef DEBUG + printf ("read %d rules.\n", rulecount); i = 0; rl = *(ruletab + i); + while (rl != NULL) { printf ("\nRULESET %d:\n", i); print_expr (rl->expr); @@ -113,27 +124,64 @@ /* open files for reading data and detach from console */ open_files (); - if (daemon (0, 0) != 0) bail_out (EXIT_IO, "daemon()"); - pthread_mutex_init (&changing_data, NULL); + //if (daemon (0, 0) != 0) bail_out (EXIT_IO, "daemon()"); - /* rule-watching threads start before data provider, so we must get fresh - * stats before starting them */ + /* initialize semaphore */ + semid = semaphore_init (SEMAPHORE_SET); + if (semid < 0) bail_out (EXIT_SEM, NULL); + + /* count ruletab size */ + ruletabsize = 0; + for (i=0; i<rulecount; i++) + ruletabsize += get_rule_size (*(ruletab+i)); + + /* initialize shared memory */ + shmid = shared_mem_init (STA_LAST * sizeof(statval) + + ruletabsize + + rulecount * sizeof (void *)); + if (shmid < 0) bail_out (EXIT_SHM, NULL); + shm = shared_mem_attach (shmid); + + stat = (statval *)shm; + + /* copy ruletab to shared memory */ + ruleshm = (ruleset **)(shm + STA_LAST * sizeof (statval)); + shm = (void *)(ruleshm + rulecount * sizeof (void *)); + + for (i=0; i<rulecount ;i++) { + *(ruleshm + i) = (ruleset *)cp_rule (&shm, *(ruletab + i)); + } + + /* rule-watching processes start before data provider, so we must get fresh + * stats before starting them */ fetch_la (); fetch_mem (); - /* one child for every ruleset */ - threads = (pthread_t *) xalloc (NULL, sizeof (pthread_t) * rulecount); - for (i = 0; i < rulecount; i ++) - pthread_create ( - threads + i, - NULL, - (void *) rule_watch_loop, - (void *) *(ruletab + i) - ); - - /* parent goes into data-providing loop */ - res_probe_loop (); + assert (rulecount); + ruleprocesses = (int *)xalloc (NULL, rulecount*sizeof(int)); + i = rulecount; + do { + pid = (int)fork (); + i--; + if (pid) *(ruleprocesses + i) = pid; + } while ((pid !=0) && (i!=0)); + + /* child */ + if (pid == 0) { + /* one child process one ruleset */ + rule_watch_loop (*(ruleshm + i)); + } else /* parent */ + { + /* start handling signals */ + signal_init (); + + /* parent goes into data-providing loop */ + res_probe_loop (); + } + + /* remove compilation warning */ + return 0; } /* $Id$ */ |