xref: /freebsd/lib/libproc/proc_create.c (revision 5e53a4f90f82c4345f277dd87cc9292f26e04a29)
12c633af4SJohn Birrell /*-
2*5e53a4f9SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*5e53a4f9SPedro F. Giffuni  *
42c633af4SJohn Birrell  * Copyright (c) 2008 John Birrell (jb@freebsd.org)
52c633af4SJohn Birrell  * All rights reserved.
62c633af4SJohn Birrell  *
72c633af4SJohn Birrell  * Redistribution and use in source and binary forms, with or without
82c633af4SJohn Birrell  * modification, are permitted provided that the following conditions
92c633af4SJohn Birrell  * are met:
102c633af4SJohn Birrell  * 1. Redistributions of source code must retain the above copyright
112c633af4SJohn Birrell  *    notice, this list of conditions and the following disclaimer.
122c633af4SJohn Birrell  * 2. Redistributions in binary form must reproduce the above copyright
132c633af4SJohn Birrell  *    notice, this list of conditions and the following disclaimer in the
142c633af4SJohn Birrell  *    documentation and/or other materials provided with the distribution.
152c633af4SJohn Birrell  *
162c633af4SJohn Birrell  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
172c633af4SJohn Birrell  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
182c633af4SJohn Birrell  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
192c633af4SJohn Birrell  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
202c633af4SJohn Birrell  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
212c633af4SJohn Birrell  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
222c633af4SJohn Birrell  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
232c633af4SJohn Birrell  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
242c633af4SJohn Birrell  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
252c633af4SJohn Birrell  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
262c633af4SJohn Birrell  * SUCH DAMAGE.
272c633af4SJohn Birrell  */
282c633af4SJohn Birrell 
29fcf9fc10SMark Johnston #include <sys/cdefs.h>
30fcf9fc10SMark Johnston __FBSDID("$FreeBSD$");
31fcf9fc10SMark Johnston 
32acc0eea6SMark Johnston #include <sys/types.h>
33acc0eea6SMark Johnston #include <sys/sysctl.h>
344808a678SMark Johnston #include <sys/user.h>
35acc0eea6SMark Johnston #include <sys/wait.h>
36acc0eea6SMark Johnston 
372c633af4SJohn Birrell #include <err.h>
382c633af4SJohn Birrell #include <errno.h>
392c633af4SJohn Birrell #include <fcntl.h>
402c633af4SJohn Birrell #include <limits.h>
412c633af4SJohn Birrell #include <stdlib.h>
422c633af4SJohn Birrell #include <string.h>
432c633af4SJohn Birrell #include <unistd.h>
44acc0eea6SMark Johnston 
454808a678SMark Johnston #include <libelf.h>
464808a678SMark Johnston #include <libprocstat.h>
474808a678SMark Johnston 
48acc0eea6SMark Johnston #include "_libproc.h"
49acc0eea6SMark Johnston 
504808a678SMark Johnston static int	getelfclass(int);
514808a678SMark Johnston static int	proc_init(pid_t, int, int, struct proc_handle **);
52acc0eea6SMark Johnston 
53acc0eea6SMark Johnston static int
544808a678SMark Johnston getelfclass(int fd)
55acc0eea6SMark Johnston {
564808a678SMark Johnston 	GElf_Ehdr ehdr;
574808a678SMark Johnston 	Elf *e;
584808a678SMark Johnston 	int class;
594808a678SMark Johnston 
604808a678SMark Johnston 	class = ELFCLASSNONE;
614808a678SMark Johnston 
624808a678SMark Johnston 	if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL)
634808a678SMark Johnston 		goto out;
644808a678SMark Johnston 	if (gelf_getehdr(e, &ehdr) == NULL)
654808a678SMark Johnston 		goto out;
664808a678SMark Johnston 	class = ehdr.e_ident[EI_CLASS];
674808a678SMark Johnston out:
684808a678SMark Johnston 	(void)elf_end(e);
694808a678SMark Johnston 	return (class);
704808a678SMark Johnston }
714808a678SMark Johnston 
724808a678SMark Johnston static int
734808a678SMark Johnston proc_init(pid_t pid, int flags, int status, struct proc_handle **pphdl)
744808a678SMark Johnston {
754808a678SMark Johnston 	struct kinfo_proc *kp;
764808a678SMark Johnston 	struct proc_handle *phdl;
774808a678SMark Johnston 	int error, class, count, fd;
784808a678SMark Johnston 
79ce601a26SConrad Meyer 	error = ENOMEM;
804808a678SMark Johnston 	if ((phdl = malloc(sizeof(*phdl))) == NULL)
81ce601a26SConrad Meyer 		goto out;
82acc0eea6SMark Johnston 
83acc0eea6SMark Johnston 	memset(phdl, 0, sizeof(*phdl));
84b1bb30e5SMark Johnston 	phdl->public.pid = pid;
85acc0eea6SMark Johnston 	phdl->flags = flags;
86acc0eea6SMark Johnston 	phdl->status = status;
874808a678SMark Johnston 	phdl->procstat = procstat_open_sysctl();
884808a678SMark Johnston 	if (phdl->procstat == NULL)
89ce601a26SConrad Meyer 		goto out;
90acc0eea6SMark Johnston 
914808a678SMark Johnston 	/* Obtain a path to the executable. */
924808a678SMark Johnston 	if ((kp = procstat_getprocs(phdl->procstat, KERN_PROC_PID, pid,
934808a678SMark Johnston 	    &count)) == NULL)
94ce601a26SConrad Meyer 		goto out;
954808a678SMark Johnston 	error = procstat_getpathname(phdl->procstat, kp, phdl->execpath,
964808a678SMark Johnston 	    sizeof(phdl->execpath));
974808a678SMark Johnston 	procstat_freeprocs(phdl->procstat, kp);
984808a678SMark Johnston 	if (error != 0)
99ce601a26SConrad Meyer 		goto out;
100acc0eea6SMark Johnston 
1014808a678SMark Johnston 	/* Use it to determine the data model for the process. */
1024808a678SMark Johnston 	if ((fd = open(phdl->execpath, O_RDONLY)) < 0) {
1034808a678SMark Johnston 		error = errno;
1044808a678SMark Johnston 		goto out;
1054808a678SMark Johnston 	}
1064808a678SMark Johnston 	class = getelfclass(fd);
1074808a678SMark Johnston 	switch (class) {
1084808a678SMark Johnston 	case ELFCLASS64:
1094808a678SMark Johnston 		phdl->model = PR_MODEL_LP64;
1104808a678SMark Johnston 		break;
1114808a678SMark Johnston 	case ELFCLASS32:
1124808a678SMark Johnston 		phdl->model = PR_MODEL_ILP32;
1134808a678SMark Johnston 		break;
1144808a678SMark Johnston 	case ELFCLASSNONE:
1154808a678SMark Johnston 	default:
1164808a678SMark Johnston 		error = EINVAL;
1174808a678SMark Johnston 		break;
1184808a678SMark Johnston 	}
1194808a678SMark Johnston 	(void)close(fd);
1204808a678SMark Johnston 
1214808a678SMark Johnston out:
1224808a678SMark Johnston 	*pphdl = phdl;
1234808a678SMark Johnston 	return (error);
124acc0eea6SMark Johnston }
1252c633af4SJohn Birrell 
1262c633af4SJohn Birrell int
1272c633af4SJohn Birrell proc_attach(pid_t pid, int flags, struct proc_handle **pphdl)
1282c633af4SJohn Birrell {
1292c633af4SJohn Birrell 	struct proc_handle *phdl;
1304808a678SMark Johnston 	int error, status;
1312c633af4SJohn Birrell 
132b043b5dcSMark Johnston 	if (pid == 0 || (pid == getpid() && (flags & PATTACH_RDONLY) == 0))
1332c633af4SJohn Birrell 		return (EINVAL);
1344808a678SMark Johnston 	if (elf_version(EV_CURRENT) == EV_NONE)
1354808a678SMark Johnston 		return (ENOENT);
1362c633af4SJohn Birrell 
1372c633af4SJohn Birrell 	/*
1382c633af4SJohn Birrell 	 * Allocate memory for the process handle, a structure containing
1392c633af4SJohn Birrell 	 * all things related to the process.
1402c633af4SJohn Birrell 	 */
1414808a678SMark Johnston 	error = proc_init(pid, flags, PS_RUN, &phdl);
142acc0eea6SMark Johnston 	if (error != 0)
143acc0eea6SMark Johnston 		goto out;
144acc0eea6SMark Johnston 
145b043b5dcSMark Johnston 	if ((flags & PATTACH_RDONLY) == 0) {
146b1bb30e5SMark Johnston 		if (ptrace(PT_ATTACH, proc_getpid(phdl), 0, 0) != 0) {
1472c633af4SJohn Birrell 			error = errno;
1488eb20f36SRui Paulo 			DPRINTF("ERROR: cannot ptrace child process %d", pid);
1498eb20f36SRui Paulo 			goto out;
1508eb20f36SRui Paulo 		}
1512c633af4SJohn Birrell 
1522c633af4SJohn Birrell 		/* Wait for the child process to stop. */
1538eb20f36SRui Paulo 		if (waitpid(pid, &status, WUNTRACED) == -1) {
1548eb20f36SRui Paulo 			error = errno;
1558eb20f36SRui Paulo 			DPRINTF("ERROR: child process %d didn't stop as expected", pid);
1568eb20f36SRui Paulo 			goto out;
1578eb20f36SRui Paulo 		}
1582c633af4SJohn Birrell 
1592c633af4SJohn Birrell 		/* Check for an unexpected status. */
1604808a678SMark Johnston 		if (!WIFSTOPPED(status))
16130e81f7eSMark Johnston 			DPRINTFX("ERROR: child process %d status 0x%x", pid, status);
1622c633af4SJohn Birrell 		else
1632c633af4SJohn Birrell 			phdl->status = PS_STOP;
1642c633af4SJohn Birrell 
165b043b5dcSMark Johnston 		if ((flags & PATTACH_NOSTOP) != 0)
166b043b5dcSMark Johnston 			proc_continue(phdl);
167b043b5dcSMark Johnston 	}
168b043b5dcSMark Johnston 
169a8375da0SAndriy Gapon out:
170b043b5dcSMark Johnston 	if (error != 0 && phdl != NULL) {
1712c633af4SJohn Birrell 		proc_free(phdl);
1724808a678SMark Johnston 		phdl = NULL;
1734808a678SMark Johnston 	}
1742c633af4SJohn Birrell 	*pphdl = phdl;
1752c633af4SJohn Birrell 	return (error);
1762c633af4SJohn Birrell }
1772c633af4SJohn Birrell 
1782c633af4SJohn Birrell int
179820e0679SCraig Rodrigues proc_create(const char *file, char * const *argv, proc_child_func *pcf,
180820e0679SCraig Rodrigues     void *child_arg, struct proc_handle **pphdl)
1812c633af4SJohn Birrell {
1822c633af4SJohn Birrell 	struct proc_handle *phdl;
1838440c5e6SMark Johnston 	int error, status;
1842c633af4SJohn Birrell 	pid_t pid;
1852c633af4SJohn Birrell 
1864808a678SMark Johnston 	if (elf_version(EV_CURRENT) == EV_NONE)
1874808a678SMark Johnston 		return (ENOENT);
1888eb20f36SRui Paulo 
189643fc6b2SMark Johnston 	error = 0;
190643fc6b2SMark Johnston 	phdl = NULL;
191643fc6b2SMark Johnston 
1922c633af4SJohn Birrell 	/* Fork a new process. */
193820e0679SCraig Rodrigues 	if ((pid = vfork()) == -1)
1942c633af4SJohn Birrell 		error = errno;
1952c633af4SJohn Birrell 	else if (pid == 0) {
1962c633af4SJohn Birrell 		/* The child expects to be traced. */
1972c633af4SJohn Birrell 		if (ptrace(PT_TRACE_ME, 0, 0, 0) != 0)
1982c633af4SJohn Birrell 			_exit(1);
1992c633af4SJohn Birrell 
200820e0679SCraig Rodrigues 		if (pcf != NULL)
201820e0679SCraig Rodrigues 			(*pcf)(child_arg);
202820e0679SCraig Rodrigues 
2032c633af4SJohn Birrell 		/* Execute the specified file: */
2042c633af4SJohn Birrell 		execvp(file, argv);
2052c633af4SJohn Birrell 
2062c633af4SJohn Birrell 		/* Couldn't execute the file. */
2072c633af4SJohn Birrell 		_exit(2);
2084808a678SMark Johnston 		/* NOTREACHED */
2092c633af4SJohn Birrell 	} else {
2102c633af4SJohn Birrell 		/* The parent owns the process handle. */
2114808a678SMark Johnston 		error = proc_init(pid, 0, PS_IDLE, &phdl);
212acc0eea6SMark Johnston 		if (error != 0)
213acc0eea6SMark Johnston 			goto bad;
2142c633af4SJohn Birrell 
2152c633af4SJohn Birrell 		/* Wait for the child process to stop. */
2168eb20f36SRui Paulo 		if (waitpid(pid, &status, WUNTRACED) == -1) {
2178eb20f36SRui Paulo 			error = errno;
2188eb20f36SRui Paulo 			DPRINTF("ERROR: child process %d didn't stop as expected", pid);
2198eb20f36SRui Paulo 			goto bad;
2202c633af4SJohn Birrell 		}
2212c633af4SJohn Birrell 
2228eb20f36SRui Paulo 		/* Check for an unexpected status. */
2234808a678SMark Johnston 		if (!WIFSTOPPED(status)) {
2248440c5e6SMark Johnston 			error = EBUSY;
22530e81f7eSMark Johnston 			DPRINTFX("ERROR: child process %d status 0x%x", pid, status);
2268eb20f36SRui Paulo 			goto bad;
2278eb20f36SRui Paulo 		}
2288440c5e6SMark Johnston 		phdl->status = PS_STOP;
2298440c5e6SMark Johnston 
2308eb20f36SRui Paulo bad:
2318440c5e6SMark Johnston 		if (error != 0 && phdl != NULL) {
2322c633af4SJohn Birrell 			proc_free(phdl);
2334808a678SMark Johnston 			phdl = NULL;
2344808a678SMark Johnston 		}
2358440c5e6SMark Johnston 	}
2362c633af4SJohn Birrell 	*pphdl = phdl;
2372c633af4SJohn Birrell 	return (error);
2382c633af4SJohn Birrell }
2392c633af4SJohn Birrell 
2402c633af4SJohn Birrell void
2412c633af4SJohn Birrell proc_free(struct proc_handle *phdl)
2422c633af4SJohn Birrell {
24307a9c2e6SMark Johnston 	struct file_info *file;
24407a9c2e6SMark Johnston 	size_t i;
2454808a678SMark Johnston 
24607a9c2e6SMark Johnston 	for (i = 0; i < phdl->nmappings; i++) {
24707a9c2e6SMark Johnston 		file = phdl->mappings[i].file;
24807a9c2e6SMark Johnston 		if (file != NULL && --file->refs == 0) {
24907a9c2e6SMark Johnston 			if (file->elf != NULL) {
25007a9c2e6SMark Johnston 				(void)elf_end(file->elf);
25107a9c2e6SMark Johnston 				(void)close(file->fd);
252c156354fSMark Johnston 				if (file->symtab.nsyms > 0)
253c156354fSMark Johnston 					free(file->symtab.index);
254c156354fSMark Johnston 				if (file->dynsymtab.nsyms > 0)
255c156354fSMark Johnston 					free(file->dynsymtab.index);
25607a9c2e6SMark Johnston 			}
25707a9c2e6SMark Johnston 			free(file);
25807a9c2e6SMark Johnston 		}
25907a9c2e6SMark Johnston 	}
26007a9c2e6SMark Johnston 	if (phdl->maparrsz > 0)
26107a9c2e6SMark Johnston 		free(phdl->mappings);
2624808a678SMark Johnston 	if (phdl->procstat != NULL)
2634808a678SMark Johnston 		procstat_close(phdl->procstat);
26407a9c2e6SMark Johnston 	if (phdl->rdap != NULL)
26507a9c2e6SMark Johnston 		rd_delete(phdl->rdap);
2662c633af4SJohn Birrell 	free(phdl);
2672c633af4SJohn Birrell }
268