⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 system.html

📁 IEEE 1003.1-2003, Single Unix Specification v3
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta name="generator" content="HTML Tidy, see www.w3.org"><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 --><!-- Copyright (c) 2001-2003 The Open Group, All Rights Reserved --><title>system</title></head><body bgcolor="white"><script type="text/javascript" language="JavaScript" src="../jscript/codes.js"></script><basefont size="3"> <a name="system"></a> <a name="tag_03_757"></a><!-- system --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2003 Edition<br>Copyright &copy; 2001-2003 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_03_757_01"></a>NAME</h4><blockquote>system - issue a command</blockquote><h4><a name="tag_03_757_02"></a>SYNOPSIS</h4><blockquote class="synopsis"><p><code><tt>#include &lt;<a href="../basedefs/stdlib.h.html">stdlib.h</a>&gt;<br><br> int system(const char *</tt><i>command</i><tt>);<br></tt></code></p></blockquote><h4><a name="tag_03_757_03"></a>DESCRIPTION</h4><blockquote><div class="box"><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]"border="0"> The functionality described on this reference page is aligned with the ISO&nbsp;C standard. Any conflict between therequirements described here and the ISO&nbsp;C standard is unintentional. This volume of IEEE&nbsp;Std&nbsp;1003.1-2001 defers tothe ISO&nbsp;C standard. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></div><p>If <i>command</i> is a null pointer, the <i>system</i>() function shall determine whether the host environment has a commandprocessor. If <i>command</i> is not a null pointer, the <i>system</i>() function shall pass the string pointed to by <i>command</i>to that command processor to be executed in an implementation-defined manner; this might then cause the program calling<i>system</i>() to behave in a non-conforming manner or to terminate.</p><p><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> Theenvironment of the executed command shall be as if a child process were created using <a href="../functions/fork.html"><i>fork</i>()</a>, and the child process invoked the <a href="../utilities/sh.html"><i>sh</i></a> utilityusing <a href="../functions/execl.html"><i>execl</i>()</a> as follows:</p><pre><tt>execl(&lt;</tt><i>shell path</i><tt>&gt;, "sh", "-c",</tt> <i>command</i><tt>, (char *)0);</tt></pre><p>where &lt;<i>shell path</i>&gt; is an unspecified pathname for the <a href="../utilities/sh.html"><i>sh</i></a> utility.</p><p>The <i>system</i>() function shall ignore the SIGINT and SIGQUIT signals, and shall block the SIGCHLD signal, while waiting forthe command to terminate. If this might cause the application to miss a signal that would have killed it, then the applicationshould examine the return value from <i>system</i>() and take whatever action is appropriate to the application if the commandterminated due to receipt of a signal.</p><p>The <i>system</i>() function shall not affect the termination status of any child of the calling processes other than theprocess or processes it itself creates.</p><p>The <i>system</i>() function shall not return until the child process has terminated. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p></blockquote><h4><a name="tag_03_757_04"></a>RETURN VALUE</h4><blockquote><p>If <i>command</i> is a null pointer, <i>system</i>() shall return non-zero to indicate that a command processor is available, orzero if none is available. <sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> The <i>system</i>() function shall always return non-zero when <i>command</i> is NULL. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p><p><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> If<i>command</i> is not a null pointer, <i>system</i>() shall return the termination status of the command language interpreter inthe format specified by <a href="../functions/waitpid.html"><i>waitpid</i>()</a>. The termination status shall be as defined forthe <a href="../utilities/sh.html"><i>sh</i></a> utility; otherwise, the termination status is unspecified. If some error preventsthe command language interpreter from executing after the child process is created, the return value from <i>system</i>() shall beas if the command language interpreter had terminated using <i>exit</i>(127) or <i>_exit</i>(127). If a child process cannot becreated, or if the termination status for the command language interpreter cannot be obtained, <i>system</i>() shall return -1 andset <i>errno</i> to indicate the error. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p></blockquote><h4><a name="tag_03_757_05"></a>ERRORS</h4><blockquote><p><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> The<i>system</i>() function may set <i>errno</i> values as described by <a href="fork.html"><i>fork</i>()</a> . <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p><p>In addition, <i>system</i>() may fail if:</p><dl compact><dt>[ECHILD]</dt><dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">The status of the child process created by <i>system</i>() is no longer available. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd></dl></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_03_757_06"></a>EXAMPLES</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_757_07"></a>APPLICATION USAGE</h4><blockquote><p>If the return value of <i>system</i>() is not -1, its value can be decoded through the use of the macros described in <a href="../basedefs/sys/wait.h.html"><i>&lt;sys/wait.h&gt;</i></a>. For convenience, these macros are also provided in <a href="../basedefs/stdlib.h.html"><i>&lt;stdlib.h&gt;</i></a>.</p><p>Note that, while <i>system</i>() must ignore SIGINT and SIGQUIT and block SIGCHLD while waiting for the child to terminate, thehandling of signals in the executed command is as specified by <a href="../functions/fork.html"><i>fork</i>()</a> and <i><a href="../functions/exec.html">exec</a></i>. For example, if SIGINT is being caught or is set to SIG_DFL when <i>system</i>() is called,then the child is started with SIGINT handling set to SIG_DFL.</p><p>Ignoring SIGINT and SIGQUIT in the parent process prevents coordination problems (two processes reading from the same terminal,for example) when the executed command ignores or catches one of the signals. It is also usually the correct action when the userhas given a command to the application to be executed synchronously (as in the <tt>'!'</tt> command in many interactiveapplications). In either case, the signal should be delivered only to the child process, not to the application itself. There isone situation where ignoring the signals might have less than the desired effect. This is when the application uses <i>system</i>()to perform some task invisible to the user. If the user typed the interrupt character ( <tt>"^C"</tt> , for example) while<i>system</i>() is being used in this way, one would expect the application to be killed, but only the executed command is killed.Applications that use <i>system</i>() in this way should carefully check the return status from <i>system</i>() to see if theexecuted command was successful, and should take appropriate action when the command fails.</p><p>Blocking SIGCHLD while waiting for the child to terminate prevents the application from catching the signal and obtaining statusfrom <i>system</i>()'s child process before <i>system</i>() can get the status itself.</p><p>The context in which the utility is ultimately executed may differ from that in which <i>system</i>() was called. For example,file descriptors that have the FD_CLOEXEC flag set are closed, and the process ID and parent process ID are different. Also, if theexecuted utility changes its environment variables or its current working directory, that change is not reflected in the caller'scontext.</p><p>There is no defined way for an application to find the specific path for the shell. However, <a href="../functions/confstr.html"><i>confstr</i>()</a> can provide a value for <i>PATH</i> that is guaranteed to find the <a href="../utilities/sh.html"><i>sh</i></a> utility.</p></blockquote><h4><a name="tag_03_757_08"></a>RATIONALE</h4><blockquote><p>The <i>system</i>() function should not be used by programs that have set user (or group) ID privileges. The <a href="../functions/fork.html"><i>fork</i>()</a> and <i><a href="../functions/exec.html">exec</a></i> family of functions (except <ahref="../functions/execlp.html"><i>execlp</i>()</a> and <a href="../functions/execvp.html"><i>execvp</i>()</a>), should be usedinstead. This prevents any unforeseen manipulation of the environment of the user that could cause execution of commands notanticipated by the calling program.</p>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -