]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/mach/hurd/setsid.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / sysdeps / mach / hurd / setsid.c
CommitLineData
2b778ceb 1/* Copyright (C) 1993-2021 Free Software Foundation, Inc.
ebbad4cc 2 This file is part of the GNU C Library.
28f540f4 3
ebbad4cc 4 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
28f540f4 8
ebbad4cc
UD
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 12 Lesser General Public License for more details.
28f540f4 13
41bdb6e2 14 You should have received a copy of the GNU Lesser General Public
59ba27a6 15 License along with the GNU C Library; if not, see
5a82c748 16 <https://www.gnu.org/licenses/>. */
28f540f4 17
28f540f4
RM
18#include <errno.h>
19#include <unistd.h>
20#include <hurd.h>
21#include <hurd/port.h>
50b076f4 22#include <hurd/fd.h>
868df0f9 23#include <hurd/ioctl.h>
fb4cc8a0 24#include <lowlevellock.h>
28f540f4 25
28f540f4
RM
26/* Create a new session with the calling process as its leader.
27 The process group IDs of the session and the calling process
28 are set to the process ID of the calling process, which is returned. */
50b076f4
RM
29pid_t
30__setsid (void)
28f540f4
RM
31{
32 error_t err;
33 unsigned int stamp;
34
50b076f4
RM
35 HURD_CRITICAL_BEGIN;
36 __mutex_lock (&_hurd_dtable_lock);
37
28f540f4
RM
38 stamp = _hurd_pids_changed_stamp; /* Atomic fetch. */
39
40 /* Tell the proc server we want to start a new session. */
50b076f4 41 err = __USEPORT (PROC, __proc_setsid (port));
72e1a750
RM
42 if (err)
43 __mutex_unlock (&_hurd_dtable_lock);
44 else
45 {
46 /* Punt our current ctty, and update the dtable accordingly. We hold
47 the dtable lock from before the proc_setsid call through clearing
48 the cttyid port and processing the dtable, so that we can be sure
49 that it's all done by the time the signal thread processes the
50 pgrp change notification. */
51 _hurd_locked_install_cttyid (MACH_PORT_NULL);
28f540f4 52
72e1a750
RM
53 /* Synchronize with the signal thread to make sure we have received
54 and processed proc_newids before returning to the user.
55 This is necessary to ensure that _hurd_pgrp (and thus the value
56 returned by `getpgrp ()' in other threads) has been updated before
57 we return. */
58 while (_hurd_pids_changed_stamp == stamp)
bec41242 59 lll_wait (_hurd_pids_changed_stamp, stamp, 0);
72e1a750 60 }
50b076f4
RM
61
62 HURD_CRITICAL_END;
28f540f4 63
50b076f4 64 return err ? __hurd_fail (err) : _hurd_pgrp;
28f540f4
RM
65}
66
67weak_alias (__setsid, setsid)