xref: /freebsd/lib/libproc/proc_create.c (revision 4808a67805fe7287d8054934d978cd1076302689)
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  * $FreeBSD$
272c633af4SJohn Birrell  */
282c633af4SJohn Birrell 
29acc0eea6SMark Johnston #include <sys/types.h>
30acc0eea6SMark Johnston #include <sys/sysctl.h>
31*4808a678SMark Johnston #include <sys/user.h>
32acc0eea6SMark Johnston #include <sys/wait.h>
33acc0eea6SMark Johnston 
342c633af4SJohn Birrell #include <err.h>
352c633af4SJohn Birrell #include <errno.h>
362c633af4SJohn Birrell #include <fcntl.h>
372c633af4SJohn Birrell #include <limits.h>
382c633af4SJohn Birrell #include <stdlib.h>
392c633af4SJohn Birrell #include <string.h>
402c633af4SJohn Birrell #include <unistd.h>
41acc0eea6SMark Johnston 
42*4808a678SMark Johnston #include <libelf.h>
43*4808a678SMark Johnston #include <libprocstat.h>
44*4808a678SMark Johnston 
45acc0eea6SMark Johnston #include "_libproc.h"
46acc0eea6SMark Johnston 
47*4808a678SMark Johnston static int	getelfclass(int);
48*4808a678SMark Johnston static int	proc_init(pid_t, int, int, struct proc_handle **);
49acc0eea6SMark Johnston 
50acc0eea6SMark Johnston static int
51*4808a678SMark Johnston getelfclass(int fd)
52acc0eea6SMark Johnston {
53*4808a678SMark Johnston 	GElf_Ehdr ehdr;
54*4808a678SMark Johnston 	Elf *e;
55*4808a678SMark Johnston 	int class;
56*4808a678SMark Johnston 
57*4808a678SMark Johnston 	class = ELFCLASSNONE;
58*4808a678SMark Johnston 
59*4808a678SMark Johnston 	if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL)
60*4808a678SMark Johnston 		goto out;
61*4808a678SMark Johnston 	if (gelf_getehdr(e, &ehdr) == NULL)
62*4808a678SMark Johnston 		goto out;
63*4808a678SMark Johnston 	class = ehdr.e_ident[EI_CLASS];
64*4808a678SMark Johnston out:
65*4808a678SMark Johnston 	(void)elf_end(e);
66*4808a678SMark Johnston 	return (class);
67*4808a678SMark Johnston }
68*4808a678SMark Johnston 
69*4808a678SMark Johnston static int
70*4808a678SMark Johnston proc_init(pid_t pid, int flags, int status, struct proc_handle **pphdl)
71*4808a678SMark Johnston {
72*4808a678SMark Johnston 	struct kinfo_proc *kp;
73*4808a678SMark Johnston 	struct proc_handle *phdl;
74*4808a678SMark Johnston 	int error, class, count, fd;
75*4808a678SMark Johnston 
76*4808a678SMark Johnston 	*pphdl = NULL;
77*4808a678SMark Johnston 	if ((phdl = malloc(sizeof(*phdl))) == NULL)
78*4808a678SMark Johnston 		return (ENOMEM);
79acc0eea6SMark Johnston 
80acc0eea6SMark Johnston 	memset(phdl, 0, sizeof(*phdl));
81acc0eea6SMark Johnston 	phdl->pid = pid;
82acc0eea6SMark Johnston 	phdl->flags = flags;
83acc0eea6SMark Johnston 	phdl->status = status;
84*4808a678SMark Johnston 	phdl->procstat = procstat_open_sysctl();
85*4808a678SMark Johnston 	if (phdl->procstat == NULL)
86*4808a678SMark Johnston 		return (ENOMEM);
87acc0eea6SMark Johnston 
88*4808a678SMark Johnston 	/* Obtain a path to the executable. */
89*4808a678SMark Johnston 	if ((kp = procstat_getprocs(phdl->procstat, KERN_PROC_PID, pid,
90*4808a678SMark Johnston 	    &count)) == NULL)
91*4808a678SMark Johnston 		return (ENOMEM);
92*4808a678SMark Johnston 	error = procstat_getpathname(phdl->procstat, kp, phdl->execpath,
93*4808a678SMark Johnston 	    sizeof(phdl->execpath));
94*4808a678SMark Johnston 	procstat_freeprocs(phdl->procstat, kp);
95*4808a678SMark Johnston 	if (error != 0)
96acc0eea6SMark Johnston 		return (error);
97acc0eea6SMark Johnston 
98*4808a678SMark Johnston 	/* Use it to determine the data model for the process. */
99*4808a678SMark Johnston 	if ((fd = open(phdl->execpath, O_RDONLY)) < 0) {
100*4808a678SMark Johnston 		error = errno;
101*4808a678SMark Johnston 		goto out;
102*4808a678SMark Johnston 	}
103*4808a678SMark Johnston 	class = getelfclass(fd);
104*4808a678SMark Johnston 	switch (class) {
105*4808a678SMark Johnston 	case ELFCLASS64:
106*4808a678SMark Johnston 		phdl->model = PR_MODEL_LP64;
107*4808a678SMark Johnston 		break;
108*4808a678SMark Johnston 	case ELFCLASS32:
109*4808a678SMark Johnston 		phdl->model = PR_MODEL_ILP32;
110*4808a678SMark Johnston 		break;
111*4808a678SMark Johnston 	case ELFCLASSNONE:
112*4808a678SMark Johnston 	default:
113*4808a678SMark Johnston 		error = EINVAL;
114*4808a678SMark Johnston 		break;
115*4808a678SMark Johnston 	}
116*4808a678SMark Johnston 	(void)close(fd);
117*4808a678SMark Johnston 
118*4808a678SMark Johnston out:
119*4808a678SMark Johnston 	*pphdl = phdl;
120*4808a678SMark Johnston 	return (error);
121acc0eea6SMark Johnston }
1222c633af4SJohn Birrell 
1232c633af4SJohn Birrell int
1242c633af4SJohn Birrell proc_attach(pid_t pid, int flags, struct proc_handle **pphdl)
1252c633af4SJohn Birrell {
1262c633af4SJohn Birrell 	struct proc_handle *phdl;
127*4808a678SMark Johnston 	int error, status;
1282c633af4SJohn Birrell 
1298eb20f36SRui Paulo 	if (pid == 0 || pid == getpid())
1302c633af4SJohn Birrell 		return (EINVAL);
131*4808a678SMark Johnston 	if (elf_version(EV_CURRENT) == EV_NONE)
132*4808a678SMark Johnston 		return (ENOENT);
1332c633af4SJohn Birrell 
1342c633af4SJohn Birrell 	/*
1352c633af4SJohn Birrell 	 * Allocate memory for the process handle, a structure containing
1362c633af4SJohn Birrell 	 * all things related to the process.
1372c633af4SJohn Birrell 	 */
138*4808a678SMark Johnston 	error = proc_init(pid, flags, PS_RUN, &phdl);
139acc0eea6SMark Johnston 	if (error != 0)
140acc0eea6SMark Johnston 		goto out;
141acc0eea6SMark Johnston 
1428eb20f36SRui Paulo 	if (ptrace(PT_ATTACH, phdl->pid, 0, 0) != 0) {
1432c633af4SJohn Birrell 		error = errno;
1448eb20f36SRui Paulo 		DPRINTF("ERROR: cannot ptrace child process %d", pid);
1458eb20f36SRui Paulo 		goto out;
1468eb20f36SRui Paulo 	}
1472c633af4SJohn Birrell 
1482c633af4SJohn Birrell 	/* Wait for the child process to stop. */
1498eb20f36SRui Paulo 	if (waitpid(pid, &status, WUNTRACED) == -1) {
1508eb20f36SRui Paulo 		error = errno;
1518eb20f36SRui Paulo 		DPRINTF("ERROR: child process %d didn't stop as expected", pid);
1528eb20f36SRui Paulo 		goto out;
1538eb20f36SRui Paulo 	}
1542c633af4SJohn Birrell 
1552c633af4SJohn Birrell 	/* Check for an unexpected status. */
156*4808a678SMark Johnston 	if (!WIFSTOPPED(status))
15730e81f7eSMark Johnston 		DPRINTFX("ERROR: child process %d status 0x%x", pid, status);
1582c633af4SJohn Birrell 	else
1592c633af4SJohn Birrell 		phdl->status = PS_STOP;
1602c633af4SJohn Birrell 
161a8375da0SAndriy Gapon out:
162*4808a678SMark Johnston 	if (error && phdl != NULL) {
1632c633af4SJohn Birrell 		proc_free(phdl);
164*4808a678SMark Johnston 		phdl = NULL;
165*4808a678SMark Johnston 	}
1662c633af4SJohn Birrell 	*pphdl = phdl;
1672c633af4SJohn Birrell 	return (error);
1682c633af4SJohn Birrell }
1692c633af4SJohn Birrell 
1702c633af4SJohn Birrell int
171820e0679SCraig Rodrigues proc_create(const char *file, char * const *argv, proc_child_func *pcf,
172820e0679SCraig Rodrigues     void *child_arg, struct proc_handle **pphdl)
1732c633af4SJohn Birrell {
1742c633af4SJohn Birrell 	struct proc_handle *phdl;
1752c633af4SJohn Birrell 	int error = 0;
1762c633af4SJohn Birrell 	int status;
1772c633af4SJohn Birrell 	pid_t pid;
1782c633af4SJohn Birrell 
179*4808a678SMark Johnston 	if (elf_version(EV_CURRENT) == EV_NONE)
180*4808a678SMark Johnston 		return (ENOENT);
1818eb20f36SRui Paulo 
1822c633af4SJohn Birrell 	/* Fork a new process. */
183820e0679SCraig Rodrigues 	if ((pid = vfork()) == -1)
1842c633af4SJohn Birrell 		error = errno;
1852c633af4SJohn Birrell 	else if (pid == 0) {
1862c633af4SJohn Birrell 		/* The child expects to be traced. */
1872c633af4SJohn Birrell 		if (ptrace(PT_TRACE_ME, 0, 0, 0) != 0)
1882c633af4SJohn Birrell 			_exit(1);
1892c633af4SJohn Birrell 
190820e0679SCraig Rodrigues 		if (pcf != NULL)
191820e0679SCraig Rodrigues 			(*pcf)(child_arg);
192820e0679SCraig Rodrigues 
1932c633af4SJohn Birrell 		/* Execute the specified file: */
1942c633af4SJohn Birrell 		execvp(file, argv);
1952c633af4SJohn Birrell 
1962c633af4SJohn Birrell 		/* Couldn't execute the file. */
1972c633af4SJohn Birrell 		_exit(2);
198*4808a678SMark Johnston 		/* NOTREACHED */
1992c633af4SJohn Birrell 	} else {
2002c633af4SJohn Birrell 		/* The parent owns the process handle. */
201*4808a678SMark Johnston 		error = proc_init(pid, 0, PS_IDLE, &phdl);
202acc0eea6SMark Johnston 		if (error != 0)
203acc0eea6SMark Johnston 			goto bad;
2042c633af4SJohn Birrell 
2052c633af4SJohn Birrell 		/* Wait for the child process to stop. */
2068eb20f36SRui Paulo 		if (waitpid(pid, &status, WUNTRACED) == -1) {
2078eb20f36SRui Paulo 			error = errno;
2088eb20f36SRui Paulo 			DPRINTF("ERROR: child process %d didn't stop as expected", pid);
2098eb20f36SRui Paulo 			goto bad;
2102c633af4SJohn Birrell 		}
2112c633af4SJohn Birrell 
2128eb20f36SRui Paulo 		/* Check for an unexpected status. */
213*4808a678SMark Johnston 		if (!WIFSTOPPED(status)) {
2148eb20f36SRui Paulo 			error = errno;
21530e81f7eSMark Johnston 			DPRINTFX("ERROR: child process %d status 0x%x", pid, status);
2168eb20f36SRui Paulo 			goto bad;
2178eb20f36SRui Paulo 		} else
2188eb20f36SRui Paulo 			phdl->status = PS_STOP;
2198eb20f36SRui Paulo 	}
2208eb20f36SRui Paulo bad:
221*4808a678SMark Johnston 	if (error && phdl != NULL) {
2222c633af4SJohn Birrell 		proc_free(phdl);
223*4808a678SMark Johnston 		phdl = NULL;
224*4808a678SMark Johnston 	}
2252c633af4SJohn Birrell 	*pphdl = phdl;
2262c633af4SJohn Birrell 	return (error);
2272c633af4SJohn Birrell }
2282c633af4SJohn Birrell 
2292c633af4SJohn Birrell void
2302c633af4SJohn Birrell proc_free(struct proc_handle *phdl)
2312c633af4SJohn Birrell {
232*4808a678SMark Johnston 
233*4808a678SMark Johnston 	if (phdl->procstat != NULL)
234*4808a678SMark Johnston 		procstat_close(phdl->procstat);
2352c633af4SJohn Birrell 	free(phdl);
2362c633af4SJohn Birrell }
237