signal: Add __libc_sigaction
[platform/upstream/glibc.git] / sysdeps / unix / sysv / linux / libc_sigaction.c
1 /* Copyright (C) 1997-2021 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <https://www.gnu.org/licenses/>.  */
17
18 #include <errno.h>
19 #include <signal.h>
20 #include <string.h>
21
22 #include <sysdep.h>
23 #include <sys/syscall.h>
24
25 /* New ports should not define the obsolete SA_RESTORER, however some
26    architecture requires for compat mode and/or due old ABI.  */
27 #include <kernel_sigaction.h>
28
29 #ifndef SA_RESTORER
30 # define SET_SA_RESTORER(kact, act)
31 # define RESET_SA_RESTORER(act, kact)
32 #endif
33
34 /* SPARC passes the restore function as an argument to rt_sigaction.  */
35 #ifndef STUB
36 # define STUB(act, sigsetsize) (sigsetsize)
37 #endif
38
39 /* If ACT is not NULL, change the action for SIG to *ACT.
40    If OACT is not NULL, put the old action for SIG in *OACT.  */
41 int
42 __libc_sigaction (int sig, const struct sigaction *act, struct sigaction *oact)
43 {
44   int result;
45
46   struct kernel_sigaction kact, koact;
47
48   if (act)
49     {
50       kact.k_sa_handler = act->sa_handler;
51       memcpy (&kact.sa_mask, &act->sa_mask, sizeof (sigset_t));
52       kact.sa_flags = act->sa_flags;
53       SET_SA_RESTORER (&kact, act);
54     }
55
56   /* XXX The size argument hopefully will have to be changed to the
57      real size of the user-level sigset_t.  */
58   result = INLINE_SYSCALL_CALL (rt_sigaction, sig,
59                                 act ? &kact : NULL,
60                                 oact ? &koact : NULL, STUB (act,
61                                                             __NSIG_BYTES));
62
63   if (oact && result >= 0)
64     {
65       oact->sa_handler = koact.k_sa_handler;
66       memcpy (&oact->sa_mask, &koact.sa_mask, sizeof (sigset_t));
67       oact->sa_flags = koact.sa_flags;
68       RESET_SA_RESTORER (oact, &koact);
69     }
70   return result;
71 }
72 libc_hidden_def (__libc_sigaction)