]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/setpgid.2
libc.7: Add a note on why glibc 2.x uses the soname libc.so.6
[thirdparty/man-pages.git] / man2 / setpgid.2
CommitLineData
fea681da 1.\" Copyright (c) 1983, 1991 Regents of the University of California.
0786e308 2.\" and Copyright (C) 2007, Michael Kerrisk <mtk.manpages@gmail.com>
fea681da
MK
3.\" All rights reserved.
4.\"
a9cd9cb7 5.\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
fea681da
MK
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\" notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\" notice, this list of conditions and the following disclaimer in the
13.\" documentation and/or other materials provided with the distribution.
14.\" 3. All advertising materials mentioning features or use of this software
15.\" must display the following acknowledgement:
16.\" This product includes software developed by the University of
17.\" California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\" may be used to endorse or promote products derived from this software
20.\" without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
8c9302dc 33.\" %%%LICENSE_END
fea681da
MK
34.\"
35.\" @(#)getpgrp.2 6.4 (Berkeley) 3/10/91
36.\"
37.\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
38.\" Modified 1995-04-15 by Michael Chastain <mec@shell.portal.com>:
39.\" Added 'getpgid'.
40.\" Modified 1996-07-21 by Andries Brouwer <aeb@cwi.nl>
41.\" Modified 1996-11-06 by Eric S. Raymond <esr@thyrsus.com>
42.\" Modified 1999-09-02 by Michael Haardt <michael@moria.de>
c11b1abf 43.\" Modified 2002-01-18 by Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 44.\" Modified 2003-01-20 by Andries Brouwer <aeb@cwi.nl>
0bbb92a3
MK
45.\" 2007-07-25, mtk, fairly substantial rewrites and rearrangements
46.\" of text.
fea681da 47.\"
b8efb414 48.TH SETPGID 2 2016-10-08 "Linux" "Linux Programmer's Manual"
fea681da
MK
49.SH NAME
50setpgid, getpgid, setpgrp, getpgrp \- set/get process group
51.SH SYNOPSIS
52.B #include <unistd.h>
53.sp
54.BI "int setpgid(pid_t " pid ", pid_t " pgid );
55.br
56.BI "pid_t getpgid(pid_t " pid );
0bbb92a3 57.sp
72ef5c5c 58.BR "pid_t getpgrp(void);" " /* POSIX.1 version */"
fea681da 59.br
72ef5c5c 60.BI "pid_t getpgrp(pid_t " pid ");\ \ \ \ \ \ \ \ \ \ \ "
0bbb92a3
MK
61/* BSD version */
62.sp
72ef5c5c 63.BR "int setpgrp(void);" " /* System V version */"
fea681da 64.br
72ef5c5c 65.BI "int setpgrp(pid_t " pid ", pid_t " pgid ");\ "
0bbb92a3
MK
66/* BSD version */
67.sp
68.in -4n
69Feature Test Macro Requirements for glibc (see
70.BR feature_test_macros (7)):
71.in
72.sp
cc4615cc 73.ad l
fc541b7a 74.PD 0
0bbb92a3 75.BR getpgid ():
fc541b7a 76.RS 4
cf7fa0a1
MK
77_XOPEN_SOURCE\ >=\ 500
78.\" || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
3ba63d80 79.br
cf7fa0a1 80 || /* Since glibc 2.12: */ _POSIX_C_SOURCE\ >=\ 200809L
fc541b7a
MK
81.RE
82.sp
83.BR setpgrp "() (POSIX.1):"
84.nf
f8619b6a 85 _XOPEN_SOURCE\ >=\ 500
cf7fa0a1 86.\" || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
f8619b6a
MK
87 || /* Since glibc 2.19: */ _DEFAULT_SOURCE
88 || /* Glibc versions <= 2.19: */ _SVID_SOURCE
fc541b7a 89.fi
0bbb92a3
MK
90.sp
91.BR setpgrp "()\ (BSD),"
f8619b6a 92.BR getpgrp "()\ (BSD):"
fc541b7a 93.nf
f8619b6a 94 [These are available only before glibc 2.19]
fc541b7a
MK
95 _BSD_SOURCE &&
96 !\ (_POSIX_SOURCE || _POSIX_C_SOURCE || _XOPEN_SOURCE ||
52d1e2b2 97 _GNU_SOURCE || _SVID_SOURCE)
fc541b7a
MK
98.fi
99.PD
02e92562 100.ad
fea681da 101.SH DESCRIPTION
0bbb92a3
MK
102All of these interfaces are available on Linux,
103and are used for getting and setting the
104process group ID (PGID) of a process.
105The preferred, POSIX.1-specified ways of doing this are:
106.BR getpgrp (void),
107for retrieving the calling process's PGID; and
108.BR setpgid (),
109for setting a process's PGID.
110
e511ffb6 111.BR setpgid ()
0bbb92a3 112sets the PGID of the process specified by
fea681da
MK
113.I pid
114to
115.IR pgid .
116If
117.I pid
17c69289 118is zero, then the process ID of the calling process is used.
c13182ef 119If
fea681da 120.I pgid
17c69289 121is zero, then the PGID of the process specified by
fea681da 122.I pid
17c69289 123is made the same as its process ID.
60a90ecd
MK
124If
125.BR setpgid ()
126is used to move a process from one process
fea681da 127group to another (as is done by some shells when creating pipelines),
0bbb92a3
MK
128both process groups must be part of the same session (see
129.BR setsid (2)
e0bf9127 130and
0bbb92a3 131.BR credentials (7)).
c13182ef 132In this case,
fea681da
MK
133the \fIpgid\fP specifies an existing process group to be joined and the
134session ID of that group must match the session ID of the joining process.
135
0bbb92a3
MK
136The POSIX.1 version of
137.BR getpgrp (),
138which takes no arguments,
139returns the PGID of the calling process.
140
e511ffb6 141.BR getpgid ()
0bbb92a3 142returns the PGID of the process specified by
fea681da
MK
143.IR pid .
144If
145.I pid
a1ffe9f5 146is zero, the process ID of the calling process is used.
0bbb92a3
MK
147(Retrieving the PGID of a process other than the caller is rarely
148necessary, and the POSIX.1
149.BR getpgrp ()
150is preferred for that task.)
fea681da 151
efbfd7ec 152The System\ V-style
aadd7709
MK
153.BR setpgrp (),
154which takes no arguments, is equivalent to
ab7b478b 155.IR "setpgid(0,\ 0)" .
fea681da 156
0bbb92a3
MK
157The BSD-specific
158.BR setpgrp ()
159call, which takes arguments
160.I pid
fea681da 161and
0bbb92a3 162.IR pgid ,
8e6ee8f3 163is a wrapper function that calls
ded44f9b
MK
164
165 setpgid(pid, pgid)
166
0bbb92a3
MK
167.\" The true BSD setpgrp() system call differs in allowing the PGID
168.\" to be set to arbitrary values, rather than being restricted to
169.\" PGIDs in the same session.
ded44f9b
MK
170Since glibc 2.19, the BSD-specific
171.BR setpgrp ()
172function is no longer exposed by
173.IR <unistd.h> ;
174calls should be replaced with the
175.BR setpgid ()
176call shown above.
fea681da 177
0bbb92a3
MK
178The BSD-specific
179.BR getpgrp ()
180call, which takes a single
181.I pid
ded44f9b
MK
182argument, is a wrapper function that calls
183
184 getpgid(pid)
185
186Since glibc 2.19, the BSD-specific
187.BR getpgrp ()
188function is no longer exposed by
189.IR <unistd.h> ;
190calls should be replaced with calls to the POSIX.1
191.BR getpgrp ()
192which takes no arguments (if the intent is to obtain the caller's PGID),
193or with the
194.BR getpgid ()
195call shown above.
47297adb 196.SH RETURN VALUE
fea681da 197On success,
ae050d9a 198.BR setpgid ()
c13182ef 199and
ae050d9a 200.BR setpgrp ()
c13182ef
MK
201return zero.
202On error, \-1 is returned, and
fea681da
MK
203.I errno
204is set appropriately.
205
0bbb92a3
MK
206The POSIX.1
207.BR getpgrp ()
aadd7709 208always returns the PGID of the caller.
0bbb92a3
MK
209
210.BR getpgid (),
211and the BSD-specific
212.BR getpgrp ()
213return a process group on success.
fea681da
MK
214On error, \-1 is returned, and
215.I errno
216is set appropriately.
fea681da
MK
217.SH ERRORS
218.TP
219.B EACCES
220An attempt was made to change the process group ID
221of one of the children of the calling process and the child had
60a90ecd
MK
222already performed an
223.BR execve (2)
224.RB ( setpgid (),
225.BR setpgrp ()).
fea681da
MK
226.TP
227.B EINVAL
228.I pgid
229is less than 0
60a90ecd
MK
230.RB ( setpgid (),
231.BR setpgrp ()).
fea681da
MK
232.TP
233.B EPERM
c13182ef
MK
234An attempt was made to move a process into a process group in a
235different session, or to change the process
236group ID of one of the children of the calling process and the
fea681da
MK
237child was in a different session, or to change the process group ID of
238a session leader
60a90ecd
MK
239.RB ( setpgid (),
240.BR setpgrp ()).
fea681da
MK
241.TP
242.B ESRCH
243For
e511ffb6 244.BR getpgid ():
fea681da
MK
245.I pid
246does not match any process.
247For
e511ffb6 248.BR setpgid ():
fea681da 249.I pid
a1ffe9f5 250is not the calling process and not a child of the calling process.
47297adb 251.SH CONFORMING TO
e511ffb6 252.BR setpgid ()
0bbb92a3 253and the version of
e511ffb6 254.BR getpgrp ()
d68b2713 255with no arguments
97c1eac8 256conform to POSIX.1-2001.
0bbb92a3
MK
257
258POSIX.1-2001 also specifies
e511ffb6 259.BR getpgid ()
0bbb92a3
MK
260and the version of
261.BR setpgrp ()
262that takes no arguments.
55cfc574
MK
263(POSIX.1-2008 marks this
264.BR setpgrp ()
265specification as obsolete.)
0bbb92a3
MK
266
267The version of
268.BR getpgrp ()
269with one argument and the version of
270.BR setpgrp ()
271that takes two arguments derive from 4.2BSD,
272and are not specified by POSIX.1.
fea681da 273.SH NOTES
c13182ef 274A child created via
73ac0666
MK
275.BR fork (2)
276inherits its parent's process group ID.
aadd7709 277The PGID is preserved across an
73ac0666
MK
278.BR execve (2).
279
0bbb92a3
MK
280Each process group is a member of a session and each process is a
281member of the session of which its process group is a member.
1a765504
MK
282(See
283.BR credentials (7).)
0bbb92a3
MK
284
285A session can have a controlling terminal.
286At any time, one (and only one) of the process groups
287in the session can be the foreground process group
288for the terminal;
289the remaining process groups are in the background.
290If a signal is generated from the terminal (e.g., typing the
291interrupt key to generate
292.BR SIGINT ),
293that signal is sent to the foreground process group.
294(See
295.BR termios (3)
296for a description of the characters that generate signals.)
aadd7709
MK
297Only the foreground process group may
298.BR read (2)
299from the terminal;
300if a background process group tries to
301.BR read (2)
15b17877 302from the terminal, then the group is sent a
07bfe93f 303.B SIGTTIN
0bbb92a3
MK
304signal, which suspends it.
305The
306.BR tcgetpgrp (3)
307and
308.BR tcsetpgrp (3)
309functions are used to get/set the foreground
310process group of the controlling terminal.
311
312The
e511ffb6 313.BR setpgid ()
c3dfd2c8 314and
0bbb92a3
MK
315.BR getpgrp ()
316calls are used by programs such as
317.BR bash (1)
318to create process groups in order to implement shell job control.
319
343313da 320If the termination of a process causes a process group to become orphaned,
0bbb92a3
MK
321and if any member of the newly orphaned process group is stopped, then a
322.B SIGHUP
323signal followed by a
324.B SIGCONT
325signal will be sent to each process
326in the newly orphaned process group.
a38dc43a
MK
327.\" exit.3 refers to the following text:
328An orphaned process group is one in which the parent of
329every member of process group is either itself also a member
330of the process group or is a member of a process group
331in a different session (see also
332.BR credentials (7)).
47297adb 333.SH SEE ALSO
fea681da
MK
334.BR getuid (2),
335.BR setsid (2),
336.BR tcgetpgrp (3),
337.BR tcsetpgrp (3),
0786e308
MK
338.BR termios (3),
339.BR credentials (7)