]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
[Bug 2905] DNS lookups broken.
authorJuergen Perlinger <perlinger@ntp.org>
Sat, 5 Dec 2015 12:44:57 +0000 (13:44 +0100)
committerJuergen Perlinger <perlinger@ntp.org>
Sat, 5 Dec 2015 12:44:57 +0000 (13:44 +0100)
  - added limits to stack consumption, fixed some return code handling

bk: 5662dc49tnEwGqj3N24AXOtpduPGbw

ChangeLog
libntp/work_thread.c
ntpd/ntpd.c

index a787d4306b28c0d33452f9df3df4f4183874717b..4999e0996964e2e2cae3248b37231a1a653d2e71 100644 (file)
--- 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
index 49e90c16c3266dd70b984035b428b37d4f9df25c..576f1f33f273da161cfe74d56c00cc19ec2be079 100644 (file)
 
 #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];
index 931ac344f4d61f890b81d6860e228944e882a84c..cb4c949fe18440152530a83a1b69d3dd53fc9d8a 100644 (file)
@@ -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);
        }