|
Packit |
7cfc04 |
.\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
|
|
Packit |
7cfc04 |
.\" and Copyright (c) 2014 by Michael Kerrisk <mtk.manpages@gmail.com>
|
|
Packit |
7cfc04 |
.\"
|
|
Packit |
7cfc04 |
.\" %%%LICENSE_START(VERBATIM)
|
|
Packit |
7cfc04 |
.\" Permission is granted to make and distribute verbatim copies of this
|
|
Packit |
7cfc04 |
.\" manual provided the copyright notice and this permission notice are
|
|
Packit |
7cfc04 |
.\" preserved on all copies.
|
|
Packit |
7cfc04 |
.\"
|
|
Packit |
7cfc04 |
.\" Permission is granted to copy and distribute modified versions of this
|
|
Packit |
7cfc04 |
.\" manual under the conditions for verbatim copying, provided that the
|
|
Packit |
7cfc04 |
.\" entire resulting derived work is distributed under the terms of a
|
|
Packit |
7cfc04 |
.\" permission notice identical to this one.
|
|
Packit |
7cfc04 |
.\"
|
|
Packit |
7cfc04 |
.\" Since the Linux kernel and libraries are constantly changing, this
|
|
Packit |
7cfc04 |
.\" manual page may be incorrect or out-of-date. The author(s) assume no
|
|
Packit |
7cfc04 |
.\" responsibility for errors or omissions, or for damages resulting from
|
|
Packit |
7cfc04 |
.\" the use of the information contained herein. The author(s) may not
|
|
Packit |
7cfc04 |
.\" have taken the same level of care in the production of this manual,
|
|
Packit |
7cfc04 |
.\" which is licensed free of charge, as they might when working
|
|
Packit |
7cfc04 |
.\" professionally.
|
|
Packit |
7cfc04 |
.\"
|
|
Packit |
7cfc04 |
.\" Formatted or processed versions of this manual, if unaccompanied by
|
|
Packit |
7cfc04 |
.\" the source, must acknowledge the copyright and authors of this work.
|
|
Packit |
7cfc04 |
.\" %%%LICENSE_END
|
|
Packit |
7cfc04 |
.\"
|
|
Packit |
7cfc04 |
.\" Modified Sat Jul 24 17:51:15 1993 by Rik Faith (faith@cs.unc.edu)
|
|
Packit |
7cfc04 |
.\" Modified 11 May 1998 by Joseph S. Myers (jsm28@cam.ac.uk)
|
|
Packit |
7cfc04 |
.\" Modified 14 May 2001, 23 Sep 2001 by aeb
|
|
Packit |
7cfc04 |
.\" 2004-12-20, mtk
|
|
Packit |
7cfc04 |
.\"
|
|
Packit |
7cfc04 |
.TH SYSTEM 3 2017-09-15 "" "Linux Programmer's Manual"
|
|
Packit |
7cfc04 |
.SH NAME
|
|
Packit |
7cfc04 |
system \- execute a shell command
|
|
Packit |
7cfc04 |
.SH SYNOPSIS
|
|
Packit |
7cfc04 |
.nf
|
|
Packit |
7cfc04 |
.B #include <stdlib.h>
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
.BI "int system(const char *" "command" );
|
|
Packit |
7cfc04 |
.fi
|
|
Packit |
7cfc04 |
.SH DESCRIPTION
|
|
Packit |
7cfc04 |
The
|
|
Packit |
7cfc04 |
.BR system ()
|
|
Packit |
7cfc04 |
library function uses
|
|
Packit |
7cfc04 |
.BR fork (2)
|
|
Packit |
7cfc04 |
to create a child process that executes the shell command specified in
|
|
Packit |
7cfc04 |
.I command
|
|
Packit |
7cfc04 |
using
|
|
Packit |
7cfc04 |
.BR execl (3)
|
|
Packit |
7cfc04 |
as follows:
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
execl("/bin/sh", "sh", "-c", command, (char *) 0);
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
.BR system ()
|
|
Packit |
7cfc04 |
returns after the command has been completed.
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
During execution of the command,
|
|
Packit |
7cfc04 |
.B SIGCHLD
|
|
Packit |
7cfc04 |
will be blocked, and
|
|
Packit |
7cfc04 |
.B SIGINT
|
|
Packit |
7cfc04 |
and
|
|
Packit |
7cfc04 |
.B SIGQUIT
|
|
Packit |
7cfc04 |
will be ignored, in the process that calls
|
|
Packit |
7cfc04 |
.BR system ()
|
|
Packit |
7cfc04 |
(these signals will be handled according to their defaults inside
|
|
Packit |
7cfc04 |
the child process that executes
|
|
Packit |
7cfc04 |
.IR command ).
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
If
|
|
Packit |
7cfc04 |
.I command
|
|
Packit |
7cfc04 |
is NULL, then
|
|
Packit |
7cfc04 |
.BR system ()
|
|
Packit |
7cfc04 |
returns a status indicating whether a shell is available on the system.
|
|
Packit |
7cfc04 |
.SH RETURN VALUE
|
|
Packit |
7cfc04 |
The return value of
|
|
Packit |
7cfc04 |
.BR system ()
|
|
Packit |
7cfc04 |
is one of the following:
|
|
Packit |
7cfc04 |
.IP * 3
|
|
Packit |
7cfc04 |
If
|
|
Packit |
7cfc04 |
.I command
|
|
Packit |
7cfc04 |
is NULL, then a nonzero value if a shell is available,
|
|
Packit |
7cfc04 |
or 0 if no shell is available.
|
|
Packit |
7cfc04 |
.IP *
|
|
Packit |
7cfc04 |
If a child process could not be created,
|
|
Packit |
7cfc04 |
or its status could not be retrieved,
|
|
Packit |
7cfc04 |
the return value is \-1.
|
|
Packit |
7cfc04 |
.IP *
|
|
Packit |
7cfc04 |
If a shell could not be executed in the child process,
|
|
Packit |
7cfc04 |
then the return value is as though the child shell terminated by calling
|
|
Packit |
7cfc04 |
.BR _exit (2)
|
|
Packit |
7cfc04 |
with the status 127.
|
|
Packit |
7cfc04 |
.IP *
|
|
Packit |
7cfc04 |
If all system calls succeed,
|
|
Packit |
7cfc04 |
then the return value is the termination status of the child shell
|
|
Packit |
7cfc04 |
used to execute
|
|
Packit |
7cfc04 |
.IR command .
|
|
Packit |
7cfc04 |
(The termination status of a shell is the termination status of
|
|
Packit |
7cfc04 |
the last command it executes.)
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
In the last two cases,
|
|
Packit |
7cfc04 |
the return value is a "wait status" that can be examined using
|
|
Packit |
7cfc04 |
the macros described in
|
|
Packit |
7cfc04 |
.BR waitpid (2).
|
|
Packit |
7cfc04 |
(i.e.,
|
|
Packit |
7cfc04 |
.BR WIFEXITED (),
|
|
Packit |
7cfc04 |
.BR WEXITSTATUS (),
|
|
Packit |
7cfc04 |
and so on).
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
.BR system ()
|
|
Packit |
7cfc04 |
does not affect the wait status of any other children.
|
|
Packit |
7cfc04 |
.SH ATTRIBUTES
|
|
Packit |
7cfc04 |
For an explanation of the terms used in this section, see
|
|
Packit |
7cfc04 |
.BR attributes (7).
|
|
Packit |
7cfc04 |
.TS
|
|
Packit |
7cfc04 |
allbox;
|
|
Packit |
7cfc04 |
lb lb lb
|
|
Packit |
7cfc04 |
l l l.
|
|
Packit |
7cfc04 |
Interface Attribute Value
|
|
Packit |
7cfc04 |
T{
|
|
Packit |
7cfc04 |
.BR system ()
|
|
Packit |
7cfc04 |
T} Thread safety MT-Safe
|
|
Packit |
7cfc04 |
.TE
|
|
Packit |
7cfc04 |
.SH CONFORMING TO
|
|
Packit |
7cfc04 |
POSIX.1-2001, POSIX.1-2008, C89, C99.
|
|
Packit |
7cfc04 |
.SH NOTES
|
|
Packit |
7cfc04 |
.BR system ()
|
|
Packit |
7cfc04 |
provides simplicity and convenience:
|
|
Packit |
7cfc04 |
it handles all of the details of calling
|
|
Packit |
7cfc04 |
.BR fork (2),
|
|
Packit |
7cfc04 |
.BR execl (3),
|
|
Packit |
7cfc04 |
and
|
|
Packit |
7cfc04 |
.BR waitpid (2),
|
|
Packit |
7cfc04 |
as well as the necessary manipulations of signals;
|
|
Packit |
7cfc04 |
in addition,
|
|
Packit |
7cfc04 |
the shell performs the usual substitutions and I/O redirections for
|
|
Packit |
7cfc04 |
.IR command .
|
|
Packit |
7cfc04 |
The main cost of
|
|
Packit |
7cfc04 |
.BR system ()
|
|
Packit |
7cfc04 |
is inefficiency:
|
|
Packit |
7cfc04 |
additional system calls are required to create the process that
|
|
Packit |
7cfc04 |
runs the shell and to execute the shell.
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
If the
|
|
Packit |
7cfc04 |
.B _XOPEN_SOURCE
|
|
Packit |
7cfc04 |
feature test macro is defined
|
|
Packit |
7cfc04 |
(before including
|
|
Packit |
7cfc04 |
.I any
|
|
Packit |
7cfc04 |
header files),
|
|
Packit |
7cfc04 |
then the macros described in
|
|
Packit |
7cfc04 |
.BR waitpid (2)
|
|
Packit |
7cfc04 |
.RB ( WEXITSTATUS (),
|
|
Packit |
7cfc04 |
etc.) are made available when including
|
|
Packit |
7cfc04 |
.IR <stdlib.h> .
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
As mentioned,
|
|
Packit |
7cfc04 |
.BR system ()
|
|
Packit |
7cfc04 |
ignores
|
|
Packit |
7cfc04 |
.B SIGINT
|
|
Packit |
7cfc04 |
and
|
|
Packit |
7cfc04 |
.BR SIGQUIT .
|
|
Packit |
7cfc04 |
This may make programs that call it
|
|
Packit |
7cfc04 |
from a loop uninterruptible, unless they take care themselves
|
|
Packit |
7cfc04 |
to check the exit status of the child.
|
|
Packit |
7cfc04 |
For example:
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
.in +4n
|
|
Packit |
7cfc04 |
.EX
|
|
Packit |
7cfc04 |
while (something) {
|
|
Packit |
7cfc04 |
int ret = system("foo");
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
if (WIFSIGNALED(ret) &&
|
|
Packit |
7cfc04 |
(WTERMSIG(ret) == SIGINT || WTERMSIG(ret) == SIGQUIT))
|
|
Packit |
7cfc04 |
break;
|
|
Packit |
7cfc04 |
}
|
|
Packit |
7cfc04 |
.EE
|
|
Packit |
7cfc04 |
.in
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
According to POSIX.1, it is unspecified whether handlers registered using
|
|
Packit |
7cfc04 |
.BR pthread_atfork (3)
|
|
Packit |
7cfc04 |
are called during the execution of
|
|
Packit |
7cfc04 |
.BR system ().
|
|
Packit |
7cfc04 |
In the glibc implementation, such handlers are not called.
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
In versions of glibc before 2.1.3, the check for the availability of
|
|
Packit |
7cfc04 |
.I /bin/sh
|
|
Packit |
7cfc04 |
was not actually performed if
|
|
Packit |
7cfc04 |
.I command
|
|
Packit |
7cfc04 |
was NULL; instead it was always assumed to be available, and
|
|
Packit |
7cfc04 |
.BR system ()
|
|
Packit |
7cfc04 |
always returned 1 in this case.
|
|
Packit |
7cfc04 |
Since glibc 2.1.3, this check is performed because, even though
|
|
Packit |
7cfc04 |
POSIX.1-2001 requires a conforming implementation to provide
|
|
Packit |
7cfc04 |
a shell, that shell may not be available or executable if
|
|
Packit |
7cfc04 |
the calling program has previously called
|
|
Packit |
7cfc04 |
.BR chroot (2)
|
|
Packit |
7cfc04 |
(which is not specified by POSIX.1-2001).
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
It is possible for the shell command to terminate with a status of 127,
|
|
Packit |
7cfc04 |
which yields a
|
|
Packit |
7cfc04 |
.BR system ()
|
|
Packit |
7cfc04 |
return value that is indistinguishable from the case
|
|
Packit |
7cfc04 |
where a shell could not be executed in the child process.
|
|
Packit |
7cfc04 |
.\"
|
|
Packit |
7cfc04 |
.SS Caveats
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
Do not use
|
|
Packit |
7cfc04 |
.BR system ()
|
|
Packit |
7cfc04 |
from a privileged program
|
|
Packit |
7cfc04 |
(a set-user-ID or set-group-ID program, or a program with capabilities)
|
|
Packit |
7cfc04 |
because strange values for some environment variables
|
|
Packit |
7cfc04 |
might be used to subvert system integrity.
|
|
Packit |
7cfc04 |
For example,
|
|
Packit |
7cfc04 |
.BR PATH
|
|
Packit |
7cfc04 |
could be manipulated so that an arbitrary program
|
|
Packit |
7cfc04 |
is executed with privilege.
|
|
Packit |
7cfc04 |
Use the
|
|
Packit |
7cfc04 |
.BR exec (3)
|
|
Packit |
7cfc04 |
family of functions instead, but not
|
|
Packit |
7cfc04 |
.BR execlp (3)
|
|
Packit |
7cfc04 |
or
|
|
Packit |
7cfc04 |
.BR execvp (3)
|
|
Packit |
7cfc04 |
(which also use the
|
|
Packit |
7cfc04 |
.B PATH
|
|
Packit |
7cfc04 |
environment variable to search for an executable).
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
.BR system ()
|
|
Packit |
7cfc04 |
will not, in fact, work properly from programs with set-user-ID or
|
|
Packit |
7cfc04 |
set-group-ID privileges on systems on which
|
|
Packit |
7cfc04 |
.I /bin/sh
|
|
Packit |
7cfc04 |
is bash version 2: as a security measure, bash 2 drops privileges on startup.
|
|
Packit |
7cfc04 |
(Debian uses a different shell,
|
|
Packit |
7cfc04 |
.BR dash (1),
|
|
Packit |
7cfc04 |
which does not do this when invoked as
|
|
Packit |
7cfc04 |
.BR sh .)
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
Any user input that is employed as part of
|
|
Packit |
7cfc04 |
.I command
|
|
Packit |
7cfc04 |
should be
|
|
Packit |
7cfc04 |
.I carefully
|
|
Packit |
7cfc04 |
sanitized, to ensure that unexpected shell commands or command options
|
|
Packit |
7cfc04 |
are not executed.
|
|
Packit |
7cfc04 |
Such risks are especially grave when using
|
|
Packit |
7cfc04 |
.BR system ()
|
|
Packit |
7cfc04 |
from a privileged program.
|
|
Packit |
7cfc04 |
.SH SEE ALSO
|
|
Packit |
7cfc04 |
.BR sh (1),
|
|
Packit |
7cfc04 |
.BR execve (2),
|
|
Packit |
7cfc04 |
.BR fork (2),
|
|
Packit |
7cfc04 |
.BR sigaction (2),
|
|
Packit |
7cfc04 |
.BR sigprocmask (2),
|
|
Packit |
7cfc04 |
.BR wait (2),
|
|
Packit |
7cfc04 |
.BR exec (3),
|
|
Packit |
7cfc04 |
.BR signal (7)
|
|
Packit |
7cfc04 |
.SH COLOPHON
|
|
Packit |
7cfc04 |
This page is part of release 4.15 of the Linux
|
|
Packit |
7cfc04 |
.I man-pages
|
|
Packit |
7cfc04 |
project.
|
|
Packit |
7cfc04 |
A description of the project,
|
|
Packit |
7cfc04 |
information about reporting bugs,
|
|
Packit |
7cfc04 |
and the latest version of this page,
|
|
Packit |
7cfc04 |
can be found at
|
|
Packit |
7cfc04 |
\%https://www.kernel.org/doc/man\-pages/.
|