xref: /freebsd/lib/libproc/proc_create.c (revision 8aac90f18aef7c9eea906c3ff9a001ca7b94f375)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2008 John Birrell (jb@freebsd.org)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/types.h>
30 #include <sys/sysctl.h>
31 #include <sys/user.h>
32 #include <sys/wait.h>
33 
34 #include <err.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <limits.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 
42 #include <libelf.h>
43 #include <libprocstat.h>
44 
45 #include "_libproc.h"
46 
47 extern char * const *environ;
48 
49 static int	getelfclass(int);
50 static int	proc_init(pid_t, int, int, struct proc_handle **);
51 
52 static int
53 getelfclass(int fd)
54 {
55 	GElf_Ehdr ehdr;
56 	Elf *e;
57 	int class;
58 
59 	class = ELFCLASSNONE;
60 
61 	if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL)
62 		goto out;
63 	if (gelf_getehdr(e, &ehdr) == NULL)
64 		goto out;
65 	class = ehdr.e_ident[EI_CLASS];
66 out:
67 	(void)elf_end(e);
68 	return (class);
69 }
70 
71 static int
72 proc_init(pid_t pid, int flags, int status, struct proc_handle **pphdl)
73 {
74 	struct kinfo_proc *kp;
75 	struct proc_handle *phdl;
76 	int error, class, count, fd;
77 
78 	error = ENOMEM;
79 	if ((phdl = malloc(sizeof(*phdl))) == NULL)
80 		goto out;
81 
82 	memset(phdl, 0, sizeof(*phdl));
83 	phdl->public.pid = pid;
84 	phdl->flags = flags;
85 	phdl->status = status;
86 	phdl->procstat = procstat_open_sysctl();
87 	if (phdl->procstat == NULL)
88 		goto out;
89 
90 	/* Obtain a path to the executable. */
91 	if ((kp = procstat_getprocs(phdl->procstat, KERN_PROC_PID, pid,
92 	    &count)) == NULL)
93 		goto out;
94 	error = procstat_getpathname(phdl->procstat, kp, phdl->execpath,
95 	    sizeof(phdl->execpath));
96 	procstat_freeprocs(phdl->procstat, kp);
97 	if (error != 0)
98 		goto out;
99 
100 	/* Use it to determine the data model for the process. */
101 	if ((fd = open(phdl->execpath, O_RDONLY)) < 0) {
102 		error = errno;
103 		goto out;
104 	}
105 	class = getelfclass(fd);
106 	switch (class) {
107 	case ELFCLASS64:
108 		phdl->model = PR_MODEL_LP64;
109 		break;
110 	case ELFCLASS32:
111 		phdl->model = PR_MODEL_ILP32;
112 		break;
113 	case ELFCLASSNONE:
114 	default:
115 		error = EINVAL;
116 		break;
117 	}
118 	(void)close(fd);
119 
120 out:
121 	*pphdl = phdl;
122 	return (error);
123 }
124 
125 int
126 proc_attach(pid_t pid, int flags, struct proc_handle **pphdl)
127 {
128 	struct proc_handle *phdl;
129 	int error, status;
130 
131 	if (pid == 0 || (pid == getpid() && (flags & PATTACH_RDONLY) == 0))
132 		return (EINVAL);
133 	if (elf_version(EV_CURRENT) == EV_NONE)
134 		return (ENOENT);
135 
136 	/*
137 	 * Allocate memory for the process handle, a structure containing
138 	 * all things related to the process.
139 	 */
140 	error = proc_init(pid, flags, PS_RUN, &phdl);
141 	if (error != 0)
142 		goto out;
143 
144 	if ((flags & PATTACH_RDONLY) == 0) {
145 		if (ptrace(PT_ATTACH, proc_getpid(phdl), 0, 0) != 0) {
146 			error = errno;
147 			DPRINTF("ERROR: cannot ptrace child process %d", pid);
148 			goto out;
149 		}
150 
151 		/* Wait for the child process to stop. */
152 		if (waitpid(pid, &status, WUNTRACED) == -1) {
153 			error = errno;
154 			DPRINTF("ERROR: child process %d didn't stop as expected", pid);
155 			goto out;
156 		}
157 
158 		/* Check for an unexpected status. */
159 		if (!WIFSTOPPED(status))
160 			DPRINTFX("ERROR: child process %d status 0x%x", pid, status);
161 		else
162 			phdl->status = PS_STOP;
163 
164 		if ((flags & PATTACH_NOSTOP) != 0)
165 			proc_continue(phdl);
166 	}
167 
168 out:
169 	if (error != 0 && phdl != NULL) {
170 		proc_free(phdl);
171 		phdl = NULL;
172 	}
173 	*pphdl = phdl;
174 	return (error);
175 }
176 
177 int
178 proc_create(const char *file, char * const *argv, char * const *envp,
179     proc_child_func *pcf, void *child_arg, struct proc_handle **pphdl)
180 {
181 	struct proc_handle *phdl;
182 	int error, status;
183 	pid_t pid;
184 
185 	if (elf_version(EV_CURRENT) == EV_NONE)
186 		return (ENOENT);
187 
188 	error = 0;
189 	phdl = NULL;
190 
191 	if ((pid = fork()) == -1)
192 		error = errno;
193 	else if (pid == 0) {
194 		/* The child expects to be traced. */
195 		if (ptrace(PT_TRACE_ME, 0, 0, 0) != 0)
196 			_exit(1);
197 
198 		if (pcf != NULL)
199 			(*pcf)(child_arg);
200 
201 		if (envp != NULL)
202 			environ = envp;
203 
204 		execvp(file, argv);
205 
206 		_exit(2);
207 		/* NOTREACHED */
208 	} else {
209 		/* Wait for the child process to stop. */
210 		if (waitpid(pid, &status, WUNTRACED) == -1) {
211 			error = errno;
212 			DPRINTF("ERROR: child process %d didn't stop as expected", pid);
213 			goto bad;
214 		}
215 
216 		/* Check for an unexpected status. */
217 		if (!WIFSTOPPED(status)) {
218 			error = ENOENT;
219 			DPRINTFX("ERROR: child process %d status 0x%x", pid, status);
220 			goto bad;
221 		}
222 
223 		/* The parent owns the process handle. */
224 		error = proc_init(pid, 0, PS_IDLE, &phdl);
225 		if (error == 0)
226 			phdl->status = PS_STOP;
227 
228 bad:
229 		if (error != 0 && phdl != NULL) {
230 			proc_free(phdl);
231 			phdl = NULL;
232 		}
233 	}
234 	*pphdl = phdl;
235 	return (error);
236 }
237 
238 void
239 proc_free(struct proc_handle *phdl)
240 {
241 	struct file_info *file;
242 	size_t i;
243 
244 	for (i = 0; i < phdl->nmappings; i++) {
245 		file = phdl->mappings[i].file;
246 		if (file != NULL && --file->refs == 0) {
247 			if (file->elf != NULL) {
248 				(void)elf_end(file->elf);
249 				(void)close(file->fd);
250 				if (file->symtab.nsyms > 0)
251 					free(file->symtab.index);
252 				if (file->dynsymtab.nsyms > 0)
253 					free(file->dynsymtab.index);
254 			}
255 			free(file);
256 		}
257 	}
258 	if (phdl->maparrsz > 0)
259 		free(phdl->mappings);
260 	if (phdl->procstat != NULL)
261 		procstat_close(phdl->procstat);
262 	if (phdl->rdap != NULL)
263 		rd_delete(phdl->rdap);
264 	free(phdl);
265 }
266