Exemple #1
0
error_t
_hurd_ctty_output (io_t port, io_t ctty, error_t (*rpc) (io_t))
{
  if (ctty == MACH_PORT_NULL)
    return (*rpc) (port);
  else
    {
      struct hurd_sigstate *ss = _hurd_self_sigstate ();
      error_t err;

      do
	{
	  /* Don't use the ctty io port if we are blocking or ignoring
	     SIGTTOU.  We redo this check at the top of the loop in case
	     the signal handler changed the state.  */
	  __spin_lock (&ss->lock);
	  if (__sigismember (&ss->blocked, SIGTTOU) ||
	      ss->actions[SIGTTOU].sa_handler == SIG_IGN)
	    err = EIO;
	  else
	    err = 0;
	  __spin_unlock (&ss->lock);

	  if (err)
	    return (*rpc) (port);

	  err = (*rpc) (ctty);
	  if (err == EBACKGROUND)
	    {
	      if (_hurd_orphaned)
		/* Our process group is orphaned, so we never generate a
		   signal; we just fail.  */
		err = EIO;
	      else
		{
		  /* Send a SIGTTOU signal to our process group.

		     We must remember here not to clobber ERR, since
		     the loop condition below uses it to recall that
		  we should retry after a stop.  */

		  __USEPORT (CTTYID, _hurd_sig_post (0, SIGTTOU, port));
		  /* XXX what to do if error here? */

		  /* At this point we should have just run the handler for
		     SIGTTOU or resumed after being stopped.  Now this is
		     still a "system call", so check to see if we should
		  restart it.  */
		  __spin_lock (&ss->lock);
		  if (!(ss->actions[SIGTTOU].sa_flags & SA_RESTART))
		    err = EINTR;
		  __spin_unlock (&ss->lock);
		}
	    }
	  /* If the last RPC generated a SIGTTOU, loop to try it again.  */
	} while (err == EBACKGROUND);

      return err;
    }
}
Exemple #2
0
void
_hurd_sigstate_unlock (struct hurd_sigstate *ss)
{
  __spin_unlock (&ss->lock);
  if (sigstate_is_global_rcv (ss))
    __spin_unlock (&_hurd_global_sigstate->lock);
}
Exemple #3
0
error_t
_hurd_ctty_input (io_t port, io_t ctty, error_t (*rpc) (io_t))
{
  error_t err;

  if (ctty == MACH_PORT_NULL)
    return (*rpc) (port);

  do
    {
      err = (*rpc) (ctty);
      if (err == EBACKGROUND)
	{
	  /* We are a background job and tried to read from the tty.
	     We should probably get a SIGTTIN signal.  */
	  if (_hurd_orphaned)
	    /* Our process group is orphaned.  Don't stop; just fail.  */
	    err = EIO;
	  else
	    {
	      struct hurd_sigstate *ss = _hurd_self_sigstate ();
	      __spin_lock (&ss->lock);
	      if (__sigismember (&ss->blocked, SIGTTIN) ||
		  ss->actions[SIGTTIN].sa_handler == SIG_IGN)
		/* We are blocking or ignoring SIGTTIN.  Just fail.  */
		err = EIO;
	      __spin_unlock (&ss->lock);

	      if (err == EBACKGROUND)
		{
		  /* Send a SIGTTIN signal to our process group.

		     We must remember here not to clobber ERR, since
		     the loop condition below uses it to recall that
		  we should retry after a stop.  */

		  __USEPORT (CTTYID, _hurd_sig_post (0, SIGTTIN, port));
		  /* XXX what to do if error here? */

		  /* At this point we should have just run the handler for
		     SIGTTIN or resumed after being stopped.  Now this is
		     still a "system call", so check to see if we should
		  restart it.  */
		  __spin_lock (&ss->lock);
		  if (!(ss->actions[SIGTTIN].sa_flags & SA_RESTART))
		    err = EINTR;
		  __spin_unlock (&ss->lock);
		}
	    }
	}
      /* If the last RPC generated a SIGTTIN, loop to try it again.  */
    } while (err == EBACKGROUND);

  return err;
}
Exemple #4
0
static kern_return_t
set_int (int which, int value)
{
  switch (which)
    {
    case INIT_UMASK:
      _hurd_umask = value;
      return 0;

      /* These are pretty odd things to do.  But you asked for it.  */
    case INIT_SIGMASK:
      {
	struct hurd_sigstate *ss = _hurd_thread_sigstate (_hurd_sigthread);
	__spin_lock (&ss->lock);
	ss->blocked = value;
	__spin_unlock (&ss->lock);
	return 0;
      }
    case INIT_SIGPENDING:
      {
	struct hurd_sigstate *ss = _hurd_thread_sigstate (_hurd_sigthread);
	__spin_lock (&ss->lock);
	ss->pending = value;
	__spin_unlock (&ss->lock);
	return 0;
      }
    case INIT_SIGIGN:
      {
	struct hurd_sigstate *ss = _hurd_thread_sigstate (_hurd_sigthread);
	int sig;
	const sigset_t ign = value;
	__spin_lock (&ss->lock);
	for (sig = 1; sig < NSIG; ++sig)
	  {
	    if (__sigismember (&ign, sig))
	      ss->actions[sig].sa_handler = SIG_IGN;
	    else if (ss->actions[sig].sa_handler == SIG_IGN)
	      ss->actions[sig].sa_handler = SIG_DFL;
	  }
	__spin_unlock (&ss->lock);
	return 0;

      case INIT_TRACEMASK:
	_hurdsig_traced = value;
	return 0;
      }
    default:
      return EINVAL;
    }
}
Exemple #5
0
/* Open a directory stream on a file descriptor in Hurd internal form.
   We do no checking here on the descriptor.  */
