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

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