1 /* Convenience function to catch expected signals during an operation.
2 Copyright (C) 1996-2017 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
*),
30 /* We need to restore the signal mask, because otherwise the
31 signal-handling code will have blocked the caught signal and for
32 instance calling hurd_catch_signal again would then dump core. */
34 void throw (int signo
, long int sigcode
, struct sigcontext
*scp
)
35 { siglongjmp (buf
, scp
->sc_error
?: EGRATUITOUS
); }
37 struct hurd_signal_preemptor preemptor
=
40 NULL
, handler
== SIG_ERR
? (sighandler_t
) &throw : handler
,
43 struct hurd_sigstate
*const ss
= _hurd_self_sigstate ();
46 if (handler
!= SIG_ERR
)
47 /* Not our handler; don't bother saving state. */
50 /* This returns again with nonzero value when we preempt a signal. */
51 error
= sigsetjmp (buf
, 1);
55 /* Install a signal preemptor for the thread. */
56 __spin_lock (&ss
->lock
);
57 preemptor
.next
= ss
->preemptors
;
58 ss
->preemptors
= &preemptor
;
59 __spin_unlock (&ss
->lock
);
61 /* Try the operation that might crash. */
62 (*operate
) (&preemptor
);
65 /* Either FUNCTION completed happily and ERROR is still zero, or it hit
66 an expected signal and `throw' made setjmp return the signal error
67 code in ERROR. Now we can remove the preemptor and return. */
69 __spin_lock (&ss
->lock
);
70 assert (ss
->preemptors
== &preemptor
);
71 ss
->preemptors
= preemptor
.next
;
72 __spin_unlock (&ss
->lock
);
79 hurd_safe_memset (void *dest
, int byte
, size_t nbytes
)
81 error_t
operate (struct hurd_signal_preemptor
*preemptor
)
83 memset (dest
, byte
, nbytes
);
86 return hurd_catch_signal (sigmask (SIGBUS
) | sigmask (SIGSEGV
),
87 (vm_address_t
) dest
, (vm_address_t
) dest
+ nbytes
,
93 hurd_safe_copyout (void *dest
, const void *src
, size_t nbytes
)
95 error_t
operate (struct hurd_signal_preemptor
*preemptor
)
97 memcpy (dest
, src
, nbytes
);
100 return hurd_catch_signal (sigmask (SIGBUS
) | sigmask (SIGSEGV
),
101 (vm_address_t
) dest
, (vm_address_t
) dest
+ nbytes
,
106 hurd_safe_copyin (void *dest
, const void *src
, size_t nbytes
)
108 error_t
operate (struct hurd_signal_preemptor
*preemptor
)
110 memcpy (dest
, src
, nbytes
);
113 return hurd_catch_signal (sigmask (SIGBUS
) | sigmask (SIGSEGV
),
114 (vm_address_t
) src
, (vm_address_t
) src
+ nbytes
,
119 hurd_safe_memmove (void *dest
, const void *src
, size_t nbytes
)
122 void throw (int signo
, long int sigcode
, struct sigcontext
*scp
)
123 { longjmp (buf
, scp
->sc_error
?: EGRATUITOUS
); }
125 struct hurd_signal_preemptor src_preemptor
=
127 sigmask (SIGBUS
) | sigmask (SIGSEGV
),
128 (vm_address_t
) src
, (vm_address_t
) src
+ nbytes
,
129 NULL
, (sighandler_t
) &throw,
131 struct hurd_signal_preemptor dest_preemptor
=
133 sigmask (SIGBUS
) | sigmask (SIGSEGV
),
134 (vm_address_t
) dest
, (vm_address_t
) dest
+ nbytes
,
135 NULL
, (sighandler_t
) &throw,
139 struct hurd_sigstate
*const ss
= _hurd_self_sigstate ();
142 /* This returns again with nonzero value when we preempt a signal. */
143 error
= setjmp (buf
);
147 /* Install a signal preemptor for the thread. */
148 __spin_lock (&ss
->lock
);
149 src_preemptor
.next
= ss
->preemptors
;
150 ss
->preemptors
= &dest_preemptor
;
151 __spin_unlock (&ss
->lock
);
153 /* Do the copy; it might fault. */
154 memmove (dest
, src
, nbytes
);
157 /* Either memmove completed happily and ERROR is still zero, or it hit
158 an expected signal and `throw' made setjmp return the signal error
159 code in ERROR. Now we can remove the preemptor and return. */
161 __spin_lock (&ss
->lock
);
162 assert (ss
->preemptors
== &dest_preemptor
);
163 ss
->preemptors
= src_preemptor
.next
;
164 __spin_unlock (&ss
->lock
);