From: Juergen Perlinger Date: Sat, 5 Dec 2015 12:44:57 +0000 (+0100) Subject: [Bug 2905] DNS lookups broken. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=64c46f2d8ee1b5b4404ed7a0099eac769d36a571;p=thirdparty%2Fntp.git [Bug 2905] DNS lookups broken. - added limits to stack consumption, fixed some return code handling bk: 5662dc49tnEwGqj3N24AXOtpduPGbw --- diff --git a/ChangeLog b/ChangeLog index a787d4306..4999e0996 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,8 @@ * [Sec 2956] small-step/big-step. Close the panic gate earlier. HStenn. * CID 1339955: Free allocated memory in caljulian test. HStenn. * CID 1339962: Explicitly initialize variable in caljulian test. HStenn. +* [Bug 2905] DNS lookups broken. perlinger@ntp.org + - added limits to stack consumption, fixed some return code handling * [Bug 2932] Update leapsecond file info in miscopt.html. CWoodbury, HStenn. * [Bug 2934] tests/ntpd/t-ntp_scanner.c has a magic constant wired in. HMurray * [Bug 2954] Version 4.2.8p4 crashes on startup with sig fault diff --git a/libntp/work_thread.c b/libntp/work_thread.c index 49e90c16c..576f1f33f 100644 --- a/libntp/work_thread.c +++ b/libntp/work_thread.c @@ -25,12 +25,35 @@ #define CHILD_EXIT_REQ ((blocking_pipe_header *)(intptr_t)-1) #define CHILD_GONE_RESP CHILD_EXIT_REQ +/* Queue size increments: + * The request queue grows a bit faster than the response queue -- the + * deamon can push requests and pull results faster on avarage than the + * worker can process requests and push results... If this really pays + * off is debatable. + */ #define WORKITEMS_ALLOC_INC 16 #define RESPONSES_ALLOC_INC 4 +/* Fiddle with min/max stack sizes. 64kB minimum seems to work, so we + * set the maximum to 256kB. If the minimum goes below the + * system-defined minimum stack size, we have to adjust accordingly. + */ #ifndef THREAD_MINSTACKSIZE -#define THREAD_MINSTACKSIZE (64U * 1024) +# define THREAD_MINSTACKSIZE (64U * 1024) +#endif +#if defined(PTHREAD_STACK_MIN) && THREAD_MINSTACKSIZE < PTHREAD_STACK_MIN +# undef THREAD_MINSTACKSIZE +# define THREAD_MINSTACKSIZE PTHREAD_STACK_MIN +#endif + +#ifndef THREAD_MAXSTACKSIZE +# define THREAD_MAXSTACKSIZE (256U * 1024) #endif +#if THREAD_MAXSTACKSIZE < THREAD_MINSTACKSIZE +# undef THREAD_MAXSTACKSIZE +# define THREAD_MAXSTACKSIZE THREAD_MINSTACKSIZE +#endif + #ifdef SYS_WINNT @@ -148,15 +171,19 @@ ensure_workitems_empty_slot( size_t new_alloc; size_t slots_used; + size_t sidx; slots_used = c->head_workitem - c->tail_workitem; if (slots_used >= c->workitems_alloc) { new_alloc = c->workitems_alloc + WORKITEMS_ALLOC_INC; c->workitems = erealloc(c->workitems, new_alloc * each); + for (sidx = c->workitems_alloc; sidx < new_alloc; ++sidx) + c->workitems[sidx] = NULL; c->tail_workitem = 0; c->head_workitem = c->workitems_alloc; c->workitems_alloc = new_alloc; } + INSIST(NULL == c->workitems[c->head_workitem % c->workitems_alloc]); return (0 == slots_used); } @@ -180,15 +207,19 @@ ensure_workresp_empty_slot( size_t new_alloc; size_t slots_used; + size_t sidx; slots_used = c->head_response - c->tail_response; if (slots_used >= c->responses_alloc) { new_alloc = c->responses_alloc + RESPONSES_ALLOC_INC; c->responses = erealloc(c->responses, new_alloc * each); + for (sidx = c->responses_alloc; sidx < new_alloc; ++sidx) + c->responses[sidx] = NULL; c->tail_response = 0; c->head_response = c->responses_alloc; c->responses_alloc = new_alloc; } + INSIST(NULL == c->responses[c->head_response % c->responses_alloc]); return (0 == slots_used); } @@ -478,11 +509,11 @@ start_blocking_thread_internal( # endif pthread_attr_t thr_attr; int rc; - int saved_errno; int pipe_ends[2]; /* read then write */ int is_pipe; int flags; - size_t stacksize; + size_t ostacksize; + size_t nstacksize; sigset_t saved_sig_mask; c->thread_ref = NULL; @@ -522,21 +553,29 @@ start_blocking_thread_internal( pthread_attr_setdetachstate(&thr_attr, PTHREAD_CREATE_DETACHED); #if defined(HAVE_PTHREAD_ATTR_GETSTACKSIZE) && \ defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE) - rc = pthread_attr_getstacksize(&thr_attr, &stacksize); - if (-1 == rc) { + rc = pthread_attr_getstacksize(&thr_attr, &ostacksize); + if (0 != rc) { msyslog(LOG_ERR, - "start_blocking_thread: pthread_attr_getstacksize %m"); - } else if (stacksize < THREAD_MINSTACKSIZE) { - rc = pthread_attr_setstacksize(&thr_attr, - THREAD_MINSTACKSIZE); - if (-1 == rc) + "start_blocking_thread: pthread_attr_getstacksize() -> %s", + strerror(rc)); + } else { + if (ostacksize < THREAD_MINSTACKSIZE) + nstacksize = THREAD_MINSTACKSIZE; + else if (ostacksize > THREAD_MAXSTACKSIZE) + nstacksize = THREAD_MAXSTACKSIZE; + else + nstacksize = ostacksize; + if (nstacksize != ostacksize) + rc = pthread_attr_setstacksize(&thr_attr, nstacksize); + if (0 != rc) msyslog(LOG_ERR, - "start_blocking_thread: pthread_attr_setstacksize(0x%lx -> 0x%lx) %m", - (u_long)stacksize, - (u_long)THREAD_MINSTACKSIZE); + "start_blocking_thread: pthread_attr_setstacksize(0x%lx -> 0x%lx) -> %s", + (u_long)ostacksize, (u_long)nstacksize, + strerror(rc)); } #else - UNUSED_ARG(stacksize); + UNUSED_ARG(nstacksize); + UNUSED_ARG(ostacksize); #endif #if defined(PTHREAD_SCOPE_SYSTEM) && defined(NEED_PTHREAD_SCOPE_SYSTEM) pthread_attr_setscope(&thr_attr, PTHREAD_SCOPE_SYSTEM); @@ -545,12 +584,11 @@ start_blocking_thread_internal( block_thread_signals(&saved_sig_mask); rc = pthread_create(&c->thr_table[0], &thr_attr, &blocking_thread, c); - saved_errno = errno; pthread_sigmask(SIG_SETMASK, &saved_sig_mask, NULL); pthread_attr_destroy(&thr_attr); if (0 != rc) { - errno = saved_errno; - msyslog(LOG_ERR, "pthread_create() blocking child: %m"); + msyslog(LOG_ERR, "start_blocking_thread: pthread_create() -> %s", + strerror(rc)); exit(1); } c->thread_ref = &c->thr_table[0]; diff --git a/ntpd/ntpd.c b/ntpd/ntpd.c index 931ac344f..cb4c949fe 100644 --- a/ntpd/ntpd.c +++ b/ntpd/ntpd.c @@ -298,11 +298,28 @@ my_pthread_warmup_worker( static void my_pthread_warmup(void) { - pthread_t thread; - int rc; + pthread_t thread; + pthread_attr_t thr_attr; + int rc; + + pthread_attr_init(&thr_attr); +#if defined(HAVE_PTHREAD_ATTR_GETSTACKSIZE) && \ + defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE) && \ + defined(PTHREAD_STACK_MIN) + rc = pthread_attr_setstacksize(&thr_attr, PTHREAD_STACK_MIN); + if (0 != rc) + msyslog(LOG_ERR, + "my_pthread_warmup: pthread_attr_setstacksize() -> %s", + strerror(rc)); +#endif rc = pthread_create( - &thread, NULL, my_pthread_warmup_worker, NULL); - if (0 == rc) { + &thread, &thr_attr, my_pthread_warmup_worker, NULL); + pthread_attr_destroy(&thr_attr); + if (0 != rc) { + msyslog(LOG_ERR, + "my_pthread_warmup: pthread_create() -> %s", + strerror(rc)); + } else { pthread_cancel(thread); pthread_join(thread, NULL); }