DIR *
_hurd_fd_opendir (struct hurd_fd *d)
{
  DIR *dirp;

  if (d == NULL)
    {
      errno = EBADF;
      return NULL;
    }

  dirp = (DIR *) malloc (sizeof (DIR));
  if (dirp == NULL)
    return NULL;

  /* Set the descriptor to close on exec. */
  HURD_CRITICAL_BEGIN;
  __spin_lock (&d->port.lock);
  d->flags |= FD_CLOEXEC;
  __spin_unlock (&d->port.lock);
  HURD_CRITICAL_END;

  dirp->__fd = d;
  dirp->__data = dirp->__ptr = NULL;
  dirp->__entry_data = dirp->__entry_ptr = 0;
  dirp->__allocation = 0;
  dirp->__size = 0;

  __libc_lock_init (dirp->__lock);

  return dirp;
}
error_t
_hurd_fd_close (struct hurd_fd *fd)
{
  error_t err;

  HURD_CRITICAL_BEGIN;

  __spin_lock (&fd->port.lock);
  if (fd->port.port == MACH_PORT_NULL)
    {
      __spin_unlock (&fd->port.lock);
      err = EBADF;
    }
  else
    {
      /* Clear the descriptor's port cells.
	 This deallocates the ports if noone else is still using them.  */
      _hurd_port_set (&fd->ctty, MACH_PORT_NULL);
      _hurd_port_locked_set (&fd->port, MACH_PORT_NULL);
      err = 0;
    }

  HURD_CRITICAL_END;

  return err;
}
Exemple #7
0
/* In the childs, unlock the interlock, and start a profiling thread up
   if necessary. */
