]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libproc/proc_create.c
Fix iconv buffer overflow.
[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/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 "_libproc.h"
43
44 static int      proc_init(pid_t, int, int, struct proc_handle *);
45
46 static int
47 proc_init(pid_t pid, int flags, int status, struct proc_handle *phdl)
48 {
49         int mib[4], error;
50         size_t len;
51
52         memset(phdl, 0, sizeof(*phdl));
53         phdl->pid = pid;
54         phdl->flags = flags;
55         phdl->status = status;
56
57         mib[0] = CTL_KERN;
58         mib[1] = KERN_PROC;
59         mib[2] = KERN_PROC_PATHNAME;
60         mib[3] = pid;
61         len = sizeof(phdl->execname);
62         if (sysctl(mib, 4, phdl->execname, &len, NULL, 0) != 0) {
63                 error = errno;
64                 DPRINTF("ERROR: cannot get pathname for child process %d", pid);
65                 return (error);
66         }
67         if (len == 0)
68                 phdl->execname[0] = '\0';
69
70         return (0);
71 }
72
73 int
74 proc_attach(pid_t pid, int flags, struct proc_handle **pphdl)
75 {
76         struct proc_handle *phdl;
77         int error = 0;
78         int status;
79
80         if (pid == 0 || pid == getpid())
81                 return (EINVAL);
82
83         /*
84          * Allocate memory for the process handle, a structure containing
85          * all things related to the process.
86          */
87         if ((phdl = malloc(sizeof(struct proc_handle))) == NULL)
88                 return (ENOMEM);
89
90         elf_version(EV_CURRENT);
91
92         error = proc_init(pid, flags, PS_RUN, phdl);
93         if (error != 0)
94                 goto out;
95
96         if (ptrace(PT_ATTACH, phdl->pid, 0, 0) != 0) {
97                 error = errno;
98                 DPRINTF("ERROR: cannot ptrace child process %d", pid);
99                 goto out;
100         }
101
102         /* Wait for the child process to stop. */
103         if (waitpid(pid, &status, WUNTRACED) == -1) {
104                 error = errno;
105                 DPRINTF("ERROR: child process %d didn't stop as expected", pid);
106                 goto out;
107         }
108
109         /* Check for an unexpected status. */
110         if (WIFSTOPPED(status) == 0)
111                 DPRINTFX("ERROR: child process %d status 0x%x", pid, status);
112         else
113                 phdl->status = PS_STOP;
114
115 out:
116         if (error)
117                 proc_free(phdl);
118         else
119                 *pphdl = phdl;
120         return (error);
121 }
122
123 int
124 proc_create(const char *file, char * const *argv, proc_child_func *pcf,
125     void *child_arg, struct proc_handle **pphdl)
126 {
127         struct proc_handle *phdl;
128         int error = 0;
129         int status;
130         pid_t pid;
131
132         /*
133          * Allocate memory for the process handle, a structure containing
134          * all things related to the process.
135          */
136         if ((phdl = malloc(sizeof(struct proc_handle))) == NULL)
137                 return (ENOMEM);
138
139         elf_version(EV_CURRENT);
140
141         /* Fork a new process. */
142         if ((pid = vfork()) == -1)
143                 error = errno;
144         else if (pid == 0) {
145                 /* The child expects to be traced. */
146                 if (ptrace(PT_TRACE_ME, 0, 0, 0) != 0)
147                         _exit(1);
148
149                 if (pcf != NULL)
150                         (*pcf)(child_arg);
151
152                 /* Execute the specified file: */
153                 execvp(file, argv);
154
155                 /* Couldn't execute the file. */
156                 _exit(2);
157         } else {
158                 /* The parent owns the process handle. */
159                 error = proc_init(pid, 0, PS_IDLE, phdl);
160                 if (error != 0)
161                         goto bad;
162
163                 /* Wait for the child process to stop. */
164                 if (waitpid(pid, &status, WUNTRACED) == -1) {
165                         error = errno;
166                         DPRINTF("ERROR: child process %d didn't stop as expected", pid);
167                         goto bad;
168                 }
169
170                 /* Check for an unexpected status. */
171                 if (WIFSTOPPED(status) == 0) {
172                         error = errno;
173                         DPRINTFX("ERROR: child process %d status 0x%x", pid, status);
174                         goto bad;
175                 } else
176                         phdl->status = PS_STOP;
177         }
178 bad:
179         if (error)
180                 proc_free(phdl);
181         else
182                 *pphdl = phdl;
183         return (error);
184 }
185
186 void
187 proc_free(struct proc_handle *phdl)
188 {
189         free(phdl);
190 }