Back to index

glibc  2.9
sigtramp.c
Go to the documentation of this file.
00001 /* Copyright (C) 1993, 1996, 1997, 2004 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #ifndef       __GNUC__
00020   #error This file uses GNU C extensions; you must compile with GCC.
00021 #endif
00022 
00023 /* Get the definition of `struct sigcontext'.  */
00024 #define       KERNEL
00025 #define       sigvec        sun_sigvec
00026 #define       sigstack      sun_sigstack
00027 #define       sigcontext    sun_sigcontext
00028 #include "/usr/include/sys/signal.h"
00029 #undef sigvec
00030 #undef sigstack
00031 #undef sigcontext
00032 #undef NSIG
00033 #undef SIGABRT
00034 #undef SIGCLD
00035 #undef SV_ONSTACK
00036 #undef SV_RESETHAND
00037 #undef SV_INTERRUPT
00038 #undef SA_ONSTACK
00039 #undef SA_NOCLDSTOP
00040 #undef SIG_ERR
00041 #undef SIG_DFL
00042 #undef SIG_IGN
00043 #undef sigmask
00044 #undef SIG_BLOCK
00045 #undef SIG_UNBLOCK
00046 #undef SIG_SETMASK
00047 
00048 #include <signal.h>
00049 #include <stddef.h>
00050 #include <errno.h>
00051 
00052 /* Defined in __sigvec.S.  */
00053 extern int __raw_sigvec (int sig, const struct sigvec *vec,
00054                       struct sigvec *ovec);
00055 
00056 /* User-specified signal handlers.  */
00057 #define mytramp 1
00058 #ifdef mytramp
00059 static __sighandler_t handlers[NSIG];
00060 #else
00061 #define handlers _sigfunc
00062 extern __sighandler_t _sigfunc[];
00063 #endif
00064 
00065 #if mytramp
00066 
00067 /* Handler for all signals that are handled by a user-specified function.
00068    Saves and restores the general regs %g2-%g7, the %y register, and
00069    all the FPU regs (including %fsr), around calling the user's handler.  */
00070 static void
00071 trampoline (sig, code, context, addr)
00072      int sig;
00073      int code;
00074      struct sigcontext *context;
00075      void *addr;
00076 {
00077   int save[4];
00078 
00079   /* Save the call-clobbered registers.  */
00080   asm volatile ("movem%.l d0-d1/a0-a1, %0" : : "m" (save[0]));
00081 
00082   /* XXX should save/restore FP regs */
00083 
00084   /* Call the user's handler.  */
00085   (*((void (*) (int sig, int code, struct sigcontext *context,
00086               PTR addr)) handlers[sig]))
00087     (sig, code, context, addr);
00088 
00089   /* Restore the call-clobbered registers.  */
00090   asm volatile ("movem%.l %0, d0-d1/a0-a1" : : "g" (save[0]) :
00091               "d0", "d1", "a0", "a1");
00092 
00093   __sigreturn (context);
00094 }
00095 
00096 #endif
00097 
00098 int
00099 __sigvec (sig, vec, ovec)
00100      int sig;
00101      const struct sigvec *vec;
00102      struct sigvec *ovec;
00103 {
00104 #ifndef       mytramp
00105   extern void _sigtramp (int);
00106 #define       trampoline    _sigtramp
00107 #endif
00108   struct sigvec myvec;
00109   int mask;
00110   __sighandler_t ohandler;
00111 
00112   if (sig <= 0 || sig >= NSIG)
00113     {
00114       __set_errno (EINVAL);
00115       return -1;
00116     }
00117 
00118   mask = __sigblock(sigmask(sig));
00119 
00120   ohandler = handlers[sig];
00121 
00122   if (vec != NULL &&
00123       vec->sv_handler != SIG_IGN && vec->sv_handler != SIG_DFL)
00124     {
00125       handlers[sig] = vec->sv_handler;
00126       myvec = *vec;
00127       myvec.sv_handler = trampoline;
00128       vec = &myvec;
00129     }
00130 
00131   if (__raw_sigvec(sig, vec, ovec) < 0)
00132     {
00133       int save = errno;
00134       (void) __sigsetmask(mask);
00135       errno = save;
00136       return -1;
00137     }
00138 
00139   if (ovec != NULL && ovec->sv_handler == trampoline)
00140     ovec->sv_handler = ohandler;
00141 
00142   (void) __sigsetmask(mask);
00143 
00144   return 0;
00145 }