mount_setattr.2: Further tweaks after feedback from Christian Brauner
[man-pages.git] / man3 / system.3
blob880fe93c622c1ee542b05b65dfe469e4196e4008
1 .\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
2 .\" and Copyright (c) 2014 by Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein.  The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .\" Modified Sat Jul 24 17:51:15 1993 by Rik Faith (faith@cs.unc.edu)
27 .\" Modified 11 May 1998 by Joseph S. Myers (jsm28@cam.ac.uk)
28 .\" Modified 14 May 2001, 23 Sep 2001 by aeb
29 .\" 2004-12-20, mtk
30 .\"
31 .TH SYSTEM 3  2021-03-22 "" "Linux Programmer's Manual"
32 .SH NAME
33 system \- execute a shell command
34 .SH SYNOPSIS
35 .nf
36 .B #include <stdlib.h>
37 .PP
38 .BI "int system(const char *" "command" );
39 .fi
40 .SH DESCRIPTION
41 The
42 .BR system ()
43 library function uses
44 .BR fork (2)
45 to create a child process that executes the shell command specified in
46 .I command
47 using
48 .BR execl (3)
49 as follows:
50 .PP
51 .in +4n
52 .EX
53 execl("/bin/sh", "sh", "\-c", command, (char *) NULL);
54 .EE
55 .in
56 .PP
57 .BR system ()
58 returns after the command has been completed.
59 .PP
60 During execution of the command,
61 .B SIGCHLD
62 will be blocked, and
63 .B SIGINT
64 and
65 .B SIGQUIT
66 will be ignored, in the process that calls
67 .BR system ().
68 (These signals will be handled according to their defaults inside
69 the child process that executes
70 .IR command .)
71 .PP
73 .I command
74 is NULL, then
75 .BR system ()
76 returns a status indicating whether a shell is available on the system.
77 .SH RETURN VALUE
78 The return value of
79 .BR system ()
80 is one of the following:
81 .IP * 3
83 .I command
84 is NULL, then a nonzero value if a shell is available,
85 or 0 if no shell is available.
86 .IP *
87 If a child process could not be created,
88 or its status could not be retrieved,
89 the return value is \-1 and
90 .I errno
91 is set to indicate the error.
92 .IP *
93 If a shell could not be executed in the child process,
94 then the return value is as though the child shell terminated by calling
95 .BR _exit (2)
96 with the status 127.
97 .IP *
98 If all system calls succeed,
99 then the return value is the termination status of the child shell
100 used to execute
101 .IR command .
102 (The termination status of a shell is the termination status of
103 the last command it executes.)
105 In the last two cases,
106 the return value is a "wait status" that can be examined using
107 the macros described in
108 .BR waitpid (2).
109 (i.e.,
110 .BR WIFEXITED (),
111 .BR WEXITSTATUS (),
112 and so on).
114 .BR system ()
115 does not affect the wait status of any other children.
116 .SH ERRORS
117 .BR system ()
118 can fail with any of the same errors as
119 .BR fork (2).
120 .SH ATTRIBUTES
121 For an explanation of the terms used in this section, see
122 .BR attributes (7).
123 .ad l
126 allbox;
127 lbx lb lb
128 l l l.
129 Interface       Attribute       Value
131 .BR system ()
132 T}      Thread safety   MT-Safe
136 .sp 1
137 .SH CONFORMING TO
138 POSIX.1-2001, POSIX.1-2008, C89, C99.
139 .SH NOTES
140 .BR system ()
141 provides simplicity and convenience:
142 it handles all of the details of calling
143 .BR fork (2),
144 .BR execl (3),
146 .BR waitpid (2),
147 as well as the necessary manipulations of signals;
148 in addition,
149 the shell performs the usual substitutions and I/O redirections for
150 .IR command .
151 The main cost of
152 .BR system ()
153 is inefficiency:
154 additional system calls are required to create the process that
155 runs the shell and to execute the shell.
157 If the
158 .B _XOPEN_SOURCE
159 feature test macro is defined
160 (before including
161 .I any
162 header files),
163 then the macros described in
164 .BR waitpid (2)
165 .RB ( WEXITSTATUS (),
166 etc.) are made available when including
167 .IR <stdlib.h> .
169 As mentioned,
170 .BR system ()
171 ignores
172 .B SIGINT
174 .BR SIGQUIT .
175 This may make programs that call it
176 from a loop uninterruptible, unless they take care themselves
177 to check the exit status of the child.
178 For example:
180 .in +4n
182 while (something) {
183     int ret = system("foo");
185     if (WIFSIGNALED(ret) &&
186         (WTERMSIG(ret) == SIGINT || WTERMSIG(ret) == SIGQUIT))
187             break;
192 According to POSIX.1, it is unspecified whether handlers registered using
193 .BR pthread_atfork (3)
194 are called during the execution of
195 .BR system ().
196 In the glibc implementation, such handlers are not called.
198 In versions of glibc before 2.1.3, the check for the availability of
199 .I /bin/sh
200 was not actually performed if
201 .I command
202 was NULL; instead it was always assumed to be available, and
203 .BR system ()
204 always returned 1 in this case.
205 Since glibc 2.1.3, this check is performed because, even though
206 POSIX.1-2001 requires a conforming implementation to provide
207 a shell, that shell may not be available or executable if
208 the calling program has previously called
209 .BR chroot (2)
210 (which is not specified by POSIX.1-2001).
212 It is possible for the shell command to terminate with a status of 127,
213 which yields a
214 .BR system ()
215 return value that is indistinguishable from the case
216 where a shell could not be executed in the child process.
218 .SS Caveats
219 Do not use
220 .BR system ()
221 from a privileged program
222 (a set-user-ID or set-group-ID program, or a program with capabilities)
223 because strange values for some environment variables
224 might be used to subvert system integrity.
225 For example,
226 .BR PATH
227 could be manipulated so that an arbitrary program
228 is executed with privilege.
229 Use the
230 .BR exec (3)
231 family of functions instead, but not
232 .BR execlp (3)
234 .BR execvp (3)
235 (which also use the
236 .B PATH
237 environment variable to search for an executable).
239 .BR system ()
240 will not, in fact, work properly from programs with set-user-ID or
241 set-group-ID privileges on systems on which
242 .I /bin/sh
243 is bash version 2: as a security measure, bash 2 drops privileges on startup.
244 (Debian uses a different shell,
245 .BR dash (1),
246 which does not do this when invoked as
247 .BR sh .)
249 Any user input that is employed as part of
250 .I command
251 should be
252 .I carefully
253 sanitized, to ensure that unexpected shell commands or command options
254 are not executed.
255 Such risks are especially grave when using
256 .BR system ()
257 from a privileged program.
258 .SH BUGS
259 .\" [BUG 211029](https://bugzilla.kernel.org/show_bug.cgi?id=211029)
260 .\" [Glibc bug](https://sourceware.org/bugzilla/show_bug.cgi?id=27143)
261 .\" [POSIX bug](https://www.austingroupbugs.net/view.php?id=1440)
262 If the command name starts with a hyphen,
263 .BR sh (1)
264 interprets the command name as an option,
265 and the behavior is undefined.
266 (See the
267 .B \-c
268 option to
269 .BR sh (1).)
270 To work around this problem,
271 prepend the command with a space as in the following call:
273 .in +4n
275     system(" \-unfortunate\-command\-name");
278 .SH SEE ALSO
279 .BR sh (1),
280 .BR execve (2),
281 .BR fork (2),
282 .BR sigaction (2),
283 .BR sigprocmask (2),
284 .BR wait (2),
285 .BR exec (3),
286 .BR signal (7)