Blame SOURCES/net-snmp-5.7.2-systemd.patch

b5ae06
718183 - Provide native systemd unit file
b5ae06
b5ae06
Gathered from following upstream git commits and backported to 5.7.
b5ae06
b5ae06
commit 19499c3c90bf9d7b2b9e5d08baa26cc6bba28a11
b5ae06
Author: Jan Safranek <jsafranek@users.sourceforge.net>
b5ae06
Date:   Mon Aug 8 15:48:54 2011 +0200
b5ae06
b5ae06
    CHANGES: snmpd: integrated with systemd, see README.systemd for details.
b5ae06
    
b5ae06
    It brings sd-daemon.c and .h directly downloaded from systemd. I've made very
b5ae06
    few changes to it to match our NETSNMP_NO_SYSTEMD and include paths.
b5ae06
b5ae06
commit fef6cddfdb94da1a6b1fb768af62918b80f11fd3
b5ae06
Author: Jan Safranek <jsafranek@users.sourceforge.net>
b5ae06
Date:   Mon Aug 8 15:48:54 2011 +0200
b5ae06
b5ae06
    CHANGES: snmptrapd: integrate systemd notification support.
b5ae06
b5ae06
commit 0641e43c694c485cbbffef0556efc4641bd3ff50
b5ae06
Author: Jan Safranek <jsafranek@users.sourceforge.net>
b5ae06
Date:   Mon Aug 8 15:48:54 2011 +0200
b5ae06
b5ae06
    Add sd_find_inet_socket() and sd_find_inet_unisx() helpers into
b5ae06
    system-specific code. This will help us to find various sockets
b5ae06
    created by systemd much easier.
b5ae06
b5ae06
commit 76530a89f1c8bbd0b63acce63e10d5d4812a1a16
b5ae06
Author: Jan Safranek <jsafranek@users.sourceforge.net>
b5ae06
Date:   Mon Aug 8 15:48:54 2011 +0200
b5ae06
b5ae06
    Check sockets created by systemd when opening new server sockets.
b5ae06
    
b5ae06
    systemd can pass sockets to our daemons during startup using LISTEN_FDS
b5ae06
    environment variable. So check this variable when opening new listening
b5ae06
    socket - maybe system has already opened the socket for us.
b5ae06
b5ae06
commit bf108d7f1354f6276fc43c129963f2c49b9fc242
b5ae06
Author: Jan Safranek <jsafranek@users.sourceforge.net>
b5ae06
Date:   Mon Aug 8 15:48:54 2011 +0200
b5ae06
b5ae06
    Added sample systemd service files.
b5ae06
b5ae06
commit 884ec488a6596380ba283d707827dd926a52e0b2
b5ae06
Author: Jan Safranek <jsafranek@users.sourceforge.net>
b5ae06
Date:   Mon Aug 8 15:48:55 2011 +0200
b5ae06
b5ae06
    Run autoheader+autoconf.
b5ae06
b5ae06
commit 86132e3f1e6ef7b4e0b96d8fa24e37c81b71b0e0
b5ae06
Author: Jan Safranek <jsafranek@users.sourceforge.net>
b5ae06
Date:   Tue Aug 9 10:53:43 2011 +0200
b5ae06
b5ae06
    Update systemd documentation and samples.
b5ae06
    
b5ae06
    - add socket unit for snmpd to paralelize boot
b5ae06
    - update WantedBy in socket units as recommended by http://0pointer.de/blog/projects/socket-activation.html
b5ae06
    - rephrase README.systemd
b5ae06
b5ae06
b5ae06
diff -up net-snmp-5.7.2/agent/snmpd.c.systemd net-snmp-5.7.2/agent/snmpd.c
b5ae06
--- net-snmp-5.7.2/agent/snmpd.c.systemd	2012-10-10 00:28:58.000000000 +0200
b5ae06
+++ net-snmp-5.7.2/agent/snmpd.c	2012-11-12 10:18:46.084369548 +0100
b5ae06
@@ -164,6 +164,10 @@ typedef long    fd_mask;
b5ae06
 
b5ae06
 #endif
b5ae06
 
b5ae06
+#ifndef NETSNMP_NO_SYSTEMD
b5ae06
+#include <net-snmp/library/sd-daemon.h>
b5ae06
+#endif
b5ae06
+
b5ae06
 netsnmp_feature_want(logging_file)
b5ae06
 netsnmp_feature_want(logging_stdio)
b5ae06
 netsnmp_feature_want(logging_syslog)
b5ae06
@@ -441,18 +445,26 @@ main(int argc, char *argv[])
b5ae06
     int             agent_mode = -1;
b5ae06
     char           *pid_file = NULL;
b5ae06
     char            option_compatability[] = "-Le";
b5ae06
+    int             prepared_sockets = 0;
b5ae06
 #if HAVE_GETPID
b5ae06
     int fd;
b5ae06
     FILE           *PID;
b5ae06
 #endif
b5ae06
 
b5ae06
 #ifndef WIN32
b5ae06
+#ifndef NETSNMP_NO_SYSYSTEMD
b5ae06
+    /* check if systemd has sockets for us and don't close them */
b5ae06
+    prepared_sockets = netsnmp_sd_listen_fds(0);
b5ae06
+#endif /* NETSNMP_NO_SYSYSTEMD */
b5ae06
+
b5ae06
     /*
b5ae06
      * close all non-standard file descriptors we may have
b5ae06
      * inherited from the shell.
b5ae06
      */
b5ae06
-    for (i = getdtablesize() - 1; i > 2; --i) {
b5ae06
-        (void) close(i);
b5ae06
+    if (!prepared_sockets) {
b5ae06
+        for (i = getdtablesize() - 1; i > 2; --i) {
b5ae06
+            (void) close(i);
b5ae06
+        }
b5ae06
     }
b5ae06
 #endif /* #WIN32 */
b5ae06
     
b5ae06
@@ -1100,6 +1112,19 @@ main(int argc, char *argv[])
b5ae06
     netsnmp_addrcache_initialise();
b5ae06
 
b5ae06
     /*
b5ae06
+     * Let systemd know we're up.
b5ae06
+     */
b5ae06
+#ifndef NETSNMP_NO_SYSTEMD
b5ae06
+    netsnmp_sd_notify(1, "READY=1\n");
b5ae06
+    if (prepared_sockets)
b5ae06
+        /*
b5ae06
+         * Clear the environment variable, we already processed all the sockets
b5ae06
+         * by now.
b5ae06
+         */
b5ae06
+        netsnmp_sd_listen_fds(1);
b5ae06
+#endif
b5ae06
+
b5ae06
+    /*
b5ae06
      * Forever monitor the dest_port for incoming PDUs.  
b5ae06
      */
b5ae06
     DEBUGMSGTL(("snmpd/main", "We're up.  Starting to process data.\n"));
b5ae06
diff -up net-snmp-5.7.2/apps/snmptrapd.c.systemd net-snmp-5.7.2/apps/snmptrapd.c
b5ae06
--- net-snmp-5.7.2/apps/snmptrapd.c.systemd	2012-10-10 00:28:58.000000000 +0200
b5ae06
+++ net-snmp-5.7.2/apps/snmptrapd.c	2012-11-12 10:18:46.084369548 +0100
b5ae06
@@ -125,6 +125,10 @@ SOFTWARE.
b5ae06
 
b5ae06
 #include <net-snmp/net-snmp-features.h>
b5ae06
 
b5ae06
+#ifndef NETSNMP_NO_SYSTEMD
b5ae06
+#include <net-snmp/library/sd-daemon.h>
b5ae06
+#endif
b5ae06
+
b5ae06
 #ifndef BSD4_3
b5ae06
 #define BSD4_2
b5ae06
 #endif
b5ae06
@@ -655,15 +659,22 @@ main(int argc, char *argv[])
b5ae06
     int             agentx_subagent = 1;
b5ae06
 #endif
b5ae06
     netsnmp_trapd_handler *traph;
b5ae06
+    int             prepared_sockets = 0;
b5ae06
 
b5ae06
 
b5ae06
 #ifndef WIN32
b5ae06
+#ifndef NETSNMP_NO_SYSTEMD
b5ae06
+    /* check if systemd has sockets for us and don't close them */
b5ae06
+    prepared_sockets = netsnmp_sd_listen_fds(0);
b5ae06
+#endif
b5ae06
     /*
b5ae06
      * close all non-standard file descriptors we may have
b5ae06
      * inherited from the shell.
b5ae06
      */
b5ae06
-    for (i = getdtablesize() - 1; i > 2; --i) {
b5ae06
-        (void) close(i);
b5ae06
+    if (!prepared_sockets) {
b5ae06
+        for (i = getdtablesize() - 1; i > 2; --i) {
b5ae06
+            (void) close(i);
b5ae06
+        }
b5ae06
     }
b5ae06
 #endif /* #WIN32 */
b5ae06
     
b5ae06
@@ -1311,6 +1322,19 @@ main(int argc, char *argv[])
b5ae06
 #endif
b5ae06
 #endif
b5ae06
 
b5ae06
+    /*
b5ae06
+     * Let systemd know we're up.
b5ae06
+     */
b5ae06
+#ifndef NETSNMP_NO_SYSTEMD
b5ae06
+    netsnmp_sd_notify(1, "READY=1\n");
b5ae06
+    if (prepared_sockets)
b5ae06
+        /*
b5ae06
+         * Clear the environment variable, we already processed all the sockets
b5ae06
+         * by now.
b5ae06
+         */
b5ae06
+        netsnmp_sd_listen_fds(1);
b5ae06
+#endif
b5ae06
+
b5ae06
 #ifdef WIN32SERVICE
b5ae06
     trapd_status = SNMPTRAPD_RUNNING;
b5ae06
 #endif
b5ae06
diff -up net-snmp-5.7.2/configure.d/config_modules_lib.systemd net-snmp-5.7.2/configure.d/config_modules_lib
b5ae06
--- net-snmp-5.7.2/configure.d/config_modules_lib.systemd	2012-10-10 00:28:58.000000000 +0200
b5ae06
+++ net-snmp-5.7.2/configure.d/config_modules_lib	2012-11-12 10:18:46.085369546 +0100
b5ae06
@@ -53,6 +53,14 @@ if test "x$PARTIALTARGETOS" = "xmingw32"
b5ae06
   other_ftobjs_list="$other_ftobjs_list winpipe.ft"
b5ae06
 fi
b5ae06
 
b5ae06
+# Linux systemd
b5ae06
+if test "x$with_systemd" == "xyes"; then
b5ae06
+  other_src_list="$other_src_list sd-daemon.c"
b5ae06
+  other_objs_list="$other_objs_list sd-daemon.o"
b5ae06
+  other_lobjs_list="$other_lobjs_list sd-daemon.lo"
b5ae06
+  other_ftobjs_list="$other_ftobjs_list sd-daemon.ft"
b5ae06
+fi
b5ae06
+
b5ae06
 AC_SUBST(other_src_list)
b5ae06
 AC_SUBST(other_objs_list)
b5ae06
 AC_SUBST(other_lobjs_list)
b5ae06
diff -up net-snmp-5.7.2/configure.d/config_project_with_enable.systemd net-snmp-5.7.2/configure.d/config_project_with_enable
b5ae06
--- net-snmp-5.7.2/configure.d/config_project_with_enable.systemd	2012-10-10 00:28:58.000000000 +0200
b5ae06
+++ net-snmp-5.7.2/configure.d/config_project_with_enable	2012-11-12 10:18:46.086369544 +0100
b5ae06
@@ -690,6 +690,15 @@ if test "x$with_dummy_values" != "xyes";
b5ae06
      data for])
