Replace FSF snail mail address with URLs.
[glibc.git] / hurd / catch-signal.c
blob50cd36600d74f65e033caa5e6750fe4b27786671
1 /* Convenience function to catch expected signals during an operation.
2 Copyright (C) 1996 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <hurd/signal.h>
20 #include <hurd/sigpreempt.h>
21 #include <string.h>
22 #include <assert.h>
24 error_t
25 hurd_catch_signal (sigset_t sigset,
26 unsigned long int first, unsigned long int last,
27 error_t (*operate) (struct hurd_signal_preemptor *),
28 sighandler_t handler)
30 jmp_buf buf;
31 void throw (int signo, long int sigcode, struct sigcontext *scp)
32 { longjmp (buf, scp->sc_error ?: EGRATUITOUS); }
34 struct hurd_signal_preemptor preemptor =
36 sigset, first, last,
37 NULL, handler == SIG_ERR ? (sighandler_t) &throw : handler,
40 struct hurd_sigstate *const ss = _hurd_self_sigstate ();
41 error_t error;
43 if (handler == SIG_ERR)
44 /* Not our handler; don't bother saving state. */
45 error = 0;
46 else
47 /* This returns again with nonzero value when we preempt a signal. */
48 error = setjmp (buf);
50 if (error == 0)
52 /* Install a signal preemptor for the thread. */
53 __spin_lock (&ss->lock);
54 preemptor.next = ss->preemptors;
55 ss->preemptors = &preemptor;
56 __spin_unlock (&ss->lock);
58 /* Try the operation that might crash. */
59 (*operate) (&preemptor);
62 /* Either FUNCTION completed happily and ERROR is still zero, or it hit
63 an expected signal and `throw' made setjmp return the signal error
64 code in ERROR. Now we can remove the preemptor and return. */
66 __spin_lock (&ss->lock);
67 assert (ss->preemptors == &preemptor);
68 ss->preemptors = preemptor.next;
69 __spin_unlock (&ss->lock);
71 return error;
75 error_t
76 hurd_safe_memset (void *dest, int byte, size_t nbytes)
78 error_t operate (struct hurd_signal_preemptor *preemptor)
80 memset (dest, byte, nbytes);
81 return 0;
83 return hurd_catch_signal (sigmask (SIGBUS) | sigmask (SIGSEGV),
84 (vm_address_t) dest, (vm_address_t) dest + nbytes,
85 &operate, SIG_ERR);
89 error_t
90 hurd_safe_copyout (void *dest, const void *src, size_t nbytes)
92 error_t operate (struct hurd_signal_preemptor *preemptor)
94 memcpy (dest, src, nbytes);
95 return 0;
97 return hurd_catch_signal (sigmask (SIGBUS) | sigmask (SIGSEGV),
98 (vm_address_t) dest, (vm_address_t) dest + nbytes,
99 &operate, SIG_ERR);
102 error_t
103 hurd_safe_copyin (void *dest, const void *src, size_t nbytes)
105 error_t operate (struct hurd_signal_preemptor *preemptor)
107 memcpy (dest, src, nbytes);
108 return 0;
110 return hurd_catch_signal (sigmask (SIGBUS) | sigmask (SIGSEGV),
111 (vm_address_t) src, (vm_address_t) src + nbytes,
112 &operate, SIG_ERR);
115 error_t
116 hurd_safe_memmove (void *dest, const void *src, size_t nbytes)
118 jmp_buf buf;
119 void throw (int signo, long int sigcode, struct sigcontext *scp)
120 { longjmp (buf, scp->sc_error ?: EGRATUITOUS); }
122 struct hurd_signal_preemptor src_preemptor =
124 sigmask (SIGBUS) | sigmask (SIGSEGV),
125 (vm_address_t) src, (vm_address_t) src + nbytes,
126 NULL, (sighandler_t) &throw,
128 struct hurd_signal_preemptor dest_preemptor =
130 sigmask (SIGBUS) | sigmask (SIGSEGV),
131 (vm_address_t) dest, (vm_address_t) dest + nbytes,
132 NULL, (sighandler_t) &throw,
133 &src_preemptor
136 struct hurd_sigstate *const ss = _hurd_self_sigstate ();
137 error_t error;
139 /* This returns again with nonzero value when we preempt a signal. */
140 error = setjmp (buf);
142 if (error == 0)
144 /* Install a signal preemptor for the thread. */
145 __spin_lock (&ss->lock);
146 src_preemptor.next = ss->preemptors;
147 ss->preemptors = &dest_preemptor;
148 __spin_unlock (&ss->lock);
150 /* Do the copy; it might fault. */
151 memmove (dest, src, nbytes);
154 /* Either memmove completed happily and ERROR is still zero, or it hit
155 an expected signal and `throw' made setjmp return the signal error
156 code in ERROR. Now we can remove the preemptor and return. */
158 __spin_lock (&ss->lock);
159 assert (ss->preemptors == &dest_preemptor);
160 ss->preemptors = src_preemptor.next;
161 __spin_unlock (&ss->lock);
163 return error;