]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/setsid.2
setsid.2: Improve wording of text on calling setsid() after fork()+_exit()
[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 2016-10-08 "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 <unistd.h>
37 .sp
38 .B pid_t setsid(void);
39 .br
40 .ad b
41 .SH DESCRIPTION
42 .BR setsid ()
43 creates a new session if the calling process is not a
44 process group leader.
45 The calling process is the leader of the new session
46 (i.e., its session ID is made the same as its process ID).
47 The calling process also becomes
48 the process group leader of a new process group in the session
49 (i.e., its process group ID is made the same as its process ID).
50
51 The calling process will be the only process in
52 the new process group and in the new session.
53
54 Initially, the new session has no controlling terminal.
55 For details of how a session acquires a controlling terminal, see
56 .BR credentials (7).
57 .SH RETURN VALUE
58 On success, the (new) session ID of the calling process is returned.
59 On error,
60 .I "(pid_t)\ \-1"
61 is returned, and
62 .I errno
63 is set to indicate the error.
64 .SH ERRORS
65 .TP
66 .B EPERM
67 The process group ID of any process equals the PID of the calling process.
68 Thus, in particular,
69 .BR setsid ()
70 fails if the calling process is already a process group leader.
71 .SH CONFORMING TO
72 POSIX.1-2001, POSIX.1-2008, SVr4.
73 .SH NOTES
74 A child created via
75 .BR fork (2)
76 inherits its parent's session ID.
77 The session ID is preserved across an
78 .BR execve (2).
79
80 A process group leader is a process whose process group ID equals its PID.
81 Disallowing a process group leader from calling
82 .BR setsid ()
83 prevents the possibility that a process group leader places itself
84 in a new session while other processes in the process group remain
85 in the original session;
86 such a scenario would break the strict
87 two-level hierarchy of sessions and process groups.
88 In order to be sure that
89 .BR setsid ()
90 will succeed, call
91 .BR fork (2)
92 and have the parent
93 .BR _exit (2),
94 while the child (which by definition can't be a process group leader) calls
95 .BR setsid ().
96
97 If a session has a controlling terminal, and the
98 .B CLOCAL
99 flag for that terminal is not set,
100 and a terminal hangup occurs, then the session leader is sent a
101 .BR SIGHUP
102 signal.
103
104 If a process that is a session leader terminates, then a
105 .B SIGHUP
106 signal is sent to each process in the foreground
107 process group of the controlling terminal.
108 .SH SEE ALSO
109 .BR setsid (1),
110 .BR getsid (2),
111 .BR setpgid (2),
112 .BR setpgrp (2),
113 .BR tcgetsid (3),
114 .BR credentials (7)