]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - lib/libc_r/uthread/uthread_init.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / lib / libc_r / uthread / uthread_init.c
1 /*
2  * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of any co-contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31
32 /* Allocate space for global thread variables here: */
33 #define GLOBAL_PTHREAD_PRIVATE
34
35 #include "namespace.h"
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <machine/reg.h>
39
40 #include <sys/ioctl.h>
41 #include <sys/mount.h>
42 #include <sys/uio.h>
43 #include <sys/socket.h>
44 #include <sys/event.h>
45 #include <sys/stat.h>
46 #include <sys/sysctl.h>
47 #include <sys/time.h>
48 #include <sys/ttycom.h>
49 #include <sys/wait.h>
50 #include <sys/mman.h>
51 #include <dirent.h>
52 #include <errno.h>
53 #include <fcntl.h>
54 #include <paths.h>
55 #include <poll.h>
56 #include <pthread.h>
57 #include <pthread_np.h>
58 #include <signal.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63 #include "un-namespace.h"
64
65 #include "libc_private.h"
66 #include "pthread_private.h"
67
68 int     __pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *);
69 int     __pthread_mutex_lock(pthread_mutex_t *);
70 int     __pthread_mutex_trylock(pthread_mutex_t *);
71
72 /*
73  * All weak references used within libc should be in this table.
74  * This allows static libraries to work.
75  */
76 static void *references[] = {
77         &_accept,
78         &_bind,
79         &_close,
80         &_connect,
81         &_dup,
82         &_dup2,
83         &_execve,
84         &_fcntl,
85         &_flock,
86         &_flockfile,
87         &_fstat,
88         &_fstatfs,
89         &_fsync,
90         &_funlockfile,
91         &_getdirentries,
92         &_getlogin,
93         &_getpeername,
94         &_getsockname,
95         &_getsockopt,
96         &_ioctl,
97         &_kevent,
98         &_kqueue,
99         &_listen,
100         &_nanosleep,
101         &_open,
102         &_pthread_cond_destroy,
103         &_pthread_cond_init,
104         &_pthread_cond_signal,
105         &_pthread_cond_wait,
106         &_pthread_getspecific,
107         &_pthread_key_create,
108         &_pthread_key_delete,
109         &_pthread_mutex_destroy,
110         &_pthread_mutex_init,
111         &_pthread_mutex_lock,
112         &_pthread_mutex_trylock,
113         &_pthread_mutex_unlock,
114         &_pthread_mutexattr_init,
115         &_pthread_mutexattr_destroy,
116         &_pthread_mutexattr_settype,
117         &_pthread_once,
118         &_pthread_setspecific,
119         &_read,
120         &_readv,
121         &_recvfrom,
122         &_recvmsg,
123         &_select,
124         &_sendmsg,
125         &_sendto,
126         &_setsockopt,
127         &_sigaction,
128         &_sigprocmask,
129         &_sigsuspend,
130         &_socket,
131         &_socketpair,
132         &_wait4,
133         &_write,
134         &_writev
135 };
136
137 /*
138  * These are needed when linking statically.  All references within
139  * libgcc (and libc) to these routines are weak, but if they are not
140  * (strongly) referenced by the application or other libraries, then
141  * the actual functions will not be loaded.
142  */
143 static void *libgcc_references[] = {
144         &_pthread_once,
145         &_pthread_key_create,
146         &_pthread_key_delete,
147         &_pthread_getspecific,
148         &_pthread_setspecific,
149         &_pthread_mutex_init,
150         &_pthread_mutex_destroy,
151         &_pthread_mutex_lock,
152         &_pthread_mutex_trylock,
153         &_pthread_mutex_unlock
154 };
155
156 #define DUAL_ENTRY(entry)       \
157         (pthread_func_t)entry, (pthread_func_t)entry
158
159 static pthread_func_t jmp_table[][2] = {
160         {DUAL_ENTRY(_pthread_atfork)},  /* PJT_ATFORK */
161         {DUAL_ENTRY(_pthread_attr_destroy)},    /* PJT_ATTR_DESTROY */
162         {DUAL_ENTRY(_pthread_attr_getdetachstate)},     /* PJT_ATTR_GETDETACHSTATE */
163         {DUAL_ENTRY(_pthread_attr_getguardsize)},       /* PJT_ATTR_GETGUARDSIZE */
164         {DUAL_ENTRY(_pthread_attr_getinheritsched)},    /* PJT_ATTR_GETINHERITSCHED */
165         {DUAL_ENTRY(_pthread_attr_getschedparam)},      /* PJT_ATTR_GETSCHEDPARAM */
166         {DUAL_ENTRY(_pthread_attr_getschedpolicy)},     /* PJT_ATTR_GETSCHEDPOLICY */
167         {DUAL_ENTRY(_pthread_attr_getscope)},   /* PJT_ATTR_GETSCOPE */
168         {DUAL_ENTRY(_pthread_attr_getstackaddr)},       /* PJT_ATTR_GETSTACKADDR */
169         {DUAL_ENTRY(_pthread_attr_getstacksize)},       /* PJT_ATTR_GETSTACKSIZE */
170         {DUAL_ENTRY(_pthread_attr_init)},       /* PJT_ATTR_INIT */
171         {DUAL_ENTRY(_pthread_attr_setdetachstate)},     /* PJT_ATTR_SETDETACHSTATE */
172         {DUAL_ENTRY(_pthread_attr_setguardsize)},       /* PJT_ATTR_SETGUARDSIZE */
173         {DUAL_ENTRY(_pthread_attr_setinheritsched)},    /* PJT_ATTR_SETINHERITSCHED */
174         {DUAL_ENTRY(_pthread_attr_setschedparam)},      /* PJT_ATTR_SETSCHEDPARAM */
175         {DUAL_ENTRY(_pthread_attr_setschedpolicy)},     /* PJT_ATTR_SETSCHEDPOLICY */
176         {DUAL_ENTRY(_pthread_attr_setscope)},   /* PJT_ATTR_SETSCOPE */
177         {DUAL_ENTRY(_pthread_attr_setstackaddr)},       /* PJT_ATTR_SETSTACKADDR */
178         {DUAL_ENTRY(_pthread_attr_setstacksize)},       /* PJT_ATTR_SETSTACKSIZE */
179         {DUAL_ENTRY(_pthread_cancel)},  /* PJT_CANCEL */
180         {DUAL_ENTRY(_pthread_cleanup_pop)},     /* PJT_CLEANUP_POP */
181         {DUAL_ENTRY(_pthread_cleanup_push)},    /* PJT_CLEANUP_PUSH */
182         {DUAL_ENTRY(_pthread_cond_broadcast)},  /* PJT_COND_BROADCAST */
183         {DUAL_ENTRY(_pthread_cond_destroy)},    /* PJT_COND_DESTROY */
184         {DUAL_ENTRY(_pthread_cond_init)},       /* PJT_COND_INIT */
185         {DUAL_ENTRY(_pthread_cond_signal)},     /* PJT_COND_SIGNAL */
186         {DUAL_ENTRY(_pthread_cond_timedwait)},  /* PJT_COND_TIMEDWAIT */
187         {(pthread_func_t)__pthread_cond_wait,
188          (pthread_func_t)_pthread_cond_wait},   /* PJT_COND_WAIT */
189         {DUAL_ENTRY(_pthread_detach)},  /* PJT_DETACH */
190         {DUAL_ENTRY(_pthread_equal)},   /* PJT_EQUAL */
191         {DUAL_ENTRY(_pthread_exit)},    /* PJT_EXIT */
192         {DUAL_ENTRY(_pthread_getspecific)},     /* PJT_GETSPECIFIC */
193         {DUAL_ENTRY(_pthread_join)},    /* PJT_JOIN */
194         {DUAL_ENTRY(_pthread_key_create)},      /* PJT_KEY_CREATE */
195         {DUAL_ENTRY(_pthread_key_delete)},      /* PJT_KEY_DELETE*/
196         {DUAL_ENTRY(_pthread_kill)},    /* PJT_KILL */
197         {DUAL_ENTRY(_pthread_main_np)},         /* PJT_MAIN_NP */
198         {DUAL_ENTRY(_pthread_mutexattr_destroy)}, /* PJT_MUTEXATTR_DESTROY */
199         {DUAL_ENTRY(_pthread_mutexattr_init)},  /* PJT_MUTEXATTR_INIT */
200         {DUAL_ENTRY(_pthread_mutexattr_settype)}, /* PJT_MUTEXATTR_SETTYPE */
201         {DUAL_ENTRY(_pthread_mutex_destroy)},   /* PJT_MUTEX_DESTROY */
202         {DUAL_ENTRY(_pthread_mutex_init)},      /* PJT_MUTEX_INIT */
203         {(pthread_func_t)__pthread_mutex_lock,
204          (pthread_func_t)_pthread_mutex_lock},  /* PJT_MUTEX_LOCK */
205         {(pthread_func_t)__pthread_mutex_trylock,
206          (pthread_func_t)_pthread_mutex_trylock},/* PJT_MUTEX_TRYLOCK */
207         {DUAL_ENTRY(_pthread_mutex_unlock)},    /* PJT_MUTEX_UNLOCK */
208         {DUAL_ENTRY(_pthread_once)},            /* PJT_ONCE */
209         {DUAL_ENTRY(_pthread_rwlock_destroy)},  /* PJT_RWLOCK_DESTROY */
210         {DUAL_ENTRY(_pthread_rwlock_init)},     /* PJT_RWLOCK_INIT */
211         {DUAL_ENTRY(_pthread_rwlock_rdlock)},   /* PJT_RWLOCK_RDLOCK */
212         {DUAL_ENTRY(_pthread_rwlock_tryrdlock)},/* PJT_RWLOCK_TRYRDLOCK */
213         {DUAL_ENTRY(_pthread_rwlock_trywrlock)},/* PJT_RWLOCK_TRYWRLOCK */
214         {DUAL_ENTRY(_pthread_rwlock_unlock)},   /* PJT_RWLOCK_UNLOCK */
215         {DUAL_ENTRY(_pthread_rwlock_wrlock)},   /* PJT_RWLOCK_WRLOCK */
216         {DUAL_ENTRY(_pthread_self)},            /* PJT_SELF */
217         {DUAL_ENTRY(_pthread_setcancelstate)},  /* PJT_SETCANCELSTATE */
218         {DUAL_ENTRY(_pthread_setcanceltype)},   /* PJT_SETCANCELTYPE */
219         {DUAL_ENTRY(_pthread_setspecific)},     /* PJT_SETSPECIFIC */
220         {DUAL_ENTRY(_pthread_sigmask)},         /* PJT_SIGMASK */
221         {DUAL_ENTRY(_pthread_testcancel)}       /* PJT_TESTCANCEL */
222 };
223
224 int _pthread_guard_default;
225 int _pthread_page_size;
226 int _pthread_stack_default;
227 int _pthread_stack_initial;
228
229 /*
230  * Threaded process initialization
231  */
232 void
233 _thread_init(void)
234 {
235         int             fd;
236         int             flags;
237         int             i;
238         size_t          len;
239         int             mib[2];
240         int             sched_stack_size;       /* Size of scheduler stack. */
241 #if !defined(__ia64__)
242         u_long          stackp;
243 #endif
244
245         struct clockinfo clockinfo;
246         struct sigaction act;
247
248
249         /* Check if this function has already been called: */
250         if (_thread_initial)
251                 /* Only initialise the threaded application once. */
252                 return;
253
254         _pthread_page_size = getpagesize();;
255         _pthread_guard_default = _pthread_page_size;
256         sched_stack_size = 4 * _pthread_page_size;
257         if (sizeof(void *) == 8) {
258                 _pthread_stack_default = PTHREAD_STACK64_DEFAULT;
259                 _pthread_stack_initial = PTHREAD_STACK64_INITIAL;
260         }
261         else {
262                 _pthread_stack_default = PTHREAD_STACK32_DEFAULT;
263                 _pthread_stack_initial = PTHREAD_STACK32_INITIAL;
264         }
265
266         _pthread_attr_default.guardsize_attr = _pthread_guard_default;
267         _pthread_attr_default.stacksize_attr = _pthread_stack_default;
268
269         /*
270          * Make gcc quiescent about {,libgcc_}references not being
271          * referenced:
272          */
273         if ((references[0] == NULL) || (libgcc_references[0] == NULL))
274                 PANIC("Failed loading mandatory references in _thread_init");
275
276         /*
277          * Check the size of the jump table to make sure it is preset
278          * with the correct number of entries.
279          */
280         if (sizeof(jmp_table) != (sizeof(pthread_func_t) * PJT_MAX * 2))
281                 PANIC("Thread jump table not properly initialized");
282         memcpy(__thr_jtable, jmp_table, sizeof(jmp_table));
283
284         /*
285          * Check for the special case of this process running as
286          * or in place of init as pid = 1:
287          */
288         if (getpid() == 1) {
289                 /*
290                  * Setup a new session for this process which is
291                  * assumed to be running as root.
292                  */
293                 if (setsid() == -1)
294                         PANIC("Can't set session ID");
295                 if (revoke(_PATH_CONSOLE) != 0)
296                         PANIC("Can't revoke console");
297                 if ((fd = __sys_open(_PATH_CONSOLE, O_RDWR)) < 0)
298                         PANIC("Can't open console");
299                 if (setlogin("root") == -1)
300                         PANIC("Can't set login to root");
301                 if (__sys_ioctl(fd, TIOCSCTTY, (char *) NULL) == -1)
302                         PANIC("Can't set controlling terminal");
303                 if (__sys_dup2(fd, 0) == -1 ||
304                     __sys_dup2(fd, 1) == -1 ||
305                     __sys_dup2(fd, 2) == -1)
306                         PANIC("Can't dup2");
307         }
308
309         /* Get the standard I/O flags before messing with them : */
310         for (i = 0; i < 3; i++) {
311                 if (((_pthread_stdio_flags[i] =
312                     __sys_fcntl(i, F_GETFL, NULL)) == -1) &&
313                     (errno != EBADF))
314                         PANIC("Cannot get stdio flags");
315         }
316
317         /*
318          * Create a pipe that is written to by the signal handler to prevent
319          * signals being missed in calls to _select:
320          */
321         if (__sys_pipe(_thread_kern_pipe) != 0) {
322                 /* Cannot create pipe, so abort: */
323                 PANIC("Cannot create kernel pipe");
324         }
325
326         /*
327          * Make sure the pipe does not get in the way of stdio:
328          */
329         for (i = 0; i < 2; i++) {
330                 if (_thread_kern_pipe[i] < 3) {
331                         fd = __sys_fcntl(_thread_kern_pipe[i], F_DUPFD, 3);
332                         if (fd == -1)
333                             PANIC("Cannot create kernel pipe");
334                         __sys_close(_thread_kern_pipe[i]);
335                         _thread_kern_pipe[i] = fd;
336                 }
337         }
338         /* Get the flags for the read pipe: */
339         if ((flags = __sys_fcntl(_thread_kern_pipe[0], F_GETFL, NULL)) == -1) {
340                 /* Abort this application: */
341                 PANIC("Cannot get kernel read pipe flags");
342         }
343         /* Make the read pipe non-blocking: */
344         else if (__sys_fcntl(_thread_kern_pipe[0], F_SETFL, flags | O_NONBLOCK) == -1) {
345                 /* Abort this application: */
346                 PANIC("Cannot make kernel read pipe non-blocking");
347         }
348         /* Get the flags for the write pipe: */
349         else if ((flags = __sys_fcntl(_thread_kern_pipe[1], F_GETFL, NULL)) == -1) {
350                 /* Abort this application: */
351                 PANIC("Cannot get kernel write pipe flags");
352         }
353         /* Make the write pipe non-blocking: */
354         else if (__sys_fcntl(_thread_kern_pipe[1], F_SETFL, flags | O_NONBLOCK) == -1) {
355                 /* Abort this application: */
356                 PANIC("Cannot get kernel write pipe flags");
357         }
358         /* Allocate and initialize the ready queue: */
359         else if (_pq_alloc(&_readyq, PTHREAD_MIN_PRIORITY, PTHREAD_LAST_PRIORITY) != 0) {
360                 /* Abort this application: */
361                 PANIC("Cannot allocate priority ready queue.");
362         }
363         /* Allocate memory for the thread structure of the initial thread: */
364         else if ((_thread_initial = (pthread_t) malloc(sizeof(struct pthread))) == NULL) {
365                 /*
366                  * Insufficient memory to initialise this application, so
367                  * abort:
368                  */
369                 PANIC("Cannot allocate memory for initial thread");
370         }
371         /* Allocate memory for the scheduler stack: */
372         else if ((_thread_kern_sched_stack = malloc(sched_stack_size)) == NULL)
373                 PANIC("Failed to allocate stack for scheduler");
374         else {
375                 /* Zero the global kernel thread structure: */
376                 memset(&_thread_kern_thread, 0, sizeof(struct pthread));
377                 _thread_kern_thread.flags = PTHREAD_FLAGS_PRIVATE;
378                 memset(_thread_initial, 0, sizeof(struct pthread));
379
380                 /* Initialize the waiting and work queues: */
381                 TAILQ_INIT(&_waitingq);
382                 TAILQ_INIT(&_workq);
383
384                 /* Initialize the scheduling switch hook routine: */
385                 _sched_switch_hook = NULL;
386
387                 /* Give this thread default attributes: */
388                 memcpy((void *) &_thread_initial->attr, &_pthread_attr_default,
389                     sizeof(struct pthread_attr));
390
391                 /* Find the stack top */
392                 mib[0] = CTL_KERN;
393                 mib[1] = KERN_USRSTACK;
394                 len = sizeof (_usrstack);
395                 if (sysctl(mib, 2, &_usrstack, &len, NULL, 0) == -1)
396                         _usrstack = (void *)USRSTACK;
397                 /*
398                  * Create a red zone below the main stack.  All other stacks are
399                  * constrained to a maximum size by the paramters passed to
400                  * mmap(), but this stack is only limited by resource limits, so
401                  * this stack needs an explicitly mapped red zone to protect the
402                  * thread stack that is just beyond.
403                  */
404                 if (mmap(_usrstack - _pthread_stack_initial -
405                     _pthread_guard_default, _pthread_guard_default, 0,
406                     MAP_ANON, -1, 0) == MAP_FAILED)
407                         PANIC("Cannot allocate red zone for initial thread");
408
409                 /* Set the main thread stack pointer. */
410                 _thread_initial->stack = _usrstack - _pthread_stack_initial;
411
412                 /* Set the stack attributes: */
413                 _thread_initial->attr.stackaddr_attr = _thread_initial->stack;
414                 _thread_initial->attr.stacksize_attr = _pthread_stack_initial;
415
416                 /* Setup the context for the scheduler: */
417                 _setjmp(_thread_kern_sched_jb);
418 #if !defined(__ia64__)
419                 stackp = (long)_thread_kern_sched_stack + sched_stack_size - sizeof(double);
420 #if defined(__amd64__)
421                 stackp &= ~0xFUL;
422 #endif
423                 SET_STACK_JB(_thread_kern_sched_jb, stackp);
424 #else
425                 SET_STACK_JB(_thread_kern_sched_jb, _thread_kern_sched_stack,
426                     sched_stack_size);
427 #endif
428                 SET_RETURN_ADDR_JB(_thread_kern_sched_jb, _thread_kern_scheduler);
429
430                 /*
431                  * Write a magic value to the thread structure
432                  * to help identify valid ones:
433                  */
434                 _thread_initial->magic = PTHREAD_MAGIC;
435
436                 /* Set the initial cancel state */
437                 _thread_initial->cancelflags = PTHREAD_CANCEL_ENABLE |
438                     PTHREAD_CANCEL_DEFERRED;
439
440                 /* Default the priority of the initial thread: */
441                 _thread_initial->base_priority = PTHREAD_DEFAULT_PRIORITY;
442                 _thread_initial->active_priority = PTHREAD_DEFAULT_PRIORITY;
443                 _thread_initial->inherited_priority = 0;
444
445                 /* Initialise the state of the initial thread: */
446                 _thread_initial->state = PS_RUNNING;
447
448                 /* Set the name of the thread: */
449                 _thread_initial->name = strdup("_thread_initial");
450
451                 /* Initialize joiner to NULL (no joiner): */
452                 _thread_initial->joiner = NULL;
453
454                 /* Initialize the owned mutex queue and count: */
455                 TAILQ_INIT(&(_thread_initial->mutexq));
456                 _thread_initial->priority_mutex_count = 0;
457
458                 /* Initialize the global scheduling time: */
459                 _sched_ticks = 0;
460                 gettimeofday((struct timeval *) &_sched_tod, NULL);
461
462                 /* Initialize last active: */
463                 _thread_initial->last_active = (long) _sched_ticks;
464
465                 /* Initialize the initial context: */
466                 _thread_initial->curframe = NULL;
467
468                 /* Initialise the rest of the fields: */
469                 _thread_initial->poll_data.nfds = 0;
470                 _thread_initial->poll_data.fds = NULL;
471                 _thread_initial->sig_defer_count = 0;
472                 _thread_initial->yield_on_sig_undefer = 0;
473                 _thread_initial->specific = NULL;
474                 _thread_initial->cleanup = NULL;
475                 _thread_initial->flags = 0;
476                 _thread_initial->error = 0;
477                 TAILQ_INIT(&_thread_list);
478                 TAILQ_INSERT_HEAD(&_thread_list, _thread_initial, tle);
479                 _set_curthread(_thread_initial);
480                 TAILQ_INIT(&_atfork_list);
481                 _pthread_mutex_init(&_atfork_mutex, NULL);
482
483                 /* Initialise the global signal action structure: */
484                 sigfillset(&act.sa_mask);
485                 act.sa_handler = (void (*) ()) _thread_sig_handler;
486                 act.sa_flags = SA_SIGINFO | SA_RESTART;
487
488                 /* Clear pending signals for the process: */
489                 sigemptyset(&_process_sigpending);
490
491                 /* Clear the signal queue: */
492                 memset(_thread_sigq, 0, sizeof(_thread_sigq));
493
494                 /* Enter a loop to get the existing signal status: */
495                 for (i = 1; i < NSIG; i++) {
496                         /* Check for signals which cannot be trapped: */
497                         if (i == SIGKILL || i == SIGSTOP) {
498                         }
499
500                         /* Get the signal handler details: */
501                         else if (__sys_sigaction(i, NULL,
502                             &_thread_sigact[i - 1]) != 0) {
503                                 /*
504                                  * Abort this process if signal
505                                  * initialisation fails:
506                                  */
507                                 PANIC("Cannot read signal handler info");
508                         }
509
510                         /* Initialize the SIG_DFL dummy handler count. */
511                         _thread_dfl_count[i] = 0;
512                 }
513
514                 /*
515                  * Install the signal handler for the most important
516                  * signals that the user-thread kernel needs. Actually
517                  * SIGINFO isn't really needed, but it is nice to have.
518                  */
519                 if (__sys_sigaction(_SCHED_SIGNAL, &act, NULL) != 0 ||
520                     __sys_sigaction(SIGINFO,       &act, NULL) != 0 ||
521                     __sys_sigaction(SIGCHLD,       &act, NULL) != 0) {
522                         /*
523                          * Abort this process if signal initialisation fails:
524                          */
525                         PANIC("Cannot initialise signal handler");
526                 }
527                 _thread_sigact[_SCHED_SIGNAL - 1].sa_flags = SA_SIGINFO;
528                 _thread_sigact[SIGINFO - 1].sa_flags = SA_SIGINFO;
529                 _thread_sigact[SIGCHLD - 1].sa_flags = SA_SIGINFO;
530
531                 /* Get the process signal mask: */
532                 __sys_sigprocmask(SIG_SETMASK, NULL, &_process_sigmask);
533
534                 /* Get the kernel clockrate: */
535                 mib[0] = CTL_KERN;
536                 mib[1] = KERN_CLOCKRATE;
537                 len = sizeof (struct clockinfo);
538                 if (sysctl(mib, 2, &clockinfo, &len, NULL, 0) == 0)
539                         _clock_res_usec = clockinfo.tick > CLOCK_RES_USEC_MIN ?
540                             clockinfo.tick : CLOCK_RES_USEC_MIN;
541
542                 /* Get the table size: */
543                 if ((_thread_dtablesize = getdtablesize()) < 0) {
544                         /*
545                          * Cannot get the system defined table size, so abort
546                          * this process.
547                          */
548                         PANIC("Cannot get dtablesize");
549                 }
550                 /* Allocate memory for the file descriptor table: */
551                 if ((_thread_fd_table = (struct fd_table_entry **) malloc(sizeof(struct fd_table_entry *) * _thread_dtablesize)) == NULL) {
552                         /* Avoid accesses to file descriptor table on exit: */
553                         _thread_dtablesize = 0;
554
555                         /*
556                          * Cannot allocate memory for the file descriptor
557                          * table, so abort this process.
558                          */
559                         PANIC("Cannot allocate memory for file descriptor table");
560                 }
561                 /* Allocate memory for the pollfd table: */
562                 if ((_thread_pfd_table = (struct pollfd *) malloc(sizeof(struct pollfd) * _thread_dtablesize)) == NULL) {
563                         /*
564                          * Cannot allocate memory for the file descriptor
565                          * table, so abort this process.
566                          */
567                         PANIC("Cannot allocate memory for pollfd table");
568                 } else {
569                         /*
570                          * Enter a loop to initialise the file descriptor
571                          * table:
572                          */
573                         for (i = 0; i < _thread_dtablesize; i++) {
574                                 /* Initialise the file descriptor table: */
575                                 _thread_fd_table[i] = NULL;
576                         }
577
578                         /* Initialize stdio file descriptor table entries: */
579                         for (i = 0; i < 3; i++) {
580                                 if ((_thread_fd_table_init(i) != 0) &&
581                                     (errno != EBADF))
582                                         PANIC("Cannot initialize stdio file "
583                                             "descriptor table entry");
584                         }
585                 }
586         }
587
588         /* Initialise the garbage collector mutex and condition variable. */
589         if (_pthread_mutex_init(&_gc_mutex,NULL) != 0 ||
590             _pthread_cond_init(&_gc_cond,NULL) != 0)
591                 PANIC("Failed to initialise garbage collector mutex or condvar");
592 }
593
594
595 /*
596  * Special start up code for NetBSD/Alpha
597  */
598 #if     defined(__NetBSD__) && defined(__alpha__)
599 int
600 main(int argc, char *argv[], char *env);
601
602 int
603 _thread_main(int argc, char *argv[], char *env)
604 {
605         _thread_init();
606         return (main(argc, argv, env));
607 }
608 #endif