1 | /* Copyright (C) 1993-2022 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. |
3 | |
4 | The GNU C Library is free software; you can redistribute it and/or |
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. |
8 | |
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 |
12 | Lesser General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU Lesser General Public |
15 | License along with the GNU C Library; if not, see |
16 | <https://www.gnu.org/licenses/>. */ |
17 | |
18 | #include <errno.h> |
19 | #include <unistd.h> |
20 | #include <hurd.h> |
21 | #include <hurd/port.h> |
22 | #include <hurd/fd.h> |
23 | #include <hurd/ioctl.h> |
24 | #include <lowlevellock.h> |
25 | |
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. */ |
29 | pid_t |
30 | __setsid (void) |
31 | { |
32 | error_t err; |
33 | unsigned int stamp; |
34 | |
35 | retry: |
36 | HURD_CRITICAL_BEGIN; |
37 | __mutex_lock (&_hurd_dtable_lock); |
38 | |
39 | stamp = _hurd_pids_changed_stamp; /* Atomic fetch. */ |
40 | |
41 | /* Tell the proc server we want to start a new session. */ |
42 | err = __USEPORT (PROC, __proc_setsid (port)); |
43 | if (err) |
44 | __mutex_unlock (&_hurd_dtable_lock); |
45 | else |
46 | { |
47 | /* Punt our current ctty, and update the dtable accordingly. We hold |
48 | the dtable lock from before the proc_setsid call through clearing |
49 | the cttyid port and processing the dtable, so that we can be sure |
50 | that it's all done by the time the signal thread processes the |
51 | pgrp change notification. */ |
52 | _hurd_locked_install_cttyid (MACH_PORT_NULL); |
53 | |
54 | /* Synchronize with the signal thread to make sure we have received |
55 | and processed proc_newids before returning to the user. |
56 | This is necessary to ensure that _hurd_pgrp (and thus the value |
57 | returned by `getpgrp ()' in other threads) has been updated before |
58 | we return. */ |
59 | while (_hurd_pids_changed_stamp == stamp) |
60 | lll_wait (_hurd_pids_changed_stamp, stamp, 0); |
61 | } |
62 | |
63 | HURD_CRITICAL_END; |
64 | if (err == EINTR) |
65 | /* Got a signal while inside an RPC of the critical section, retry again */ |
66 | goto retry; |
67 | |
68 | return err ? __hurd_fail (err) : _hurd_pgrp; |
69 | } |
70 | |
71 | weak_alias (__setsid, setsid) |
72 | |