b5ae06
 fi
b5ae06
 
b5ae06
+NETSNMP_ARG_WITH(systemd,
b5ae06
+[  --with-systemd                 Provide systemd support. See README.systemd
b5ae06
+                                  for details.])
b5ae06
+# Define unless specifically suppressed (i.e., option defaults to false).
b5ae06
+if test "x$with_systemd" != "xyes"; then
b5ae06
+  AC_DEFINE(NETSNMP_NO_SYSTEMD, 1,
b5ae06
+    [If you don't want to integrate with systemd.])
b5ae06
+fi
b5ae06
+
b5ae06
 NETSNMP_ARG_ENABLE(set-support,
b5ae06
 [  --disable-set-support           Do not allow SNMP set requests.])
b5ae06
 if test "x$enable_set_support" = "xno"; then
b5ae06
diff -up net-snmp-5.7.2/configure.systemd net-snmp-5.7.2/configure
b5ae06
--- net-snmp-5.7.2/configure.systemd	2012-10-10 00:35:37.000000000 +0200
b5ae06
+++ net-snmp-5.7.2/configure	2012-11-12 10:18:46.099369517 +0100
b5ae06
@@ -950,6 +950,8 @@ with_kmem_usage
b5ae06
 enable_kmem_usage
b5ae06
 with_dummy_values
b5ae06
 enable_dummy_values
b5ae06
+with_systemd
b5ae06
+enable_systemd
b5ae06
 enable_set_support
b5ae06
 with_set_support
b5ae06
 with_sys_contact
b5ae06
@@ -1866,6 +1868,8 @@ Configuring the agent:
b5ae06
                                   This is technically not compliant with the
b5ae06
                                   SNMP specifications, but was how the agent
b5ae06
                                   operated for versions < 4.0.
b5ae06
+  --with-systemd                 Provide systemd support. See README.systemd
b5ae06
+                                  for details.
b5ae06
   --with-sys-contact="who@where"  Default system contact.
b5ae06
                                     (Default: LOGIN@DOMAINNAME)
b5ae06
   --with-sys-location="location"  Default system location.
b5ae06
@@ -4397,6 +4401,24 @@ $as_echo "#define NETSNMP_NO_DUMMY_VALUE
b5ae06
 
b5ae06
 fi
b5ae06
 
b5ae06
+
b5ae06
+# Check whether --with-systemd was given.
b5ae06
+if test "${with_systemd+set}" = set; then :
b5ae06
+  withval=$with_systemd;
b5ae06
+fi
b5ae06
+
b5ae06
+   # Check whether --enable-systemd was given.
b5ae06
+if test "${enable_systemd+set}" = set; then :
b5ae06
+  enableval=$enable_systemd; as_fn_error $? "Invalid option. Use --with-systemd/--without-systemd instead" "$LINENO" 5
b5ae06
+fi
b5ae06
+
b5ae06
+# Define unless specifically suppressed (i.e., option defaults to false).
b5ae06
+if test "x$with_systemd" != "xyes"; then
b5ae06
+
b5ae06
+$as_echo "#define NETSNMP_NO_SYSTEMD 1" >>confdefs.h
b5ae06
+
b5ae06
+fi
b5ae06
+
b5ae06
 # Check whether --enable-set-support was given.
b5ae06
 if test "${enable_set_support+set}" = set; then :
b5ae06
   enableval=$enable_set_support;
b5ae06
@@ -18239,6 +18261,14 @@ if test "x$PARTIALTARGETOS" = "xmingw32"
b5ae06
   other_ftobjs_list="$other_ftobjs_list winpipe.ft"
b5ae06
 fi
b5ae06
 
b5ae06
+# Linux systemd
b5ae06
+if test "x$with_systemd" == "xyes"; then
b5ae06
+  other_src_list="$other_src_list sd-daemon.c"
b5ae06
+  other_objs_list="$other_objs_list sd-daemon.o"
b5ae06
+  other_lobjs_list="$other_lobjs_list sd-daemon.lo"
b5ae06
+  other_ftobjs_list="$other_ftobjs_list sd-daemon.ft"
b5ae06
+fi
b5ae06
+
b5ae06
 
b5ae06
 
b5ae06
 
b5ae06
diff -up net-snmp-5.7.2/dist/snmpd.service.systemd net-snmp-5.7.2/dist/snmpd.service
b5ae06
--- net-snmp-5.7.2/dist/snmpd.service.systemd	2012-11-12 10:18:46.104369507 +0100
b5ae06
+++ net-snmp-5.7.2/dist/snmpd.service	2012-11-12 10:18:46.104369507 +0100
b5ae06
@@ -0,0 +1,18 @@
b5ae06
+#
b5ae06
+# SNMP agent service file for systemd
b5ae06
+#
b5ae06
+#
b5ae06
+# The service should be enabled, i.e. snmpd should start during machine boot.
b5ae06
+# Socket activation shall not be used. See README.systemd for details.
b5ae06
+
b5ae06
+[Unit]
b5ae06
+Description=Simple Network Management Protocol (SNMP) daemon.
b5ae06
+After=syslog.target network.target
b5ae06
+
b5ae06
+[Service]
b5ae06
+# Type=notify is also supported. It should be set when snmpd.socket is not used.
b5ae06
+Type=simple
b5ae06
+ExecStart=/usr/sbin/snmpd -f
b5ae06
+
b5ae06
+[Install]
b5ae06
+WantedBy=multi-user.target
b5ae06
diff -up net-snmp-5.7.2/dist/snmpd.socket.systemd net-snmp-5.7.2/dist/snmpd.socket
b5ae06
--- net-snmp-5.7.2/dist/snmpd.socket.systemd	2012-11-12 10:18:46.104369507 +0100
b5ae06
+++ net-snmp-5.7.2/dist/snmpd.socket	2012-11-12 10:18:46.104369507 +0100
b5ae06
@@ -0,0 +1,17 @@
b5ae06
+[Unit]
b5ae06
+Description=Socket listening for SNMP and AgentX messages
b5ae06
+
b5ae06
+[Socket]
b5ae06
+ListenDatagram=0.0.0.0:161
b5ae06
+# Uncomment other listening addresses as needed - TCP, UDP6, TCP6.
b5ae06
+# It must match listening addresses/ports defined in snmpd.service
b5ae06
+# or snmpd.conf.
b5ae06
+# ListenStream=0.0.0.0:161
b5ae06
+# ListenDatagram=[::]:161
b5ae06
+# ListenStream=[::]:161
b5ae06
+#
b5ae06
+# Uncomment AgentX socket if snmpd.conf enables AgentX protocol.
b5ae06
+# ListenStream=/var/agentx/master
b5ae06
+
b5ae06
+[Install]
b5ae06
+WantedBy=sockets.target
b5ae06
diff -up net-snmp-5.7.2/dist/snmptrapd.service.systemd net-snmp-5.7.2/dist/snmptrapd.service
b5ae06
--- net-snmp-5.7.2/dist/snmptrapd.service.systemd	2012-11-12 10:18:46.105369505 +0100
b5ae06
+++ net-snmp-5.7.2/dist/snmptrapd.service	2012-11-12 10:18:46.105369505 +0100
b5ae06
@@ -0,0 +1,16 @@
b5ae06
+#
b5ae06
+# SNMP trap-processing service file for systemd
b5ae06
+#
b5ae06
+
b5ae06
+[Unit]
b5ae06
+Description=Simple Network Management Protocol (SNMP) Trap daemon.
b5ae06
+After=syslog.target network.target
b5ae06
+
b5ae06
+[Service]
b5ae06
+# Type=notify is also supported. It should be set when snmptrapd.socket is not
b5ae06
+# used.
b5ae06
+Type=simple
b5ae06
+ExecStart=/usr/sbin/snmptrapd -f
b5ae06
+
b5ae06
+[Install]
b5ae06
+WantedBy=multi-user.target
b5ae06
diff -up net-snmp-5.7.2/dist/snmptrapd.socket.systemd net-snmp-5.7.2/dist/snmptrapd.socket
b5ae06
--- net-snmp-5.7.2/dist/snmptrapd.socket.systemd	2012-11-12 10:18:46.105369505 +0100
b5ae06
+++ net-snmp-5.7.2/dist/snmptrapd.socket	2012-11-12 10:18:46.105369505 +0100
b5ae06
@@ -0,0 +1,14 @@
b5ae06
+[Unit]
b5ae06
+Description=Socket listening for SNMP trap messages
b5ae06
+
b5ae06
+[Socket]
b5ae06
+ListenDatagram=0.0.0.0:162
b5ae06
+# Uncomment other listening addresses as needed - TCP, UDP6, TCP6.
b5ae06
+# It must match listening addresses/ports defined in snmptrapd.service
b5ae06
+# or snmptrapd.conf.
b5ae06
+# ListenStream=0.0.0.0:162
b5ae06
+# ListenDatagram=[::]:162
b5ae06
+# ListenStream=[::]:162
b5ae06
+
b5ae06
+[Install]
b5ae06
+WantedBy=sockets.target
b5ae06
diff -up net-snmp-5.7.2/include/net-snmp/library/sd-daemon.h.systemd net-snmp-5.7.2/include/net-snmp/library/sd-daemon.h
b5ae06
--- net-snmp-5.7.2/include/net-snmp/library/sd-daemon.h.systemd	2012-11-12 10:18:46.106369503 +0100
b5ae06
+++ net-snmp-5.7.2/include/net-snmp/library/sd-daemon.h	2012-11-12 10:18:46.106369503 +0100
b5ae06
@@ -0,0 +1,286 @@
b5ae06
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
b5ae06
+
b5ae06
+#ifndef SNMPD_SD_DAEMON_H
b5ae06
+#define SNMPD_SD_DAEMON_H
b5ae06
+
b5ae06
+/***
b5ae06
+  Copyright 2010 Lennart Poettering
b5ae06
+
b5ae06
+  Permission is hereby granted, free of charge, to any person
b5ae06
+  obtaining a copy of this software and associated documentation files
b5ae06
+  (the "Software"), to deal in the Software without restriction,
b5ae06
+  including without limitation the rights to use, copy, modify, merge,
b5ae06
+  publish, distribute, sublicense, and/or sell copies of the Software,
b5ae06
+  and to permit persons to whom the Software is furnished to do so,
b5ae06
+  subject to the following conditions:
b5ae06
+
b5ae06
+  The above copyright notice and this permission notice shall be
b5ae06
+  included in all copies or substantial portions of the Software.
b5ae06
+
b5ae06
+  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
b5ae06
+  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
b5ae06
+  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
b5ae06
+  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
b5ae06
+  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
b5ae06
+  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
b5ae06
+  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
b5ae06
+  SOFTWARE.
b5ae06
+***/
b5ae06
+
b5ae06
+#include <sys/types.h>
b5ae06
+#include <inttypes.h>
b5ae06
+
b5ae06
+#ifdef __cplusplus
b5ae06
+extern "C" {
b5ae06
+#endif
b5ae06
+
b5ae06
+/*
b5ae06
+  Reference implementation of a few systemd related interfaces for
b5ae06
+  writing daemons. These interfaces are trivial to implement. To
b5ae06
+  simplify porting we provide this reference implementation.
b5ae06
+  Applications are welcome to reimplement the algorithms described
b5ae06
+  here if they do not want to include these two source files.
b5ae06
+
b5ae06
+  The following functionality is provided:
b5ae06
+
b5ae06
+  - Support for logging with log levels on stderr
b5ae06
+  - File descriptor passing for socket-based activation
b5ae06
+  - Daemon startup and status notification
b5ae06
+  - Detection of systemd boots
b5ae06
+
b5ae06
+  You may compile this with -DDISABLE_SYSTEMD to disable systemd
b5ae06
+  support. This makes all those calls NOPs that are directly related to
b5ae06
+  systemd (i.e. only sd_is_xxx() will stay useful).
b5ae06
+
b5ae06
+  Since this is drop-in code we don't want any of our symbols to be
b5ae06
+  exported in any case. Hence we declare hidden visibility for all of
b5ae06
+  them.
b5ae06
+
b5ae06
+  You may find an up-to-date version of these source files online:
b5ae06
+
b5ae06
+  http://cgit.freedesktop.org/systemd/plain/src/sd-daemon.h
b5ae06
+  http://cgit.freedesktop.org/systemd/plain/src/sd-daemon.c
b5ae06
+
b5ae06
+  This should compile on non-Linux systems, too, but with the
b5ae06
+  exception of the sd_is_xxx() calls all functions will become NOPs.
b5ae06
+
b5ae06
+  See sd-daemon(7) for more information.
b5ae06
+*/
b5ae06
+
b5ae06
+#ifndef _sd_printf_attr_
b5ae06
+#if __GNUC__ >= 4
b5ae06
+#define _sd_printf_attr_(a,b) __attribute__ ((format (printf, a, b)))
b5ae06
+#else
b5ae06
+#define _sd_printf_attr_(a,b)
b5ae06
+#endif
b5ae06
+#endif
b5ae06
+
b5ae06
+/*
b5ae06
+  Log levels for usage on stderr:
b5ae06
+
b5ae06
+          fprintf(stderr, SD_NOTICE "Hello World!\n");
b5ae06
+
b5ae06
+  This is similar to printk() usage in the kernel.
b5ae06
+*/
b5ae06
+#define SD_EMERG   "<0>"  /* system is unusable */
b5ae06
+#define SD_ALERT   "<1>"  /* action must be taken immediately */
b5ae06
+#define SD_CRIT    "<2>"  /* critical conditions */
b5ae06
+#define SD_ERR     "<3>"  /* error conditions */
b5ae06
+#define SD_WARNING "<4>"  /* warning conditions */
b5ae06
+#define SD_NOTICE  "<5>"  /* normal but significant condition */
b5ae06
+#define SD_INFO    "<6>"  /* informational */
b5ae06
+#define SD_DEBUG   "<7>"  /* debug-level messages */
b5ae06
+
b5ae06
+/* The first passed file descriptor is fd 3 */
b5ae06
+#define SD_LISTEN_FDS_START 3
b5ae06
+
b5ae06
+/*
b5ae06
+  Returns how many file descriptors have been passed, or a negative
b5ae06
+  errno code on failure. Optionally, removes the $LISTEN_FDS and
b5ae06
+  $LISTEN_PID file descriptors from the environment (recommended, but
b5ae06
+  problematic in threaded environments). If r is the return value of
b5ae06
+  this function you'll find the file descriptors passed as fds
b5ae06
+  SD_LISTEN_FDS_START to SD_LISTEN_FDS_START+r-1. Returns a negative
b5ae06
+  errno style error code on failure. This function call ensures that
b5ae06
+  the FD_CLOEXEC flag is set for the passed file descriptors, to make
b5ae06
+  sure they are not passed on to child processes. If FD_CLOEXEC shall
b5ae06
+  not be set, the caller needs to unset it after this call for all file
b5ae06
+  descriptors that are used.
b5ae06
+
b5ae06
+  See sd_listen_fds(3) for more information.
b5ae06
+*/
b5ae06
+int netsnmp_sd_listen_fds(int unset_environment);
b5ae06
+
b5ae06
+/*
b5ae06
+  Helper call for identifying a passed file descriptor. Returns 1 if
b5ae06
+  the file descriptor is a FIFO in the file system stored under the
b5ae06
+  specified path, 0 otherwise. If path is NULL a path name check will
b5ae06
+  not be done and the call only verifies if the file descriptor
b5ae06
+  refers to a FIFO. Returns a negative errno style error code on
b5ae06
+  failure.
b5ae06
+
b5ae06
+  See sd_is_fifo(3) for more information.
b5ae06
+*/
b5ae06
+int netsnmp_sd_is_fifo(int fd, const char *path);
b5ae06
+
b5ae06
+/*
b5ae06
+  Helper call for identifying a passed file descriptor. Returns 1 if
b5ae06
+  the file descriptor is a special character device on the file
b5ae06
+  system stored under the specified path, 0 otherwise.
b5ae06
+  If path is NULL a path name check will not be done and the call
b5ae06
+  only verifies if the file descriptor refers to a special character.
b5ae06
+  Returns a negative errno style error code on failure.
b5ae06
+
b5ae06
+  See sd_is_special(3) for more information.
b5ae06
+*/
b5ae06
+int netsnmp_sd_is_special(int fd, const char *path);
b5ae06
+
b5ae06
+/*
b5ae06
+  Helper call for identifying a passed file descriptor. Returns 1 if
b5ae06
+  the file descriptor is a socket of the specified family (AF_INET,
b5ae06
+  ...) and type (SOCK_DGRAM, SOCK_STREAM, ...), 0 otherwise. If
b5ae06
+  family is 0 a socket family check will not be done. If type is 0 a
b5ae06
+  socket type check will not be done and the call only verifies if
b5ae06
+  the file descriptor refers to a socket. If listening is > 0 it is
b5ae06
+  verified that the socket is in listening mode. (i.e. listen() has
b5ae06
+  been called) If listening is == 0 it is verified that the socket is
b5ae06
+  not in listening mode. If listening is < 0 no listening mode check
b5ae06
+  is done. Returns a negative errno style error code on failure.
b5ae06
+
b5ae06
+  See sd_is_socket(3) for more information.
b5ae06
+*/
b5ae06
+int netsnmp_sd_is_socket(int fd, int family, int type, int listening);
b5ae06
+
b5ae06
+/*
b5ae06
+  Helper call for identifying a passed file descriptor. Returns 1 if
b5ae06
+  the file descriptor is an Internet socket, of the specified family
b5ae06
+  (either AF_INET or AF_INET6) and the specified type (SOCK_DGRAM,
b5ae06
+  SOCK_STREAM, ...), 0 otherwise. If version is 0 a protocol version
b5ae06
+  check is not done. If type is 0 a socket type check will not be
b5ae06
+  done. If port is 0 a socket port check will not be done. The
b5ae06
+  listening flag is used the same way as in sd_is_socket(). Returns a
b5ae06
+  negative errno style error code on failure.
b5ae06
+
b5ae06
+  See sd_is_socket_inet(3) for more information.
b5ae06
+*/
b5ae06
+int netsnmp_sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port);
b5ae06
+
b5ae06
+/*
b5ae06
+  Helper call for identifying a passed file descriptor. Returns 1 if
b5ae06
+  the file descriptor is an AF_UNIX socket of the specified type
b5ae06
+  (SOCK_DGRAM, SOCK_STREAM, ...) and path, 0 otherwise. If type is 0
b5ae06
+  a socket type check will not be done. If path is NULL a socket path
b5ae06
+  check will not be done. For normal AF_UNIX sockets set length to
b5ae06
+  0. For abstract namespace sockets set length to the length of the
b5ae06
+  socket name (including the initial 0 byte), and pass the full
b5ae06
+  socket path in path (including the initial 0 byte). The listening
b5ae06
+  flag is used the same way as in sd_is_socket(). Returns a negative
b5ae06
+  errno style error code on failure.
b5ae06
+
b5ae06
+  See sd_is_socket_unix(3) for more information.
b5ae06
+*/
b5ae06
+int netsnmp_sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length);
b5ae06
+
b5ae06
+/*
b5ae06
+  Informs systemd about changed daemon state. This takes a number of
b5ae06
+  newline separated environment-style variable assignments in a
b5ae06
+  string. The following variables are known:
b5ae06
+
b5ae06
+     READY=1      Tells systemd that daemon startup is finished (only
b5ae06
+                  relevant for services of Type=notify). The passed
b5ae06
+                  argument is a boolean "1" or "0". Since there is
b5ae06
+                  little value in signaling non-readiness the only
b5ae06
+                  value daemons should send is "READY=1".
b5ae06
+
b5ae06
+     STATUS=...   Passes a single-line status string back to systemd
b5ae06
+                  that describes the daemon state. This is free-from
b5ae06
+                  and can be used for various purposes: general state
b5ae06
+                  feedback, fsck-like programs could pass completion
b5ae06
+                  percentages and failing programs could pass a human
b5ae06
+                  readable error message. Example: "STATUS=Completed
b5ae06
+                  66% of file system check..."
b5ae06
+
b5ae06
+     ERRNO=...    If a daemon fails, the errno-style error code,
b5ae06
+                  formatted as string. Example: "ERRNO=2" for ENOENT.
b5ae06
+
b5ae06
+     BUSERROR=... If a daemon fails, the D-Bus error-style error
b5ae06
+                  code. Example: "BUSERROR=org.freedesktop.DBus.Error.TimedOut"
b5ae06
+
b5ae06
+     MAINPID=...  The main pid of a daemon, in case systemd did not
b5ae06
+                  fork off the process itself. Example: "MAINPID=4711"
b5ae06
+
b5ae06
+  Daemons can choose to send additional variables. However, it is
b5ae06
+  recommended to prefix variable names not listed above with X_.
b5ae06
+
b5ae06
+  Returns a negative errno-style error code on failure. Returns > 0
b5ae06
+  if systemd could be notified, 0 if it couldn't possibly because
b5ae06
+  systemd is not running.
b5ae06
+
b5ae06
+  Example: When a daemon finished starting up, it could issue this
b5ae06
+  call to notify systemd about it:
b5ae06
+
b5ae06
+     sd_notify(0, "READY=1");
b5ae06
+
b5ae06
+  See sd_notifyf() for more complete examples.
b5ae06
+
b5ae06
+  See sd_notify(3) for more information.
b5ae06
+*/
b5ae06
+int netsnmp_sd_notify(int unset_environment, const char *state);
b5ae06
+
b5ae06
+/*
b5ae06
+  Similar to sd_notify() but takes a format string.
b5ae06
+
b5ae06
+  Example 1: A daemon could send the following after initialization:
b5ae06
+
b5ae06
+     sd_notifyf(0, "READY=1\n"
b5ae06
+                   "STATUS=Processing requests...\n"
b5ae06
+                   "MAINPID=%lu",
b5ae06
+                   (unsigned long) getpid());
b5ae06
+
b5ae06
+  Example 2: A daemon could send the following shortly before
b5ae06
+  exiting, on failure:
b5ae06
+
b5ae06
+     sd_notifyf(0, "STATUS=Failed to start up: %s\n"
b5ae06
+                   "ERRNO=%i",
b5ae06
+                   strerror(errno),
b5ae06
+                   errno);
b5ae06
+
b5ae06
+  See sd_notifyf(3) for more information.
b5ae06
+*/
b5ae06
+int netsnmp_sd_notifyf(int unset_environment, const char *format, ...) _sd_printf_attr_(2,3);
b5ae06
+
b5ae06
+/*
b5ae06
+  Returns > 0 if the system was booted with systemd. Returns < 0 on
b5ae06
+  error. Returns 0 if the system was not booted with systemd. Note
b5ae06
+  that all of the functions above handle non-systemd boots just
b5ae06
+  fine. You should NOT protect them with a call to this function. Also
b5ae06
+  note that this function checks whether the system, not the user
b5ae06
+  session is controlled by systemd. However the functions above work
b5ae06
+  for both user and system services.
b5ae06
+
b5ae06
+  See sd_booted(3) for more information.
b5ae06
+*/
b5ae06
+int netsnmp_sd_booted(void);
b5ae06
+
b5ae06
+/**
b5ae06
+ * Find an socket with given parameters. See man sd_is_socket_inet for
b5ae06
+ * description of the arguments.
b5ae06
+ *
b5ae06
+ * Returns the file descriptor if it is found, 0 otherwise.
b5ae06
+ */
b5ae06
+int netsnmp_sd_find_inet_socket(int family, int type, int listening, int port);
b5ae06
+
b5ae06
+/**
b5ae06
+ * Find an unix socket with given parameters. See man sd_is_socket_unix for
b5ae06
+ * description of the arguments.
b5ae06
+ *
b5ae06
+ * Returns the file descriptor if it is found, 0 otherwise.
b5ae06
+ */
b5ae06
+int
b5ae06
+netsnmp_sd_find_unix_socket(int type, int listening, const char *path);
b5ae06
+
b5ae06
+#ifdef __cplusplus
b5ae06
+}
b5ae06
+#endif
b5ae06
+
b5ae06
+#endif /* SNMPD_SD_DAEMON_H */
b5ae06
diff -up net-snmp-5.7.2/include/net-snmp/net-snmp-config.h.in.systemd net-snmp-5.7.2/include/net-snmp/net-snmp-config.h.in
b5ae06
--- net-snmp-5.7.2/include/net-snmp/net-snmp-config.h.in.systemd	2012-10-10 00:28:58.000000000 +0200
b5ae06
+++ net-snmp-5.7.2/include/net-snmp/net-snmp-config.h.in	2012-11-12 10:18:46.107369501 +0100
b5ae06
@@ -1389,6 +1389,9 @@
b5ae06
 /* If you don't have root access don't exit upon kmem errors */
