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 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/signal.h>
21 #include <hurd/sigpreempt.h>
26 hurd_catch_signal (sigset_t sigset,
27 unsigned long int first, unsigned long int last,
28 error_t (*operate) (struct hurd_signal_preempter *),
32 void throw (int signo, long int sigcode, struct sigcontext *scp)
33 { longjmp (buf, scp->sc_error ?: EGRATUITOUS); }
35 struct hurd_signal_preempter preempter =
38 NULL, handler == SIG_ERR ? (sighandler_t) &throw : handler,
41 struct hurd_sigstate *const ss = _hurd_self_sigstate ();
44 if (handler == SIG_ERR)
45 /* Not our handler; don't bother saving state. */
48 /* This returns again with nonzero value when we preempt a signal. */
53 /* Install a signal preempter for the thread. */
54 __spin_lock (&ss->lock);
55 preempter.next = ss->preempters;
56 ss->preempters = &preempter;
57 __spin_unlock (&ss->lock);
59 /* Try the operation that might crash. */
60 (*operate) (&preempter);
63 /* Either FUNCTION completed happily and ERROR is still zero, or it hit
64 an expected signal and `throw' made setjmp return the signal error
65 code in ERROR. Now we can remove the preempter and return. */
67 __spin_lock (&ss->lock);
68 assert (ss->preempters == &preempter);
69 ss->preempters = preempter.next;
70 __spin_unlock (&ss->lock);
77 hurd_safe_memset (void *dest, int byte, size_t nbytes)
79 error_t operate (struct hurd_signal_preempter *preempter)
81 memset (dest, byte, nbytes);
84 return hurd_catch_signal (sigmask (SIGBUS) | sigmask (SIGSEGV),
85 (vm_address_t) dest, (vm_address_t) dest + nbytes,
91 hurd_safe_copyout (void *dest, const void *src, size_t nbytes)
93 error_t operate (struct hurd_signal_preempter *preempter)
95 memcpy (dest, src, nbytes);
98 return hurd_catch_signal (sigmask (SIGBUS) | sigmask (SIGSEGV),
99 (vm_address_t) dest, (vm_address_t) dest + nbytes,
104 hurd_safe_copyin (void *dest, const void *src, size_t nbytes)
106 error_t operate (struct hurd_signal_preempter *preempter)
108 memcpy (dest, src, nbytes);
111 return hurd_catch_signal (sigmask (SIGBUS) | sigmask (SIGSEGV),
112 (vm_address_t) src, (vm_address_t) src + nbytes,
117 hurd_safe_memmove (void *dest, const void *src, size_t nbytes)
120 void throw (int signo, long int sigcode, struct sigcontext *scp)
121 { longjmp (buf, scp->sc_error ?: EGRATUITOUS); }
123 struct hurd_signal_preempter src_preempter =
125 sigmask (SIGBUS) | sigmask (SIGSEGV),
126 (vm_address_t) src, (vm_address_t) src + nbytes,
127 NULL, (sighandler_t) &throw,
129 struct hurd_signal_preempter dest_preempter =
131 sigmask (SIGBUS) | sigmask (SIGSEGV),
132 (vm_address_t) dest, (vm_address_t) dest + nbytes,
133 NULL, (sighandler_t) &throw,
137 struct hurd_sigstate *const ss = _hurd_self_sigstate ();
140 /* This returns again with nonzero value when we preempt a signal. */
141 error = setjmp (buf);
145 /* Install a signal preempter for the thread. */
146 __spin_lock (&ss->lock);
147 src_preempter.next = ss->preempters;
148 ss->preempters = &dest_preempter;
149 __spin_unlock (&ss->lock);
151 /* Do the copy; it might fault. */
152 memmove (dest, src, nbytes);
155 /* Either memmove completed happily and ERROR is still zero, or it hit
156 an expected signal and `throw' made setjmp return the signal error
157 code in ERROR. Now we can remove the preempter and return. */
159 __spin_lock (&ss->lock);
160 assert (ss->preempters == &dest_preempter);
161 ss->preempters = src_preempter.next;
162 __spin_unlock (&ss->lock);