1 718183 - Provide native systemd unit file
3 Gathered from following upstream git commits and backported to 5.7.
5 commit 19499c3c90bf9d7b2b9e5d08baa26cc6bba28a11
6 Author: Jan Safranek <jsafranek@users.sourceforge.net>
7 Date: Mon Aug 8 15:48:54 2011 +0200
9 CHANGES: snmpd: integrated with systemd, see README.systemd for details.
11 It brings sd-daemon.c and .h directly downloaded from systemd. I've made very
12 few changes to it to match our NETSNMP_NO_SYSTEMD and include paths.
14 commit fef6cddfdb94da1a6b1fb768af62918b80f11fd3
15 Author: Jan Safranek <jsafranek@users.sourceforge.net>
16 Date: Mon Aug 8 15:48:54 2011 +0200
18 CHANGES: snmptrapd: integrate systemd notification support.
20 commit 0641e43c694c485cbbffef0556efc4641bd3ff50
21 Author: Jan Safranek <jsafranek@users.sourceforge.net>
22 Date: Mon Aug 8 15:48:54 2011 +0200
24 Add sd_find_inet_socket() and sd_find_inet_unisx() helpers into
25 system-specific code. This will help us to find various sockets
26 created by systemd much easier.
28 commit 76530a89f1c8bbd0b63acce63e10d5d4812a1a16
29 Author: Jan Safranek <jsafranek@users.sourceforge.net>
30 Date: Mon Aug 8 15:48:54 2011 +0200
32 Check sockets created by systemd when opening new server sockets.
34 systemd can pass sockets to our daemons during startup using LISTEN_FDS
35 environment variable. So check this variable when opening new listening
36 socket - maybe system has already opened the socket for us.
38 commit bf108d7f1354f6276fc43c129963f2c49b9fc242
39 Author: Jan Safranek <jsafranek@users.sourceforge.net>
40 Date: Mon Aug 8 15:48:54 2011 +0200
42 Added sample systemd service files.
44 commit 884ec488a6596380ba283d707827dd926a52e0b2
45 Author: Jan Safranek <jsafranek@users.sourceforge.net>
46 Date: Mon Aug 8 15:48:55 2011 +0200
48 Run autoheader+autoconf.
50 commit 86132e3f1e6ef7b4e0b96d8fa24e37c81b71b0e0
51 Author: Jan Safranek <jsafranek@users.sourceforge.net>
52 Date: Tue Aug 9 10:53:43 2011 +0200
54 Update systemd documentation and samples.
56 - add socket unit for snmpd to paralelize boot
57 - update WantedBy in socket units as recommended by http://0pointer.de/blog/projects/socket-activation.html
58 - rephrase README.systemd
61 diff -up net-snmp-5.7.2/agent/snmpd.c.systemd net-snmp-5.7.2/agent/snmpd.c
62 --- net-snmp-5.7.2/agent/snmpd.c.systemd 2012-10-10 00:28:58.000000000 +0200
63 +++ net-snmp-5.7.2/agent/snmpd.c 2012-11-12 10:18:46.084369548 +0100
64 @@ -164,6 +164,10 @@ typedef long fd_mask;
68 +#ifndef NETSNMP_NO_SYSTEMD
69 +#include <net-snmp/library/sd-daemon.h>
72 netsnmp_feature_want(logging_file)
73 netsnmp_feature_want(logging_stdio)
74 netsnmp_feature_want(logging_syslog)
75 @@ -441,18 +445,26 @@ main(int argc, char *argv[])
77 char *pid_file = NULL;
78 char option_compatability[] = "-Le";
79 + int prepared_sockets = 0;
86 +#ifndef NETSNMP_NO_SYSYSTEMD
87 + /* check if systemd has sockets for us and don't close them */
88 + prepared_sockets = netsnmp_sd_listen_fds(0);
89 +#endif /* NETSNMP_NO_SYSYSTEMD */
92 * close all non-standard file descriptors we may have
93 * inherited from the shell.
95 - for (i = getdtablesize() - 1; i > 2; --i) {
97 + if (!prepared_sockets) {
98 + for (i = getdtablesize() - 1; i > 2; --i) {
104 @@ -1100,6 +1112,19 @@ main(int argc, char *argv[])
105 netsnmp_addrcache_initialise();
108 + * Let systemd know we're up.
110 +#ifndef NETSNMP_NO_SYSTEMD
111 + netsnmp_sd_notify(1, "READY=1\n");
112 + if (prepared_sockets)
114 + * Clear the environment variable, we already processed all the sockets
117 + netsnmp_sd_listen_fds(1);
121 * Forever monitor the dest_port for incoming PDUs.
123 DEBUGMSGTL(("snmpd/main", "We're up. Starting to process data.\n"));
124 diff -up net-snmp-5.7.2/apps/snmptrapd.c.systemd net-snmp-5.7.2/apps/snmptrapd.c
125 --- net-snmp-5.7.2/apps/snmptrapd.c.systemd 2012-10-10 00:28:58.000000000 +0200
126 +++ net-snmp-5.7.2/apps/snmptrapd.c 2012-11-12 10:18:46.084369548 +0100
127 @@ -125,6 +125,10 @@ SOFTWARE.
129 #include <net-snmp/net-snmp-features.h>
131 +#ifndef NETSNMP_NO_SYSTEMD
132 +#include <net-snmp/library/sd-daemon.h>
138 @@ -655,15 +659,22 @@ main(int argc, char *argv[])
139 int agentx_subagent = 1;
141 netsnmp_trapd_handler *traph;
142 + int prepared_sockets = 0;
146 +#ifndef NETSNMP_NO_SYSTEMD
147 + /* check if systemd has sockets for us and don't close them */
148 + prepared_sockets = netsnmp_sd_listen_fds(0);
151 * close all non-standard file descriptors we may have
152 * inherited from the shell.
154 - for (i = getdtablesize() - 1; i > 2; --i) {
156 + if (!prepared_sockets) {
157 + for (i = getdtablesize() - 1; i > 2; --i) {
163 @@ -1311,6 +1322,19 @@ main(int argc, char *argv[])
168 + * Let systemd know we're up.
170 +#ifndef NETSNMP_NO_SYSTEMD
171 + netsnmp_sd_notify(1, "READY=1\n");
172 + if (prepared_sockets)
174 + * Clear the environment variable, we already processed all the sockets
177 + netsnmp_sd_listen_fds(1);
181 trapd_status = SNMPTRAPD_RUNNING;
183 diff -up net-snmp-5.7.2/configure.d/config_modules_lib.systemd net-snmp-5.7.2/configure.d/config_modules_lib
184 --- net-snmp-5.7.2/configure.d/config_modules_lib.systemd 2012-10-10 00:28:58.000000000 +0200
185 +++ net-snmp-5.7.2/configure.d/config_modules_lib 2012-11-12 10:18:46.085369546 +0100
186 @@ -53,6 +53,14 @@ if test "x$PARTIALTARGETOS" = "xmingw32"
187 other_ftobjs_list="$other_ftobjs_list winpipe.ft"
191 +if test "x$with_systemd" == "xyes"; then
192 + other_src_list="$other_src_list sd-daemon.c"
193 + other_objs_list="$other_objs_list sd-daemon.o"
194 + other_lobjs_list="$other_lobjs_list sd-daemon.lo"
195 + other_ftobjs_list="$other_ftobjs_list sd-daemon.ft"
198 AC_SUBST(other_src_list)
199 AC_SUBST(other_objs_list)
200 AC_SUBST(other_lobjs_list)
201 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
202 --- net-snmp-5.7.2/configure.d/config_project_with_enable.systemd 2012-10-10 00:28:58.000000000 +0200
203 +++ net-snmp-5.7.2/configure.d/config_project_with_enable 2012-11-12 10:18:46.086369544 +0100
204 @@ -690,6 +690,15 @@ if test "x$with_dummy_values" != "xyes";
208 +NETSNMP_ARG_WITH(systemd,
209 +[ --with-systemd Provide systemd support. See README.systemd
211 +# Define unless specifically suppressed (i.e., option defaults to false).
212 +if test "x$with_systemd" != "xyes"; then
213 + AC_DEFINE(NETSNMP_NO_SYSTEMD, 1,
214 + [If you don't want to integrate with systemd.])
217 NETSNMP_ARG_ENABLE(set-support,
218 [ --disable-set-support Do not allow SNMP set requests.])
219 if test "x$enable_set_support" = "xno"; then
220 diff -up net-snmp-5.7.2/configure.systemd net-snmp-5.7.2/configure
221 --- net-snmp-5.7.2/configure.systemd 2012-10-10 00:35:37.000000000 +0200
222 +++ net-snmp-5.7.2/configure 2012-11-12 10:18:46.099369517 +0100
223 @@ -950,6 +950,8 @@ with_kmem_usage
232 @@ -1866,6 +1868,8 @@ Configuring the agent:
233 This is technically not compliant with the
234 SNMP specifications, but was how the agent
235 operated for versions < 4.0.
236 + --with-systemd Provide systemd support. See README.systemd
238 --with-sys-contact="who@where" Default system contact.
239 (Default: LOGIN@DOMAINNAME)
240 --with-sys-location="location" Default system location.
241 @@ -4397,6 +4401,24 @@ $as_echo "#define NETSNMP_NO_DUMMY_VALUE
246 +# Check whether --with-systemd was given.
247 +if test "${with_systemd+set}" = set; then :
248 + withval=$with_systemd;
251 + # Check whether --enable-systemd was given.
252 +if test "${enable_systemd+set}" = set; then :
253 + enableval=$enable_systemd; as_fn_error $? "Invalid option. Use --with-systemd/--without-systemd instead" "$LINENO" 5
256 +# Define unless specifically suppressed (i.e., option defaults to false).
257 +if test "x$with_systemd" != "xyes"; then
259 +$as_echo "#define NETSNMP_NO_SYSTEMD 1" >>confdefs.h
263 # Check whether --enable-set-support was given.
264 if test "${enable_set_support+set}" = set; then :
265 enableval=$enable_set_support;
266 @@ -18239,6 +18261,14 @@ if test "x$PARTIALTARGETOS" = "xmingw32"
267 other_ftobjs_list="$other_ftobjs_list winpipe.ft"
271 +if test "x$with_systemd" == "xyes"; then
272 + other_src_list="$other_src_list sd-daemon.c"
273 + other_objs_list="$other_objs_list sd-daemon.o"
274 + other_lobjs_list="$other_lobjs_list sd-daemon.lo"
275 + other_ftobjs_list="$other_ftobjs_list sd-daemon.ft"
281 diff -up net-snmp-5.7.2/dist/snmpd.service.systemd net-snmp-5.7.2/dist/snmpd.service
282 --- net-snmp-5.7.2/dist/snmpd.service.systemd 2012-11-12 10:18:46.104369507 +0100
283 +++ net-snmp-5.7.2/dist/snmpd.service 2012-11-12 10:18:46.104369507 +0100
286 +# SNMP agent service file for systemd
289 +# The service should be enabled, i.e. snmpd should start during machine boot.
290 +# Socket activation shall not be used. See README.systemd for details.
293 +Description=Simple Network Management Protocol (SNMP) daemon.
294 +After=syslog.target network.target
297 +# Type=notify is also supported. It should be set when snmpd.socket is not used.
299 +ExecStart=/usr/sbin/snmpd -f
302 +WantedBy=multi-user.target
303 diff -up net-snmp-5.7.2/dist/snmpd.socket.systemd net-snmp-5.7.2/dist/snmpd.socket
304 --- net-snmp-5.7.2/dist/snmpd.socket.systemd 2012-11-12 10:18:46.104369507 +0100
305 +++ net-snmp-5.7.2/dist/snmpd.socket 2012-11-12 10:18:46.104369507 +0100
308 +Description=Socket listening for SNMP and AgentX messages
311 +ListenDatagram=0.0.0.0:161
312 +# Uncomment other listening addresses as needed - TCP, UDP6, TCP6.
313 +# It must match listening addresses/ports defined in snmpd.service
315 +# ListenStream=0.0.0.0:161
316 +# ListenDatagram=[::]:161
317 +# ListenStream=[::]:161
319 +# Uncomment AgentX socket if snmpd.conf enables AgentX protocol.
320 +# ListenStream=/var/agentx/master
323 +WantedBy=sockets.target
324 diff -up net-snmp-5.7.2/dist/snmptrapd.service.systemd net-snmp-5.7.2/dist/snmptrapd.service
325 --- net-snmp-5.7.2/dist/snmptrapd.service.systemd 2012-11-12 10:18:46.105369505 +0100
326 +++ net-snmp-5.7.2/dist/snmptrapd.service 2012-11-12 10:18:46.105369505 +0100
329 +# SNMP trap-processing service file for systemd
333 +Description=Simple Network Management Protocol (SNMP) Trap daemon.
334 +After=syslog.target network.target
337 +# Type=notify is also supported. It should be set when snmptrapd.socket is not
340 +ExecStart=/usr/sbin/snmptrapd -f
343 +WantedBy=multi-user.target
344 diff -up net-snmp-5.7.2/dist/snmptrapd.socket.systemd net-snmp-5.7.2/dist/snmptrapd.socket
345 --- net-snmp-5.7.2/dist/snmptrapd.socket.systemd 2012-11-12 10:18:46.105369505 +0100
346 +++ net-snmp-5.7.2/dist/snmptrapd.socket 2012-11-12 10:18:46.105369505 +0100
349 +Description=Socket listening for SNMP trap messages
352 +ListenDatagram=0.0.0.0:162
353 +# Uncomment other listening addresses as needed - TCP, UDP6, TCP6.
354 +# It must match listening addresses/ports defined in snmptrapd.service
355 +# or snmptrapd.conf.
356 +# ListenStream=0.0.0.0:162
357 +# ListenDatagram=[::]:162
358 +# ListenStream=[::]:162
361 +WantedBy=sockets.target
362 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
363 --- net-snmp-5.7.2/include/net-snmp/library/sd-daemon.h.systemd 2012-11-12 10:18:46.106369503 +0100
364 +++ net-snmp-5.7.2/include/net-snmp/library/sd-daemon.h 2012-11-12 10:18:46.106369503 +0100
366 +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
368 +#ifndef SNMPD_SD_DAEMON_H
369 +#define SNMPD_SD_DAEMON_H
372 + Copyright 2010 Lennart Poettering
374 + Permission is hereby granted, free of charge, to any person
375 + obtaining a copy of this software and associated documentation files
376 + (the "Software"), to deal in the Software without restriction,
377 + including without limitation the rights to use, copy, modify, merge,
378 + publish, distribute, sublicense, and/or sell copies of the Software,
379 + and to permit persons to whom the Software is furnished to do so,
380 + subject to the following conditions:
382 + The above copyright notice and this permission notice shall be
383 + included in all copies or substantial portions of the Software.
385 + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
386 + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
387 + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
388 + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
389 + BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
390 + ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
391 + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
395 +#include <sys/types.h>
396 +#include <inttypes.h>
403 + Reference implementation of a few systemd related interfaces for
404 + writing daemons. These interfaces are trivial to implement. To
405 + simplify porting we provide this reference implementation.
406 + Applications are welcome to reimplement the algorithms described
407 + here if they do not want to include these two source files.
409 + The following functionality is provided:
411 + - Support for logging with log levels on stderr
412 + - File descriptor passing for socket-based activation
413 + - Daemon startup and status notification
414 + - Detection of systemd boots
416 + You may compile this with -DDISABLE_SYSTEMD to disable systemd
417 + support. This makes all those calls NOPs that are directly related to
418 + systemd (i.e. only sd_is_xxx() will stay useful).
420 + Since this is drop-in code we don't want any of our symbols to be
421 + exported in any case. Hence we declare hidden visibility for all of
424 + You may find an up-to-date version of these source files online:
426 + http://cgit.freedesktop.org/systemd/plain/src/sd-daemon.h
427 + http://cgit.freedesktop.org/systemd/plain/src/sd-daemon.c
429 + This should compile on non-Linux systems, too, but with the
430 + exception of the sd_is_xxx() calls all functions will become NOPs.
432 + See sd-daemon(7) for more information.
435 +#ifndef _sd_printf_attr_
437 +#define _sd_printf_attr_(a,b) __attribute__ ((format (printf, a, b)))
439 +#define _sd_printf_attr_(a,b)
444 + Log levels for usage on stderr:
446 + fprintf(stderr, SD_NOTICE "Hello World!\n");
448 + This is similar to printk() usage in the kernel.
450 +#define SD_EMERG "<0>" /* system is unusable */
451 +#define SD_ALERT "<1>" /* action must be taken immediately */
452 +#define SD_CRIT "<2>" /* critical conditions */
453 +#define SD_ERR "<3>" /* error conditions */
454 +#define SD_WARNING "<4>" /* warning conditions */
455 +#define SD_NOTICE "<5>" /* normal but significant condition */
456 +#define SD_INFO "<6>" /* informational */
457 +#define SD_DEBUG "<7>" /* debug-level messages */
459 +/* The first passed file descriptor is fd 3 */
460 +#define SD_LISTEN_FDS_START 3
463 + Returns how many file descriptors have been passed, or a negative
464 + errno code on failure. Optionally, removes the $LISTEN_FDS and
465 + $LISTEN_PID file descriptors from the environment (recommended, but
466 + problematic in threaded environments). If r is the return value of
467 + this function you'll find the file descriptors passed as fds
468 + SD_LISTEN_FDS_START to SD_LISTEN_FDS_START+r-1. Returns a negative
469 + errno style error code on failure. This function call ensures that
470 + the FD_CLOEXEC flag is set for the passed file descriptors, to make
471 + sure they are not passed on to child processes. If FD_CLOEXEC shall
472 + not be set, the caller needs to unset it after this call for all file
473 + descriptors that are used.
475 + See sd_listen_fds(3) for more information.
477 +int netsnmp_sd_listen_fds(int unset_environment);
480 + Helper call for identifying a passed file descriptor. Returns 1 if
481 + the file descriptor is a FIFO in the file system stored under the
482 + specified path, 0 otherwise. If path is NULL a path name check will
483 + not be done and the call only verifies if the file descriptor
484 + refers to a FIFO. Returns a negative errno style error code on
487 + See sd_is_fifo(3) for more information.
489 +int netsnmp_sd_is_fifo(int fd, const char *path);
492 + Helper call for identifying a passed file descriptor. Returns 1 if
493 + the file descriptor is a special character device on the file
494 + system stored under the specified path, 0 otherwise.
495 + If path is NULL a path name check will not be done and the call
496 + only verifies if the file descriptor refers to a special character.
497 + Returns a negative errno style error code on failure.
499 + See sd_is_special(3) for more information.
501 +int netsnmp_sd_is_special(int fd, const char *path);
504 + Helper call for identifying a passed file descriptor. Returns 1 if
505 + the file descriptor is a socket of the specified family (AF_INET,
506 + ...) and type (SOCK_DGRAM, SOCK_STREAM, ...), 0 otherwise. If
507 + family is 0 a socket family check will not be done. If type is 0 a
508 + socket type check will not be done and the call only verifies if
509 + the file descriptor refers to a socket. If listening is > 0 it is
510 + verified that the socket is in listening mode. (i.e. listen() has
511 + been called) If listening is == 0 it is verified that the socket is
512 + not in listening mode. If listening is < 0 no listening mode check
513 + is done. Returns a negative errno style error code on failure.
515 + See sd_is_socket(3) for more information.
517 +int netsnmp_sd_is_socket(int fd, int family, int type, int listening);
520 + Helper call for identifying a passed file descriptor. Returns 1 if
521 + the file descriptor is an Internet socket, of the specified family
522 + (either AF_INET or AF_INET6) and the specified type (SOCK_DGRAM,
523 + SOCK_STREAM, ...), 0 otherwise. If version is 0 a protocol version
524 + check is not done. If type is 0 a socket type check will not be
525 + done. If port is 0 a socket port check will not be done. The
526 + listening flag is used the same way as in sd_is_socket(). Returns a
527 + negative errno style error code on failure.
529 + See sd_is_socket_inet(3) for more information.
531 +int netsnmp_sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port);
534 + Helper call for identifying a passed file descriptor. Returns 1 if
535 + the file descriptor is an AF_UNIX socket of the specified type
536 + (SOCK_DGRAM, SOCK_STREAM, ...) and path, 0 otherwise. If type is 0
537 + a socket type check will not be done. If path is NULL a socket path
538 + check will not be done. For normal AF_UNIX sockets set length to
539 + 0. For abstract namespace sockets set length to the length of the
540 + socket name (including the initial 0 byte), and pass the full
541 + socket path in path (including the initial 0 byte). The listening
542 + flag is used the same way as in sd_is_socket(). Returns a negative
543 + errno style error code on failure.
545 + See sd_is_socket_unix(3) for more information.
547 +int netsnmp_sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length);
550 + Informs systemd about changed daemon state. This takes a number of
551 + newline separated environment-style variable assignments in a
552 + string. The following variables are known:
554 + READY=1 Tells systemd that daemon startup is finished (only
555 + relevant for services of Type=notify). The passed
556 + argument is a boolean "1" or "0". Since there is
557 + little value in signaling non-readiness the only
558 + value daemons should send is "READY=1".
560 + STATUS=... Passes a single-line status string back to systemd
561 + that describes the daemon state. This is free-from
562 + and can be used for various purposes: general state
563 + feedback, fsck-like programs could pass completion
564 + percentages and failing programs could pass a human
565 + readable error message. Example: "STATUS=Completed
566 + 66% of file system check..."
568 + ERRNO=... If a daemon fails, the errno-style error code,
569 + formatted as string. Example: "ERRNO=2" for ENOENT.
571 + BUSERROR=... If a daemon fails, the D-Bus error-style error
572 + code. Example: "BUSERROR=org.freedesktop.DBus.Error.TimedOut"
574 + MAINPID=... The main pid of a daemon, in case systemd did not
575 + fork off the process itself. Example: "MAINPID=4711"
577 + Daemons can choose to send additional variables. However, it is
578 + recommended to prefix variable names not listed above with X_.
580 + Returns a negative errno-style error code on failure. Returns > 0
581 + if systemd could be notified, 0 if it couldn't possibly because
582 + systemd is not running.
584 + Example: When a daemon finished starting up, it could issue this
585 + call to notify systemd about it:
587 + sd_notify(0, "READY=1");
589 + See sd_notifyf() for more complete examples.
591 + See sd_notify(3) for more information.
593 +int netsnmp_sd_notify(int unset_environment, const char *state);
596 + Similar to sd_notify() but takes a format string.
598 + Example 1: A daemon could send the following after initialization:
600 + sd_notifyf(0, "READY=1\n"
601 + "STATUS=Processing requests...\n"
603 + (unsigned long) getpid());
605 + Example 2: A daemon could send the following shortly before
606 + exiting, on failure:
608 + sd_notifyf(0, "STATUS=Failed to start up: %s\n"
613 + See sd_notifyf(3) for more information.
615 +int netsnmp_sd_notifyf(int unset_environment, const char *format, ...) _sd_printf_attr_(2,3);
618 + Returns > 0 if the system was booted with systemd. Returns < 0 on
619 + error. Returns 0 if the system was not booted with systemd. Note
620 + that all of the functions above handle non-systemd boots just
621 + fine. You should NOT protect them with a call to this function. Also
622 + note that this function checks whether the system, not the user
623 + session is controlled by systemd. However the functions above work
624 + for both user and system services.
626 + See sd_booted(3) for more information.
628 +int netsnmp_sd_booted(void);
631 + * Find an socket with given parameters. See man sd_is_socket_inet for
632 + * description of the arguments.
634 + * Returns the file descriptor if it is found, 0 otherwise.
636 +int netsnmp_sd_find_inet_socket(int family, int type, int listening, int port);
639 + * Find an unix socket with given parameters. See man sd_is_socket_unix for
640 + * description of the arguments.
642 + * Returns the file descriptor if it is found, 0 otherwise.
645 +netsnmp_sd_find_unix_socket(int type, int listening, const char *path);
651 +#endif /* SNMPD_SD_DAEMON_H */
652 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
653 --- net-snmp-5.7.2/include/net-snmp/net-snmp-config.h.in.systemd 2012-10-10 00:28:58.000000000 +0200
654 +++ net-snmp-5.7.2/include/net-snmp/net-snmp-config.h.in 2012-11-12 10:18:46.107369501 +0100
655 @@ -1389,6 +1389,9 @@
656 /* If you don't have root access don't exit upon kmem errors */
657 #undef NETSNMP_NO_ROOT_ACCESS
659 +/* If you don't want to integrate with systemd. */
660 +#undef NETSNMP_NO_SYSTEMD
662 /* Define if you want to remove all SET/write access from the code */
663 #undef NETSNMP_NO_WRITE_SUPPORT
665 diff -up net-snmp-5.7.2/README.systemd.systemd net-snmp-5.7.2/README.systemd
666 --- net-snmp-5.7.2/README.systemd.systemd 2012-11-12 10:18:46.108369499 +0100
667 +++ net-snmp-5.7.2/README.systemd 2012-11-12 10:18:46.108369499 +0100
671 +Net-SNMP provides two daemons, which support systemd system manager.
672 +See http://www.freedesktop.org/wiki/Software/systemd to learn how
673 +systemd works. Both socket activation and notification is supported by these
676 +To enable systemd support, the sources must be compiled with
677 +--with-systemd configure option.
679 +snmpd - The SNMP agent
680 +----------------------
681 +Socket activation od snmpd daemon is implemented, but it's discouraged.
682 +The reason is simple - snmpd not only listens and processes SNMP requests
683 +from network, but also gathers system statistics counters, sends traps and
684 +communicates with subagents. It even opens few netlink sockets.
686 +In other words, snmpd should run from system start to properly work.
687 +This can be done in two ways:
688 +1) either as snmpd service unit with 'Type=notification' and without a socket
690 +2) or as snmpd service unit with 'Type=simple', appropriate socket socket unit
691 + and the snmpd service enabled. This way systemd creates the snmpd listening
692 + socket early during boot and passes the sockets to snmpd slightly later
693 + (but still during machine boot). This way systemd can paralelize start of
694 + services, which depend on snmpd. Admins must adjust the socket file manually,
695 + depending if the snmpd support AgentX, IPv6, SMUX etc.
697 +snmpd should be started with '-f' command line parameter to disable forking -
698 +systemd does that for us automatically.
701 +snmptrapd - The trap processing daemon
702 +--------------------------------------
703 +snmptrapd supports full socket activation and also notification (if needed).
704 +Both 'Type=simple' (with appropriate socket unit) and 'Type=notify' services
705 +will work. Again, '-f' parameter should be provided on snmptrapd command line.
707 +If integration with SNMP agent using AgentX protocol is enabled, snmptrapd should
708 +start during boot and not after first SNMP trap arrives. Same rules as for snmpd
710 \ No newline at end of file
711 diff -up net-snmp-5.7.2/snmplib/sd-daemon.c.systemd net-snmp-5.7.2/snmplib/sd-daemon.c
712 --- net-snmp-5.7.2/snmplib/sd-daemon.c.systemd 2012-11-12 10:18:46.109369497 +0100
713 +++ net-snmp-5.7.2/snmplib/sd-daemon.c 2012-11-12 10:18:46.109369497 +0100
716 + * Systemd integration parts.
718 + * Most of this file is directly copied from systemd sources.
720 + * - all functions were renamed to have netsnmp_ prefix
721 + * - includes were changed to match Net-SNMP style.
722 + * - removed gcc export macros
723 + * - removed POSIX message queues
726 +#include <net-snmp/net-snmp-config.h>
727 +#include <net-snmp/net-snmp-features.h>
728 +#include <net-snmp/types.h>
729 +#include <net-snmp/library/snmp_debug.h>
731 +#ifndef NETSNMP_NO_SYSTEMD
734 + Copyright 2010 Lennart Poettering
736 + Permission is hereby granted, free of charge, to any person
737 + obtaining a copy of this software and associated documentation files
738 + (the "Software"), to deal in the Software without restriction,
739 + including without limitation the rights to use, copy, modify, merge,
740 + publish, distribute, sublicense, and/or sell copies of the Software,
741 + and to permit persons to whom the Software is furnished to do so,
742 + subject to the following conditions:
744 + The above copyright notice and this permission notice shall be
745 + included in all copies or substantial portions of the Software.
747 + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
748 + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
749 + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
750 + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
751 + BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
752 + ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
753 + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
761 +#include <sys/types.h>
762 +#include <sys/stat.h>
763 +#include <sys/socket.h>
765 +#include <sys/fcntl.h>
766 +#include <netinet/in.h>
776 +#include <net-snmp/library/sd-daemon.h>
778 +int netsnmp_sd_listen_fds(int unset_environment) {
785 + if (!(e = getenv("LISTEN_PID"))) {
791 + l = strtoul(e, &p, 10);
798 + if (!p || *p || l <= 0) {
803 + /* Is this for us? */
804 + if (getpid() != (pid_t) l) {
809 + if (!(e = getenv("LISTEN_FDS"))) {
815 + l = strtoul(e, &p, 10);
827 + for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + (int) l; fd ++) {
830 + if ((flags = fcntl(fd, F_GETFD)) < 0) {
835 + if (flags & FD_CLOEXEC)
838 + if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) {
847 + if (unset_environment) {
848 + unsetenv("LISTEN_PID");
849 + unsetenv("LISTEN_FDS");
855 +int netsnmp_sd_is_fifo(int fd, const char *path) {
861 + memset(&st_fd, 0, sizeof(st_fd));
862 + if (fstat(fd, &st_fd) < 0)
865 + if (!S_ISFIFO(st_fd.st_mode))
869 + struct stat st_path;
871 + memset(&st_path, 0, sizeof(st_path));
872 + if (stat(path, &st_path) < 0) {
874 + if (errno == ENOENT || errno == ENOTDIR)
881 + st_path.st_dev == st_fd.st_dev &&
882 + st_path.st_ino == st_fd.st_ino;
888 +int netsnmp_sd_is_special(int fd, const char *path) {
894 + if (fstat(fd, &st_fd) < 0)
897 + if (!S_ISREG(st_fd.st_mode) && !S_ISCHR(st_fd.st_mode))
901 + struct stat st_path;
903 + if (stat(path, &st_path) < 0) {
905 + if (errno == ENOENT || errno == ENOTDIR)
911 + if (S_ISREG(st_fd.st_mode) && S_ISREG(st_path.st_mode))
913 + st_path.st_dev == st_fd.st_dev &&
914 + st_path.st_ino == st_fd.st_ino;
915 + else if (S_ISCHR(st_fd.st_mode) && S_ISCHR(st_path.st_mode))
916 + return st_path.st_rdev == st_fd.st_rdev;
924 +static int sd_is_socket_internal(int fd, int type, int listening) {
927 + if (fd < 0 || type < 0)
930 + if (fstat(fd, &st_fd) < 0)
933 + if (!S_ISSOCK(st_fd.st_mode))
937 + int other_type = 0;
938 + socklen_t l = sizeof(other_type);
940 + if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &other_type, &l) < 0)
943 + if (l != sizeof(other_type))
946 + if (other_type != type)
950 + if (listening >= 0) {
952 + socklen_t l = sizeof(accepting);
954 + if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &accepting, &l) < 0)
957 + if (l != sizeof(accepting))
960 + if (!accepting != !listening)
967 +union sockaddr_union {
968 + struct sockaddr sa;
969 + struct sockaddr_in in4;
970 + struct sockaddr_in6 in6;
971 + struct sockaddr_un un;
972 + struct sockaddr_storage storage;
975 +int netsnmp_sd_is_socket(int fd, int family, int type, int listening) {
981 + if ((r = sd_is_socket_internal(fd, type, listening)) <= 0)
985 + union sockaddr_union sockaddr;
988 + memset(&sockaddr, 0, sizeof(sockaddr));
989 + l = sizeof(sockaddr);
991 + if (getsockname(fd, &sockaddr.sa, &l) < 0)
994 + if (l < sizeof(sa_family_t))
997 + return sockaddr.sa.sa_family == family;
1003 +int netsnmp_sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port) {
1004 + union sockaddr_union sockaddr;
1008 + if (family != 0 && family != AF_INET && family != AF_INET6)
1011 + if ((r = sd_is_socket_internal(fd, type, listening)) <= 0)
1014 + memset(&sockaddr, 0, sizeof(sockaddr));
1015 + l = sizeof(sockaddr);
1017 + if (getsockname(fd, &sockaddr.sa, &l) < 0)
1020 + if (l < sizeof(sa_family_t))
1023 + if (sockaddr.sa.sa_family != AF_INET &&
1024 + sockaddr.sa.sa_family != AF_INET6)
1028 + if (sockaddr.sa.sa_family != family)
1032 + if (sockaddr.sa.sa_family == AF_INET) {
1033 + if (l < sizeof(struct sockaddr_in))
1036 + return htons(port) == sockaddr.in4.sin_port;
1038 + if (l < sizeof(struct sockaddr_in6))
1041 + return htons(port) == sockaddr.in6.sin6_port;
1048 +int netsnmp_sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length) {
1049 + union sockaddr_union sockaddr;
1053 + if ((r = sd_is_socket_internal(fd, type, listening)) <= 0)
1056 + memset(&sockaddr, 0, sizeof(sockaddr));
1057 + l = sizeof(sockaddr);
1059 + if (getsockname(fd, &sockaddr.sa, &l) < 0)
1062 + if (l < sizeof(sa_family_t))
1065 + if (sockaddr.sa.sa_family != AF_UNIX)
1070 + length = strlen(path);
1073 + /* Unnamed socket */
1074 + return l == offsetof(struct sockaddr_un, sun_path);
1077 + /* Normal path socket */
1079 + (l >= offsetof(struct sockaddr_un, sun_path) + length + 1) &&
1080 + memcmp(path, sockaddr.un.sun_path, length+1) == 0;
1082 + /* Abstract namespace socket */
1084 + (l == offsetof(struct sockaddr_un, sun_path) + length) &&
1085 + memcmp(path, sockaddr.un.sun_path, length) == 0;
1091 +int netsnmp_sd_notify(int unset_environment, const char *state) {
1093 + struct msghdr msghdr;
1094 + struct iovec iovec;
1095 + union sockaddr_union sockaddr;
1103 + if (!(e = getenv("NOTIFY_SOCKET")))
1106 + /* Must be an abstract socket, or an absolute path */
1107 + if ((e[0] != '@' && e[0] != '/') || e[1] == 0) {
1112 + if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) {
1117 + memset(&sockaddr, 0, sizeof(sockaddr));
1118 + sockaddr.sa.sa_family = AF_UNIX;
1119 + strncpy(sockaddr.un.sun_path, e, sizeof(sockaddr.un.sun_path));
1121 + if (sockaddr.un.sun_path[0] == '@')
1122 + sockaddr.un.sun_path[0] = 0;
1124 + memset(&iovec, 0, sizeof(iovec));
1125 + iovec.iov_base = (char *)state;
1126 + iovec.iov_len = strlen(state);
1128 + memset(&msghdr, 0, sizeof(msghdr));
1129 + msghdr.msg_name = &sockaddr;
1130 + msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(e);
1132 + if (msghdr.msg_namelen > sizeof(struct sockaddr_un))
1133 + msghdr.msg_namelen = sizeof(struct sockaddr_un);
1135 + msghdr.msg_iov = &iovec;
1136 + msghdr.msg_iovlen = 1;
1138 + if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) {
1146 + if (unset_environment)
1147 + unsetenv("NOTIFY_SOCKET");
1155 +int netsnmp_sd_notifyf(int unset_environment, const char *format, ...) {
1160 + va_start(ap, format);
1161 + r = vasprintf(&p, format, ap);
1167 + r = netsnmp_sd_notify(unset_environment, p);
1173 +int netsnmp_sd_booted(void) {
1176 + /* We simply test whether the systemd cgroup hierarchy is
1179 + if (lstat("/sys/fs/cgroup", &a) < 0)
1182 + if (lstat("/sys/fs/cgroup/systemd", &b) < 0)
1185 + return a.st_dev != b.st_dev;
1188 +/* End of original sd-daemon.c from systemd sources */
1191 +netsnmp_sd_find_inet_socket(int family, int type, int listening, int port)
1195 + count = netsnmp_sd_listen_fds(0);
1197 + DEBUGMSGTL(("systemd:find_inet_socket", "No LISTEN_FDS found.\n"));
1200 + DEBUGMSGTL(("systemd:find_inet_socket", "LISTEN_FDS reports %d sockets.\n",
1203 + for (fd = 3; fd < 3+count; fd++) {
1204 + int rc = netsnmp_sd_is_socket_inet(fd, family, type, listening, port);
1206 + DEBUGMSGTL(("systemd:find_inet_socket",
1207 + "sd_is_socket_inet error: %d\n", rc));
1209 + DEBUGMSGTL(("systemd:find_inet_socket",
1210 + "Found the socket in LISTEN_FDS\n"));
1214 + DEBUGMSGTL(("systemd:find_inet_socket", "Socket not found in LISTEN_FDS\n"));
1219 +netsnmp_sd_find_unix_socket(int type, int listening, const char *path)
1223 + count = netsnmp_sd_listen_fds(0);
1225 + DEBUGMSGTL(("systemd:find_unix_socket", "No LISTEN_FDS found.\n"));
1228 + DEBUGMSGTL(("systemd:find_unix_socket", "LISTEN_FDS reports %d sockets.\n",
1231 + for (fd = 3; fd < 3+count; fd++) {
1232 + int rc = netsnmp_sd_is_socket_unix(fd, type, listening, path, 0);
1234 + DEBUGMSGTL(("systemd:find_unix_socket",
1235 + "netsnmp_sd_is_socket_unix error: %d\n", rc));
1237 + DEBUGMSGTL(("systemd:find_unix_socket",
1238 + "Found the socket in LISTEN_FDS\n"));
1242 + DEBUGMSGTL(("systemd:find_unix_socket", "Socket not found in LISTEN_FDS\n"));
1246 +#endif /* ! NETSNMP_NO_SYSTEMD */
1247 diff -up net-snmp-5.7.2/snmplib/transports/snmpTCPDomain.c.systemd net-snmp-5.7.2/snmplib/transports/snmpTCPDomain.c
1248 --- net-snmp-5.7.2/snmplib/transports/snmpTCPDomain.c.systemd 2012-10-10 00:28:58.000000000 +0200
1249 +++ net-snmp-5.7.2/snmplib/transports/snmpTCPDomain.c 2012-11-12 10:19:41.767217067 +0100
1251 #include <net-snmp/library/snmpTCPBaseDomain.h>
1252 #include <net-snmp/library/tools.h>
1254 +#ifndef NETSNMP_NO_SYSTEMD
1255 +#include <net-snmp/library/sd-daemon.h>
1259 * needs to be in sync with the definitions in snmplib/snmpUDPDomain.c
1260 * and perl/agent/agent.xs
1261 @@ -149,6 +153,7 @@ netsnmp_tcp_transport(struct sockaddr_in
1262 netsnmp_transport *t = NULL;
1263 netsnmp_udp_addr_pair *addr_pair = NULL;
1265 + int socket_initialized = 0;
1267 #ifdef NETSNMP_NO_LISTEN_SUPPORT
1269 @@ -178,7 +183,19 @@ netsnmp_tcp_transport(struct sockaddr_in
1271 sizeof(netsnmp_snmpTCPDomain) / sizeof(netsnmp_snmpTCPDomain[0]);
1273 - t->sock = socket(PF_INET, SOCK_STREAM, 0);
1274 +#ifndef NETSNMP_NO_SYSTEMD
1276 + * Maybe the socket was already provided by systemd...
1279 + t->sock = netsnmp_sd_find_inet_socket(PF_INET, SOCK_STREAM, 1,
1280 + ntohs(addr->sin_port));
1282 + socket_initialized = 1;
1285 + if (!socket_initialized)
1286 + t->sock = socket(PF_INET, SOCK_STREAM, 0);
1288 netsnmp_transport_free(t);
1290 @@ -215,11 +232,13 @@ netsnmp_tcp_transport(struct sockaddr_in
1291 setsockopt(t->sock, SOL_SOCKET, SO_REUSEADDR, (void *)&opt,
1294 - rc = bind(t->sock, (struct sockaddr *)addr, sizeof(struct sockaddr));
1296 - netsnmp_socketbase_close(t);
1297 - netsnmp_transport_free(t);
1299 + if (!socket_initialized) {
1300 + rc = bind(t->sock, (struct sockaddr *)addr, sizeof(struct sockaddr));
1302 + netsnmp_socketbase_close(t);
1303 + netsnmp_transport_free(t);
1309 @@ -235,12 +254,13 @@ netsnmp_tcp_transport(struct sockaddr_in
1311 * Now sit here and wait for connections to arrive.
1314 - rc = listen(t->sock, NETSNMP_STREAM_QUEUE_LEN);
1316 - netsnmp_socketbase_close(t);
1317 - netsnmp_transport_free(t);
1319 + if (!socket_initialized) {
1320 + rc = listen(t->sock, NETSNMP_STREAM_QUEUE_LEN);
1322 + netsnmp_socketbase_close(t);
1323 + netsnmp_transport_free(t);
1329 diff -up net-snmp-5.7.2/snmplib/transports/snmpTCPIPv6Domain.c.systemd net-snmp-5.7.2/snmplib/transports/snmpTCPIPv6Domain.c
1330 --- net-snmp-5.7.2/snmplib/transports/snmpTCPIPv6Domain.c.systemd 2012-10-10 00:28:58.000000000 +0200
1331 +++ net-snmp-5.7.2/snmplib/transports/snmpTCPIPv6Domain.c 2012-11-12 10:20:32.019078971 +0100
1333 #include <net-snmp/library/snmpTCPBaseDomain.h>
1334 #include <net-snmp/library/tools.h>
1336 +#ifndef NETSNMP_NO_SYSTEMD
1337 +#include <net-snmp/library/sd-daemon.h>
1340 #include "inet_ntop.h"
1342 oid netsnmp_TCPIPv6Domain[] = { TRANSPORT_DOMAIN_TCP_IPV6 };
1343 @@ -140,6 +144,7 @@ netsnmp_tcp6_transport(struct sockaddr_i
1345 netsnmp_transport *t = NULL;
1347 + int socket_initialized = 0;
1349 #ifdef NETSNMP_NO_LISTEN_SUPPORT
1351 @@ -174,7 +179,19 @@ netsnmp_tcp6_transport(struct sockaddr_i
1352 t->domain = netsnmp_TCPIPv6Domain;
1353 t->domain_length = sizeof(netsnmp_TCPIPv6Domain) / sizeof(oid);
1355 - t->sock = socket(PF_INET6, SOCK_STREAM, 0);
1356 +#ifndef NETSNMP_NO_SYSTEMD
1358 + * Maybe the socket was already provided by systemd...
1361 + t->sock = netsnmp_sd_find_inet_socket(PF_INET6, SOCK_STREAM, 1,
1362 + ntohs(addr->sin6_port));
1364 + socket_initialized = 1;
1367 + if (!socket_initialized)
1368 + t->sock = socket(PF_INET6, SOCK_STREAM, 0);
1370 netsnmp_transport_free(t);
1372 @@ -220,12 +237,14 @@ netsnmp_tcp6_transport(struct sockaddr_i
1374 setsockopt(t->sock, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, sizeof(opt));
1376 - rc = bind(t->sock, (struct sockaddr *) addr,
1377 - sizeof(struct sockaddr_in6));
1379 - netsnmp_socketbase_close(t);
1380 - netsnmp_transport_free(t);
1382 + if (!socket_initialized) {
1383 + rc = bind(t->sock, (struct sockaddr *) addr,
1384 + sizeof(struct sockaddr_in6));
1386 + netsnmp_socketbase_close(t);
1387 + netsnmp_transport_free(t);
1393 @@ -242,11 +261,13 @@ netsnmp_tcp6_transport(struct sockaddr_i
1394 * Now sit here and wait for connections to arrive.
1397 - rc = listen(t->sock, NETSNMP_STREAM_QUEUE_LEN);
1399 - netsnmp_socketbase_close(t);
1400 - netsnmp_transport_free(t);
1402 + if (!socket_initialized) {
1403 + rc = listen(t->sock, NETSNMP_STREAM_QUEUE_LEN);
1405 + netsnmp_socketbase_close(t);
1406 + netsnmp_transport_free(t);
1412 diff -up net-snmp-5.7.2/snmplib/transports/snmpUDPIPv4BaseDomain.c.systemd net-snmp-5.7.2/snmplib/transports/snmpUDPIPv4BaseDomain.c
1413 --- net-snmp-5.7.2/snmplib/transports/snmpUDPIPv4BaseDomain.c.systemd 2012-10-10 00:28:58.000000000 +0200
1414 +++ net-snmp-5.7.2/snmplib/transports/snmpUDPIPv4BaseDomain.c 2012-11-12 10:22:30.279750750 +0100
1417 #include <net-snmp/library/snmpSocketBaseDomain.h>
1419 +#ifndef NETSNMP_NO_SYSTEMD
1420 +#include <net-snmp/library/sd-daemon.h>
1423 #if (defined(linux) && defined(IP_PKTINFO)) \
1424 || defined(IP_RECVDSTADDR) && HAVE_STRUCT_MSGHDR_MSG_CONTROL \
1425 && HAVE_STRUCT_MSGHDR_MSG_FLAGS
1426 @@ -67,6 +71,7 @@ netsnmp_udpipv4base_transport(struct soc
1427 char *client_socket = NULL;
1428 netsnmp_indexed_addr_pair addr_pair;
1429 socklen_t local_addr_len;
1430 + int socket_initialized = 0;
1432 #ifdef NETSNMP_NO_LISTEN_SUPPORT
1434 @@ -91,7 +96,20 @@ netsnmp_udpipv4base_transport(struct soc
1438 - t->sock = socket(PF_INET, SOCK_DGRAM, 0);
1439 +#ifndef NETSNMP_NO_SYSTEMD
1441 + * Maybe the socket was already provided by systemd...
1444 + t->sock = netsnmp_sd_find_inet_socket(PF_INET, SOCK_DGRAM, -1,
1445 + ntohs(addr->sin_port));
1447 + socket_initialized = 1;
1450 + if (!socket_initialized)
1451 + t->sock = socket(PF_INET, SOCK_DGRAM, 0);
1453 DEBUGMSGTL(("UDPBase", "openned socket %d as local=%d\n", t->sock, local));
1455 netsnmp_transport_free(t);
1456 @@ -141,13 +159,15 @@ netsnmp_udpipv4base_transport(struct soc
1457 DEBUGMSGTL(("netsnmp_udp", "set IP_RECVDSTADDR\n"));
1460 - rc = bind(t->sock, (struct sockaddr *) addr,
1461 - sizeof(struct sockaddr));
1463 - netsnmp_socketbase_close(t);
1464 - netsnmp_transport_free(t);
1467 + if (!socket_initialized) {
1468 + rc = bind(t->sock, (struct sockaddr *) addr,
1469 + sizeof(struct sockaddr));
1471 + netsnmp_socketbase_close(t);
1472 + netsnmp_transport_free(t);
1478 #else /* NETSNMP_NO_LISTEN_SUPPORT */
1479 diff -up net-snmp-5.7.2/snmplib/transports/snmpUDPIPv6Domain.c.systemd net-snmp-5.7.2/snmplib/transports/snmpUDPIPv6Domain.c
1480 --- net-snmp-5.7.2/snmplib/transports/snmpUDPIPv6Domain.c.systemd 2012-10-10 00:28:58.000000000 +0200
1481 +++ net-snmp-5.7.2/snmplib/transports/snmpUDPIPv6Domain.c 2012-11-12 10:23:19.713603003 +0100
1482 @@ -67,6 +67,10 @@ static const struct in6_addr in6addr_any
1483 #include <net-snmp/library/snmpSocketBaseDomain.h>
1484 #include <net-snmp/library/tools.h>
1486 +#ifndef NETSNMP_NO_SYSTEMD
1487 +#include <net-snmp/library/sd-daemon.h>
1490 #include "inet_ntop.h"
1491 #include "inet_pton.h"
1493 @@ -190,6 +194,7 @@ netsnmp_udp6_transport(struct sockaddr_i
1495 netsnmp_transport *t = NULL;
1497 + int socket_initialized = 0;
1499 #ifdef NETSNMP_NO_LISTEN_SUPPORT
1501 @@ -217,7 +222,19 @@ netsnmp_udp6_transport(struct sockaddr_i
1503 sizeof(netsnmp_UDPIPv6Domain) / sizeof(netsnmp_UDPIPv6Domain[0]);
1505 - t->sock = socket(PF_INET6, SOCK_DGRAM, 0);
1506 +#ifndef NETSNMP_NO_SYSTEMD
1508 + * Maybe the socket was already provided by systemd...
1511 + t->sock = netsnmp_sd_find_inet_socket(PF_INET6, SOCK_DGRAM, -1,
1512 + ntohs(addr->sin6_port));
1514 + socket_initialized = 1;
1517 + if (!socket_initialized)
1518 + t->sock = socket(PF_INET6, SOCK_DGRAM, 0);
1520 netsnmp_transport_free(t);
1522 @@ -242,13 +259,14 @@ netsnmp_udp6_transport(struct sockaddr_i
1527 - rc = bind(t->sock, (struct sockaddr *) addr,
1528 - sizeof(struct sockaddr_in6));
1530 - netsnmp_socketbase_close(t);
1531 - netsnmp_transport_free(t);
1533 + if (!socket_initialized) {
1534 + rc = bind(t->sock, (struct sockaddr *) addr,
1535 + sizeof(struct sockaddr_in6));
1537 + netsnmp_socketbase_close(t);
1538 + netsnmp_transport_free(t);
1542 t->local = (unsigned char*)malloc(18);
1543 if (t->local == NULL) {
1544 diff -up net-snmp-5.7.2/snmplib/transports/snmpUnixDomain.c.systemd net-snmp-5.7.2/snmplib/transports/snmpUnixDomain.c
1545 --- net-snmp-5.7.2/snmplib/transports/snmpUnixDomain.c.systemd 2012-10-10 00:28:58.000000000 +0200
1546 +++ net-snmp-5.7.2/snmplib/transports/snmpUnixDomain.c 2012-11-12 10:24:02.803466358 +0100
1548 #include <net-snmp/library/system.h> /* mkdirhier */
1549 #include <net-snmp/library/tools.h>
1551 +#ifndef NETSNMP_NO_SYSTEMD
1552 +#include <net-snmp/library/sd-daemon.h>
1555 netsnmp_feature_child_of(transport_unix_socket_all, transport_all)
1556 netsnmp_feature_child_of(unix_socket_paths, transport_unix_socket_all)
1558 @@ -295,6 +299,7 @@ netsnmp_unix_transport(struct sockaddr_u
1559 netsnmp_transport *t = NULL;
1560 sockaddr_un_pair *sup = NULL;
1562 + int socket_initialized = 0;
1564 #ifdef NETSNMP_NO_LISTEN_SUPPORT
1565 /* SPECIAL CIRCUMSTANCE: We still want AgentX to be able to operate,
1566 @@ -333,7 +338,18 @@ netsnmp_unix_transport(struct sockaddr_u
1567 t->data_length = sizeof(sockaddr_un_pair);
1568 sup = (sockaddr_un_pair *) t->data;
1570 - t->sock = socket(PF_UNIX, SOCK_STREAM, 0);
1571 +#ifndef NETSNMP_NO_SYSTEMD
1573 + * Maybe the socket was already provided by systemd...
1576 + t->sock = netsnmp_sd_find_unix_socket(SOCK_STREAM, 1, addr->sun_path);
1578 + socket_initialized = 1;
1581 + if (!socket_initialized)
1582 + t->sock = socket(PF_UNIX, SOCK_STREAM, 0);
1584 netsnmp_transport_free(t);
1586 @@ -357,25 +373,26 @@ netsnmp_unix_transport(struct sockaddr_u
1588 t->flags |= NETSNMP_TRANSPORT_FLAG_LISTEN;
1590 - unlink(addr->sun_path);
1591 - rc = bind(t->sock, (struct sockaddr *) addr, SUN_LEN(addr));
1593 - if (rc != 0 && errno == ENOENT && create_path) {
1594 - rc = mkdirhier(addr->sun_path, create_mode, 1);
1595 + if (!socket_initialized) {
1596 + unlink(addr->sun_path);
1597 + rc = bind(t->sock, (struct sockaddr *) addr, SUN_LEN(addr));
1598 + if (rc != 0 && errno == ENOENT && create_path) {
1599 + rc = mkdirhier(addr->sun_path, create_mode, 1);
1601 + netsnmp_unix_close(t);
1602 + netsnmp_transport_free(t);
1605 + rc = bind(t->sock, (struct sockaddr *) addr, SUN_LEN(addr));
1608 + DEBUGMSGTL(("netsnmp_unix_transport",
1609 + "couldn't bind \"%s\", errno %d (%s)\n",
1610 + addr->sun_path, errno, strerror(errno)));
1611 netsnmp_unix_close(t);
1612 netsnmp_transport_free(t);
1615 - rc = bind(t->sock, (struct sockaddr *) addr, SUN_LEN(addr));
1618 - DEBUGMSGTL(("netsnmp_unix_transport",
1619 - "couldn't bind \"%s\", errno %d (%s)\n",
1620 - addr->sun_path, errno, strerror(errno)));
1621 - netsnmp_unix_close(t);
1622 - netsnmp_transport_free(t);
1627 @@ -391,16 +408,17 @@ netsnmp_unix_transport(struct sockaddr_u
1628 * Now sit here and listen for connections to arrive.
1631 - rc = listen(t->sock, NETSNMP_STREAM_QUEUE_LEN);
1633 - DEBUGMSGTL(("netsnmp_unix_transport",
1634 - "couldn't listen to \"%s\", errno %d (%s)\n",
1635 - addr->sun_path, errno, strerror(errno)));
1636 - netsnmp_unix_close(t);
1637 - netsnmp_transport_free(t);
1639 + if (!socket_initialized) {
1640 + rc = listen(t->sock, NETSNMP_STREAM_QUEUE_LEN);
1642 + DEBUGMSGTL(("netsnmp_unix_transport",
1643 + "couldn't listen to \"%s\", errno %d (%s)\n",
1644 + addr->sun_path, errno, strerror(errno)));
1645 + netsnmp_unix_close(t);
1646 + netsnmp_transport_free(t);
1652 t->remote = (u_char *)malloc(strlen(addr->sun_path));
1653 if (t->remote == NULL) {