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