1 .\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
2 .\" and Copyright (c) 2014 by Michael Kerrisk <mtk.manpages@gmail.com>
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.
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.
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
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
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
31 .TH SYSTEM 3 2019-03-06 "" "Linux Programmer's Manual"
33 system \- execute a shell command
36 .B #include <stdlib.h>
38 .BI "int system(const char *" "command" );
45 to create a child process that executes the shell command specified in
53 execl("/bin/sh", "sh", "-c", command, (char *) NULL);
58 returns after the command has been completed.
60 During execution of the command,
66 will be ignored, in the process that calls
68 (These signals will be handled according to their defaults inside
69 the child process that executes
76 returns a status indicating whether a shell is available on the system.
80 is one of the following:
84 is NULL, then a nonzero value if a shell is available,
85 or 0 if no shell is available.
87 If a child process could not be created,
88 or its status could not be retrieved,
89 the return value is \-1 and
91 is set to indicate the error.
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
98 If all system calls succeed,
99 then the return value is the termination status of the child shell
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
115 does not affect the wait status of any other children.
118 can fail with any of the same errors as
121 For an explanation of the terms used in this section, see
127 Interface Attribute Value
130 T} Thread safety MT-Safe
133 POSIX.1-2001, POSIX.1-2008, C89, C99.
136 provides simplicity and convenience:
137 it handles all of the details of calling
142 as well as the necessary manipulations of signals;
144 the shell performs the usual substitutions and I/O redirections for
149 additional system calls are required to create the process that
150 runs the shell and to execute the shell.
154 feature test macro is defined
158 then the macros described in
160 .RB ( WEXITSTATUS (),
161 etc.) are made available when including
170 This may make programs that call it
171 from a loop uninterruptible, unless they take care themselves
172 to check the exit status of the child.
178 int ret = system("foo");
180 if (WIFSIGNALED(ret) &&
181 (WTERMSIG(ret) == SIGINT || WTERMSIG(ret) == SIGQUIT))
187 According to POSIX.1, it is unspecified whether handlers registered using
188 .BR pthread_atfork (3)
189 are called during the execution of
191 In the glibc implementation, such handlers are not called.
193 In versions of glibc before 2.1.3, the check for the availability of
195 was not actually performed if
197 was NULL; instead it was always assumed to be available, and
199 always returned 1 in this case.
200 Since glibc 2.1.3, this check is performed because, even though
201 POSIX.1-2001 requires a conforming implementation to provide
202 a shell, that shell may not be available or executable if
203 the calling program has previously called
205 (which is not specified by POSIX.1-2001).
207 It is possible for the shell command to terminate with a status of 127,
210 return value that is indistinguishable from the case
211 where a shell could not be executed in the child process.
217 from a privileged program
218 (a set-user-ID or set-group-ID program, or a program with capabilities)
219 because strange values for some environment variables
220 might be used to subvert system integrity.
223 could be manipulated so that an arbitrary program
224 is executed with privilege.
227 family of functions instead, but not
233 environment variable to search for an executable).
236 will not, in fact, work properly from programs with set-user-ID or
237 set-group-ID privileges on systems on which
239 is bash version 2: as a security measure, bash 2 drops privileges on startup.
240 (Debian uses a different shell,
242 which does not do this when invoked as
245 Any user input that is employed as part of
249 sanitized, to ensure that unexpected shell commands or command options
251 Such risks are especially grave when using
253 from a privileged program.