spawnlp()

Updated: October 28, 2024

Spawn a child process, given a list of arguments and a relative path

Synopsis:

#include <process.h>

int spawnlp( int mode,
             const char * file,
             const char * arg0,
             const char * arg1...,
             const char * argn,
             NULL );

Arguments:

mode
How you want to load the child process, and how you want the parent program to behave after the child program is initiated:
  • P_WAIT — load the child program into available memory, execute it, and make the parent program resume execution after the child process ends.
  • P_NOWAIT — execute the parent program concurrently with the new child process.
  • P_NOWAITO — execute the parent program concurrently with the new child process. You can't use wait() to obtain the exit code.
  • P_OVERLAY — replace the parent program with the child program in memory and execute the child. No return is made to the parent program. This is equivalent to calling the appropriate exec*() function.
file
The name of the executable file. If this argument contains a slash, it's used as the pathname of the executable; otherwise, the function searches for file in the directories listed in the PATH environment variable.
arg0, ... argn, NULL
The arguments that you want to pass to the new process. You must pass at least arg0, which by convention is a pointer to the name of the new child process, and can't be NULL. You must terminate the list with an argument of NULL.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The spawnlp() function creates and executes a new child process, named in file with NULL-terminated list of arguments in arg0 … argn. This function calls spawnvpe().

Note:
  • In order to create a child process, your process must have the PROCMGR_AID_SPAWN ability enabled. For more information, see procmgr_ability().
  • If the new child process is a shell script, the first line must start with #!, followed by the path of the program to run to interpret the script, optionally followed by one argument. The script must also be marked as executable. For more information, see The first line in the Writing Shell Scripts chapter of the QNX Neutrino User's Guide.

The spawnlp() function is a QNX Neutrino-specific convenience function. For greater portability and control, use posix_spawn().

Arguments are passed to the child process by supplying one or more pointers to character strings as arguments. These character strings are concatenated with spaces inserted to separate the arguments to form one argument string for the child process.

The child process inherits the parent's environment, with some restrictions. The inherited attributes always include any environment variables defined with the export shell command, the env utility, or by the successful execution of the putenv() or setenv() function. A program may read these values with the getenv() function.

The inheritance of other attributes such as file descriptors depends on the mode argument: If it's not P_OVERLAY, inheritance proceeds as though calling spawn() with fd_count set to 0, fd_map set to NULL, and inherit->flags set to 0. This means all of the parent's file descriptors are inherited by the child, except those created with O_CLOEXEC. Note that side channels are not inherited, because they aren't considered file descriptors. To understand how the flags setting of 0 affects the inheritance of other attributes, see the inheritance structure description. If flags is P_OVERLAY, inheritance proceeds as though calling the appropriate exec*() function, which has the same inheritance policy as spawn(); the spawn() reference lists all of the inherited attributes.

Note: A parent/child relationship doesn't imply that the child process dies when the parent process dies.

Returns:

The spawnlp() function's return value depends on the mode argument:

mode Return value
P_WAIT The exit status of the child process. For information about macros that extract information from this status, see Status macros in the documentation for wait().
P_NOWAIT The process ID of the child process. To get the exit status for a P_NOWAIT process, you must use the waitpid() function, giving it this process ID.
P_NOWAITO The process ID of the child process. You can't get the exit status of a P_NOWAITO process.

If an error occurs, -1 is returned (errno is set).

Errors:

E2BIG
The number of bytes used by the argument list of the new child process is greater than ARG_MAX bytes.
EACCES
The calling process doesn't have permission to search a directory listed in path, or it doesn't have permission to execute path, or path's filesystem was mounted with the ST_NOEXEC flag.
EAGAIN
Insufficient resources available to create the child process.
EBADF
An error occurred duplicating open file descriptors to the new process.
ECHILD
The mode is P_WAIT, and the spawned process terminated before the call to waitpid() was completed.
EFAULT
One of the buffers specified in the function call is invalid.
EINTR
The function was interrupted by a signal.
EINVAL
An argument is invalid (e.g., arg0 is NULL, or the value of mode isn't valid).
ELOOP
Too many levels of symbolic links or prefixes.
EMFILE
Insufficient resources available to load the new executable image or to remap file descriptors in the child process.
ENAMETOOLONG
The length of file exceeds PATH_MAX.
ENOENT
The file identified by the file argument is empty, or one or more components of the pathname of the child process don't exist.
ENOEXEC
The child process's file has the correct permissions, but isn't in the correct format for an executable.
ENOMEM
Insufficient memory available to create the child process.
ENOSYS
The spawnlp() function isn't implemented for the filesystem underlying the path specified in file.
ENOTDIR
A component of the path prefix of the child process isn't a directory.
EPERM
The calling process doesn't have the required permission (see procmgr_ability()), or an underlying call to mmap() failed because it attempted to set PROT_EXEC for a region of memory covered by an untrusted memory-mapped file.
ETXTBSY
The text file that you're trying to execute is busy (e.g., it might be open for writing).

See also the errors for ConnectAttach() and MsgSendvnc().

Classification:

QNX Neutrino

Safety:  
Cancellation point Read the Caveats
Interrupt handler No
Signal handler No
Thread Yes

Caveats:

If mode is P_WAIT, this function is a cancellation point.

  翻译: