xref: /freebsd/lib/libproc/proc_create.c (revision 07a9c2e65d5f2f21cc9783e33dd94a5ce8cc592d)
12c633af4SJohn Birrell /*-
22c633af4SJohn Birrell  * Copyright (c) 2008 John Birrell (jb@freebsd.org)
32c633af4SJohn Birrell  * All rights reserved.
42c633af4SJohn Birrell  *
52c633af4SJohn Birrell  * Redistribution and use in source and binary forms, with or without
62c633af4SJohn Birrell  * modification, are permitted provided that the following conditions
72c633af4SJohn Birrell  * are met:
82c633af4SJohn Birrell  * 1. Redistributions of source code must retain the above copyright
92c633af4SJohn Birrell  *    notice, this list of conditions and the following disclaimer.
102c633af4SJohn Birrell  * 2. Redistributions in binary form must reproduce the above copyright
112c633af4SJohn Birrell  *    notice, this list of conditions and the following disclaimer in the
122c633af4SJohn Birrell  *    documentation and/or other materials provided with the distribution.
132c633af4SJohn Birrell  *
142c633af4SJohn Birrell  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
152c633af4SJohn Birrell  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
162c633af4SJohn Birrell  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
172c633af4SJohn Birrell  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
182c633af4SJohn Birrell  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
192c633af4SJohn Birrell  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
202c633af4SJohn Birrell  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
212c633af4SJohn Birrell  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
222c633af4SJohn Birrell  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
232c633af4SJohn Birrell  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
242c633af4SJohn Birrell  * SUCH DAMAGE.
252c633af4SJohn Birrell  */
262c633af4SJohn Birrell 
27fcf9fc10SMark Johnston #include <sys/cdefs.h>
28fcf9fc10SMark Johnston __FBSDID("$FreeBSD$");
29fcf9fc10SMark Johnston 
30acc0eea6SMark Johnston #include <sys/types.h>
31acc0eea6SMark Johnston #include <sys/sysctl.h>
324808a678SMark Johnston #include <sys/user.h>
33acc0eea6SMark Johnston #include <sys/wait.h>
34acc0eea6SMark Johnston 
352c633af4SJohn Birrell #include <err.h>
362c633af4SJohn Birrell #include <errno.h>
372c633af4SJohn Birrell #include <fcntl.h>
382c633af4SJohn Birrell #include <limits.h>
392c633af4SJohn Birrell #include <stdlib.h>
402c633af4SJohn Birrell #include <string.h>
412c633af4SJohn Birrell #include <unistd.h>
42acc0eea6SMark Johnston 
434808a678SMark Johnston #include <libelf.h>
444808a678SMark Johnston #include <libprocstat.h>
454808a678SMark Johnston 
46acc0eea6SMark Johnston #include "_libproc.h"
47acc0eea6SMark Johnston 
484808a678SMark Johnston static int	getelfclass(int);
494808a678SMark Johnston static int	proc_init(pid_t, int, int, struct proc_handle **);
50acc0eea6SMark Johnston 
51acc0eea6SMark Johnston static int
524808a678SMark Johnston getelfclass(int fd)
53acc0eea6SMark Johnston {
544808a678SMark Johnston 	GElf_Ehdr ehdr;
554808a678SMark Johnston 	Elf *e;
564808a678SMark Johnston 	int class;
574808a678SMark Johnston 
584808a678SMark Johnston 	class = ELFCLASSNONE;
594808a678SMark Johnston 
604808a678SMark Johnston 	if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL)
614808a678SMark Johnston 		goto out;
624808a678SMark Johnston 	if (gelf_getehdr(e, &ehdr) == NULL)
634808a678SMark Johnston 		goto out;
644808a678SMark Johnston 	class = ehdr.e_ident[EI_CLASS];
654808a678SMark Johnston out:
664808a678SMark Johnston 	(void)elf_end(e);
674808a678SMark Johnston 	return (class);
684808a678SMark Johnston }
694808a678SMark Johnston 
704808a678SMark Johnston static int
714808a678SMark Johnston proc_init(pid_t pid, int flags, int status, struct proc_handle **pphdl)
724808a678SMark Johnston {
734808a678SMark Johnston 	struct kinfo_proc *kp;
744808a678SMark Johnston 	struct proc_handle *phdl;
754808a678SMark Johnston 	int error, class, count, fd;
764808a678SMark Johnston 
77ce601a26SConrad Meyer 	error = ENOMEM;
784808a678SMark Johnston 	if ((phdl = malloc(sizeof(*phdl))) == NULL)
79ce601a26SConrad Meyer 		goto out;
80acc0eea6SMark Johnston 
81acc0eea6SMark Johnston 	memset(phdl, 0, sizeof(*phdl));
82b1bb30e5SMark Johnston 	phdl->public.pid = pid;
83acc0eea6SMark Johnston 	phdl->flags = flags;
84acc0eea6SMark Johnston 	phdl->status = status;
854808a678SMark Johnston 	phdl->procstat = procstat_open_sysctl();
864808a678SMark Johnston 	if (phdl->procstat == NULL)
87ce601a26SConrad Meyer 		goto out;
88acc0eea6SMark Johnston 
894808a678SMark Johnston 	/* Obtain a path to the executable. */
904808a678SMark Johnston 	if ((kp = procstat_getprocs(phdl->procstat, KERN_PROC_PID, pid,
914808a678SMark Johnston 	    &count)) == NULL)
92ce601a26SConrad Meyer 		goto out;
934808a678SMark Johnston 	error = procstat_getpathname(phdl->procstat, kp, phdl->execpath,
944808a678SMark Johnston 	    sizeof(phdl->execpath));
954808a678SMark Johnston 	procstat_freeprocs(phdl->procstat, kp);
964808a678SMark Johnston 	if (error != 0)
97ce601a26SConrad Meyer 		goto out;
98acc0eea6SMark Johnston 
994808a678SMark Johnston 	/* Use it to determine the data model for the process. */
1004808a678SMark Johnston 	if ((fd = open(phdl->execpath, O_RDONLY)) < 0) {
1014808a678SMark Johnston 		error = errno;
1024808a678SMark Johnston 		goto out;
1034808a678SMark Johnston 	}
1044808a678SMark Johnston 	class = getelfclass(fd);
1054808a678SMark Johnston 	switch (class) {
1064808a678SMark Johnston 	case ELFCLASS64:
1074808a678SMark Johnston 		phdl->model = PR_MODEL_LP64;
1084808a678SMark Johnston 		break;
1094808a678SMark Johnston 	case ELFCLASS32:
1104808a678SMark Johnston 		phdl->model = PR_MODEL_ILP32;
1114808a678SMark Johnston 		break;
1124808a678SMark Johnston 	case ELFCLASSNONE:
1134808a678SMark Johnston 	default:
1144808a678SMark Johnston 		error = EINVAL;
1154808a678SMark Johnston 		break;
1164808a678SMark Johnston 	}
1174808a678SMark Johnston 	(void)close(fd);
1184808a678SMark Johnston 
1194808a678SMark Johnston out:
1204808a678SMark Johnston 	*pphdl = phdl;
1214808a678SMark Johnston 	return (error);
122acc0eea6SMark Johnston }
1232c633af4SJohn Birrell 
1242c633af4SJohn Birrell int
1252c633af4SJohn Birrell proc_attach(pid_t pid, int flags, struct proc_handle **pphdl)
1262c633af4SJohn Birrell {
1272c633af4SJohn Birrell 	struct proc_handle *phdl;
1284808a678SMark Johnston 	int error, status;
1292c633af4SJohn Birrell 
1308eb20f36SRui Paulo 	if (pid == 0 || pid == getpid())
1312c633af4SJohn Birrell 		return (EINVAL);
1324808a678SMark Johnston 	if (elf_version(EV_CURRENT) == EV_NONE)
1334808a678SMark Johnston 		return (ENOENT);
1342c633af4SJohn Birrell 
1352c633af4SJohn Birrell 	/*
1362c633af4SJohn Birrell 	 * Allocate memory for the process handle, a structure containing
1372c633af4SJohn Birrell 	 * all things related to the process.
1382c633af4SJohn Birrell 	 */
1394808a678SMark Johnston 	error = proc_init(pid, flags, PS_RUN, &phdl);
140acc0eea6SMark Johnston 	if (error != 0)
141acc0eea6SMark Johnston 		goto out;
142acc0eea6SMark Johnston 
143b1bb30e5SMark Johnston 	if (ptrace(PT_ATTACH, proc_getpid(phdl), 0, 0) != 0) {
1442c633af4SJohn Birrell 		error = errno;
1458eb20f36SRui Paulo 		DPRINTF("ERROR: cannot ptrace child process %d", pid);
1468eb20f36SRui Paulo 		goto out;
1478eb20f36SRui Paulo 	}
1482c633af4SJohn Birrell 
1492c633af4SJohn Birrell 	/* Wait for the child process to stop. */
1508eb20f36SRui Paulo 	if (waitpid(pid, &status, WUNTRACED) == -1) {
1518eb20f36SRui Paulo 		error = errno;
1528eb20f36SRui Paulo 		DPRINTF("ERROR: child process %d didn't stop as expected", pid);
1538eb20f36SRui Paulo 		goto out;
1548eb20f36SRui Paulo 	}
1552c633af4SJohn Birrell 
1562c633af4SJohn Birrell 	/* Check for an unexpected status. */
1574808a678SMark Johnston 	if (!WIFSTOPPED(status))
15830e81f7eSMark Johnston 		DPRINTFX("ERROR: child process %d status 0x%x", pid, status);
1592c633af4SJohn Birrell 	else
1602c633af4SJohn Birrell 		phdl->status = PS_STOP;
1612c633af4SJohn Birrell 
162a8375da0SAndriy Gapon out:
1634808a678SMark Johnston 	if (error && phdl != NULL) {
1642c633af4SJohn Birrell 		proc_free(phdl);
1654808a678SMark Johnston 		phdl = NULL;
1664808a678SMark Johnston 	}
1672c633af4SJohn Birrell 	*pphdl = phdl;
1682c633af4SJohn Birrell 	return (error);
1692c633af4SJohn Birrell }
1702c633af4SJohn Birrell 
1712c633af4SJohn Birrell int
172820e0679SCraig Rodrigues proc_create(const char *file, char * const *argv, proc_child_func *pcf,
173820e0679SCraig Rodrigues     void *child_arg, struct proc_handle **pphdl)
1742c633af4SJohn Birrell {
1752c633af4SJohn Birrell 	struct proc_handle *phdl;
1762c633af4SJohn Birrell 	int error = 0;
1772c633af4SJohn Birrell 	int status;
1782c633af4SJohn Birrell 	pid_t pid;
1792c633af4SJohn Birrell 
1804808a678SMark Johnston 	if (elf_version(EV_CURRENT) == EV_NONE)
1814808a678SMark Johnston 		return (ENOENT);
1828eb20f36SRui Paulo 
1832c633af4SJohn Birrell 	/* Fork a new process. */
184820e0679SCraig Rodrigues 	if ((pid = vfork()) == -1)
1852c633af4SJohn Birrell 		error = errno;
1862c633af4SJohn Birrell 	else if (pid == 0) {
1872c633af4SJohn Birrell 		/* The child expects to be traced. */
1882c633af4SJohn Birrell 		if (ptrace(PT_TRACE_ME, 0, 0, 0) != 0)
1892c633af4SJohn Birrell 			_exit(1);
1902c633af4SJohn Birrell 
191820e0679SCraig Rodrigues 		if (pcf != NULL)
192820e0679SCraig Rodrigues 			(*pcf)(child_arg);
193820e0679SCraig Rodrigues 
1942c633af4SJohn Birrell 		/* Execute the specified file: */
1952c633af4SJohn Birrell 		execvp(file, argv);
1962c633af4SJohn Birrell 
1972c633af4SJohn Birrell 		/* Couldn't execute the file. */
1982c633af4SJohn Birrell 		_exit(2);
1994808a678SMark Johnston 		/* NOTREACHED */
2002c633af4SJohn Birrell 	} else {
2012c633af4SJohn Birrell 		/* The parent owns the process handle. */
2024808a678SMark Johnston 		error = proc_init(pid, 0, PS_IDLE, &phdl);
203acc0eea6SMark Johnston 		if (error != 0)
204acc0eea6SMark Johnston 			goto bad;
2052c633af4SJohn Birrell 
2062c633af4SJohn Birrell 		/* Wait for the child process to stop. */
2078eb20f36SRui Paulo 		if (waitpid(pid, &status, WUNTRACED) == -1) {
2088eb20f36SRui Paulo 			error = errno;
2098eb20f36SRui Paulo 			DPRINTF("ERROR: child process %d didn't stop as expected", pid);
2108eb20f36SRui Paulo 			goto bad;
2112c633af4SJohn Birrell 		}
2122c633af4SJohn Birrell 
2138eb20f36SRui Paulo 		/* Check for an unexpected status. */
2144808a678SMark Johnston 		if (!WIFSTOPPED(status)) {
2158eb20f36SRui Paulo 			error = errno;
21630e81f7eSMark Johnston 			DPRINTFX("ERROR: child process %d status 0x%x", pid, status);
2178eb20f36SRui Paulo 			goto bad;
2188eb20f36SRui Paulo 		} else
2198eb20f36SRui Paulo 			phdl->status = PS_STOP;
2208eb20f36SRui Paulo 	}
2218eb20f36SRui Paulo bad:
2224808a678SMark Johnston 	if (error && phdl != NULL) {
2232c633af4SJohn Birrell 		proc_free(phdl);
2244808a678SMark Johnston 		phdl = NULL;
2254808a678SMark Johnston 	}
2262c633af4SJohn Birrell 	*pphdl = phdl;
2272c633af4SJohn Birrell 	return (error);
2282c633af4SJohn Birrell }
2292c633af4SJohn Birrell 
2302c633af4SJohn Birrell void
2312c633af4SJohn Birrell proc_free(struct proc_handle *phdl)
2322c633af4SJohn Birrell {
233*07a9c2e6SMark Johnston 	struct file_info *file;
234*07a9c2e6SMark Johnston 	size_t i;
2354808a678SMark Johnston 
236*07a9c2e6SMark Johnston 	for (i = 0; i < phdl->nmappings; i++) {
237*07a9c2e6SMark Johnston 		file = phdl->mappings[i].file;
238*07a9c2e6SMark Johnston 		if (file != NULL && --file->refs == 0) {
239*07a9c2e6SMark Johnston 			if (file->elf != NULL) {
240*07a9c2e6SMark Johnston 				(void)elf_end(file->elf);
241*07a9c2e6SMark Johnston 				(void)close(file->fd);
242*07a9c2e6SMark Johnston 			}
243*07a9c2e6SMark Johnston 			free(file);
244*07a9c2e6SMark Johnston 		}
245*07a9c2e6SMark Johnston 	}
246*07a9c2e6SMark Johnston 	if (phdl->maparrsz > 0)
247*07a9c2e6SMark Johnston 		free(phdl->mappings);
2484808a678SMark Johnston 	if (phdl->procstat != NULL)
2494808a678SMark Johnston 		procstat_close(phdl->procstat);
250*07a9c2e6SMark Johnston 	if (phdl->rdap != NULL)
251*07a9c2e6SMark Johnston 		rd_delete(phdl->rdap);
2522c633af4SJohn Birrell 	free(phdl);
2532c633af4SJohn Birrell }
254