[BACK]Return to __exception.c CVS log [TXT][DIR] Up to [local] / prex-old / usr / lib / posix / signal

Annotation of prex-old/usr/lib/posix/signal/__exception.c, Revision 1.1.1.1

1.1       nbrk        1: /*
                      2:  * Copyright (c) 2005, Kohsuke Ohtani
                      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 THE AUTHOR 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:
                     30: #include <prex/prex.h>
                     31: #include <prex/signal.h>
                     32:
                     33: #include <unistd.h>
                     34: #include <signal.h>
                     35: #include <stdlib.h>
                     36:
                     37: struct sigaction __sig_act[NSIG];
                     38: sigset_t __sig_mask;
                     39: sigset_t __sig_pending;
                     40:
                     41: #ifdef _REENTRANT
                     42: volatile mutex_t __sig_lock;
                     43: #endif
                     44:
                     45: /*
                     46:  * Process all pending and unmasked signal
                     47:  *
                     48:  * return 0 if at least one pending signal was processed.
                     49:  * return -1 if no signal was processed.
                     50:  */
                     51: int
                     52: __sig_flush(void)
                     53: {
                     54:        int sig;
                     55:        sigset_t active, org_mask;
                     56:        struct sigaction action;
                     57:        struct siginfo si;
                     58:        int rc = -1;
                     59:
                     60:        sig = 1;
                     61:        for (;;) {
                     62:                SIGNAL_LOCK();
                     63:                active = __sig_pending & ~__sig_mask;
                     64:                action = __sig_act[sig];
                     65:                SIGNAL_UNLOCK();
                     66:
                     67:                if (active == 0)
                     68:                        break;
                     69:                if (active & sigmask(sig)) {
                     70:
                     71:                        SIGNAL_LOCK();
                     72:                        org_mask = __sig_mask;
                     73:                        __sig_mask |= action.sa_mask;
                     74:                        SIGNAL_UNLOCK();
                     75:
                     76:                        if (action.sa_handler == SIG_DFL) {
                     77:                                /* Default */
                     78:                                switch (sig) {
                     79:                                case SIGCHLD:
                     80:                                        /* XXX: */
                     81:                                        break;
                     82:                                default:
                     83:                                        exit(0);
                     84:                                }
                     85:                        } else if (action.sa_handler != SIG_IGN) {
                     86:                                /* User defined */
                     87:                                if (action.sa_flags & SA_SIGINFO) {
                     88:                                        si.si_signo = sig;
                     89:                                        si.si_code = 0;
                     90:                                        si.si_value.sival_int = 0;
                     91:                                        action.sa_sigaction(sig, &si, NULL);
                     92:                                } else {
                     93:                                        action.sa_handler(sig);
                     94:                                }
                     95:                        }
                     96:                        SIGNAL_LOCK();
                     97:                        __sig_pending &= ~sigmask(sig);
                     98:                        __sig_mask = org_mask;
                     99:                        SIGNAL_UNLOCK();
                    100:
                    101:                        switch (sig) {
                    102:                        case SIGILL:
                    103:                        case SIGTRAP:
                    104:                        case SIGFPE:
                    105:                        case SIGSEGV:
                    106:                                for (;;);       /* exception from kernel */
                    107:                                break;
                    108:                        }
                    109:                        if (action.sa_handler != SIG_IGN)
                    110:                                rc = 0; /* Signal is processed */
                    111:                }
                    112:
                    113:                if (++sig >= NSIG)
                    114:                        sig = 1;
                    115:        }
                    116:        return rc;
                    117: }
                    118:
                    119: /*
                    120:  * Exception handler for signal emulation
                    121:  */
                    122: static void
                    123: __exception_handler(int excpt, void *regs)
                    124: {
                    125:
                    126:        if (excpt > 0 && excpt <= NSIG) {
                    127:
                    128:                SIGNAL_LOCK();
                    129:
                    130:                if (__sig_act[excpt].sa_handler != SIG_IGN)
                    131:                        __sig_pending |= sigmask(excpt);
                    132:
                    133:                SIGNAL_UNLOCK();
                    134:        }
                    135:        __sig_flush();
                    136:        exception_return(regs);
                    137: }
                    138:
                    139: /*
                    140:  * Initialize exception
                    141:  */
                    142: void
                    143: __exception_init(void)
                    144: {
                    145:        int i;
                    146:
                    147: #ifdef _REENTRANT
                    148:        mutex_init(&__sig_lock);
                    149: #endif
                    150:        exception_setup(NULL);
                    151:
                    152:        __sig_mask = 0;
                    153:        __sig_pending = 0;
                    154:
                    155:        for (i = 0; i < NSIG; i++) {
                    156:                __sig_act[i].sa_flags = 0;
                    157:                __sig_act[i].sa_handler = SIG_DFL;
                    158:        }
                    159:
                    160:        /* Install exception handler */
                    161:        exception_setup(__exception_handler);
                    162: }
                    163:
                    164: /*
                    165:  * Clean up
                    166:  */
                    167: void
                    168: __exception_exit(void)
                    169: {
                    170:
                    171: #ifdef _REENTRANT
                    172:        mutex_destroy(&__sig_lock);
                    173: #endif
                    174:        exception_setup(NULL);
                    175: }

CVSweb