hjl / source-git / glibc

Forked from source-git/glibc 3 years ago
Clone

Blame hurd/ctty-output.c

Packit 6c4009
/* _hurd_ctty_output -- Do an output RPC and generate SIGTTOU if necessary.
Packit 6c4009
   Copyright (C) 1995-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.h>
Packit 6c4009
#include <hurd/signal.h>
Packit 6c4009
Packit 6c4009
/* Call *RPC on PORT and/or CTTY.  If a call on CTTY returns EBACKGROUND,
Packit 6c4009
   generate SIGTTOU if appropriate.  */
Packit 6c4009
Packit 6c4009
error_t
Packit 6c4009
_hurd_ctty_output (io_t port, io_t ctty, error_t (*rpc) (io_t))
Packit 6c4009
{
Packit 6c4009
  if (ctty == MACH_PORT_NULL)
Packit 6c4009
    return (*rpc) (port);
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      struct hurd_sigstate *ss = _hurd_self_sigstate ();
Packit 6c4009
      error_t err;
Packit 6c4009
Packit 6c4009
      do
Packit 6c4009
	{
Packit 6c4009
	  /* Don't use the ctty io port if we are blocking or ignoring
Packit 6c4009
	     SIGTTOU.  We redo this check at the top of the loop in case
Packit 6c4009
	     the signal handler changed the state.  */
Packit 6c4009
	  __spin_lock (&ss->lock);
Packit 6c4009
	  if (__sigismember (&ss->blocked, SIGTTOU) ||
Packit 6c4009
	      ss->actions[SIGTTOU].sa_handler == SIG_IGN)
Packit 6c4009
	    err = EIO;
Packit 6c4009
	  else
Packit 6c4009
	    err = 0;
Packit 6c4009
	  __spin_unlock (&ss->lock);
Packit 6c4009
Packit 6c4009
	  if (err)
Packit 6c4009
	    return (*rpc) (port);
Packit 6c4009
Packit 6c4009
	  err = (*rpc) (ctty);
Packit 6c4009
	  if (err == EBACKGROUND)
Packit 6c4009
	    {
Packit 6c4009
	      if (_hurd_orphaned)
Packit 6c4009
		/* Our process group is orphaned, so we never generate a
Packit 6c4009
		   signal; we just fail.  */
Packit 6c4009
		err = EIO;
Packit 6c4009
	      else
Packit 6c4009
		{
Packit 6c4009
		  /* Send a SIGTTOU signal to our process group.
Packit 6c4009
Packit 6c4009
		     We must remember here not to clobber ERR, since
Packit 6c4009
		     the loop condition below uses it to recall that
Packit 6c4009
		  we should retry after a stop.  */
Packit 6c4009
Packit 6c4009
		  __USEPORT (CTTYID, _hurd_sig_post (0, SIGTTOU, port));
Packit 6c4009
		  /* XXX what to do if error here? */
Packit 6c4009
Packit 6c4009
		  /* At this point we should have just run the handler for
Packit 6c4009
		     SIGTTOU or resumed after being stopped.  Now this is
Packit 6c4009
		     still a "system call", so check to see if we should
Packit 6c4009
		  restart it.  */
Packit 6c4009
		  __spin_lock (&ss->lock);
Packit 6c4009
		  if (!(ss->actions[SIGTTOU].sa_flags & SA_RESTART))
Packit 6c4009
		    err = EINTR;
Packit 6c4009
		  __spin_unlock (&ss->lock);
Packit 6c4009
		}
Packit 6c4009
	    }
Packit 6c4009
	  /* If the last RPC generated a SIGTTOU, loop to try it again.  */
Packit 6c4009
	} while (err == EBACKGROUND);
Packit 6c4009
Packit 6c4009
      return err;
Packit 6c4009
    }
Packit 6c4009
}