]> git.puffer.fish Git - mirror/frr.git/commitdiff
lib: use heap for pollfds 648/head
authorQuentin Young <qlyoung@cumulusnetworks.com>
Wed, 31 May 2017 23:21:40 +0000 (23:21 +0000)
committerQuentin Young <qlyoung@cumulusnetworks.com>
Wed, 31 May 2017 23:21:40 +0000 (23:21 +0000)
a bunch of pollfds can cause a stack overflow when using a stack
allocated buffer...silly me...

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
lib/thread.c
lib/thread.h

index 848e39e1aef3b067f0eb52133ab6ecb7e41e7af5..2f15659a18fb4840a12aa0e3010534581123d70f 100644 (file)
@@ -393,6 +393,8 @@ thread_master_create (void)
   rv->handler.pfdcount = 0;
   rv->handler.pfds = XCALLOC (MTYPE_THREAD_MASTER,
                               sizeof (struct pollfd) * rv->handler.pfdsize);
+  rv->handler.copy = XCALLOC (MTYPE_THREAD_MASTER,
+                              sizeof (struct pollfd) * rv->handler.pfdsize);
 
   return rv;
 }
@@ -544,6 +546,7 @@ thread_master_free (struct thread_master *m)
   close (m->io_pipe[1]);
 
   XFREE (MTYPE_THREAD_MASTER, m->handler.pfds);
+  XFREE (MTYPE_THREAD_MASTER, m->handler.copy);
   XFREE (MTYPE_THREAD_MASTER, m);
 
   pthread_mutex_lock (&cpu_record_mtx);
@@ -1231,17 +1234,15 @@ thread_fetch (struct thread_master *m, struct thread *fetch)
           timer_wait = &timer_val;
         }
 
-      /* copy pollfds so we can unlock during blocking calls to poll() */
-      struct pollfd pfds[m->handler.pfdsize];
       unsigned int count = m->handler.pfdcount + m->handler.pfdcountsnmp;
-      memcpy (pfds, m->handler.pfds, count * sizeof (struct pollfd));
+      memcpy (m->handler.copy, m->handler.pfds, count * sizeof (struct pollfd));
 
       pthread_mutex_unlock (&m->mtx);
       {
-        num = fd_poll (m, pfds, m->handler.pfdsize, count, timer_wait);
+        num = fd_poll (m, m->handler.copy, m->handler.pfdsize, count, timer_wait);
       }
       pthread_mutex_lock (&m->mtx);
-      
+
       /* Signals should get quick treatment */
       if (num < 0)
         {
@@ -1263,7 +1264,7 @@ thread_fetch (struct thread_master *m, struct thread *fetch)
       
       /* Got IO, process it */
       if (num > 0)
-        thread_process_io (m, pfds, num, count);
+        thread_process_io (m, m->handler.copy, num, count);
 
 #if 0
       /* If any threads were made ready above (I/O or foreground timer),
index 753aa41ffd131968c284e5ff3538f3e98f77113f..608fb8b8c0c8e8a3b9b04a559e4980f0d8881140 100644 (file)
@@ -53,7 +53,10 @@ struct fd_handler
   nfds_t pfdcountsnmp;
   /* number of pfd that fit in the allocated space of pfds */
   nfds_t pfdsize;
+  /* file descriptors to monitor for i/o */
   struct pollfd *pfds;
+  /* chunk used for temp copy of pollfds */
+  struct pollfd *copy;
 };
 
 /* Master of the theads. */