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