[Sysfence-commit] sysfence/sys Makefile,NONE,1.1 communication.c,NONE,1.1 communication.h,NONE,1.1 e
Status: Alpha
Brought to you by:
emes
|
From: mkoperto <mko...@us...> - 2004-05-26 17:30:54
|
Update of /cvsroot/sysfence/sysfence/sys In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11212 Added Files: Makefile communication.c communication.h exit.c exit.h log.c log.h processtitle.c processtitle.h sighandlers.c sighandlers.h xalloc.c xalloc.h Log Message: moved to new directory --- NEW FILE: processtitle.c --- /* copyright (c) 2004, Mirek Kopertowski <m.k...@po...> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2 as published by the Free Software Foundation (see file COPYING for 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. The idea is taken from sendmail. */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdarg.h> #include "processtitle.h" #define SPT_BUFSIZE 2048 #define max(A,B) ((A) > (B) ? (A) : (B)) extern char **environ; static char **arg; static int arglength; char buf[SPT_BUFSIZE]; int auxtitlelength; void initproctitle (int argc, char **argv) { int i; char **envp = environ; /* determine the size of environ */ for (i = 0; envp[i] != NULL; i++); /* copy environ to new place */ environ = (char **) malloc(sizeof(char *) * (i + 1)); for (i = 0; envp[i] != NULL; i++) environ[i] = strdup(envp[i]); environ[i] = NULL; /* determine the available space */ arg = argv; if (i > 0) arglength = envp[i-1] + strlen(envp[i-1]) - argv[0]; else arglength = argv[argc-1] + strlen(argv[argc-1]) - argv[0]; /* auxiliary title length */ auxtitlelength = max (strlen (STATE_STOPPED_NAME), strlen (STATE_EXEC_NAME)) + 3; } void setproctitle (int process, int state, const char *rulename) { static int length; switch (process) { case MAIN_PROCESS: length = strlen (MAIN_PROCESS_NAME); /* do nothing if title too long */ if (length + 1 > SPT_BUFSIZE) return; sprintf (buf, "%s", MAIN_PROCESS_NAME); break; case RULE_PROCESS: if (!rulename) return; length = auxtitlelength + strlen(rulename); /* do nothing if title too long */ if (length + 1 > SPT_BUFSIZE) return; switch (state) { case STATE_NORMAL: sprintf (buf, "%s '%s'", RULE_PROCESS_NAME, rulename); break; case STATE_STOPPED: sprintf (buf, "%s %s '%s'", RULE_PROCESS_NAME, STATE_STOPPED_NAME, rulename); break; case STATE_EXEC: sprintf (buf, "%s %s '%s'", RULE_PROCESS_NAME, STATE_EXEC_NAME, rulename); break; default: return; } break; default: return; } /* cut title if too long */ if (length > arglength - 2) { length = arglength - 2; buf[length] = '\0'; } /* clear the memory area */ memset (arg[0], '\0', arglength); strcpy (arg[0], buf); /* only one arg */ arg[1] = NULL; } --- NEW FILE: communication.h --- /* copyright (c) 2004, Mirek Kopertowski <m.k...@po...> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2 as published by the Free Software Foundation (see file COPYING for 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. */ #define SEMAPHORE_SET 1 #define SEMAPHORE_RESET 0 int semaphore_init (int val); int semaphore_op (int semid, int sem_op); int semaphore_del (int semid); #define semaphore_wait(S) semaphore_op (S, -1) #define semaphore_post(S) semaphore_op (S, 1) int shared_mem_init (int memsize); void *shared_mem_attach (int shmid); void shared_mem_detach (void *shm); void shared_mem_del (int shmid); --- NEW FILE: communication.c --- /* copyright (c) 2004, Mirek Kopertowski <m.k...@po...> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2 as published by the Free Software Foundation (see file COPYING for 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 <stdio.h> #include <sys/shm.h> #include <sys/stat.h> #include <sys/ipc.h> #include <sys/sem.h> #include <sys/types.h> union semun { int val; struct semid_ds *buf; unsigned short int *array; struct seminfo *__buf; }; int semaphore_op (int semid, int sem_op) { static struct sembuf operation[1]; operation[0].sem_num = 0; operation[0].sem_op = sem_op; operation[0].sem_flg = SEM_UNDO; return semop (semid, operation, 1); } int semaphore_init (int val) { int semid; union semun argument; unsigned short values[1]; /* create semaphore */ semid = semget (IPC_PRIVATE, 1, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR); values[0] = val; argument.array = values; /* set semaphore value */ semctl (semid, 0, SETALL, argument); return semid; } int semaphore_del (int semid) { union semun ignored_argument; /* delete semaphore - last process */ return semctl (semid, 1, IPC_RMID, ignored_argument); } int shared_mem_init (int memsize) { /* allocate shared memory */ return shmget (IPC_PRIVATE, memsize, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR); } void *shared_mem_attach (int shmid) { return shmat (shmid, NULL, 0); } void shared_mem_detach (void *shm) { shmdt (shm); } void shared_mem_del (int shmid) { shmctl (shmid, IPC_RMID, NULL); } --- NEW FILE: processtitle.h --- /* copyright (c) 2004, Mirek Kopertowski <m.k...@po...> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2 as published by the Free Software Foundation (see file COPYING for 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. The idea is taken from sendmail. */ #define STATE_NORMAL 0 #define STATE_EXEC 1 #define STATE_STOPPED 2 #define MAIN_PROCESS 1 #define RULE_PROCESS 2 #define MAIN_PROCESS_NAME "sffetch" #define RULE_PROCESS_NAME "sfwatch" #define STATE_EXEC_NAME "EXEC" #define STATE_STOPPED_NAME "STOPPED" void initproctitle (int argc, char **argv); void setproctitle (int process, int state, const char *rulename); --- NEW FILE: Makefile --- CC=gcc CFLAGS=-Wall -O2 all: $(CC) -c $(CFLAGS) exit.c -o exit.o $(CC) -c $(CFLAGS) log.c -o log.o $(CC) -c $(CFLAGS) xalloc.c -o xalloc.o $(CC) -c $(CFLAGS) communication.c -o communication.o $(CC) -c $(CFLAGS) processtitle.c -o processtitle.o $(CC) -c $(CFLAGS) sighandlers.c -o sighandlers.o debug: gcc -c -ggdb -DDEBUG exit.c -o exit.o gcc -c -ggdb -DDEBUG log.c -o log.o gcc -c -ggdb -DDEBUG xalloc.c -o xalloc.o gcc -c -ggdb -DDEBUG communication.c -o communication.o gcc -c -ggdb -DDEBUG processtitle.c -o processtitle.o gcc -c -ggdb -DDEBUG sighandlers.c -o sighandlers.o clean: rm -f *.o --- NEW FILE: log.c --- /* copyright (c) 2004, Michal Salaban <em...@pl...> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2 as published by the Free Software Foundation (see file COPYING for 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. $Id: log.c,v 1.1 2004/05/26 17:30:42 mkoperto Exp $ */ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <syslog.h> #include "xalloc.h" #include "../parseopt/lex.h" #include "../datastruct.h" #include "../getstats.h" #include "../conditions.h" #include "log.h" #define BIGSTRBUF 1024 char * stats_2_string (sf_list *hd) { char *gluebuf, *thisbuf, *nextbuf, *defbuf; sf_value val; if (!hd) return NULL; thisbuf = (char *) xalloc (NULL, STRBUF + 1); defbuf = def_2_string ((sf_stat_def *) hd->el); #ifdef DEBUG val = get_stat_value ((sf_stat_def *) hd->el, "stats_2_string()"); #else val = get_stat_value ((sf_stat_def *) hd->el); #endif #ifdef DEBUG syslog (LOG_DEBUG, "%s = type: %x, ptr: %x", defbuf, val.type, (long int) val.ptr); syslog (LOG_DEBUG, "+-- val: %x", *((long int *) val.ptr)); #endif nextbuf = stats_2_string (hd->next); switch (val.type) { case INTEGER: snprintf (thisbuf, STRBUF, "%s = %d", defbuf, *((long int *) val.ptr)); break; case DOUBLE: snprintf (thisbuf, STRBUF, "%s = %.2f", defbuf, *((double *) val.ptr)); break; default: #ifdef DEBUG syslog (LOG_DEBUG, "stats_2_string(): invalid val type: %d", val.type); #endif break; // to avoid warnings } free (defbuf); if (nextbuf) { gluebuf = (char *) xalloc (NULL, BIGSTRBUF + 1); snprintf (gluebuf, BIGSTRBUF, "%s, %s", thisbuf, nextbuf); free (thisbuf); free (nextbuf); return gluebuf; } else return thisbuf; } void log_start (int rules) { openlog ("sysfence", LOG_NDELAY | LOG_PID | LOG_CONS, LOG_DAEMON); syslog (LOG_INFO, "loaded %d rules", rules); } void log_rulehit (sf_rule *rule) { char *stattxt = stats_2_string (rule->watchstats); syslog (LOG_WARNING, "%s: %s", rule->name, stattxt); free (stattxt); } void log_end () { syslog (LOG_INFO, "exiting"); closelog (); } /* $Id: log.c,v 1.1 2004/05/26 17:30:42 mkoperto Exp $ */ --- NEW FILE: exit.h --- /* copyright (c) 2004, Michal Salaban <em...@pl...> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2 as published by the Free Software Foundation (see file COPYING for 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. $Id: exit.h,v 1.1 2004/05/26 17:30:42 mkoperto Exp $ */ #include <stdio.h> #define EXIT_OK 0 #define EXIT_IO 1 #define EXIT_MEM 2 #define EXIT_NOCONF 11 #define EXIT_NORULE 12 #define EXIT_PARSE 21 #define EXIT_VALUE 22 #define EXIT_OPTION 23 #define EXIT_SHM 31 #define EXIT_SEM 32 #define EXIT_BUG 41 void bail_out (int excode, const char *details); /* $Id: exit.h,v 1.1 2004/05/26 17:30:42 mkoperto Exp $ */ --- NEW FILE: xalloc.c --- /* copyright (c) 2004, Michal Salaban <em...@pl...> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2 as published by the Free Software Foundation (see file COPYING for 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. $Id: xalloc.c,v 1.1 2004/05/26 17:30:42 mkoperto Exp $ */ #include <stdlib.h> #include <stdio.h> #include "exit.h" void * xalloc (void * ptr, size_t size) { void *res; res = (void *) realloc (ptr, size); if (!res) bail_out (EXIT_MEM, NULL); return res; } /* $Id: xalloc.c,v 1.1 2004/05/26 17:30:42 mkoperto Exp $ */ --- NEW FILE: sighandlers.c --- /* copyright (c) 2004, Mirek Kopertowski <m.k...@po...> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2 as published by the Free Software Foundation (see file COPYING for 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 <signal.h> #include <sys/types.h> #include <sys/wait.h> #include <string.h> #include <stdlib.h> #include "exit.h" #include "../parseopt/lex.h" #include "../datastruct.h" #include "../getstats.h" #include "../conditions.h" #include "../mainloop.h" #include "communication.h" #include "sighandlers.h" #include "processtitle.h" struct sigaction schildact, stermact, susr1act; void schild_action (int snumber); void sterm_action (int snumber); void susr1_action (int snumber); void signal_init (int process) { switch (process) { case PARENT: memset (&schildact, 0, sizeof(schildact)); schildact.sa_handler = &schild_action; sigaction (SIGCHLD, &schildact, NULL); memset (&stermact, 0, sizeof(stermact)); stermact.sa_handler = &sterm_action; sigaction (SIGTERM, &stermact, NULL); return; case CHILD: memset (&susr1act, 0, sizeof(susr1act)); susr1act.sa_handler = &susr1_action; sigaction (SIGCHLD, &susr1act, NULL); memset (&stermact, 0, sizeof(stermact)); stermact.sa_handler = SIG_DFL; sigaction (SIGTERM, &stermact, NULL); return; default: return; } } void signals (int onoff) { switch (onoff) { case SIGON: susr1act.sa_handler = &susr1_action; sigaction (SIGUSR1, &susr1act, NULL); stermact.sa_handler = SIG_DFL; sigaction (SIGTERM, &stermact, NULL); return; case SIGOFF: susr1act.sa_handler = SIG_IGN; sigaction (SIGUSR1, &susr1act, NULL); stermact.sa_handler = SIG_IGN; sigaction (SIGTERM, &stermact, NULL); return; default: return; } } /* local functions -----------------------------------------------------------*/ void schild_action (int snumber) { int exitstatus, pid, i=0; pid = (int)wait (&exitstatus); while (*(ruleprocesses + i)!=pid) i++; *(ruleprocesses + i) = 0; } void sterm_action (int snumber) { int i; /* deactive handling SIGCHLD */ memset (&schildact, 0, sizeof(schildact)); schildact.sa_handler = SIG_IGN; sigaction (SIGCHLD, &schildact, NULL); /* terminate child processes */ for (i=0; i<rulecount; i++) if (*(ruleprocesses + i)) kill ((pid_t)*(ruleprocesses + i), SIGTERM); /* detach, deallocate shared memory */ shared_mem_detach (db_shm); shared_mem_del (db_shmid); shared_mem_detach (fs_shm); shared_mem_del (fs_shmid); shared_mem_detach (proc_shm); shared_mem_del (proc_shmid); shared_mem_detach (rules_shm); shared_mem_del (rules_shmid); /* deallocate semaphore */ semaphore_del (semid); bail_out (EXIT_OK, NULL); } void susr1_action (int snumber) { //setproctitle (RULE_PROCESS, STATE_STOPPED, rule->name); raise (SIGSTOP); } --- NEW FILE: xalloc.h --- /* copyright (c) 2004, Michal Salaban <em...@pl...> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2 as published by the Free Software Foundation (see file COPYING for 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. $Id: xalloc.h,v 1.1 2004/05/26 17:30:42 mkoperto Exp $ */ void * xalloc (void * ptr, size_t size); /* $Id: xalloc.h,v 1.1 2004/05/26 17:30:42 mkoperto Exp $ */ --- NEW FILE: log.h --- /* copyright (c) 2004, Michal Salaban <em...@pl...> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2 as published by the Free Software Foundation (see file COPYING for 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. $Id: log.h,v 1.1 2004/05/26 17:30:42 mkoperto Exp $ */ #define LOGLINEBUF 256 #define LOGVARBUF 64 void log_start (int rules); void log_rulehit (sf_rule *rule); void log_end (); /* $Id: log.h,v 1.1 2004/05/26 17:30:42 mkoperto Exp $ */ --- NEW FILE: sighandlers.h --- /* copyright (c) 2004, Mirek Kopertowski <m.k...@po...> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2 as published by the Free Software Foundation (see file COPYING for 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. */ #define PARENT 0 #define CHILD 1 #define SIGON 0 #define SIGOFF 1 void signal_init (); void signals (int onoff); --- NEW FILE: exit.c --- /* copyright (c) 2004, Michal Salaban <em...@pl...> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2 as published by the Free Software Foundation (see file COPYING for 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. $Id: exit.c,v 1.1 2004/05/26 17:30:42 mkoperto Exp $ */ #include "exit.h" #include <stdio.h> #include <stdlib.h> #include <syslog.h> char *errormessage[] = { [EXIT_IO] = "I/O error on %s", [EXIT_MEM] = "Out of memory", [EXIT_NOCONF] = "No configuration given", [EXIT_NORULE] = "No rules given", [EXIT_PARSE] = "Parse error: %s", [EXIT_VALUE] = "Invalid value: %s", [EXIT_OPTION] = "Invalid option: %s", [EXIT_SHM] = "Shared memory exists", [EXIT_SEM] = "Semaphore exists", [EXIT_BUG] = "Internal bug" }; extern char *usage; void bail_out (int excode, const char *details) { #ifdef DEBUG fprintf (stderr, "bail_out() exit code %d\n", excode); #endif if (excode == EXIT_OK) exit (EXIT_OK); if (details != NULL) fprintf (stderr, errormessage[excode], details); else fprintf (stderr, errormessage[excode]); fprintf (stderr, "\n"); if ((excode == EXIT_NOCONF) || (excode == EXIT_VALUE) || (excode == EXIT_OPTION)) fprintf (stderr, usage); if (excode == EXIT_BUG) syslog (LOG_DEBUG, "%s %s", errormessage[excode], details); exit (excode); } /* $Id: exit.c,v 1.1 2004/05/26 17:30:42 mkoperto Exp $ */ |