]>
Commit | Line | Data |
---|---|---|
fea681da MK |
1 | .\" (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de) |
2 | .\" | |
3 | .\" Permission is granted to make and distribute verbatim copies of this | |
4 | .\" manual provided the copyright notice and this permission notice are | |
5 | .\" preserved on all copies. | |
6 | .\" | |
7 | .\" Permission is granted to copy and distribute modified versions of this | |
8 | .\" manual under the conditions for verbatim copying, provided that the | |
9 | .\" entire resulting derived work is distributed under the terms of a | |
10 | .\" permission notice identical to this one. | |
11 | .\" | |
12 | .\" Since the Linux kernel and libraries are constantly changing, this | |
13 | .\" manual page may be incorrect or out-of-date. The author(s) assume no | |
14 | .\" responsibility for errors or omissions, or for damages resulting from | |
15 | .\" the use of the information contained herein. The author(s) may not | |
16 | .\" have taken the same level of care in the production of this manual, | |
17 | .\" which is licensed free of charge, as they might when working | |
18 | .\" professionally. | |
19 | .\" | |
20 | .\" Formatted or processed versions of this manual, if unaccompanied by | |
21 | .\" the source, must acknowledge the copyright and authors of this work. | |
22 | .\" License. | |
23 | .\" Modified Sat Jul 24 17:51:15 1993 by Rik Faith (faith@cs.unc.edu) | |
24 | .\" Modified 11 May 1998 by Joseph S. Myers (jsm28@cam.ac.uk) | |
25 | .\" Modified 14 May 2001, 23 Sep 2001 by aeb | |
26 | .\" | |
27 | .TH SYSTEM 3 2001-09-23 "" "Linux Programmer's Manual" | |
28 | .SH NAME | |
29 | system \- execute a shell command | |
30 | .SH SYNOPSIS | |
31 | .nf | |
32 | .B #include <stdlib.h> | |
33 | .sp | |
34 | .BI "int system(const char *" "string" ); | |
35 | .fi | |
36 | .SH DESCRIPTION | |
37 | .B system() | |
38 | executes a command specified in | |
39 | .I string | |
40 | by calling | |
41 | .BR "/bin/sh -c" | |
42 | .IR string , | |
43 | and returns after the command has been completed. | |
44 | During execution of the command, | |
45 | .B SIGCHLD | |
46 | will be blocked, and | |
47 | .B SIGINT | |
48 | and | |
49 | .B SIGQUIT | |
50 | will be ignored. | |
51 | .SH "RETURN VALUE" | |
52 | The value returned is \-1 on error (e.g. fork failed), | |
53 | and the return status of the command otherwise. | |
54 | This latter return status is in the format | |
55 | specified in | |
56 | .BR wait (2). | |
57 | Thus, the exit code of the command will be | |
58 | .IR WEXITSTATUS(status) . | |
59 | In case | |
60 | .B "/bin/sh" | |
61 | could not be executed, the exit status will be that of | |
62 | a command that does | |
63 | .IR exit(127) . | |
64 | .PP | |
65 | If the value of | |
66 | .I string | |
67 | is | |
68 | .BR NULL , | |
69 | .B system() | |
70 | returns nonzero if the shell is available, and zero if not. | |
71 | .PP | |
72 | .B system() | |
73 | does not affect the wait status of any other children. | |
74 | .SH "CONFORMING TO" | |
75 | ANSI C, POSIX.2, BSD 4.3 | |
76 | .SH NOTES | |
77 | .PP | |
78 | As mentioned, | |
79 | .B system() | |
80 | ignores SIGINT and SIGQUIT. This may make programs that call it | |
81 | from a loop uninterruptable, unless they take care themselves | |
82 | to check the exit status of the child. E.g. | |
83 | .br | |
84 | .nf | |
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 | .fi | |
94 | .PP | |
95 | Do not use | |
96 | .B system() | |
97 | from a program with suid or sgid privileges, because strange values for | |
98 | some environment variables might be used to subvert system integrity. | |
99 | Use the | |
100 | .BR exec (3) | |
101 | family of functions instead, but not | |
102 | .BR execlp (3) | |
103 | or | |
104 | .BR execvp (3). | |
105 | .B system() | |
106 | will not, in fact, work properly from programs with suid or sgid | |
107 | privileges on systems on which | |
108 | .B /bin/sh | |
109 | is bash version 2, since bash 2 drops privileges on startup. | |
110 | (Debian uses a modified bash which does not do this when invoked as | |
111 | .BR sh .) | |
112 | .PP | |
113 | The check for the availability of | |
114 | .B /bin/sh | |
115 | is not actually performed; it is always assumed to be available. ISO | |
116 | C specifies the check, but POSIX.2 specifies that the return shall | |
117 | always be non-zero, since a system without the shell is not | |
118 | conforming, and it is this that is implemented. | |
119 | .PP | |
120 | It is possible for the shell command to return 127, so that code is not | |
121 | a sure indication that the | |
122 | .B execve() | |
123 | call failed. | |
124 | .SH "SEE ALSO" | |
125 | .BR sh (1), | |
126 | .BR signal (2), | |
127 | .BR wait (2), | |
128 | .BR exec (3) |