Code

Merge branch 'collectd-4.2' into collectd-4.3
authorFlorian Forster <octo@leeloo.lan.home.verplant.org>
Sat, 23 Feb 2008 09:58:09 +0000 (10:58 +0100)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Sat, 23 Feb 2008 09:58:09 +0000 (10:58 +0100)
1  2 
configure.in
src/exec.c

diff --combined configure.in
index f59fc6b00500b35718d836ec3b07f7b4b50b1283,07c9b4270f16d7df4192bb933ca756e8eb1cecc9..69ee32264037321f530ac359e48faf573aa8291d
@@@ -17,7 -17,6 +17,7 @@@ AC_PROG_CP
  AC_PROG_INSTALL
  AC_PROG_LN_S
  AC_PROG_MAKE_SET
 +AM_PROG_CC_C_O
  AM_CONDITIONAL(COMPILER_IS_GCC, test "x$GCC" = "xyes")
  
  dnl configure libtool
@@@ -311,7 -310,7 +311,7 @@@ AC_CHECK_HEADERS(linux/un.h, [], []
  #endif
  ])
  
 -AC_CHECK_HEADERS(pwd.h grp.h sys/un.h ctype.h limits.h sys/quota.h xfs/xqm.h fs_info.h fshelp.h paths.h mntent.h mnttab.h sys/fstyp.h sys/fs_types.h sys/mntent.h sys/mnttab.h sys/statfs.h sys/statvfs.h sys/vfs.h sys/vfstab.h kvm.h)
 +AC_CHECK_HEADERS(pwd.h grp.h sys/un.h ctype.h limits.h sys/quota.h xfs/xqm.h fs_info.h fshelp.h paths.h mntent.h mnttab.h sys/fstyp.h sys/fs_types.h sys/mntent.h sys/mnttab.h sys/statfs.h sys/statvfs.h sys/vfs.h sys/vfstab.h kvm.h wordexp.h)
  
  # For the dns plugin
  AC_CHECK_HEADERS(arpa/nameser.h)
