Update.
[platform/upstream/linaro-glibc.git] / sysdeps / unix / sysv / linux / i386 / sigaction.c
1 /* POSIX.1 `sigaction' call for Linux/i386.
2    Copyright (C) 1991, 1995, 1996, 1997 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public
16    License along with the GNU C Library; see the file COPYING.LIB.  If not,
17    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18    Boston, MA 02111-1307, USA.  */
19
20 #include <sysdep.h>
21 #include <errno.h>
22 #include <stddef.h>
23 #include <signal.h>
24
25 /* The difference here is that the sigaction structure used in the
26    kernel is not the same as we use in the libc.  Therefore we must
27    translate it here.  */
28 #include <kernel_sigaction.h>
29
30
31 /* If ACT is not NULL, change the action for SIG to *ACT.
32    If OACT is not NULL, put the old action for SIG in *OACT.  */
33 int
34 __sigaction (int sig, const struct sigaction *act, struct sigaction *oact)
35 {
36   struct kernel_sigaction k_newact, k_oldact;
37   int result;
38
39   if (act)
40     {
41       k_newact.sa_handler = act->sa_handler;
42       k_newact.sa_mask = act->sa_mask.__val[0];
43       k_newact.sa_flags = act->sa_flags;
44
45       k_newact.sa_restorer = ((act->sa_flags & SA_NOMASK)
46                               ? &&restore_nomask : &&restore);
47     }
48
49   asm volatile ("pushl %%ebx\n"
50                 "movl %2, %%ebx\n"
51                 "int $0x80\n"
52                 "popl %%ebx"
53                 : "=a" (result)
54                 : "0" (SYS_ify (sigaction)), "r" (sig),
55                   "c" (act ? &k_newact : 0), "d" (oact ? &k_oldact : 0));
56
57   if (result < 0)
58     {
59       __set_errno (-result);
60       return -1;
61     }
62
63   if (oact)
64     {
65       oact->sa_handler = k_oldact.sa_handler;
66       oact->sa_mask.__val[0] = k_oldact.sa_mask;
67       oact->sa_flags = k_oldact.sa_flags;
68       oact->sa_restorer = k_oldact.sa_restorer;
69     }
70
71   return 0;
72
73  restore:
74   asm (
75 #ifdef  PIC
76        "        pushl %%ebx\n"
77        "        call 0f\n"
78        "0:      popl %%ebx\n"
79        "        addl $_GLOBAL_OFFSET_TABLE_+[.-0b], %%ebx\n"
80        "        addl $8, %%esp\n"
81        "        call __sigsetmask@PLT\n"
82        "        addl $8, %%esp\n"
83        "        popl %%ebx\n"
84 #else
85        "        addl $4, %%esp\n"
86        "        call __sigsetmask\n"
87        "        addl $4, %%esp\n"
88 #endif
89        "        popl %%eax\n"
90        "        popl %%ecx\n"
91        "        popl %%edx\n"
92        "        popf\n"
93        "        ret"
94        : : );
95
96  restore_nomask:
97   asm ("        addl $4, %%esp\n"
98        "        popl %%eax\n"
99        "        popl %%ecx\n"
100        "        popl %%edx\n"
101        "        popf\n"
102        "        ret"
103        : : );
104
105   /* NOTREACHED */
106   return -1;
107 }
108
109 weak_alias (__sigaction, sigaction)