Blame hurd/catch-signal.c

Packit 6c4009
/* Convenience function to catch expected signals during an operation.
Packit 6c4009
   Copyright (C) 1996-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <hurd/signal.h>
Packit 6c4009
#include <hurd/sigpreempt.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <assert.h>
Packit 6c4009
Packit 6c4009
error_t
Packit 6c4009
__hurd_catch_signal (sigset_t sigset,
Packit 6c4009
		     unsigned long int first, unsigned long int last,
Packit 6c4009
		     error_t (*operate) (struct hurd_signal_preemptor *),
Packit 6c4009
		     sighandler_t handler)
Packit 6c4009
{
Packit 6c4009
  /* We need to restore the signal mask, because otherwise the
Packit 6c4009
     signal-handling code will have blocked the caught signal and for
Packit 6c4009
     instance calling hurd_catch_signal again would then dump core.  */
Packit 6c4009
  sigjmp_buf buf;
Packit 6c4009
  void throw (int signo, long int sigcode, struct sigcontext *scp)
Packit 6c4009
    { siglongjmp (buf, scp->sc_error ?: EGRATUITOUS); }
Packit 6c4009
Packit 6c4009
  struct hurd_signal_preemptor preemptor =
Packit 6c4009
    {
Packit 6c4009
      sigset, first, last,
Packit 6c4009
      NULL, handler == SIG_ERR ? (sighandler_t) &throw : handler,
Packit 6c4009
    };
Packit 6c4009
Packit 6c4009
  struct hurd_sigstate *const ss = _hurd_self_sigstate ();
Packit 6c4009
  error_t error;
Packit 6c4009
Packit 6c4009
  if (handler != SIG_ERR)
Packit 6c4009
    /* Not our handler; don't bother saving state.  */
Packit 6c4009
    error = 0;
Packit 6c4009
  else
Packit 6c4009
    /* This returns again with nonzero value when we preempt a signal.  */
Packit 6c4009
    error = sigsetjmp (buf, 1);
Packit 6c4009
Packit 6c4009
  if (error == 0)
