From 5577b8a709c072fedc73dde9b3ad730699438bf7 Mon Sep 17 00:00:00 2001 From: Mark Johnston Date: Sun, 3 Dec 2017 16:50:16 +0000 Subject: [PATCH] Add an envp argument to proc_create(). This is needed to support dtrace's -x setenv option. MFC after: 2 weeks --- ObsoleteFiles.inc | 3 +++ .../lib/libdtrace/common/dt_proc.c | 2 +- lib/libproc/Makefile | 2 +- lib/libproc/libproc.h | 4 +-- lib/libproc/proc_create.c | 26 +++++++++---------- lib/libproc/tests/proc_test.c | 2 +- 6 files changed, 21 insertions(+), 18 deletions(-) diff --git a/ObsoleteFiles.inc b/ObsoleteFiles.inc index 666b15838a6a..997c9364ab90 100644 --- a/ObsoleteFiles.inc +++ b/ObsoleteFiles.inc @@ -38,6 +38,9 @@ # xargs -n1 | sort | uniq -d; # done +# 20171203: libproc version bump +OLD_LIBS+=usr/lib/libproc.so.4 +OLD_LIBS+=usr/lib32/libproc.so.4 # 20171203: new clang import which bumps version from 5.0.0 to 5.0.1. OLD_FILES+=usr/lib/clang/5.0.0/include/sanitizer/allocator_interface.h OLD_FILES+=usr/lib/clang/5.0.0/include/sanitizer/asan_interface.h diff --git a/cddl/contrib/opensolaris/lib/libdtrace/common/dt_proc.c b/cddl/contrib/opensolaris/lib/libdtrace/common/dt_proc.c index 9215b438a40f..6cdd36c24b36 100644 --- a/cddl/contrib/opensolaris/lib/libdtrace/common/dt_proc.c +++ b/cddl/contrib/opensolaris/lib/libdtrace/common/dt_proc.c @@ -967,7 +967,7 @@ dt_proc_create(dtrace_hdl_t *dtp, const char *file, char *const *argv, #ifdef illumos if ((dpr->dpr_proc = Pcreate(file, argv, &err, NULL, 0)) == NULL) { #else - if ((err = proc_create(file, argv, pcf, child_arg, + if ((err = proc_create(file, argv, NULL, pcf, child_arg, &dpr->dpr_proc)) != 0) { #endif return (dt_proc_error(dtp, dpr, diff --git a/lib/libproc/Makefile b/lib/libproc/Makefile index ed37b8acb3c0..cd78eb335ecc 100644 --- a/lib/libproc/Makefile +++ b/lib/libproc/Makefile @@ -37,7 +37,7 @@ CFLAGS+= -I${SRCTOP}/cddl/contrib/opensolaris/lib/libctf/common \ CFLAGS+= -DNO_CTF .endif -SHLIB_MAJOR= 4 +SHLIB_MAJOR= 5 MAN= diff --git a/lib/libproc/libproc.h b/lib/libproc/libproc.h index 7c17943b10f2..ef88f94c4752 100644 --- a/lib/libproc/libproc.h +++ b/lib/libproc/libproc.h @@ -142,8 +142,8 @@ int proc_addr2sym(struct proc_handle *, uintptr_t, char *, size_t, GElf_Sym *); int proc_attach(pid_t pid, int flags, struct proc_handle **pphdl); int proc_continue(struct proc_handle *); int proc_clearflags(struct proc_handle *, int); -int proc_create(const char *, char * const *, proc_child_func *, void *, - struct proc_handle **); +int proc_create(const char *, char * const *, char * const *, + proc_child_func *, void *, struct proc_handle **); int proc_detach(struct proc_handle *, int); int proc_getflags(struct proc_handle *); int proc_name2sym(struct proc_handle *, const char *, const char *, diff --git a/lib/libproc/proc_create.c b/lib/libproc/proc_create.c index 21accb619c47..72e3a087b774 100644 --- a/lib/libproc/proc_create.c +++ b/lib/libproc/proc_create.c @@ -176,9 +176,10 @@ proc_attach(pid_t pid, int flags, struct proc_handle **pphdl) } int -proc_create(const char *file, char * const *argv, proc_child_func *pcf, - void *child_arg, struct proc_handle **pphdl) +proc_create(const char *file, char * const *argv, char * const *envp, + proc_child_func *pcf, void *child_arg, struct proc_handle **pphdl) { + extern char * const *environ; struct proc_handle *phdl; int error, status; pid_t pid; @@ -189,8 +190,7 @@ proc_create(const char *file, char * const *argv, proc_child_func *pcf, error = 0; phdl = NULL; - /* Fork a new process. */ - if ((pid = vfork()) == -1) + if ((pid = fork()) == -1) error = errno; else if (pid == 0) { /* The child expects to be traced. */ @@ -200,18 +200,14 @@ proc_create(const char *file, char * const *argv, proc_child_func *pcf, if (pcf != NULL) (*pcf)(child_arg); - /* Execute the specified file: */ + if (envp != NULL) + environ = envp; + execvp(file, argv); - /* Couldn't execute the file. */ _exit(2); /* NOTREACHED */ } else { - /* The parent owns the process handle. */ - error = proc_init(pid, 0, PS_IDLE, &phdl); - if (error != 0) - goto bad; - /* Wait for the child process to stop. */ if (waitpid(pid, &status, WUNTRACED) == -1) { error = errno; @@ -221,11 +217,15 @@ proc_create(const char *file, char * const *argv, proc_child_func *pcf, /* Check for an unexpected status. */ if (!WIFSTOPPED(status)) { - error = EBUSY; + error = ENOENT; DPRINTFX("ERROR: child process %d status 0x%x", pid, status); goto bad; } - phdl->status = PS_STOP; + + /* The parent owns the process handle. */ + error = proc_init(pid, 0, PS_IDLE, &phdl); + if (error == 0) + phdl->status = PS_STOP; bad: if (error != 0 && phdl != NULL) { diff --git a/lib/libproc/tests/proc_test.c b/lib/libproc/tests/proc_test.c index 9ec99cd355c4..ca7afc73b7ae 100644 --- a/lib/libproc/tests/proc_test.c +++ b/lib/libproc/tests/proc_test.c @@ -65,7 +65,7 @@ start_prog(const struct atf_tc *tc, bool sig) argv[1] = NULL; } - error = proc_create(argv[0], argv, NULL, NULL, &phdl); + error = proc_create(argv[0], argv, NULL, NULL, NULL, &phdl); ATF_REQUIRE_EQ_MSG(error, 0, "failed to run '%s'", target_prog_file); ATF_REQUIRE(phdl != NULL);