1POSIX_SPAWN_FILE_ACTIONS_ADDPCOLSOISXE(P3rPo)grammePrO'SsIXM_aSnPuAaWlN_FILE_ACTIONS_ADDCLOSE(3P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10

NAME

12       posix_spawn_file_actions_addclose,  posix_spawn_file_actions_addopen  -
13       add  close  or open action to spawn file actions object (ADVANCED REAL‐
14       TIME)
15

SYNOPSIS

17       #include <spawn.h>
18
19       int posix_spawn_file_actions_addclose(posix_spawn_file_actions_t *
20              file_actions, int fildes);
21       int posix_spawn_file_actions_addopen(posix_spawn_file_actions_t *
22              restrict file_actions, int fildes,
23              const char *restrict path, int oflag, mode_t mode);
24
25

DESCRIPTION

27       These functions shall add or delete a close or open action to  a  spawn
28       file actions object.
29
30       A  spawn  file  actions  object  is  of type posix_spawn_file_actions_t
31       (defined in <spawn.h>) and is used to specify a series of actions to be
32       performed  by  a  posix_spawn() or posix_spawnp() operation in order to
33       arrive at the set of open file descriptors for the child process  given
34       the  set  of  open file descriptors of the parent. IEEE Std 1003.1-2001
35       does not  define  comparison  or  assignment  operators  for  the  type
36       posix_spawn_file_actions_t.
37
38       A   spawn   file  actions  object,  when  passed  to  posix_spawn()  or
39       posix_spawnp(), shall specify how the set of open file  descriptors  in
40       the  calling process is transformed into a set of potentially open file
41       descriptors for the spawned process. This transformation shall be as if
42       the  specified  sequence  of actions was performed exactly once, in the
43       context of the spawned process (prior to execution of the  new  process
44       image),  in  the  order  in which the actions were added to the object;
45       additionally, when the new process image is executed, any file descrip‐
46       tor  (from  this  new  set)  which has its FD_CLOEXEC flag set shall be
47       closed (see posix_spawn()).
48
49       The posix_spawn_file_actions_addclose()  function  shall  add  a  close
50       action  to  the  object referenced by file_actions that shall cause the
51       file descriptor fildes to be closed (as  if  close(  fildes)  had  been
52       called) when a new process is spawned using this file actions object.
53
54       The  posix_spawn_file_actions_addopen()  function  shall  add  an  open
55       action to the object referenced by file_actions that  shall  cause  the
56       file  named  by  path  to be opened (as if open( path, oflag, mode) had
57       been called, and the returned file descriptor, if not fildes, had  been
58       changed  to  fildes)  when  a  new  process  is spawned using this file
59       actions object. If fildes was already an open file descriptor, it shall
60       be closed before the new file is opened.
61
62       The    string    described   by   path   shall   be   copied   by   the
63       posix_spawn_file_actions_addopen() function.
64

RETURN VALUE

66       Upon successful completion, these functions shall return  zero;  other‐
67       wise, an error number shall be returned to indicate the error.
68

ERRORS

70       These functions shall fail if:
71
72       EBADF  The  value  specified  by  fildes is negative or greater than or
73              equal to {OPEN_MAX}.
74
75
76       These functions may fail if:
77
78       EINVAL The value specified by file_actions is invalid.
79
80       ENOMEM Insufficient memory exists to add  to  the  spawn  file  actions
81              object.
82
83
84       It  shall  not be considered an error for the fildes argument passed to
85       these functions to specify a file descriptor for  which  the  specified
86       operation  could  not  be  performed  at the time of the call. Any such
87       error will be detected when the associated file actions object is later
88       used during a posix_spawn() or posix_spawnp() operation.
89
90       The following sections are informative.
91

EXAMPLES

93       None.
94

APPLICATION USAGE

96       These  functions  are part of the Spawn option and need not be provided
97       on all implementations.
98

RATIONALE