static void
fork_profil_child (void)
{
    u_short *sb;
    size_t n, o, ss;
    error_t err;

    __spin_unlock (&lock);

    if (profile_thread != MACH_PORT_NULL)
    {
        __mach_port_deallocate (__mach_task_self (), profile_thread);
        profile_thread = MACH_PORT_NULL;
    }

    sb = samples;
    samples = NULL;
    n = maxsamples;
    maxsamples = 0;
    o = pc_offset;
    pc_offset = 0;
    ss = sample_scale;
    sample_scale = 0;

    if (ss != 0)
    {
        err = update_waiter (sb, n * sizeof *sb, o, ss);
        assert_perror (err);
    }
}
Exemple #8
0
int
__profil (u_short *sample_buffer, size_t size, size_t offset, u_int scale)
{
    error_t err;

    __spin_lock (&lock);

    if (scale == 0)
    {
        /* Disable profiling.  */
        int count;

        if (profile_thread != MACH_PORT_NULL)
            __thread_suspend (profile_thread);

        /* Fetch the last set of samples */
        if (sample_scale)
            fetch_samples ();

        err = __task_disable_pc_sampling (__mach_task_self (), &count);
        sample_scale = 0;
        seqno = 0;
    }
    else
        err = update_waiter (sample_buffer, size, offset, scale);

    __spin_unlock (&lock);

    return err ? __hurd_fail (err) : 0;
}
/* Open a directory stream on NAME.  */
DIR *
__opendir (const char *name)
{
  DIR *dirp;
  int fd;
  struct hurd_fd *d;

  if (name[0] == '\0')
    {
      /* POSIX.1-1990 says an empty name gets ENOENT;
	 but `open' might like it fine.  */
      __set_errno (ENOENT);
      return NULL;
    }

  {
    /* Append trailing slash to directory name to force ENOTDIR
       if it's not a directory.  */
    size_t len = strlen (name);
    if (name[len - 1] == '/')
      fd = __open (name, O_RDONLY);
    else
      {
	char n[len + 2];
	memcpy (n, name, len);
	n[len] = '/';
	n[len + 1] = '\0';
	fd = __open (n, O_RDONLY);
      }
  }
  if (fd < 0)
    return NULL;

  dirp = (DIR *) malloc (sizeof (DIR));
  if (dirp == NULL)
    {
      __close (fd);
      return NULL;
    }

  /* Extract the pointer to the descriptor structure.  */
  __mutex_lock (&_hurd_dtable_lock);
  d = dirp->__fd = _hurd_dtable[fd];
  __mutex_unlock (&_hurd_dtable_lock);

  /* Set the descriptor to close on exec. */
  __spin_lock (&d->port.lock);
  d->flags |= FD_CLOEXEC;
  __spin_unlock (&d->port.lock);

  dirp->__data = dirp->__ptr = NULL;
  dirp->__entry_data = dirp->__entry_ptr = 0;
  dirp->__allocation = 0;
  dirp->__size = 0;

  __libc_lock_init (dirp->__lock);

  return dirp;
}
Exemple #10
0
static kern_return_t
get_int (int which, int *value)
{
  switch (which)
    {
    case INIT_UMASK:
      *value = _hurd_umask;
      return 0;
    case INIT_SIGMASK:
      {
	struct hurd_sigstate *ss = _hurd_thread_sigstate (_hurd_sigthread);
	__spin_lock (&ss->lock);
	*value = ss->blocked;
	__spin_unlock (&ss->lock);
	return 0;
      }
    case INIT_SIGPENDING:
      {
	struct hurd_sigstate *ss = _hurd_thread_sigstate (_hurd_sigthread);
	__spin_lock (&ss->lock);
	*value = ss->pending;
	__spin_unlock (&ss->lock);
	return 0;
      }
    case INIT_SIGIGN:
      {
	struct hurd_sigstate *ss = _hurd_thread_sigstate (_hurd_sigthread);
	sigset_t ign;
	int sig;
	__spin_lock (&ss->lock);
	__sigemptyset (&ign);
	for (sig = 1; sig < NSIG; ++sig)
	  if (ss->actions[sig].sa_handler == SIG_IGN)
	    __sigaddset (&ign, sig);
	__spin_unlock (&ss->lock);
	*value = ign;
	return 0;
      }
    default:
      return EINVAL;
    }
}
Exemple #11
0
struct hurd_sigstate *
_hurd_thread_sigstate (thread_t thread)
{
  struct hurd_sigstate *ss;
  __mutex_lock (&_hurd_siglock);
  for (ss = _hurd_sigstates; ss != NULL; ss = ss->next)
    if (ss->thread == thread)
       break;
  if (ss == NULL)
    {
      ss = malloc (sizeof (*ss));
      if (ss == NULL)
	__libc_fatal ("hurd: Can't allocate sigstate\n");
      ss->thread = thread;
      __spin_lock_init (&ss->lock);

      /* Initialize default state.  */
      __sigemptyset (&ss->blocked);
      __sigemptyset (&ss->pending);
      memset (&ss->sigaltstack, 0, sizeof (ss->sigaltstack));
      ss->preemptors = NULL;
      ss->suspended = MACH_PORT_NULL;
      ss->intr_port = MACH_PORT_NULL;
      ss->context = NULL;

      if (thread == MACH_PORT_NULL)
	{
	  /* Process-wide sigstate, use the system defaults.  */
	  default_sigaction (ss->actions);

	  /* The global sigstate is not added to the _hurd_sigstates list.
	     It is created with _hurd_thread_sigstate (MACH_PORT_NULL)
	     but should be accessed through _hurd_global_sigstate.  */
	}
      else
	{
	  /* Use the global actions as a default for new threads.  */
	  struct hurd_sigstate *s = _hurd_global_sigstate;
	  if (s)
	    {
	      __spin_lock (&s->lock);
	      memcpy (ss->actions, s->actions, sizeof (s->actions));
	      __spin_unlock (&s->lock);
	    }
	  else
	    default_sigaction (ss->actions);

	  ss->next = _hurd_sigstates;
	  _hurd_sigstates = ss;
	}
    }
  __mutex_unlock (&_hurd_siglock);
  return ss;
}
Exemple #12
0
struct hurd_sigstate *
_hurd_thread_sigstate (thread_t thread)
{
  struct hurd_sigstate *ss;
  __mutex_lock (&_hurd_siglock);
  for (ss = _hurd_sigstates; ss != NULL; ss = ss->next)
    if (ss->thread == thread)
       break;
  if (ss == NULL)
    {
      ss = malloc (sizeof (*ss));
      if (ss == NULL)
	__libc_fatal ("hurd: Can't allocate thread sigstate\n");
      ss->thread = thread;
      __spin_lock_init (&ss->lock);

      /* Initialize default state.  */
      __sigemptyset (&ss->blocked);
      __sigemptyset (&ss->pending);
      memset (&ss->sigaltstack, 0, sizeof (ss->sigaltstack));
      ss->sigaltstack.ss_flags |= SS_DISABLE;
      ss->preemptors = NULL;
      ss->suspended = MACH_PORT_NULL;
      ss->intr_port = MACH_PORT_NULL;
      ss->context = NULL;

      /* Initialize the sigaction vector from the default signal receiving
	 thread's state, and its from the system defaults.  */
      if (thread == _hurd_sigthread)
	default_sigaction (ss->actions);
      else
	{
	  struct hurd_sigstate *s;
	  for (s = _hurd_sigstates; s != NULL; s = s->next)
	    if (s->thread == _hurd_sigthread)
	      break;
	  if (s)
	    {
	      __spin_lock (&s->lock);
	      memcpy (ss->actions, s->actions, sizeof (s->actions));
	      __spin_unlock (&s->lock);
	    }
	  else
	    default_sigaction (ss->actions);
	}

      ss->next = _hurd_sigstates;
      _hurd_sigstates = ss;
    }
  __mutex_unlock (&_hurd_siglock);
  return ss;
}
Exemple #13
0
error_t
hurd_thread_cancel (thread_t thread)
{
    struct hurd_sigstate *ss = _hurd_thread_sigstate (thread);
    struct machine_thread_all_state state;
    int state_change;
    error_t err;

    if (! ss)
        return EINVAL;
    if (ss == _hurd_self_sigstate ())
    {
        /* We are cancelling ourselves, so it is easy to succeed
        quickly.  Since this function is not a cancellation point, we
         just leave the flag set pending the next cancellation point
         (hurd_check_cancel or RPC) and return success.  */
        ss->cancel = 1;
        return 0;
    }

    assert (! __spin_lock_locked (&ss->critical_section_lock));
    __spin_lock (&ss->critical_section_lock);
    __spin_lock (&ss->lock);
    err = __thread_suspend (thread);
    __spin_unlock (&ss->lock);

    if (! err)
    {
        /* Set the flag telling the thread its operation is being cancelled.  */
        ss->cancel = 1;

        /* Interrupt any interruptible RPC now in progress.  */
        state.set = 0;
        _hurdsig_abort_rpcs (ss, 0, 0, &state, &state_change, NULL, 0, 0);
        if (state_change)
            err = __thread_set_state (thread, MACHINE_THREAD_STATE_FLAVOR,
                                      (natural_t *) &state.basic,
                                      MACHINE_THREAD_STATE_COUNT);

        if (ss->cancel_hook)
            /* The code being cancelled has a special wakeup function.
               Calling this should make the thread wake up and check the
               cancellation flag.  */
            (*ss->cancel_hook) ();

        __thread_resume (thread);
    }

    _hurd_critical_section_unlock (ss);
    return err;
}
Exemple #14
0
int
hurd_check_cancel (void)
{
    struct hurd_sigstate *ss = _hurd_self_sigstate ();
    int cancel;

    __spin_lock (&ss->lock);
    assert (! __spin_lock_locked (&ss->critical_section_lock));
    cancel = ss->cancel;
    ss->cancel = 0;
    __spin_unlock (&ss->lock);

    return cancel;
}
Exemple #15
0
/* Run signals handlers on the stack specified by SS (if not NULL).
   If OSS is not NULL, it is filled in with the old signal stack status.  */
int
__sigaltstack (const stack_t *argss, stack_t *oss)
{
  struct hurd_sigstate *s;
  stack_t ss, old;

  /* Fault before taking any locks.  */
  if (argss != NULL)
    ss = *argss;
  if (oss != NULL)
    *(volatile stack_t *) oss = *oss;

  s = _hurd_self_sigstate ();
  __spin_lock (&s->lock);

  if (argss != NULL
      && (ss.ss_flags & SS_DISABLE)
      && (s->sigaltstack.ss_flags & SS_ONSTACK))
    {
      /* Can't disable a stack that is in use.  */
      __spin_unlock (&s->lock);
      errno = EINVAL;
      return -1;
    }

  old = s->sigaltstack;

  if (argss != NULL)
    s->sigaltstack = ss;

  __spin_unlock (&s->lock);

  if (oss != NULL)
    *oss = old;

  return 0;
}
Exemple #16
0
void
_hurd_port2fd (struct hurd_fd *d, io_t port, int flags)
{
  io_t ctty;
  mach_port_t cttyid;
  int is_ctty = !(flags & O_IGNORE_CTTY) && ! __term_getctty (port, &cttyid);

  if (is_ctty)
    {
      /* This port is capable of being a controlling tty.
	 Is it ours?  */
      struct hurd_port *const id = &_hurd_ports[INIT_PORT_CTTYID];
      __spin_lock (&id->lock);
      if (id->port == MACH_PORT_NULL)
	/* We have no controlling tty, so make this one it.  */
	_hurd_port_locked_set (id, cttyid);
      else
	{
	  if (cttyid != id->port)
	    /* We have a controlling tty and this is not it.  */
	    is_ctty = 0;
	  /* Either we don't want CTTYID, or ID->port already is it.
	     So we don't need to change ID->port, and we can release
	     the reference to CTTYID.  */
	  __spin_unlock (&id->lock);
	  __mach_port_deallocate (__mach_task_self (), cttyid);
	}
    }

  if (!is_ctty || __term_open_ctty (port, _hurd_pid, _hurd_pgrp, &ctty) != 0)
    /* XXX if IS_CTTY, then this port is our ctty, but we are
       not doing ctty style i/o because term_become_ctty barfed.
       What to do?  */
    /* No ctty magic happening here.  */
    ctty = MACH_PORT_NULL;

  /* Install PORT in the descriptor cell, leaving it locked.  */
  {
    mach_port_t old
      = _hurd_userlink_clear (&d->port.users) ? d->port.port : MACH_PORT_NULL;
    d->port.port = port;
    if (old != MACH_PORT_NULL)
      __mach_port_deallocate (__mach_task_self (), old);
  }

  _hurd_port_set (&d->ctty, ctty);
}
Exemple #17
0
error_t
__pthread_sigstate (struct __pthread *thread, int how,
		    const sigset_t *set, sigset_t *oset, int clear_pending)
{
  error_t err = 0;
  struct hurd_sigstate *ss;

  ss = _hurd_thread_sigstate (thread->kernel_thread);
  assert (ss);

  __spin_lock (&ss->lock);

  if (oset != NULL)
    *oset = ss->blocked;

  if (set != NULL)
    {
      switch (how)
	{
	case SIG_BLOCK:
	  ss->blocked |= *set;
	  break;

	case SIG_SETMASK:
	  ss->blocked = *set;
	  break;

	case SIG_UNBLOCK:
	  ss->blocked &= ~*set;
	  break;

	default:
	  err = EINVAL;
	  break;
	}
      ss->blocked &= ~_SIG_CANT_MASK;
    }

  if (!err && clear_pending)
    __sigemptyset (&ss->pending);

  __spin_unlock (&ss->lock);

  return err;
}
Exemple #18
0
/* XXX should be __sigpending ? */
int
sigpending (sigset_t *set)
{
  struct hurd_sigstate *ss;
  sigset_t pending;

  if (set == NULL)
    {
      errno = EINVAL;
      return -1;
    }

  ss = _hurd_self_sigstate ();
  __spin_lock (&ss->lock);
  pending = ss->pending;
  __spin_unlock (&ss->lock);

  *set = pending;
  return 0;
}
Exemple #19
0
/* This function must be very careful not to depend on Hurd threadvar
   variables.  We arrange that by using special stubs arranged for at the
   end of this file. */