b5ae06
 #undef NETSNMP_NO_ROOT_ACCESS
b5ae06
 
b5ae06
+/* If you don't want to integrate with systemd. */
b5ae06
+#undef NETSNMP_NO_SYSTEMD
b5ae06
+
b5ae06
 /* Define if you want to remove all SET/write access from the code */
b5ae06
 #undef NETSNMP_NO_WRITE_SUPPORT
b5ae06
 
b5ae06
diff -up net-snmp-5.7.2/README.systemd.systemd net-snmp-5.7.2/README.systemd
b5ae06
--- net-snmp-5.7.2/README.systemd.systemd	2012-11-12 10:18:46.108369499 +0100
b5ae06
+++ net-snmp-5.7.2/README.systemd	2012-11-12 10:18:46.108369499 +0100
b5ae06
@@ -0,0 +1,41 @@
b5ae06
+README.systemd
b5ae06
+--------------
b5ae06
+Net-SNMP provides two daemons, which support systemd system manager. 
b5ae06
+See http://www.freedesktop.org/wiki/Software/systemd to learn how
b5ae06
+systemd works. Both socket activation and notification is supported by these
b5ae06
+daemons.
b5ae06
+
b5ae06
+To enable systemd support, the sources must be compiled with
b5ae06
+--with-systemd configure option.
b5ae06
+
b5ae06
+snmpd - The SNMP agent
b5ae06
+----------------------
b5ae06
+Socket activation od snmpd daemon is implemented, but it's discouraged.
b5ae06
+The reason is simple - snmpd not only listens and processes SNMP requests
b5ae06
+from network, but also gathers system statistics counters, sends traps and
b5ae06
+communicates with subagents. It even opens few netlink sockets.
b5ae06
+
b5ae06
+In other words, snmpd should run from system start to properly work.
b5ae06
+This can be done in two ways:
b5ae06
+1) either as snmpd service unit with 'Type=notification' and without a socket
b5ae06
+   unit
b5ae06
+2) or as snmpd service unit with 'Type=simple', appropriate socket socket unit
b5ae06
+   and the snmpd service enabled. This way systemd creates the snmpd listening
b5ae06
+   socket early during boot and passes the sockets to snmpd slightly later
b5ae06
+   (but still during machine boot). This way systemd can paralelize start of
b5ae06
+   services, which depend on snmpd. Admins must adjust the socket file manually,
b5ae06
+   depending if the snmpd support AgentX, IPv6, SMUX etc.
b5ae06
+
b5ae06
+snmpd should be started with '-f' command line parameter to disable forking -
b5ae06
+systemd does that for us automatically.
b5ae06
+
b5ae06
+
b5ae06
+snmptrapd - The trap processing daemon
b5ae06
+--------------------------------------
b5ae06
+snmptrapd supports full socket activation and also notification (if needed).
b5ae06
+Both 'Type=simple' (with appropriate socket unit) and 'Type=notify' services
b5ae06
+will work. Again, '-f' parameter should be provided on snmptrapd command line.
b5ae06
+
b5ae06
+If integration with SNMP agent using AgentX protocol is enabled, snmptrapd should
b5ae06
+start during boot and not after first SNMP trap arrives. Same rules as for snmpd
b5ae06
+applies then.
b5ae06
\ No newline at end of file
b5ae06
diff -up net-snmp-5.7.2/snmplib/sd-daemon.c.systemd net-snmp-5.7.2/snmplib/sd-daemon.c
b5ae06
--- net-snmp-5.7.2/snmplib/sd-daemon.c.systemd	2012-11-12 10:18:46.109369497 +0100
b5ae06
+++ net-snmp-5.7.2/snmplib/sd-daemon.c	2012-11-12 10:18:46.109369497 +0100
b5ae06
@@ -0,0 +1,532 @@
b5ae06
+/*
b5ae06
+ * Systemd integration parts.
b5ae06
+ *
b5ae06
+ * Most of this file is directly copied from systemd sources.
b5ae06
+ * Changes:
b5ae06
+ * - all functions were renamed to have netsnmp_ prefix
b5ae06
+ * - includes were  changed to match Net-SNMP style.
b5ae06
+ * - removed gcc export macros
b5ae06
+ * - removed POSIX message queues
b5ae06
+ */
b5ae06
+
b5ae06
+#include <net-snmp/net-snmp-config.h>
b5ae06
+#include <net-snmp/net-snmp-features.h>
b5ae06
+#include <net-snmp/types.h>
b5ae06
+#include <net-snmp/library/snmp_debug.h>
b5ae06
+
b5ae06
+#ifndef NETSNMP_NO_SYSTEMD
b5ae06
+
b5ae06
+/***
b5ae06
+  Copyright 2010 Lennart Poettering
b5ae06
+
b5ae06
+  Permission is hereby granted, free of charge, to any person
b5ae06
+  obtaining a copy of this software and associated documentation files
b5ae06
+  (the "Software"), to deal in the Software without restriction,
b5ae06
+  including without limitation the rights to use, copy, modify, merge,
b5ae06
+  publish, distribute, sublicense, and/or sell copies of the Software,
b5ae06
+  and to permit persons to whom the Software is furnished to do so,
b5ae06
+  subject to the following conditions:
b5ae06
+
b5ae06
+  The above copyright notice and this permission notice shall be
b5ae06
+  included in all copies or substantial portions of the Software.
b5ae06
+
b5ae06
+  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
b5ae06
+  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
b5ae06
+  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
b5ae06
+  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
b5ae06
+  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
b5ae06
+  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
b5ae06
+  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
b5ae06
+  SOFTWARE.
b5ae06
+***/
b5ae06
+
b5ae06
+#ifndef _GNU_SOURCE
b5ae06
+#define _GNU_SOURCE
b5ae06
+#endif
b5ae06
+
b5ae06
+#include <sys/types.h>
b5ae06
+#include <sys/stat.h>
b5ae06
+#include <sys/socket.h>
b5ae06
+#include <sys/un.h>
b5ae06
+#include <sys/fcntl.h>
b5ae06
+#include <netinet/in.h>
b5ae06
+#include <stdlib.h>
b5ae06
+#include <errno.h>
b5ae06
+#include <unistd.h>
b5ae06
+#include <string.h>
b5ae06
+#include <stdarg.h>
b5ae06
+#include <stdio.h>
b5ae06
+#include <stddef.h>
b5ae06
+#include <limits.h>
b5ae06
+
b5ae06
+#include <net-snmp/library/sd-daemon.h>
b5ae06
+
b5ae06
+int netsnmp_sd_listen_fds(int unset_environment) {
b5ae06
+
b5ae06
+        int r, fd;
b5ae06
+        const char *e;
b5ae06
+        char *p = NULL;
b5ae06
+        unsigned long l;
b5ae06
+
b5ae06
+        if (!(e = getenv("LISTEN_PID"))) {
b5ae06
+                r = 0;
b5ae06
+                goto finish;
b5ae06
+        }
b5ae06
+
b5ae06
+        errno = 0;
b5ae06
+        l = strtoul(e, &p, 10);
b5ae06
+
b5ae06
+        if (errno != 0) {
b5ae06
+                r = -errno;
b5ae06
+                goto finish;
b5ae06
+        }
b5ae06
+
b5ae06
+        if (!p || *p || l <= 0) {
b5ae06
+                r = -EINVAL;
b5ae06
+                goto finish;
b5ae06
+        }
b5ae06
+
b5ae06
+        /* Is this for us? */
b5ae06
+        if (getpid() != (pid_t) l) {
b5ae06
+                r = 0;
b5ae06
+                goto finish;
b5ae06
+        }
b5ae06
+
b5ae06
+        if (!(e = getenv("LISTEN_FDS"))) {
b5ae06
+                r = 0;
b5ae06
+                goto finish;
b5ae06
+        }
b5ae06
+
b5ae06
+        errno = 0;
b5ae06
+        l = strtoul(e, &p, 10);
b5ae06
+
b5ae06
+        if (errno != 0) {
b5ae06
+                r = -errno;
b5ae06
+                goto finish;
b5ae06
+        }
b5ae06
+
b5ae06
+        if (!p || *p) {
b5ae06
+                r = -EINVAL;
b5ae06
+                goto finish;
b5ae06
+        }
b5ae06
+
b5ae06
+        for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + (int) l; fd ++) {
b5ae06
+                int flags;
b5ae06
+
b5ae06
+                if ((flags = fcntl(fd, F_GETFD)) < 0) {
b5ae06
+                        r = -errno;
b5ae06
+                        goto finish;
b5ae06
+                }
b5ae06
+
b5ae06
+                if (flags & FD_CLOEXEC)
b5ae06
+                        continue;
b5ae06
+
b5ae06
+                if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) {
b5ae06
+                        r = -errno;
b5ae06
+                        goto finish;
b5ae06
+                }
b5ae06
+        }
b5ae06
+
b5ae06
+        r = (int) l;
b5ae06
+
b5ae06
+finish:
b5ae06
+        if (unset_environment) {
b5ae06
+                unsetenv("LISTEN_PID");
b5ae06
+                unsetenv("LISTEN_FDS");
b5ae06
+        }
b5ae06
+
b5ae06
+        return r;
b5ae06
+}
b5ae06
+
b5ae06
+int netsnmp_sd_is_fifo(int fd, const char *path) {
b5ae06
+        struct stat st_fd;
b5ae06
+
b5ae06
+        if (fd < 0)
b5ae06
+                return -EINVAL;
b5ae06
+
b5ae06
+        memset(&st_fd, 0, sizeof(st_fd));
b5ae06
+        if (fstat(fd, &st_fd) < 0)
b5ae06
+                return -errno;
b5ae06
+
b5ae06
+        if (!S_ISFIFO(st_fd.st_mode))
b5ae06
+                return 0;
b5ae06
+
b5ae06
+        if (path) {
b5ae06
+                struct stat st_path;
b5ae06
+
b5ae06
+                memset(&st_path, 0, sizeof(st_path));
b5ae06
+                if (stat(path, &st_path) < 0) {
b5ae06
+
b5ae06
+                        if (errno == ENOENT || errno == ENOTDIR)
b5ae06
+                                return 0;
b5ae06
+
b5ae06
+                        return -errno;
b5ae06
+                }
b5ae06
+
b5ae06
+                return
b5ae06
+                        st_path.st_dev == st_fd.st_dev &&
b5ae06
+                        st_path.st_ino == st_fd.st_ino;
b5ae06
+        }
b5ae06
+
b5ae06
+        return 1;
b5ae06
+}
b5ae06
+
b5ae06
+int netsnmp_sd_is_special(int fd, const char *path) {
b5ae06
+        struct stat st_fd;
b5ae06
+
b5ae06
+        if (fd < 0)
b5ae06
+                return -EINVAL;
b5ae06
+
b5ae06
+        if (fstat(fd, &st_fd) < 0)
b5ae06
+                return -errno;
b5ae06
+
b5ae06
+        if (!S_ISREG(st_fd.st_mode) && !S_ISCHR(st_fd.st_mode))
b5ae06
+                return 0;
b5ae06
+
b5ae06
+        if (path) {
b5ae06
+                struct stat st_path;
b5ae06
+
b5ae06
+                if (stat(path, &st_path) < 0) {
b5ae06
+
b5ae06
+                        if (errno == ENOENT || errno == ENOTDIR)
b5ae06
+                                return 0;
b5ae06
+
b5ae06
+                        return -errno;
b5ae06
+                }
b5ae06
+
b5ae06
+                if (S_ISREG(st_fd.st_mode) && S_ISREG(st_path.st_mode))
b5ae06
+                        return
b5ae06
+                                st_path.st_dev == st_fd.st_dev &&
b5ae06
+                                st_path.st_ino == st_fd.st_ino;
b5ae06
+                else if (S_ISCHR(st_fd.st_mode) && S_ISCHR(st_path.st_mode))
b5ae06
+                        return st_path.st_rdev == st_fd.st_rdev;
b5ae06
+                else
b5ae06
+                        return 0;
b5ae06
+        }
b5ae06
+
b5ae06
+        return 1;
b5ae06
+}
b5ae06
+
b5ae06
+static int sd_is_socket_internal(int fd, int type, int listening) {
b5ae06
+        struct stat st_fd;
b5ae06
+
b5ae06
+        if (fd < 0 || type < 0)
b5ae06
+                return -EINVAL;
b5ae06
+
b5ae06
+        if (fstat(fd, &st_fd) < 0)
b5ae06
+                return -errno;
b5ae06
+
b5ae06
+        if (!S_ISSOCK(st_fd.st_mode))
b5ae06
+                return 0;
b5ae06
+
b5ae06
+        if (type != 0) {
b5ae06
+                int other_type = 0;
b5ae06
+                socklen_t l = sizeof(other_type);
b5ae06
+
b5ae06
+                if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &other_type, &l) < 0)
b5ae06
+                        return -errno;
b5ae06
+
b5ae06
+                if (l != sizeof(other_type))
b5ae06
+                        return -EINVAL;
b5ae06
+
b5ae06
+                if (other_type != type)
b5ae06
+                        return 0;
b5ae06
+        }
b5ae06
+
b5ae06
+        if (listening >= 0) {
b5ae06
+                int accepting = 0;
b5ae06
+                socklen_t l = sizeof(accepting);
b5ae06
+
b5ae06
+                if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &accepting, &l) < 0)
b5ae06
+                        return -errno;
b5ae06
+
b5ae06
+                if (l != sizeof(accepting))
b5ae06
+                        return -EINVAL;
b5ae06
+
b5ae06
+                if (!accepting != !listening)
b5ae06
+                        return 0;
b5ae06
+        }
b5ae06
+
b5ae06
+        return 1;
b5ae06
+}
b5ae06
+
b5ae06
+union sockaddr_union {
b5ae06
+        struct sockaddr sa;
b5ae06
+        struct sockaddr_in in4;
b5ae06
+        struct sockaddr_in6 in6;
b5ae06
+        struct sockaddr_un un;
b5ae06
+        struct sockaddr_storage storage;
b5ae06
+};
b5ae06
+
b5ae06
+int netsnmp_sd_is_socket(int fd, int family, int type, int listening) {
b5ae06
+        int r;
b5ae06
+
b5ae06
+        if (family < 0)
b5ae06
+                return -EINVAL;
b5ae06
+
b5ae06
+        if ((r = sd_is_socket_internal(fd, type, listening)) <= 0)
b5ae06
+                return r;
b5ae06
+
b5ae06
+        if (family > 0) {
b5ae06
+                union sockaddr_union sockaddr;
b5ae06
+                socklen_t l;
b5ae06
+
b5ae06
+                memset(&sockaddr, 0, sizeof(sockaddr));
b5ae06
+                l = sizeof(sockaddr);
b5ae06
+
b5ae06
+                if (getsockname(fd, &sockaddr.sa, &l) < 0)
b5ae06
+                        return -errno;
b5ae06
+
b5ae06
+                if (l < sizeof(sa_family_t))
b5ae06
+                        return -EINVAL;
b5ae06
+
b5ae06
+                return sockaddr.sa.sa_family == family;
b5ae06
+        }
b5ae06
+
b5ae06
+        return 1;
b5ae06
+}
b5ae06
+
b5ae06
+int netsnmp_sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port) {
b5ae06
+        union sockaddr_union sockaddr;
b5ae06
+        socklen_t l;
b5ae06
+        int r;
b5ae06
+
b5ae06
+        if (family != 0 && family != AF_INET && family != AF_INET6)
b5ae06
+                return -EINVAL;
b5ae06
+
b5ae06
+        if ((r = sd_is_socket_internal(fd, type, listening)) <= 0)
b5ae06
+                return r;
b5ae06
+
b5ae06
+        memset(&sockaddr, 0, sizeof(sockaddr));
b5ae06
+        l = sizeof(sockaddr);
b5ae06
+
b5ae06
+        if (getsockname(fd, &sockaddr.sa, &l) < 0)
b5ae06
+                return -errno;
b5ae06
+
b5ae06
+        if (l < sizeof(sa_family_t))
b5ae06
+                return -EINVAL;
b5ae06
+
b5ae06
+        if (sockaddr.sa.sa_family != AF_INET &&
b5ae06
+            sockaddr.sa.sa_family != AF_INET6)
b5ae06
+                return 0;
b5ae06
+
b5ae06
+        if (family > 0)
b5ae06
+                if (sockaddr.sa.sa_family != family)
b5ae06
+                        return 0;
b5ae06
+
b5ae06
+        if (port > 0) {
b5ae06
+                if (sockaddr.sa.sa_family == AF_INET) {
b5ae06
+                        if (l < sizeof(struct sockaddr_in))
b5ae06
+                                return -EINVAL;
b5ae06
+
b5ae06
+                        return htons(port) == sockaddr.in4.sin_port;
b5ae06
+                } else {
b5ae06
+                        if (l < sizeof(struct sockaddr_in6))
b5ae06
+                                return -EINVAL;
b5ae06
+
b5ae06
+                        return htons(port) == sockaddr.in6.sin6_port;
b5ae06
+                }
b5ae06
+        }
b5ae06
+
b5ae06
+        return 1;
b5ae06
+}
b5ae06
+
b5ae06
+int netsnmp_sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length) {
b5ae06
+        union sockaddr_union sockaddr;
b5ae06
+        socklen_t l;
b5ae06
+        int r;
b5ae06
+
b5ae06
+        if ((r = sd_is_socket_internal(fd, type, listening)) <= 0)
b5ae06
+                return r;
b5ae06
+
b5ae06
+        memset(&sockaddr, 0, sizeof(sockaddr));
b5ae06
+        l = sizeof(sockaddr);
b5ae06
+
b5ae06
+        if (getsockname(fd, &sockaddr.sa, &l) < 0)
b5ae06
+                return -errno;
b5ae06
+
b5ae06
+        if (l < sizeof(sa_family_t))
b5ae06
+                return -EINVAL;
b5ae06
+
b5ae06
+        if (sockaddr.sa.sa_family != AF_UNIX)
b5ae06
+                return 0;
b5ae06
+
b5ae06
+        if (path) {
b5ae06
+                if (length <= 0)
b5ae06
+                        length = strlen(path);
b5ae06
+
b5ae06
+                if (length <= 0)
b5ae06
+                        /* Unnamed socket */
b5ae06
+                        return l == offsetof(struct sockaddr_un, sun_path);
b5ae06
+
b5ae06
+                if (path[0])
b5ae06
+                        /* Normal path socket */
b5ae06
+                        return
b5ae06
+                                (l >= offsetof(struct sockaddr_un, sun_path) + length + 1) &&
b5ae06
+                                memcmp(path, sockaddr.un.sun_path, length+1) == 0;
b5ae06
+                else
b5ae06
+                        /* Abstract namespace socket */
b5ae06
+                        return
b5ae06
+                                (l == offsetof(struct sockaddr_un, sun_path) + length) &&
b5ae06
+                                memcmp(path, sockaddr.un.sun_path, length) == 0;
b5ae06
+        }
b5ae06
+
b5ae06
+        return 1;
b5ae06
+}
b5ae06
+
b5ae06
+int netsnmp_sd_notify(int unset_environment, const char *state) {
b5ae06
+        int fd = -1, r;
b5ae06
+        struct msghdr msghdr;
b5ae06
+        struct iovec iovec;
b5ae06
+        union sockaddr_union sockaddr;
b5ae06
+        const char *e;
b5ae06
+
b5ae06
+        if (!state) {
b5ae06
+                r = -EINVAL;
b5ae06
+                goto finish;
b5ae06
+        }
b5ae06
+
b5ae06
+        if (!(e = getenv("NOTIFY_SOCKET")))
b5ae06
+                return 0;
b5ae06
+
b5ae06
+        /* Must be an abstract socket, or an absolute path */
b5ae06
+        if ((e[0] != '@' && e[0] != '/') || e[1] == 0) {
b5ae06
+                r = -EINVAL;
b5ae06
+                goto finish;
b5ae06
+        }
b5ae06
+
b5ae06
+        if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) {
b5ae06
+                r = -errno;
b5ae06
+                goto finish;
b5ae06
+        }
b5ae06
+
b5ae06
+        memset(&sockaddr, 0, sizeof(sockaddr));
b5ae06
+        sockaddr.sa.sa_family = AF_UNIX;
b5ae06
+        strncpy(sockaddr.un.sun_path, e, sizeof(sockaddr.un.sun_path));
b5ae06
+
b5ae06
+        if (sockaddr.un.sun_path[0] == '@')
b5ae06
+                sockaddr.un.sun_path[0] = 0;
b5ae06
+
b5ae06
+        memset(&iovec, 0, sizeof(iovec));
b5ae06
+        iovec.iov_base = (char *)state;
b5ae06
+        iovec.iov_len = strlen(state);
b5ae06
+
b5ae06
+        memset(&msghdr, 0, sizeof(msghdr));
b5ae06
+        msghdr.msg_name = &sockaddr;
b5ae06
+        msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(e);
b5ae06
+
b5ae06
+        if (msghdr.msg_namelen > sizeof(struct sockaddr_un))
b5ae06
+                msghdr.msg_namelen = sizeof(struct sockaddr_un);
b5ae06
+
b5ae06
+        msghdr.msg_iov = &iovec;
b5ae06
+        msghdr.msg_iovlen = 1;
b5ae06
+
b5ae06
+        if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) {
b5ae06
+                r = -errno;
b5ae06
+                goto finish;
b5ae06
+        }
b5ae06
+
b5ae06
+        r = 1;
b5ae06
+
b5ae06
+finish:
b5ae06
+        if (unset_environment)
b5ae06
+                unsetenv("NOTIFY_SOCKET");
b5ae06
+
b5ae06
+        if (fd >= 0)
b5ae06
+                close(fd);
b5ae06
+
b5ae06
+        return r;
b5ae06
+}
b5ae06
+
b5ae06
+int netsnmp_sd_notifyf(int unset_environment, const char *format, ...) {
b5ae06
+        va_list ap;
b5ae06
+        char *p = NULL;
b5ae06
+        int r;
b5ae06
+
b5ae06
+        va_start(ap, format);
b5ae06
+        r = vasprintf(&p, format, ap);
b5ae06
+        va_end(ap);
b5ae06
+
b5ae06
+        if (r < 0 || !p)
b5ae06
+                return -ENOMEM;
b5ae06
+
b5ae06
+        r = netsnmp_sd_notify(unset_environment, p);
b5ae06
+        free(p);
b5ae06
+
b5ae06
+        return r;
b5ae06
+}
b5ae06
+
b5ae06
+int netsnmp_sd_booted(void) {
b5ae06
+        struct stat a, b;
b5ae06
+
b5ae06
+        /* We simply test whether the systemd cgroup hierarchy is
b5ae06
+         * mounted */
b5ae06
+
b5ae06
+        if (lstat("/sys/fs/cgroup", &a) < 0)
b5ae06
+                return 0;
b5ae06
+
b5ae06
+        if (lstat("/sys/fs/cgroup/systemd", &b) < 0)
b5ae06
+                return 0;
b5ae06
+
b5ae06
+        return a.st_dev != b.st_dev;
b5ae06
+}
b5ae06
+
b5ae06
+/* End of original sd-daemon.c from systemd sources */
b5ae06
+
b5ae06
+int
b5ae06
+netsnmp_sd_find_inet_socket(int family, int type, int listening, int port)
b5ae06
+{
b5ae06
+    int count, fd;
b5ae06
+
b5ae06
+    count = netsnmp_sd_listen_fds(0);
b5ae06
+    if (count <= 0) {
b5ae06
+        DEBUGMSGTL(("systemd:find_inet_socket", "No LISTEN_FDS found.\n"));
b5ae06
+        return 0;
b5ae06
+    }
b5ae06
+    DEBUGMSGTL(("systemd:find_inet_socket", "LISTEN_FDS reports %d sockets.\n",
b5ae06
+            count));
b5ae06
+
b5ae06
+    for (fd = 3; fd < 3+count; fd++) {
b5ae06
+        int rc = netsnmp_sd_is_socket_inet(fd, family, type, listening, port);
b5ae06
+        if (rc < 0)
b5ae06
+            DEBUGMSGTL(("systemd:find_inet_socket",
b5ae06
+                    "sd_is_socket_inet error: %d\n", rc));
b5ae06
+        if (rc > 0) {
b5ae06
+            DEBUGMSGTL(("systemd:find_inet_socket",
b5ae06
+                    "Found the socket in LISTEN_FDS\n"));
b5ae06
+            return fd;
b5ae06
+        }
b5ae06
+    }
b5ae06
+    DEBUGMSGTL(("systemd:find_inet_socket", "Socket not found in LISTEN_FDS\n"));
b5ae06
+    return 0;
b5ae06
+}
b5ae06
+
b5ae06
+int
b5ae06
+netsnmp_sd_find_unix_socket(int type, int listening, const char *path)
b5ae06
+{
b5ae06
+    int count, fd;
b5ae06
+
b5ae06
+    count = netsnmp_sd_listen_fds(0);
b5ae06
+    if (count <= 0) {
b5ae06
+        DEBUGMSGTL(("systemd:find_unix_socket", "No LISTEN_FDS found.\n"));
b5ae06
+        return 0;
b5ae06
+    }
b5ae06
+    DEBUGMSGTL(("systemd:find_unix_socket", "LISTEN_FDS reports %d sockets.\n",
b5ae06
+            count));
b5ae06
+
b5ae06
+    for (fd = 3; fd < 3+count; fd++) {
b5ae06
+        int rc = netsnmp_sd_is_socket_unix(fd, type, listening, path, 0);
b5ae06
+        if (rc < 0)
b5ae06
+            DEBUGMSGTL(("systemd:find_unix_socket",
b5ae06
+                    "netsnmp_sd_is_socket_unix error: %d\n", rc));
b5ae06
+        if (rc > 0) {
b5ae06
+            DEBUGMSGTL(("systemd:find_unix_socket",
b5ae06
+                    "Found the socket in LISTEN_FDS\n"));
b5ae06
+            return fd;
b5ae06
+        }
b5ae06
+    }
b5ae06
+    DEBUGMSGTL(("systemd:find_unix_socket", "Socket not found in LISTEN_FDS\n"));
b5ae06
+    return 0;
b5ae06
+}
b5ae06
+
b5ae06
+#endif /* ! NETSNMP_NO_SYSTEMD */
b5ae06
diff -up net-snmp-5.7.2/snmplib/transports/snmpTCPDomain.c.systemd net-snmp-5.7.2/snmplib/transports/snmpTCPDomain.c
b5ae06
--- net-snmp-5.7.2/snmplib/transports/snmpTCPDomain.c.systemd	2012-10-10 00:28:58.000000000 +0200
b5ae06
+++ net-snmp-5.7.2/snmplib/transports/snmpTCPDomain.c	2012-11-12 10:19:41.767217067 +0100
b5ae06
@@ -43,6 +43,10 @@
b5ae06
 #include <net-snmp/library/snmpTCPBaseDomain.h>
