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>
25 hurd_catch_signal (sigset_t sigset
,
26 unsigned long int first
, unsigned long int last
,
27 error_t (*operate
) (struct hurd_signal_preemptor
*),
31 void throw (int signo
, long int sigcode
, struct sigcontext
*scp
)
32 { longjmp (buf
, scp
->sc_error
?: EGRATUITOUS
); }
34 struct hurd_signal_preemptor preemptor
=
37 NULL
, handler
== SIG_ERR
? (sighandler_t
) &throw : handler
,
40 struct hurd_sigstate
*const ss
= _hurd_self_sigstate ();
43 if (handler
== SIG_ERR
)
44 /* Not our handler; don't bother saving state. */
47 /* This returns again with nonzero value when we preempt a signal. */
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
);
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
);
83 return hurd_catch_signal (sigmask (SIGBUS
) | sigmask (SIGSEGV
),
84 (vm_address_t
) dest
, (vm_address_t
) dest
+ nbytes
,
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
);
97 return hurd_catch_signal (sigmask (SIGBUS
) | sigmask (SIGSEGV
),
98 (vm_address_t
) dest
, (vm_address_t
) dest
+ nbytes
,
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
);
110 return hurd_catch_signal (sigmask (SIGBUS
) | sigmask (SIGSEGV
),
111 (vm_address_t
) src
, (vm_address_t
) src
+ nbytes
,
116 hurd_safe_memmove (void *dest
, const void *src
, size_t nbytes
)
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,
136 struct hurd_sigstate
*const ss
= _hurd_self_sigstate ();
139 /* This returns again with nonzero value when we preempt a signal. */
140 error
= setjmp (buf
);
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
);