Remove _hurd_longjmp_thread_state decl.
[glibc.git] / hurd / sigunwind.c
blob23309fd0d7d0d0af343950247b69b2e3580c4bd5
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>
26 /* _hurd_setup_sighandler puts a link on the `active resources' chain so that
27 _longjmp_unwind will call this function with the `struct sigcontext *'
28 describing the context interrupted by the signal, when `longjmp' is jumping
29 to an environment that unwinds past the interrupted frame. */
31 void
32 _hurdsig_longjmp_from_handler (void *data, jmp_buf env, int val)
34 struct sigcontext *scp = data;
35 struct hurd_sigstate *ss = _hurd_self_sigstate ();
36 int onstack;
37 inline void cleanup (void)
39 /* Destroy the MiG reply port used by the signal handler, and restore
40 the reply port in use by the thread when interrupted. */
41 mach_port_t *reply_port =
42 (mach_port_t *) __hurd_threadvar_location (_HURD_THREADVAR_MIG_REPLY);
43 if (*reply_port)
44 __mach_port_destroy (__mach_task_self (), *reply_port);
45 *reply_port = scp->sc_reply_port;
48 __spin_lock (&ss->lock);
49 /* We should only ever be called from _longjmp_unwind (in jmp-unwind.c),
50 which calls us inside a critical section. */
51 assert (ss->critical_section);
52 /* Are we on the alternate signal stack now? */
53 onstack = (ss->sigaltstack.ss_flags & SA_ONSTACK);
54 __spin_unlock (&ss->lock);
56 if (onstack && ! scp->sc_onstack)
58 /* We are unwinding off the signal stack. We must use sigreturn to
59 do it robustly. Mutate the sigcontext so that when sigreturn
60 resumes from that context, it will be as if `__longjmp (ENV, VAL)'
61 were done. */
63 struct hurd_userlink *link;
65 /* Continue _longjmp_unwind's job of running the unwind
66 forms for frames being unwound, since we will not
67 return to its loop like this one, which called us. */
68 for (link = ss->active_resources;
69 link && _JMPBUF_UNWINDS (env[0].__jmpbuf, link);
70 link = link->thread.next)
71 if (_hurd_userlink_unlink (link))
73 if (link->cleanup == &_hurdsig_longjmp_from_handler)
75 /* We are unwinding past another signal handler invocation.
76 Just finish the cleanup for this (inner) one, and then
77 swap SCP to restore to the outer context. */
78 cleanup ();
79 scp = link->cleanup_data;
81 else
82 (*link->cleanup) (link->cleanup_data, env, val);
85 #define sc_machine_thread_state paste(sc_,machine_thread_state)
86 #define paste(a,b) paste1(a,b)
87 #define paste1(a,b) a##b
89 /* There are no more unwind forms to be run!
90 Now we can just have the sigreturn do the longjmp for us. */
91 _hurd_longjmp_thread_state
92 ((struct machine_thread_state *) &scp->sc_machine_thread_state,
93 env, val);
95 /* Restore to the same current signal mask. If sigsetjmp saved the
96 mask, longjmp has already restored it as desired; if not, we
97 should leave it as it is. */
98 scp->sc_mask = ss->blocked;
100 /* sigreturn expects the link added by _hurd_setup_sighandler
101 to still be there, but _longjmp_unwind removed it just before
102 calling us. Put it back now so sigreturn can find it. */
103 link = (void *) &scp[1];
104 assert (! link->resource.next && ! link->resource.prevp);
105 assert (link->thread.next == ss->active_resources);
106 assert (link->thread.prevp = &ss->active_resources);
107 if (link->thread.next)
108 link->thread.next->thread.prevp = &link->thread.next;
109 ss->active_resources = link;
111 /* We must momentarily exit the critical section so that sigreturn
112 does not get upset with us. But we don't want signal handlers
113 running right now, because we are presently in the bogus state of
114 having run all the unwind forms back to ENV's frame, but our SP is
115 still inside those unwound frames. */
116 __spin_lock (&ss->lock);
117 ss->critical_section = 0;
118 ss->blocked = ~(sigset_t) 0 & ~_SIG_CANT_MASK;
119 __spin_unlock (&ss->lock);
121 /* Restore to the modified signal context that now
122 performs `longjmp (ENV, VAL)'. */
123 __sigreturn (scp);
124 assert (! "sigreturn returned!");
127 /* We are not unwinding off the alternate signal stack. So nothing
128 really funny is going on here. We can just clean up this handler
129 frame and let _longjmp_unwind continue unwinding. */
130 cleanup ();
131 ss->intr_port = scp->sc_intr_port;