9a6c41
Bug 1252048 - net-snmp snmpd fork() overhead [fix available]
9a6c41
9a6c41
Backported from:
9a6c41
9a6c41
commit f0e87f4918ffc41e03f707e9670ea422cd154a9b
9a6c41
Author: Bart Van Assche <bvanassche@acm.org>
9a6c41
Date:   Sat Jan 31 12:05:24 2015 +0100
9a6c41
9a6c41
    CHANGES: snmpd: BUG: 2596: Reduce fork() overhead
9a6c41
9a6c41
    Avoid that the close() loop that is executed after a fork() delays
9a6c41
    the pass/extend API on systems with a large maximum number of files
9a6c41
    by reducing the number of iterations of this loop on Linux systems.
9a6c41
9a6c41
    See also http://sourceforge.net/p/net-snmp/bugs/2596.
9a6c41
9a6c41
    Reported-by: andymf <andymf@users.sf.net>
9a6c41
9a6c41
9a6c41
diff -up net-snmp-5.7.2/agent/mibgroup/util_funcs.c.test net-snmp-5.7.2/agent/mibgroup/util_funcs.c
9a6c41
--- net-snmp-5.7.2/agent/mibgroup/util_funcs.c.test	2012-10-10 00:28:58.000000000 +0200
9a6c41
+++ net-snmp-5.7.2/agent/mibgroup/util_funcs.c	2015-08-18 10:15:18.888767023 +0200
9a6c41
@@ -480,8 +480,7 @@ get_exec_pipes(char *cmd, int *fdIn, int
9a6c41
         /*
9a6c41
          * close all non-standard open file descriptors 
9a6c41
          */
9a6c41
-        for (cnt = getdtablesize() - 1; cnt >= 2; --cnt)
9a6c41
-            (void) close(cnt);
9a6c41
+        netsnmp_close_fds(1);
9a6c41
         (void) dup(1);          /* stderr */
9a6c41
 
9a6c41
         for (cnt = 1, cptr1 = cmd, cptr2 = argvs; *cptr1 != 0;
9a6c41
diff -up net-snmp-5.7.2/agent/mibgroup/utilities/execute.c.test net-snmp-5.7.2/agent/mibgroup/utilities/execute.c
9a6c41
--- net-snmp-5.7.2/agent/mibgroup/utilities/execute.c.test	2012-10-10 00:28:58.000000000 +0200
9a6c41
+++ net-snmp-5.7.2/agent/mibgroup/utilities/execute.c	2015-08-18 10:15:18.889767028 +0200
9a6c41
@@ -22,6 +22,9 @@
9a6c41
 #if HAVE_FCNTL_H
9a6c41
 #include <fcntl.h>
9a6c41
 #endif
9a6c41
+#if HAVE_DIRENT_H
9a6c41
+#include <dirent.h>
9a6c41
+#endif
9a6c41
 #if HAVE_SYS_WAIT_H
9a6c41
 #include <sys/wait.h>
9a6c41
 #endif
9a6c41
@@ -207,8 +210,8 @@ run_exec_command( char *command, char *i
9a6c41
         close(opipe[0]);
9a6c41
         close(2);
9a6c41
         dup(1);
9a6c41
-        for (i = getdtablesize()-1; i>2; i--)
9a6c41
-            close(i);
9a6c41
+
9a6c41
+        netsnmp_close_fds(2);
9a6c41
 
9a6c41
         /*
9a6c41
          * Set up the argv array and execute it
9a6c41
@@ -406,3 +409,30 @@ run_exec_command( char *command, char *i
9a6c41
     return run_shell_command( command, input, output, out_len );
9a6c41
 #endif
9a6c41
 }
9a6c41
+
9a6c41
+/**
9a6c41
+ * Close all file descriptors larger than @fd.
9a6c41
+ */
9a6c41
+void netsnmp_close_fds(int fd)
9a6c41
+{
9a6c41
+#if defined(HAVE_FORK)
9a6c41
+    DIR            *dir;
9a6c41
+    struct dirent  *ent;
9a6c41
+    int             i, largest_fd = -1;
9a6c41
+
9a6c41
+    if ((dir = opendir("/proc/self/fd"))) {
9a6c41
+        while ((ent = readdir(dir))) {
9a6c41
+            if (sscanf(ent->d_name, "%d", &i) == 1) {
9a6c41
+                if (i > largest_fd)
9a6c41
+                    largest_fd = i;
9a6c41
+            }
9a6c41
+        }
9a6c41
+        closedir(dir);
9a6c41
+    } else {
9a6c41
+        largest_fd = getdtablesize() - 1;
9a6c41
+    }
9a6c41
+
9a6c41
+    for (i = largest_fd; i > fd && i > 0; i--)
9a6c41
+        close(i);
9a6c41
+#endif
9a6c41
+}
9a6c41
diff -up net-snmp-5.7.2/agent/mibgroup/utilities/execute.h.test net-snmp-5.7.2/agent/mibgroup/utilities/execute.h
9a6c41
--- net-snmp-5.7.2/agent/mibgroup/utilities/execute.h.test	2012-10-10 00:28:58.000000000 +0200
9a6c41
+++ net-snmp-5.7.2/agent/mibgroup/utilities/execute.h	2015-08-18 10:15:18.889767028 +0200
9a6c41
@@ -3,6 +3,7 @@
9a6c41
 
9a6c41
 config_belongs_in(agent_module)
9a6c41
 
9a6c41
+void netsnmp_close_fds(int fd);
9a6c41
 int run_shell_command(char *command, char *input,
9a6c41
                       char *output,  int  *out_len);
9a6c41
 int run_exec_command( char *command, char *input,
9a6c41
diff -up net-snmp-5.7.2/agent/snmpd.c.test net-snmp-5.7.2/agent/snmpd.c
9a6c41
--- net-snmp-5.7.2/agent/snmpd.c.test	2015-08-18 10:15:08.450714809 +0200
9a6c41
+++ net-snmp-5.7.2/agent/snmpd.c	2015-08-18 10:17:31.579430763 +0200
9a6c41
@@ -143,6 +143,7 @@ typedef long    fd_mask;
9a6c41
 #include <net-snmp/agent/agent_module_config.h>
9a6c41
 #include <net-snmp/agent/mib_module_config.h>
9a6c41
 
9a6c41
+#include "utilities/execute.h" /* netsnmp_close_fds() */
9a6c41
 #include "snmpd.h"
9a6c41
 
9a6c41
 #include <net-snmp/agent/mib_modules.h>
9a6c41
@@ -451,7 +452,6 @@ main(int argc, char *argv[])
9a6c41
     FILE           *PID;
9a6c41
 #endif
9a6c41
 
9a6c41
-#ifndef WIN32
9a6c41
 #ifndef NETSNMP_NO_SYSYSTEMD
9a6c41
     /* check if systemd has sockets for us and don't close them */
9a6c41
     prepared_sockets = netsnmp_sd_listen_fds(0);
9a6c41
@@ -462,11 +462,8 @@ main(int argc, char *argv[])
9a6c41
      * inherited from the shell.
9a6c41
      */
9a6c41
     if (!prepared_sockets) {
9a6c41
-        for (i = getdtablesize() - 1; i > 2; --i) {
9a6c41
-            (void) close(i);
9a6c41
-        }
9a6c41
+        netsnmp_close_fds(2);
9a6c41
     }
9a6c41
-#endif /* #WIN32 */
9a6c41
     
9a6c41
     /*
9a6c41
      * register signals ASAP to prevent default action (usually core)
9a6c41
diff -up net-snmp-5.7.2/apps/snmptrapd.c.test net-snmp-5.7.2/apps/snmptrapd.c
9a6c41
--- net-snmp-5.7.2/apps/snmptrapd.c.test	2015-08-18 10:15:08.450714809 +0200
9a6c41
+++ net-snmp-5.7.2/apps/snmptrapd.c	2015-08-18 10:18:15.454650235 +0200
9a6c41
@@ -97,6 +97,7 @@ SOFTWARE.
9a6c41
 #include <net-snmp/net-snmp-includes.h>
9a6c41
 #include <net-snmp/agent/net-snmp-agent-includes.h>
9a6c41
 #include <net-snmp/library/fd_event_manager.h>
9a6c41
+#include "utilities/execute.h" /* netsnmp_close_fds() */
9a6c41
 #include "snmptrapd_handlers.h"
9a6c41
 #include "snmptrapd_log.h"
9a6c41
 #include "snmptrapd_auth.h"
9a6c41
@@ -662,7 +663,6 @@ main(int argc, char *argv[])
9a6c41
     int             prepared_sockets = 0;
9a6c41
 
9a6c41
 
9a6c41
-#ifndef WIN32
9a6c41
 #ifndef NETSNMP_NO_SYSTEMD
9a6c41
     /* check if systemd has sockets for us and don't close them */
9a6c41
     prepared_sockets = netsnmp_sd_listen_fds(0);
9a6c41
@@ -672,11 +672,8 @@ main(int argc, char *argv[])
9a6c41
      * inherited from the shell.
9a6c41
      */
9a6c41
     if (!prepared_sockets) {
9a6c41
-        for (i = getdtablesize() - 1; i > 2; --i) {
9a6c41
-            (void) close(i);
9a6c41
-        }
9a6c41
+        netsnmp_close_fds(2);
9a6c41
     }
9a6c41
-#endif /* #WIN32 */
9a6c41
     
9a6c41
 #ifdef SIGTERM
9a6c41
     signal(SIGTERM, term_handler);
9a6c41
@@ -1382,18 +1379,6 @@ trapd_update_config(void)
9a6c41
     read_configs();
9a6c41
 }
9a6c41
 
9a6c41
-
9a6c41
-#if !defined(HAVE_GETDTABLESIZE) && !defined(WIN32)
9a6c41
-#include <sys/resource.h>
9a6c41
-int
9a6c41
-getdtablesize(void)
9a6c41
-{
9a6c41
-    struct rlimit   rl;
9a6c41
-    getrlimit(RLIMIT_NOFILE, &rl);
9a6c41
-    return (rl.rlim_cur);
9a6c41
-}
9a6c41
-#endif
9a6c41
-
9a6c41
 /*
9a6c41
  * Windows Service Related functions 
9a6c41
  */