xref: /freebsd/lib/libproc/proc_create.c (revision 640235e2c2ba32947f7c59d168437ffa1280f1e6)
1 /*-
2  * Copyright (c) 2008 John Birrell (jb@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$
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 static int	getelfclass(int);
48 static int	proc_init(pid_t, int, int, struct proc_handle **);
49 
50 static int
51 getelfclass(int fd)
52 {
53 	GElf_Ehdr ehdr;
54 	Elf *e;
55 	int class;
56 
57 	class = ELFCLASSNONE;
58 
59 	if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL)
60 		goto out;
61 	if (gelf_getehdr(e, &ehdr) == NULL)
62 		goto out;
63 	class = ehdr.e_ident[EI_CLASS];
64 out:
65 	(void)elf_end(e);
66 	return (class);
67 }
68 
69 static int
70 proc_init(pid_t pid, int flags, int status, struct proc_handle **pphdl)
71 {
72 	struct kinfo_proc *kp;
73 	struct proc_handle *phdl;
74 	int error, class, count, fd;
75 
76 	error = ENOMEM;
77 	if ((phdl = malloc(sizeof(*phdl))) == NULL)
78 		goto out;
79 
80 	memset(phdl, 0, sizeof(*phdl));
81 	phdl->pid = pid;
82 	phdl->flags = flags;
83 	phdl->status = status;
84 	phdl->procstat = procstat_open_sysctl();
85 	if (phdl->procstat == NULL)
86 		goto out;
87 
88 	/* Obtain a path to the executable. */
89 	if ((kp = procstat_getprocs(phdl->procstat, KERN_PROC_PID, pid,
90 	    &count)) == NULL)
91 		goto out;
92 	error = procstat_getpathname(phdl->procstat, kp, phdl->execpath,
93 	    sizeof(phdl->execpath));
94 	procstat_freeprocs(phdl->procstat, kp);
95 	if (error != 0)
96 		goto out;
97 
98 	/* Use it to determine the data model for the process. */
99 	if ((fd = open(phdl->execpath, O_RDONLY)) < 0) {
100 		error = errno;
101 		goto out;
102 	}
103 	class = getelfclass(fd);
104 	switch (class) {
105 	case ELFCLASS64:
106 		phdl->model = PR_MODEL_LP64;
107 		break;
108 	case ELFCLASS32:
109 		phdl->model = PR_MODEL_ILP32;
110 		break;
111 	case ELFCLASSNONE:
112 	default:
113 		error = EINVAL;
114 		break;
115 	}
116 	(void)close(fd);
117 
118 out:
119 	*pphdl = phdl;
120 	return (error);
121 }
122 
123 int
124 proc_attach(pid_t pid, int flags, struct proc_handle **pphdl)
125 {
126 	struct proc_handle *phdl;
127 	int error, status;
128 
129 	if (pid == 0 || pid == getpid())
130 		return (EINVAL);
131 	if (elf_version(EV_CURRENT) == EV_NONE)
132 		return (ENOENT);
133 
134 	/*
135 	 * Allocate memory for the process handle, a structure containing
136 	 * all things related to the process.
137 	 */
138 	error = proc_init(pid, flags, PS_RUN, &phdl);
139 	if (error != 0)
140 		goto out;
141 
142 	if (ptrace(PT_ATTACH, phdl->pid, 0, 0) != 0) {
143 		error = errno;
144 		DPRINTF("ERROR: cannot ptrace child process %d", pid);
145 		goto out;
146 	}
147 
148 	/* Wait for the child process to stop. */
149 	if (waitpid(pid, &status, WUNTRACED) == -1) {
150 		error = errno;
151 		DPRINTF("ERROR: child process %d didn't stop as expected", pid);
152 		goto out;
153 	}
154 
155 	/* Check for an unexpected status. */
156 	if (!WIFSTOPPED(status))
157 		DPRINTFX("ERROR: child process %d status 0x%x", pid, status);
158 	else
159 		phdl->status = PS_STOP;
160 
161 out:
162 	if (error && phdl != NULL) {
163 		proc_free(phdl);
164 		phdl = NULL;
165 	}
166 	*pphdl = phdl;
167 	return (error);
168 }
169 
170 int
171 proc_create(const char *file, char * const *argv, proc_child_func *pcf,
172     void *child_arg, struct proc_handle **pphdl)
173 {
174 	struct proc_handle *phdl;
175 	int error = 0;
176 	int status;
177 	pid_t pid;
178 
179 	if (elf_version(EV_CURRENT) == EV_NONE)
180 		return (ENOENT);
181 
182 	/* Fork a new process. */
183 	if ((pid = vfork()) == -1)
184 		error = errno;
185 	else if (pid == 0) {
186 		/* The child expects to be traced. */
187 		if (ptrace(PT_TRACE_ME, 0, 0, 0) != 0)
188 			_exit(1);
189 
190 		if (pcf != NULL)
191 			(*pcf)(child_arg);
192 
193 		/* Execute the specified file: */
194 		execvp(file, argv);
195 
196 		/* Couldn't execute the file. */
197 		_exit(2);
198 		/* NOTREACHED */
199 	} else {
200 		/* The parent owns the process handle. */
201 		error = proc_init(pid, 0, PS_IDLE, &phdl);
202 		if (error != 0)
203 			goto bad;
204 
205 		/* Wait for the child process to stop. */
206 		if (waitpid(pid, &status, WUNTRACED) == -1) {
207 			error = errno;
208 			DPRINTF("ERROR: child process %d didn't stop as expected", pid);
209 			goto bad;
210 		}
211 
212 		/* Check for an unexpected status. */
213 		if (!WIFSTOPPED(status)) {
214 			error = errno;
215 			DPRINTFX("ERROR: child process %d status 0x%x", pid, status);
216 			goto bad;
217 		} else
218 			phdl->status = PS_STOP;
219 	}
220 bad:
221 	if (error && phdl != NULL) {
222 		proc_free(phdl);
223 		phdl = NULL;
224 	}
225 	*pphdl = phdl;
226 	return (error);
227 }
228 
229 void
230 proc_free(struct proc_handle *phdl)
231 {
232 
233 	if (phdl->procstat != NULL)
234 		procstat_close(phdl->procstat);
235 	free(phdl);
236 }
237