[Sysfence-commit] sysfence datastruct.c,NONE,1.1 datastruct.h,1.1,1.2
Status: Alpha
Brought to you by:
emes
|
From: Michal S. <em...@us...> - 2004-05-26 04:07:23
|
Update of /cvsroot/sysfence/sysfence In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30999 Modified Files: datastruct.h Added Files: datastruct.c Log Message: + added functions that work on sf_* structures Index: datastruct.h =================================================================== RCS file: /cvsroot/sysfence/sysfence/datastruct.h,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- datastruct.h 23 May 2004 20:53:01 -0000 1.1 +++ datastruct.h 26 May 2004 04:07:13 -0000 1.2 @@ -14,14 +14,31 @@ */ +/*************************************************************************** + * * + * BIG RED WARNING! * + * * + * Changing anything? * + * Be sure that functions in... * + * datastruct.c * + * cp2memory.c * + * ...will work properly! * + * * + * Other source files may also require adjustment :( * + * * + ***************************************************************************/ + #include <sys/types.h> -#define STAT_MAX_ARGS 2 +#define STRBUF 256 -#define PROC_RUN 1 -#define PROC_SLEEP 2 -#define PROC_STOP 4 -#define PROC_ZOMBIE 8 +#define STAT_MAX_ARGS 2 + +#define PROC_RUN 1 +#define PROC_SLEEP 2 +#define PROC_STOP 4 +#define PROC_ZOMBIE 8 +#define PROC_ANY 15 /*************************************************************************** All-purpose list @@ -137,6 +154,9 @@ // indicate if expression was true after this check and previous one int hit, prevhit; + // All stats that are watched by this rule, organized in list + // (for fast logging) + sf_list *watchstats; } sf_rule; /*************************************************************************** @@ -158,4 +178,13 @@ void *proc; // NIY } sf_database; +/*************************************************************************** + Helper functions operating on sf_* structures + ***************************************************************************/ + +void add_def_to_list (sf_list **hd, sf_stat_def *def); +int equal_defs (sf_stat_def *d1, sf_stat_def *d2); +int uid_in_list (sf_list *hd, uid_t uid); +char * def_2_string (sf_stat_def *def); + /* $Id$ */ --- NEW FILE: datastruct.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: datastruct.c,v 1.1 2004/05/26 04:07:13 emes Exp $ */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include "xalloc.h" #include "parseopt/lex.h" #include "datastruct.h" #ifdef DEBUG #include <syslog.h> #endif void add_def_to_list (sf_list **hd, sf_stat_def *def) { /* adds stat def to list, discarding duplicates */ #ifdef DEBUG syslog (LOG_DEBUG, "adding def: %s @ %x", def_2_string (def), *hd); #endif if (! *hd) { /* this is tail */ sf_list *new = (sf_list *) xalloc (NULL, sizeof (sf_list)); new->elsize = sizeof (sf_stat_def); new->el = (void *) def; new->next = NULL; *hd = new; } else { /* this is existing entry */ sf_stat_def *cur = (sf_stat_def *) (*hd)->el; /* if we're carrying a duplicate, drop it */ if (equal_defs (def, cur)) return; else add_def_to_list (&(*hd)->next, def); } } int uid_in_list (sf_list *hd, uid_t uid) { /* checks if uid is in the list */ if (!hd) return 0; if (*((uid_t *)hd->el) != uid) return 0; return uid_in_list (hd->next, uid); } int equal_uid_lists (sf_list *l1, sf_list *l2) { /* * This function works in 2 * |l1| * |l2| time * but is used during parsing process and for short lists (usually) * so, there is no need to code it better. am i right? ;> */ sf_list *lp1 = l1, *lp2 = l2; while (lp1) { if (! uid_in_list (l2, *((uid_t *)lp1->el))) return 0; lp1 = lp1->next; } while (lp2) { if (! uid_in_list (l1, *((uid_t *)lp2->el))) return 0; lp2 = lp2->next; } return 1; } int equal_defs (sf_stat_def *d1, sf_stat_def *d2) { /* checks if two defs describe the same stat */ #ifdef DEBUG syslog (LOG_DEBUG, "checking [%s] == [%s]", def_2_string (d1), def_2_string (d2) ); #endif if (d1->label != d2->label) return 0; switch (d1->label) { case ST_LOAD: return (d1->arg[0].laminutes == d2->arg[0].laminutes); case ST_MEM: case ST_SWAP: return (d1->arg[0].resstat == d2->arg[0].resstat); case ST_FS: return ((d1->arg[0].resstat == d2->arg[0].resstat) && strcmp (d1->arg[1].path, d2->arg[1].path)); case ST_PROC: return ((d1->arg[1].procstates == d2->arg[1].procstates) && equal_uid_lists (d1->arg[0].uids, d2->arg[0].uids)); default: return 1; } } char * res_state_2_string (sf_res_state sta) { switch (sta) { case VA_USED: return "used"; case VA_FREE: return "free"; case VA_AVAIL: return "available"; default: return ""; } } char * def_2_string (sf_stat_def *def) { char *buf = (char *) xalloc (NULL, STRBUF + 1); *buf = '\0'; switch (def->label) { case ST_LOAD: snprintf (buf, STRBUF, "loadavg(%d)", def->arg[0].laminutes ); break; case ST_MEM: snprintf (buf, STRBUF, "%s memory", res_state_2_string (def->arg[0].resstat) ); break; case ST_SWAP: snprintf (buf, STRBUF, "%s swap", res_state_2_string (def->arg[0].resstat) ); break; case ST_FS: snprintf (buf, STRBUF, "%s space in %s", res_state_2_string (def->arg[0].resstat), def->arg[1].path ); break; case ST_PROC: snprintf (buf, STRBUF, "nproc (uid: %s) (state: %s)", "NIY", "NIY" ); default: break; // to avoid warnings } return buf; } |