static void
profile_waiter (void)
{
    mach_msg_header_t msg;
    mach_port_t timeout_reply_port;

    profil_reply_port = __mach_reply_port ();
    timeout_reply_port = __mach_reply_port ();

    while (1)
    {
        __spin_lock (&lock);

        fetch_samples ();

        __spin_unlock (&lock);

        __mach_msg (&msg, MACH_RCV_MSG|MACH_RCV_TIMEOUT, 0, sizeof msg,
                    timeout_reply_port, collector_timeout, MACH_PORT_NULL);
    }
}
Exemple #20
0
static void
check_one_fd (int fd, int mode)
{
  struct hurd_fd *d;

  d = _hurd_fd_get (fd);
  if (d == NULL)
    {
      /* This descriptor hasn't been opened.  We try to allocate the
         descriptor and open /dev/null on it so that the SUID program
         we are about to start does not accidentally use this
         descriptor.  */
      d = _hurd_alloc_fd (NULL, fd);
      if (d != NULL)
	{
	  mach_port_t port;

	  port = __file_name_lookup (_PATH_DEVNULL, mode, 0);
	  if (port)
	    {
	      /* Since /dev/null isn't supposed to be a terminal, we
		 avoid any ctty magic.  */
	      d->port.port = port;
	      d->flags = 0;

	      __spin_unlock (&d->port.lock);
	      return;
	    }
	}

      /* We cannot even give an error message here since it would run
	 into the same problems.  */
      while (1)
	/* Try for ever and ever.  */
	ABORT_INSTRUCTION;
    }
}
Exemple #21
0
/* Open a directory stream on NAME.  */
DIR *
__opendir (const char *name)
{
  DIR *dirp;
  int fd;
  struct hurd_fd *d;

  fd = __open (name, O_RDONLY);
  if (fd < 0)
    return NULL;

  dirp = (DIR *) malloc (sizeof (DIR));
  if (dirp == NULL)
    {
      __close (fd);
      return NULL;
    }

  /* Extract the pointer to the descriptor structure.  */
  __mutex_lock (&_hurd_dtable_lock);
  d = dirp->__fd = _hurd_dtable[fd];
  __mutex_unlock (&_hurd_dtable_lock);

  /* Set the descriptor to close on exec. */
  __spin_lock (&d->port.lock);
  d->flags |= FD_CLOEXEC;
  __spin_unlock (&d->port.lock);

  dirp->__data = dirp->__ptr = NULL;
  dirp->__entry_data = dirp->__entry_ptr = 0;
  dirp->__allocation = 0;
  dirp->__size = 0;

  __libc_lock_init (dirp->__lock);

  return dirp;
}
Exemple #22
0
void
_longjmp_unwind (jmp_buf env, int val)
{
  struct hurd_sigstate *ss = _hurd_self_sigstate ();
  struct hurd_userlink *link;

  /* All access to SS->active_resources must take place inside a critical
     section where signal handlers cannot run.  */
  __spin_lock (&ss->lock);
  assert (! __spin_lock_locked (&ss->critical_section_lock));
  __spin_lock (&ss->critical_section_lock);

  /* Remove local signal preemptors being unwound past.  */
  while (ss->preemptors &&
	 _JMPBUF_UNWINDS (env[0].__jmpbuf, ss->preemptors, demangle_ptr))
    ss->preemptors = ss->preemptors->next;

  __spin_unlock (&ss->lock);

  /* Iterate over the current thread's list of active resources.
     Process the head portion of the list whose links reside
     in stack frames being unwound by this jump.  */

  for (link = ss->active_resources;
       link && _JMPBUF_UNWINDS (env[0].__jmpbuf, link, demangle_ptr);
       link = link->thread.next)
    /* Remove this link from the resource's users list,
       since the frame using the resource is being unwound.
       This call returns nonzero if that was the last user.  */
    if (_hurd_userlink_unlink (link))
      /* One of the frames being unwound by the longjmp was the last user
	 of its resource.  Call the cleanup function to deallocate it.  */
      (*link->cleanup) (link->cleanup_data, env, val);

  _hurd_critical_section_unlock (ss);
}
Exemple #23
0
void
_hurd_raise_signal (struct hurd_sigstate *ss,
		    int signo, const struct hurd_signal_detail *detail)
{
  if (ss == NULL)
    {
      ss = _hurd_self_sigstate ();
      __spin_lock (&ss->lock);
    }

  /* Mark SIGNO as pending to be delivered.  */
  __sigaddset (&ss->pending, signo);
  ss->pending_data[signo] = *detail;

  __spin_unlock (&ss->lock);

  /* Send a message to the signal thread so it will wake up and check for
     pending signals.  This is a generic "poll request" message (SIGNO==0)
     rather than delivering this signal and its detail, because we have
     already marked the signal as pending for the particular thread we
     want.  Generating the signal with an RPC might deliver it to some
     other thread.  */
  __msg_sig_post (_hurd_msgport, 0, 0, __mach_task_self ());
}
Exemple #24
0
/* Allocate a new file descriptor and install PORT in it.  FLAGS are as for
   `open'; only O_IGNORE_CTTY and O_CLOEXEC are meaningful.

   If the descriptor table is full, set errno, and return -1.
   If DEALLOC is nonzero, deallocate PORT first.  */
