POSIX_SPAWN(2) | System Calls Manual | POSIX_SPAWN(2) |
posix_spawn
posix_spawnp
— spawn a
process
#include
<spawn.h>
int
posix_spawn
(pid_t *restrict pid,
const char *restrict path, const
posix_spawn_file_actions_t *file_actions, const
posix_spawnattr_t *restrict attrp, char *const
argv[restrict], char *const envp[restrict]);
int
posix_spawnp
(pid_t *restrict
pid, const char *restrict file,
const posix_spawn_file_actions_t *file_actions,
const posix_spawnattr_t *restrict attrp,
char *const argv[restrict], char
*const envp[restrict]);
The
posix_spawn
()
function creates a new process from the executable file, called the
new process
file, specified by path, which is an absolute or
relative path to the file. The
posix_spawnp
()
function is identical to the posix_spawn
() function
if the file specified contains a slash character;
otherwise, the file parameter is used to construct a
pathname, with its path prefix being obtained by a search of the path
specified in the environment by the “PATH
variable
”. If this variable isn't specified, the default path
is set according to the _PATH_DEFPATH
definition in
<paths.h>
, which is set to
“/usr/bin:/bin
”. This pathname either
refers to an executable object file, or a file of data for an interpreter;
execve(2) for more details.
The argument pid is a pointer to a pid_t variable to receive the pid of the spawned process; if this is NULL, then the pid of the spawned process is not returned. If this pointer is non-NULL, then on successful completion, the variable will be modified to contain the pid of the spawned process. The value is undefined in the case of a failure.
The argument file_actions is either NULL, or it is a pointer to a file actions object that was initialized by a call to posix_spawn_file_actions_init(3) and represents zero or more file actions.
File descriptors open in the calling process
image remain open in the new process image, except for those for which the
close-on-exec flag is set (see close(2) and
fcntl(2)). Descriptors that remain open are unaffected by
posix_spawn
()
unless their behaviour is modified by particular spawn flags or a file
action; see posix_spawnattr_setflags(3) and
posix_spawn_file_actions_init(3) for additional
information.
The argument attrp is either NULL, or it is a pointer to an attributes object that was initialized by a call to posix_spawnattr_init(3) and represents a set of spawn attributes to apply. If NULL, then the default attributes are applied; otherwise, these attributes can control various aspects of the spawned process, and are applied prior to the spawned process beginning execution; see posix_spawnattr_init(3) for more information.
The argument argv is a pointer to a null-terminated array of character pointers to null-terminated character strings. These strings construct the argument list to be made available to the new process. At least argv[0] must be present in the array, and should contain the file name of the program being spawned, e.g. the last component of the path or file argument.
The argument envp is a pointer to a null-terminated array of character pointers to null-terminated strings. A pointer to this array is normally stored in the global variable environ. These strings pass information to the new process that is not directly an argument to the command (see environ(7)).
Signals set to be ignored in the calling process are set to be ignored in the new process, unless the behaviour is modified by user specified spawn attributes. Signals which are set to be caught in the calling process image are set to default action in the new process image. Blocked signals remain blocked regardless of changes to the signal action, unless the mask is overridden by user specified spawn attributes. The signal stack is reset to be undefined (see sigaction(2) for more information).
By default, the effective user ID and group ID will be the same as those of the calling process image; however, this may be overridden to force them to be the real user ID and group ID of the parent process by user specified spawn attributes (see posix_spawnattr_init(3) for more information).
If the set-user-ID mode bit of the new process image file is set (see chmod(2)), the effective user ID of the new process image is set to the owner ID of the new process image file. If the set-group-ID mode bit of the new process image file is set, the effective group ID of the new process image is set to the group ID of the new process image file. (The effective group ID is the first element of the group list.) The real user ID, real group ID and supplementary group IDs of the new process image remain the same as the calling process image. After any set-user-ID and set-group-ID processing, the effective user ID is recorded as the saved set-user-ID, and the effective group ID is recorded as the saved set-group-ID. These values may be used in changing the effective IDs later (see setuid(2)).
The new process also inherits the following attributes from the calling process:
parent process ID | see getppid(2) |
process group ID | see getpgrp(2), posix_spawnattr_init(3) |
access groups | see getgroups(2) |
working directory | see chdir(2) |
root directory | see chroot(2) |
control terminal | see termios(4) |
resource usages | see getrusage(2) |
interval timers | see getitimer(2) |
resource limits | see getrlimit(2) |
file mode mask | see umask(2) |
signal mask | see sigaction(2), sigsetmask(2), posix_spawnattr_init(3) |
When a program is executed as a result of a
posix_spawn
()
or
posix_spawnp
()
call, it is entered as follows:
main(argc, argv, envp) int argc; char **argv, **envp;
where argc is the number of elements in argv (the ``arg count'') and argv points to the array of character pointers to the arguments themselves.
If the pid argument is NULL, no pid is returned
to the calling process; if it is non-NULL, then
posix_spawn
() and
posix_spawnp
() functions return the process ID of
the child process into the pid_t variable pointed to by the
pid argument and return a 0 on success. If an error
occurs, they return a non-zero error code as the function return value, and
no child process is created.
The posix_spawn
() and
posix_spawnp
() functions will fail and return to the
calling process if:
EINVAL
]E2BIG
]KERN_ARGMAX
.EACCES
]EACCES
]EACCES
]EACCES
]MNT_NOEXEC
in
⟨sys/mount.h⟩).EFAULT
]EFAULT
]EIO
]ELOOP
]ENAMETOOLONG
]{NAME_MAX}
characters, or an entire path name exceeded
{PATH_MAX}
characters.ENOENT
]ENOEXEC
]ENOMEM
]ENOTDIR
]ETXTBSY
]EBADARCH
]EBADF
]Additionally, they may fail for any of the reasons listed in fork(2) or exec(3).
If a program is setuid to a non-super-user, but is executed when the real uid is ``root'', then the program has some of the powers of a super-user as well.
exit(2), fork(2), execl(3), sysctl(3), environ(7), posix_spawnattr_init(3), posix_spawn_file_actions_init(3),
Version 3 of the Single UNIX Specification (“SUSv3”) [SPN]
The posix_spawn
() and
posix_spawnp
() function calls appeared in
Version 3 of the Single UNIX Specification
(“SUSv3”) [SPN].
November 2, 2010 | Mac OS X |