update from main archive 961001
[glibc.git] / sysdeps / posix / sigvec.c
blob7e9aeb368eac63dfac0b00395d53f0c6b7332e09
1 /* Copyright (C) 1991, 92, 94, 95, 96 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
19 #include <signal.h>
20 #include <errno.h>
21 #include <stddef.h>
24 /* We use a wrapper handler to support SV_RESETHAND. */
26 static __sighandler_t wrapped_handlers[NSIG];
27 static sigset_t wrapped_masks[NSIG];
29 static void wrapper_handler __P ((int sig));
30 static inline int convert_mask __P ((sigset_t *set, const int mask));
32 static void
33 wrapper_handler (sig)
34 int sig;
36 int save;
37 struct sigaction act;
39 act.sa_handler = SIG_DFL;
40 act.sa_mask = wrapped_masks[sig];
41 act.sa_flags = 0;
42 save = errno;
43 (void) __sigaction (sig, &act, (struct sigaction *) NULL);
44 __set_errno (save);
46 (*wrapped_handlers[sig]) (sig);
49 static inline int
50 convert_mask (set, mask)
51 sigset_t *set;
52 const int mask;
54 register int sig;
56 if (sizeof (*set) == sizeof (mask))
58 *(int *) set = mask;
59 return 0;
62 if (__sigemptyset (set) < 0)
63 return -1;
65 for (sig = 1; sig < NSIG; ++sig)
66 if (mask & sigmask (sig))
67 if (__sigaddset (set, sig) < 0)
68 return -1;
70 return 0;
73 /* If VEC is non-NULL, set the handler for SIG to the `sv_handler' member
74 of VEC. The signals in `sv_mask' will be blocked while the handler runs.
75 If the SV_RESETHAND bit is set in `sv_flags', the handler for SIG will be
76 reset to SIG_DFL before `sv_handler' is entered. If OVEC is non-NULL,
77 it is filled in with the old information for SIG. */
78 int
79 __sigvec (sig, vec, ovec)
80 int sig;
81 const struct sigvec *vec;
82 struct sigvec *ovec;
84 struct sigaction old;
86 if (vec == NULL || !(vec->sv_flags & SV_RESETHAND))
88 struct sigaction new, *n;
90 if (vec == NULL)
91 n = NULL;
92 else
94 n = &new;
95 n->sa_handler = vec->sv_handler;
96 if (convert_mask (&n->sa_mask, vec->sv_mask) < 0)
97 return -1;
98 n->sa_flags = 0;
100 if (vec->sv_flags & SV_ONSTACK)
102 #ifdef SA_ONSTACK
103 n->sa_flags |= SA_ONSTACK;
104 #else
105 __set_errno (ENOSYS);
106 return -1;
107 #endif
109 #ifdef SA_RESTART
110 if (!(vec->sv_flags & SV_INTERRUPT))
111 n->sa_flags |= SA_RESTART;
112 #endif
115 if (__sigaction (sig, n, &old) < 0)
116 return -1;
118 else
120 struct sigaction wrapper;
122 wrapper.sa_handler = wrapper_handler;
123 wrapped_handlers[sig] = vec->sv_handler;
124 if (convert_mask (&wrapped_masks[sig], vec->sv_mask) < 0)
125 return -1;
127 if (__sigaction (sig, &wrapper, &old) < 0)
128 return -1;
131 if (ovec != NULL)
133 register int i;
134 int mask = 0;
136 if (sizeof (int) == sizeof (sigset_t))
137 mask = *(int *) &old.sa_mask;
138 else
139 for (i = 1; i < NSIG; ++i)
140 if (__sigismember(&old.sa_mask, i))
141 mask |= sigmask(i);
143 ovec->sv_mask = mask;
144 ovec->sv_flags = 0;
145 #ifdef SA_ONSTACK
146 if (old.sa_flags & SA_ONSTACK)
147 ovec->sv_flags |= SV_ONSTACK;
148 #endif
149 #ifdef SA_RESTART
150 if (!(old.sa_flags & SA_RESTART))
151 #endif
152 ovec->sv_flags |= SV_INTERRUPT;
153 if (old.sa_handler == wrapper_handler)
155 ovec->sv_flags |= SV_RESETHAND;
156 ovec->sv_handler = wrapped_handlers[sig];
158 else
159 ovec->sv_handler = old.sa_handler;
162 return 0;
165 weak_alias (__sigvec, sigvec)