int
_hurd_intern_fd (io_t port, int flags, int dealloc)
{
  int fd;
  struct hurd_fd *d;

  HURD_CRITICAL_BEGIN;
  d = _hurd_alloc_fd (&fd, 0);
  if (d != NULL)
    {
      _hurd_port2fd (d, port, flags);
      __spin_unlock (&d->port.lock);
    }
  HURD_CRITICAL_END;

  if (d == NULL)
    {
      if (dealloc)
	__mach_port_deallocate (__mach_task_self (), port);
      return -1;
    }

  return fd;
}
Exemple #25
0
/* Overlay TASK, executing FILE with arguments ARGV and environment ENVP.
   If TASK == mach_task_self (), some ports are dealloc'd by the exec server.
   ARGV and ENVP are terminated by NULL pointers.  */
error_t
_hurd_exec (task_t task, file_t file,
	    char *const argv[], char *const envp[])
{
  error_t err;
  char *args, *env;
  size_t argslen, envlen;
  int ints[INIT_INT_MAX];
  mach_port_t ports[_hurd_nports];
  struct hurd_userlink ulink_ports[_hurd_nports];
  file_t *dtable;
  unsigned int dtablesize, i;
  struct hurd_port **dtable_cells;
  struct hurd_userlink *ulink_dtable;
  struct hurd_sigstate *ss;
  mach_port_t *please_dealloc, *pdp;

  /* XXX needs to be hurdmalloc XXX */
  if (err = __argz_create (argv, &args, &argslen))
    return err;
  if (err = __argz_create (envp, &env, &envlen))
    goto outargs;

  /* Load up the ports to give to the new program.  */
  for (i = 0; i < _hurd_nports; ++i)
    if (i == INIT_PORT_PROC && task != __mach_task_self ())
      {
	/* This is another task, so we need to ask the proc server
	   for the right proc server port for it.  */
	if (err = __USEPORT (PROC, __proc_task2proc (port, task, &ports[i])))
	  {
	    while (--i > 0)
	      _hurd_port_free (&_hurd_ports[i], &ulink_ports[i], ports[i]);
	    goto outenv;
	  }
      }
    else
      ports[i] = _hurd_port_get (&_hurd_ports[i], &ulink_ports[i]);


  /* Load up the ints to give the new program.  */
  for (i = 0; i < INIT_INT_MAX; ++i)
    switch (i)
      {
      case INIT_UMASK:
	ints[i] = _hurd_umask;
	break;

      case INIT_SIGMASK:
      case INIT_SIGIGN:
      case INIT_SIGPENDING:
	/* We will set these all below.  */
	break;

      case INIT_TRACEMASK:
	ints[i] = _hurdsig_traced;
	break;

      default:
	ints[i] = 0;
      }

  ss = _hurd_self_sigstate ();

  assert (! __spin_lock_locked (&ss->critical_section_lock));
  __spin_lock (&ss->critical_section_lock);

  __spin_lock (&ss->lock);
  ints[INIT_SIGMASK] = ss->blocked;
  ints[INIT_SIGPENDING] = ss->pending;
  ints[INIT_SIGIGN] = 0;
  for (i = 1; i < NSIG; ++i)
    if (ss->actions[i].sa_handler == SIG_IGN)
      ints[INIT_SIGIGN] |= __sigmask (i);

  /* We hold the sigstate lock until the exec has failed so that no signal
     can arrive between when we pack the blocked and ignored signals, and
     when the exec actually happens.  A signal handler could change what
     signals are blocked and ignored.  Either the change will be reflected
     in the exec, or the signal will never be delivered.  Setting the
     critical section flag avoids anything we call trying to acquire the
     sigstate lock.  */

  __spin_unlock (&ss->lock);

  /* Pack up the descriptor table to give the new program.  */
  __mutex_lock (&_hurd_dtable_lock);

  dtablesize = _hurd_dtable ? _hurd_dtablesize : _hurd_init_dtablesize;

  if (task == __mach_task_self ())
    /* Request the exec server to deallocate some ports from us if the exec
       succeeds.  The init ports and descriptor ports will arrive in the
       new program's exec_startup message.  If we failed to deallocate
       them, the new program would have duplicate user references for them.
       But we cannot deallocate them ourselves, because we must still have
       them after a failed exec call.  */
    please_dealloc = __alloca ((_hurd_nports + (2 * dtablesize))
				* sizeof (mach_port_t));
  else
    please_dealloc = NULL;
  pdp = please_dealloc;

  if (_hurd_dtable != NULL)
    {
      dtable = __alloca (dtablesize * sizeof (dtable[0]));
      ulink_dtable = __alloca (dtablesize * sizeof (ulink_dtable[0]));
      dtable_cells = __alloca (dtablesize * sizeof (dtable_cells[0]));
      for (i = 0; i < dtablesize; ++i)
	{
	  struct hurd_fd *const d = _hurd_dtable[i];
	  if (d == NULL)
	    {
	      dtable[i] = MACH_PORT_NULL;
	      continue;
	    }
	  __spin_lock (&d->port.lock);
	  if (d->flags & FD_CLOEXEC)
	    {
	      /* This descriptor is marked to be closed on exec.
		 So don't pass it to the new program.  */
	      dtable[i] = MACH_PORT_NULL;
	      if (pdp && d->port.port != MACH_PORT_NULL)
		{
		  /* We still need to deallocate the ports.  */
		  *pdp++ = d->port.port;
		  if (d->ctty.port != MACH_PORT_NULL)
		    *pdp++ = d->ctty.port;
		}
	      __spin_unlock (&d->port.lock);
	    }
	  else
	    {
	      if (pdp && d->ctty.port != MACH_PORT_NULL)
		/* All the elements of DTABLE are added to PLEASE_DEALLOC
		   below, so we needn't add the port itself.
		   But we must deallocate the ctty port as well as
		   the normal port that got installed in DTABLE[I].  */
		*pdp++ = d->ctty.port;
	      dtable[i] = _hurd_port_locked_get (&d->port, &ulink_dtable[i]);
	      dtable_cells[i] = &d->port;
	    }
	}
    }
  else
    {
      dtable = _hurd_init_dtable;
      ulink_dtable = NULL;
      dtable_cells = NULL;
    }

  /* The information is all set up now.  Try to exec the file.  */

  {
    if (pdp)
      {
	/* Request the exec server to deallocate some ports from us if the exec
	   succeeds.  The init ports and descriptor ports will arrive in the
	   new program's exec_startup message.  If we failed to deallocate
	   them, the new program would have duplicate user references for them.
	   But we cannot deallocate them ourselves, because we must still have
	   them after a failed exec call.  */

	for (i = 0; i < _hurd_nports; ++i)
	  *pdp++ = ports[i];
	for (i = 0; i < dtablesize; ++i)
	  *pdp++ = dtable[i];
      }

    err = __file_exec (file, task, 0,
		       args, argslen, env, envlen,
		       dtable, MACH_MSG_TYPE_COPY_SEND, dtablesize,
		       ports, MACH_MSG_TYPE_COPY_SEND, _hurd_nports,
		       ints, INIT_INT_MAX,
		       please_dealloc, pdp - please_dealloc,
		       &_hurd_msgport, task == __mach_task_self () ? 1 : 0);
  }

  /* Release references to the standard ports.  */
  for (i = 0; i < _hurd_nports; ++i)
    if (i == INIT_PORT_PROC && task != __mach_task_self ())
      __mach_port_deallocate (__mach_task_self (), ports[i]);
    else
      _hurd_port_free (&_hurd_ports[i], &ulink_ports[i], ports[i]);

  if (ulink_dtable != NULL)
    /* Release references to the file descriptor ports.  */
    for (i = 0; i < dtablesize; ++i)
      if (dtable[i] != MACH_PORT_NULL)
	_hurd_port_free (dtable_cells[i], &ulink_dtable[i], dtable[i]);

  /* Release lock on the file descriptor table. */
  __mutex_unlock (&_hurd_dtable_lock);

  /* Safe to let signals happen now.  */
  _hurd_critical_section_unlock (ss);

 outargs:
  free (args);
 outenv:
  free (env);
  return err;
}
Exemple #26
0
kern_return_t
_S_catch_exception_raise (mach_port_t port,
			  thread_t thread,
			  task_t task,
#ifdef EXC_MASK_ALL		/* New interface flavor.  */
			  exception_type_t exception,
			  exception_data_t code,
			  mach_msg_type_number_t codeCnt
#else				/* Vanilla Mach 3.0 interface.  */
			  integer_t exception,
			  integer_t code, integer_t subcode
#endif
			  )
{
  struct hurd_sigstate *ss;
  int signo;
  struct hurd_signal_detail d;

  if (task != __mach_task_self ())
    /* The sender wasn't the kernel.  */
    return EPERM;

  d.exc = exception;
#ifdef EXC_MASK_ALL
  assert (codeCnt >= 2);
  d.exc_code = code[0];
  d.exc_subcode = code[1];
#else
  d.exc_code = code;
  d.exc_subcode = subcode;
#endif

  /* Call the machine-dependent function to translate the Mach exception
     codes into a signal number and subcode.  */
  _hurd_exception2signal (&d, &signo);

  /* Find the sigstate structure for the faulting thread.  */
  __mutex_lock (&_hurd_siglock);
  for (ss = _hurd_sigstates; ss != NULL; ss = ss->next)
    if (ss->thread == thread)
      break;
  __mutex_unlock (&_hurd_siglock);
  if (ss == NULL)
    ss = _hurd_thread_sigstate (thread); /* Allocate a fresh one.  */

  if (__spin_lock_locked (&ss->lock))
    {
      /* Loser.  The thread faulted with its sigstate lock held.  Its
	 sigstate data is now suspect.  So we reset the parts of it which
	 could cause trouble for the signal thread.  Anything else
	 clobbered therein will just hose this user thread, but it's
	 faulting already.

	 This is almost certainly a library bug: unless random memory
	 clobberation caused the sigstate lock to gratuitously appear held,
	 no code should do anything that can fault while holding the
	 sigstate lock.  */

      __spin_unlock (&ss->critical_section_lock);
      ss->context = NULL;
      __spin_unlock (&ss->lock);
    }

  /* Post the signal.  */
  _hurd_internal_post_signal (ss, signo, &d,
			      MACH_PORT_NULL, MACH_MSG_TYPE_PORT_SEND,
			      0);

  return KERN_SUCCESS;
}
Exemple #27
0
/* In the parent, unlock the interlock once fork is complete. */
static void
fork_profil_parent (void)
{
    __spin_unlock (&lock);
}
Exemple #28
0
/* Duplicate FD to FD2, closing the old FD2 and making FD2 be
   open on the same file as FD is, and setting FD2's flags according to FLAGS.
   Return FD2 or -1.  */
