From 7cad1983dafd36623ea7ba2fcaaa22705c64e009 Mon Sep 17 00:00:00 2001 From: rofl0r Date: Sun, 15 Oct 2017 17:53:50 +0100 Subject: [PATCH] set a reasonable stacksize for threads to prevent huge waste with GLIBC GLIBC defaults to giving new threads an insanely huge 8MB stack each, which causes unnecessary memory waste. we set it to a conservative 64KB, even though we need less than 1KB ourselves, to give sufficient leeway to buffers used by libc itself when calling out to some of its functions. --- src/pixiewps.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/pixiewps.c b/src/pixiewps.c index ceb0239..eb20a7f 100644 --- a/src/pixiewps.c +++ b/src/pixiewps.c @@ -19,6 +19,8 @@ * along with this program. If not, see . */ +#define _POSIX_C_SOURCE 200809L + #include #include #include @@ -26,6 +28,7 @@ #include #include #include +#include #include #include @@ -121,6 +124,10 @@ static void *crack_thread(void *arg) { return 0; } +static size_t getminstacksize(size_t minimum) { + return (minimum < PTHREAD_STACK_MIN) ? PTHREAD_STACK_MIN : minimum; +} + static void init_crack_jobs(struct global *wps) { job_control.jobs = wps->jobs; job_control.end = wps->end; @@ -138,10 +145,15 @@ static void init_crack_jobs(struct global *wps) { job_control.randr_enonce[i] |= wps->e_nonce[j++]; } job_control.crack_jobs = malloc(wps->jobs * sizeof (struct job_control)); + size_t stacksize = getminstacksize(64*1024); time_t curr = wps->start; for(i = 0; i < wps->jobs; i++) { job_control.crack_jobs[i].start = curr; - pthread_create(&job_control.crack_jobs[i].thr, NULL, crack_thread, &job_control.crack_jobs[i]); + pthread_attr_t attr; + int attr_ok = pthread_attr_init(&attr) == 0 ; + if(attr_ok) pthread_attr_setstacksize(&attr, stacksize); + pthread_create(&job_control.crack_jobs[i].thr, &attr, crack_thread, &job_control.crack_jobs[i]); + if(attr_ok) pthread_attr_destroy(&attr); curr -= SECS_PER_JOB_BLOCK; } }