Packit 6c4009
    {
Packit 6c4009
      /* Install a signal preemptor for the thread.  */
Packit 6c4009
      __spin_lock (&ss->lock);
Packit 6c4009
      preemptor.next = ss->preemptors;
Packit 6c4009
      ss->preemptors = &preemptor;
Packit 6c4009
      __spin_unlock (&ss->lock);
Packit 6c4009
Packit 6c4009
      /* Try the operation that might crash.  */
Packit 6c4009
      (*operate) (&preemptor);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Either FUNCTION completed happily and ERROR is still zero, or it hit
Packit 6c4009
     an expected signal and `throw' made setjmp return the signal error
Packit 6c4009
     code in ERROR.  Now we can remove the preemptor and return.  */
Packit 6c4009
Packit 6c4009
  __spin_lock (&ss->lock);
Packit 6c4009
  assert (ss->preemptors == &preemptor);
Packit 6c4009
  ss->preemptors = preemptor.next;
Packit 6c4009
  __spin_unlock (&ss->lock);
Packit 6c4009
Packit 6c4009
  return error;
Packit 6c4009
}
Packit 6c4009
strong_alias (__hurd_catch_signal, hurd_catch_signal)
Packit 6c4009
Packit 6c4009
Packit 6c4009
error_t
Packit 6c4009
hurd_safe_memset (void *dest, int byte, size_t nbytes)
Packit 6c4009
{
Packit 6c4009
  error_t operate (struct hurd_signal_preemptor *preemptor)
Packit 6c4009
    {
Packit 6c4009
      memset (dest, byte, nbytes);
Packit 6c4009
      return 0;
Packit 6c4009
    }
Packit 6c4009
  return __hurd_catch_signal (sigmask (SIGBUS) | sigmask (SIGSEGV),
Packit 6c4009
			      (vm_address_t) dest, (vm_address_t) dest + nbytes,
Packit 6c4009
			      &operate, SIG_ERR);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
error_t
Packit 6c4009
hurd_safe_copyout (void *dest, const void *src, size_t nbytes)
Packit 6c4009
{
Packit 6c4009
  error_t operate (struct hurd_signal_preemptor *preemptor)
Packit 6c4009
    {
Packit 6c4009
      memcpy (dest, src, nbytes);
Packit 6c4009
      return 0;
Packit 6c4009
    }
Packit 6c4009
  return __hurd_catch_signal (sigmask (SIGBUS) | sigmask (SIGSEGV),
Packit 6c4009
			      (vm_address_t) dest, (vm_address_t) dest + nbytes,
Packit 6c4009
			      &operate, SIG_ERR);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
error_t
Packit 6c4009
hurd_safe_copyin (void *dest, const void *src, size_t nbytes)
Packit 6c4009
{
Packit 6c4009
  error_t operate (struct hurd_signal_preemptor *preemptor)
Packit 6c4009
    {
Packit 6c4009
      memcpy (dest, src, nbytes);
Packit 6c4009
      return 0;
Packit 6c4009
    }
Packit 6c4009
  return __hurd_catch_signal (sigmask (SIGBUS) | sigmask (SIGSEGV),
Packit 6c4009
			      (vm_address_t) src, (vm_address_t) src + nbytes,
Packit 6c4009
			      &operate, SIG_ERR);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
error_t
Packit 6c4009
hurd_safe_memmove (void *dest, const void *src, size_t nbytes)
Packit 6c4009
{
Packit 6c4009
  jmp_buf buf;
Packit 6c4009
  void throw (int signo, long int sigcode, struct sigcontext *scp)
Packit 6c4009
    { longjmp (buf, scp->sc_error ?: EGRATUITOUS); }
Packit 6c4009
Packit 6c4009
  struct hurd_signal_preemptor src_preemptor =
Packit 6c4009
    {
Packit 6c4009
      sigmask (SIGBUS) | sigmask (SIGSEGV),
Packit 6c4009
      (vm_address_t) src, (vm_address_t) src + nbytes,
Packit 6c4009
      NULL, (sighandler_t) &throw,
Packit 6c4009
    };
Packit 6c4009
  struct hurd_signal_preemptor dest_preemptor =
Packit 6c4009
    {
Packit 6c4009
      sigmask (SIGBUS) | sigmask (SIGSEGV),
Packit 6c4009
      (vm_address_t) dest, (vm_address_t) dest + nbytes,
Packit 6c4009
      NULL, (sighandler_t) &throw,
Packit 6c4009
      &src_preemptor
Packit 6c4009
    };
Packit 6c4009
Packit 6c4009
  struct hurd_sigstate *const ss = _hurd_self_sigstate ();
Packit 6c4009
  error_t error;
Packit 6c4009
Packit 6c4009
  /* This returns again with nonzero value when we preempt a signal.  */
Packit 6c4009
  error = setjmp (buf);
Packit 6c4009
Packit 6c4009
  if (error == 0)
Packit 6c4009
    {
Packit 6c4009
      /* Install a signal preemptor for the thread.  */
Packit 6c4009
      __spin_lock (&ss->lock);
Packit 6c4009
      src_preemptor.next = ss->preemptors;
Packit 6c4009
      ss->preemptors = &dest_preemptor;
Packit 6c4009
      __spin_unlock (&ss->lock);
Packit 6c4009
Packit 6c4009
      /* Do the copy; it might fault.  */
Packit 6c4009
      memmove (dest, src, nbytes);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Either memmove completed happily and ERROR is still zero, or it hit
Packit 6c4009
     an expected signal and `throw' made setjmp return the signal error
Packit 6c4009
     code in ERROR.  Now we can remove the preemptor and return.  */
Packit 6c4009
Packit 6c4009
  __spin_lock (&ss->lock);
Packit 6c4009
  assert (ss->preemptors == &dest_preemptor);
Packit 6c4009
  ss->preemptors = src_preemptor.next;
Packit 6c4009
  __spin_unlock (&ss->lock);
Packit 6c4009
Packit 6c4009
  return error;
Packit 6c4009
}