Changes.old: tfix
[man-pages.git] / man2 / setsid.2
blob3ee4e2a66b2785ecc764b3ab2a89d82191bfa332
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 .\" SPDX-License-Identifier: GPL-2.0-or-later
6 .\"
7 .\" Modified Sun Sep 11 19:19:05 1994 <faith@cs.unc.edu>
8 .\" Modified Mon Mar 25 10:19:00 1996 <aeb@cwi.nl> (merged a few
9 .\"     tiny changes from a man page by Charles Livingston).
10 .\" Modified Sun Jul 21 14:45:46 1996 <aeb@cwi.nl>
11 .\"
12 .TH setsid 2 (date) "Linux man-pages (unreleased)"
13 .SH NAME
14 setsid \- creates a session and sets the process group ID
15 .SH LIBRARY
16 Standard C library
17 .RI ( libc ", " \-lc )
18 .SH SYNOPSIS
19 .nf
20 .B #include <unistd.h>
22 .B pid_t setsid(void);
23 .fi
24 .SH DESCRIPTION
25 .BR setsid ()
26 creates a new session if the calling process is not a
27 process group leader.
28 The calling process is the leader of the new session
29 (i.e., its session ID is made the same as its process ID).
30 The calling process also becomes
31 the process group leader of a new process group in the session
32 (i.e., its process group ID is made the same as its process ID).
34 The calling process will be the only process in
35 the new process group and in the new session.
37 Initially, the new session has no controlling terminal.
38 For details of how a session acquires a controlling terminal, see
39 .BR credentials (7).
40 .SH RETURN VALUE
41 On success, the (new) session ID of the calling process is returned.
42 On error,
43 .I "(pid_t)\ \-1"
44 is returned, and
45 .I errno
46 is set to indicate the error.
47 .SH ERRORS
48 .TP
49 .B EPERM
50 The process group ID of any process equals the PID of the calling process.
51 Thus, in particular,
52 .BR setsid ()
53 fails if the calling process is already a process group leader.
54 .SH STANDARDS
55 POSIX.1-2008.
56 .SH HISTORY
57 POSIX.1-2001, SVr4.
58 .SH NOTES
59 A child created via
60 .BR fork (2)
61 inherits its parent's session ID.
62 The session ID is preserved across an
63 .BR execve (2).
65 A process group leader is a process whose process group ID equals its PID.
66 Disallowing a process group leader from calling
67 .BR setsid ()
68 prevents the possibility that a process group leader places itself
69 in a new session while other processes in the process group remain
70 in the original session;
71 such a scenario would break the strict
72 two-level hierarchy of sessions and process groups.
73 In order to be sure that
74 .BR setsid ()
75 will succeed, call
76 .BR fork (2)
77 and have the parent
78 .BR _exit (2),
79 while the child (which by definition can't be a process group leader) calls
80 .BR setsid ().
82 If a session has a controlling terminal, and the
83 .B CLOCAL
84 flag for that terminal is not set,
85 and a terminal hangup occurs, then the session leader is sent a
86 .B SIGHUP
87 signal.
89 If a process that is a session leader terminates, then a
90 .B SIGHUP
91 signal is sent to each process in the foreground
92 process group of the controlling terminal.
93 .SH SEE ALSO
94 .BR setsid (1),
95 .BR getsid (2),
96 .BR setpgid (2),
97 .BR setpgrp (2),
98 .BR tcgetsid (3),
99 .BR credentials (7),
100 .BR sched (7)