@@@ -372,8 -371,7 +372,7 @@@ AC_CHECK_FUNCS(gettimeofday select strd
  
  AC_FUNC_STRERROR_R
  
- AC_CHECK_FUNCS(getpwnam_r)
- AC_CHECK_FUNCS(getgrnam_r)
+ AC_CHECK_FUNCS(getpwnam_r getgrnam_r setgroups regcomp regerror regexec regfree)
  
  socket_needs_socket="no"
  AC_CHECK_FUNCS(socket, [], AC_CHECK_LIB(socket, socket, [socket_needs_socket="yes"], AC_MSG_ERROR(cannot find socket)))
@@@ -383,8 -381,6 +382,6 @@@ nanosleep_needs_rt="no
  AC_CHECK_FUNCS(nanosleep, [], AC_CHECK_LIB(rt, nanosleep, [nanosleep_needs_rt="yes"], AC_MSG_ERROR(cannot find nanosleep)))
  AM_CONDITIONAL(BUILD_WITH_LIBRT, test "x$nanosleep_needs_rt" = "xyes")
  
- AC_CHECK_FUNCS(regcomp regerror regexec regfree)
  AC_CHECK_FUNCS(sysctlbyname, [have_sysctlbyname="yes"], [have_sysctlbyname="no"])
  AC_CHECK_FUNCS(host_statistics, [have_host_statistics="yes"], [have_host_statistics="no"])
  AC_CHECK_FUNCS(processor_info, [have_processor_info="yes"], [have_processor_info="no"])
@@@ -670,20 -666,6 +667,20 @@@ AC_CHECK_LIB(resolv, res_search
  [with_libresolv="no"])
  AM_CONDITIONAL(BUILD_WITH_LIBRESOLV, test "x$with_libresolv" = "xyes")
  
 +dnl Check for HAL (hardware abstraction library)
 +with_libhal="yes"
 +AC_CHECK_LIB(hal,libhal_device_property_exists,
 +           [AC_DEFINE(HAVE_LIBHAL, 1, [Define to 1 if you have 'hal' library])],
 +           [with_libhal="no"])
 +if test "x$with_libhal" = "xyes"; then
 +      PKG_PROG_PKG_CONFIG
 +      if test "x$PKG_CONFIG" != "x"; then
 +              BUILD_WITH_LIBHAL_CFLAGS="`pkg-config --cflags hal`"
 +              BUILD_WITH_LIBHAL_LIBS="`pkg-config --libs hal`"
 +              AC_SUBST(BUILD_WITH_LIBHAL_CFLAGS)
 +              AC_SUBST(BUILD_WITH_LIBHAL_LIBS)
 +      fi
 +fi
  
  m4_divert_once([HELP_WITH], [
  collectd additional packages:])
@@@ -698,8 -680,6 +695,8 @@@ AC_ARG_WITH(rrdtool, [AS_HELP_STRING([-
                librrd_cflags="-I$withval/include"
                librrd_ldflags="-L$withval/lib"
                with_rrdtool="yes"
 +      else
 +              with_rrdtool="$withval"
        fi
  ], [with_rrdtool="yes"])
  if test "x$with_rrdtool" = "xyes"
@@@ -885,7 -865,7 +882,7 @@@ AC_CHECK_LIB(IOKit, IOServiceGetMatchin
  [
        with_libiokit="yes"
        collectd_libiokit=1
 -], 
 +],
  [
        with_libiokit="no"
        collectd_libiokit=0
@@@ -902,8 -882,6 +899,8 @@@ AC_ARG_WITH(libstatgrab, [AS_HELP_STRIN
                LDFLAGS="$LDFLAGS -L$withval/lib"
                CPPFLAGS="$CPPFLAGS -I$withval/include"
                with_libstatgrab="yes"
 +      else
 +              with_libstatgrab="$withval"
        fi
  ],
  [
@@@ -1142,12 -1120,10 +1139,12 @@@ AC_ARG_WITH(liboping, [AS_HELP_STRING([
        then
                with_liboping="no"
                with_own_liboping="no"
 -      fi
 +      else if test "x$withval" = "xyes"
 +      then
 +              with_liboping="yes"
 +      fi; fi
  ],
  [
 -      #753
        with_liboping="yes"
  ])
  
@@@ -1179,8 -1155,6 +1176,8 @@@ AC_ARG_WITH(libpcap, [AS_HELP_STRING([-
                LDFLAGS="$LDFLAGS -L$withval/lib"
                CPPFLAGS="$CPPFLAGS -I$withval/include"
                with_libpcap="yes"
 +      else
 +              with_libpcap="$withval"
        fi
  ],
  [
@@@ -1219,8 -1193,6 +1216,8 @@@ AC_ARG_WITH(libperl, [AS_HELP_STRING([-
                CPPFLAGS="$CPPFLAGS -I$withval/include"
                perl_interpreter="$withval/bin/perl"
                with_libperl="yes"
 +      else
 +              with_libperl="$withval"
        fi
  ],
  [
  fi
  AM_CONDITIONAL(BUILD_WITH_LIBPERL, test "x$with_libperl" = "xyes")
  
 +if test "x$with_libperl" = "xyes"
 +then
 +      SAVE_CFLAGS=$CFLAGS
 +      SAVE_LDFLAGS=$LDFLAGS
 +      CFLAGS="$CFLAGS $PERL_CFLAGS"
 +      LDFLAGS="$LDFLAGS $PERL_LDFLAGS"
 +
 +      AC_CACHE_CHECK([if perl supports ithreads],
 +              [have_perl_ithreads],
 +              AC_LINK_IFELSE(
 +                      AC_LANG_PROGRAM(
 +                      [[
 +#include <EXTERN.h>
 +#include <perl.h>
 +#include <XSUB.h>
 +
 +#if !defined(USE_ITHREADS)
 +# error "Perl does not support ithreads!"
 +#endif /* !defined(USE_ITHREADS) */
 +                      ]],
 +                      [[ ]]),
 +                      [have_perl_ithreads="yes"],
 +                      [have_perl_ithreads="no"]
 +              )
 +      )
 +
 +      if test "x$have_perl_ithreads" = "xyes"
 +      then
 +              AC_DEFINE(HAVE_PERL_ITHREADS, 1, [Define if Perl supports ithreads.])
 +      fi
 +
 +      CFLAGS=$SAVE_CFLAGS
 +      LDFLAGS=$SAVE_LDFLAGS
 +fi
 +
  AC_ARG_WITH(libiptc, [AS_HELP_STRING([--with-libiptc@<:@=PREFIX@:>@], [Path to libiptc.])],
  [
        if test "x$withval" != "xno" && test "x$withval" != "xyes"
                LDFLAGS="$LDFLAGS -L$withval/lib"
                CPPFLAGS="$CPPFLAGS -I$withval/include"
                with_libiptc="yes"
 +      else
 +              with_libiptc="$withval"
        fi
  ],
  [
 -      if test "x$ac_system" = "xLinux"
 +      if test "x$ac_system" = "xLinux"
        then
                with_libiptc="yes"
        else
@@@ -1353,14 -1288,14 +1350,14 @@@ with_snmp_cflags="
  with_snmp_libs=""
  AC_ARG_WITH(libnetsnmp, [AS_HELP_STRING([--with-libnetsnmp@<:@=PREFIX@:>@], [Path to the Net-SNMPD library.])],
  [
 -      if test "x$withval" = "xno"
 +      if test "x$withval" = "xno"
        then
                with_libnetsnmp="no"
        else if test "x$withval" = "xyes"
        then
                with_libnetsnmp="yes"
        else
 -              if test -x "$withval"
 +              if test -x "$withval"
                then
                        with_snmp_config="$withval"
                        with_libnetsnmp="yes"
@@@ -1480,7 -1415,7 +1477,7 @@@ the
  fi
  if test "x$with_libupsclient" = "xyes"
  then
 -      AC_CHECK_TYPES([UPSCONN_t, UPSCONN], [], [], 
 +      AC_CHECK_TYPES([UPSCONN_t, UPSCONN], [], [],
  [#include <stdlib.h>
  #include <stdio.h>
  #include <upsclient.h>])
  fi
  AM_CONDITIONAL(BUILD_WITH_LIBNETLINK, test "x$with_libnetlink" = "xyes")
  
 +dnl Check for libvirt and libxml2 libraries.
 +with_libxml2="no (pkg-config isn't available)"
 +with_libxml2_cflags=""
 +with_libxml2_ldflags=""
 +with_libvirt="no (pkg-config isn't available)"
 +with_libvirt_cflags=""
 +with_libvirt_ldflags=""
 +PKG_PROG_PKG_CONFIG
 +if test "x$PKG_CONFIG" != "x"
 +then
 +      pkg-config --exists 'libxml-2.0' 2>/dev/null
 +      if test "$?" = "0"
 +      then
 +              with_libxml2="yes"
 +      else
 +              with_libxml2="no (pkg-config doesn't know library)"
 +      fi
 +
 +      pkg-config --exists libvirt 2>/dev/null
 +      if test "$?" = "0"
 +      then
 +              with_libvirt="yes"
 +      else
 +              with_libvirt="no (pkg-config doesn't know library)"
 +      fi
 +fi
 +if test "x$with_libxml2" = "xyes"
 +then
 +      with_libxml2_cflags="`pkg-config --cflags libxml-2.0`"
 +      if test $? -ne 0
 +      then
 +              with_libxml2="no"
 +      fi
 +      with_libxml2_ldflags="`pkg-config --libs libxml-2.0`"
 +      if test $? -ne 0
 +      then
 +              with_libxml2="no"
 +      fi
 +fi
 +if test "x$with_libxml2" = "xyes"
 +then
 +      SAVE_CPPFLAGS="$CPPFLAGS"
 +      CPPFLAGS="$CPPFLAGS $with_libxml2_cflags"
 +
 +      AC_CHECK_HEADERS(libxml/parser.h, [],
 +                    [with_libxml2="no (libxml/parser.h not found)"])
 +
 +      CPPFLAGS="$SAVE_CPPFLAGS"
 +fi
 +if test "x$with_libxml2" = "xyes"
 +then
 +      SAVE_CFLAGS="$CFLAGS"
 +      SAVE_LD_FLAGS="$LDFLAGS"
 +
 +      CFLAGS="$CFLAGS $with_libxml2_cflags"
 +      LDFLAGS="$LDFLAGS $with_libxml2_ldflags"
 +
 +      AC_CHECK_LIB(xml2, xmlXPathEval,
 +                   [with_libxml2="yes"],
 +                   [with_libxml2="no (symbol xmlXPathEval not found)"])
 +
 +      CFLAGS="$SAVE_CFLAGS"
 +      LDFLAGS="$SAVE_LDFLAGS"
 +fi
 +dnl Add the right compiler flags and libraries.
 +if test "x$with_libxml2" = "xyes"; then
 +      BUILD_WITH_LIBXML2_CFLAGS="$with_libxml2_cflags"
 +      BUILD_WITH_LIBXML2_LIBS="$with_libxml2_ldflags"
 +      AC_SUBST(BUILD_WITH_LIBXML2_CFLAGS)
 +      AC_SUBST(BUILD_WITH_LIBXML2_LIBS)
 +fi
 +if test "x$with_libvirt" = "xyes"
 +then
 +      with_libvirt_cflags="`pkg-config --cflags libvirt`"
 +      if test $? -ne 0
 +      then
 +              with_libvirt="no"
 +      fi
 +      with_libvirt_ldflags="`pkg-config --libs libvirt`"
 +      if test $? -ne 0
 +      then
 +              with_libvirt="no"
 +      fi
 +fi
 +if test "x$with_libvirt" = "xyes"
 +then
 +      SAVE_CPPFLAGS="$CPPFLAGS"
 +      CPPFLAGS="$CPPFLAGS $with_libvirt_cflags"
 +
 +      AC_CHECK_HEADERS(libvirt/libvirt.h, [],
 +                    [with_libvirt="no (libvirt/libvirt.h not found)"])
 +
 +      CPPFLAGS="$SAVE_CPPFLAGS"
 +fi
 +if test "x$with_libvirt" = "xyes"
 +then
 +      SAVE_CFLAGS="$CFLAGS"
 +      SAVE_LD_FLAGS="$LDFLAGS"
 +
 +      CFLAGS="$CFLAGS $with_libvirt_cflags"
 +      LDFLAGS="$LDFLAGS $with_libvirt_ldflags"
 +
 +      AC_CHECK_LIB(virt, virDomainBlockStats,
 +                   [with_libvirt="yes"],
 +                   [with_libvirt="no (symbol virDomainBlockStats not found)"])
 +
 +      CFLAGS="$SAVE_CFLAGS"
 +      LDFLAGS="$SAVE_LDFLAGS"
 +fi
 +dnl Add the right compiler flags and libraries.
 +if test "x$with_libvirt" = "xyes"; then
 +      BUILD_WITH_LIBVIRT_CFLAGS="$with_libvirt_cflags"
 +      BUILD_WITH_LIBVIRT_LIBS="$with_libvirt_ldflags"
 +      AC_SUBST(BUILD_WITH_LIBVIRT_CFLAGS)
 +      AC_SUBST(BUILD_WITH_LIBVIRT_LIBS)
 +fi
 +
 +dnl End of check for libvirt and libxml2 libraries.
 +
  # Check for enabled/disabled features
  #
  
@@@ -1891,12 -1707,10 +1888,12 @@@ plugin_entropy="no
  plugin_interface="no"
  plugin_ipvs="no"
  plugin_irq="no"
 +plugin_libvirt="no"
  plugin_load="no"
  plugin_memory="no"
  plugin_multimeter="no"
  plugin_nfs="no"
 +plugin_perl="no"
  plugin_processes="no"
  plugin_serial="no"
  plugin_swap="no"
        plugin_interface="yes"
  fi
  
 +if test "x$with_libxml2" = "xyes" && test "x$with_libvirt" = "xyes"
 +then
 +      plugin_libvirt="yes"
 +fi
 +
  if test "x$have_getloadavg" = "xyes"
  then
        plugin_load="yes"
  fi
  
 +if test "x$have_libperl$have_perl_ithreads" = "xyesyes"
 +then
 +      plugin_perl="yes"
 +fi
 +
  # Mac OS X memory interface
  if test "x$have_host_statistics" = "xyes"
  then
@@@ -2052,7 -1856,6 +2049,7 @@@ AC_PLUGIN([interface],   [$plugin_inter
  AC_PLUGIN([iptables],    [$with_libiptc],      [IPTables rule counters])
  AC_PLUGIN([ipvs],        [$plugin_ipvs],       [IPVS connection statistics])
  AC_PLUGIN([irq],         [$plugin_irq],        [IRQ statistics])
 +AC_PLUGIN([libvirt],     [$plugin_libvirt],    [Virtual machine statistics])
  AC_PLUGIN([load],        [$plugin_load],       [System load])
  AC_PLUGIN([logfile],     [yes],                [File logging plugin])
  AC_PLUGIN([mbmon],       [yes],                [Query mbmond])
@@@ -2066,7 -1869,7 +2063,7 @@@ AC_PLUGIN([nfs],         [$plugin_nfs]
  AC_PLUGIN([nginx],       [$with_libcurl],      [nginx statistics])
  AC_PLUGIN([ntpd],        [yes],                [NTPd statistics])
  AC_PLUGIN([nut],         [$with_libupsclient], [Network UPS tools statistics])
 -AC_PLUGIN([perl],        [$with_libperl],      [Embed a Perl interpreter])
 +AC_PLUGIN([perl],        [$plugin_perl],       [Embed a Perl interpreter])
  AC_PLUGIN([ping],        [$with_liboping],     [Network latency statistics])
  AC_PLUGIN([processes],   [$plugin_processes],  [Process statistics])
  AC_PLUGIN([rrdtool],     [$with_rrdtool],      [RRDTool output plugin])
@@@ -2079,7 -1882,6 +2076,7 @@@ AC_PLUGIN([tape],        [$plugin_tape]
  AC_PLUGIN([tcpconns],    [$plugin_tcpconns],   [TCP connection statistics])
  AC_PLUGIN([unixsock],    [yes],                [Unixsock communication plugin])
  AC_PLUGIN([users],       [$plugin_users],      [User statistics])
 +AC_PLUGIN([uuid],        [yes],                [UUID as hostname plugin])
  AC_PLUGIN([vserver],     [$plugin_vserver],    [Linux VServer statistics])
  AC_PLUGIN([wireless],    [$plugin_wireless],   [Wireless statistics])
  AC_PLUGIN([xmms],        [$with_libxmms],      [XMMS statistics])
@@@ -2117,11 -1919,6 +2114,11 @@@ AC_SUBST(PERL_BINDINGS_OPTIONS
  
  AC_OUTPUT(Makefile src/Makefile src/collectd.conf src/liboconfig/Makefile src/liboping/Makefile bindings/Makefile)
  
 +if test "x$with_rrdtool" = "xyes" -a "x$librrd_threadsafe" != "xyes"
 +then
 +      with_rrdtool="yes (warning: librrd is not thread-safe)"
 +fi
 +
  if test "x$with_liboping" = "xyes" -a "x$with_own_liboping" = "xyes"
  then
        with_liboping="yes (shipped version)"
@@@ -2160,8 -1957,6 +2157,8 @@@ Configuration
      libsensors  . . . . $with_lm_sensors
      libstatgrab . . . . $with_libstatgrab
      libupsclient  . . . $with_libupsclient
 +    libvirt . . . . . . $with_libvirt
 +    libxml2 . . . . . . $with_libxml2
      libxmms . . . . . . $with_libxmms
  
    Features:
      iptables  . . . . . $enable_iptables
      ipvs  . . . . . . . $enable_ipvs
      irq . . . . . . . . $enable_irq
 +    libvirt . . . . . . $enable_libvirt
      load  . . . . . . . $enable_load
      logfile . . . . . . $enable_logfile
      mbmon . . . . . . . $enable_mbmon
      tcpconns  . . . . . $enable_tcpconns
      unixsock  . . . . . $enable_unixsock
      users . . . . . . . $enable_users
 +    uuid  . . . . . . . $enable_uuid
      vserver . . . . . . $enable_vserver
      wireless  . . . . . $enable_wireless
      xmms  . . . . . . . $enable_xmms
diff --combined src/exec.c
index c6a58d1cff68b62bad8de377a1d4058136579edf,b8b538b8c765d1f09ea5312b39e384b11306c92b..b25e76979ebb2322db79a5acdd746d28ecf73335
@@@ -1,6 -1,6 +1,6 @@@
  /**
   * collectd - src/exec.c
 - * Copyright (C) 2007  Florian octo Forster
 + * Copyright (C) 2007,2008  Florian octo Forster
   *
   * 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
@@@ -22,9 -22,7 +22,9 @@@
  #include "collectd.h"
  #include "common.h"
  #include "plugin.h"
 +
  #include "utils_cmd_putval.h"
 +#include "utils_cmd_putnotif.h"
  
  #include <sys/types.h>
  #include <pwd.h>
  
  #include <pthread.h>
  
 +#define PL_NORMAL        0x01
 +#define PL_NOTIF_ACTION  0x02
 +
 +#define PL_RUNNING       0x10
 +
  /*
   * Private data types
   */
 +/*
 + * Access to this structure is serialized using the `pl_lock' lock and the
 + * `PL_RUNNING' flag. The execution of notifications is *not* serialized, so
 + * all functions used to handle notifications MUST NOT write to this structure.
 + * The `pid' and `status' fields are thus unused if the `PL_NOTIF_ACTION' flag
 + * is set.
 + * The `PL_RUNNING' flag is set in `exec_read' and unset in `exec_read_one'.
 + */
  struct program_list_s;
  typedef struct program_list_s program_list_t;
  struct program_list_s
    char           *user;
    char           *group;
    char           *exec;
 +  char          **argv;
    int             pid;
 +  int             status;
 +  int             flags;
    program_list_t *next;
  };
  
 +typedef struct program_list_and_notification_s
 +{
 +  program_list_t *pl;
 +  notification_t n;
 +} program_list_and_notification_t;
 +
  /*
   * Private variables
   */
 -static const char *config_keys[] =
 -{
 -  "Exec"
 -};
 -static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
 -
  static program_list_t *pl_head = NULL;
 +static pthread_mutex_t pl_lock = PTHREAD_MUTEX_INITIALIZER;
  
  /*
   * Functions
   */
 -static int exec_config (const char *key, const char *value)
 +static void sigchld_handler (int signal) /* {{{ */
  {
 -  if (strcasecmp ("Exec", key) == 0)
 +  pid_t pid;
 +  int status;
 +  while ((pid = waitpid (-1, &status, WNOHANG)) > 0)
    {
      program_list_t *pl;
 -    pl = (program_list_t *) malloc (sizeof (program_list_t));
 -    if (pl == NULL)
 -      return (1);
 -    memset (pl, '\0', sizeof (program_list_t));
 +    for (pl = pl_head; pl != NULL; pl = pl->next)
 +      if (pl->pid == pid)
 +      break;
 +    if (pl != NULL)
 +      pl->status = status;
 +  } /* while (waitpid) */
 +} /* void sigchld_handler }}} */
 +
 +static int exec_config_exec (oconfig_item_t *ci) /* {{{ */
 +{
 +  program_list_t *pl;
 +  char buffer[128];
 +  int i;
  
 -    pl->user = strdup (value);
 -    if (pl->user == NULL)
 -    {
 -      sfree (pl);
 -      return (1);
 -    }
 +  if (ci->children_num != 0)
 +  {
 +    WARNING ("exec plugin: The config option `%s' may not be a block.",
 +      ci->key);
 +    return (-1);
 +  }
 +  if (ci->values_num < 2)
 +  {
 +    WARNING ("exec plugin: The config option `%s' needs at least two "
 +      "arguments.", ci->key);
 +    return (-1);
 +  }
 +  if ((ci->values[0].type != OCONFIG_TYPE_STRING)
 +      || (ci->values[1].type != OCONFIG_TYPE_STRING))
 +  {
 +    WARNING ("exec plugin: The first two arguments to the `%s' option must "
 +      "be string arguments.", ci->key);
 +    return (-1);
 +  }
 +
 +  pl = (program_list_t *) malloc (sizeof (program_list_t));
 +  if (pl == NULL)
 +  {
 +    ERROR ("exec plugin: malloc failed.");
 +    return (-1);
 +  }
 +  memset (pl, '\0', sizeof (program_list_t));
 +
 +  if (strcasecmp ("NotificationExec", ci->key) == 0)
 +    pl->flags |= PL_NOTIF_ACTION;
 +  else
 +    pl->flags |= PL_NORMAL;
 +
 +  pl->user = strdup (ci->values[0].value.string);
 +  if (pl->user == NULL)
 +  {
 +    ERROR ("exec plugin: strdup failed.");
 +    sfree (pl);
 +    return (-1);
 +  }
 +
 +  pl->group = strchr (pl->user, ':');
 +  if (pl->group != NULL)
 +  {
 +    *pl->group = '\0';
 +    pl->group++;
 +  }
 +
 +  pl->exec = strdup (ci->values[1].value.string);
 +  if (pl->exec == NULL)
 +  {
 +    ERROR ("exec plugin: strdup failed.");
 +    sfree (pl->user);
 +    sfree (pl);
 +    return (-1);
 +  }
  
 -    pl->exec = strchr (pl->user, ' ');
 -    if (pl->exec == NULL)
 +  pl->argv = (char **) malloc (ci->values_num * sizeof (char *));
 +  if (pl->argv == NULL)
 +  {
 +    ERROR ("exec plugin: malloc failed.");
 +    sfree (pl->exec);
 +    sfree (pl->user);
 +    sfree (pl);
 +    return (-1);
 +  }
 +  memset (pl->argv, '\0', ci->values_num * sizeof (char *));
 +
 +  {
 +    char *tmp = strrchr (ci->values[1].value.string, '/');
 +    if (tmp == NULL)
 +      strncpy (buffer, ci->values[1].value.string, sizeof (buffer));
 +    else
 +      strncpy (buffer, tmp + 1, sizeof (buffer));
 +    buffer[sizeof (buffer) - 1] = '\0';
 +  }
 +  pl->argv[0] = strdup (buffer);
 +  if (pl->argv[0] == NULL)
 +  {
 +    ERROR ("exec plugin: malloc failed.");
 +    sfree (pl->argv);
 +    sfree (pl->exec);
 +    sfree (pl->user);
 +    sfree (pl);
 +    return (-1);
 +  }
 +
 +  for (i = 1; i < (ci->values_num - 1); i++)
 +  {
 +    if (ci->values[i + 1].type == OCONFIG_TYPE_STRING)
      {
 -      sfree (pl->user);
 -      sfree (pl);
 -      return (1);
 +      pl->argv[i] = strdup (ci->values[i + 1].value.string);
      }
 -    while (*pl->exec == ' ')
 +    else
      {
 -      *pl->exec = '\0';
 -      pl->exec++;
 +      if (ci->values[i + 1].type == OCONFIG_TYPE_NUMBER)
 +      {
 +      snprintf (buffer, sizeof (buffer), "%lf",
 +          ci->values[i + 1].value.number);
 +      }
 +      else
 +      {
 +      if (ci->values[i + 1].value.boolean)
 +        strncpy (buffer, "true", sizeof (buffer));
 +      else
 +        strncpy (buffer, "false", sizeof (buffer));
 +      }
 +      buffer[sizeof (buffer) - 1] = '\0';
 +
 +      pl->argv[i] = strdup (buffer);
      }
  
 -    if (*pl->exec == '\0')
 +    if (pl->argv[i] == NULL)
      {
 -      sfree (pl->user);
 -      sfree (pl);
 -      return (1);
 +      ERROR ("exec plugin: strdup failed.");
 +      break;
      }
 +  } /* for (i) */
  
 -    pl->next = pl_head;
 -    pl_head = pl;
 -
 -    pl->group = strchr (pl->user, ':');
 -    if (NULL != pl->group) {
 -      *pl->group = '\0';
 -      pl->group++;
 +  if (i < (ci->values_num - 1))
 +  {
 +    while ((--i) >= 0)
 +    {
 +      sfree (pl->argv[i]);
      }
 +    sfree (pl->argv);
 +    sfree (pl->exec);
 +    sfree (pl->user);
 +    sfree (pl);
 +    return (-1);
    }
 -  else
 +
 +  for (i = 0; pl->argv[i] != NULL; i++)
    {
 -    return (-1);
 +    DEBUG ("exec plugin: argv[%i] = %s", i, pl->argv[i]);
    }
  
 +  pl->next = pl_head;
 +  pl_head = pl;
 +
 +  return (0);
 +} /* int exec_config_exec }}} */
 +
 +static int exec_config (oconfig_item_t *ci) /* {{{ */
 +{
 +  int i;
 +
 +  for (i = 0; i < ci->children_num; i++)
 +  {
 +    oconfig_item_t *child = ci->children + i;
 +    if ((strcasecmp ("Exec", child->key) == 0)
 +      || (strcasecmp ("NotificationExec", child->key) == 0))
 +      exec_config_exec (child);
 +    else
 +    {
 +      WARNING ("exec plugin: Unknown config option `%s'.", child->key);
 +    }
 +  } /* for (i) */
 +
    return (0);
 -} /* int exec_config */
 +} /* int exec_config }}} */
  
 -static void exec_child (program_list_t *pl)
 +static void exec_child (program_list_t *pl) /* {{{ */
  {
    int status;
    int uid;
    int gid;
    int egid;
 -  char *arg0;
  
    struct passwd *sp_ptr;
    struct passwd sp;
      }
    } /* if (pl->group == NULL) */
  
+ #if HAVE_SETGROUPS
+   if (getuid () == 0)
+   {
+     gid_t  glist[2];
+     size_t glist_len;
+     glist[0] = gid;
+     glist_len = 1;
+     if (gid != egid)
+     {
+       glist[1] = egid;
+       glist_len = 2;
+     }
+     setgroups (glist_len, glist);
+   }
+ #endif /* HAVE_SETGROUPS */
    status = setgid (gid);
    if (status != 0)
    {
      exit (-1);
    }
  
 -  arg0 = strrchr (pl->exec, '/');
 -  if (arg0 != NULL)
 -    arg0++;
 -  if ((arg0 == NULL) || (*arg0 == '\0'))
 -    arg0 = pl->exec;
 -
 -  status = execlp (pl->exec, arg0, (char *) 0);
 +  status = execvp (pl->exec, pl->argv);
  
    ERROR ("exec plugin: exec failed: %s",
        sstrerror (errno, errbuf, sizeof (errbuf)));
    exit (-1);
 -} /* void exec_child */
 +} /* void exec_child }}} */
  
 -static int fork_child (program_list_t *pl)
 +/*
 + * Creates two pipes (one for reading, ong for writing), forks a child, sets up
 + * the pipes so that fd_in is connected to STDIN of the child and fd_out is
 + * connected to STDOUT and STDERR of the child. Then is calls `exec_child'.
 + */
 +static int fork_child (program_list_t *pl, int *fd_in, int *fd_out) /* {{{ */
  {
 -  int fd_pipe[2];
 +  int fd_pipe_in[2];
 +  int fd_pipe_out[2];
    int status;
 +  int pid;
  
    if (pl->pid != 0)
      return (-1);
  
 -  status = pipe (fd_pipe);
 +  status = pipe (fd_pipe_in);
    if (status != 0)
    {
      char errbuf[1024];
      return (-1);
    }
  
 -  pl->pid = fork ();
 -  if (pl->pid < 0)
 +  status = pipe (fd_pipe_out);
 +  if (status != 0)
 +  {
 +    char errbuf[1024];
 +    ERROR ("exec plugin: pipe failed: %s",
 +      sstrerror (errno, errbuf, sizeof (errbuf)));
 +    return (-1);
 +  }
 +
 +  pid = fork ();
 +  if (pid < 0)
    {
      char errbuf[1024];
      ERROR ("exec plugin: fork failed: %s",
        sstrerror (errno, errbuf, sizeof (errbuf)));
      return (-1);
    }
 -  else if (pl->pid == 0)
 +  else if (pid == 0)
    {
 -    close (fd_pipe[0]);
 +    close (fd_pipe_in[1]);
 +    close (fd_pipe_out[0]);
  
 -    /* Connect the pipe to STDOUT and STDERR */
 -    if (fd_pipe[1] != STDOUT_FILENO)
 -      dup2 (fd_pipe[1], STDOUT_FILENO);
 -    if (fd_pipe[1] != STDERR_FILENO)
 -      dup2 (fd_pipe[1], STDERR_FILENO);
 -    if ((fd_pipe[1] != STDOUT_FILENO) && (fd_pipe[1] != STDERR_FILENO))
 -      close (fd_pipe[1]);
 +    /* If the `out' pipe has the filedescriptor STDIN we have to be careful
 +     * with the `dup's below. So, if this is the case we have to handle the
 +     * `out' pipe first. */
 +    if (fd_pipe_out[1] == STDIN_FILENO)
 +    {
 +      int new_fileno = (fd_pipe_in[0] == STDOUT_FILENO)
 +      ? STDERR_FILENO : STDOUT_FILENO;
 +      dup2 (fd_pipe_out[1], new_fileno);
 +      close (fd_pipe_out[1]);
 +      fd_pipe_out[1] = new_fileno;
 +    }
 +    /* Now `fd_pipe_out[1]' is either `STDOUT' or `STDERR', but definitely not
 +     * `STDIN_FILENO'. */
 +
 +    /* Connect the `in' pipe to STDIN */
 +    if (fd_pipe_in[0] != STDIN_FILENO)
 +    {
 +      dup2 (fd_pipe_in[0], STDIN_FILENO);
 +      close (fd_pipe_in[0]);
 +      fd_pipe_in[0] = STDIN_FILENO;
 +    }
 +
 +    /* Now connect the `out' pipe to STDOUT and STDERR */
 +    if (fd_pipe_out[1] != STDOUT_FILENO)
 +      dup2 (fd_pipe_out[1], STDOUT_FILENO);
 +    if (fd_pipe_out[1] != STDERR_FILENO)
 +      dup2 (fd_pipe_out[1], STDERR_FILENO);
 +
 +    /* If the pipe has some FD that's something completely different, close it
 +     * now. */
 +    if ((fd_pipe_out[1] != STDOUT_FILENO) && (fd_pipe_out[1] != STDERR_FILENO))
 +    {
 +      close (fd_pipe_out[1]);
 +      fd_pipe_out[1] = STDOUT_FILENO;
 +    }
  
      exec_child (pl);
      /* does not return */
    }
  
 -  close (fd_pipe[1]);
 -  return (fd_pipe[0]);
 -} /* int fork_child */
 +  close (fd_pipe_in[0]);
 +  close (fd_pipe_out[1]);
  
 -static int parse_line (char *buffer)
 +  if (fd_in != NULL)
 +    *fd_in = fd_pipe_in[1];
 +  else
 +    close (fd_pipe_in[1]);
 +
 +  if (fd_out != NULL)
 +    *fd_out = fd_pipe_out[0];
 +  else
 +    close (fd_pipe_out[0]);
 +
 +  return (pid);
 +} /* int fork_child }}} */
 +
 +static int parse_line (char *buffer) /* {{{ */
  {
    char *fields[256];
    int fields_num;
  
    fields[0] = "PUTVAL";
 -  fields_num = strsplit (buffer, &fields[1], STATIC_ARRAY_SIZE(fields) - 1);
 +  fields_num = strsplit (buffer, fields + 1, STATIC_ARRAY_SIZE(fields) - 1);
  
 -  handle_putval (stdout, fields, fields_num + 1);
 -  return (0);
 -} /* int parse_line */
 +  if (strcasecmp (fields[1], "putval") == 0)
 +    return (handle_putval (stdout, fields + 1, fields_num));
 +  else if (strcasecmp (fields[1], "putnotif") == 0)
 +    return (handle_putnotif (stdout, fields + 1, fields_num));
  
 -static void *exec_read_one (void *arg)
 +  /* compatibility code */
 +  return (handle_putval (stdout, fields, fields_num + 1));
 +} /* int parse_line }}} */
 +
 +static void *exec_read_one (void *arg) /* {{{ */
  {
    program_list_t *pl = (program_list_t *) arg;
    int fd;
    FILE *fh;
    char buffer[1024];
 +  int status;
  
 -  fd = fork_child (pl);
 -  if (fd < 0)
 +  status = fork_child (pl, NULL, &fd);
 +  if (status < 0)
      pthread_exit ((void *) 1);
 +  pl->pid = status;
  
    assert (pl->pid != 0);
  
      pthread_exit ((void *) 1);
    }
  
 +  buffer[0] = '\0';
    while (fgets (buffer, sizeof (buffer), fh) != NULL)
    {
      int len;
    } /* while (fgets) */
  
    fclose (fh);
 +
 +  if (waitpid (pl->pid, &status, 0) > 0)
 +    pl->status = status;
 +
 +  DEBUG ("exec plugin: Child %i exited with status %i.",
 +      (int) pl->pid, pl->status);
 +
    pl->pid = 0;
  
 +  pthread_mutex_lock (&pl_lock);
 +  pl->flags &= ~PL_RUNNING;
 +  pthread_mutex_unlock (&pl_lock);
 +
    pthread_exit ((void *) 0);
    return (NULL);
 -} /* void *exec_read_one */
 +} /* void *exec_read_one }}} */
  
 -static int exec_read (void)
 +static void *exec_notification_one (void *arg) /* {{{ */
 +{
 +  program_list_t *pl = ((program_list_and_notification_t *) arg)->pl;
 +  const notification_t *n = &((program_list_and_notification_t *) arg)->n;
 +  int fd;
 +  FILE *fh;
 +  int pid;
 +  int status;
 +  const char *severity;
 +
 +  pid = fork_child (pl, &fd, NULL);
 +  if (pid < 0) {
 +    sfree (arg);
 +    pthread_exit ((void *) 1);
 +  }
 +
 +  fh = fdopen (fd, "w");
 +  if (fh == NULL)
 +  {
 +    char errbuf[1024];
 +    ERROR ("exec plugin: fdopen (%i) failed: %s", fd,
 +      sstrerror (errno, errbuf, sizeof (errbuf)));
 +    kill (pl->pid, SIGTERM);
 +    pl->pid = 0;
 +    close (fd);
 +    sfree (arg);
 +    pthread_exit ((void *) 1);
 +  }
 +
 +  severity = "FAILURE";
 +  if (n->severity == NOTIF_WARNING)
 +    severity = "WARNING";
 +  else if (n->severity == NOTIF_OKAY)
 +    severity = "OKAY";
 +
 +  fprintf (fh,
 +      "Severity: %s\n"
 +      "Time: %u\n",
 +      severity, (unsigned int) n->time);
 +
 +  /* Print the optional fields */
 +  if (strlen (n->host) > 0)
 +    fprintf (fh, "Host: %s\n", n->host);
 +  if (strlen (n->plugin) > 0)
 +    fprintf (fh, "Plugin: %s\n", n->plugin);
 +  if (strlen (n->plugin_instance) > 0)
 +    fprintf (fh, "PluginInstance: %s\n", n->plugin_instance);
 +  if (strlen (n->type) > 0)
 +    fprintf (fh, "Type: %s\n", n->type);
 +  if (strlen (n->type_instance) > 0)
 +    fprintf (fh, "TypeInstance: %s\n", n->type_instance);
 +
 +  fprintf (fh, "\n%s\n", n->message);
 +
 +  fflush (fh);
 +  fclose (fh);
 +
 +  waitpid (pid, &status, 0);
 +
 +  DEBUG ("exec plugin: Child %i exited with status %i.",
 +      pid, status);
 +
 +  sfree (arg);
 +  pthread_exit ((void *) 0);
 +  return (NULL);
 +} /* void *exec_notification_one }}} */
 +
 +static int exec_init (void) /* {{{ */
 +{
 +  struct sigaction sa;
 +
 +  memset (&sa, '\0', sizeof (sa));
 +  sa.sa_handler = sigchld_handler;
 +  sigaction (SIGCHLD, &sa, NULL);
 +
 +  return (0);
 +} /* int exec_init }}} */
 +
 +static int exec_read (void) /* {{{ */
  {
    program_list_t *pl;
  
      pthread_t t;
      pthread_attr_t attr;
  
 -    if (pl->pid != 0)
 +    /* Only execute `normal' style executables here. */
 +    if ((pl->flags & PL_NORMAL) == 0)
        continue;
  
 +    pthread_mutex_lock (&pl_lock);
 +    /* Skip if a child is already running. */
 +    if ((pl->flags & PL_RUNNING) != 0)
 +    {
 +      pthread_mutex_unlock (&pl_lock);
 +      continue;
 +    }
 +    pl->flags |= PL_RUNNING;
 +    pthread_mutex_unlock (&pl_lock);
 +
      pthread_attr_init (&attr);
      pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
      pthread_create (&t, &attr, exec_read_one, (void *) pl);
    } /* for (pl) */
  
    return (0);
 -} /* int exec_read */
 +} /* int exec_read }}} */
 +
 +static int exec_notification (const notification_t *n)
 +{
 +  program_list_t *pl;
 +  program_list_and_notification_t *pln;
 +
 +  for (pl = pl_head; pl != NULL; pl = pl->next)
 +  {
 +    pthread_t t;
 +    pthread_attr_t attr;
 +
 +    /* Only execute `notification' style executables here. */
 +    if ((pl->flags & PL_NOTIF_ACTION) == 0)
 +      continue;
 +
 +    /* Skip if a child is already running. */
 +    if (pl->pid != 0)
 +      continue;
 +
 +    pln = (program_list_and_notification_t *) malloc (sizeof
 +      (program_list_and_notification_t));
 +    if (pln == NULL)
 +    {
 +      ERROR ("exec plugin: malloc failed.");
 +      continue;
 +    }
 +
 +    pln->pl = pl;
 +    memcpy (&pln->n, n, sizeof (notification_t));
 +
 +    pthread_attr_init (&attr);
 +    pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
 +    pthread_create (&t, &attr, exec_notification_one, (void *) pln);
 +  } /* for (pl) */
 +
 +  return (0);
 +} /* int exec_notification */
  
 -static int exec_shutdown (void)
 +static int exec_shutdown (void) /* {{{ */
  {
    program_list_t *pl;
    program_list_t *next;
    pl_head = NULL;
  
    return (0);
 -} /* int exec_shutdown */
 +} /* int exec_shutdown }}} */
  
  void module_register (void)
  {
 -  plugin_register_config ("exec", exec_config, config_keys, config_keys_num);
 +  plugin_register_complex_config ("exec", exec_config);
 +  plugin_register_init ("exec", exec_init);
    plugin_register_read ("exec", exec_read);
 +  plugin_register_notification ("exec", exec_notification);
    plugin_register_shutdown ("exec", exec_shutdown);
  } /* void module_register */
  
  /*
 - * vim:shiftwidth=2:softtabstop=2:tabstop=8
 + * vim:shiftwidth=2:softtabstop=2:tabstop=8:fdm=marker
   */