Blame hurd/ctty-input.c

Packit 6c4009
/* _hurd_ctty_input -- Do an input RPC and generate SIGTTIN 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 SIGTTIN or EIO as appropriate.  */
Packit 6c4009
Packit 6c4009
error_t
Packit 6c4009
_hurd_ctty_input (io_t port, io_t ctty, error_t (*rpc) (io_t))
Packit 6c4009
{
Packit 6c4009
  error_t err;
Packit 6c4009
Packit 6c4009
  if (ctty == MACH_PORT_NULL)
Packit 6c4009
    return (*rpc) (port);
Packit 6c4009
Packit 6c4009
  do
Packit 6c4009
    {
Packit 6c4009
      err = (*rpc) (ctty);
Packit 6c4009
      if (err == EBACKGROUND)
Packit 6c4009
	{
Packit 6c4009
	  /* We are a background job and tried to read from the tty.
Packit 6c4009
	     We should probably get a SIGTTIN signal.  */
Packit 6c4009
	  if (_hurd_orphaned)
Packit 6c4009
	    /* Our process group is orphaned.  Don't stop; just fail.  */
Packit 6c4009
	    err = EIO;
Packit 6c4009
	  else
Packit 6c4009
	    {
Packit 6c4009
	      struct hurd_sigstate *ss = _hurd_self_sigstate ();
Packit 6c4009
	      __spin_lock (&ss->lock);
Packit 6c4009
	      if (__sigismember (&ss->blocked, SIGTTIN) ||
Packit 6c4009
		  ss->actions[SIGTTIN].sa_handler == SIG_IGN)
Packit 6c4009
		/* We are blocking or ignoring SIGTTIN.  Just fail.  */
Packit 6c4009
		err = EIO;
Packit 6c4009
	      __spin_unlock (&ss->lock);
Packit 6c4009
Packit 6c4009
	      if (err == EBACKGROUND)
Packit 6c4009
		{
Packit 6c4009
		  /* Send a SIGTTIN 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, SIGTTIN, 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
		     SIGTTIN 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[SIGTTIN].sa_flags & SA_RESTART))
Packit 6c4009
		    err = EINTR;
Packit 6c4009
		  __spin_unlock (&ss->lock);
Packit 6c4009
		}
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      /* If the last RPC generated a SIGTTIN, loop to try it again.  */
Packit 6c4009
    } while (err == EBACKGROUND);
Packit 6c4009
Packit 6c4009
  return err;
Packit 6c4009
}