2 * Copyright (c) 2008 John Birrell (jb@freebsd.org)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/types.h>
30 #include <sys/sysctl.h>
43 static int proc_init(pid_t, int, int, struct proc_handle *);
46 proc_init(pid_t pid, int flags, int status, struct proc_handle *phdl)
51 memset(phdl, 0, sizeof(*phdl));
54 phdl->status = status;
58 mib[2] = KERN_PROC_PATHNAME;
60 len = sizeof(phdl->execname);
61 if (sysctl(mib, 4, phdl->execname, &len, NULL, 0) != 0) {
63 DPRINTF("ERROR: cannot get pathname for child process %d", pid);
67 phdl->execname[0] = '\0';
73 proc_attach(pid_t pid, int flags, struct proc_handle **pphdl)
75 struct proc_handle *phdl;
79 if (pid == 0 || pid == getpid())
83 * Allocate memory for the process handle, a structure containing
84 * all things related to the process.
86 if ((phdl = malloc(sizeof(struct proc_handle))) == NULL)
89 elf_version(EV_CURRENT);
91 error = proc_init(pid, flags, PS_RUN, phdl);
95 if (ptrace(PT_ATTACH, phdl->pid, 0, 0) != 0) {
97 DPRINTF("ERROR: cannot ptrace child process %d", pid);
101 /* Wait for the child process to stop. */
102 if (waitpid(pid, &status, WUNTRACED) == -1) {
104 DPRINTF("ERROR: child process %d didn't stop as expected", pid);
108 /* Check for an unexpected status. */
109 if (WIFSTOPPED(status) == 0)
110 DPRINTFX("ERROR: child process %d status 0x%x", pid, status);
112 phdl->status = PS_STOP;
123 proc_create(const char *file, char * const *argv, proc_child_func *pcf,
124 void *child_arg, struct proc_handle **pphdl)
126 struct proc_handle *phdl;
132 * Allocate memory for the process handle, a structure containing
133 * all things related to the process.
135 if ((phdl = malloc(sizeof(struct proc_handle))) == NULL)
138 elf_version(EV_CURRENT);
140 /* Fork a new process. */
141 if ((pid = vfork()) == -1)
144 /* The child expects to be traced. */
145 if (ptrace(PT_TRACE_ME, 0, 0, 0) != 0)
151 /* Execute the specified file: */
154 /* Couldn't execute the file. */
157 /* The parent owns the process handle. */
158 error = proc_init(pid, 0, PS_IDLE, phdl);
162 /* Wait for the child process to stop. */
163 if (waitpid(pid, &status, WUNTRACED) == -1) {
165 DPRINTF("ERROR: child process %d didn't stop as expected", pid);
169 /* Check for an unexpected status. */
170 if (WIFSTOPPED(status) == 0) {
172 DPRINTFX("ERROR: child process %d status 0x%x", pid, status);
175 phdl->status = PS_STOP;
186 proc_free(struct proc_handle *phdl)