diff options
| -rw-r--r-- | meta-networking/recipes-protocols/net-snmp/net-snmp/systemd-support.patch | 1652 |
1 files changed, 0 insertions, 1652 deletions
diff --git a/meta-networking/recipes-protocols/net-snmp/net-snmp/systemd-support.patch b/meta-networking/recipes-protocols/net-snmp/net-snmp/systemd-support.patch deleted file mode 100644 index c6af8c0f33..0000000000 --- a/meta-networking/recipes-protocols/net-snmp/net-snmp/systemd-support.patch +++ /dev/null | |||
| @@ -1,1652 +0,0 @@ | |||
| 1 | From 0cad0c6c36af2a2d589563804c9ed2b37b7085fb Mon Sep 17 00:00:00 2001 | ||
| 2 | From: Li xin <lixin.fnst@cn.fujitsu.com> | ||
| 3 | Date: Fri, 21 Aug 2015 14:37:02 +0900 | ||
| 4 | Subject: [PATCH] ystemd support backported from the master branch as of | ||
| 5 | 23/04/2012 (post 5.7.1, pre 5.8). | ||
| 6 | |||
| 7 | The following commits have been cherry-picked: | ||
| 8 | |||
| 9 | 19499c3c90bf9d7b2b9e5d08baa26cc6bba28a11 | ||
| 10 | fef6cddfdb94da1a6b1fb768af62918b80f11fd3 | ||
| 11 | 0641e43c694c485cbbffef0556efc4641bd3ff50 | ||
| 12 | 76530a89f1c8bbd0b63acce63e10d5d4812a1a16 (conflict resolved) | ||
| 13 | bf108d7f1354f6276fc43c129963f2c49b9fc242 | ||
| 14 | 3692875172352f72cf3afd0d35f355e83d7e421b | ||
| 15 | 74412748067c685e1d8ab6ed3bcc3ca9c2774844 | ||
| 16 | 86132e3f1e6ef7b4e0b96d8fa24e37c81b71b0e0 | ||
| 17 | 63557cf8986a33dba1d4429b583a901361052c4f | ||
| 18 | |||
| 19 | Upstream-Status: Backport | ||
| 20 | |||
| 21 | Signed-off-by: Thomas Fitzsimmons <fitzsim@cisco.com> | ||
| 22 | --- | ||
| 23 | README.systemd | 41 +++ | ||
| 24 | agent/snmpd.c | 33 +- | ||
| 25 | apps/snmptrapd.c | 32 +- | ||
| 26 | configure.d/config_modules_lib | 8 + | ||
| 27 | configure.d/config_project_with_enable | 9 + | ||
| 28 | dist/snmpd.servic | 18 + | ||
| 29 | dist/snmpd.socket | 17 + | ||
| 30 | dist/snmptrapd.service | 16 + | ||
| 31 | dist/snmptrapd.socket | 14 + | ||
| 32 | include/net-snmp/library/sd-daemon.h | 290 ++++++++++++++++ | ||
| 33 | snmplib/sd-daemon.c | 532 +++++++++++++++++++++++++++++ | ||
| 34 | snmplib/transports/snmpTCPDomain.c | 43 ++- | ||
| 35 | snmplib/transports/snmpTCPIPv6Domain.c | 46 ++- | ||
| 36 | snmplib/transports/snmpUDPIPv4BaseDomain.c | 33 +- | ||
| 37 | snmplib/transports/snmpUDPIPv6Domain.c | 34 +- | ||
| 38 | snmplib/transports/snmpUnixDomain.c | 66 ++-- | ||
| 39 | win32/libsnmp/Makefile.in | 6 + | ||
| 40 | win32/net-snmp/net-snmp-config.h | 2 + | ||
| 41 | win32/net-snmp/net-snmp-config.h.in | 2 + | ||
| 42 | 19 files changed, 1176 insertions(+), 66 deletions(-) | ||
| 43 | create mode 100644 README.systemd | ||
| 44 | create mode 100644 dist/snmpd.servic | ||
| 45 | create mode 100644 dist/snmpd.socket | ||
| 46 | create mode 100644 dist/snmptrapd.service | ||
| 47 | create mode 100644 dist/snmptrapd.socket | ||
| 48 | create mode 100644 include/net-snmp/library/sd-daemon.h | ||
| 49 | create mode 100644 snmplib/sd-daemon.c | ||
| 50 | |||
| 51 | diff --git a/README.systemd b/README.systemd | ||
| 52 | new file mode 100644 | ||
| 53 | index 0000000..dba15d1 | ||
| 54 | --- /dev/null | ||
| 55 | +++ b/README.systemd | ||
| 56 | @@ -0,0 +1,41 @@ | ||
| 57 | +README.systemd | ||
| 58 | +-------------- | ||
| 59 | +Net-SNMP provides two daemons, which support systemd system manager. | ||
| 60 | +See http://www.freedesktop.org/wiki/Software/systemd to learn how | ||
| 61 | +systemd works. Both socket activation and notification is supported by these | ||
| 62 | +daemons. | ||
| 63 | + | ||
| 64 | +To enable systemd support, the sources must be compiled with | ||
| 65 | +--with-systemd configure option. | ||
| 66 | + | ||
| 67 | +snmpd - The SNMP agent | ||
| 68 | +---------------------- | ||
| 69 | +Socket activation od snmpd daemon is implemented, but it's discouraged. | ||
| 70 | +The reason is simple - snmpd not only listens and processes SNMP requests | ||
| 71 | +from network, but also gathers system statistics counters, sends traps and | ||
| 72 | +communicates with subagents. It even opens few netlink sockets. | ||
| 73 | + | ||
| 74 | +In other words, snmpd should run from system start to properly work. | ||
| 75 | +This can be done in two ways: | ||
| 76 | +1) either as snmpd service unit with 'Type=notification' and without a socket | ||
| 77 | + unit | ||
| 78 | +2) or as snmpd service unit with 'Type=simple', appropriate socket socket unit | ||
| 79 | + and the snmpd service enabled. This way systemd creates the snmpd listening | ||
| 80 | + socket early during boot and passes the sockets to snmpd slightly later | ||
| 81 | + (but still during machine boot). This way systemd can paralelize start of | ||
| 82 | + services, which depend on snmpd. Admins must adjust the socket file manually, | ||
| 83 | + depending if the snmpd support AgentX, IPv6, SMUX etc. | ||
| 84 | + | ||
| 85 | +snmpd should be started with '-f' command line parameter to disable forking - | ||
| 86 | +systemd does that for us automatically. | ||
| 87 | + | ||
| 88 | + | ||
| 89 | +snmptrapd - The trap processing daemon | ||
| 90 | +-------------------------------------- | ||
| 91 | +snmptrapd supports full socket activation and also notification (if needed). | ||
| 92 | +Both 'Type=simple' (with appropriate socket unit) and 'Type=notify' services | ||
| 93 | +will work. Again, '-f' parameter should be provided on snmptrapd command line. | ||
| 94 | + | ||
| 95 | +If integration with SNMP agent using AgentX protocol is enabled, snmptrapd should | ||
| 96 | +start during boot and not after first SNMP trap arrives. Same rules as for snmpd | ||
| 97 | +applies then. | ||
| 98 | diff --git a/agent/snmpd.c b/agent/snmpd.c | ||
| 99 | index cfc7bce..116ee5c 100644 | ||
| 100 | --- a/agent/snmpd.c | ||
| 101 | +++ b/agent/snmpd.c | ||
| 102 | @@ -164,6 +164,10 @@ typedef long fd_mask; | ||
| 103 | |||
| 104 | #endif | ||
| 105 | |||
| 106 | +#ifndef NETSNMP_NO_SYSTEMD | ||
| 107 | +#include <net-snmp/library/sd-daemon.h> | ||
| 108 | +#endif | ||
| 109 | + | ||
| 110 | netsnmp_feature_want(logging_file) | ||
| 111 | netsnmp_feature_want(logging_stdio) | ||
| 112 | netsnmp_feature_want(logging_syslog) | ||
| 113 | @@ -443,19 +447,29 @@ main(int argc, char *argv[]) | ||
| 114 | int agent_mode = -1; | ||
| 115 | char *pid_file = NULL; | ||
| 116 | char option_compatability[] = "-Le"; | ||
| 117 | +#ifndef WIN32 | ||
| 118 | + int prepared_sockets = 0; | ||
| 119 | +#endif | ||
| 120 | #if HAVE_GETPID | ||
| 121 | int fd; | ||
| 122 | FILE *PID; | ||
| 123 | #endif | ||
| 124 | |||
| 125 | #ifndef WIN32 | ||
| 126 | +#ifndef NETSNMP_NO_SYSTEMD | ||
| 127 | + /* check if systemd has sockets for us and don't close them */ | ||
| 128 | + prepared_sockets = netsnmp_sd_listen_fds(0); | ||
| 129 | +#endif /* NETSNMP_NO_SYSTEMD */ | ||
| 130 | + | ||
| 131 | /* | ||
| 132 | * close all non-standard file descriptors we may have | ||
| 133 | * inherited from the shell. | ||
| 134 | */ | ||
| 135 | - for (i = getdtablesize() - 1; i > 2; --i) { | ||
| 136 | - (void) close(i); | ||
| 137 | - } | ||
| 138 | + if (!prepared_sockets) { | ||
| 139 | + for (i = getdtablesize() - 1; i > 2; --i) { | ||
| 140 | + (void) close(i); | ||
| 141 | + } | ||
| 142 | +} | ||
| 143 | #endif /* #WIN32 */ | ||
| 144 | |||
| 145 | /* | ||
| 146 | @@ -1107,6 +1121,19 @@ main(int argc, char *argv[]) | ||
| 147 | netsnmp_addrcache_initialise(); | ||
| 148 | |||
| 149 | /* | ||
| 150 | + * Let systemd know we're up. | ||
| 151 | + */ | ||
| 152 | +#ifndef NETSNMP_NO_SYSTEMD | ||
| 153 | + netsnmp_sd_notify(1, "READY=1\n"); | ||
| 154 | + if (prepared_sockets) | ||
| 155 | + /* | ||
| 156 | + * Clear the environment variable, we already processed all the sockets | ||
| 157 | + * by now. | ||
| 158 | + */ | ||
| 159 | + netsnmp_sd_listen_fds(1); | ||
| 160 | +#endif | ||
| 161 | + | ||
| 162 | + /* | ||
| 163 | * Forever monitor the dest_port for incoming PDUs. | ||
| 164 | */ | ||
| 165 | DEBUGMSGTL(("snmpd/main", "We're up. Starting to process data.\n")); | ||
| 166 | diff --git a/apps/snmptrapd.c b/apps/snmptrapd.c | ||
| 167 | index bce0d47..c6a74ec 100644 | ||
| 168 | --- a/apps/snmptrapd.c | ||
| 169 | +++ b/apps/snmptrapd.c | ||
| 170 | @@ -125,6 +125,10 @@ SOFTWARE. | ||
| 171 | |||
| 172 | #include <net-snmp/net-snmp-features.h> | ||
| 173 | |||
| 174 | +#ifndef NETSNMP_NO_SYSTEMD | ||
| 175 | +#include <net-snmp/library/sd-daemon.h> | ||
| 176 | +#endif | ||
| 177 | + | ||
| 178 | #ifndef BSD4_3 | ||
| 179 | #define BSD4_2 | ||
| 180 | #endif | ||
| 181 | @@ -657,16 +661,25 @@ main(int argc, char *argv[]) | ||
| 182 | int agentx_subagent = 1; | ||
| 183 | #endif | ||
| 184 | netsnmp_trapd_handler *traph; | ||
| 185 | +#ifndef WIN32 | ||
| 186 | + int prepared_sockets = 0; | ||
| 187 | +#endif | ||
| 188 | |||
| 189 | |||
| 190 | #ifndef WIN32 | ||
| 191 | +#ifndef NETSNMP_NO_SYSTEMD | ||
| 192 | + /* check if systemd has sockets for us and don't close them */ | ||
| 193 | + prepared_sockets = netsnmp_sd_listen_fds(0); | ||
| 194 | +#endif | ||
| 195 | /* | ||
| 196 | * close all non-standard file descriptors we may have | ||
| 197 | * inherited from the shell. | ||
| 198 | */ | ||
| 199 | - for (i = getdtablesize() - 1; i > 2; --i) { | ||
| 200 | - (void) close(i); | ||
| 201 | - } | ||
| 202 | + if (!prepared_sockets) { | ||
| 203 | + for (i = getdtablesize() - 1; i > 2; --i) { | ||
| 204 | + (void) close(i); | ||
| 205 | + } | ||
| 206 | +} | ||
| 207 | #endif /* #WIN32 */ | ||
| 208 | |||
| 209 | #ifdef SIGTERM | ||
| 210 | @@ -1318,6 +1331,19 @@ main(int argc, char *argv[]) | ||
| 211 | #endif | ||
| 212 | #endif | ||
| 213 | |||
| 214 | + /* | ||
| 215 | + * Let systemd know we're up. | ||
| 216 | + */ | ||
| 217 | +#ifndef NETSNMP_NO_SYSTEMD | ||
| 218 | + netsnmp_sd_notify(1, "READY=1\n"); | ||
| 219 | + if (prepared_sockets) | ||
| 220 | + /* | ||
| 221 | + * Clear the environment variable, we already processed all the sockets | ||
| 222 | + * by now. | ||
| 223 | + */ | ||
| 224 | + netsnmp_sd_listen_fds(1); | ||
| 225 | +#endif | ||
| 226 | + | ||
| 227 | #ifdef WIN32SERVICE | ||
| 228 | trapd_status = SNMPTRAPD_RUNNING; | ||
| 229 | #endif | ||
| 230 | diff --git a/configure.d/config_modules_lib b/configure.d/config_modules_lib | ||
| 231 | index 362ba0a..bb69daa 100644 | ||
| 232 | --- a/configure.d/config_modules_lib | ||
| 233 | +++ b/configure.d/config_modules_lib | ||
| 234 | @@ -53,6 +53,14 @@ if test "x$PARTIALTARGETOS" = "xmingw32" -o "x$PARTIALTARGETOS" = "xmingw32msvc" | ||
| 235 | other_ftobjs_list="$other_ftobjs_list winpipe.ft" | ||
| 236 | fi | ||
| 237 | |||
| 238 | +# Linux systemd | ||
| 239 | +if test "x$with_systemd" == "xyes"; then | ||
| 240 | + other_src_list="$other_src_list sd-daemon.c" | ||
| 241 | + other_objs_list="$other_objs_list sd-daemon.o" | ||
| 242 | + other_lobjs_list="$other_lobjs_list sd-daemon.lo" | ||
| 243 | + other_ftobjs_list="$other_ftobjs_list sd-daemon.ft" | ||
| 244 | +fi | ||
| 245 | + | ||
| 246 | AC_SUBST(other_src_list) | ||
| 247 | AC_SUBST(other_objs_list) | ||
| 248 | AC_SUBST(other_lobjs_list) | ||
| 249 | diff --git a/configure.d/config_project_with_enable b/configure.d/config_project_with_enable | ||
| 250 | index 61ba026..d782d12 100644 | ||
| 251 | --- a/configure.d/config_project_with_enable | ||
| 252 | +++ b/configure.d/config_project_with_enable | ||
| 253 | @@ -690,6 +690,15 @@ if test "x$with_dummy_values" != "xyes"; then | ||
| 254 | data for]) | ||
| 255 | fi | ||
| 256 | |||
| 257 | +NETSNMP_ARG_WITH(systemd, | ||
| 258 | +[ --with-systemd Provide systemd support. See README.systemd | ||
| 259 | + for details.]) | ||
| 260 | +# Define unless specifically suppressed (i.e., option defaults to false). | ||
| 261 | +if test "x$with_systemd" != "xyes"; then | ||
| 262 | + AC_DEFINE(NETSNMP_NO_SYSTEMD, 1, | ||
| 263 | + [If you don't want to integrate with systemd.]) | ||
| 264 | +fi | ||
| 265 | + | ||
| 266 | NETSNMP_ARG_ENABLE(set-support, | ||
| 267 | [ --disable-set-support Do not allow SNMP set requests.]) | ||
| 268 | if test "x$enable_set_support" = "xno"; then | ||
| 269 | diff --git a/dist/snmpd.servic b/dist/snmpd.servic | ||
| 270 | new file mode 100644 | ||
| 271 | index 0000000..31391e5 | ||
| 272 | --- /dev/null | ||
| 273 | +++ b/dist/snmpd.servic | ||
| 274 | @@ -0,0 +1,18 @@ | ||
| 275 | +# | ||
| 276 | +# SNMP agent service file for systemd | ||
| 277 | +# | ||
| 278 | +# | ||
| 279 | +# The service should be enabled, i.e. snmpd should start during machine boot. | ||
| 280 | +# Socket activation shall not be used. See README.systemd for details. | ||
| 281 | + | ||
| 282 | +[Unit] | ||
| 283 | +Description=Simple Network Management Protocol (SNMP) daemon. | ||
| 284 | +After=syslog.target network.target | ||
| 285 | + | ||
| 286 | +[Service] | ||
| 287 | +# Type=notify is also supported. It should be set when snmpd.socket is not used. | ||
| 288 | +Type=simple | ||
| 289 | +ExecStart=/usr/sbin/snmpd -f | ||
| 290 | + | ||
| 291 | +[Install] | ||
| 292 | +WantedBy=multi-user.target | ||
| 293 | diff --git a/dist/snmpd.socket b/dist/snmpd.socket | ||
| 294 | new file mode 100644 | ||
| 295 | index 0000000..7f3a2d9 | ||
| 296 | --- /dev/null | ||
| 297 | +++ b/dist/snmpd.socket | ||
| 298 | @@ -0,0 +1,17 @@ | ||
| 299 | +[Unit] | ||
| 300 | +Description=Socket listening for SNMP and AgentX messages | ||
| 301 | + | ||
| 302 | +[Socket] | ||
| 303 | +ListenDatagram=0.0.0.0:161 | ||
| 304 | +# Uncomment other listening addresses as needed - TCP, UDP6, TCP6. | ||
| 305 | +# It must match listening addresses/ports defined in snmpd.service | ||
| 306 | +# or snmpd.conf. | ||
| 307 | +# ListenStream=0.0.0.0:161 | ||
| 308 | +# ListenDatagram=[::]:161 | ||
| 309 | +# ListenStream=[::]:161 | ||
| 310 | +# | ||
| 311 | +# Uncomment AgentX socket if snmpd.conf enables AgentX protocol. | ||
| 312 | +# ListenStream=/var/agentx/master | ||
| 313 | + | ||
| 314 | +[Install] | ||
| 315 | +WantedBy=sockets.target | ||
| 316 | diff --git a/dist/snmptrapd.service b/dist/snmptrapd.service | ||
| 317 | new file mode 100644 | ||
| 318 | index 0000000..e88a5b4 | ||
| 319 | --- /dev/null | ||
| 320 | +++ b/dist/snmptrapd.service | ||
| 321 | @@ -0,0 +1,16 @@ | ||
| 322 | +# | ||
| 323 | +# SNMP trap-processing service file for systemd | ||
| 324 | +# | ||
| 325 | + | ||
| 326 | +[Unit] | ||
| 327 | +Description=Simple Network Management Protocol (SNMP) Trap daemon. | ||
| 328 | +After=syslog.target network.target | ||
| 329 | + | ||
| 330 | +[Service] | ||
| 331 | +# Type=notify is also supported. It should be set when snmptrapd.socket is not | ||
| 332 | +# used. | ||
| 333 | +Type=simple | ||
| 334 | +ExecStart=/usr/sbin/snmptrapd -f | ||
| 335 | + | ||
| 336 | +[Install] | ||
| 337 | +WantedBy=multi-user.target | ||
| 338 | diff --git a/dist/snmptrapd.socket b/dist/snmptrapd.socket | ||
| 339 | new file mode 100644 | ||
| 340 | index 0000000..2d24fb8 | ||
| 341 | --- /dev/null | ||
| 342 | +++ b/dist/snmptrapd.socket | ||
| 343 | @@ -0,0 +1,14 @@ | ||
| 344 | ++[Unit] | ||
| 345 | ++Description=Socket listening for SNMP trap messages | ||
| 346 | ++ | ||
| 347 | ++[Socket] | ||
| 348 | ++ListenDatagram=0.0.0.0:162 | ||
| 349 | ++# Uncomment other listening addresses as needed - TCP, UDP6, TCP6. | ||
| 350 | ++# It must match listening addresses/ports defined in snmptrapd.service | ||
| 351 | ++# or snmptrapd.conf. | ||
| 352 | ++# ListenStream=0.0.0.0:162 | ||
| 353 | ++# ListenDatagram=[::]:162 | ||
| 354 | ++# ListenStream=[::]:162 | ||
| 355 | ++ | ||
| 356 | ++[Install] | ||
| 357 | ++WantedBy=sockets.target | ||
| 358 | diff --git a/include/net-snmp/library/sd-daemon.h b/include/net-snmp/library/sd-daemon.h | ||
| 359 | new file mode 100644 | ||
| 360 | index 0000000..85274c9 | ||
| 361 | --- /dev/null | ||
| 362 | +++ b/include/net-snmp/library/sd-daemon.h | ||
| 363 | @@ -0,0 +1,290 @@ | ||
| 364 | +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ | ||
| 365 | + | ||
| 366 | +#ifndef SNMPD_SD_DAEMON_H | ||
| 367 | +#define SNMPD_SD_DAEMON_H | ||
| 368 | + | ||
| 369 | +/*** | ||
| 370 | + Copyright 2010 Lennart Poettering | ||
| 371 | + | ||
| 372 | + Permission is hereby granted, free of charge, to any person | ||
| 373 | + obtaining a copy of this software and associated documentation files | ||
| 374 | + (the "Software"), to deal in the Software without restriction, | ||
| 375 | + including without limitation the rights to use, copy, modify, merge, | ||
| 376 | + publish, distribute, sublicense, and/or sell copies of the Software, | ||
| 377 | + and to permit persons to whom the Software is furnished to do so, | ||
| 378 | + subject to the following conditions: | ||
| 379 | + | ||
| 380 | + The above copyright notice and this permission notice shall be | ||
| 381 | + included in all copies or substantial portions of the Software. | ||
| 382 | + | ||
| 383 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
| 384 | + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
| 385 | + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
| 386 | + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | ||
| 387 | + BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | ||
| 388 | + ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
| 389 | + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| 390 | + SOFTWARE. | ||
| 391 | +***/ | ||
| 392 | + | ||
| 393 | +#ifdef HAVE_SYS_TYPES_H | ||
| 394 | +#include <sys/types.h> | ||
| 395 | +#endif | ||
| 396 | +#ifdef HAVE_INTTYPES_H | ||
| 397 | +#include <inttypes.h> | ||
| 398 | +#endif | ||
| 399 | + | ||
| 400 | +#ifdef __cplusplus | ||
| 401 | +extern "C" { | ||
| 402 | +#endif | ||
| 403 | + | ||
| 404 | +/* | ||
| 405 | + Reference implementation of a few systemd related interfaces for | ||
| 406 | + writing daemons. These interfaces are trivial to implement. To | ||
| 407 | + simplify porting we provide this reference implementation. | ||
| 408 | + Applications are welcome to reimplement the algorithms described | ||
| 409 | + here if they do not want to include these two source files. | ||
| 410 | + | ||
| 411 | + The following functionality is provided: | ||
| 412 | + | ||
| 413 | + - Support for logging with log levels on stderr | ||
| 414 | + - File descriptor passing for socket-based activation | ||
| 415 | + - Daemon startup and status notification | ||
| 416 | + - Detection of systemd boots | ||
| 417 | + | ||
| 418 | + You may compile this with -DDISABLE_SYSTEMD to disable systemd | ||
| 419 | + support. This makes all those calls NOPs that are directly related to | ||
| 420 | + systemd (i.e. only sd_is_xxx() will stay useful). | ||
| 421 | + | ||
| 422 | + Since this is drop-in code we don't want any of our symbols to be | ||
| 423 | + exported in any case. Hence we declare hidden visibility for all of | ||
| 424 | + them. | ||
| 425 | + | ||
| 426 | + You may find an up-to-date version of these source files online: | ||
| 427 | + | ||
| 428 | + http://cgit.freedesktop.org/systemd/plain/src/sd-daemon.h | ||
| 429 | + http://cgit.freedesktop.org/systemd/plain/src/sd-daemon.c | ||
| 430 | + | ||
| 431 | + This should compile on non-Linux systems, too, but with the | ||
| 432 | + exception of the sd_is_xxx() calls all functions will become NOPs. | ||
| 433 | + | ||
| 434 | + See sd-daemon(7) for more information. | ||
| 435 | +*/ | ||
| 436 | + | ||
| 437 | +#ifndef _sd_printf_attr_ | ||
| 438 | +#if __GNUC__ >= 4 | ||
| 439 | +#define _sd_printf_attr_(a,b) __attribute__ ((format (printf, a, b))) | ||
| 440 | +#else | ||
| 441 | +#define _sd_printf_attr_(a,b) | ||
| 442 | +#endif | ||
| 443 | +#endif | ||
| 444 | + | ||
| 445 | +/* | ||
| 446 | + Log levels for usage on stderr: | ||
| 447 | + | ||
| 448 | + fprintf(stderr, SD_NOTICE "Hello World!\n"); | ||
| 449 | + | ||
| 450 | + This is similar to printk() usage in the kernel. | ||
| 451 | +*/ | ||
| 452 | +#define SD_EMERG "<0>" /* system is unusable */ | ||
| 453 | +#define SD_ALERT "<1>" /* action must be taken immediately */ | ||
| 454 | +#define SD_CRIT "<2>" /* critical conditions */ | ||
| 455 | +#define SD_ERR "<3>" /* error conditions */ | ||
| 456 | +#define SD_WARNING "<4>" /* warning conditions */ | ||
| 457 | +#define SD_NOTICE "<5>" /* normal but significant condition */ | ||
| 458 | +#define SD_INFO "<6>" /* informational */ | ||
| 459 | +#define SD_DEBUG "<7>" /* debug-level messages */ | ||
| 460 | + | ||
| 461 | +/* The first passed file descriptor is fd 3 */ | ||
| 462 | +#define SD_LISTEN_FDS_START 3 | ||
| 463 | + | ||
| 464 | +/* | ||
| 465 | + Returns how many file descriptors have been passed, or a negative | ||
| 466 | + errno code on failure. Optionally, removes the $LISTEN_FDS and | ||
| 467 | + $LISTEN_PID file descriptors from the environment (recommended, but | ||
| 468 | + problematic in threaded environments). If r is the return value of | ||
| 469 | + this function you'll find the file descriptors passed as fds | ||
| 470 | + SD_LISTEN_FDS_START to SD_LISTEN_FDS_START+r-1. Returns a negative | ||
| 471 | + errno style error code on failure. This function call ensures that | ||
| 472 | + the FD_CLOEXEC flag is set for the passed file descriptors, to make | ||
| 473 | + sure they are not passed on to child processes. If FD_CLOEXEC shall | ||
| 474 | + not be set, the caller needs to unset it after this call for all file | ||
| 475 | + descriptors that are used. | ||
| 476 | + | ||
| 477 | + See sd_listen_fds(3) for more information. | ||
| 478 | +*/ | ||
| 479 | +int netsnmp_sd_listen_fds(int unset_environment); | ||
| 480 | + | ||
| 481 | +/* | ||
| 482 | + Helper call for identifying a passed file descriptor. Returns 1 if | ||
| 483 | + the file descriptor is a FIFO in the file system stored under the | ||
| 484 | + specified path, 0 otherwise. If path is NULL a path name check will | ||
| 485 | + not be done and the call only verifies if the file descriptor | ||
| 486 | + refers to a FIFO. Returns a negative errno style error code on | ||
| 487 | + failure. | ||
| 488 | + | ||
| 489 | + See sd_is_fifo(3) for more information. | ||
| 490 | +*/ | ||
| 491 | +int netsnmp_sd_is_fifo(int fd, const char *path); | ||
| 492 | + | ||
| 493 | +/* | ||
| 494 | + Helper call for identifying a passed file descriptor. Returns 1 if | ||
| 495 | + the file descriptor is a special character device on the file | ||
| 496 | + system stored under the specified path, 0 otherwise. | ||
| 497 | + If path is NULL a path name check will not be done and the call | ||
| 498 | + only verifies if the file descriptor refers to a special character. | ||
| 499 | + Returns a negative errno style error code on failure. | ||
| 500 | + | ||
| 501 | + See sd_is_special(3) for more information. | ||
| 502 | +*/ | ||
| 503 | +int netsnmp_sd_is_special(int fd, const char *path); | ||
| 504 | + | ||
| 505 | +/* | ||
| 506 | + Helper call for identifying a passed file descriptor. Returns 1 if | ||
| 507 | + the file descriptor is a socket of the specified family (AF_INET, | ||
| 508 | + ...) and type (SOCK_DGRAM, SOCK_STREAM, ...), 0 otherwise. If | ||
| 509 | + family is 0 a socket family check will not be done. If type is 0 a | ||
| 510 | + socket type check will not be done and the call only verifies if | ||
| 511 | + the file descriptor refers to a socket. If listening is > 0 it is | ||
| 512 | + verified that the socket is in listening mode. (i.e. listen() has | ||
| 513 | + been called) If listening is == 0 it is verified that the socket is | ||
| 514 | + not in listening mode. If listening is < 0 no listening mode check | ||
| 515 | + is done. Returns a negative errno style error code on failure. | ||
| 516 | + | ||
| 517 | + See sd_is_socket(3) for more information. | ||
| 518 | +*/ | ||
| 519 | +int netsnmp_sd_is_socket(int fd, int family, int type, int listening); | ||
| 520 | + | ||
| 521 | +/* | ||
| 522 | + Helper call for identifying a passed file descriptor. Returns 1 if | ||
| 523 | + the file descriptor is an Internet socket, of the specified family | ||
| 524 | + (either AF_INET or AF_INET6) and the specified type (SOCK_DGRAM, | ||
| 525 | + SOCK_STREAM, ...), 0 otherwise. If version is 0 a protocol version | ||
| 526 | + check is not done. If type is 0 a socket type check will not be | ||
| 527 | + done. If port is 0 a socket port check will not be done. The | ||
| 528 | + listening flag is used the same way as in sd_is_socket(). Returns a | ||
| 529 | + negative errno style error code on failure. | ||
| 530 | + | ||
| 531 | + See sd_is_socket_inet(3) for more information. | ||
| 532 | +*/ | ||
| 533 | +int netsnmp_sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port); | ||
| 534 | + | ||
| 535 | +/* | ||
| 536 | + Helper call for identifying a passed file descriptor. Returns 1 if | ||
| 537 | + the file descriptor is an AF_UNIX socket of the specified type | ||
| 538 | + (SOCK_DGRAM, SOCK_STREAM, ...) and path, 0 otherwise. If type is 0 | ||
| 539 | + a socket type check will not be done. If path is NULL a socket path | ||
| 540 | + check will not be done. For normal AF_UNIX sockets set length to | ||
| 541 | + 0. For abstract namespace sockets set length to the length of the | ||
| 542 | + socket name (including the initial 0 byte), and pass the full | ||
| 543 | + socket path in path (including the initial 0 byte). The listening | ||
| 544 | + flag is used the same way as in sd_is_socket(). Returns a negative | ||
| 545 | + errno style error code on failure. | ||
| 546 | + | ||
| 547 | + See sd_is_socket_unix(3) for more information. | ||
| 548 | +*/ | ||
| 549 | +int netsnmp_sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length); | ||
| 550 | + | ||
| 551 | +/* | ||
| 552 | + Informs systemd about changed daemon state. This takes a number of | ||
| 553 | + newline separated environment-style variable assignments in a | ||
| 554 | + string. The following variables are known: | ||
| 555 | + | ||
| 556 | + READY=1 Tells systemd that daemon startup is finished (only | ||
| 557 | + relevant for services of Type=notify). The passed | ||
| 558 | + argument is a boolean "1" or "0". Since there is | ||
| 559 | + little value in signaling non-readiness the only | ||
| 560 | + value daemons should send is "READY=1". | ||
| 561 | + | ||
| 562 | + STATUS=... Passes a single-line status string back to systemd | ||
| 563 | + that describes the daemon state. This is free-from | ||
| 564 | + and can be used for various purposes: general state | ||
| 565 | + feedback, fsck-like programs could pass completion | ||
| 566 | + percentages and failing programs could pass a human | ||
| 567 | + readable error message. Example: "STATUS=Completed | ||
| 568 | + 66% of file system check..." | ||
| 569 | + | ||
| 570 | + ERRNO=... If a daemon fails, the errno-style error code, | ||
| 571 | + formatted as string. Example: "ERRNO=2" for ENOENT. | ||
| 572 | + | ||
| 573 | + BUSERROR=... If a daemon fails, the D-Bus error-style error | ||
| 574 | + code. Example: "BUSERROR=org.freedesktop.DBus.Error.TimedOut" | ||
| 575 | + | ||
| 576 | + MAINPID=... The main pid of a daemon, in case systemd did not | ||
| 577 | + fork off the process itself. Example: "MAINPID=4711" | ||
| 578 | + | ||
| 579 | + Daemons can choose to send additional variables. However, it is | ||
| 580 | + recommended to prefix variable names not listed above with X_. | ||
| 581 | + | ||
| 582 | + Returns a negative errno-style error code on failure. Returns > 0 | ||
| 583 | + if systemd could be notified, 0 if it couldn't possibly because | ||
| 584 | + systemd is not running. | ||
| 585 | + | ||
| 586 | + Example: When a daemon finished starting up, it could issue this | ||
| 587 | + call to notify systemd about it: | ||
| 588 | + | ||
| 589 | + sd_notify(0, "READY=1"); | ||
| 590 | + | ||
| 591 | + See sd_notifyf() for more complete examples. | ||
| 592 | + | ||
| 593 | + See sd_notify(3) for more information. | ||
| 594 | +*/ | ||
| 595 | +int netsnmp_sd_notify(int unset_environment, const char *state); | ||
| 596 | + | ||
| 597 | +/* | ||
| 598 | + Similar to sd_notify() but takes a format string. | ||
| 599 | + | ||
| 600 | + Example 1: A daemon could send the following after initialization: | ||
| 601 | + | ||
| 602 | + sd_notifyf(0, "READY=1\n" | ||
| 603 | + "STATUS=Processing requests...\n" | ||
| 604 | + "MAINPID=%lu", | ||
| 605 | + (unsigned long) getpid()); | ||
| 606 | + | ||
| 607 | + Example 2: A daemon could send the following shortly before | ||
| 608 | + exiting, on failure: | ||
| 609 | + | ||
| 610 | + sd_notifyf(0, "STATUS=Failed to start up: %s\n" | ||
| 611 | + "ERRNO=%i", | ||
| 612 | + strerror(errno), | ||
| 613 | + errno); | ||
| 614 | + | ||
| 615 | + See sd_notifyf(3) for more information. | ||
| 616 | +*/ | ||
| 617 | +int netsnmp_sd_notifyf(int unset_environment, const char *format, ...) _sd_printf_attr_(2,3); | ||
| 618 | + | ||
| 619 | +/* | ||
| 620 | + Returns > 0 if the system was booted with systemd. Returns < 0 on | ||
| 621 | + error. Returns 0 if the system was not booted with systemd. Note | ||
| 622 | + that all of the functions above handle non-systemd boots just | ||
| 623 | + fine. You should NOT protect them with a call to this function. Also | ||
| 624 | + note that this function checks whether the system, not the user | ||
| 625 | + session is controlled by systemd. However the functions above work | ||
| 626 | + for both user and system services. | ||
| 627 | + | ||
| 628 | + See sd_booted(3) for more information. | ||
| 629 | +*/ | ||
| 630 | +int netsnmp_sd_booted(void); | ||
| 631 | + | ||
| 632 | +/** | ||
| 633 | + * Find an socket with given parameters. See man sd_is_socket_inet for | ||
| 634 | + * description of the arguments. | ||
| 635 | + * | ||
| 636 | + * Returns the file descriptor if it is found, 0 otherwise. | ||
| 637 | + */ | ||
| 638 | +int netsnmp_sd_find_inet_socket(int family, int type, int listening, int port); | ||
| 639 | + | ||
| 640 | +/** | ||
| 641 | + * Find an unix socket with given parameters. See man sd_is_socket_unix for | ||
| 642 | + * description of the arguments. | ||
| 643 | + * | ||
| 644 | + * Returns the file descriptor if it is found, 0 otherwise. | ||
| 645 | + */ | ||
| 646 | +int | ||
| 647 | +netsnmp_sd_find_unix_socket(int type, int listening, const char *path); | ||
| 648 | + | ||
| 649 | +#ifdef __cplusplus | ||
| 650 | +} | ||
| 651 | +#endif | ||
| 652 | + | ||
| 653 | +#endif /* SNMPD_SD_DAEMON_H */ | ||
| 654 | diff --git a/snmplib/sd-daemon.c b/snmplib/sd-daemon.c | ||
| 655 | new file mode 100644 | ||
| 656 | index 0000000..42dba29 | ||
| 657 | --- /dev/null | ||
| 658 | +++ b/snmplib/sd-daemon.c | ||
| 659 | @@ -0,0 +1,532 @@ | ||
| 660 | +/* | ||
| 661 | + * Systemd integration parts. | ||
| 662 | + * | ||
| 663 | + * Most of this file is directly copied from systemd sources. | ||
| 664 | + * Changes: | ||
| 665 | + * - all functions were renamed to have netsnmp_ prefix | ||
| 666 | + * - includes were changed to match Net-SNMP style. | ||
| 667 | + * - removed gcc export macros | ||
| 668 | + * - removed POSIX message queues | ||
| 669 | + */ | ||
| 670 | + | ||
| 671 | +#include <net-snmp/net-snmp-config.h> | ||
| 672 | +#include <net-snmp/net-snmp-features.h> | ||
| 673 | +#include <net-snmp/types.h> | ||
| 674 | +#include <net-snmp/library/snmp_debug.h> | ||
| 675 | + | ||
| 676 | +#ifndef NETSNMP_NO_SYSTEMD | ||
| 677 | + | ||
| 678 | +/*** | ||
| 679 | + Copyright 2010 Lennart Poettering | ||
| 680 | + | ||
| 681 | + Permission is hereby granted, free of charge, to any person | ||
| 682 | + obtaining a copy of this software and associated documentation files | ||
| 683 | + (the "Software"), to deal in the Software without restriction, | ||
| 684 | + including without limitation the rights to use, copy, modify, merge, | ||
| 685 | + publish, distribute, sublicense, and/or sell copies of the Software, | ||
| 686 | + and to permit persons to whom the Software is furnished to do so, | ||
| 687 | + subject to the following conditions: | ||
| 688 | + | ||
| 689 | + The above copyright notice and this permission notice shall be | ||
| 690 | + included in all copies or substantial portions of the Software. | ||
| 691 | + | ||
| 692 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
| 693 | + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
| 694 | + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
| 695 | + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | ||
| 696 | + BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | ||
| 697 | + ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
| 698 | + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| 699 | + SOFTWARE. | ||
| 700 | +***/ | ||
| 701 | + | ||
| 702 | +#ifndef _GNU_SOURCE | ||
| 703 | +#define _GNU_SOURCE | ||
| 704 | +#endif | ||
| 705 | + | ||
| 706 | +#include <sys/types.h> | ||
| 707 | +#include <sys/stat.h> | ||
| 708 | +#include <sys/socket.h> | ||
| 709 | +#include <sys/un.h> | ||
| 710 | +#include <sys/fcntl.h> | ||
| 711 | +#include <netinet/in.h> | ||
| 712 | +#include <stdlib.h> | ||
| 713 | +#include <errno.h> | ||
| 714 | +#include <unistd.h> | ||
| 715 | +#include <string.h> | ||
| 716 | +#include <stdarg.h> | ||
| 717 | +#include <stdio.h> | ||
| 718 | +#include <stddef.h> | ||
| 719 | +#include <limits.h> | ||
| 720 | + | ||
| 721 | +#include <net-snmp/library/sd-daemon.h> | ||
| 722 | + | ||
| 723 | +int netsnmp_sd_listen_fds(int unset_environment) { | ||
| 724 | + | ||
| 725 | + int r, fd; | ||
| 726 | + const char *e; | ||
| 727 | + char *p = NULL; | ||
| 728 | + unsigned long l; | ||
| 729 | + | ||
| 730 | + if (!(e = getenv("LISTEN_PID"))) { | ||
| 731 | + r = 0; | ||
| 732 | + goto finish; | ||
| 733 | + } | ||
| 734 | + | ||
| 735 | + errno = 0; | ||
| 736 | + l = strtoul(e, &p, 10); | ||
| 737 | + | ||
| 738 | + if (errno != 0) { | ||
| 739 | + r = -errno; | ||
| 740 | + goto finish; | ||
| 741 | + } | ||
| 742 | + | ||
| 743 | + if (!p || *p || l <= 0) { | ||
| 744 | + r = -EINVAL; | ||
| 745 | + goto finish; | ||
| 746 | + } | ||
| 747 | + | ||
| 748 | + /* Is this for us? */ | ||
| 749 | + if (getpid() != (pid_t) l) { | ||
| 750 | + r = 0; | ||
| 751 | + goto finish; | ||
| 752 | + } | ||
| 753 | + | ||
| 754 | + if (!(e = getenv("LISTEN_FDS"))) { | ||
| 755 | + r = 0; | ||
| 756 | + goto finish; | ||
| 757 | + } | ||
| 758 | + | ||
| 759 | + errno = 0; | ||
| 760 | + l = strtoul(e, &p, 10); | ||
| 761 | + | ||
| 762 | + if (errno != 0) { | ||
| 763 | + r = -errno; | ||
| 764 | + goto finish; | ||
| 765 | + } | ||
| 766 | + | ||
| 767 | + if (!p || *p) { | ||
| 768 | + r = -EINVAL; | ||
| 769 | + goto finish; | ||
| 770 | + } | ||
| 771 | + | ||
| 772 | + for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + (int) l; fd ++) { | ||
| 773 | + int flags; | ||
| 774 | + | ||
| 775 | + if ((flags = fcntl(fd, F_GETFD)) < 0) { | ||
| 776 | + r = -errno; | ||
| 777 | + goto finish; | ||
| 778 | + } | ||
| 779 | + | ||
| 780 | + if (flags & FD_CLOEXEC) | ||
| 781 | + continue; | ||
| 782 | + | ||
| 783 | + if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) { | ||
| 784 | + r = -errno; | ||
| 785 | + goto finish; | ||
| 786 | + } | ||
| 787 | + } | ||
| 788 | + | ||
| 789 | + r = (int) l; | ||
| 790 | + | ||
| 791 | +finish: | ||
| 792 | + if (unset_environment) { | ||
| 793 | + unsetenv("LISTEN_PID"); | ||
| 794 | + unsetenv("LISTEN_FDS"); | ||
| 795 | + } | ||
| 796 | + | ||
| 797 | + return r; | ||
| 798 | +} | ||
| 799 | + | ||
| 800 | +int netsnmp_sd_is_fifo(int fd, const char *path) { | ||
| 801 | + struct stat st_fd; | ||
| 802 | + | ||
| 803 | + if (fd < 0) | ||
| 804 | + return -EINVAL; | ||
| 805 | + | ||
| 806 | + memset(&st_fd, 0, sizeof(st_fd)); | ||
| 807 | + if (fstat(fd, &st_fd) < 0) | ||
| 808 | + return -errno; | ||
| 809 | + | ||
| 810 | + if (!S_ISFIFO(st_fd.st_mode)) | ||
| 811 | + return 0; | ||
| 812 | + | ||
| 813 | + if (path) { | ||
| 814 | + struct stat st_path; | ||
| 815 | + | ||
| 816 | + memset(&st_path, 0, sizeof(st_path)); | ||
| 817 | + if (stat(path, &st_path) < 0) { | ||
| 818 | + | ||
| 819 | + if (errno == ENOENT || errno == ENOTDIR) | ||
| 820 | + return 0; | ||
| 821 | + | ||
| 822 | + return -errno; | ||
| 823 | + } | ||
| 824 | + | ||
| 825 | + return | ||
| 826 | + st_path.st_dev == st_fd.st_dev && | ||
| 827 | + st_path.st_ino == st_fd.st_ino; | ||
| 828 | + } | ||
| 829 | + | ||
| 830 | + return 1; | ||
| 831 | +} | ||
| 832 | + | ||
| 833 | +int netsnmp_sd_is_special(int fd, const char *path) { | ||
| 834 | + struct stat st_fd; | ||
| 835 | + | ||
| 836 | + if (fd < 0) | ||
| 837 | + return -EINVAL; | ||
| 838 | + | ||
| 839 | + if (fstat(fd, &st_fd) < 0) | ||
| 840 | + return -errno; | ||
| 841 | + | ||
| 842 | + if (!S_ISREG(st_fd.st_mode) && !S_ISCHR(st_fd.st_mode)) | ||
| 843 | + return 0; | ||
| 844 | + | ||
| 845 | + if (path) { | ||
| 846 | + struct stat st_path; | ||
| 847 | + | ||
| 848 | + if (stat(path, &st_path) < 0) { | ||
| 849 | + | ||
| 850 | + if (errno == ENOENT || errno == ENOTDIR) | ||
| 851 | + return 0; | ||
| 852 | + | ||
| 853 | + return -errno; | ||
| 854 | + } | ||
| 855 | + | ||
| 856 | + if (S_ISREG(st_fd.st_mode) && S_ISREG(st_path.st_mode)) | ||
| 857 | + return | ||
| 858 | + st_path.st_dev == st_fd.st_dev && | ||
| 859 | + st_path.st_ino == st_fd.st_ino; | ||
| 860 | + else if (S_ISCHR(st_fd.st_mode) && S_ISCHR(st_path.st_mode)) | ||
| 861 | + return st_path.st_rdev == st_fd.st_rdev; | ||
| 862 | + else | ||
| 863 | + return 0; | ||
| 864 | + } | ||
| 865 | + | ||
| 866 | + return 1; | ||
| 867 | +} | ||
| 868 | + | ||
| 869 | +static int sd_is_socket_internal(int fd, int type, int listening) { | ||
| 870 | + struct stat st_fd; | ||
| 871 | + | ||
| 872 | + if (fd < 0 || type < 0) | ||
| 873 | + return -EINVAL; | ||
| 874 | + | ||
| 875 | + if (fstat(fd, &st_fd) < 0) | ||
| 876 | + return -errno; | ||
| 877 | + | ||
| 878 | + if (!S_ISSOCK(st_fd.st_mode)) | ||
| 879 | + return 0; | ||
| 880 | + | ||
| 881 | + if (type != 0) { | ||
| 882 | + int other_type = 0; | ||
| 883 | + socklen_t l = sizeof(other_type); | ||
| 884 | + | ||
| 885 | + if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &other_type, &l) < 0) | ||
| 886 | + return -errno; | ||
| 887 | + | ||
| 888 | + if (l != sizeof(other_type)) | ||
| 889 | + return -EINVAL; | ||
| 890 | + | ||
| 891 | + if (other_type != type) | ||
| 892 | + return 0; | ||
| 893 | + } | ||
| 894 | + | ||
| 895 | + if (listening >= 0) { | ||
| 896 | + int accepting = 0; | ||
| 897 | + socklen_t l = sizeof(accepting); | ||
| 898 | + | ||
| 899 | + if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &accepting, &l) < 0) | ||
| 900 | + return -errno; | ||
| 901 | + | ||
| 902 | + if (l != sizeof(accepting)) | ||
| 903 | + return -EINVAL; | ||
| 904 | + | ||
| 905 | + if (!accepting != !listening) | ||
| 906 | + return 0; | ||
| 907 | + } | ||
| 908 | + | ||
| 909 | + return 1; | ||
| 910 | +} | ||
| 911 | + | ||
| 912 | +union sockaddr_union { | ||
| 913 | + struct sockaddr sa; | ||
| 914 | + struct sockaddr_in in4; | ||
| 915 | + struct sockaddr_in6 in6; | ||
| 916 | + struct sockaddr_un un; | ||
| 917 | + struct sockaddr_storage storage; | ||
| 918 | +}; | ||
| 919 | + | ||
| 920 | +int netsnmp_sd_is_socket(int fd, int family, int type, int listening) { | ||
| 921 | + int r; | ||
| 922 | + | ||
| 923 | + if (family < 0) | ||
| 924 | + return -EINVAL; | ||
| 925 | + | ||
| 926 | + if ((r = sd_is_socket_internal(fd, type, listening)) <= 0) | ||
| 927 | + return r; | ||
| 928 | + | ||
| 929 | + if (family > 0) { | ||
| 930 | + union sockaddr_union sockaddr; | ||
| 931 | + socklen_t l; | ||
| 932 | + | ||
| 933 | + memset(&sockaddr, 0, sizeof(sockaddr)); | ||
| 934 | + l = sizeof(sockaddr); | ||
| 935 | + | ||
| 936 | + if (getsockname(fd, &sockaddr.sa, &l) < 0) | ||
| 937 | + return -errno; | ||
| 938 | + | ||
| 939 | + if (l < sizeof(sa_family_t)) | ||
| 940 | + return -EINVAL; | ||
| 941 | + | ||
| 942 | + return sockaddr.sa.sa_family == family; | ||
| 943 | + } | ||
| 944 | + | ||
| 945 | + return 1; | ||
| 946 | +} | ||
| 947 | + | ||
| 948 | +int netsnmp_sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port) { | ||
| 949 | + union sockaddr_union sockaddr; | ||
| 950 | + socklen_t l; | ||
| 951 | + int r; | ||
| 952 | + | ||
| 953 | + if (family != 0 && family != AF_INET && family != AF_INET6) | ||
| 954 | + return -EINVAL; | ||
| 955 | + | ||
| 956 | + if ((r = sd_is_socket_internal(fd, type, listening)) <= 0) | ||
| 957 | + return r; | ||
| 958 | + | ||
| 959 | + memset(&sockaddr, 0, sizeof(sockaddr)); | ||
| 960 | + l = sizeof(sockaddr); | ||
| 961 | + | ||
| 962 | + if (getsockname(fd, &sockaddr.sa, &l) < 0) | ||
| 963 | + return -errno; | ||
| 964 | + | ||
| 965 | + if (l < sizeof(sa_family_t)) | ||
| 966 | + return -EINVAL; | ||
| 967 | + | ||
| 968 | + if (sockaddr.sa.sa_family != AF_INET && | ||
| 969 | + sockaddr.sa.sa_family != AF_INET6) | ||
| 970 | + return 0; | ||
| 971 | + | ||
| 972 | + if (family > 0) | ||
| 973 | + if (sockaddr.sa.sa_family != family) | ||
| 974 | + return 0; | ||
| 975 | + | ||
| 976 | + if (port > 0) { | ||
| 977 | + if (sockaddr.sa.sa_family == AF_INET) { | ||
| 978 | + if (l < sizeof(struct sockaddr_in)) | ||
| 979 | + return -EINVAL; | ||
| 980 | + | ||
| 981 | + return htons(port) == sockaddr.in4.sin_port; | ||
| 982 | + } else { | ||
| 983 | + if (l < sizeof(struct sockaddr_in6)) | ||
| 984 | + return -EINVAL; | ||
| 985 | + | ||
| 986 | + return htons(port) == sockaddr.in6.sin6_port; | ||
| 987 | + } | ||
| 988 | + } | ||
| 989 | + | ||
| 990 | + return 1; | ||
| 991 | +} | ||
| 992 | + | ||
| 993 | +int netsnmp_sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length) { | ||
| 994 | + union sockaddr_union sockaddr; | ||
| 995 | + socklen_t l; | ||
| 996 | + int r; | ||
| 997 | + | ||
| 998 | + if ((r = sd_is_socket_internal(fd, type, listening)) <= 0) | ||
| 999 | + return r; | ||
| 1000 | + | ||
| 1001 | + memset(&sockaddr, 0, sizeof(sockaddr)); | ||
| 1002 | + l = sizeof(sockaddr); | ||
| 1003 | + | ||
| 1004 | + if (getsockname(fd, &sockaddr.sa, &l) < 0) | ||
| 1005 | + return -errno; | ||
| 1006 | + | ||
| 1007 | + if (l < sizeof(sa_family_t)) | ||
| 1008 | + return -EINVAL; | ||
| 1009 | + | ||
| 1010 | + if (sockaddr.sa.sa_family != AF_UNIX) | ||
| 1011 | + return 0; | ||
| 1012 | + | ||
| 1013 | + if (path) { | ||
| 1014 | + if (length <= 0) | ||
| 1015 | + length = strlen(path); | ||
| 1016 | + | ||
| 1017 | + if (length <= 0) | ||
| 1018 | + /* Unnamed socket */ | ||
| 1019 | + return l == offsetof(struct sockaddr_un, sun_path); | ||
| 1020 | + | ||
| 1021 | + if (path[0]) | ||
| 1022 | + /* Normal path socket */ | ||
| 1023 | + return | ||
| 1024 | + (l >= offsetof(struct sockaddr_un, sun_path) + length + 1) && | ||
| 1025 | + memcmp(path, sockaddr.un.sun_path, length+1) == 0; | ||
| 1026 | + else | ||
| 1027 | + /* Abstract namespace socket */ | ||
| 1028 | + return | ||
| 1029 | + (l == offsetof(struct sockaddr_un, sun_path) + length) && | ||
| 1030 | + memcmp(path, sockaddr.un.sun_path, length) == 0; | ||
| 1031 | + } | ||
| 1032 | + | ||
| 1033 | + return 1; | ||
| 1034 | +} | ||
| 1035 | + | ||
| 1036 | +int netsnmp_sd_notify(int unset_environment, const char *state) { | ||
| 1037 | + int fd = -1, r; | ||
| 1038 | + struct msghdr msghdr; | ||
| 1039 | + struct iovec iovec; | ||
| 1040 | + union sockaddr_union sockaddr; | ||
| 1041 | + const char *e; | ||
| 1042 | + | ||
| 1043 | + if (!state) { | ||
| 1044 | + r = -EINVAL; | ||
| 1045 | + goto finish; | ||
| 1046 | + } | ||
| 1047 | + | ||
| 1048 | + if (!(e = getenv("NOTIFY_SOCKET"))) | ||
| 1049 | + return 0; | ||
| 1050 | + | ||
| 1051 | + /* Must be an abstract socket, or an absolute path */ | ||
| 1052 | + if ((e[0] != '@' && e[0] != '/') || e[1] == 0) { | ||
| 1053 | + r = -EINVAL; | ||
| 1054 | + goto finish; | ||
| 1055 | + } | ||
| 1056 | + | ||
| 1057 | + if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) { | ||
| 1058 | + r = -errno; | ||
| 1059 | + goto finish; | ||
| 1060 | + } | ||
| 1061 | + | ||
| 1062 | + memset(&sockaddr, 0, sizeof(sockaddr)); | ||
| 1063 | + sockaddr.sa.sa_family = AF_UNIX; | ||
| 1064 | + strncpy(sockaddr.un.sun_path, e, sizeof(sockaddr.un.sun_path)); | ||
| 1065 | + | ||
| 1066 | + if (sockaddr.un.sun_path[0] == '@') | ||
| 1067 | + sockaddr.un.sun_path[0] = 0; | ||
| 1068 | + | ||
| 1069 | + memset(&iovec, 0, sizeof(iovec)); | ||
| 1070 | + iovec.iov_base = (char *)state; | ||
| 1071 | + iovec.iov_len = strlen(state); | ||
| 1072 | + | ||
| 1073 | + memset(&msghdr, 0, sizeof(msghdr)); | ||
| 1074 | + msghdr.msg_name = &sockaddr; | ||
| 1075 | + msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(e); | ||
| 1076 | + | ||
| 1077 | + if (msghdr.msg_namelen > sizeof(struct sockaddr_un)) | ||
| 1078 | + msghdr.msg_namelen = sizeof(struct sockaddr_un); | ||
| 1079 | + | ||
| 1080 | + msghdr.msg_iov = &iovec; | ||
| 1081 | + msghdr.msg_iovlen = 1; | ||
| 1082 | + | ||
| 1083 | + if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) { | ||
| 1084 | + r = -errno; | ||
| 1085 | + goto finish; | ||
| 1086 | + } | ||
| 1087 | + | ||
| 1088 | + r = 1; | ||
| 1089 | + | ||
| 1090 | +finish: | ||
| 1091 | + if (unset_environment) | ||
| 1092 | + unsetenv("NOTIFY_SOCKET"); | ||
| 1093 | + | ||
| 1094 | + if (fd >= 0) | ||
| 1095 | + close(fd); | ||
| 1096 | + | ||
| 1097 | + return r; | ||
| 1098 | +} | ||
| 1099 | + | ||
| 1100 | +int netsnmp_sd_notifyf(int unset_environment, const char *format, ...) { | ||
| 1101 | + va_list ap; | ||
| 1102 | + char *p = NULL; | ||
| 1103 | + int r; | ||
| 1104 | + | ||
| 1105 | + va_start(ap, format); | ||
| 1106 | + r = vasprintf(&p, format, ap); | ||
| 1107 | + va_end(ap); | ||
| 1108 | + | ||
| 1109 | + if (r < 0 || !p) | ||
| 1110 | + return -ENOMEM; | ||
| 1111 | + | ||
| 1112 | + r = netsnmp_sd_notify(unset_environment, p); | ||
| 1113 | + free(p); | ||
| 1114 | + | ||
| 1115 | + return r; | ||
| 1116 | +} | ||
| 1117 | + | ||
| 1118 | +int netsnmp_sd_booted(void) { | ||
| 1119 | + struct stat a, b; | ||
| 1120 | + | ||
| 1121 | + /* We simply test whether the systemd cgroup hierarchy is | ||
| 1122 | + * mounted */ | ||
| 1123 | + | ||
| 1124 | + if (lstat("/sys/fs/cgroup", &a) < 0) | ||
| 1125 | + return 0; | ||
| 1126 | + | ||
| 1127 | + if (lstat("/sys/fs/cgroup/systemd", &b) < 0) | ||
| 1128 | + return 0; | ||
| 1129 | + | ||
| 1130 | + return a.st_dev != b.st_dev; | ||
| 1131 | +} | ||
| 1132 | + | ||
| 1133 | +/* End of original sd-daemon.c from systemd sources */ | ||
| 1134 | + | ||
| 1135 | +int | ||
| 1136 | +netsnmp_sd_find_inet_socket(int family, int type, int listening, int port) | ||
| 1137 | +{ | ||
| 1138 | + int count, fd; | ||
| 1139 | + | ||
| 1140 | + count = netsnmp_sd_listen_fds(0); | ||
| 1141 | + if (count <= 0) { | ||
| 1142 | + DEBUGMSGTL(("systemd:find_inet_socket", "No LISTEN_FDS found.\n")); | ||
| 1143 | + return 0; | ||
| 1144 | + } | ||
| 1145 | + DEBUGMSGTL(("systemd:find_inet_socket", "LISTEN_FDS reports %d sockets.\n", | ||
| 1146 | + count)); | ||
| 1147 | + | ||
| 1148 | + for (fd = 3; fd < 3+count; fd++) { | ||
| 1149 | + int rc = netsnmp_sd_is_socket_inet(fd, family, type, listening, port); | ||
| 1150 | + if (rc < 0) | ||
| 1151 | + DEBUGMSGTL(("systemd:find_inet_socket", | ||
| 1152 | + "sd_is_socket_inet error: %d\n", rc)); | ||
| 1153 | + if (rc > 0) { | ||
| 1154 | + DEBUGMSGTL(("systemd:find_inet_socket", | ||
| 1155 | + "Found the socket in LISTEN_FDS\n")); | ||
| 1156 | + return fd; | ||
| 1157 | + } | ||
| 1158 | + } | ||
| 1159 | + DEBUGMSGTL(("systemd:find_inet_socket", "Socket not found in LISTEN_FDS\n")); | ||
| 1160 | + return 0; | ||
| 1161 | +} | ||
| 1162 | + | ||
| 1163 | +int | ||
| 1164 | +netsnmp_sd_find_unix_socket(int type, int listening, const char *path) | ||
| 1165 | +{ | ||
| 1166 | + int count, fd; | ||
| 1167 | + | ||
| 1168 | + count = netsnmp_sd_listen_fds(0); | ||
| 1169 | + if (count <= 0) { | ||
| 1170 | + DEBUGMSGTL(("systemd:find_unix_socket", "No LISTEN_FDS found.\n")); | ||
| 1171 | + return 0; | ||
| 1172 | + } | ||
| 1173 | + DEBUGMSGTL(("systemd:find_unix_socket", "LISTEN_FDS reports %d sockets.\n", | ||
| 1174 | + count)); | ||
| 1175 | + | ||
| 1176 | + for (fd = 3; fd < 3+count; fd++) { | ||
| 1177 | + int rc = netsnmp_sd_is_socket_unix(fd, type, listening, path, 0); | ||
| 1178 | + if (rc < 0) | ||
| 1179 | + DEBUGMSGTL(("systemd:find_unix_socket", | ||
| 1180 | + "netsnmp_sd_is_socket_unix error: %d\n", rc)); | ||
| 1181 | + if (rc > 0) { | ||
| 1182 | + DEBUGMSGTL(("systemd:find_unix_socket", | ||
| 1183 | + "Found the socket in LISTEN_FDS\n")); | ||
| 1184 | + return fd; | ||
| 1185 | + } | ||
| 1186 | + } | ||
| 1187 | + DEBUGMSGTL(("systemd:find_unix_socket", "Socket not found in LISTEN_FDS\n")); | ||
| 1188 | + return 0; | ||
| 1189 | +} | ||
| 1190 | + | ||
| 1191 | +#endif /* ! NETSNMP_NO_SYSTEMD */ | ||
| 1192 | diff --git a/snmplib/transports/snmpTCPDomain.c b/snmplib/transports/snmpTCPDomain.c | ||
| 1193 | index 7feb028..a41b926 100644 | ||
| 1194 | --- a/snmplib/transports/snmpTCPDomain.c | ||
| 1195 | +++ b/snmplib/transports/snmpTCPDomain.c | ||
| 1196 | @@ -43,6 +43,10 @@ | ||
| 1197 | #include <net-snmp/library/snmpTCPBaseDomain.h> | ||
| 1198 | #include <net-snmp/library/tools.h> | ||
| 1199 | |||
| 1200 | +#ifndef NETSNMP_NO_SYSTEMD | ||
| 1201 | +#include <net-snmp/library/sd-daemon.h> | ||
| 1202 | +#endif | ||
| 1203 | + | ||
| 1204 | /* | ||
| 1205 | * needs to be in sync with the definitions in snmplib/snmpUDPDomain.c | ||
| 1206 | * and perl/agent/agent.xs | ||
| 1207 | @@ -149,6 +153,7 @@ netsnmp_tcp_transport(struct sockaddr_in *addr, int local) | ||
| 1208 | netsnmp_transport *t = NULL; | ||
| 1209 | netsnmp_udp_addr_pair *addr_pair = NULL; | ||
| 1210 | int rc = 0; | ||
| 1211 | + int socket_initialized = 0; | ||
| 1212 | |||
| 1213 | #ifdef NETSNMP_NO_LISTEN_SUPPORT | ||
| 1214 | if (local) | ||
| 1215 | @@ -178,7 +183,19 @@ netsnmp_tcp_transport(struct sockaddr_in *addr, int local) | ||
| 1216 | t->domain_length = | ||
| 1217 | sizeof(netsnmp_snmpTCPDomain) / sizeof(netsnmp_snmpTCPDomain[0]); | ||
| 1218 | |||
| 1219 | - t->sock = socket(PF_INET, SOCK_STREAM, 0); | ||
| 1220 | +#ifndef NETSNMP_NO_SYSTEMD | ||
| 1221 | + /* | ||
| 1222 | + * Maybe the socket was already provided by systemd... | ||
| 1223 | + */ | ||
| 1224 | + if (local) { | ||
| 1225 | + t->sock = netsnmp_sd_find_inet_socket(PF_INET, SOCK_STREAM, 1, | ||
| 1226 | + ntohs(addr->sin_port)); | ||
| 1227 | + if (t->sock) | ||
| 1228 | + socket_initialized = 1; | ||
| 1229 | + } | ||
| 1230 | +#endif | ||
| 1231 | + if (!socket_initialized) | ||
| 1232 | + t->sock = socket(PF_INET, SOCK_STREAM, 0); | ||
| 1233 | if (t->sock < 0) { | ||
| 1234 | netsnmp_transport_free(t); | ||
| 1235 | return NULL; | ||
| 1236 | @@ -215,11 +232,13 @@ netsnmp_tcp_transport(struct sockaddr_in *addr, int local) | ||
| 1237 | setsockopt(t->sock, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, | ||
| 1238 | sizeof(opt)); | ||
| 1239 | |||
| 1240 | - rc = bind(t->sock, (struct sockaddr *)addr, sizeof(struct sockaddr)); | ||
| 1241 | - if (rc != 0) { | ||
| 1242 | - netsnmp_socketbase_close(t); | ||
| 1243 | - netsnmp_transport_free(t); | ||
| 1244 | - return NULL; | ||
| 1245 | + if (!socket_initialized) { | ||
| 1246 | + rc = bind(t->sock, (struct sockaddr *)addr, sizeof(struct sockaddr)); | ||
| 1247 | + if (rc != 0) { | ||
| 1248 | + netsnmp_socketbase_close(t); | ||
| 1249 | + netsnmp_transport_free(t); | ||
| 1250 | + return NULL; | ||
| 1251 | + } | ||
| 1252 | } | ||
| 1253 | |||
| 1254 | /* | ||
| 1255 | @@ -236,11 +255,13 @@ netsnmp_tcp_transport(struct sockaddr_in *addr, int local) | ||
| 1256 | * Now sit here and wait for connections to arrive. | ||
| 1257 | */ | ||
| 1258 | |||
| 1259 | - rc = listen(t->sock, NETSNMP_STREAM_QUEUE_LEN); | ||
| 1260 | - if (rc != 0) { | ||
| 1261 | - netsnmp_socketbase_close(t); | ||
| 1262 | - netsnmp_transport_free(t); | ||
| 1263 | - return NULL; | ||
| 1264 | + if (!socket_initialized) { | ||
| 1265 | + rc = listen(t->sock, NETSNMP_STREAM_QUEUE_LEN); | ||
| 1266 | + if (rc != 0) { | ||
| 1267 | + netsnmp_socketbase_close(t); | ||
| 1268 | + netsnmp_transport_free(t); | ||
| 1269 | + return NULL; | ||
| 1270 | + } | ||
| 1271 | } | ||
| 1272 | |||
| 1273 | /* | ||
| 1274 | diff --git a/snmplib/transports/snmpTCPIPv6Domain.c b/snmplib/transports/snmpTCPIPv6Domain.c | ||
| 1275 | index d2e0a2d..22de6d4 100644 | ||
| 1276 | --- a/snmplib/transports/snmpTCPIPv6Domain.c | ||
| 1277 | +++ b/snmplib/transports/snmpTCPIPv6Domain.c | ||
| 1278 | @@ -49,6 +49,10 @@ | ||
| 1279 | #include <net-snmp/library/snmpTCPBaseDomain.h> | ||
| 1280 | #include <net-snmp/library/tools.h> | ||
| 1281 | |||
| 1282 | +#ifndef NETSNMP_NO_SYSTEMD | ||
| 1283 | +#include <net-snmp/library/sd-daemon.h> | ||
| 1284 | +#endif | ||
| 1285 | + | ||
| 1286 | #include "inet_ntop.h" | ||
| 1287 | |||
| 1288 | oid netsnmp_TCPIPv6Domain[] = { TRANSPORT_DOMAIN_TCP_IPV6 }; | ||
| 1289 | @@ -140,6 +144,8 @@ netsnmp_tcp6_transport(struct sockaddr_in6 *addr, int local) | ||
| 1290 | { | ||
| 1291 | netsnmp_transport *t = NULL; | ||
| 1292 | int rc = 0; | ||
| 1293 | + char *str = NULL; | ||
| 1294 | + int socket_initialized = 0; | ||
| 1295 | |||
| 1296 | #ifdef NETSNMP_NO_LISTEN_SUPPORT | ||
| 1297 | if (local) | ||
| 1298 | @@ -174,7 +180,19 @@ netsnmp_tcp6_transport(struct sockaddr_in6 *addr, int local) | ||
| 1299 | t->domain = netsnmp_TCPIPv6Domain; | ||
| 1300 | t->domain_length = sizeof(netsnmp_TCPIPv6Domain) / sizeof(oid); | ||
| 1301 | |||
| 1302 | - t->sock = socket(PF_INET6, SOCK_STREAM, 0); | ||
| 1303 | +#ifndef NETSNMP_NO_SYSTEMD | ||
| 1304 | + /* | ||
| 1305 | + * Maybe the socket was already provided by systemd... | ||
| 1306 | + */ | ||
| 1307 | + if (local) { | ||
| 1308 | + t->sock = netsnmp_sd_find_inet_socket(PF_INET6, SOCK_STREAM, 1, | ||
| 1309 | + ntohs(addr->sin6_port)); | ||
| 1310 | + if (t->sock) | ||
| 1311 | + socket_initialized = 1; | ||
| 1312 | + } | ||
| 1313 | +#endif | ||
| 1314 | + if (!socket_initialized) | ||
| 1315 | + t->sock = socket(PF_INET6, SOCK_STREAM, 0); | ||
| 1316 | if (t->sock < 0) { | ||
| 1317 | netsnmp_transport_free(t); | ||
| 1318 | return NULL; | ||
| 1319 | @@ -220,12 +238,14 @@ netsnmp_tcp6_transport(struct sockaddr_in6 *addr, int local) | ||
| 1320 | |||
| 1321 | setsockopt(t->sock, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, sizeof(opt)); | ||
| 1322 | |||
| 1323 | - rc = bind(t->sock, (struct sockaddr *) addr, | ||
| 1324 | - sizeof(struct sockaddr_in6)); | ||
| 1325 | - if (rc != 0) { | ||
| 1326 | - netsnmp_socketbase_close(t); | ||
| 1327 | - netsnmp_transport_free(t); | ||
| 1328 | - return NULL; | ||
| 1329 | + if (!socket_initialized) { | ||
| 1330 | + rc = bind(t->sock, (struct sockaddr *) addr, | ||
| 1331 | + sizeof(struct sockaddr_in6)); | ||
| 1332 | + if (rc != 0) { | ||
| 1333 | + netsnmp_socketbase_close(t); | ||
| 1334 | + netsnmp_transport_free(t); | ||
| 1335 | + return NULL; | ||
| 1336 | + } | ||
| 1337 | } | ||
| 1338 | |||
| 1339 | /* | ||
| 1340 | @@ -242,11 +262,13 @@ netsnmp_tcp6_transport(struct sockaddr_in6 *addr, int local) | ||
| 1341 | * Now sit here and wait for connections to arrive. | ||
| 1342 | */ | ||
| 1343 | |||
| 1344 | - rc = listen(t->sock, NETSNMP_STREAM_QUEUE_LEN); | ||
| 1345 | - if (rc != 0) { | ||
| 1346 | - netsnmp_socketbase_close(t); | ||
| 1347 | - netsnmp_transport_free(t); | ||
| 1348 | - return NULL; | ||
| 1349 | + if (!socket_initialized) { | ||
| 1350 | + rc = listen(t->sock, NETSNMP_STREAM_QUEUE_LEN); | ||
| 1351 | + if (rc != 0) { | ||
| 1352 | + netsnmp_socketbase_close(t); | ||
| 1353 | + netsnmp_transport_free(t); | ||
| 1354 | + return NULL; | ||
| 1355 | + } | ||
| 1356 | } | ||
| 1357 | |||
| 1358 | /* | ||
| 1359 | diff --git a/snmplib/transports/snmpUDPIPv4BaseDomain.c b/snmplib/transports/snmpUDPIPv4BaseDomain.c | ||
| 1360 | index 8c0fb05..00e5bbc 100644 | ||
| 1361 | --- a/snmplib/transports/snmpUDPIPv4BaseDomain.c | ||
| 1362 | +++ b/snmplib/transports/snmpUDPIPv4BaseDomain.c | ||
| 1363 | @@ -40,6 +40,10 @@ | ||
| 1364 | |||
| 1365 | #include <net-snmp/library/snmpSocketBaseDomain.h> | ||
| 1366 | |||
| 1367 | +#ifndef NETSNMP_NO_SYSTEMD | ||
| 1368 | +#include <net-snmp/library/sd-daemon.h> | ||
| 1369 | +#endif | ||
| 1370 | + | ||
| 1371 | #if defined(HAVE_IP_PKTINFO) || defined(HAVE_IP_RECVDSTADDR) | ||
| 1372 | int netsnmp_udpipv4_recvfrom(int s, void *buf, int len, struct sockaddr *from, | ||
| 1373 | socklen_t *fromlen, struct sockaddr *dstip, | ||
| 1374 | @@ -64,6 +68,7 @@ netsnmp_udpipv4base_transport(struct sockaddr_in *addr, int local) | ||
| 1375 | char *client_socket = NULL; | ||
| 1376 | netsnmp_indexed_addr_pair addr_pair; | ||
| 1377 | socklen_t local_addr_len; | ||
| 1378 | + int socket_initialized = 0; | ||
| 1379 | |||
| 1380 | #ifdef NETSNMP_NO_LISTEN_SUPPORT | ||
| 1381 | if (local) | ||
| 1382 | @@ -88,7 +93,19 @@ netsnmp_udpipv4base_transport(struct sockaddr_in *addr, int local) | ||
| 1383 | free(str); | ||
| 1384 | } | ||
| 1385 | |||
| 1386 | - t->sock = socket(PF_INET, SOCK_DGRAM, 0); | ||
| 1387 | +#ifndef NETSNMP_NO_SYSTEMD | ||
| 1388 | + /* | ||
| 1389 | + * Maybe the socket was already provided by systemd... | ||
| 1390 | + */ | ||
| 1391 | + if (local) { | ||
| 1392 | + t->sock = netsnmp_sd_find_inet_socket(PF_INET, SOCK_DGRAM, -1, | ||
| 1393 | + ntohs(addr->sin_port)); | ||
| 1394 | + if (t->sock) | ||
| 1395 | + socket_initialized = 1; | ||
| 1396 | + } | ||
| 1397 | +#endif | ||
| 1398 | + if (!socket_initialized) | ||
| 1399 | + t->sock = socket(PF_INET, SOCK_DGRAM, 0); | ||
| 1400 | DEBUGMSGTL(("UDPBase", "openned socket %d as local=%d\n", t->sock, local)); | ||
| 1401 | if (t->sock < 0) { | ||
| 1402 | netsnmp_transport_free(t); | ||
| 1403 | @@ -151,12 +168,14 @@ netsnmp_udpipv4base_transport(struct sockaddr_in *addr, int local) | ||
| 1404 | } | ||
| 1405 | } | ||
| 1406 | #endif /* !defined(WIN32) */ | ||
| 1407 | - rc = bind(t->sock, (struct sockaddr *) addr, | ||
| 1408 | - sizeof(struct sockaddr)); | ||
| 1409 | - if (rc != 0) { | ||
| 1410 | - netsnmp_socketbase_close(t); | ||
| 1411 | - netsnmp_transport_free(t); | ||
| 1412 | - return NULL; | ||
| 1413 | + if (!socket_initialized) { | ||
| 1414 | + rc = bind(t->sock, (struct sockaddr *) addr, | ||
| 1415 | + sizeof(struct sockaddr)); | ||
| 1416 | + if (rc != 0) { | ||
| 1417 | + netsnmp_socketbase_close(t); | ||
| 1418 | + netsnmp_transport_free(t); | ||
| 1419 | + return NULL; | ||
| 1420 | + } | ||
| 1421 | } | ||
| 1422 | t->data = NULL; | ||
| 1423 | t->data_length = 0; | ||
| 1424 | diff --git a/snmplib/transports/snmpUDPIPv6Domain.c b/snmplib/transports/snmpUDPIPv6Domain.c | ||
| 1425 | index 18de876..fd2ced4 100644 | ||
| 1426 | --- a/snmplib/transports/snmpUDPIPv6Domain.c | ||
| 1427 | +++ b/snmplib/transports/snmpUDPIPv6Domain.c | ||
| 1428 | @@ -67,6 +67,10 @@ static const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; | ||
| 1429 | #include <net-snmp/library/snmpSocketBaseDomain.h> | ||
| 1430 | #include <net-snmp/library/tools.h> | ||
| 1431 | |||
| 1432 | +#ifndef NETSNMP_NO_SYSTEMD | ||
| 1433 | +#include <net-snmp/library/sd-daemon.h> | ||
| 1434 | +#endif | ||
| 1435 | + | ||
| 1436 | #include "inet_ntop.h" | ||
| 1437 | #include "inet_pton.h" | ||
| 1438 | |||
| 1439 | @@ -190,6 +194,8 @@ netsnmp_udp6_transport(struct sockaddr_in6 *addr, int local) | ||
| 1440 | { | ||
| 1441 | netsnmp_transport *t = NULL; | ||
| 1442 | int rc = 0; | ||
| 1443 | + char *str = NULL; | ||
| 1444 | + int socket_initialized = 0; | ||
| 1445 | |||
| 1446 | #ifdef NETSNMP_NO_LISTEN_SUPPORT | ||
| 1447 | if (local) | ||
| 1448 | @@ -217,7 +223,19 @@ netsnmp_udp6_transport(struct sockaddr_in6 *addr, int local) | ||
| 1449 | t->domain_length = | ||
| 1450 | sizeof(netsnmp_UDPIPv6Domain) / sizeof(netsnmp_UDPIPv6Domain[0]); | ||
| 1451 | |||
| 1452 | - t->sock = socket(PF_INET6, SOCK_DGRAM, 0); | ||
| 1453 | +#ifndef NETSNMP_NO_SYSTEMD | ||
| 1454 | + /* | ||
| 1455 | + * Maybe the socket was already provided by systemd... | ||
| 1456 | + */ | ||
| 1457 | + if (local) { | ||
| 1458 | + t->sock = netsnmp_sd_find_inet_socket(PF_INET6, SOCK_DGRAM, -1, | ||
| 1459 | + ntohs(addr->sin6_port)); | ||
| 1460 | + if (t->sock) | ||
| 1461 | + socket_initialized = 1; | ||
| 1462 | + } | ||
| 1463 | +#endif | ||
| 1464 | + if (!socket_initialized) | ||
| 1465 | + t->sock = socket(PF_INET6, SOCK_DGRAM, 0); | ||
| 1466 | if (t->sock < 0) { | ||
| 1467 | netsnmp_transport_free(t); | ||
| 1468 | return NULL; | ||
| 1469 | @@ -243,12 +261,14 @@ netsnmp_udp6_transport(struct sockaddr_in6 *addr, int local) | ||
| 1470 | } | ||
| 1471 | #endif | ||
| 1472 | |||
| 1473 | - rc = bind(t->sock, (struct sockaddr *) addr, | ||
| 1474 | - sizeof(struct sockaddr_in6)); | ||
| 1475 | - if (rc != 0) { | ||
| 1476 | - netsnmp_socketbase_close(t); | ||
| 1477 | - netsnmp_transport_free(t); | ||
| 1478 | - return NULL; | ||
| 1479 | + if (!socket_initialized) { | ||
| 1480 | + rc = bind(t->sock, (struct sockaddr *) addr, | ||
| 1481 | + sizeof(struct sockaddr_in6)); | ||
| 1482 | + if (rc != 0) { | ||
| 1483 | + netsnmp_socketbase_close(t); | ||
| 1484 | + netsnmp_transport_free(t); | ||
| 1485 | + return NULL; | ||
| 1486 | + } | ||
| 1487 | } | ||
| 1488 | t->local = (unsigned char*)malloc(18); | ||
| 1489 | if (t->local == NULL) { | ||
| 1490 | diff --git a/snmplib/transports/snmpUnixDomain.c b/snmplib/transports/snmpUnixDomain.c | ||
| 1491 | index 47dffc1..8f34c37 100644 | ||
| 1492 | --- a/snmplib/transports/snmpUnixDomain.c | ||
| 1493 | +++ b/snmplib/transports/snmpUnixDomain.c | ||
| 1494 | @@ -37,6 +37,10 @@ | ||
| 1495 | #include <net-snmp/library/system.h> /* mkdirhier */ | ||
| 1496 | #include <net-snmp/library/tools.h> | ||
| 1497 | |||
| 1498 | +#ifndef NETSNMP_NO_SYSTEMD | ||
| 1499 | +#include <net-snmp/library/sd-daemon.h> | ||
| 1500 | +#endif | ||
| 1501 | + | ||
| 1502 | netsnmp_feature_child_of(transport_unix_socket_all, transport_all) | ||
| 1503 | netsnmp_feature_child_of(unix_socket_paths, transport_unix_socket_all) | ||
| 1504 | |||
| 1505 | @@ -295,6 +299,8 @@ netsnmp_unix_transport(struct sockaddr_un *addr, int local) | ||
| 1506 | netsnmp_transport *t = NULL; | ||
| 1507 | sockaddr_un_pair *sup = NULL; | ||
| 1508 | int rc = 0; | ||
| 1509 | + char *string = NULL; | ||
| 1510 | + int socket_initialized = 0; | ||
| 1511 | |||
| 1512 | #ifdef NETSNMP_NO_LISTEN_SUPPORT | ||
| 1513 | /* SPECIAL CIRCUMSTANCE: We still want AgentX to be able to operate, | ||
| 1514 | @@ -333,7 +339,18 @@ netsnmp_unix_transport(struct sockaddr_un *addr, int local) | ||
| 1515 | t->data_length = sizeof(sockaddr_un_pair); | ||
| 1516 | sup = (sockaddr_un_pair *) t->data; | ||
| 1517 | |||
| 1518 | - t->sock = socket(PF_UNIX, SOCK_STREAM, 0); | ||
| 1519 | +#ifndef NETSNMP_NO_SYSTEMD | ||
| 1520 | + /* | ||
| 1521 | + * Maybe the socket was already provided by systemd... | ||
| 1522 | + */ | ||
| 1523 | + if (local) { | ||
| 1524 | + t->sock = netsnmp_sd_find_unix_socket(SOCK_STREAM, 1, addr->sun_path); | ||
| 1525 | + if (t->sock) | ||
| 1526 | + socket_initialized = 1; | ||
| 1527 | + } | ||
| 1528 | +#endif | ||
| 1529 | + if (!socket_initialized) | ||
| 1530 | + t->sock = socket(PF_UNIX, SOCK_STREAM, 0); | ||
| 1531 | if (t->sock < 0) { | ||
| 1532 | netsnmp_transport_free(t); | ||
| 1533 | return NULL; | ||
| 1534 | @@ -357,25 +374,26 @@ netsnmp_unix_transport(struct sockaddr_un *addr, int local) | ||
| 1535 | |||
| 1536 | t->flags |= NETSNMP_TRANSPORT_FLAG_LISTEN; | ||
| 1537 | |||
| 1538 | - unlink(addr->sun_path); | ||
| 1539 | - rc = bind(t->sock, (struct sockaddr *) addr, SUN_LEN(addr)); | ||
| 1540 | - | ||
| 1541 | - if (rc != 0 && errno == ENOENT && create_path) { | ||
| 1542 | - rc = mkdirhier(addr->sun_path, create_mode, 1); | ||
| 1543 | + if (!socket_initialized) { | ||
| 1544 | + unlink(addr->sun_path); | ||
| 1545 | + rc = bind(t->sock, (struct sockaddr *) addr, SUN_LEN(addr)); | ||
| 1546 | + if (rc != 0 && errno == ENOENT && create_path) { | ||
| 1547 | + rc = mkdirhier(addr->sun_path, create_mode, 1); | ||
| 1548 | + if (rc != 0) { | ||
| 1549 | + netsnmp_unix_close(t); | ||
| 1550 | + netsnmp_transport_free(t); | ||
| 1551 | + return NULL; | ||
| 1552 | + } | ||
| 1553 | + rc = bind(t->sock, (struct sockaddr *) addr, SUN_LEN(addr)); | ||
| 1554 | + } | ||
| 1555 | if (rc != 0) { | ||
| 1556 | + DEBUGMSGTL(("netsnmp_unix_transport", | ||
| 1557 | + "couldn't bind \"%s\", errno %d (%s)\n", | ||
| 1558 | + addr->sun_path, errno, strerror(errno))); | ||
| 1559 | netsnmp_unix_close(t); | ||
| 1560 | netsnmp_transport_free(t); | ||
| 1561 | return NULL; | ||
| 1562 | } | ||
| 1563 | - rc = bind(t->sock, (struct sockaddr *) addr, SUN_LEN(addr)); | ||
| 1564 | - } | ||
| 1565 | - if (rc != 0) { | ||
| 1566 | - DEBUGMSGTL(("netsnmp_unix_transport", | ||
| 1567 | - "couldn't bind \"%s\", errno %d (%s)\n", | ||
| 1568 | - addr->sun_path, errno, strerror(errno))); | ||
| 1569 | - netsnmp_unix_close(t); | ||
| 1570 | - netsnmp_transport_free(t); | ||
| 1571 | - return NULL; | ||
| 1572 | } | ||
| 1573 | |||
| 1574 | /* | ||
| 1575 | @@ -391,14 +409,16 @@ netsnmp_unix_transport(struct sockaddr_un *addr, int local) | ||
| 1576 | * Now sit here and listen for connections to arrive. | ||
| 1577 | */ | ||
| 1578 | |||
| 1579 | - rc = listen(t->sock, NETSNMP_STREAM_QUEUE_LEN); | ||
| 1580 | - if (rc != 0) { | ||
| 1581 | - DEBUGMSGTL(("netsnmp_unix_transport", | ||
| 1582 | - "couldn't listen to \"%s\", errno %d (%s)\n", | ||
| 1583 | - addr->sun_path, errno, strerror(errno))); | ||
| 1584 | - netsnmp_unix_close(t); | ||
| 1585 | - netsnmp_transport_free(t); | ||
| 1586 | - return NULL; | ||
| 1587 | + if (!socket_initialized) { | ||
| 1588 | + rc = listen(t->sock, NETSNMP_STREAM_QUEUE_LEN); | ||
| 1589 | + if (rc != 0) { | ||
| 1590 | + DEBUGMSGTL(("netsnmp_unix_transport", | ||
| 1591 | + "couldn't listen to \"%s\", errno %d (%s)\n", | ||
| 1592 | + addr->sun_path, errno, strerror(errno))); | ||
| 1593 | + netsnmp_unix_close(t); | ||
| 1594 | + netsnmp_transport_free(t); | ||
| 1595 | + return NULL; | ||
| 1596 | + } | ||
| 1597 | } | ||
| 1598 | |||
| 1599 | } else { | ||
| 1600 | diff --git a/win32/libsnmp/Makefile.in b/win32/libsnmp/Makefile.in | ||
| 1601 | index 98d83c8..b228d20 100644 | ||
| 1602 | --- a/win32/libsnmp/Makefile.in | ||
| 1603 | +++ b/win32/libsnmp/Makefile.in | ||
| 1604 | @@ -42,6 +42,7 @@ LIB32_OBJS= \ | ||
| 1605 | "$(INTDIR)\read_config.obj" \ | ||
| 1606 | "$(INTDIR)\readdir.obj" \ | ||
| 1607 | "$(INTDIR)\scapi.obj" \ | ||
| 1608 | + "$(INTDIR)\sd-daemon.obj" \ | ||
| 1609 | "$(INTDIR)\snmp-tc.obj" \ | ||
| 1610 | "$(INTDIR)\snmp.obj" \ | ||
| 1611 | "$(INTDIR)\snmpCallbackDomain.obj" \ | ||
| 1612 | @@ -138,6 +139,11 @@ SOURCE=..\..\snmplib\asn1.c | ||
| 1613 | "$(INTDIR)\asn1.obj" : $(SOURCE) "$(INTDIR)" | ||
| 1614 | $(CPP) $(CPP_PROJ) $(SOURCE) | ||
| 1615 | |||
| 1616 | +SOURCE=..\..\snmplib\sd-daemon.c | ||
| 1617 | + | ||
| 1618 | +"$(INTDIR)\sd-daemon.obj" : $(SOURCE) "$(INTDIR)" | ||
| 1619 | + $(CPP) $(CPP_PROJ) $(SOURCE) | ||
| 1620 | + | ||
| 1621 | |||
| 1622 | SOURCE=..\..\snmplib\callback.c | ||
| 1623 | |||
| 1624 | diff --git a/win32/net-snmp/net-snmp-config.h b/win32/net-snmp/net-snmp-config.h | ||
| 1625 | index 1608563..7aec547 100644 | ||
| 1626 | --- a/win32/net-snmp/net-snmp-config.h | ||
| 1627 | +++ b/win32/net-snmp/net-snmp-config.h | ||
| 1628 | @@ -1717,6 +1717,8 @@ enum { | ||
| 1629 | #define DMALLOC_FUNC_CHECK | ||
| 1630 | #endif | ||
| 1631 | |||
| 1632 | ++#define NETSNMP_NO_SYSTEMD | ||
| 1633 | ++ | ||
| 1634 | /* #undef NETSNMP_ENABLE_LOCAL_SMUX */ | ||
| 1635 | |||
| 1636 | /* define if agentx transport is to use domain sockets only */ | ||
| 1637 | diff --git a/win32/net-snmp/net-snmp-config.h.in b/win32/net-snmp/net-snmp-config.h.in | ||
| 1638 | index 9693730..96ec3d9 100644 | ||
| 1639 | --- a/win32/net-snmp/net-snmp-config.h.in | ||
| 1640 | +++ b/win32/net-snmp/net-snmp-config.h.in | ||
| 1641 | @@ -1717,6 +1717,8 @@ enum { | ||
| 1642 | #define DMALLOC_FUNC_CHECK | ||
| 1643 | #endif | ||
| 1644 | |||
| 1645 | +#define NETSNMP_NO_SYSTEMD | ||
| 1646 | + | ||
| 1647 | /* #undef NETSNMP_ENABLE_LOCAL_SMUX */ | ||
| 1648 | |||
| 1649 | /* define if agentx transport is to use domain sockets only */ | ||
| 1650 | -- | ||
| 1651 | 1.8.4.2 | ||
| 1652 | |||
