proc_create.c revision 1.2 1 /*-
2 * Copyright (c) 2008 John Birrell (jb (at) freebsd.org)
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/lib/libproc/proc_create.c 265255 2014-05-03 04:44:03Z markj $
27 */
28 #include <sys/cdefs.h>
29 __RCSID("$NetBSD: proc_create.c,v 1.2 2015/09/24 14:12:48 christos Exp $");
30
31 #include <sys/types.h>
32 #include <sys/sysctl.h>
33 #include <sys/wait.h>
34
35 #include <err.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <limits.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42
43 #include "_libproc.h"
44
45 static int proc_init(pid_t, int, int, struct proc_handle *);
46
47 static int
48 proc_init(pid_t pid, int flags, int status, struct proc_handle *phdl)
49 {
50 int mib[4], error;
51 size_t len;
52
53 memset(phdl, 0, sizeof(*phdl));
54 phdl->pid = pid;
55 phdl->flags = flags;
56 phdl->status = status;
57
58 mib[0] = CTL_KERN;
59 mib[1] = KERN_PROC_ARGS;
60 mib[2] = pid;
61 mib[3] = KERN_PROC_PATHNAME;
62 len = sizeof(phdl->execname);
63 if (sysctl(mib, 4, phdl->execname, &len, NULL, 0) != 0) {
64 error = errno;
65 DPRINTF("ERROR: cannot get pathname for child process %d", pid);
66 return (error);
67 }
68 if (len == 0)
69 phdl->execname[0] = '\0';
70
71 return (0);
72 }
73
74 int
75 proc_attach(pid_t pid, int flags, struct proc_handle **pphdl)
76 {
77 struct proc_handle *phdl;
78 int error = 0;
79 int status;
80
81 if (pid == 0 || pid == getpid())
82 return (EINVAL);
83
84 /*
85 * Allocate memory for the process handle, a structure containing
86 * all things related to the process.
87 */
88 if ((phdl = malloc(sizeof(struct proc_handle))) == NULL)
89 return (ENOMEM);
90
91 elf_version(EV_CURRENT);
92
93 error = proc_init(pid, flags, PS_RUN, phdl);
94 if (error != 0)
95 goto out;
96
97 if (ptrace(PT_ATTACH, phdl->pid, 0, 0) != 0) {
98 error = errno;
99 DPRINTF("ERROR: cannot ptrace child process %d", pid);
100 goto out;
101 }
102
103 /* Wait for the child process to stop. */
104 if (waitpid(pid, &status, WUNTRACED) == -1) {
105 error = errno;
106 DPRINTF("ERROR: child process %d didn't stop as expected", pid);
107 goto out;
108 }
109
110 /* Check for an unexpected status. */
111 if (WIFSTOPPED(status) == 0)
112 DPRINTFX("ERROR: child process %d status 0x%x", pid, status);
113 else
114 phdl->status = PS_STOP;
115
116 out:
117 if (error)
118 proc_free(phdl);
119 else
120 *pphdl = phdl;
121 return (error);
122 }
123
124 int
125 proc_create(const char *file, char * const *argv, proc_child_func *pcf,
126 void *child_arg, struct proc_handle **pphdl)
127 {
128 struct proc_handle *phdl;
129 int error = 0;
130 int status;
131 pid_t pid;
132
133 /*
134 * Allocate memory for the process handle, a structure containing
135 * all things related to the process.
136 */
137 if ((phdl = malloc(sizeof(struct proc_handle))) == NULL)
138 return (ENOMEM);
139
140 elf_version(EV_CURRENT);
141
142 /* Fork a new process. */
143 if ((pid = vfork()) == -1)
144 error = errno;
145 else if (pid == 0) {
146 /* The child expects to be traced. */
147 if (ptrace(PT_TRACE_ME, 0, 0, 0) != 0)
148 _exit(1);
149
150 if (pcf != NULL)
151 (*pcf)(child_arg);
152
153 /* Execute the specified file: */
154 execvp(file, argv);
155
156 /* Couldn't execute the file. */
157 _exit(2);
158 } else {
159 /* The parent owns the process handle. */
160 error = proc_init(pid, 0, PS_IDLE, phdl);
161 if (error != 0)
162 goto bad;
163
164 /* Wait for the child process to stop. */
165 if (waitpid(pid, &status, WUNTRACED) == -1) {
166 error = errno;
167 DPRINTF("ERROR: child process %d didn't stop as expected", pid);
168 goto bad;
169 }
170
171 /* Check for an unexpected status. */
172 if (WIFSTOPPED(status) == 0) {
173 error = errno;
174 DPRINTFX("ERROR: child process %d status 0x%x", pid, status);
175 goto bad;
176 } else
177 phdl->status = PS_STOP;
178 }
179 bad:
180 if (error)
181 proc_free(phdl);
182 else
183 *pphdl = phdl;
184 return (error);
185 }
186
187 void
188 proc_free(struct proc_handle *phdl)
189 {
190 free(phdl);
191 }
192