Blame sysdeps/mach/hurd/setsid.c

Packit 6c4009
/* Copyright (C) 1993-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 <errno.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <hurd.h>
Packit 6c4009
#include <hurd/port.h>
Packit 6c4009
#include <hurd/fd.h>
Packit 6c4009
#include <hurd/ioctl.h>
Packit 6c4009
#include <lowlevellock.h>
Packit 6c4009
Packit 6c4009
/* Create a new session with the calling process as its leader.
Packit 6c4009
   The process group IDs of the session and the calling process
Packit 6c4009
   are set to the process ID of the calling process, which is returned.  */
Packit 6c4009
pid_t
Packit 6c4009
__setsid (void)
Packit 6c4009
{
Packit 6c4009
  error_t err;
Packit 6c4009
  unsigned int stamp;
Packit 6c4009
Packit 6c4009
  HURD_CRITICAL_BEGIN;
Packit 6c4009
  __mutex_lock (&_hurd_dtable_lock);
Packit 6c4009
Packit 6c4009
  stamp = _hurd_pids_changed_stamp; /* Atomic fetch.  */
Packit 6c4009
Packit 6c4009
  /* Tell the proc server we want to start a new session.  */
Packit 6c4009
  err = __USEPORT (PROC, __proc_setsid (port));
Packit 6c4009
  if (err)
Packit 6c4009
    __mutex_unlock (&_hurd_dtable_lock);
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      /* Punt our current ctty, and update the dtable accordingly.  We hold
Packit 6c4009
	 the dtable lock from before the proc_setsid call through clearing
Packit 6c4009
	 the cttyid port and processing the dtable, so that we can be sure
Packit 6c4009
	 that it's all done by the time the signal thread processes the
Packit 6c4009
	 pgrp change notification.  */
Packit 6c4009
      _hurd_locked_install_cttyid (MACH_PORT_NULL);
Packit 6c4009
Packit 6c4009
      /* Synchronize with the signal thread to make sure we have received
Packit 6c4009
	 and processed proc_newids before returning to the user.
Packit 6c4009
	 This is necessary to ensure that _hurd_pgrp (and thus the value
Packit 6c4009
	 returned by `getpgrp ()' in other threads) has been updated before
Packit 6c4009
	 we return.  */
Packit 6c4009
      while (_hurd_pids_changed_stamp == stamp)
Packit 6c4009
        lll_wait (&_hurd_pids_changed_stamp, stamp, 0);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  HURD_CRITICAL_END;
Packit 6c4009
Packit 6c4009
  return err ? __hurd_fail (err) : _hurd_pgrp;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
weak_alias (__setsid, setsid)