Back to index

glibc  2.9
sigaction.c
Go to the documentation of this file.
00001 /* Copyright (C) 1992,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 <sysdep.h>
00020 #include <errno.h>
00021 #include <stddef.h>
00022 #include <signal.h>
00023 
00024 
00025 /* If ACT is not NULL, change the action for SIG to *ACT.
00026    If OACT is not NULL, put the old action for SIG in *OACT.  */
00027 int
00028 __sigaction (sig, act, oact)
00029      int sig;
00030      const struct sigaction *act;
00031      struct sigaction *oact;
00032 {
00033   sighandler_t handler;
00034   int save;
00035 
00036   if (sig <= 0 || sig >= NSIG)
00037     {
00038       __set_errno (EINVAL);
00039       return -1;
00040     }
00041 
00042   if (act == NULL)
00043     {
00044       if (oact == NULL)
00045        return 0;
00046       /* Race condition, but this is the only way to do it.  */
00047       handler = signal (sig, SIG_IGN);
00048       if (handler == SIG_ERR)
00049        return -1;
00050       save = errno;
00051       (void) signal (sig, handler);
00052       __set_errno (save);
00053     }
00054   else
00055     {
00056       int i;
00057 
00058       if (act->sa_flags != 0)
00059        {
00060        unimplemented:
00061          __set_errno (ENOSYS);
00062          return -1;
00063        }
00064 
00065       for (i = 1; i < NSIG; ++i)
00066        if (__sigismember (&act->sa_mask, i))
00067          goto unimplemented;
00068 
00069       handler = signal (sig, act->sa_handler);
00070       if (handler == SIG_ERR)
00071        return -1;
00072     }
00073 
00074   if (oact != NULL)
00075     {
00076       oact->sa_handler = handler;
00077       __sigemptyset (&oact->sa_mask);
00078       oact->sa_flags = 0;
00079     }
00080 
00081   return 0;
00082 }
00083 libc_hidden_def (__sigaction)
00084 weak_alias (__sigaction, sigaction)