Back to index

glibc  2.9
sigaction.c
Go to the documentation of this file.
00001 /* Copyright (C) 1994,1995,1996,1997,2002 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 #include <errno.h>
00020 #include <signal.h>
00021 #include <stddef.h>
00022 
00023 static __sighandler_t user_handlers[NSIG];
00024 
00025 extern int __context_syscall (int, struct sigcontext *);
00026 extern int __sigaction_syscall (int,
00027                             const struct sigaction *, struct sigaction *);
00028 
00029 static void
00030 trampoline (int sig, int code, struct sigcontext *context)
00031 {
00032   (*(void (*) (int, int, struct sigcontext *)) user_handlers[sig])
00033     (sig, code, context);
00034   __context_syscall (1, context);
00035 }
00036 
00037 /* If ACT is not NULL, change the action for SIG to *ACT.
00038    If OACT is not NULL, put the old action for SIG in *OACT.  */
00039 int
00040 __sigaction (sig, act, oact)
00041      int sig;
00042      const struct sigaction *act;
00043      struct sigaction *oact;
00044 {
00045   struct sigaction myact;
00046   __sighandler_t ohandler;
00047 
00048   if (sig <= 0 || sig >= NSIG)
00049     {
00050       __set_errno (EINVAL);
00051       return -1;
00052     }
00053 
00054   ohandler = user_handlers[sig];
00055 
00056   if (act != NULL)
00057     {
00058       user_handlers[sig] = act->sa_handler;
00059       if (act->sa_handler != SIG_DFL && act->sa_handler != SIG_IGN)
00060        {
00061          myact = *act;
00062          act = &myact;
00063          act->sa_handler = (__sighandler_t) trampoline;
00064        }
00065     }
00066 
00067   if (__sigaction_syscall (sig, act, oact) < 0)
00068     {
00069       /* The syscall got an error.  Restore the old handler and return -1.  */
00070       user_handlers[sig] = ohandler;
00071       return -1;
00072     }
00073 
00074   if (oact != NULL && oact->sa_handler == (__sighandler_t) trampoline)
00075     oact->sa_handler = ohandler;
00076 
00077   return 0;
00078 }
00079 libc_hidden_def (__sigaction)
00080 weak_alias (__sigaction, sigaction)