100       A spawn file actions object may be initialized to  contain  an  ordered
101       sequence  of  close(),  dup2(),  and  open()  operations  to be used by
102       posix_spawn() or posix_spawnp() to arrive  at  the  set  of  open  file
103       descriptors  inherited by the spawned process from the set of open file
104       descriptors  in  the  parent  at  the  time  of  the  posix_spawn()  or
105       posix_spawnp() call.  It had been suggested that the close() and dup2()
106       operations alone are sufficient to rearrange file descriptors, and that
107       files  which  need  to  be opened for use by the spawned process can be
108       handled either by having the  calling  process  open  them  before  the
109       posix_spawn()  or  posix_spawnp()  call  (and  close them after), or by
110       passing filenames to the spawned process (in argv) so that it may  open
111       them  itself.  The  standard developers recommend that applications use
112       one of these two methods when practical, since detailed error status on
113       a  failed  open  operation  is always available to the application this
114       way.  However, the standard developers feel that allowing a spawn  file
115       actions object to specify open operations is still appropriate because:
116
117        1. It is consistent with equivalent POSIX.5 (Ada) functionality.
118
119        2. It supports the I/O redirection paradigm commonly employed by POSIX
120           programs designed to be invoked from a shell. When such  a  program
121           is  the  child process, it may not be designed to open files on its
122           own.
123
124        3. It allows file opens that might otherwise fail or violate file own‐
125           ership/access rights if executed by the parent process.
126
127       Regarding  2.  above,  note that the spawn open file action provides to
128       posix_spawn() and posix_spawnp() the same capability that the shell re‐
129       direction  operators  provide to system(), only without the intervening
130       execution of a shell; for example:
131
132
133              system ("myprog <file1 3<file2");
134
135       Regarding 3. above, note that if the calling process needs to open  one
136       or  more  files for access by the spawned process, but has insufficient
137       spare file descriptors, then the open action is necessary to allow  the
138       open()  to  occur  in the context of the child process after other file
139       descriptors have been closed (that must remain open in the parent).
140
141       Additionally, if a parent is executed from a file having  a  "set-user-
142       id"  mode bit set and the POSIX_SPAWN_RESETIDS flag is set in the spawn
143       attributes, a file created within the  parent  process  will  (possibly
144       incorrectly)  have the parent's effective user ID as its owner, whereas
145       a  file  created  via  an  open()  action   during   posix_spawn()   or
146       posix_spawnp() will have the parent's real ID as its owner; and an open
147       by the parent process may successfully open a file to  which  the  real
148       user  should  not  have access or fail to open a file to which the real
149       user should have access.
150
151   File Descriptor Mapping
152       The standard developers had originally proposed using  an  array  which
153       specified  the  mapping  of child file descriptors back to those of the
154       parent. It was pointed out by the ballot group that it is not  possible
155       to  reshuffle  file descriptors arbitrarily in a library implementation
156       of posix_spawn() or posix_spawnp() without provision for  one  or  more
157       spare file descriptor entries (which simply may not be available). Such
158       an array requires that an implementation develop a complex strategy  to
159       achieve  the  desired  mapping  without inadvertently closing the wrong
160       file descriptor at the wrong time.
161
162       It was noted by a member of the Ada  Language  Bindings  working  group
163       that  the  approved  Ada Language Start_Process family of POSIX process
164       primitives use a caller-specified set of file actions to alter the nor‐
165       mal  fork()/  exec  semantics  for inheritance of file descriptors in a
166       very flexible way, yet no such problems exist  because  the  burden  of
167       determining  how  to  achieve the final file descriptor mapping is com‐
168       pletely on the application.  Furthermore,  although  the  file  actions
169       interface  appears  frightening  at  first glance, it is actually quite
170       simple to implement in either a library or the kernel.
171

FUTURE DIRECTIONS

173       None.
174

SEE ALSO

176       close(),            dup(),            open(),            posix_spawn(),
177       posix_spawn_file_actions_adddup2(), posix_spawn_file_actions_destroy(),
178       posix_spawnp(), the Base Definitions  volume  of  IEEE Std 1003.1-2001,
179       <spawn.h>
180
182       Portions  of  this text are reprinted and reproduced in electronic form
183       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
184       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
185       Specifications Issue 6, Copyright (C) 2001-2003  by  the  Institute  of
186       Electrical  and  Electronics  Engineers, Inc and The Open Group. In the
187       event of any discrepancy between this version and the original IEEE and
188       The  Open Group Standard, the original IEEE and The Open Group Standard
189       is the referee document. The original Standard can be  obtained  online
190       at http://www.opengroup.org/unix/online.html .
191
192
193
194IEEE/The Open Group                  2003POSIX_SPAWN_FILE_ACTIONS_ADDCLOSE(3P)
Impressum