]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/setsid.2
Many pages: Use correct letter case in page titles (TH)
[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 .\" 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>
21 .PP
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).
33 .PP
34 The calling process will be the only process in
35 the new process group and in the new session.
36 .PP
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-2001, POSIX.1-2008, SVr4.
56 .SH NOTES
57 A child created via
58 .BR fork (2)
59 inherits its parent's session ID.
60 The session ID is preserved across an
61 .BR execve (2).
62 .PP
63 A process group leader is a process whose process group ID equals its PID.
64 Disallowing a process group leader from calling
65 .BR setsid ()
66 prevents the possibility that a process group leader places itself
67 in a new session while other processes in the process group remain
68 in the original session;
69 such a scenario would break the strict
70 two-level hierarchy of sessions and process groups.
71 In order to be sure that
72 .BR setsid ()
73 will succeed, call
74 .BR fork (2)
75 and have the parent
76 .BR _exit (2),
77 while the child (which by definition can't be a process group leader) calls
78 .BR setsid ().
79 .PP
80 If a session has a controlling terminal, and the
81 .B CLOCAL
82 flag for that terminal is not set,
83 and a terminal hangup occurs, then the session leader is sent a
84 .B SIGHUP
85 signal.
86 .PP
87 If a process that is a session leader terminates, then a
88 .B SIGHUP
89 signal is sent to each process in the foreground
90 process group of the controlling terminal.
91 .SH SEE ALSO
92 .BR setsid (1),
93 .BR getsid (2),
94 .BR setpgid (2),
95 .BR setpgrp (2),
96 .BR tcgetsid (3),
97 .BR credentials (7),
98 .BR sched (7)