]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/posix/sigvec.c
* sysdeps/generic/sigset-cvt-mask.h (sigset_set_old_mask): Replace
[thirdparty/glibc.git] / sysdeps / posix / sigvec.c
1 /* Copyright (C) 1991,92,94,95,96,97,98,2002 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, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
18
19 #include <signal.h>
20 #include <errno.h>
21 #include <stddef.h>
22
23 /* Include macros to convert between `sigset_t' and old-style mask. */
24 #include <sigset-cvt-mask.h>
25
26 /* We use a wrapper handler to support SV_RESETHAND. */
27 struct sigvec_wrapper_data
28 {
29 __sighandler_t sw_handler;
30 unsigned int sw_mask;
31 };
32
33 static void sigvec_wrapper_handler __P ((int sig));
34
35 static struct sigvec_wrapper_data sigvec_wrapper_data[NSIG];
36
37
38 /* If VEC is non-NULL, set the handler for SIG to the `sv_handler' member
39 of VEC. The signals in `sv_mask' will be blocked while the handler runs.
40 If the SV_RESETHAND bit is set in `sv_flags', the handler for SIG will be
41 reset to SIG_DFL before `sv_handler' is entered. If OVEC is non-NULL,
42 it is filled in with the old information for SIG. */
43 int
44 __sigvec (sig, vec, ovec)
45 int sig;
46 const struct sigvec *vec;
47 struct sigvec *ovec;
48 {
49 struct sigaction old;
50
51 if (vec == NULL || !(vec->sv_flags & SV_RESETHAND))
52 {
53 struct sigaction new, *n;
54
55 if (vec == NULL)
56 n = NULL;
57 else
58 {
59 __sighandler_t handler;
60 unsigned int mask;
61 unsigned int sv_flags;
62 unsigned int sa_flags;
63
64 handler = vec->sv_handler;
65 mask = vec->sv_mask;
66 sv_flags = vec->sv_flags;
67 sa_flags = 0;
68 if (sv_flags & SV_ONSTACK)
69 {
70 #ifdef SA_ONSTACK
71 sa_flags |= SA_ONSTACK;
72 #else
73 __set_errno (ENOSYS);
74 return -1;
75 #endif
76 }
77 #ifdef SA_RESTART
78 if (!(sv_flags & SV_INTERRUPT))
79 sa_flags |= SA_RESTART;
80 #endif
81 n = &new;
82 new.sa_handler = handler;
83 if (sigset_set_old_mask (&new.sa_mask, mask) < 0)
84 return -1;
85 new.sa_flags = sa_flags;
86 }
87
88 if (__sigaction (sig, n, &old) < 0)
89 return -1;
90 }
91 else
92 {
93 __sighandler_t handler;
94 unsigned int mask;
95 struct sigvec_wrapper_data *data;
96 struct sigaction wrapper;
97
98 handler = vec->sv_handler;
99 mask = (unsigned int)vec->sv_mask;
100 data = &sigvec_wrapper_data[sig];
101 wrapper.sa_handler = sigvec_wrapper_handler;
102 /* FIXME: should we set wrapper.sa_mask, wrapper.sa_flags?? */
103 data->sw_handler = handler;
104 data->sw_mask = mask;
105
106 if (__sigaction (sig, &wrapper, &old) < 0)
107 return -1;
108 }
109
110 if (ovec != NULL)
111 {
112 __sighandler_t handler;
113 unsigned int sv_flags;
114 unsigned int sa_flags;
115 unsigned int mask;
116
117 handler = old.sa_handler;
118 sv_flags = 0;
119 sa_flags = old.sa_flags;
120 if (handler == sigvec_wrapper_handler)
121 {
122 handler = sigvec_wrapper_data[sig].sw_handler;
123 /* should we use data->sw_mask?? */
124 sv_flags |= SV_RESETHAND;
125 }
126 mask = sigset_get_old_mask (&old.sa_mask);
127 #ifdef SA_ONSTACK
128 if (sa_flags & SA_ONSTACK)
129 sv_flags |= SV_ONSTACK;
130 #endif
131 #ifdef SA_RESTART
132 if (!(sa_flags & SA_RESTART))
133 #endif
134 sv_flags |= SV_INTERRUPT;
135 ovec->sv_handler = handler;
136 ovec->sv_mask = (int)mask;
137 ovec->sv_flags = (int)sv_flags;
138 }
139
140 return 0;
141 }
142
143 weak_alias (__sigvec, sigvec)
144
145
146 static void
147 sigvec_wrapper_handler (sig)
148 int sig;
149 {
150 struct sigvec_wrapper_data *data;
151 struct sigaction act;
152 int save;
153 __sighandler_t handler;
154
155 data = &sigvec_wrapper_data[sig];
156 act.sa_handler = SIG_DFL;
157 act.sa_flags = 0;
158 sigset_set_old_mask (&act.sa_mask, data->sw_mask);
159 handler = data->sw_handler;
160 save = errno;
161 (void) __sigaction (sig, &act, (struct sigaction *) NULL);
162 __set_errno (save);
163
164 (*handler) (sig);
165 }