OSDN Git Service

(split) LDP: Update original to LDP v3.65
[linuxjm/LDP_man-pages.git] / original / man3 / system.3
1 .\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .\" Modified Sat Jul 24 17:51:15 1993 by Rik Faith (faith@cs.unc.edu)
26 .\" Modified 11 May 1998 by Joseph S. Myers (jsm28@cam.ac.uk)
27 .\" Modified 14 May 2001, 23 Sep 2001 by aeb
28 .\" 2004-12-20, mtk
29 .\"
30 .TH SYSTEM 3  2010-09-10 "" "Linux Programmer's Manual"
31 .SH NAME
32 system \- execute a shell command
33 .SH SYNOPSIS
34 .nf
35 .B #include <stdlib.h>
36 .sp
37 .BI "int system(const char *" "command" );
38 .fi
39 .SH DESCRIPTION
40 .BR system ()
41 executes a command specified in
42 .I command
43 by calling
44 .BR "/bin/sh \-c"
45 .IR command ,
46 and returns after the command has been completed.
47 During execution of the command,
48 .B SIGCHLD
49 will be blocked, and
50 .B SIGINT
51 and
52 .B SIGQUIT
53 will be ignored.
54 .SH RETURN VALUE
55 The value returned is \-1 on error (e.g.,
56 .BR fork (2)
57 failed),
58 and the return status of the command otherwise.
59 This latter return status is in the format
60 specified in
61 .BR wait (2).
62 Thus, the exit code of the command will be
63 .IR WEXITSTATUS(status) .
64 In case
65 .I "/bin/sh"
66 could not be executed, the exit status will be that of
67 a command that does
68 .IR exit(127) .
69 .PP
70 If the value of
71 .I command
72 is NULL,
73 .BR system ()
74 returns nonzero if the shell is available, and zero if not.
75 .PP
76 .BR system ()
77 does not affect the wait status of any other children.
78 .SH CONFORMING TO
79 C89, C99, POSIX.1-2001.
80 .SH NOTES
81 .PP
82 If the
83 .B _XOPEN_SOURCE
84 feature test macro is defined
85 (before including
86 .I any
87 header files),
88 then the macros described in
89 .BR wait (2)
90 .RB ( WEXITSTATUS (),
91 etc.) are made available when including
92 .IR <stdlib.h> .
93 .PP
94 As mentioned,
95 .BR system ()
96 ignores
97 .B SIGINT
98 and
99 .BR SIGQUIT .
100 This may make programs that call it
101 from a loop uninterruptible, unless they take care themselves
102 to check the exit status of the child.
103 E.g.
104 .br
105 .nf
106
107     while (something) {
108         int ret = system("foo");
109
110         if (WIFSIGNALED(ret) &&
111             (WTERMSIG(ret) == SIGINT || WTERMSIG(ret) == SIGQUIT))
112                 break;
113     }
114 .fi
115 .PP
116 Do not use
117 .BR system ()
118 from a program with set-user-ID or set-group-ID privileges,
119 because strange values for some environment variables
120 might be used to subvert system integrity.
121 Use the
122 .BR exec (3)
123 family of functions instead, but not
124 .BR execlp (3)
125 or
126 .BR execvp (3).
127 .BR system ()
128 will not, in fact, work properly from programs with set-user-ID or
129 set-group-ID privileges on systems on which
130 .I /bin/sh
131 is bash version 2, since bash 2 drops privileges on startup.
132 (Debian uses a modified bash which does not do this when invoked as
133 .BR sh .)
134 .PP
135 In versions of glibc before 2.1.3, the check for the availability of
136 .I /bin/sh
137 was not actually performed if
138 .I command
139 was NULL; instead it was always assumed to be available, and
140 .BR system ()
141 always returned 1 in this case.
142 Since glibc 2.1.3, this check is performed because, even though
143 POSIX.1-2001 requires a conforming implementation to provide
144 a shell, that shell may not be available or executable if
145 the calling program has previously called
146 .BR chroot (2)
147 (which is not specified by POSIX.1-2001).
148 .PP
149 It is possible for the shell command to return 127, so that code is not
150 a sure indication that the
151 .BR execve (2)
152 call failed.
153 .SH SEE ALSO
154 .BR sh (1),
155 .BR signal (2),
156 .BR wait (2),
157 .BR exec (3)
158 .SH COLOPHON
159 This page is part of release 3.65 of the Linux
160 .I man-pages
161 project.
162 A description of the project,
163 and information about reporting bugs,
164 can be found at
165 \%http://www.kernel.org/doc/man\-pages/.