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