Create threads with a 32 KB stack rather than what happens to be the default.
authorLinus Nordberg <linus@nordberg.se>
Mon, 26 Aug 2013 08:35:12 +0000 (10:35 +0200)
committerLinus Nordberg <linus@nordberg.se>
Mon, 26 Aug 2013 09:49:59 +0000 (11:49 +0200)
On Linux, the default stack size is typically 8 MB.

Patch by Fabian Mauchle.

dtls.c
radsecproxy.c
radsecproxy.h
tcp.c
tls.c
udp.c

diff --git a/dtls.c b/dtls.c
index 0965ea8..2586b8f 100644 (file)
--- a/dtls.c
+++ b/dtls.c
@@ -305,7 +305,7 @@ void dtlsserverrd(struct client *client) {
 
     debug(DBG_DBG, "dtlsserverrd: starting for %s", addr2string(client->addr));
 
-    if (pthread_create(&dtlsserverwrth, NULL, dtlsserverwr, (void *)client)) {
+    if (pthread_create(&dtlsserverwrth, &pthread_attr, dtlsserverwr, (void *)client)) {
        debug(DBG_ERR, "dtlsserverrd: pthread_create failed");
        return;
     }
@@ -508,7 +508,7 @@ void *udpdtlsserverrd(void *arg) {
 
            if (udp2bio(s, params->sesscache->rbios, cnt)) {
                debug(DBG_DBG, "udpdtlsserverrd: got DTLS in UDP from %s", addr2string((struct sockaddr *)&from));
-               if (!pthread_create(&dtlsserverth, NULL, dtlsservernew, (void *)params)) {
+               if (!pthread_create(&dtlsserverth, &pthread_attr, dtlsservernew, (void *)params)) {
                    pthread_detach(dtlsserverth);
                    cacheexpire(sessioncache, &lastexpiry);
                    continue;
@@ -697,10 +697,10 @@ void initextradtls() {
     }
 
     if (client4_sock >= 0)
-       if (pthread_create(&cl4th, NULL, udpdtlsclientrd, (void *)&client4_sock))
+       if (pthread_create(&cl4th, &pthread_attr, udpdtlsclientrd, (void *)&client4_sock))
            debugx(1, DBG_ERR, "pthread_create failed");
     if (client6_sock >= 0)
-       if (pthread_create(&cl6th, NULL, udpdtlsclientrd, (void *)&client6_sock))
+       if (pthread_create(&cl6th, &pthread_attr, udpdtlsclientrd, (void *)&client6_sock))
            debugx(1, DBG_ERR, "pthread_create failed");
 }
 #else
index b70ea45..d2f72b0 100644 (file)
@@ -1803,7 +1803,7 @@ void *clientwr(void *arg) {
 #if defined ENABLE_EXPERIMENTAL_DYNDISC
        server->in_use = 1;
 #endif
-       if (pthread_create(&clientrdth, NULL, conf->pdef->clientconnreader, (void *)server)) {
+       if (pthread_create(&clientrdth, &pthread_attr, conf->pdef->clientconnreader, (void *)server)) {
            debugerrno(errno, DBG_ERR, "clientwr: pthread_create failed");
            goto errexit;
        }
@@ -1962,7 +1962,7 @@ void createlistener(uint8_t type, char *arg) {
         if (!sp)
             debugx(1, DBG_ERR, "malloc failed");
        *sp = s;
-       if (pthread_create(&th, NULL, protodefs[type]->listener, (void *)sp))
+       if (pthread_create(&th, &pthread_attr, protodefs[type]->listener, (void *)sp))
             debugerrnox(errno, DBG_ERR, "pthread_create failed");
        pthread_detach(th);
     }
@@ -2212,7 +2212,7 @@ struct list *createsubrealmservers(struct realm *realm, struct list *srvconfs) {
 #if defined ENABLE_EXPERIMENTAL_DYNDISC
                pthread_mutex_lock(&srvconf->servers->lock);
 #endif
-               if (pthread_create(&clientth, NULL, clientwr, (void *)(srvconf->servers))) {
+               if (pthread_create(&clientth, &pthread_attr, clientwr, (void *)(srvconf->servers))) {
 #if defined ENABLE_EXPERIMENTAL_DYNDISC
                     pthread_mutex_unlock(&srvconf->servers->lock);
 #endif
@@ -3331,6 +3331,11 @@ int radsecproxy_main(int argc, char **argv) {
     debug_init("radsecproxy");
     debug_set_level(DEBUG_LEVEL);
 
+    if (pthread_attr_init(&pthread_attr))
+       debugx(1, DBG_ERR, "pthread_attr_init failed");
+    if (pthread_attr_setstacksize(&pthread_attr, PTHREAD_STACK_SIZE))
+       debugx(1, DBG_ERR, "pthread_attr_setstacksize failed");
+
     for (i = 0; i < RAD_PROTOCOUNT; i++)
        protodefs[i] = protoinits[i](i);
 
@@ -3382,7 +3387,7 @@ int radsecproxy_main(int argc, char **argv) {
     sigaddset(&sigset, SIGHUP);
     sigaddset(&sigset, SIGPIPE);
     pthread_sigmask(SIG_BLOCK, &sigset, NULL);
-    pthread_create(&sigth, NULL, sighandler, NULL);
+    pthread_create(&sigth, &pthread_attr, sighandler, NULL);
 
     for (entry = list_first(srvconfs); entry; entry = list_next(entry)) {
        srvconf = (struct clsrvconf *)entry->data;
@@ -3390,7 +3395,7 @@ int radsecproxy_main(int argc, char **argv) {
            continue;
        if (!addserver(srvconf))
            debugx(1, DBG_ERR, "failed to add server");
-       if (pthread_create(&srvconf->servers->clientth, NULL, clientwr,
+       if (pthread_create(&srvconf->servers->clientth, &pthread_attr, clientwr,
                           (void *)(srvconf->servers)))
            debugx(1, DBG_ERR, "pthread_create failed");
     }
index e0b2696..0b2aebe 100644 (file)
@@ -23,6 +23,7 @@
 #define MAX_CERT_DEPTH 5
 #define STATUS_SERVER_PERIOD 25
 #define IDLE_TIMEOUT 300
+#define PTHREAD_STACK_SIZE 32768
 
 /* 27262 is vendor DANTE Ltd. */
 #define DEFAULT_TTL_ATTR "27262:1"
@@ -241,6 +242,7 @@ int radsrv(struct request *rq);
 void replyh(struct server *server, unsigned char *buf);
 struct addrinfo *resolve_hostport_addrinfo(uint8_t type, char *hostport);
 uint8_t *radattr2ascii(struct tlv *attr);
+pthread_attr_t pthread_attr;
 
 /* Local Variables: */
 /* c-file-style: "stroustrup" */
diff --git a/tcp.c b/tcp.c
index 3b758a9..0ad574c 100644 (file)
--- a/tcp.c
+++ b/tcp.c
@@ -272,7 +272,7 @@ void tcpserverrd(struct client *client) {
 
     debug(DBG_DBG, "tcpserverrd: starting for %s", addr2string(client->addr));
 
-    if (pthread_create(&tcpserverwrth, NULL, tcpserverwr, (void *)client)) {
+    if (pthread_create(&tcpserverwrth, &pthread_attr, tcpserverwr, (void *)client)) {
        debug(DBG_ERR, "tcpserverrd: pthread_create failed");
        return;
     }
@@ -353,7 +353,7 @@ void *tcplistener(void *arg) {
            debug(DBG_WARN, "accept failed");
            continue;
        }
-       if (pthread_create(&tcpserverth, NULL, tcpservernew, (void *)&s)) {
+       if (pthread_create(&tcpserverth, &pthread_attr, tcpservernew, (void *)&s)) {
            debug(DBG_ERR, "tcplistener: pthread_create failed");
            shutdown(s, SHUT_RDWR);
            close(s);
diff --git a/tls.c b/tls.c
index ac33bc9..4197069 100644 (file)
--- a/tls.c
+++ b/tls.c
@@ -335,7 +335,7 @@ void tlsserverrd(struct client *client) {
 
     debug(DBG_DBG, "tlsserverrd: starting for %s", addr2string(client->addr));
 
-    if (pthread_create(&tlsserverwrth, NULL, tlsserverwr, (void *)client)) {
+    if (pthread_create(&tlsserverwrth, &pthread_attr, tlsserverwr, (void *)client)) {
        debug(DBG_ERR, "tlsserverrd: pthread_create failed");
        return;
     }
@@ -456,7 +456,7 @@ void *tlslistener(void *arg) {
            debug(DBG_WARN, "accept failed");
            continue;
        }
-       if (pthread_create(&tlsserverth, NULL, tlsservernew, (void *)&s)) {
+       if (pthread_create(&tlsserverth, &pthread_attr, tlsservernew, (void *)&s)) {
            debug(DBG_ERR, "tlslistener: pthread_create failed");
            shutdown(s, SHUT_RDWR);
            close(s);
diff --git a/udp.c b/udp.c
index efb5f09..ff7ad77 100644 (file)
--- a/udp.c
+++ b/udp.c
@@ -348,15 +348,15 @@ void initextraudp() {
     }
 
     if (client4_sock >= 0)
-       if (pthread_create(&cl4th, NULL, udpclientrd, (void *)&client4_sock))
+       if (pthread_create(&cl4th, &pthread_attr, udpclientrd, (void *)&client4_sock))
            debugx(1, DBG_ERR, "pthread_create failed");
     if (client6_sock >= 0)
-       if (pthread_create(&cl6th, NULL, udpclientrd, (void *)&client6_sock))
+       if (pthread_create(&cl6th, &pthread_attr, udpclientrd, (void *)&client6_sock))
            debugx(1, DBG_ERR, "pthread_create failed");
 
     if (find_clconf_type(handle, NULL)) {
        server_replyq = newqueue();
-       if (pthread_create(&srvth, NULL, udpserverwr, (void *)server_replyq))
+       if (pthread_create(&srvth, &pthread_attr, udpserverwr, (void *)server_replyq))
            debugx(1, DBG_ERR, "pthread_create failed");
     }
 }