Add a listen-backlog directive
authorFabian Keil <fk@fabiankeil.de>
Mon, 26 Jun 2017 12:13:52 +0000 (12:13 +0000)
committerFabian Keil <fk@fabiankeil.de>
Mon, 26 Jun 2017 12:13:52 +0000 (12:13 +0000)
Which specifies the backlog value passed to listen().

Sponsored by: Robert Klemme

jbsockets.c
jbsockets.h
jcc.c
loadcfg.c
project.h

index 765aafb..0783441 100644 (file)
@@ -1,4 +1,4 @@
-const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.146 2017/06/26 12:11:12 fabiankeil Exp $";
+const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.147 2017/06/26 12:12:55 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/jbsockets.c,v $
@@ -116,8 +116,6 @@ const char jbsockets_h_rcs[] = JBSOCKETS_H_VERSION;
  */
 #define MAX_DNS_RETRIES 10
 
-#define MAX_LISTEN_BACKLOG 128
-
 #ifdef HAVE_RFC2553
 static jb_socket rfc2553_connect_to(const char *host, int portnum, struct client_state *csp);
 #else
@@ -890,14 +888,15 @@ void drain_and_close_socket(jb_socket fd)
  * Parameters  :
  *          1  :  hostnam = TCP/IP address to bind/listen to
  *          2  :  portnum = port to listen on
- *          3  :  pfd = pointer used to return file descriptor.
+ *          3  :  backlog = Listen backlog
+ *          4  :  pfd = pointer used to return file descriptor.
  *
  * Returns     :  if success, returns 0 and sets *pfd.
  *                if failure, returns -3 if address is in use,
  *                                    -2 if address unresolvable,
  *                                    -1 otherwise
  *********************************************************************/
-int bind_port(const char *hostnam, int portnum, jb_socket *pfd)
+int bind_port(const char *hostnam, int portnum, int backlog, jb_socket *pfd)
 {
 #ifdef HAVE_RFC2553
    struct addrinfo hints;
@@ -1072,7 +1071,7 @@ int bind_port(const char *hostnam, int portnum, jb_socket *pfd)
    }
 #endif /* ndef HAVE_RFC2553 */
 
