Add POSIX routines called posix_spawn() and posix_spawnp(), which
can be used as replacements for exec/fork in a lot of cases. This change also added execvpe() which allows environment variable PATH to be used for searching executable file, it is used for implementing posix_spawnp(). PR: standards/122051
This commit is contained in:
parent
aec9f8e993
commit
947aa542e9
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=179838
@ -19,7 +19,7 @@ INCS= a.out.h ar.h assert.h bitstring.h complex.h cpio.h _ctype.h ctype.h \
|
||||
printf.h proc_service.h pthread.h \
|
||||
pthread_np.h pwd.h ranlib.h readpassphrase.h regex.h regexp.h \
|
||||
res_update.h resolv.h runetype.h search.h setjmp.h sgtty.h \
|
||||
signal.h stab.h \
|
||||
signal.h spawn.h stab.h \
|
||||
stdbool.h stddef.h stdio.h stdlib.h string.h stringlist.h \
|
||||
strings.h sysexits.h tar.h tgmath.h \
|
||||
time.h timeconv.h timers.h ttyent.h \
|
||||
|
115
include/spawn.h
Normal file
115
include/spawn.h
Normal file
@ -0,0 +1,115 @@
|
||||
/*-
|
||||
* Copyright (c) 2008 Ed Schouten <ed@80386.nl>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* $FreeBSD$
|
||||
*/
|
||||
|
||||
#ifndef _SPAWN_H_
|
||||
#define _SPAWN_H_
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#include <sys/_types.h>
|
||||
#include <sys/_sigset.h>
|
||||
|
||||
#ifndef _MODE_T_DECLARED
|
||||
typedef __mode_t mode_t;
|
||||
#define _MODE_T_DECLARED
|
||||
#endif
|
||||
|
||||
#ifndef _PID_T_DECLARED
|
||||
typedef __pid_t pid_t;
|
||||
#define _PID_T_DECLARED
|
||||
#endif
|
||||
|
||||
#ifndef _SIGSET_T_DECLARED
|
||||
#define _SIGSET_T_DECLARED
|
||||
typedef __sigset_t sigset_t;
|
||||
#endif
|
||||
|
||||
struct sched_param;
|
||||
|
||||
typedef struct __posix_spawnattr *posix_spawnattr_t;
|
||||
typedef struct __posix_spawn_file_actions *posix_spawn_file_actions_t;
|
||||
|
||||
#define POSIX_SPAWN_RESETIDS 0x01
|
||||
#define POSIX_SPAWN_SETPGROUP 0x02
|
||||
#define POSIX_SPAWN_SETSCHEDPARAM 0x04
|
||||
#define POSIX_SPAWN_SETSCHEDULER 0x08
|
||||
#define POSIX_SPAWN_SETSIGDEF 0x10
|
||||
#define POSIX_SPAWN_SETSIGMASK 0x20
|
||||
|
||||
__BEGIN_DECLS
|
||||
/*
|
||||
* Spawn routines
|
||||
*/
|
||||
int posix_spawn(pid_t * __restrict, const char * __restrict,
|
||||
const posix_spawn_file_actions_t *, const posix_spawnattr_t * __restrict,
|
||||
char * const [__restrict], char * const [__restrict]);
|
||||
int posix_spawnp(pid_t * __restrict, const char * __restrict,
|
||||
const posix_spawn_file_actions_t *, const posix_spawnattr_t * __restrict,
|
||||
char * const [__restrict], char * const [__restrict]);
|
||||
|
||||
/*
|
||||
* File descriptor actions
|
||||
*/
|
||||
int posix_spawn_file_actions_init(posix_spawn_file_actions_t *);
|
||||
int posix_spawn_file_actions_destroy(posix_spawn_file_actions_t *);
|
||||
|
||||
int posix_spawn_file_actions_addopen(posix_spawn_file_actions_t * __restrict,
|
||||
int, const char * __restrict, int, mode_t);
|
||||
int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t *, int, int);
|
||||
int posix_spawn_file_actions_addclose(posix_spawn_file_actions_t *, int);
|
||||
|
||||
/*
|
||||
* Spawn attributes
|
||||
*/
|
||||
int posix_spawnattr_init(posix_spawnattr_t *);
|
||||
int posix_spawnattr_destroy(posix_spawnattr_t *);
|
||||
|
||||
int posix_spawnattr_getflags(const posix_spawnattr_t * __restrict,
|
||||
short * __restrict);
|
||||
int posix_spawnattr_getpgroup(const posix_spawnattr_t * __restrict,
|
||||
pid_t * __restrict);
|
||||
int posix_spawnattr_getschedparam(const posix_spawnattr_t * __restrict,
|
||||
struct sched_param * __restrict);
|
||||
int posix_spawnattr_getschedpolicy(const posix_spawnattr_t * __restrict,
|
||||
int * __restrict);
|
||||
int posix_spawnattr_getsigdefault(const posix_spawnattr_t * __restrict,
|
||||
sigset_t * __restrict);
|
||||
int posix_spawnattr_getsigmask(const posix_spawnattr_t * __restrict,
|
||||
sigset_t * __restrict sigmask);
|
||||
|
||||
int posix_spawnattr_setflags(posix_spawnattr_t *, short);
|
||||
int posix_spawnattr_setpgroup(posix_spawnattr_t *, pid_t);
|
||||
int posix_spawnattr_setschedparam(posix_spawnattr_t * __restrict,
|
||||
const struct sched_param * __restrict);
|
||||
int posix_spawnattr_setschedpolicy(posix_spawnattr_t *, int);
|
||||
int posix_spawnattr_setsigdefault(posix_spawnattr_t * __restrict,
|
||||
const sigset_t * __restrict);
|
||||
int posix_spawnattr_setsigmask(posix_spawnattr_t * __restrict,
|
||||
const sigset_t * __restrict);
|
||||
__END_DECLS
|
||||
|
||||
#endif /* !_SPAWN_H_ */
|
@ -335,6 +335,7 @@ int execlp(const char *, const char *, ...);
|
||||
int execv(const char *, char * const *);
|
||||
int execve(const char *, char * const *, char * const *);
|
||||
int execvp(const char *, char * const *);
|
||||
int execvpe(const char *, char * const *, char * const *);
|
||||
pid_t fork(void);
|
||||
long fpathconf(int, int);
|
||||
char *getcwd(char *, size_t);
|
||||
|
@ -21,7 +21,7 @@ SRCS+= __getosreldate.c __xuname.c \
|
||||
initgroups.c isatty.c isinf.c isnan.c jrand48.c lcong48.c \
|
||||
lockf.c lrand48.c mrand48.c nftw.c nice.c \
|
||||
nlist.c nrand48.c opendir.c \
|
||||
pause.c pmadvise.c popen.c pselect.c \
|
||||
pause.c pmadvise.c popen.c posix_spawn.c pselect.c \
|
||||
psignal.c pw_scan.c pwcache.c \
|
||||
raise.c readdir.c readpassphrase.c rewinddir.c \
|
||||
scandir.c seed48.c seekdir.c sem.c semctl.c \
|
||||
@ -81,7 +81,7 @@ MLINKS+=err.3 err_set_exit.3 err.3 err_set_file.3 err.3 errc.3 err.3 errx.3 \
|
||||
err.3 verr.3 err.3 verrc.3 err.3 verrx.3 err.3 vwarn.3 err.3 vwarnc.3 \
|
||||
err.3 vwarnx.3 err.3 warnc.3 err.3 warn.3 err.3 warnx.3
|
||||
MLINKS+=exec.3 execl.3 exec.3 execle.3 exec.3 execlp.3 exec.3 exect.3 \
|
||||
exec.3 execv.3 exec.3 execvp.3 exec.3 execvP.3
|
||||
exec.3 execv.3 exec.3 execvP.3 exec.3 execvp.3 exec.3 execvpe.3
|
||||
MLINKS+=fpclassify.3 finite.3 fpclassify.3 finitef.3 \
|
||||
fpclassify.3 isfinite.3 fpclassify.3 isinf.3 fpclassify.3 isnan.3 \
|
||||
fpclassify.3 isnormal.3
|
||||
|
@ -329,6 +329,7 @@ FBSD_1.0 {
|
||||
};
|
||||
|
||||
FBSD_1.1 {
|
||||
execvpe;
|
||||
fdopendir;
|
||||
fts_open;
|
||||
fts_close;
|
||||
@ -339,6 +340,27 @@ FBSD_1.1 {
|
||||
fts_get_stream;
|
||||
fts_set_clientptr;
|
||||
tcgetsid;
|
||||
posix_spawn;
|
||||
posix_spawn_file_actions_addclose;
|
||||
posix_spawn_file_actions_adddup2;
|
||||
posix_spawn_file_actions_addopen;
|
||||
posix_spawn_file_actions_destroy;
|
||||
posix_spawn_file_actions_init;
|
||||
posix_spawnattr_destroy;
|
||||
posix_spawnattr_getflags;
|
||||
posix_spawnattr_getpgroup;
|
||||
posix_spawnattr_getschedparam;
|
||||
posix_spawnattr_getschedpolicy;
|
||||
posix_spawnattr_getsigdefault;
|
||||
posix_spawnattr_getsigmask;
|
||||
posix_spawnattr_init;
|
||||
posix_spawnattr_setflags;
|
||||
posix_spawnattr_setpgroup;
|
||||
posix_spawnattr_setschedparam;
|
||||
posix_spawnattr_setschedpolicy;
|
||||
posix_spawnattr_setsigdefault;
|
||||
posix_spawnattr_setsigmask;
|
||||
posix_spawnp;
|
||||
};
|
||||
|
||||
FBSDprivate_1.0 {
|
||||
|
@ -38,6 +38,7 @@
|
||||
.Nm exect ,
|
||||
.Nm execv ,
|
||||
.Nm execvp ,
|
||||
.Nm execvpe ,
|
||||
.Nm execvP
|
||||
.Nd execute a file
|
||||
.Sh LIBRARY
|
||||
@ -64,6 +65,8 @@
|
||||
.Ft int
|
||||
.Fn execvp "const char *file" "char *const argv[]"
|
||||
.Ft int
|
||||
.Fn execvpe "const char *file" "char *const argv[]" "char *const envp[]"
|
||||
.Ft int
|
||||
.Fn execvP "const char *file" "const char *search_path" "char *const argv[]"
|
||||
.Sh DESCRIPTION
|
||||
The
|
||||
@ -118,9 +121,10 @@ be terminated by a
|
||||
pointer.
|
||||
.Pp
|
||||
The
|
||||
.Fn execle
|
||||
and
|
||||
.Fn execle ,
|
||||
.Fn exect
|
||||
and
|
||||
.Fn execvpe
|
||||
functions also specify the environment of the executed process by following
|
||||
the
|
||||
.Dv NULL
|
||||
@ -142,6 +146,7 @@ Some of these functions have special semantics.
|
||||
The functions
|
||||
.Fn execlp ,
|
||||
.Fn execvp ,
|
||||
.Fn execvpe ,
|
||||
and
|
||||
.Fn execvP
|
||||
will duplicate the actions of the shell in searching for an executable file
|
||||
@ -152,6 +157,7 @@ For
|
||||
.Fn execlp
|
||||
and
|
||||
.Fn execvp ,
|
||||
.Fn execvpe ,
|
||||
search path is the path specified in the environment by
|
||||
.Dq Ev PATH
|
||||
variable.
|
||||
@ -277,7 +283,8 @@ The
|
||||
.Fn execl ,
|
||||
.Fn execle ,
|
||||
.Fn execlp ,
|
||||
.Fn execvp
|
||||
.Fn execvp ,
|
||||
.Fn execvpe
|
||||
and
|
||||
.Fn execvP
|
||||
functions
|
||||
@ -319,3 +326,7 @@ The
|
||||
.Fn execvP
|
||||
function first appeared in
|
||||
.Fx 5.2 .
|
||||
The
|
||||
.Fn execvpe
|
||||
function first appeared in
|
||||
.Fx 8.0 .
|
||||
|
@ -140,20 +140,15 @@ execv(name, argv)
|
||||
int
|
||||
execvp(const char *name, char * const *argv)
|
||||
{
|
||||
const char *path;
|
||||
|
||||
/* Get the path we're searching. */
|
||||
if ((path = getenv("PATH")) == NULL)
|
||||
path = _PATH_DEFPATH;
|
||||
|
||||
return (execvP(name, path, argv));
|
||||
return (execvpe(name, argv, environ));
|
||||
}
|
||||
|
||||
int
|
||||
execvP(name, path, argv)
|
||||
static int
|
||||
execvPe(name, path, argv, envp)
|
||||
const char *name;
|
||||
const char *path;
|
||||
char * const *argv;
|
||||
char * const *envp;
|
||||
{
|
||||
char **memp;
|
||||
int cnt, lp, ln;
|
||||
@ -269,3 +264,21 @@ retry: (void)_execve(bp, argv, environ);
|
||||
done:
|
||||
return (-1);
|
||||
}
|
||||
|
||||
int
|
||||
execvP(const char *name, const char *path, char * const argv[])
|
||||
{
|
||||
return execvPe(name, path, argv, environ);
|
||||
}
|
||||
|
||||
int
|
||||
execvpe(const char *name, char * const argv[], char * const envp[])
|
||||
{
|
||||
const char *path;
|
||||
|
||||
/* Get the path we're searching. */
|
||||
if ((path = getenv("PATH")) == NULL)
|
||||
path = _PATH_DEFPATH;
|
||||
|
||||
return (execvPe(name, path, argv, envp));
|
||||
}
|
||||
|
472
lib/libc/gen/posix_spawn.c
Normal file
472
lib/libc/gen/posix_spawn.c
Normal file
@ -0,0 +1,472 @@
|
||||
/*-
|
||||
* Copyright (c) 2008 Ed Schouten <ed@80386.nl>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
__FBSDID("$FreeBSD$");
|
||||
|
||||
#include "namespace.h"
|
||||
#include <sys/queue.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <sched.h>
|
||||
#include <spawn.h>
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include "un-namespace.h"
|
||||
|
||||
extern char **environ;
|
||||
|
||||
struct __posix_spawnattr {
|
||||
short sa_flags;
|
||||
pid_t sa_pgroup;
|
||||
struct sched_param sa_schedparam;
|
||||
int sa_schedpolicy;
|
||||
sigset_t sa_sigdefault;
|
||||
sigset_t sa_sigmask;
|
||||
};
|
||||
|
||||
struct __posix_spawn_file_actions {
|
||||
STAILQ_HEAD(, __posix_spawn_file_actions_entry) fa_list;
|
||||
};
|
||||
|
||||
typedef struct __posix_spawn_file_actions_entry {
|
||||
STAILQ_ENTRY(__posix_spawn_file_actions_entry) fae_list;
|
||||
enum { FAE_OPEN, FAE_DUP2, FAE_CLOSE } fae_action;
|
||||
|
||||
int fae_fildes;
|
||||
union {
|
||||
struct {
|
||||
char *path;
|
||||
#define fae_path fae_data.open.path
|
||||
int oflag;
|
||||
#define fae_oflag fae_data.open.oflag
|
||||
mode_t mode;
|
||||
#define fae_mode fae_data.open.mode
|
||||
} open;
|
||||
struct {
|
||||
int newfildes;
|
||||
#define fae_newfildes fae_data.dup2.newfildes
|
||||
} dup2;
|
||||
} fae_data;
|
||||
} posix_spawn_file_actions_entry_t;
|
||||
|
||||
/*
|
||||
* Spawn routines
|
||||
*/
|
||||
|
||||
static int
|
||||
process_spawnattr(const posix_spawnattr_t sa)
|
||||
{
|
||||
struct sigaction sigact = { .sa_flags = 0, .sa_handler = SIG_DFL };
|
||||
int i;
|
||||
|
||||
/*
|
||||
* POSIX doesn't really describe in which order everything
|
||||
* should be set. We'll just set them in the order in which they
|
||||
* are mentioned.
|
||||
*/
|
||||
|
||||
/* Set signal masks/defaults */
|
||||
if (sa->sa_flags & POSIX_SPAWN_SETSIGMASK) {
|
||||
_sigprocmask(SIG_SETMASK, &sa->sa_sigmask, NULL);
|
||||
}
|
||||
|
||||
if (sa->sa_flags & POSIX_SPAWN_SETSIGDEF) {
|
||||
for (i = 1; i <= _SIG_MAXSIG; i++) {
|
||||
if (sigismember(&sa->sa_sigdefault, i))
|
||||
if (_sigaction(i, &sigact, NULL) != 0)
|
||||
return (errno);
|
||||
}
|
||||
}
|
||||
|
||||
/* Reset user ID's */
|
||||
if (sa->sa_flags & POSIX_SPAWN_RESETIDS) {
|
||||
if (setegid(getgid()) != 0)
|
||||
return (errno);
|
||||
if (seteuid(getuid()) != 0)
|
||||
return (errno);
|
||||
}
|
||||
|
||||
/* Set process group */
|
||||
if (sa->sa_flags & POSIX_SPAWN_SETPGROUP) {
|
||||
if (setpgid(0, sa->sa_pgroup) != 0)
|
||||
return (errno);
|
||||
}
|
||||
|
||||
/* Set scheduler policy */
|
||||
if (sa->sa_flags & POSIX_SPAWN_SETSCHEDULER) {
|
||||
if (sched_setscheduler(0, sa->sa_schedpolicy,
|
||||
&sa->sa_schedparam) != 0)
|
||||
return (errno);
|
||||
} else if (sa->sa_flags & POSIX_SPAWN_SETSCHEDPARAM) {
|
||||
if (sched_setparam(0, &sa->sa_schedparam) != 0)
|
||||
return (errno);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
process_file_actions_entry(posix_spawn_file_actions_entry_t *fae)
|
||||
{
|
||||
int fd;
|
||||
|
||||
switch (fae->fae_action) {
|
||||
case FAE_OPEN:
|
||||
/* Perform an open(), make it use the right fd */
|
||||
fd = _open(fae->fae_path, fae->fae_oflag, fae->fae_mode);
|
||||
if (fd < 0)
|
||||
return (errno);
|
||||
if (fd != fae->fae_fildes) {
|
||||
if (_dup2(fd, fae->fae_fildes) == -1)
|
||||
return (errno);
|
||||
if (_close(fd) != 0) {
|
||||
if (errno == EBADF)
|
||||
return (EBADF);
|
||||
}
|
||||
}
|
||||
if (_fcntl(fae->fae_fildes, F_SETFD, 0) == -1)
|
||||
return (errno);
|
||||
break;
|
||||
case FAE_DUP2:
|
||||
/* Perform a dup2() */
|
||||
if (_dup2(fae->fae_fildes, fae->fae_newfildes) == -1)
|
||||
return (errno);
|
||||
if (_fcntl(fae->fae_newfildes, F_SETFD, 0) == -1)
|
||||
return (errno);
|
||||
break;
|
||||
case FAE_CLOSE:
|
||||
/* Perform a close() */
|
||||
if (_close(fae->fae_fildes) != 0) {
|
||||
if (errno == EBADF)
|
||||
return (EBADF);
|
||||
}
|
||||
break;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
process_file_actions(const posix_spawn_file_actions_t fa)
|
||||
{
|
||||
posix_spawn_file_actions_entry_t *fae;
|
||||
int error;
|
||||
|
||||
/* Replay all file descriptor modifications */
|
||||
STAILQ_FOREACH(fae, &fa->fa_list, fae_list) {
|
||||
error = process_file_actions_entry(fae);
|
||||
if (error)
|
||||
return (error);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
do_posix_spawn(pid_t *pid, const char *path,
|
||||
const posix_spawn_file_actions_t *fa,
|
||||
const posix_spawnattr_t *sa,
|
||||
char * const argv[], char * const envp[], int use_env_path)
|
||||
{
|
||||
pid_t p;
|
||||
volatile int error = 0;
|
||||
|
||||
p = vfork();
|
||||
switch (p) {
|
||||
case -1:
|
||||
return (errno);
|
||||
case 0:
|
||||
if (sa != NULL) {
|
||||
error = process_spawnattr(*sa);
|
||||
if (error)
|
||||
_exit(127);
|
||||
}
|
||||
if (fa != NULL) {
|
||||
error = process_file_actions(*fa);
|
||||
if (error)
|
||||
_exit(127);
|
||||
}
|
||||
if (use_env_path)
|
||||
execvpe(path, argv, envp != NULL ? envp : environ);
|
||||
else
|
||||
_execve(path, argv, envp != NULL ? envp : environ);
|
||||
error = errno;
|
||||
_exit(127);
|
||||
default:
|
||||
if (pid != NULL)
|
||||
*pid = p;
|
||||
return (error);
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
posix_spawn(pid_t *pid, const char *path,
|
||||
const posix_spawn_file_actions_t *fa,
|
||||
const posix_spawnattr_t *sa,
|
||||
char * const argv[], char * const envp[])
|
||||
{
|
||||
return do_posix_spawn(pid, path, fa, sa, argv, envp, 0);
|
||||
}
|
||||
|
||||
int
|
||||
posix_spawnp(pid_t *pid, const char *path,
|
||||
const posix_spawn_file_actions_t *fa,
|
||||
const posix_spawnattr_t *sa,
|
||||
char * const argv[], char * const envp[])
|
||||
{
|
||||
return do_posix_spawn(pid, path, fa, sa, argv, envp, 1);
|
||||
}
|
||||
|
||||
/*
|
||||
* File descriptor actions
|
||||
*/
|
||||
|
||||
int
|
||||
posix_spawn_file_actions_init(posix_spawn_file_actions_t *ret)
|
||||
{
|
||||
posix_spawn_file_actions_t fa;
|
||||
|
||||
fa = malloc(sizeof(struct __posix_spawn_file_actions));
|
||||
if (fa == NULL)
|
||||
return (-1);
|
||||
|
||||
STAILQ_INIT(&fa->fa_list);
|
||||
*ret = fa;
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
posix_spawn_file_actions_destroy(posix_spawn_file_actions_t *fa)
|
||||
{
|
||||
posix_spawn_file_actions_entry_t *fae;
|
||||
|
||||
while ((fae = STAILQ_FIRST(&(*fa)->fa_list)) != NULL) {
|
||||
/* Remove file action entry from the queue */
|
||||
STAILQ_REMOVE_HEAD(&(*fa)->fa_list, fae_list);
|
||||
|
||||
/* Deallocate file action entry */
|
||||
if (fae->fae_action == FAE_OPEN)
|
||||
free(fae->fae_path);
|
||||
free(fae);
|
||||
}
|
||||
|
||||
free(*fa);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
posix_spawn_file_actions_addopen(posix_spawn_file_actions_t * __restrict fa,
|
||||
int fildes, const char * __restrict path, int oflag, mode_t mode)
|
||||
{
|
||||
posix_spawn_file_actions_entry_t *fae;
|
||||
int error;
|
||||
|
||||
if (fildes < 0)
|
||||
return (EBADF);
|
||||
|
||||
/* Allocate object */
|
||||
fae = malloc(sizeof(posix_spawn_file_actions_entry_t));
|
||||
if (fae == NULL)
|
||||
return (errno);
|
||||
|
||||
/* Set values and store in queue */
|
||||
fae->fae_action = FAE_OPEN;
|
||||
fae->fae_path = strdup(path);
|
||||
if (fae->fae_path == NULL) {
|
||||
error = errno;
|
||||
free(fae);
|
||||
return (error);
|
||||
}
|
||||
fae->fae_fildes = fildes;
|
||||
fae->fae_oflag = oflag;
|
||||
fae->fae_mode = mode;
|
||||
|
||||
STAILQ_INSERT_TAIL(&(*fa)->fa_list, fae, fae_list);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t *fa,
|
||||
int fildes, int newfildes)
|
||||
{
|
||||
posix_spawn_file_actions_entry_t *fae;
|
||||
|
||||
if (fildes < 0 || newfildes < 0)
|
||||
return (EBADF);
|
||||
|
||||
/* Allocate object */
|
||||
fae = malloc(sizeof(posix_spawn_file_actions_entry_t));
|
||||
if (fae == NULL)
|
||||
return (errno);
|
||||
|
||||
/* Set values and store in queue */
|
||||
fae->fae_action = FAE_DUP2;
|
||||
fae->fae_fildes = fildes;
|
||||
fae->fae_newfildes = newfildes;
|
||||
|
||||
STAILQ_INSERT_TAIL(&(*fa)->fa_list, fae, fae_list);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int posix_spawn_file_actions_addclose(posix_spawn_file_actions_t *fa,
|
||||
int fildes)
|
||||
{
|
||||
posix_spawn_file_actions_entry_t *fae;
|
||||
|
||||
if (fildes < 0)
|
||||
return (EBADF);
|
||||
|
||||
/* Allocate object */
|
||||
fae = malloc(sizeof(posix_spawn_file_actions_entry_t));
|
||||
if (fae == NULL)
|
||||
return (errno);
|
||||
|
||||
/* Set values and store in queue */
|
||||
fae->fae_action = FAE_CLOSE;
|
||||
fae->fae_fildes = fildes;
|
||||
|
||||
STAILQ_INSERT_TAIL(&(*fa)->fa_list, fae, fae_list);
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Spawn attributes
|
||||
*/
|
||||
|
||||
int
|
||||
posix_spawnattr_init(posix_spawnattr_t *ret)
|
||||
{
|
||||
posix_spawnattr_t sa;
|
||||
|
||||
sa = calloc(1, sizeof(struct __posix_spawnattr));
|
||||
if (sa == NULL)
|
||||
return (errno);
|
||||
|
||||
/* Set defaults as specified by POSIX, cleared above */
|
||||
*ret = sa;
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
posix_spawnattr_destroy(posix_spawnattr_t *sa)
|
||||
{
|
||||
free(*sa);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
posix_spawnattr_getflags(const posix_spawnattr_t * __restrict sa,
|
||||
short * __restrict flags)
|
||||
{
|
||||
*flags = (*sa)->sa_flags;
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
posix_spawnattr_getpgroup(const posix_spawnattr_t * __restrict sa,
|
||||
pid_t * __restrict pgroup)
|
||||
{
|
||||
*pgroup = (*sa)->sa_pgroup;
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
posix_spawnattr_getschedparam(const posix_spawnattr_t * __restrict sa,
|
||||
struct sched_param * __restrict schedparam)
|
||||
{
|
||||
*schedparam = (*sa)->sa_schedparam;
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
posix_spawnattr_getschedpolicy(const posix_spawnattr_t * __restrict sa,
|
||||
int * __restrict schedpolicy)
|
||||
{
|
||||
*schedpolicy = (*sa)->sa_schedpolicy;
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
posix_spawnattr_getsigdefault(const posix_spawnattr_t * __restrict sa,
|
||||
sigset_t * __restrict sigdefault)
|
||||
{
|
||||
*sigdefault = (*sa)->sa_sigdefault;
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
posix_spawnattr_getsigmask(const posix_spawnattr_t * __restrict sa,
|
||||
sigset_t * __restrict sigmask)
|
||||
{
|
||||
*sigmask = (*sa)->sa_sigmask;
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
posix_spawnattr_setflags(posix_spawnattr_t *sa, short flags)
|
||||
{
|
||||
(*sa)->sa_flags = flags;
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
posix_spawnattr_setpgroup(posix_spawnattr_t *sa, pid_t pgroup)
|
||||
{
|
||||
(*sa)->sa_pgroup = pgroup;
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
posix_spawnattr_setschedparam(posix_spawnattr_t *sa __restrict,
|
||||
const struct sched_param * __restrict schedparam)
|
||||
{
|
||||
(*sa)->sa_schedparam = *schedparam;
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
posix_spawnattr_setschedpolicy(posix_spawnattr_t *sa, int schedpolicy)
|
||||
{
|
||||
(*sa)->sa_schedpolicy = schedpolicy;
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
posix_spawnattr_setsigdefault(posix_spawnattr_t * __restrict sa,
|
||||
const sigset_t * __restrict sigdefault)
|
||||
{
|
||||
(*sa)->sa_sigdefault = *sigdefault;
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
posix_spawnattr_setsigmask(posix_spawnattr_t * __restrict sa,
|
||||
const sigset_t * __restrict sigmask)
|
||||
{
|
||||
(*sa)->sa_sigmask = *sigmask;
|
||||
return (0);
|
||||
}
|
Loading…
Reference in New Issue
Block a user