More descriptive error messages.
[freeradius.git] / src / main / threads.c
1 /*
2  * threads.c    request threading support
3  *
4  * Version:     $Id$
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * Copyright 2000,2006  The FreeRADIUS server project
21  * Copyright 2000  Alan DeKok <aland@ox.org>
22  */
23
24 #include <freeradius-devel/ident.h>
25 RCSID("$Id$")
26
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/rad_assert.h>
29
30 /*
31  *      Other OS's have sem_init, OS X doesn't.
32  */
33 #ifdef HAVE_SEMAPHORE_H
34 #include <semaphore.h>
35 #endif
36
37 #ifdef DARWIN
38 #include <mach/task.h>
39 #include <mach/semaphore.h>
40
41 #undef sem_t
42 #define sem_t semaphore_t
43 #undef sem_init
44 #define sem_init(s,p,c) semaphore_create(mach_task_self(),s,SYNC_POLICY_FIFO,c)
45 #undef sem_wait
46 #define sem_wait(s) semaphore_wait(*s)
47 #undef sem_post
48 #define sem_post(s) semaphore_signal(*s)
49 #endif
50
51 #ifdef HAVE_SYS_WAIT_H
52 #include <sys/wait.h>
53 #endif
54
55 #ifdef HAVE_PTHREAD_H
56
57 #ifdef HAVE_OPENSSL_CRYPTO_H
58 #include <openssl/crypto.h>
59 #endif
60 #ifdef HAVE_OPENSSL_ERR_H
61 #include <openssl/err.h>
62 #endif
63 #ifdef HAVE_OPENSSL_EVP_H
64 #include <openssl/evp.h>
65 #endif
66
67 #define SEMAPHORE_LOCKED        (0)
68 #define SEMAPHORE_UNLOCKED      (1)
69
70 #define THREAD_RUNNING          (1)
71 #define THREAD_CANCELLED        (2)
72 #define THREAD_EXITED           (3)
73
74 #define NUM_FIFOS               RAD_LISTEN_MAX
75
76
77 /*
78  *  A data structure which contains the information about
79  *  the current thread.
80  *
81  *  pthread_id     pthread id
82  *  thread_num     server thread number, 1...number of threads
83  *  semaphore     used to block the thread until a request comes in
84  *  status        is the thread running or exited?
85  *  request_count the number of requests that this thread has handled
86  *  timestamp     when the thread started executing.
87  */
88 typedef struct THREAD_HANDLE {
89         struct THREAD_HANDLE *prev;
90         struct THREAD_HANDLE *next;
91         pthread_t            pthread_id;
92         int                  thread_num;
93         int                  status;
94         unsigned int         request_count;
95         time_t               timestamp;
96         REQUEST              *request;
97 } THREAD_HANDLE;
98
99 /*
100  *      For the request queue.
101  */
102 typedef struct request_queue_t {
103         REQUEST           *request;
104         RAD_REQUEST_FUNP  fun;
105 } request_queue_t;
106
107 typedef struct thread_fork_t {
108         pid_t           pid;
109         int             status;
110         int             exited;
111 } thread_fork_t;
112
113
114 /*
115  *      A data structure to manage the thread pool.  There's no real
116  *      need for a data structure, but it makes things conceptually
117  *      easier.
118  */
119 typedef struct THREAD_POOL {
120         THREAD_HANDLE *head;
121         THREAD_HANDLE *tail;
122
123         int total_threads;
124         int active_threads;     /* protected by queue_mutex */
125         int max_thread_num;
126         int start_threads;
127         int max_threads;
128         int min_spare_threads;
129         int max_spare_threads;
130         unsigned int max_requests_per_thread;
131         unsigned long request_count;
132         time_t time_last_spawned;
133         int cleanup_delay;
134         int spawn_flag;
135
136 #ifdef WNOHANG
137         pthread_mutex_t wait_mutex;
138         fr_hash_table_t *waiters;
139 #endif
140
141         /*
142          *      All threads wait on this semaphore, for requests
143          *      to enter the queue.
144          */
145         sem_t           semaphore;
146
147         /*
148          *      To ensure only one thread at a time touches the queue.
149          */
150         pthread_mutex_t queue_mutex;
151
152         int             max_queue_size;
153         int             num_queued;
154         fr_fifo_t       *fifo[NUM_FIFOS];
155 } THREAD_POOL;
156
157 static THREAD_POOL thread_pool;
158 static int pool_initialized = FALSE;
159 static time_t last_cleaned = 0;
160 static time_t almost_now = 0;
161
162 static void thread_pool_manage(time_t now);
163
164 /*
165  *      A mapping of configuration file names to internal integers
166  */
167 static const CONF_PARSER thread_config[] = {
168         { "start_servers",           PW_TYPE_INTEGER, 0, &thread_pool.start_threads,           "5" },
169         { "max_servers",             PW_TYPE_INTEGER, 0, &thread_pool.max_threads,             "32" },
170         { "min_spare_servers",       PW_TYPE_INTEGER, 0, &thread_pool.min_spare_threads,       "3" },
171         { "max_spare_servers",       PW_TYPE_INTEGER, 0, &thread_pool.max_spare_threads,       "10" },
172         { "max_requests_per_server", PW_TYPE_INTEGER, 0, &thread_pool.max_requests_per_thread, "0" },
173         { "cleanup_delay",           PW_TYPE_INTEGER, 0, &thread_pool.cleanup_delay,           "5" },
174         { "max_queue_size",          PW_TYPE_INTEGER, 0, &thread_pool.max_queue_size,           "65536" },
175         { NULL, -1, 0, NULL, NULL }
176 };
177
178
179 #ifdef HAVE_OPENSSL_CRYPTO_H
180
181 /*
182  *      If we're linking against OpenSSL, then it is the
183  *      duty of the application, if it is multithreaded,
184  *      to provide OpenSSL with appropriate thread id
185  *      and mutex locking functions
186  *
187  *      Note: this only implements static callbacks.
188  *      OpenSSL does not use dynamic locking callbacks
189  *      right now, but may in the futiure, so we will have
190  *      to add them at some point.
191  */
192
193 static pthread_mutex_t *ssl_mutexes = NULL;
194
195 static unsigned long ssl_id_function(void)
196 {
197         return (unsigned long) pthread_self();
198 }
199
200 static void ssl_locking_function(int mode, int n, const char *file, int line)
201 {
202         file = file;            /* -Wunused */
203         line = line;            /* -Wunused */
204
205         if (mode & CRYPTO_LOCK) {
206                 pthread_mutex_lock(&(ssl_mutexes[n]));
207         } else {
208                 pthread_mutex_unlock(&(ssl_mutexes[n]));
209         }
210 }
211
212 static int setup_ssl_mutexes(void)
213 {
214         int i;
215
216 #ifdef HAVE_OPENSSL_EVP_H
217         /*
218          *      Enable all ciphers and digests.
219          */
220         OpenSSL_add_all_algorithms();
221 #endif
222
223         ssl_mutexes = rad_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
224         if (!ssl_mutexes) {
225                 radlog(L_ERR, "Error allocating memory for SSL mutexes!");
226                 return 0;
227         }
228
229         for (i = 0; i < CRYPTO_num_locks(); i++) {
230                 pthread_mutex_init(&(ssl_mutexes[i]), NULL);
231         }
232
233         CRYPTO_set_id_callback(ssl_id_function);
234         CRYPTO_set_locking_callback(ssl_locking_function);
235
236         return 1;
237 }
238 #endif
239
240 #ifdef WNOHANG
241 /*
242  *      We don't want to catch SIGCHLD for a host of reasons.
243  *
244  *      - exec_wait means that someone, somewhere, somewhen, will
245  *      call waitpid(), and catch the child.
246  *
247  *      - SIGCHLD is delivered to a random thread, not the one that
248  *      forked.
249  *
250  *      - if another thread catches the child, we have to coordinate
251  *      with the thread doing the waiting.
252  *
253  *      - if we don't waitpid() for non-wait children, they'll be zombies,
254  *      and will hang around forever.
255  *
256  */
257 static void reap_children(void)
258 {
259         pid_t pid;
260         int status;
261         thread_fork_t mytf, *tf;
262
263
264         pthread_mutex_lock(&thread_pool.wait_mutex);
265
266         do {
267         retry:
268                 pid = waitpid(0, &status, WNOHANG);
269                 if (pid <= 0) break;
270
271                 mytf.pid = pid;
272                 tf = fr_hash_table_finddata(thread_pool.waiters, &mytf);
273                 if (!tf) goto retry;
274
275                 tf->status = status;
276                 tf->exited = 1;
277         } while (fr_hash_table_num_elements(thread_pool.waiters) > 0);
278
279         pthread_mutex_unlock(&thread_pool.wait_mutex);
280 }
281 #else
282 #define reap_children()
283 #endif /* WNOHANG */
284
285 /*
286  *      Add a request to the list of waiting requests.
287  *      This function gets called ONLY from the main handler thread...
288  *
289  *      This function should never fail.
290  */
291 static int request_enqueue(REQUEST *request, RAD_REQUEST_FUNP fun)
292 {
293         request_queue_t *entry;
294
295         pthread_mutex_lock(&thread_pool.queue_mutex);
296
297         thread_pool.request_count++;
298
299         if (thread_pool.num_queued >= thread_pool.max_queue_size) {
300                 pthread_mutex_unlock(&thread_pool.queue_mutex);
301
302                 /*
303                  *      Mark the request as done.
304                  */
305                 radlog(L_ERR, "Something is blocking the server.  There are %d packets in the queue, waiting to be processed.  Ignoring the new request.", thread_pool.max_queue_size);
306                 request->child_state = REQUEST_DONE;
307                 return 0;
308         }
309         request->child_state = REQUEST_QUEUED;
310         request->component = "<queue>";
311
312         entry = rad_malloc(sizeof(*entry));
313         entry->request = request;
314         entry->fun = fun;
315
316         /*
317          *      Push the request onto the appropriate fifo for that
318          */
319         if (!fr_fifo_push(thread_pool.fifo[request->priority],
320                             entry)) {
321                 pthread_mutex_unlock(&thread_pool.queue_mutex);
322                 radlog(L_ERR, "!!! ERROR !!! Failed inserting request %d into the queue", request->number);
323                 request->child_state = REQUEST_DONE;
324                 return 0;
325         }
326
327         thread_pool.num_queued++;
328
329         pthread_mutex_unlock(&thread_pool.queue_mutex);
330
331         /*
332          *      There's one more request in the queue.
333          *
334          *      Note that we're not touching the queue any more, so
335          *      the semaphore post is outside of the mutex.  This also
336          *      means that when the thread wakes up and tries to lock
337          *      the mutex, it will be unlocked, and there won't be
338          *      contention.
339          */
340         sem_post(&thread_pool.semaphore);
341
342         return 1;
343 }
344
345 /*
346  *      Remove a request from the queue.
347  */
348 static int request_dequeue(REQUEST **request, RAD_REQUEST_FUNP *fun)
349 {
350         int blocked;
351         RAD_LISTEN_TYPE i, start;
352         request_queue_t *entry;
353
354         reap_children();
355
356         pthread_mutex_lock(&thread_pool.queue_mutex);
357
358         /*
359          *      Clear old requests from all queues.
360          *
361          *      We only do one pass over the queue, in order to
362          *      amortize the work across the child threads.  Since we
363          *      do N checks for one request de-queued, the old
364          *      requests will be quickly cleared.
365          */
366         for (i = 0; i < RAD_LISTEN_MAX; i++) {
367                 entry = fr_fifo_peek(thread_pool.fifo[i]);
368                 if (!entry ||
369                     (entry->request->master_state != REQUEST_STOP_PROCESSING)) {
370                         continue;
371 }
372                 /*
373                  *      This entry was marked to be stopped.  Acknowledge it.
374                  */
375                 entry = fr_fifo_pop(thread_pool.fifo[i]);
376                 rad_assert(entry != NULL);
377                 entry->request->child_state = REQUEST_DONE;
378                 thread_pool.num_queued--;
379                 free(entry);
380                 entry = NULL;
381         }
382
383         start = 0;
384  retry:
385         /*
386          *      Pop results from the top of the queue
387          */
388         for (i = start; i < RAD_LISTEN_MAX; i++) {
389                 entry = fr_fifo_pop(thread_pool.fifo[i]);
390                 if (entry) {
391                         start = i;
392                         break;
393                 }
394         }
395
396         if (!entry) {
397                 pthread_mutex_unlock(&thread_pool.queue_mutex);
398                 *request = NULL;
399                 *fun = NULL;
400                 return 0;
401         }
402
403         rad_assert(thread_pool.num_queued > 0);
404         thread_pool.num_queued--;
405         *request = entry->request;
406         *fun = entry->fun;
407         free(entry);
408         entry = NULL;
409
410         rad_assert(*request != NULL);
411         rad_assert((*request)->magic == REQUEST_MAGIC);
412         rad_assert(*fun != NULL);
413
414         (*request)->component = "";
415
416         /*
417          *      If the request has sat in the queue for too long,
418          *      kill it.
419          *
420          *      The main clean-up code can't delete the request from
421          *      the queue, and therefore won't clean it up until we
422          *      have acknowledged it as "done".
423          */
424         if ((*request)->master_state == REQUEST_STOP_PROCESSING) {
425                 (*request)->child_state = REQUEST_DONE;
426                 goto retry;
427         }
428
429         /*
430          *      Produce messages for people who have 10 million rows
431          *      in a database, without indexes.
432          */
433         rad_assert(almost_now != 0);
434         blocked = almost_now - (*request)->timestamp;
435         if (blocked < 5) {
436                 blocked = 0;
437         } else {
438                 static time_t last_complained = 0;
439                 
440                 if (last_complained != almost_now) {
441                         last_complained = almost_now;
442                 } else {
443                         blocked = 0;
444                 }
445         }
446
447         /*
448          *      The thread is currently processing a request.
449          */
450         thread_pool.active_threads++;
451
452         pthread_mutex_unlock(&thread_pool.queue_mutex);
453
454         if (blocked) {
455                 radlog(L_ERR, "Request %u has been waiting in the processing queue for %d seconds.  Check that all databases are running properly!",
456                        (*request)->number, blocked);
457         }
458
459         return 1;
460 }
461
462
463 /*
464  *      The main thread handler for requests.
465  *
466  *      Wait on the semaphore until we have it, and process the request.
467  */
468 static void *request_handler_thread(void *arg)
469 {
470         RAD_REQUEST_FUNP  fun;
471         THREAD_HANDLE     *self = (THREAD_HANDLE *) arg;
472
473         /*
474          *      Loop forever, until told to exit.
475          */
476         do {
477                 /*
478                  *      Wait to be signalled.
479                  */
480                 DEBUG2("Thread %d waiting to be assigned a request",
481                        self->thread_num);
482         re_wait:
483                 if (sem_wait(&thread_pool.semaphore) != 0) {
484                         /*
485                          *      Interrupted system call.  Go back to
486                          *      waiting, but DON'T print out any more
487                          *      text.
488                          */
489                         if (errno == EINTR) {
490                                 DEBUG2("Re-wait %d", self->thread_num);
491                                 goto re_wait;
492                         }
493                         radlog(L_ERR, "Thread %d failed waiting for semaphore: %s: Exiting\n",
494                                self->thread_num, strerror(errno));
495                         break;
496                 }
497
498                 DEBUG2("Thread %d got semaphore", self->thread_num);
499
500 #ifdef HAVE_OPENSSL_ERR_H
501                 /*
502                  *      Clear the error queue for the current thread.
503                  */
504                 ERR_clear_error ();
505 #endif
506
507                 /*
508                  *      Try to grab a request from the queue.
509                  *
510                  *      It may be empty, in which case we fail
511                  *      gracefully.
512                  */
513                 if (!request_dequeue(&self->request, &fun)) continue;
514
515                 self->request->child_pid = self->pthread_id;
516                 self->request_count++;
517
518                 DEBUG2("Thread %d handling request %d, (%d handled so far)",
519                        self->thread_num, self->request->number,
520                        self->request_count);
521
522                 radius_handle_request(self->request, fun);
523
524                 /*
525                  *      Update the active threads.
526                  */
527                 pthread_mutex_lock(&thread_pool.queue_mutex);
528                 rad_assert(thread_pool.active_threads > 0);
529                 thread_pool.active_threads--;
530                 pthread_mutex_unlock(&thread_pool.queue_mutex);
531         } while (self->status != THREAD_CANCELLED);
532
533         DEBUG2("Thread %d exiting...", self->thread_num);
534
535 #ifdef HAVE_OPENSSL_ERR_H
536         /*
537          *      If we linked with OpenSSL, the application
538          *      must remove the thread's error queue before
539          *      exiting to prevent memory leaks.
540          */
541         ERR_remove_state(0);
542 #endif
543
544         /*
545          *  Do this as the LAST thing before exiting.
546          */
547         self->request = NULL;
548         self->status = THREAD_EXITED;
549
550         return NULL;
551 }
552
553 /*
554  *      Take a THREAD_HANDLE, delete it from the thread pool and
555  *      free its resources.
556  *
557  *      This function is called ONLY from the main server thread,
558  *      ONLY after the thread has exited.
559  */
560 static void delete_thread(THREAD_HANDLE *handle)
561 {
562         THREAD_HANDLE *prev;
563         THREAD_HANDLE *next;
564
565         rad_assert(handle->request == NULL);
566
567         DEBUG2("Deleting thread %d", handle->thread_num);
568
569         prev = handle->prev;
570         next = handle->next;
571         rad_assert(thread_pool.total_threads > 0);
572         thread_pool.total_threads--;
573
574         /*
575          *      Remove the handle from the list.
576          */
577         if (prev == NULL) {
578                 rad_assert(thread_pool.head == handle);
579                 thread_pool.head = next;
580         } else {
581                 prev->next = next;
582         }
583
584         if (next == NULL) {
585                 rad_assert(thread_pool.tail == handle);
586                 thread_pool.tail = prev;
587         } else {
588                 next->prev = prev;
589         }
590
591         /*
592          *      Free the handle, now that it's no longer referencable.
593          */
594         free(handle);
595 }
596
597
598 /*
599  *      Spawn a new thread, and place it in the thread pool.
600  *
601  *      The thread is started initially in the blocked state, waiting
602  *      for the semaphore.
603  */
604 static THREAD_HANDLE *spawn_thread(time_t now)
605 {
606         int rcode;
607         THREAD_HANDLE *handle;
608         pthread_attr_t attr;
609
610         /*
611          *      Ensure that we don't spawn too many threads.
612          */
613         if (thread_pool.total_threads >= thread_pool.max_threads) {
614                 DEBUG2("Thread spawn failed.  Maximum number of threads (%d) already running.", thread_pool.max_threads);
615                 return NULL;
616         }
617
618         /*
619          *      Allocate a new thread handle.
620          */
621         handle = (THREAD_HANDLE *) rad_malloc(sizeof(THREAD_HANDLE));
622         memset(handle, 0, sizeof(THREAD_HANDLE));
623         handle->prev = NULL;
624         handle->next = NULL;
625         handle->thread_num = thread_pool.max_thread_num++;
626         handle->request_count = 0;
627         handle->status = THREAD_RUNNING;
628         handle->timestamp = time(NULL);
629
630         /*
631          *      Initialize the thread's attributes to detached.
632          *
633          *      We could call pthread_detach() later, but if the thread
634          *      exits between the create & detach calls, it will need to
635          *      be joined, which will never happen.
636          */
637         pthread_attr_init(&attr);
638         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
639
640         /*
641          *      Create the thread detached, so that it cleans up it's
642          *      own memory when it exits.
643          *
644          *      Note that the function returns non-zero on error, NOT
645          *      -1.  The return code is the error, and errno isn't set.
646          */
647         rcode = pthread_create(&handle->pthread_id, &attr,
648                         request_handler_thread, handle);
649         if (rcode != 0) {
650                 radlog(L_ERR, "Thread create failed: %s",
651                        strerror(rcode));
652                 return NULL;
653         }
654         pthread_attr_destroy(&attr);
655
656         /*
657          *      One more thread to go into the list.
658          */
659         thread_pool.total_threads++;
660         DEBUG2("Thread spawned new child %d. Total threads in pool: %d",
661                         handle->thread_num, thread_pool.total_threads);
662
663         /*
664          *      Add the thread handle to the tail of the thread pool list.
665          */
666         if (thread_pool.tail) {
667                 thread_pool.tail->next = handle;
668                 handle->prev = thread_pool.tail;
669                 thread_pool.tail = handle;
670         } else {
671                 rad_assert(thread_pool.head == NULL);
672                 thread_pool.head = thread_pool.tail = handle;
673         }
674
675         /*
676          *      Update the time we last spawned a thread.
677          */
678         thread_pool.time_last_spawned = now;
679
680         /*
681          *      And return the new handle to the caller.
682          */
683         return handle;
684 }
685
686 /*
687  *      Temporary function to prevent server from executing a SIGHUP
688  *      until all threads are finished handling requests.  This returns
689  *      the number of active threads to 'radiusd.c'.
690  */
691 int total_active_threads(void)
692 {
693         /*
694          *      We don't acquire the mutex, so this is just an estimate.
695          *      We can't return with the lock held, so there's no point
696          *      in getting the guaranteed correct value; by the time
697          *      the caller sees it, it can be wrong again.
698          */
699         return thread_pool.active_threads;
700 }
701
702
703 #ifdef WNOHANG
704 static uint32_t pid_hash(const void *data)
705 {
706         const thread_fork_t *tf = data;
707
708         return fr_hash(&tf->pid, sizeof(tf->pid));
709 }
710
711 static int pid_cmp(const void *one, const void *two)
712 {
713         const thread_fork_t *a = one;
714         const thread_fork_t *b = two;
715
716         return (a->pid - b->pid);
717 }
718 #endif
719
720 /*
721  *      Allocate the thread pool, and seed it with an initial number
722  *      of threads.
723  *
724  *      FIXME: What to do on a SIGHUP???
725  */
726 int thread_pool_init(CONF_SECTION *cs, int *spawn_flag)
727 {
728         int             i, rcode;
729         CONF_SECTION    *pool_cf;
730         time_t          now;
731
732         now = time(NULL);
733
734         rad_assert(spawn_flag != NULL);
735         rad_assert(*spawn_flag == TRUE);
736         rad_assert(pool_initialized == FALSE); /* not called on HUP */
737
738         pool_cf = cf_subsection_find_next(cs, NULL, "thread");
739         if (!pool_cf) *spawn_flag = FALSE;
740
741         /*
742          *      Initialize the thread pool to some reasonable values.
743          */
744         memset(&thread_pool, 0, sizeof(THREAD_POOL));
745         thread_pool.head = NULL;
746         thread_pool.tail = NULL;
747         thread_pool.total_threads = 0;
748         thread_pool.max_thread_num = 1;
749         thread_pool.cleanup_delay = 5;
750         thread_pool.spawn_flag = *spawn_flag;
751         
752         /*
753          *      Don't bother initializing the mutexes or
754          *      creating the hash tables.  They won't be used.
755          */
756         if (!*spawn_flag) return 0;
757         
758 #ifdef WNOHANG
759         if ((pthread_mutex_init(&thread_pool.wait_mutex,NULL) != 0)) {
760                 radlog(L_ERR, "FATAL: Failed to initialize wait mutex: %s",
761                        strerror(errno));
762                 return -1;
763         }
764         
765         /*
766          *      Create the hash table of child PID's
767          */
768         thread_pool.waiters = fr_hash_table_create(pid_hash,
769                                                    pid_cmp,
770                                                    free);
771         if (!thread_pool.waiters) {
772                 radlog(L_ERR, "FATAL: Failed to set up wait hash");
773                 return -1;
774         }
775 #endif
776
777         if (cf_section_parse(pool_cf, NULL, thread_config) < 0) {
778                 return -1;
779         }
780
781         /*
782          *      Catch corner cases.
783          */
784         if (thread_pool.min_spare_threads < 1)
785                 thread_pool.min_spare_threads = 1;
786         if (thread_pool.max_spare_threads < 1)
787                 thread_pool.max_spare_threads = 1;
788         if (thread_pool.max_spare_threads < thread_pool.min_spare_threads)
789                 thread_pool.max_spare_threads = thread_pool.min_spare_threads;
790
791         /*
792          *      The pool has already been initialized.  Don't spawn
793          *      new threads, and don't forget about forked children,
794          */
795         if (pool_initialized) {
796                 return 0;
797         }
798
799         /*
800          *      Initialize the queue of requests.
801          */
802         memset(&thread_pool.semaphore, 0, sizeof(thread_pool.semaphore));
803         rcode = sem_init(&thread_pool.semaphore, 0, SEMAPHORE_LOCKED);
804         if (rcode != 0) {
805                 radlog(L_ERR, "FATAL: Failed to initialize semaphore: %s",
806                        strerror(errno));
807                 return -1;
808         }
809
810         rcode = pthread_mutex_init(&thread_pool.queue_mutex,NULL);
811         if (rcode != 0) {
812                 radlog(L_ERR, "FATAL: Failed to initialize queue mutex: %s",
813                        strerror(errno));
814                 return -1;
815         }
816
817         /*
818          *      Allocate multiple fifos.
819          */
820         for (i = 0; i < RAD_LISTEN_MAX; i++) {
821                 thread_pool.fifo[i] = fr_fifo_create(65536, NULL);
822                 if (!thread_pool.fifo[i]) {
823                         radlog(L_ERR, "FATAL: Failed to set up request fifo");
824                         return -1;
825                 }
826         }
827
828 #ifdef HAVE_OPENSSL_CRYPTO_H
829         /*
830          *      If we're linking with OpenSSL too, then we need
831          *      to set up the mutexes and enable the thread callbacks.
832          */
833         if (!setup_ssl_mutexes()) {
834                 radlog(L_ERR, "FATAL: Failed to set up SSL mutexes");
835                 return -1;
836         }
837 #endif
838
839
840         /*
841          *      Create a number of waiting threads.
842          *
843          *      If we fail while creating them, do something intelligent.
844          */
845         for (i = 0; i < thread_pool.start_threads; i++) {
846                 if (spawn_thread(now) == NULL) {
847                         return -1;
848                 }
849         }
850
851         DEBUG2("Thread pool initialized");
852         pool_initialized = TRUE;
853         return 0;
854 }
855
856
857 /*
858  *      Assign a new request to a free thread.
859  *
860  *      If there isn't a free thread, then try to create a new one,
861  *      up to the configured limits.
862  */
863 int thread_pool_addrequest(REQUEST *request, RAD_REQUEST_FUNP fun)
864 {
865         almost_now = request->timestamp;
866
867         /*
868          *      We've been told not to spawn threads, so don't.
869          */
870         if (!thread_pool.spawn_flag) {
871                 radius_handle_request(request, fun);
872
873 #ifdef WNOHANG
874                 /*
875                  *      Requests that care about child process exit
876                  *      codes have already either called
877                  *      rad_waitpid(), or they've given up.
878                  */
879                 wait(NULL);
880 #endif
881                 return 1;
882         }
883
884         /*
885          *      Add the new request to the queue.
886          */
887         if (!request_enqueue(request, fun)) return 0;
888
889         /*
890          *      If we haven't checked the number of child threads
891          *      in a while, OR if the thread pool appears to be full,
892          *      go manage it.
893          */
894         if ((last_cleaned < almost_now) ||
895             (thread_pool.active_threads == thread_pool.total_threads)) {
896                 thread_pool_manage(almost_now);
897         }
898
899         return 1;
900 }
901
902 /*
903  *      Check the min_spare_threads and max_spare_threads.
904  *
905  *      If there are too many or too few threads waiting, then we
906  *      either create some more, or delete some.
907  */
908 static void thread_pool_manage(time_t now)
909 {
910         int spare;
911         int i, total;
912         THREAD_HANDLE *handle, *next;
913         int active_threads;
914
915         /*
916          *      We don't need a mutex lock here, as we're reading
917          *      active_threads, and not modifying it.  We want a close
918          *      approximation of the number of active threads, and this
919          *      is good enough.
920          */
921         active_threads = thread_pool.active_threads;
922         spare = thread_pool.total_threads - active_threads;
923         if (debug_flag) {
924                 static int old_total = -1;
925                 static int old_active = -1;
926
927                 if ((old_total != thread_pool.total_threads) ||
928                                 (old_active != active_threads)) {
929                         DEBUG2("Threads: total/active/spare threads = %d/%d/%d",
930                                         thread_pool.total_threads, active_threads, spare);
931                         old_total = thread_pool.total_threads;
932                         old_active = active_threads;
933                 }
934         }
935
936         /*
937          *      If there are too few spare threads.  Go create some more.
938          */
939         if (spare < thread_pool.min_spare_threads) {
940                 total = thread_pool.min_spare_threads - spare;
941
942                 DEBUG2("Threads: Spawning %d spares", total);
943
944                 /*
945                  *      Create a number of spare threads.
946                  */
947                 for (i = 0; i < total; i++) {
948                         handle = spawn_thread(now);
949                         if (handle == NULL) {
950                                 return;
951                         }
952                 }
953
954                 return;         /* there aren't too many spare threads */
955         }
956
957         /*
958          *      Only delete spare threads if we haven't already done
959          *      so this second.
960          */
961         if (now == last_cleaned) {
962                 return;
963         }
964         last_cleaned = now;
965
966         /*
967          *      Loop over the thread pool, deleting exited threads.
968          */
969         for (handle = thread_pool.head; handle; handle = next) {
970                 next = handle->next;
971
972                 /*
973                  *      Maybe we've asked the thread to exit, and it
974                  *      has agreed.
975                  */
976                 if (handle->status == THREAD_EXITED) {
977                         delete_thread(handle);
978                 }
979         }
980
981         /*
982          *      Only delete the spare threads if sufficient time has
983          *      passed since we last created one.  This helps to minimize
984          *      the amount of create/delete cycles.
985          */
986         if ((now - thread_pool.time_last_spawned) < thread_pool.cleanup_delay) {
987                 return;
988         }
989
990         /*
991          *      If there are too many spare threads, delete one.
992          *
993          *      Note that we only delete ONE at a time, instead of
994          *      wiping out many.  This allows the excess servers to
995          *      be slowly reaped, just in case the load spike comes again.
996          */
997         if (spare > thread_pool.max_spare_threads) {
998
999                 spare -= thread_pool.max_spare_threads;
1000
1001                 DEBUG2("Threads: deleting 1 spare out of %d spares", spare);
1002
1003                 /*
1004                  *      Walk through the thread pool, deleting the
1005                  *      first idle thread we come across.
1006                  */
1007                 for (handle = thread_pool.head; (handle != NULL) && (spare > 0) ; handle = next) {
1008                         next = handle->next;
1009
1010                         /*
1011                          *      If the thread is not handling a
1012                          *      request, but still live, then tell it
1013                          *      to exit.
1014                          *
1015                          *      It will eventually wake up, and realize
1016                          *      it's been told to commit suicide.
1017                          */
1018                         if ((handle->request == NULL) &&
1019                             (handle->status == THREAD_RUNNING)) {
1020                                 handle->status = THREAD_CANCELLED;
1021                                 /*
1022                                  *      Post an extra semaphore, as a
1023                                  *      signal to wake up, and exit.
1024                                  */
1025                                 sem_post(&thread_pool.semaphore);
1026                                 spare--;
1027                                 break;
1028                         }
1029                 }
1030         }
1031
1032         /*
1033          *      If the thread has handled too many requests, then make it
1034          *      exit.
1035          */
1036         if (thread_pool.max_requests_per_thread > 0) {
1037                 for (handle = thread_pool.head; handle; handle = next) {
1038                         next = handle->next;
1039
1040                         /*
1041                          *      Not handling a request, but otherwise
1042                          *      live, we can kill it.
1043                          */
1044                         if ((handle->request == NULL) &&
1045                             (handle->status == THREAD_RUNNING) &&
1046                             (handle->request_count > thread_pool.max_requests_per_thread)) {
1047                                 handle->status = THREAD_CANCELLED;
1048                                 sem_post(&thread_pool.semaphore);
1049                         }
1050                 }
1051         }
1052
1053         /*
1054          *      Otherwise everything's kosher.  There are not too few,
1055          *      or too many spare threads.  Exit happily.
1056          */
1057         return;
1058 }
1059
1060
1061 #ifdef WNOHANG
1062 /*
1063  *      Thread wrapper for fork().
1064  */
1065 pid_t rad_fork(void)
1066 {
1067         pid_t child_pid;
1068
1069         if (!pool_initialized) return fork();
1070
1071         reap_children();        /* be nice to non-wait thingies */
1072
1073         if (fr_hash_table_num_elements(thread_pool.waiters) >= 1024) {
1074                 return -1;
1075         }
1076
1077         /*
1078          *      Fork & save the PID for later reaping.
1079          */
1080         child_pid = fork();
1081         if (child_pid > 0) {
1082                 int rcode;
1083                 thread_fork_t *tf;
1084
1085                 tf = rad_malloc(sizeof(*tf));
1086                 memset(tf, 0, sizeof(*tf));
1087
1088                 tf->pid = child_pid;
1089
1090                 pthread_mutex_lock(&thread_pool.wait_mutex);
1091                 rcode = fr_hash_table_insert(thread_pool.waiters, tf);
1092                 pthread_mutex_unlock(&thread_pool.wait_mutex);
1093
1094                 if (!rcode) {
1095                         radlog(L_ERR, "Failed to store PID, creating what will be a zombie process %d",
1096                                (int) child_pid);
1097                         free(tf);
1098                 }
1099         }
1100
1101         /*
1102          *      Return whatever we were told.
1103          */
1104         return child_pid;
1105 }
1106
1107
1108 /*
1109  *      Wait 10 seconds at most for a child to exit, then give up.
1110  */
1111 pid_t rad_waitpid(pid_t pid, int *status)
1112 {
1113         int i;
1114         thread_fork_t mytf, *tf;
1115
1116         if (!pool_initialized) return waitpid(pid, status, 0);
1117
1118         if (pid <= 0) return -1;
1119
1120         mytf.pid = pid;
1121
1122         pthread_mutex_lock(&thread_pool.wait_mutex);
1123         tf = fr_hash_table_finddata(thread_pool.waiters, &mytf);
1124         pthread_mutex_unlock(&thread_pool.wait_mutex);
1125
1126         if (!tf) return -1;
1127
1128         for (i = 0; i < 100; i++) {
1129                 reap_children();
1130
1131                 if (tf->exited) {
1132                         *status = tf->status;
1133
1134                         pthread_mutex_lock(&thread_pool.wait_mutex);
1135                         fr_hash_table_delete(thread_pool.waiters, &mytf);
1136                         pthread_mutex_unlock(&thread_pool.wait_mutex);
1137                         return pid;
1138                 }
1139                 usleep(100000); /* sleep for 1/10 of a second */
1140         }
1141
1142         /*
1143          *      10 seconds have passed, give up on the child.
1144          */
1145         pthread_mutex_lock(&thread_pool.wait_mutex);
1146         fr_hash_table_delete(thread_pool.waiters, &mytf);
1147         pthread_mutex_unlock(&thread_pool.wait_mutex);
1148
1149         return 0;
1150 }
1151 #else
1152 /*
1153  *      No rad_fork or rad_waitpid
1154  */
1155 #endif
1156
1157 void thread_pool_lock(void)
1158 {
1159         pthread_mutex_lock(&thread_pool.queue_mutex);
1160 }
1161
1162 void thread_pool_unlock(void)
1163 {
1164         pthread_mutex_unlock(&thread_pool.queue_mutex);
1165 }
1166
1167 void thread_pool_queue_stats(int *array)
1168 {
1169         int i;
1170
1171         if (pool_initialized) {
1172                 for (i = 0; i < RAD_LISTEN_MAX; i++) {
1173                         array[i] = fr_fifo_num_elements(thread_pool.fifo[i]);
1174                 }
1175         } else {
1176                 for (i = 0; i < RAD_LISTEN_MAX; i++) {
1177                         array[i] = 0;
1178                 }
1179         }
1180 }
1181 #else
1182 int thread_pool_addrequest(REQUEST *request, RAD_REQUEST_FUNP fun)
1183 {
1184         radius_handle_request(request, fun);
1185         
1186 #ifdef WNOHANG
1187         /*
1188          *      Requests that care about child process exit
1189          *      codes have already either called
1190          *      rad_waitpid(), or they've given up.
1191          */
1192         wait(NULL);
1193 #endif
1194         return 1;
1195 }
1196
1197 #endif /* HAVE_PTHREAD_H */