]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/setsid.2
capabilities.7: Add pivot_root(2) to CAP_SYS_ADMIN list
[thirdparty/man-pages.git] / man2 / setsid.2
1 .\" Copyright Michael Haardt (michael@cantor.informatik.rwth-aachen.de)
2 .\" Sat Aug 27 20:43:50 MET DST 1994
3 .\" and Copyright (C) 2014, Michael Kerrisk <mtk.manpages@gmail.com>
4 .\"
5 .\" %%%LICENSE_START(GPLv2+_DOC_FULL)
6 .\" This is free documentation; you can redistribute it and/or
7 .\" modify it under the terms of the GNU General Public License as
8 .\" published by the Free Software Foundation; either version 2 of
9 .\" the License, or (at your option) any later version.
10 .\"
11 .\" The GNU General Public License's references to "object code"
12 .\" and "executables" are to be interpreted as the output of any
13 .\" document formatting or typesetting system, including
14 .\" intermediate and printed output.
15 .\"
16 .\" This manual is distributed in the hope that it will be useful,
17 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
18 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 .\" GNU General Public License for more details.
20 .\"
21 .\" You should have received a copy of the GNU General Public
22 .\" License along with this manual; if not, see
23 .\" <http://www.gnu.org/licenses/>.
24 .\" %%%LICENSE_END
25 .\"
26 .\" Modified Sun Sep 11 19:19:05 1994 <faith@cs.unc.edu>
27 .\" Modified Mon Mar 25 10:19:00 1996 <aeb@cwi.nl> (merged a few
28 .\" tiny changes from a man page by Charles Livingston).
29 .\" Modified Sun Jul 21 14:45:46 1996 <aeb@cwi.nl>
30 .\"
31 .TH SETSID 2 2017-09-15 "Linux" "Linux Programmer's Manual"
32 .SH NAME
33 setsid \- creates a session and sets the process group ID
34 .SH SYNOPSIS
35 .ad l
36 .B #include <sys/types.h>
37 .br
38 .B #include <unistd.h>
39 .PP
40 .B pid_t setsid(void);
41 .br
42 .ad b
43 .SH DESCRIPTION
44 .BR setsid ()
45 creates a new session if the calling process is not a
46 process group leader.
47 The calling process is the leader of the new session
48 (i.e., its session ID is made the same as its process ID).
49 The calling process also becomes
50 the process group leader of a new process group in the session
51 (i.e., its process group ID is made the same as its process ID).
52 .PP
53 The calling process will be the only process in
54 the new process group and in the new session.
55 .PP
56 Initially, the new session has no controlling terminal.
57 For details of how a session acquires a controlling terminal, see
58 .BR credentials (7).
59 .SH RETURN VALUE
60 On success, the (new) session ID of the calling process is returned.
61 On error,
62 .I "(pid_t)\ \-1"
63 is returned, and
64 .I errno
65 is set to indicate the error.
66 .SH ERRORS
67 .TP
68 .B EPERM
69 The process group ID of any process equals the PID of the calling process.
70 Thus, in particular,
71 .BR setsid ()
72 fails if the calling process is already a process group leader.
73 .SH CONFORMING TO
74 POSIX.1-2001, POSIX.1-2008, SVr4.
75 .SH NOTES
76 A child created via
77 .BR fork (2)
78 inherits its parent's session ID.
79 The session ID is preserved across an
80 .BR execve (2).
81 .PP
82 A process group leader is a process whose process group ID equals its PID.
83 Disallowing a process group leader from calling
84 .BR setsid ()
85 prevents the possibility that a process group leader places itself
86 in a new session while other processes in the process group remain
87 in the original session;
88 such a scenario would break the strict
89 two-level hierarchy of sessions and process groups.
90 In order to be sure that
91 .BR setsid ()
92 will succeed, call
93 .BR fork (2)
94 and have the parent
95 .BR _exit (2),
96 while the child (which by definition can't be a process group leader) calls
97 .BR setsid ().
98 .PP
99 If a session has a controlling terminal, and the
100 .B CLOCAL
101 flag for that terminal is not set,
102 and a terminal hangup occurs, then the session leader is sent a
103 .BR SIGHUP
104 signal.
105 .PP
106 If a process that is a session leader terminates, then a
107 .B SIGHUP
108 signal is sent to each process in the foreground
109 process group of the controlling terminal.
110 .SH SEE ALSO
111 .BR setsid (1),
112 .BR getsid (2),
113 .BR setpgid (2),
114 .BR setpgrp (2),
115 .BR tcgetsid (3),
116 .BR credentials (7),
117 .BR sched (7)