Changeset 140 for trunk/src


Ignore:
Timestamp:
Oct 29, 2007, 1:09:10 AM (17 years ago)
Author:
rainer
Message:

Utility function for threaded modules.

Location:
trunk/src
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/samhain.c

    r137 r140  
    16371637  for (modnum = 0; modList[modnum].name != NULL; ++modnum)
    16381638    {
    1639       if (0 != (status = modList[modnum].mod_init()))
     1639      if ( 0 != (status = modList[modnum].mod_init(&(modList[modnum]))) )
    16401640        {
    16411641          if (status == (-1)) {
     
    17951795              for (modnum = 0; modList[modnum].name != NULL; ++modnum)
    17961796                {
    1797                   if (0 != (status = modList[modnum].mod_init()))
     1797                  if (0 != (status = modList[modnum].mod_init(&(modList[modnum]))))
    17981798                    {
    17991799                      if (status == (-1)) {
  • trunk/src/sh_error.c

    r133 r140  
    5656#include "sh_forward.h"
    5757#include "sh_prelude.h"
     58#include "sh_pthread.h"
    5859
    5960#if defined(WITH_DATABASE)
     
    857858 **********************************************************/
    858859
     860SH_MUTEX_RECURSIVE(mutex_err_handle);
     861
    859862void sh_error_handle (int sev, char * file, long line,
    860863                      long status, unsigned long msg_id, ...)
     
    868871
    869872  int    flag_inet  = S_FALSE;
     873
     874#ifdef SH_WITH_SERVER
    870875  int    class_inet = clt_class;      /* initialize from global */
    871 
    872 #ifdef SH_WITH_SERVER
    873876  char   local_inet_peer[SH_MINIBUF];
    874877#endif
     
    908911  SL_ENTER(_("sh_error_handle"));
    909912
     913  SH_MUTEX_RECURSIVE_INIT(mutex_err_handle);
     914  SH_MUTEX_RECURSIVE_LOCK(mutex_err_handle);
     915
    910916#ifdef SH_WITH_SERVER
    911917  /* copy the global string into a local array
     
    918924  else
    919925    local_inet_peer[0] = '\0';
    920 #endif
    921926
    922927  clt_class = (-1);      /* reset global */
     928#endif
     929
    923930
    924931  if (own_block == 1)
    925932    {
    926       SL_RET0(_("sh_error_handle"));
     933      goto exit_here;
    927934    }
    928935
    929936  /* --- Initialize to default values. ---
    930937   */
    931   own_block = 1;
    932938  if (IsInitialized == BAD)
    933939    (void) sh_error_init();
    934   own_block = 0;
    935 
    936   /* --- Consistency checks. ---
    937    */
    938   own_block = 1;
     940
     941  /* Returns pointer to (constant|thread-specific) static memory
     942   */
    939943  fmt = /*@i@*/get_format (msg_id, &severity, &class);
    940   own_block = 0;
    941 
     944
     945#ifdef SH_WITH_SERVER
    942946  if (class_inet != (-1))
    943947    class = (unsigned int) class_inet;
    944 
     948#endif
     949
     950  /* --- Consistency check. ---
     951   */
    945952  ASSERT((fmt != NULL), _("fmt != NULL"))
    946953  if (fmt == NULL)
     
    949956              _("ERROR: msg=<NULL format>, file=<%s>, line=<%ld>\n"),
    950957              file, line);
    951       SL_RET0(_("sh_error_handle"));
     958      goto exit_here;
    952959    }
    953960
     
    9951002       )
    9961003    {
    997       SL_RET0(_("sh_error_handle"));
     1004      goto exit_here;
    9981005    }
    9991006
    10001007  if ((severity & SH_ERR_NOT) != 0)
    10011008    {
    1002       SL_RET0(_("sh_error_handle"));
     1009      goto exit_here;
    10031010    }
    10041011
     
    10171024  lmsg->line     = line;
    10181025  lmsg->status   = status;
    1019   own_block = 0;
    1020 
    10211026
    10221027  /* Format the log message with timestamp etc.
    10231028   * Allocate lmsg->msg
    10241029   */
    1025   own_block = 1;
    10261030  va_start (vl, msg_id);
    10271031  (void) sh_error_string (lmsg, vl);
     
    13111315  SH_FREE( lmsg );
    13121316  own_block = 0;
     1317
     1318 exit_here:
     1319
     1320  SH_MUTEX_RECURSIVE_UNLOCK(mutex_err_handle);
    13131321
    13141322  /*@i@*/SL_RET0(_("sh_error_handle"));
  • trunk/src/sh_kern.c

    r137 r140  
    17541754static int AddressReconf = 0;
    17551755
    1756 int sh_kern_init ()
     1756int sh_kern_init (struct mod_type * arg)
    17571757{
    17581758#if defined(HOST_IS_LINUX)
     
    17601760  char         * str;
    17611761#endif
     1762  (void) arg;
    17621763
    17631764  SL_ENTER(_("sh_kern_init"));
  • trunk/src/sh_mem.c

    r139 r140  
    256256          eblock = 1;
    257257          (void) safe_logger (0, 0, NULL);
    258           /*
    259           sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MMEM,
    260                            file, line);
    261           */
    262258          eblock = 0;
    263259        }
     
    348344          eblock = 1;
    349345          (void) safe_logger(0, 0, NULL);
    350           /*
    351           sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MMEM,
    352                            file, line);
    353           */
    354346          eblock = 0;
    355347        }
     
    491483          eblock = 1;
    492484          (void) safe_logger(0, 0, NULL);
    493           /*
    494           sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MMEM);
    495           */
    496485          eblock = 0;
    497486        }
  • trunk/src/sh_modules.c

    r68 r140  
    77
    88#include "sh_modules.h"
     9#include "sh_pthread.h"
    910
    1011#include "sh_utmp.h"
     
    2930    N_("[Utmp]"),
    3031    sh_utmp_table,
     32    PTHREAD_MUTEX_INITIALIZER,
    3133  },
    3234#endif
     
    4446    N_("[Mounts]"),
    4547    sh_mounts_table,
     48    PTHREAD_MUTEX_INITIALIZER,
    4649  },
    4750#endif
     
    5962    N_("[UserFiles]"),
    6063    sh_userfiles_table,
     64    PTHREAD_MUTEX_INITIALIZER,
    6165  },
    6266#endif
     
    7478    N_("[Kernel]"),
    7579    sh_kern_table,
     80    PTHREAD_MUTEX_INITIALIZER,
    7681  },
    7782#endif
     
    8994    N_("[SuidCheck]"),
    9095    sh_suidchk_table,
     96    PTHREAD_MUTEX_INITIALIZER,
    9197  },
    9298#endif
     
    104110    N_("[ProcessCheck]"),
    105111    sh_prochk_table,
     112    PTHREAD_MUTEX_INITIALIZER,
    106113  },
    107114#endif
     
    119126    N_("[PortCheck]"),
    120127    sh_portchk_table,
     128    PTHREAD_MUTEX_INITIALIZER,
    121129  },
    122130#endif
     
    134142    NULL,
    135143    NULL,
     144    PTHREAD_MUTEX_INITIALIZER,
    136145  },
    137146};
  • trunk/src/sh_mounts.c

    r68 r140  
    165165 * This is called once at the start of each samhain run.
    166166 * Non-configuration setup code should be placed here. */
    167 int sh_mounts_init ()
    168 {
     167int sh_mounts_init (struct mod_type * arg)
     168{
     169  (void) arg;
    169170  SL_ENTER(_("sh_mounts_init"));
    170171
  • trunk/src/sh_portcheck.c

    r137 r140  
    815815#endif
    816816
    817 int sh_portchk_init ()
     817int sh_portchk_init (struct mod_type * arg)
    818818{
    819819  struct hostent * hent;
    820820  int              i = 0;
    821821  char errbuf[256];
     822  (void) arg;
    822823
    823824  if (portchk_debug)
  • trunk/src/sh_processcheck.c

    r132 r140  
    11501150/* Initialise.
    11511151 */
    1152 int sh_prochk_init(void)
    1153 {
     1152int sh_prochk_init(struct mod_type * arg)
     1153{
     1154  (void) arg;
    11541155  SL_ENTER(_("sh_prochk_init"));
    11551156
  • trunk/src/sh_pthread.c

    r139 r140  
    22
    33#include "sh_pthread.h"
     4#include "sh_modules.h"
    45
    56#ifdef HAVE_PTHREAD
     7
     8#include <signal.h>
     9#include "sh_calls.h"
     10
    611SH_MUTEX_INIT(mutex_skey,   PTHREAD_MUTEX_INITIALIZER);
    712SH_MUTEX_INIT(mutex_resolv, PTHREAD_MUTEX_INITIALIZER);
     
    1520}
    1621
     22int sh_pthread_init_threadspecific()
     23{
     24  int rc = 0;
     25#ifdef SH_STEALTH
     26  extern int sh_g_thread();
     27
     28  rc = sh_g_thread();
     29  if (rc != 0)
     30    return rc;
     31#endif
     32
     33  return rc;
     34}
     35
     36
     37/*
     38 *  ----  Utilities for modules  ----
     39 */
     40
     41/* MODULES: init()
     42 *             -- starts thread_run() function if threaded
     43 *             -- fallback on internal_init if threading fails
     44 *             -- and returns MODULE_INACTIVE/MODULE_ACTIVE/MODULE_THREADED
     45 *
     46 *                int retval = MODULE_INACTIVE;
     47 *                if (0 != sh_pthread_create(thread_run, NULL))
     48 *                   return internal_init();
     49 *                return MODULE_THREADED;
     50 *
     51 *          thread_run()
     52 *             -- calls internal init,
     53 *             -- polls timer,
     54 *             -- runs module check,
     55 *             -- runs sh_pthread_testcancel()
     56 *             -- returns (return == exit)
     57 */
     58
     59#define SH_NUM_THREADS 16
     60static pthread_t threads[SH_NUM_THREADS];
     61static int       ithread[SH_NUM_THREADS];
     62static pthread_mutex_t  create_mutex = PTHREAD_MUTEX_INITIALIZER;
     63
     64int sh_pthread_create(void *(*start_routine)(void*), void *arg)
     65{
     66  int rc, nthread = 1;
     67  sigset_t signal_set;
     68  int retval = 0;
     69
     70  pthread_mutex_lock(&create_mutex);
     71
     72  /* block all signals
     73   */
     74  sigfillset( &signal_set );
     75  pthread_sigmask( SIG_BLOCK, &signal_set, NULL );
     76
     77  /* find a free slot in threads[]
     78   */
     79  while (nthread < SH_NUM_THREADS)
     80    {
     81      if (ithread[nthread] == 0)
     82        break;
     83      ++nthread;
     84      if (nthread == SH_NUM_THREADS)
     85        {
     86          retval = -1;
     87          goto err_out;
     88        }
     89    }
     90
     91  rc = pthread_create(&threads[nthread], NULL, start_routine, arg);
     92  if (rc != 0)
     93    {
     94      retval = -1;
     95      goto err_out;
     96    }
     97
     98  ithread[nthread] = 1;
     99
     100 err_out:
     101  pthread_sigmask( SIG_UNBLOCK, &signal_set, NULL );
     102  pthread_mutex_unlock(&create_mutex);
     103  return retval;
     104}
     105
     106int sh_pthread_cancel_all()
     107{
     108  int i;
     109  int ret = 0;
     110
     111  SH_MUTEX_LOCK(create_mutex);
     112
     113  for (i = 1; i < SH_NUM_THREADS; ++i)
     114    {
     115      if (ithread[i] != 0)
     116        if (0 != pthread_cancel(threads[i]))
     117          ithread[i] = 0;
     118    }
     119
     120  for (i = 1; i < SH_NUM_THREADS; ++i)
     121    {
     122      if (ithread[i] != 0)
     123        pthread_join(threads[i], NULL);
     124      ithread[i] = 0;
     125    }
     126
     127  SH_MUTEX_UNLOCK(create_mutex);
     128  return ret;
     129}
     130
     131/* ---- Utility functions for modules ----
     132 */
     133void sh_threaded_module_reconf(void *arg)
     134{
     135  sh_mtype * this_module = (sh_mtype *) arg;
     136  this_module->mod_reconf();
     137  return;
     138}
     139
     140void sh_threaded_module_run(void *arg)
     141{
     142  sh_mtype * this_module = (sh_mtype *) arg;
     143
     144  /* First we lock the module. This ensures that it cannot be
     145   * run twice.
     146   */
     147  pthread_cleanup_push(sh_pthread_mutex_unlock, (void*) &(this_module->mod_mutex));
     148  pthread_mutex_lock(&(this_module->mod_mutex));
     149
     150  if (0 == sh_pthread_init_threadspecific())
     151    {
     152
     153      if (0 == this_module->mod_init(NULL))
     154        {
     155          pthread_cleanup_push(sh_threaded_module_reconf, arg);
     156
     157          while (1)
     158            {
     159              if (0 != this_module->mod_timer(time(NULL)))
     160                {
     161                  this_module->mod_check();
     162                  pthread_testcancel();
     163                  retry_msleep(1,0);
     164                }
     165            }
     166
     167          pthread_cleanup_pop(1); /* notreached,but required */
     168        }
     169    }
     170
     171  pthread_cleanup_pop(1);
     172
     173  return;
     174}
     175
     176
     177/*
     178 *  ----  Implementation of recursive mutexes from libxml2  ----
     179 */
    17180#if !defined(HAVE_PTHREAD_MUTEX_RECURSIVE)
    18181/**
     
    21184 * Gary Pennington <Gary.Pennington@uk.sun.com>
    22185 * daniel@veillard.com
    23  */
    24 
     186 
     187 * Except where otherwise noted in the source code (e.g. the files hash.c,
     188 * list.c and the trio files, which are covered by a similar licence but
     189 * with different Copyright notices) all the files are:
     190 *
     191 *    Copyright (C) 1998-2003 Daniel Veillard.  All Rights Reserved.
     192 *
     193 * Permission is hereby granted, free of charge, to any person obtaining a copy
     194 * of this software and associated documentation files (the "Software"), to deal
     195 * in the Software without restriction, including without limitation the rights
     196 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     197 * copies of the Software, and to permit persons to whom the Software is fur-
     198 * nished to do so, subject to the following conditions:
     199 *
     200 * The above copyright notice and this permission notice shall be included in
     201 * all copies or substantial portions of the Software.
     202 *
     203 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     204 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FIT-
     205 * NESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
     206 * DANIEL VEILLARD BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
     207 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CON-
     208 * NECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     209 *
     210 * Except as contained in this notice, the name of Daniel Veillard shall not
     211 * be used in advertising or otherwise to promote the sale, use or other deal-
     212 * ings in this Software without prior written authorization from him.
     213 */
     214
     215/* Modified NewRMutex -> InitRMutex. We use a static structure, rather than
     216 * allocating one. Also dropped code for non-POSIX OSes.
     217 */
    25218void sh_InitRMutex(struct sh_RMutex * tok)
    26219{
  • trunk/src/sh_suidchk.c

    r138 r140  
    11721172 *
    11731173 *************/
    1174 int sh_suidchk_init ()
    1175 {
     1174int sh_suidchk_init (struct mod_type * arg)
     1175{
     1176  (void) arg;
    11761177  if (ShSuidchkActive == S_FALSE)
    11771178    return (-1);
  • trunk/src/sh_userfiles.c

    r137 r140  
    246246/* Build the list of users, then use this to construct the filenames to
    247247 * be checked. */
    248 int sh_userfiles_init(void) {
     248int sh_userfiles_init(struct mod_type * arg) {
    249249    struct passwd *cur_user;
    250250    struct userhomeslist *end;
    251251    struct userhomeslist *new;
    252252    struct userhomeslist *homes;
     253    (void) arg;
    253254
    254255    SL_ENTER(_("sh_userfiles_init"));
  • trunk/src/sh_utmp.c

    r132 r140  
    461461 *
    462462 *************/
    463 int sh_utmp_init ()
     463int sh_utmp_init (struct mod_type * arg)
    464464{
    465465  static int done = 0;
     466  (void) arg;
    466467
    467468  SL_ENTER(_("sh_utmp_init"));
Note: See TracChangeset for help on using the changeset viewer.