int
__dup3 (int fd, int fd2, int flags)
{
  struct hurd_fd *d;

  /* Both passing flags different from O_CLOEXEC and FD2 being the same as FD
     are invalid.  */
  if ((flags & ~O_CLOEXEC
       || fd2 == fd)
      /* ... with the exception in case that dup2 behavior is requested: if FD
	 is valid and FD2 is already the same then just return it.  */
      && ! (flags == -1
	    && fd2 == fd))
    return __hurd_fail (EINVAL);

  /* Extract the ports and flags from FD.  */
  d = _hurd_fd_get (fd);
  if (d == NULL)
    return __hurd_fail (EBADF);

  HURD_CRITICAL_BEGIN;

  __spin_lock (&d->port.lock);
  if (d->port.port == MACH_PORT_NULL)
    {
      __spin_unlock (&d->port.lock);
      fd2 = __hurd_fail (EBADF);
    }
  else if (fd2 == fd)
    __spin_unlock (&d->port.lock);
  else
    {
      struct hurd_userlink ulink, ctty_ulink;
      int d_flags = d->flags;
      io_t ctty = _hurd_port_get (&d->ctty, &ctty_ulink);
      io_t port = _hurd_port_locked_get (&d->port, &ulink); /* Unlocks D.  */

      if (fd2 < 0)
	fd2 = __hurd_fail (EBADF);
      else
	{
	  /* Get a hold of the destination descriptor.  */
	  struct hurd_fd *d2;

	  __mutex_lock (&_hurd_dtable_lock);

	  if (fd2 >= _hurd_dtablesize)
	    {
	      /* The table is not large enough to hold the destination
		 descriptor.  Enlarge it as necessary to allocate this
		 descriptor.  */
	      __mutex_unlock (&_hurd_dtable_lock);
	      d2 = _hurd_alloc_fd (NULL, fd2);
	      if (d2)
		__spin_unlock (&d2->port.lock);
	      __mutex_lock (&_hurd_dtable_lock);
	    }
	  else
	    {
	      d2 = _hurd_dtable[fd2];
	      if (d2 == NULL)
		{
		  /* Must allocate a new one.  We don't initialize the port
		     cells with this call so that if it fails (out of
		     memory), we will not have already added user
		     references for the ports, which we would then have to
		     deallocate.  */
		  d2 = _hurd_dtable[fd2] = _hurd_new_fd (MACH_PORT_NULL,
							 MACH_PORT_NULL);
		}
	    }
	  __mutex_unlock (&_hurd_dtable_lock);

	  if (d2 == NULL)
	    {
	      fd2 = -1;
	      if (errno == EINVAL)
		errno = EBADF;	/* POSIX.1-1990 6.2.1.2 ll 54-55.  */
	    }
	  else
	    {
	      /* Give the ports each a user ref for the new descriptor.  */
	      __mach_port_mod_refs (__mach_task_self (), port,
				    MACH_PORT_RIGHT_SEND, 1);
	      if (ctty != MACH_PORT_NULL)
		__mach_port_mod_refs (__mach_task_self (), ctty,
				      MACH_PORT_RIGHT_SEND, 1);

	      /* Install the ports and flags in the new descriptor slot.  */
	      __spin_lock (&d2->port.lock);
	      if (flags & O_CLOEXEC)
		d2->flags = d_flags | FD_CLOEXEC;
	      else
		/* dup clears FD_CLOEXEC.  */
		d2->flags = d_flags & ~FD_CLOEXEC;
	      _hurd_port_set (&d2->ctty, ctty);
	      _hurd_port_locked_set (&d2->port, port); /* Unlocks D2.  */
	    }
	}

      _hurd_port_free (&d->port, &ulink, port);
      if (ctty != MACH_PORT_NULL)
	_hurd_port_free (&d->ctty, &ctty_ulink, port);
    }

  HURD_CRITICAL_END;

  return fd2;
}