📄 posix_spawn_file_actions_addopen.html
字号:
<!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>posix_spawn_file_actions_addclose</title></head><body bgcolor="white"><script type="text/javascript" language="JavaScript" src="../jscript/codes.js"></script><basefont size="3"> <a name="posix_spawn_file_actions_addclose"></a> <a name="tag_03_428"></a> <!-- posix_spawn_file_actions_addclose --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2003 Edition<br>Copyright © 2001-2003 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_03_428_01"></a>NAME</h4><blockquote>posix_spawn_file_actions_addclose, posix_spawn_file_actions_addopen - add close or open action to spawn file actionsobject (<b>ADVANCED REALTIME</b>)</blockquote><h4><a name="tag_03_428_02"></a>SYNOPSIS</h4><blockquote class="synopsis"><div class="box"><code><tt><sup>[<a href="javascript:open_code('SPN')">SPN</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> #include <<a href="../basedefs/spawn.h.html">spawn.h</a>><br><br> int posix_spawn_file_actions_addclose(posix_spawn_file_actions_t *<br> </tt> <i>file_actions</i><tt>, int</tt> <i>fildes</i><tt>);<br> int posix_spawn_file_actions_addopen(posix_spawn_file_actions_t *<br> restrict</tt> <i>file_actions</i><tt>, int</tt> <i>fildes</i><tt>,<br> const char *restrict</tt> <i>path</i><tt>, int</tt> <i>oflag</i><tt>, mode_t</tt><i>mode</i><tt>); <img src="../images/opt-end.gif" alt="[Option End]" border="0"></tt></code></div><tt><br></tt></blockquote><h4><a name="tag_03_428_03"></a>DESCRIPTION</h4><blockquote><p>These functions shall add or delete a close or open action to a spawn file actions object.</p><p>A spawn file actions object is of type <b>posix_spawn_file_actions_t</b> (defined in <a href="../basedefs/spawn.h.html"><i><spawn.h></i></a>) and is used to specify a series of actions to be performed by a <a href="../functions/posix_spawn.html"><i>posix_spawn</i>()</a> or <a href="../functions/posix_spawnp.html"><i>posix_spawnp</i>()</a>operation in order to arrive at the set of open file descriptors for the child process given the set of open file descriptors ofthe parent. IEEE Std 1003.1-2001 does not define comparison or assignment operators for the type<b>posix_spawn_file_actions_t</b>.</p><p>A spawn file actions object, when passed to <a href="../functions/posix_spawn.html"><i>posix_spawn</i>()</a> or <a href="../functions/posix_spawnp.html"><i>posix_spawnp</i>()</a>, shall specify how the set of open file descriptors in the callingprocess is transformed into a set of potentially open file descriptors for the spawned process. This transformation shall be as ifthe specified sequence of actions was performed exactly once, in the context of the spawned process (prior to execution of the newprocess image), in the order in which the actions were added to the object; additionally, when the new process image is executed,any file descriptor (from this new set) which has its FD_CLOEXEC flag set shall be closed (see <a href="posix_spawn.html"><i>posix_spawn</i>()</a> ).</p><p>The <i>posix_spawn_file_actions_addclose</i>() function shall add a <i>close</i> action to the object referenced by<i>file_actions</i> that shall cause the file descriptor <i>fildes</i> to be closed (as if <i>close</i>( <i>fildes</i>) had beencalled) when a new process is spawned using this file actions object.</p><p>The <i>posix_spawn_file_actions_addopen</i>() function shall add an <i>open</i> action to the object referenced by<i>file_actions</i> that shall cause the file named by <i>path</i> to be opened (as if <i>open</i>( <i>path</i>, <i>oflag</i>,<i>mode</i>) had been called, and the returned file descriptor, if not <i>fildes</i>, had been changed to <i>fildes</i>) when a newprocess is spawned using this file actions object. If <i>fildes</i> was already an open file descriptor, it shall be closed beforethe new file is opened.</p><p>The string described by <i>path</i> shall be copied by the <i>posix_spawn_file_actions_addopen</i>() function.</p></blockquote><h4><a name="tag_03_428_04"></a>RETURN VALUE</h4><blockquote><p>Upon successful completion, these functions shall return zero; otherwise, an error number shall be returned to indicate theerror.</p></blockquote><h4><a name="tag_03_428_05"></a>ERRORS</h4><blockquote><p>These functions shall fail if:</p><dl compact><dt>[EBADF]</dt><dd>The value specified by <i>fildes</i> is negative or greater than or equal to {OPEN_MAX}.</dd></dl><p>These functions may fail if:</p><dl compact><dt>[EINVAL]</dt><dd>The value specified by <i>file_actions</i> is invalid.</dd><dt>[ENOMEM]</dt><dd>Insufficient memory exists to add to the spawn file actions object.</dd></dl><p>It shall not be considered an error for the <i>fildes</i> argument passed to these functions to specify a file descriptor forwhich the specified operation could not be performed at the time of the call. Any such error will be detected when the associatedfile actions object is later used during a <a href="../functions/posix_spawn.html"><i>posix_spawn</i>()</a> or <a href="../functions/posix_spawnp.html"><i>posix_spawnp</i>()</a> operation.</p></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_03_428_06"></a>EXAMPLES</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_428_07"></a>APPLICATION USAGE</h4><blockquote><p>These functions are part of the Spawn option and need not be provided on all implementations.</p></blockquote><h4><a name="tag_03_428_08"></a>RATIONALE</h4><blockquote><p>A spawn file actions object may be initialized to contain an ordered sequence of <a href="../functions/close.html"><i>close</i>()</a>, <a href="../functions/dup2.html"><i>dup2</i>()</a>, and <a href="../functions/open.html"><i>open</i>()</a> operations to be used by <a href="../functions/posix_spawn.html"><i>posix_spawn</i>()</a> or <a href="../functions/posix_spawnp.html"><i>posix_spawnp</i>()</a> toarrive at the set of open file descriptors inherited by the spawned process from the set of open file descriptors in the parent atthe time of the <a href="../functions/posix_spawn.html"><i>posix_spawn</i>()</a> or <a href="../functions/posix_spawnp.html"><i>posix_spawnp</i>()</a> call. It had been suggested that the <a href="../functions/close.html"><i>close</i>()</a> and <a href="../functions/dup2.html"><i>dup2</i>()</a> operations alone are sufficientto rearrange file descriptors, and that files which need to be opened for use by the spawned process can be handled either byhaving the calling process open them before the <a href="../functions/posix_spawn.html"><i>posix_spawn</i>()</a> or <a href="../functions/posix_spawnp.html"><i>posix_spawnp</i>()</a> call (and close them after), or by passing filenames to the spawnedprocess (in <i>argv</i>) so that it may open them itself. The standard developers recommend that applications use one of these twomethods when practical, since detailed error status on a failed open operation is always available to the application this way.However, the standard developers feel that allowing a spawn file actions object to specify open operations is still appropriatebecause:</p><ol><li><p>It is consistent with equivalent POSIX.5 (Ada) functionality.</p></li><li><p>It supports the I/O redirection paradigm commonly employed by POSIX programs designed to be invoked from a shell. When such aprogram is the child process, it may not be designed to open files on its own.</p></li><li><p>It allows file opens that might otherwise fail or violate file ownership/access rights if executed by the parent process.</p></li></ol><p>Regarding 2. above, note that the spawn open file action provides to <a href="../functions/posix_spawn.html"><i>posix_spawn</i>()</a> and <a href="../functions/posix_spawnp.html"><i>posix_spawnp</i>()</a> thesame capability that the shell redirection operators provide to <a href="../functions/system.html"><i>system</i>()</a>, onlywithout the intervening execution of a shell; for example:</p><pre><tt>system ("myprog <file1 3<file2");</tt></pre><p>Regarding 3. above, note that if the calling process needs to open one or more files for access by the spawned process, but hasinsufficient spare file descriptors, then the open action is necessary to allow the <a href="../functions/open.html"><i>open</i>()</a> to occur in the context of the child process after other file descriptors have beenclosed (that must remain open in the parent).</p><p>Additionally, if a parent is executed from a file having a "set-user-id" mode bit set and the POSIX_SPAWN_RESETIDS flag is setin the spawn attributes, a file created within the parent process will (possibly incorrectly) have the parent's effective user IDas its owner, whereas a file created via an <a href="../functions/open.html"><i>open</i>()</a> action during <a href="../functions/posix_spawn.html"><i>posix_spawn</i>()</a> or <a href="../functions/posix_spawnp.html"><i>posix_spawnp</i>()</a> willhave the parent's real ID as its owner; and an open by the parent process may successfully open a file to which the real usershould not have access or fail to open a file to which the real user should have access.</p><h5><a name="tag_03_428_08_01"></a>File Descriptor Mapping</h5><p>The standard developers had originally proposed using an array which specified the mapping of child file descriptors back tothose of the parent. It was pointed out by the ballot group that it is not possible to reshuffle file descriptors arbitrarily in alibrary implementation of <a href="../functions/posix_spawn.html"><i>posix_spawn</i>()</a> or <a href="../functions/posix_spawnp.html"><i>posix_spawnp</i>()</a> without provision for one or more spare file descriptor entries (whichsimply may not be available). Such an array requires that an implementation develop a complex strategy to achieve the desiredmapping without inadvertently closing the wrong file descriptor at the wrong time.</p><p>It was noted by a member of the Ada Language Bindings working group that the approved Ada Language <i>Start_Process</i> familyof POSIX process primitives use a caller-specified set of file actions to alter the normal <a href="../functions/fork.html"><i>fork</i>()</a>/ <i><a href="../functions/exec.html">exec</a></i> semantics for inheritance of filedescriptors in a very flexible way, yet no such problems exist because the burden of determining how to achieve the final filedescriptor mapping is completely on the application. Furthermore, although the file actions interface appears frightening at firstglance, it is actually quite simple to implement in either a library or the kernel.</p></blockquote><h4><a name="tag_03_428_09"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_428_10"></a>SEE ALSO</h4><blockquote><p><a href="close.html"><i>close</i>()</a> , <a href="dup.html"><i>dup</i>()</a> , <a href="open.html"><i>open</i>()</a> , <a href="posix_spawn.html"><i>posix_spawn</i>()</a> , <a href="posix_spawn_file_actions_adddup2.html"><i>posix_spawn_file_actions_adddup2</i>()</a> , <a href="posix_spawn_file_actions_destroy.html"><i>posix_spawn_file_actions_destroy</i>()</a> , <a href="posix_spawnp.html"><i>posix_spawnp</i>()</a> , the Base Definitions volume of IEEE Std 1003.1-2001, <a href="../basedefs/spawn.h.html"><i><spawn.h></i></a></p></blockquote><h4><a name="tag_03_428_11"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 6. Derived from IEEE Std 1003.1d-1999.</p><p>IEEE PASC Interpretation 1003.1 #105 is applied, adding a note to the DESCRIPTION that the string pointed to by <i>path</i> iscopied by the <i>posix_spawn_file_actions_addopen</i>() function.</p></blockquote><div class="box"><em>End of informative text.</em></div><hr><hr size="2" noshade><center><font size="2"><!--footer start-->UNIX ® is a registered Trademark of The Open Group.<br>POSIX ® is a registered Trademark of The IEEE.<br>[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href="../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>]</font></center><!--footer end--><hr size="2" noshade></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -