1SYSTEM(3)                  Linux Programmer's Manual                 SYSTEM(3)
2
3
4

NAME

6       system - execute a shell command
7

SYNOPSIS

9       #include <stdlib.h>
10
11       int system(const char *command);
12

DESCRIPTION

14       The  system()  library  function uses fork(2) to create a child process
15       that executes the shell command specified in command using execl(3)  as
16       follows:
17
18           execl("/bin/sh", "sh", "-c", command, (char *) NULL);
19
20       system() returns after the command has been completed.
21
22       During  execution  of  the command, SIGCHLD will be blocked, and SIGINT
23       and SIGQUIT will be  ignored,  in  the  process  that  calls  system().
24       (These  signals  will be handled according to their defaults inside the
25       child process that executes command.)
26
27       If command is NULL, then system() returns a status indicating whether a
28       shell is available on the system.
29

RETURN VALUE

31       The return value of system() is one of the following:
32
33       *  If command is NULL, then a nonzero value if a shell is available, or
34          0 if no shell is available.
35
36       *  If a child process could not be created, or its status could not  be
37          retrieved,  the  return value is -1 and errno is set to indicate the
38          error.
39
40       *  If a shell could not be executed in  the  child  process,  then  the
41          return  value  is  as  though  the child shell terminated by calling
42          _exit(2) with the status 127.
43
44       *  If all system calls succeed, then the return value is  the  termina‐
45          tion status of the child shell used to execute command.  (The termi‐
46          nation status of a shell is the termination status of the last  com‐
47          mand it executes.)
48
49       In  the last two cases, the return value is a "wait status" that can be
50       examined using the macros described in waitpid(2).  (i.e., WIFEXITED(),
51       WEXITSTATUS(), and so on).
52
53       system() does not affect the wait status of any other children.
54

ERRORS

56       system() can fail with any of the same errors as fork(2).
57

ATTRIBUTES

59       For   an   explanation   of   the  terms  used  in  this  section,  see
60       attributes(7).
61
62       ┌──────────┬───────────────┬─────────┐
63Interface Attribute     Value   
64       ├──────────┼───────────────┼─────────┤
65system()  │ Thread safety │ MT-Safe │
66       └──────────┴───────────────┴─────────┘

CONFORMING TO

68       POSIX.1-2001, POSIX.1-2008, C89, C99.
69

NOTES

71       system() provides simplicity and convenience: it  handles  all  of  the
72       details  of  calling  fork(2), execl(3), and waitpid(2), as well as the
73       necessary manipulations of signals; in addition, the shell performs the
74       usual substitutions and I/O redirections for command.  The main cost of
75       system() is inefficiency: additional system calls are required to  cre‐
76       ate the process that runs the shell and to execute the shell.
77
78       If  the  _XOPEN_SOURCE  feature test macro is defined (before including
79       any header files), then the macros described in  waitpid(2)  (WEXITSTA‐
80       TUS(), etc.) are made available when including <stdlib.h>.
81
82       As  mentioned, system() ignores SIGINT and SIGQUIT.  This may make pro‐
83       grams that call it from a loop uninterruptible, unless they  take  care
84       themselves to check the exit status of the child.  For example:
85
86           while (something) {
87               int ret = system("foo");
88
89               if (WIFSIGNALED(ret) &&
90                   (WTERMSIG(ret) == SIGINT || WTERMSIG(ret) == SIGQUIT))
91                       break;
92           }
93
94       According  to  POSIX.1,  it  is unspecified whether handlers registered
95       using pthread_atfork(3) are called during the  execution  of  system().
96       In the glibc implementation, such handlers are not called.
97
98       In  versions  of  glibc before 2.1.3, the check for the availability of
99       /bin/sh was not actually performed if command was NULL; instead it  was
100       always  assumed to be available, and system() always returned 1 in this
101       case.  Since glibc 2.1.3, this check is performed because, even  though
102       POSIX.1-2001  requires  a conforming implementation to provide a shell,
103       that shell may not be available or executable if  the  calling  program
104       has   previously   called   chroot(2)   (which   is  not  specified  by
105       POSIX.1-2001).
106
107       It is possible for the shell command to terminate with a status of 127,
108       which yields a system() return value that is indistinguishable from the
109       case where a shell could not be executed in the child process.
110
111   Caveats
112       Do not use system() from a privileged program (a  set-user-ID  or  set-
113       group-ID  program, or a program with capabilities) because strange val‐
114       ues for some environment variables might  be  used  to  subvert  system
115       integrity.  For example, PATH could be manipulated so that an arbitrary
116       program is executed with privilege.  Use the exec(3)  family  of  func‐
117       tions  instead, but not execlp(3) or execvp(3) (which also use the PATH
118       environment variable to search for an executable).
119
120       system() will not, in fact, work properly from programs with  set-user-
121       ID  or set-group-ID privileges on systems on which /bin/sh is bash ver‐
122       sion 2: as a security measure, bash  2  drops  privileges  on  startup.
123       (Debian  uses  a  different shell, dash(1), which does not do this when
124       invoked as sh.)
125
126       Any user input that is employed as part of command should be  carefully
127       sanitized,  to ensure that unexpected shell commands or command options
128       are not executed.  Such risks are especially grave when using  system()
129       from a privileged program.
130

SEE ALSO

132       sh(1),   execve(2),  fork(2),  sigaction(2),  sigprocmask(2),  wait(2),
133       exec(3), signal(7)
134

COLOPHON

136       This page is part of release 5.07 of the Linux  man-pages  project.   A
137       description  of  the project, information about reporting bugs, and the
138       latest    version    of    this    page,    can     be     found     at
139       https://www.kernel.org/doc/man-pages/.
140
141
142
143                                  2019-03-06                         SYSTEM(3)
Impressum