HAMMER utilities: feature add.
[dfdiff.git] / lib / libc / gen / ucontext.c
blobb2247da696e318a480b2f518494954775f166359
1 /*
2 * Copyright (c) 2007 Matthew T. Emmerton <matt@gsicomp.on.ca>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Neither the name of the author nor the names of its contributors
11 * may be used to endorse or promote products derived from this software
12 * without specific prior written permission.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
26 * $DragonFly: src/lib/libc/gen/ucontext.c,v 1.1 2007/01/16 07:16:23 dillon Exp $
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/signal.h>
32 #include <sys/ucontext.h>
34 #include <errno.h>
35 #include <stdarg.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 #include <signal.h>
40 /* Prototypes */
41 __weak_reference(_swapcontext, swapcontext);
42 __weak_reference(_setcontext, setcontext);
44 int get_mcontext(mcontext_t *);
45 int set_mcontext(const mcontext_t *);
48 * We need to block most signals during a context switch so we do not
49 * dispatch a signal vector during a context switch.
51 static sigset_t sigset_block_all;
53 static void __sigset_block_all_setup(void) __attribute__ ((constructor));
55 static void
56 __sigset_block_all_setup(void)
58 sigfillset(&sigset_block_all);
59 sigdelset(&sigset_block_all, SIGSEGV);
60 sigdelset(&sigset_block_all, SIGBUS);
61 sigdelset(&sigset_block_all, SIGILL);
65 * Save the calling context in (oucp) then switch to (ucp).
67 * Block all signals while switching contexts. get_mcontext() returns zero
68 * when retrieving a context.
70 * When some other thread calls set_mcontext() to resume our thread,
71 * the resume point causes get_mcontext() to return non-zero to us.
72 * Signals will be blocked and we must restore the signal mask before
73 * returning.
75 int
76 _swapcontext(ucontext_t *oucp, const ucontext_t *ucp)
78 int ret;
80 ret = _sigprocmask(SIG_BLOCK, &sigset_block_all, &oucp->uc_sigmask);
81 if (ret == 0) {
82 if (get_mcontext(&oucp->uc_mcontext) == 0) {
83 ret = set_mcontext(&ucp->uc_mcontext);
84 } else {
85 ret = _sigprocmask(SIG_SETMASK, &oucp->uc_sigmask, NULL);
88 return(ret);
92 * Switch to the target context. The current signal mask is saved in ucp
93 * and all signals are blocked. The call to set_mcontext() causes the
94 * specified context to be switched to (usually resuming as a return from
95 * the get_mcontext() procedure). The current context is thrown away.
97 * The target context being resumed is responsible for restoring the
98 * signal mask appropriate for the target context.
101 _setcontext(ucontext_t *ucp)
103 int ret;
105 ret = _sigprocmask(SIG_BLOCK, &sigset_block_all, &ucp->uc_sigmask);
106 if (ret == 0)
107 ret = set_mcontext(&ucp->uc_mcontext);
108 return(ret);