diff options
author | markj <markj@FreeBSD.org> | 2014-08-27 19:51:42 +0000 |
---|---|---|
committer | markj <markj@FreeBSD.org> | 2014-08-27 19:51:42 +0000 |
commit | 385503e19fd756c955fb493debec6f49a00f545c (patch) | |
tree | 10ea8d1f8133c8133073dc8c9445a4aabad67376 /lib/libproc/proc_create.c | |
parent | 3d2bab33aa9bc3dde1dd57cf667e10eb89730df8 (diff) | |
download | FreeBSD-src-385503e19fd756c955fb493debec6f49a00f545c.zip FreeBSD-src-385503e19fd756c955fb493debec6f49a00f545c.tar.gz |
MFC r265255, r270506:
Allow "a.out" as an alias for the executable if no other matching entries
are found.
Diffstat (limited to 'lib/libproc/proc_create.c')
-rw-r--r-- | lib/libproc/proc_create.c | 52 |
1 files changed, 42 insertions, 10 deletions
diff --git a/lib/libproc/proc_create.c b/lib/libproc/proc_create.c index d02eccf..4a92580 100644 --- a/lib/libproc/proc_create.c +++ b/lib/libproc/proc_create.c @@ -26,8 +26,10 @@ * $FreeBSD$ */ -#include "_libproc.h" -#include <stdio.h> +#include <sys/types.h> +#include <sys/sysctl.h> +#include <sys/wait.h> + #include <err.h> #include <errno.h> #include <fcntl.h> @@ -35,7 +37,37 @@ #include <stdlib.h> #include <string.h> #include <unistd.h> -#include <sys/wait.h> + +#include "_libproc.h" + +static int proc_init(pid_t, int, int, struct proc_handle *); + +static int +proc_init(pid_t pid, int flags, int status, struct proc_handle *phdl) +{ + int mib[4], error; + size_t len; + + memset(phdl, 0, sizeof(*phdl)); + phdl->pid = pid; + phdl->flags = flags; + phdl->status = status; + + mib[0] = CTL_KERN; + mib[1] = KERN_PROC; + mib[2] = KERN_PROC_PATHNAME; + mib[3] = pid; + len = sizeof(phdl->execname); + if (sysctl(mib, 4, phdl->execname, &len, NULL, 0) != 0) { + error = errno; + DPRINTF("ERROR: cannot get pathname for child process %d", pid); + return (error); + } + if (len == 0) + phdl->execname[0] = '\0'; + + return (0); +} int proc_attach(pid_t pid, int flags, struct proc_handle **pphdl) @@ -54,12 +86,12 @@ proc_attach(pid_t pid, int flags, struct proc_handle **pphdl) if ((phdl = malloc(sizeof(struct proc_handle))) == NULL) return (ENOMEM); - memset(phdl, 0, sizeof(struct proc_handle)); - phdl->pid = pid; - phdl->flags = flags; - phdl->status = PS_RUN; elf_version(EV_CURRENT); + error = proc_init(pid, flags, PS_RUN, phdl); + if (error != 0) + goto out; + if (ptrace(PT_ATTACH, phdl->pid, 0, 0) != 0) { error = errno; DPRINTF("ERROR: cannot ptrace child process %d", pid); @@ -123,9 +155,9 @@ proc_create(const char *file, char * const *argv, proc_child_func *pcf, _exit(2); } else { /* The parent owns the process handle. */ - memset(phdl, 0, sizeof(struct proc_handle)); - phdl->pid = pid; - phdl->status = PS_IDLE; + 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) { |