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