]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libproc/proc_create.c
Upgrade to OpenSSH 7.5p1.
[FreeBSD/FreeBSD.git] / lib / libproc / proc_create.c
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/types.h>
31 #include <sys/sysctl.h>
32 #include <sys/user.h>
33 #include <sys/wait.h>
34
35 #include <err.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <limits.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42
43 #include <libelf.h>
44 #include <libprocstat.h>
45
46 #include "_libproc.h"
47
48 static int      getelfclass(int);
49 static int      proc_init(pid_t, int, int, struct proc_handle **);
50
51 static int
52 getelfclass(int fd)
53 {
54         GElf_Ehdr ehdr;
55         Elf *e;
56         int class;
57
58         class = ELFCLASSNONE;
59
60         if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL)
61                 goto out;
62         if (gelf_getehdr(e, &ehdr) == NULL)
63                 goto out;
64         class = ehdr.e_ident[EI_CLASS];
65 out:
66         (void)elf_end(e);
67         return (class);
68 }
69
70 static int
71 proc_init(pid_t pid, int flags, int status, struct proc_handle **pphdl)
72 {
73         struct kinfo_proc *kp;
74         struct proc_handle *phdl;
75         int error, class, count, fd;
76
77         error = ENOMEM;
78         if ((phdl = malloc(sizeof(*phdl))) == NULL)
79                 goto out;
80
81         memset(phdl, 0, sizeof(*phdl));
82         phdl->public.pid = pid;
83         phdl->flags = flags;
84         phdl->status = status;
85         phdl->procstat = procstat_open_sysctl();
86         if (phdl->procstat == NULL)
87                 goto out;
88
89         /* Obtain a path to the executable. */
90         if ((kp = procstat_getprocs(phdl->procstat, KERN_PROC_PID, pid,
91             &count)) == NULL)
92                 goto out;
93         error = procstat_getpathname(phdl->procstat, kp, phdl->execpath,
94             sizeof(phdl->execpath));
95         procstat_freeprocs(phdl->procstat, kp);
96         if (error != 0)
97                 goto out;
98
99         /* Use it to determine the data model for the process. */
100         if ((fd = open(phdl->execpath, O_RDONLY)) < 0) {
101                 error = errno;
102                 goto out;
103         }
104         class = getelfclass(fd);
105         switch (class) {
106         case ELFCLASS64:
107                 phdl->model = PR_MODEL_LP64;
108                 break;
109         case ELFCLASS32:
110                 phdl->model = PR_MODEL_ILP32;
111                 break;
112         case ELFCLASSNONE:
113         default:
114                 error = EINVAL;
115                 break;
116         }
117         (void)close(fd);
118
119 out:
120         *pphdl = phdl;
121         return (error);
122 }
123
124 int
125 proc_attach(pid_t pid, int flags, struct proc_handle **pphdl)
126 {
127         struct proc_handle *phdl;
128         int error, status;
129
130         if (pid == 0 || (pid == getpid() && (flags & PATTACH_RDONLY) == 0))
131                 return (EINVAL);
132         if (elf_version(EV_CURRENT) == EV_NONE)
133                 return (ENOENT);
134
135         /*
136          * Allocate memory for the process handle, a structure containing
137          * all things related to the process.
138          */
139         error = proc_init(pid, flags, PS_RUN, &phdl);
140         if (error != 0)
141                 goto out;
142
143         if ((flags & PATTACH_RDONLY) == 0) {
144                 if (ptrace(PT_ATTACH, proc_getpid(phdl), 0, 0) != 0) {
145                         error = errno;
146                         DPRINTF("ERROR: cannot ptrace child process %d", pid);
147                         goto out;
148                 }
149
150                 /* Wait for the child process to stop. */
151                 if (waitpid(pid, &status, WUNTRACED) == -1) {
152                         error = errno;
153                         DPRINTF("ERROR: child process %d didn't stop as expected", pid);
154                         goto out;
155                 }
156
157                 /* Check for an unexpected status. */
158                 if (!WIFSTOPPED(status))
159                         DPRINTFX("ERROR: child process %d status 0x%x", pid, status);
160                 else
161                         phdl->status = PS_STOP;
162
163                 if ((flags & PATTACH_NOSTOP) != 0)
164                         proc_continue(phdl);
165         }
166
167 out:
168         if (error != 0 && phdl != NULL) {
169                 proc_free(phdl);
170                 phdl = NULL;
171         }
172         *pphdl = phdl;
173         return (error);
174 }
175
176 int
177 proc_create(const char *file, char * const *argv, proc_child_func *pcf,
178     void *child_arg, struct proc_handle **pphdl)
179 {
180         struct proc_handle *phdl;
181         int error, status;
182         pid_t pid;
183
184         if (elf_version(EV_CURRENT) == EV_NONE)
185                 return (ENOENT);
186
187         error = 0;
188         phdl = NULL;
189
190         /* Fork a new process. */
191         if ((pid = vfork()) == -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                 /* Execute the specified file: */
202                 execvp(file, argv);
203
204                 /* Couldn't execute the file. */
205                 _exit(2);
206                 /* NOTREACHED */
207         } else {
208                 /* The parent owns the process handle. */
209                 error = proc_init(pid, 0, PS_IDLE, &phdl);
210                 if (error != 0)
211                         goto bad;
212
213                 /* Wait for the child process to stop. */
214                 if (waitpid(pid, &status, WUNTRACED) == -1) {
215                         error = errno;
216                         DPRINTF("ERROR: child process %d didn't stop as expected", pid);
217                         goto bad;
218                 }
219
220                 /* Check for an unexpected status. */
221                 if (!WIFSTOPPED(status)) {
222                         error = EBUSY;
223                         DPRINTFX("ERROR: child process %d status 0x%x", pid, status);
224                         goto bad;
225                 }
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 }