update from main archive 961217
[glibc.git] / sysdeps / posix / sigvec.c
blob59d30f2d29fdf3a3c4135e9c7195559b8d1a6ca3
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 not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, 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))
57 *(int *) set = mask;
58 else if (sizeof (*set) == sizeof (unsigned long int))
59 *(unsigned long int *) set = (unsigned int) mask;
60 else
62 if (__sigemptyset (set) < 0)
63 return -1;
65 for (sig = 1; sig < NSIG; ++sig)
66 if ((mask & sigmask (sig)) && __sigaddset (set, sig) < 0)
67 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 if (sizeof (unsigned long int) == sizeof (sigset_t))
139 mask = *(unsigned long int *) &old.sa_mask;
140 else
141 for (i = 1; i < NSIG; ++i)
142 if (__sigismember(&old.sa_mask, i))
143 mask |= sigmask(i);
145 ovec->sv_mask = mask;
146 ovec->sv_flags = 0;
147 #ifdef SA_ONSTACK
148 if (old.sa_flags & SA_ONSTACK)
149 ovec->sv_flags |= SV_ONSTACK;
150 #endif
151 #ifdef SA_RESTART
152 if (!(old.sa_flags & SA_RESTART))
153 #endif
154 ovec->sv_flags |= SV_INTERRUPT;
155 if (old.sa_handler == wrapper_handler)
157 ovec->sv_flags |= SV_RESETHAND;
158 ovec->sv_handler = wrapped_handlers[sig];
160 else
161 ovec->sv_handler = old.sa_handler;
164 return 0;
167 weak_alias (__sigvec, sigvec)