Don't include <string.h> at all. HPUX-9.01 and IRIX-4.0.5 vendor C compilers get...
[glibc.git] / hurd / sigunwind.c
blob7420f43ae4ba916c2d0f2afc99c2dc9acda195c4
1 /* longjmp cleanup function for unwinding past signal handlers.
2 Copyright (C) 1995 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
20 #include <hurd.h>
21 #include "thread_state.h"
22 #include <setjmp.h>
23 #include <assert.h>
25 extern void _hurd_longjmp_thread_state (struct machine_thread_state *,
26 jmp_buf env, int value);
29 /* _hurd_setup_sighandler puts a link on the `active resources' chain so that
30 _longjmp_unwind will call this function with the `struct sigcontext *'
31 describing the context interrupted by the signal, when `longjmp' is jumping
32 to an environment that unwinds past the interrupted frame. */
34 void
35 _hurdsig_longjmp_from_handler (void *data, jmp_buf env, int val)
37 struct sigcontext *scp = data;
38 struct hurd_sigstate *ss = _hurd_self_sigstate ();
39 int onstack;
40 inline void cleanup (void)
42 /* Destroy the MiG reply port used by the signal handler, and restore
43 the reply port in use by the thread when interrupted. */
44 mach_port_t *reply_port =
45 (mach_port_t *) __hurd_threadvar_location (_HURD_THREADVAR_MIG_REPLY);
46 if (*reply_port)
47 __mach_port_destroy (__mach_task_self (), *reply_port);
48 *reply_port = scp->sc_reply_port;
51 __spin_lock (&ss->lock);
52 /* We should only ever be called from _longjmp_unwind (in jmp-unwind.c),
53 which calls us inside a critical section. */
54 assert (ss->critical_section);
55 /* Are we on the alternate signal stack now? */
56 onstack = (ss->sigaltstack.ss_flags & SA_ONSTACK);
57 __spin_unlock (&ss->lock);
59 if (onstack && ! scp->sc_onstack)
61 /* We are unwinding off the signal stack. We must use sigreturn to
62 do it robustly. Mutate the sigcontext so that when sigreturn
63 resumes from that context, it will be as if `__longjmp (ENV, VAL)'
64 were done. */
66 struct hurd_userlink *link;
68 /* Continue _longjmp_unwind's job of running the unwind
69 forms for frames being unwound, since we will not
70 return to its loop like this one, which called us. */
71 for (link = ss->active_resources;
72 link && _JMPBUF_UNWINDS (env[0].__jmpbuf, link);
73 link = link->thread.next)
74 if (_hurd_userlink_unlink (link))
76 if (link->cleanup == &_hurdsig_longjmp_from_handler)
78 /* We are unwinding past another signal handler invocation.
79 Just finish the cleanup for this (inner) one, and then
80 swap SCP to restore to the outer context. */
81 cleanup ();
82 scp = link->cleanup_data;
84 else
85 (*link->cleanup) (link->cleanup_data, env, val);
88 #define sc_machine_thread_state paste(sc_,machine_thread_state)
89 #define paste(a,b) paste1(a,b)
90 #define paste1(a,b) a##b
92 /* There are no more unwind forms to be run!
93 Now we can just have the sigreturn do the longjmp for us. */
94 _hurd_longjmp_thread_state
95 ((struct machine_thread_state *) &scp->sc_machine_thread_state,
96 env, val);
98 /* Restore to the same current signal mask. If sigsetjmp saved the
99 mask, longjmp has already restored it as desired; if not, we
100 should leave it as it is. */
101 scp->sc_mask = ss->blocked;
103 /* sigreturn expects the link added by _hurd_setup_sighandler
104 to still be there, but _longjmp_unwind removed it just before
105 calling us. Put it back now so sigreturn can find it. */
106 link = (void *) &scp[1];
107 assert (! link->resource.next && ! link->resource.prevp);
108 assert (link->thread.next == ss->active_resources);
109 assert (link->thread.prevp = &ss->active_resources);
110 if (link->thread.next)
111 link->thread.next->thread.prevp = &link->thread.next;
112 ss->active_resources = link;
114 /* We must momentarily exit the critical section so that sigreturn
115 does not get upset with us. But we don't want signal handlers
116 running right now, because we are presently in the bogus state of
117 having run all the unwind forms back to ENV's frame, but our SP is
118 still inside those unwound frames. */
119 __spin_lock (&ss->lock);
120 ss->critical_section = 0;
121 ss->blocked = ~(sigset_t) 0 & ~_SIG_CANT_MASK;
122 __spin_unlock (&ss->lock);
124 /* Restore to the modified signal context that now
125 performs `longjmp (ENV, VAL)'. */
126 __sigreturn (scp);
127 assert (! "sigreturn returned!");
130 /* We are not unwinding off the alternate signal stack. So nothing
131 really funny is going on here. We can just clean up this handler
132 frame and let _longjmp_unwind continue unwinding. */
133 cleanup ();
134 ss->intr_port = scp->sc_intr_port;