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