b5ae06
 #include <net-snmp/library/tools.h>
b5ae06
 
b5ae06
+#ifndef NETSNMP_NO_SYSTEMD
b5ae06
+#include <net-snmp/library/sd-daemon.h>
b5ae06
+#endif
b5ae06
+
b5ae06
 /*
b5ae06
  * needs to be in sync with the definitions in snmplib/snmpUDPDomain.c
b5ae06
  * and perl/agent/agent.xs
b5ae06
@@ -149,6 +153,7 @@ netsnmp_tcp_transport(struct sockaddr_in
b5ae06
     netsnmp_transport *t = NULL;
b5ae06
     netsnmp_udp_addr_pair *addr_pair = NULL;
b5ae06
     int rc = 0;
b5ae06
+    int socket_initialized = 0;
b5ae06
 
b5ae06
 #ifdef NETSNMP_NO_LISTEN_SUPPORT
b5ae06
     if (local)
b5ae06
@@ -178,7 +183,19 @@ netsnmp_tcp_transport(struct sockaddr_in
b5ae06
     t->domain_length =
b5ae06
         sizeof(netsnmp_snmpTCPDomain) / sizeof(netsnmp_snmpTCPDomain[0]);
b5ae06
 
b5ae06
-    t->sock = socket(PF_INET, SOCK_STREAM, 0);
b5ae06
+#ifndef NETSNMP_NO_SYSTEMD
b5ae06
+    /*
b5ae06
+     * Maybe the socket was already provided by systemd...
b5ae06
+     */
b5ae06
+    if (local) {
b5ae06
+        t->sock = netsnmp_sd_find_inet_socket(PF_INET, SOCK_STREAM, 1,
b5ae06
+                ntohs(addr->sin_port));
b5ae06
+        if (t->sock)
b5ae06
+            socket_initialized = 1;
b5ae06
+    }
b5ae06
+#endif
b5ae06
+    if (!socket_initialized)
b5ae06
+        t->sock = socket(PF_INET, SOCK_STREAM, 0);
b5ae06
     if (t->sock < 0) {
b5ae06
         netsnmp_transport_free(t);
b5ae06
         return NULL;
b5ae06
@@ -215,11 +232,13 @@ netsnmp_tcp_transport(struct sockaddr_in
b5ae06
         setsockopt(t->sock, SOL_SOCKET, SO_REUSEADDR, (void *)&opt,
b5ae06
 		   sizeof(opt));
b5ae06
 
b5ae06
-        rc = bind(t->sock, (struct sockaddr *)addr, sizeof(struct sockaddr));
b5ae06
-        if (rc != 0) {
b5ae06
-            netsnmp_socketbase_close(t);
b5ae06
-            netsnmp_transport_free(t);
b5ae06
-            return NULL;
b5ae06
+        if (!socket_initialized) {
b5ae06
+            rc = bind(t->sock, (struct sockaddr *)addr, sizeof(struct sockaddr));
b5ae06
+            if (rc != 0) {
b5ae06
+                netsnmp_socketbase_close(t);
b5ae06
+                netsnmp_transport_free(t);
b5ae06
+                return NULL;
b5ae06
+            }
b5ae06
         }
b5ae06
 
b5ae06
         /*
b5ae06
@@ -235,12 +254,13 @@ netsnmp_tcp_transport(struct sockaddr_in
b5ae06
         /*
b5ae06
          * Now sit here and wait for connections to arrive.  
b5ae06
          */
b5ae06
-
b5ae06
-        rc = listen(t->sock, NETSNMP_STREAM_QUEUE_LEN);
b5ae06
-        if (rc != 0) {
b5ae06
-            netsnmp_socketbase_close(t);
b5ae06
-            netsnmp_transport_free(t);
b5ae06
-            return NULL;
b5ae06
+        if (!socket_initialized) {
b5ae06
+            rc = listen(t->sock, NETSNMP_STREAM_QUEUE_LEN);
b5ae06
+            if (rc != 0) {
b5ae06
+                netsnmp_socketbase_close(t);
b5ae06
+                netsnmp_transport_free(t);
b5ae06
+                return NULL;
b5ae06
+            }
b5ae06
         }
b5ae06
         
b5ae06
         /*
b5ae06
diff -up net-snmp-5.7.2/snmplib/transports/snmpTCPIPv6Domain.c.systemd net-snmp-5.7.2/snmplib/transports/snmpTCPIPv6Domain.c
b5ae06
--- net-snmp-5.7.2/snmplib/transports/snmpTCPIPv6Domain.c.systemd	2012-10-10 00:28:58.000000000 +0200
b5ae06
+++ net-snmp-5.7.2/snmplib/transports/snmpTCPIPv6Domain.c	2012-11-12 10:20:32.019078971 +0100
b5ae06
@@ -49,6 +49,10 @@
b5ae06
 #include <net-snmp/library/snmpTCPBaseDomain.h>
b5ae06
 #include <net-snmp/library/tools.h>
b5ae06
 
b5ae06
+#ifndef NETSNMP_NO_SYSTEMD
b5ae06
+#include <net-snmp/library/sd-daemon.h>
b5ae06
+#endif
b5ae06
+
b5ae06
 #include "inet_ntop.h"
b5ae06
 
b5ae06
 oid netsnmp_TCPIPv6Domain[] = { TRANSPORT_DOMAIN_TCP_IPV6 };
b5ae06
@@ -140,6 +144,7 @@ netsnmp_tcp6_transport(struct sockaddr_i
b5ae06
 {
b5ae06
     netsnmp_transport *t = NULL;
b5ae06
     int             rc = 0;
b5ae06
+    int             socket_initialized = 0;
b5ae06
 
b5ae06
 #ifdef NETSNMP_NO_LISTEN_SUPPORT
b5ae06
     if (local)
b5ae06
@@ -174,7 +179,19 @@ netsnmp_tcp6_transport(struct sockaddr_i
b5ae06
     t->domain = netsnmp_TCPIPv6Domain;
b5ae06
     t->domain_length = sizeof(netsnmp_TCPIPv6Domain) / sizeof(oid);
b5ae06
 
b5ae06
-    t->sock = socket(PF_INET6, SOCK_STREAM, 0);
b5ae06
+#ifndef NETSNMP_NO_SYSTEMD
b5ae06
+    /*
b5ae06
+     * Maybe the socket was already provided by systemd...
b5ae06
+     */
b5ae06
+    if (local) {
b5ae06
+        t->sock = netsnmp_sd_find_inet_socket(PF_INET6, SOCK_STREAM, 1,
b5ae06
+                ntohs(addr->sin6_port));
b5ae06
+        if (t->sock)
b5ae06
+            socket_initialized = 1;
b5ae06
+    }
b5ae06
+#endif
b5ae06
+    if (!socket_initialized)
b5ae06
+        t->sock = socket(PF_INET6, SOCK_STREAM, 0);
b5ae06
     if (t->sock < 0) {
b5ae06
         netsnmp_transport_free(t);
b5ae06
         return NULL;
b5ae06
@@ -220,12 +237,14 @@ netsnmp_tcp6_transport(struct sockaddr_i
b5ae06
 
b5ae06
         setsockopt(t->sock, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, sizeof(opt));
b5ae06
 
b5ae06
-        rc = bind(t->sock, (struct sockaddr *) addr,
b5ae06
-		  sizeof(struct sockaddr_in6));
b5ae06
-        if (rc != 0) {
b5ae06
-            netsnmp_socketbase_close(t);
b5ae06
-            netsnmp_transport_free(t);
b5ae06
-            return NULL;
b5ae06
+        if (!socket_initialized) {
b5ae06
+            rc = bind(t->sock, (struct sockaddr *) addr,
b5ae06
+                    sizeof(struct sockaddr_in6));
b5ae06
+            if (rc != 0) {
b5ae06
+                netsnmp_socketbase_close(t);
b5ae06
+                netsnmp_transport_free(t);
b5ae06
+                return NULL;
b5ae06
+            }
b5ae06
         }
b5ae06
 
b5ae06
         /*
b5ae06
@@ -242,11 +261,13 @@ netsnmp_tcp6_transport(struct sockaddr_i
b5ae06
          * Now sit here and wait for connections to arrive.  
b5ae06
          */
b5ae06
 
b5ae06
-        rc = listen(t->sock, NETSNMP_STREAM_QUEUE_LEN);
b5ae06
-        if (rc != 0) {
b5ae06
-            netsnmp_socketbase_close(t);
b5ae06
-            netsnmp_transport_free(t);
b5ae06
-            return NULL;
b5ae06
+        if (!socket_initialized) {
b5ae06
+            rc = listen(t->sock, NETSNMP_STREAM_QUEUE_LEN);
b5ae06
+            if (rc != 0) {
b5ae06
+                netsnmp_socketbase_close(t);
b5ae06
+                netsnmp_transport_free(t);
b5ae06
+                return NULL;
b5ae06
+            }
b5ae06
         }
b5ae06
         
b5ae06
         /*
b5ae06
diff -up net-snmp-5.7.2/snmplib/transports/snmpUDPIPv4BaseDomain.c.systemd net-snmp-5.7.2/snmplib/transports/snmpUDPIPv4BaseDomain.c
b5ae06
--- net-snmp-5.7.2/snmplib/transports/snmpUDPIPv4BaseDomain.c.systemd	2012-10-10 00:28:58.000000000 +0200
b5ae06
+++ net-snmp-5.7.2/snmplib/transports/snmpUDPIPv4BaseDomain.c	2012-11-12 10:22:30.279750750 +0100
b5ae06
@@ -40,6 +40,10 @@
b5ae06
 
b5ae06
 #include <net-snmp/library/snmpSocketBaseDomain.h>
b5ae06
 
b5ae06
+#ifndef NETSNMP_NO_SYSTEMD
b5ae06
+#include <net-snmp/library/sd-daemon.h>
b5ae06
+#endif
b5ae06
+
b5ae06
 #if (defined(linux) && defined(IP_PKTINFO)) \
b5ae06
     || defined(IP_RECVDSTADDR) && HAVE_STRUCT_MSGHDR_MSG_CONTROL \
b5ae06
                                && HAVE_STRUCT_MSGHDR_MSG_FLAGS
b5ae06
@@ -67,6 +71,7 @@ netsnmp_udpipv4base_transport(struct soc
b5ae06
     char           *client_socket = NULL;
b5ae06
     netsnmp_indexed_addr_pair addr_pair;
b5ae06
     socklen_t       local_addr_len;
b5ae06
+    int             socket_initialized = 0;
b5ae06
 
b5ae06
 #ifdef NETSNMP_NO_LISTEN_SUPPORT
b5ae06
     if (local)
b5ae06
@@ -91,7 +96,20 @@ netsnmp_udpipv4base_transport(struct soc
b5ae06
         free(str);
b5ae06
     }
b5ae06
 
b5ae06
-    t->sock = socket(PF_INET, SOCK_DGRAM, 0);
b5ae06
+#ifndef NETSNMP_NO_SYSTEMD
b5ae06
+    /*
b5ae06
+     * Maybe the socket was already provided by systemd...
b5ae06
+     */
b5ae06
+    if (local) {
b5ae06
+        t->sock = netsnmp_sd_find_inet_socket(PF_INET, SOCK_DGRAM, -1,
b5ae06
+                ntohs(addr->sin_port));
b5ae06
+        if (t->sock)
b5ae06
+            socket_initialized = 1;
b5ae06
+    }
b5ae06
+#endif
b5ae06
+    if (!socket_initialized)
b5ae06
+        t->sock = socket(PF_INET, SOCK_DGRAM, 0);
b5ae06
+
b5ae06
     DEBUGMSGTL(("UDPBase", "openned socket %d as local=%d\n", t->sock, local)); 
b5ae06
     if (t->sock < 0) {
b5ae06
         netsnmp_transport_free(t);
b5ae06
@@ -141,13 +159,15 @@ netsnmp_udpipv4base_transport(struct soc
b5ae06
             DEBUGMSGTL(("netsnmp_udp", "set IP_RECVDSTADDR\n"));
b5ae06
         }
b5ae06
 #endif
b5ae06
-        rc = bind(t->sock, (struct sockaddr *) addr,
b5ae06
-                  sizeof(struct sockaddr));
b5ae06
-        if (rc != 0) {
b5ae06
-            netsnmp_socketbase_close(t);
b5ae06
-            netsnmp_transport_free(t);
b5ae06
-            return NULL;
b5ae06
-        }
b5ae06
+        if (!socket_initialized) {
b5ae06
+            rc = bind(t->sock, (struct sockaddr *) addr,
b5ae06
+                    sizeof(struct sockaddr));
b5ae06
+            if (rc != 0) {
b5ae06
+                netsnmp_socketbase_close(t);
b5ae06
+                netsnmp_transport_free(t);
b5ae06
+                return NULL;
b5ae06
+            }
b5ae06
+        }
b5ae06
         t->data = NULL;
b5ae06
         t->data_length = 0;
b5ae06
 #else /* NETSNMP_NO_LISTEN_SUPPORT */
b5ae06
diff -up net-snmp-5.7.2/snmplib/transports/snmpUDPIPv6Domain.c.systemd net-snmp-5.7.2/snmplib/transports/snmpUDPIPv6Domain.c
b5ae06
--- net-snmp-5.7.2/snmplib/transports/snmpUDPIPv6Domain.c.systemd	2012-10-10 00:28:58.000000000 +0200
b5ae06
+++ net-snmp-5.7.2/snmplib/transports/snmpUDPIPv6Domain.c	2012-11-12 10:23:19.713603003 +0100
b5ae06
@@ -67,6 +67,10 @@ static const struct in6_addr in6addr_any
b5ae06
 #include <net-snmp/library/snmpSocketBaseDomain.h>
b5ae06
 #include <net-snmp/library/tools.h>
b5ae06
 
b5ae06
+#ifndef NETSNMP_NO_SYSTEMD
b5ae06
+#include <net-snmp/library/sd-daemon.h>
b5ae06
+#endif
b5ae06
+
b5ae06
 #include "inet_ntop.h"
b5ae06
 #include "inet_pton.h"
b5ae06
 
b5ae06
@@ -190,6 +194,7 @@ netsnmp_udp6_transport(struct sockaddr_i
b5ae06
 {
b5ae06
     netsnmp_transport *t = NULL;
b5ae06
     int             rc = 0;
b5ae06
+    int             socket_initialized = 0;
b5ae06
 
b5ae06
 #ifdef NETSNMP_NO_LISTEN_SUPPORT
b5ae06
     if (local)
b5ae06
@@ -217,7 +222,19 @@ netsnmp_udp6_transport(struct sockaddr_i
b5ae06
     t->domain_length =
b5ae06
         sizeof(netsnmp_UDPIPv6Domain) / sizeof(netsnmp_UDPIPv6Domain[0]);
b5ae06
 
b5ae06
-    t->sock = socket(PF_INET6, SOCK_DGRAM, 0);
b5ae06
+#ifndef NETSNMP_NO_SYSTEMD
b5ae06
+    /*
b5ae06
+     * Maybe the socket was already provided by systemd...
b5ae06
+     */
b5ae06
+    if (local) {
b5ae06
+        t->sock = netsnmp_sd_find_inet_socket(PF_INET6, SOCK_DGRAM, -1,
b5ae06
+                ntohs(addr->sin6_port));
b5ae06
+        if (t->sock)
b5ae06
+            socket_initialized = 1;
b5ae06
+    }
b5ae06
+#endif
b5ae06
+    if (!socket_initialized)
b5ae06
+        t->sock = socket(PF_INET6, SOCK_DGRAM, 0);
b5ae06
     if (t->sock < 0) {
b5ae06
         netsnmp_transport_free(t);
b5ae06
         return NULL;
b5ae06
@@ -242,13 +259,14 @@ netsnmp_udp6_transport(struct sockaddr_i
b5ae06
 	  } 
b5ae06
 	}
b5ae06
 #endif
b5ae06
-
b5ae06
-        rc = bind(t->sock, (struct sockaddr *) addr,
b5ae06
-		  sizeof(struct sockaddr_in6));
b5ae06
-        if (rc != 0) {
b5ae06
-            netsnmp_socketbase_close(t);
b5ae06
-            netsnmp_transport_free(t);
b5ae06
-            return NULL;
b5ae06
+        if (!socket_initialized) {
b5ae06
+            rc = bind(t->sock, (struct sockaddr *) addr,
b5ae06
+                    sizeof(struct sockaddr_in6));
b5ae06
+            if (rc != 0) {
b5ae06
+                netsnmp_socketbase_close(t);
b5ae06
+                netsnmp_transport_free(t);
b5ae06
+                return NULL;
b5ae06
+            }
b5ae06
         }
b5ae06
         t->local = (unsigned char*)malloc(18);
b5ae06
         if (t->local == NULL) {
b5ae06
diff -up net-snmp-5.7.2/snmplib/transports/snmpUnixDomain.c.systemd net-snmp-5.7.2/snmplib/transports/snmpUnixDomain.c
b5ae06
--- net-snmp-5.7.2/snmplib/transports/snmpUnixDomain.c.systemd	2012-10-10 00:28:58.000000000 +0200
b5ae06
+++ net-snmp-5.7.2/snmplib/transports/snmpUnixDomain.c	2012-11-12 10:24:02.803466358 +0100
b5ae06
@@ -37,6 +37,10 @@
b5ae06
 #include <net-snmp/library/system.h> /* mkdirhier */
b5ae06
 #include <net-snmp/library/tools.h>
b5ae06
 
b5ae06
+#ifndef NETSNMP_NO_SYSTEMD
b5ae06
+#include <net-snmp/library/sd-daemon.h>
b5ae06
+#endif
b5ae06
+
b5ae06
 netsnmp_feature_child_of(transport_unix_socket_all, transport_all)
b5ae06
 netsnmp_feature_child_of(unix_socket_paths, transport_unix_socket_all)
b5ae06
 
b5ae06
@@ -295,6 +299,7 @@ netsnmp_unix_transport(struct sockaddr_u
b5ae06
     netsnmp_transport *t = NULL;
b5ae06
     sockaddr_un_pair *sup = NULL;
b5ae06
     int             rc = 0;
b5ae06
+    int             socket_initialized = 0;
b5ae06
 
b5ae06
 #ifdef NETSNMP_NO_LISTEN_SUPPORT
b5ae06
     /* SPECIAL CIRCUMSTANCE: We still want AgentX to be able to operate,
b5ae06
@@ -333,7 +338,18 @@ netsnmp_unix_transport(struct sockaddr_u
b5ae06
     t->data_length = sizeof(sockaddr_un_pair);
b5ae06
     sup = (sockaddr_un_pair *) t->data;
b5ae06
 
b5ae06
-    t->sock = socket(PF_UNIX, SOCK_STREAM, 0);
b5ae06
+#ifndef NETSNMP_NO_SYSTEMD
b5ae06
+    /*
b5ae06
+     * Maybe the socket was already provided by systemd...
b5ae06
+     */
b5ae06
+    if (local) {
b5ae06
+        t->sock = netsnmp_sd_find_unix_socket(SOCK_STREAM, 1, addr->sun_path);
b5ae06
+        if (t->sock)
b5ae06
+            socket_initialized = 1;
b5ae06
+    }
b5ae06
+#endif
b5ae06
+    if (!socket_initialized)
b5ae06
+        t->sock = socket(PF_UNIX, SOCK_STREAM, 0);
b5ae06
     if (t->sock < 0) {
b5ae06
         netsnmp_transport_free(t);
b5ae06
         return NULL;
b5ae06
@@ -357,25 +373,26 @@ netsnmp_unix_transport(struct sockaddr_u
b5ae06
 
b5ae06
         t->flags |= NETSNMP_TRANSPORT_FLAG_LISTEN;
b5ae06
 
b5ae06
-        unlink(addr->sun_path);
b5ae06
-        rc = bind(t->sock, (struct sockaddr *) addr, SUN_LEN(addr));
b5ae06
-
b5ae06
-        if (rc != 0 && errno == ENOENT && create_path) {
b5ae06
-            rc = mkdirhier(addr->sun_path, create_mode, 1);
b5ae06
+        if (!socket_initialized) {
b5ae06
+            unlink(addr->sun_path);
b5ae06
+            rc = bind(t->sock, (struct sockaddr *) addr, SUN_LEN(addr));
b5ae06
+            if (rc != 0 && errno == ENOENT && create_path) {
b5ae06
+                rc = mkdirhier(addr->sun_path, create_mode, 1);
b5ae06
+                if (rc != 0) {
b5ae06
+                    netsnmp_unix_close(t);
b5ae06
+                    netsnmp_transport_free(t);
b5ae06
+                    return NULL;
b5ae06
+                }
b5ae06
+                rc = bind(t->sock, (struct sockaddr *) addr, SUN_LEN(addr));
b5ae06
+            }
b5ae06
             if (rc != 0) {
b5ae06
+                DEBUGMSGTL(("netsnmp_unix_transport",
b5ae06
+                        "couldn't bind \"%s\", errno %d (%s)\n",
b5ae06
+                        addr->sun_path, errno, strerror(errno)));
b5ae06
                 netsnmp_unix_close(t);
b5ae06
                 netsnmp_transport_free(t);
b5ae06
                 return NULL;
b5ae06
             }
b5ae06
-            rc = bind(t->sock, (struct sockaddr *) addr, SUN_LEN(addr));
b5ae06
-        }
b5ae06
-        if (rc != 0) {
b5ae06
-            DEBUGMSGTL(("netsnmp_unix_transport",
b5ae06
-                        "couldn't bind \"%s\", errno %d (%s)\n",
b5ae06
-                        addr->sun_path, errno, strerror(errno)));
b5ae06
-            netsnmp_unix_close(t);
b5ae06
-            netsnmp_transport_free(t);
b5ae06
-            return NULL;
b5ae06
         }
b5ae06
 
b5ae06
         /*
b5ae06
@@ -391,16 +408,17 @@ netsnmp_unix_transport(struct sockaddr_u
b5ae06
          * Now sit here and listen for connections to arrive.
b5ae06
          */
b5ae06
 
b5ae06
-        rc = listen(t->sock, NETSNMP_STREAM_QUEUE_LEN);
b5ae06
-        if (rc != 0) {
b5ae06
-            DEBUGMSGTL(("netsnmp_unix_transport",
b5ae06
-                        "couldn't listen to \"%s\", errno %d (%s)\n",
b5ae06
-                        addr->sun_path, errno, strerror(errno)));
b5ae06
-            netsnmp_unix_close(t);
b5ae06
-            netsnmp_transport_free(t);
b5ae06
-            return NULL;
b5ae06
+        if (!socket_initialized) {
b5ae06
+            rc = listen(t->sock, NETSNMP_STREAM_QUEUE_LEN);
b5ae06
+            if (rc != 0) {
b5ae06
+                DEBUGMSGTL(("netsnmp_unix_transport",
b5ae06
+                            "couldn't listen to \"%s\", errno %d (%s)\n",
b5ae06
+                            addr->sun_path, errno, strerror(errno)));
b5ae06
+                netsnmp_unix_close(t);
b5ae06
+                netsnmp_transport_free(t);
b5ae06
+                return NULL;
b5ae06
+            }
b5ae06
         }
b5ae06
-
b5ae06
     } else {
b5ae06
         t->remote = (u_char *)malloc(strlen(addr->sun_path));
b5ae06
         if (t->remote == NULL) {