-   while (listen(fd, MAX_LISTEN_BACKLOG) == -1)
+   while (listen(fd, backlog) == -1)
    {
       if (errno != EINTR)
       {
index 3259fa2..d8cbd18 100644 (file)
@@ -1,6 +1,6 @@
 #ifndef JBSOCKETS_H_INCLUDED
 #define JBSOCKETS_H_INCLUDED
-#define JBSOCKETS_H_VERSION "$Id: jbsockets.h,v 1.24 2014/06/02 06:22:20 fabiankeil Exp $"
+#define JBSOCKETS_H_VERSION "$Id: jbsockets.h,v 1.25 2017/06/04 14:37:05 fabiankeil Exp $"
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/jbsockets.h,v $
@@ -53,7 +53,7 @@ extern int data_is_available(jb_socket fd, int seconds_to_wait);
 extern void close_socket(jb_socket fd);
 extern void drain_and_close_socket(jb_socket fd);
 
-extern int bind_port(const char *hostnam, int portnum, jb_socket *pfd);
+extern int bind_port(const char *hostnam, int portnum, int backlog, jb_socket *pfd);
 extern int accept_connection(struct client_state * csp, jb_socket fds[]);
 extern void get_host_information(jb_socket afd, char **ip_address, char **port, char **hostname);
 
diff --git a/jcc.c b/jcc.c
index 734e717..1026672 100644 (file)
--- a/jcc.c
+++ b/jcc.c
@@ -1,4 +1,4 @@
-const char jcc_rcs[] = "$Id: jcc.c,v 1.462 2017/06/26 12:10:31 fabiankeil Exp $";
+const char jcc_rcs[] = "$Id: jcc.c,v 1.463 2017/06/26 12:12:55 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/jcc.c,v $
@@ -162,7 +162,7 @@ static void serve(struct client_state *csp);
 static void usage(const char *myname);
 #endif
 static void initialize_mutexes(void);
-static jb_socket bind_port_helper(const char *haddr, int hport);
+static jb_socket bind_port_helper(const char *haddr, int hport, int backlog);
 static void bind_ports_helper(struct configuration_spec *config, jb_socket sockets[]);
 static void close_ports_helper(jb_socket sockets[]);
 static void listen_loop(void);
@@ -4120,16 +4120,17 @@ int main(int argc, char **argv)
  *          1  :  haddr = Host address to bind to. Use NULL to bind to
  *                        INADDR_ANY.
  *          2  :  hport = Specifies port to bind to.
+ *          3  :  backlog = Listen backlog.
  *
  * Returns     :  Port that was opened.
  *
  *********************************************************************/
-static jb_socket bind_port_helper(const char *haddr, int hport)
+static jb_socket bind_port_helper(const char *haddr, int hport, int backlog)
 {
    int result;
    jb_socket bfd;
 
-   result = bind_port(haddr, hport, &bfd);
+   result = bind_port(haddr, hport, backlog, &bfd);
 
    if (result < 0)
    {
@@ -4209,7 +4210,8 @@ static void bind_ports_helper(struct configuration_spec * config,
    {
       if (config->hport[i])
       {
-         sockets[i] = bind_port_helper(config->haddr[i], config->hport[i]);
+         sockets[i] = bind_port_helper(config->haddr[i],
+            config->hport[i], config->listen_backlog);
 #if defined(FEATURE_ACCEPT_FILTER) && defined(SO_ACCEPTFILTER)
          if (config->enable_accept_filter && sockets[i] != JB_INVALID_SOCKET)
          {
index 6c0f464..cf6fa9e 100644 (file)
--- a/loadcfg.c
+++ b/loadcfg.c
@@ -1,4 +1,4 @@
-const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.163 2017/06/26 12:09:56 fabiankeil Exp $";
+const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.164 2017/06/26 12:11:13 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/loadcfg.c,v $
@@ -96,6 +96,11 @@ const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.163 2017/06/26 12:09:56 fabiankei
  */
 #define DEFAULT_KEEP_ALIVE_TIMEOUT 180
 
+/*
+ * Default backlog passed to listen().
+ */
+#define DEFAULT_LISTEN_BACKLOG 128
+
 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
 
 #ifdef FEATURE_TOGGLE
@@ -160,6 +165,7 @@ static struct file_list *current_configfile = NULL;
 #define hash_hostname                      10308071U /* "hostname" */
 #define hash_keep_alive_timeout          3878599515U /* "keep-alive-timeout" */
 #define hash_listen_address              1255650842U /* "listen-address" */
+#define hash_listen_backlog              1255655735U /* "listen-backlog" */
 #define hash_logdir                          422889U /* "logdir" */
 #define hash_logfile                        2114766U /* "logfile" */
 #define hash_max_client_connections      3595884446U /* "max-client-connections" */
@@ -616,6 +622,7 @@ struct configuration_spec * load_config(void)
 #if defined(FEATURE_ACCEPT_FILTER) && defined(SO_ACCEPTFILTER)
    config->enable_accept_filter      = 0;
 #endif
+   config->listen_backlog            = DEFAULT_LISTEN_BACKLOG;
    config->trusted_cgi_referrer      = NULL;
    /*
     * 128 client sockets ought to be enough for everybody who can't
@@ -1366,6 +1373,18 @@ struct configuration_spec * load_config(void)
             config->haddr[i] = strdup_or_die(arg);
             break;
 
+/* *************************************************************************
+ * listen-backlog n
+ * *************************************************************************/
+         case hash_listen_backlog :
+            /*
+             * We don't enfore an upper or lower limit because on
+             * many platforms all values are valid and negative
+             * number mean "use the highest value allowed".
+             */
+            config->listen_backlog = parse_numeric_value(cmd, arg);
+            break;
+
 /* *************************************************************************
  * logdir directory-name
  * *************************************************************************/
index da8a3fe..aa642d3 100644 (file)
--- a/project.h
+++ b/project.h
@@ -1,7 +1,7 @@
 #ifndef PROJECT_H_INCLUDED
 #define PROJECT_H_INCLUDED
 /** Version string. */
-#define PROJECT_H_VERSION "$Id: project.h,v 1.221 2017/05/29 10:02:11 fabiankeil Exp $"
+#define PROJECT_H_VERSION "$Id: project.h,v 1.222 2017/06/26 12:11:13 fabiankeil Exp $"
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/project.h,v $
@@ -1354,6 +1354,9 @@ struct configuration_spec
    /** Use accf_http(4) if available */
    int enable_accept_filter;
 
+   /** Backlog passed to listen() */
+   int listen_backlog;
+
 #ifdef FEATURE_TRUST
 
    /** The file name of the trust file. */