proc_create.c (184697) | proc_create.c (210688) |
---|---|
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 unchanged lines hidden (view full) --- 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 * | 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 unchanged lines hidden (view full) --- 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: head/lib/libproc/proc_create.c 184697 2008-11-05 19:35:43Z rodrigc $ | 26 * $FreeBSD: head/lib/libproc/proc_create.c 210688 2010-07-31 16:10:20Z rpaulo $ |
27 */ 28 29#include "_libproc.h" | 27 */ 28 29#include "_libproc.h" |
30#include <stdio.h> |
|
30#include <err.h> 31#include <errno.h> 32#include <fcntl.h> 33#include <limits.h> 34#include <stdlib.h> 35#include <string.h> 36#include <unistd.h> 37#include <sys/wait.h> 38 39int 40proc_attach(pid_t pid, int flags, struct proc_handle **pphdl) 41{ 42 struct proc_handle *phdl; | 31#include <err.h> 32#include <errno.h> 33#include <fcntl.h> 34#include <limits.h> 35#include <stdlib.h> 36#include <string.h> 37#include <unistd.h> 38#include <sys/wait.h> 39 40int 41proc_attach(pid_t pid, int flags, struct proc_handle **pphdl) 42{ 43 struct proc_handle *phdl; |
43 struct kevent kev; | |
44 int error = 0; 45 int status; 46 | 44 int error = 0; 45 int status; 46 |
47 if (pid == 0 || pphdl == NULL) | 47 if (pid == 0 || pid == getpid()) |
48 return (EINVAL); 49 50 /* 51 * Allocate memory for the process handle, a structure containing 52 * all things related to the process. 53 */ 54 if ((phdl = malloc(sizeof(struct proc_handle))) == NULL) 55 return (ENOMEM); 56 57 memset(phdl, 0, sizeof(struct proc_handle)); 58 phdl->pid = pid; 59 phdl->flags = flags; 60 phdl->status = PS_RUN; | 48 return (EINVAL); 49 50 /* 51 * Allocate memory for the process handle, a structure containing 52 * all things related to the process. 53 */ 54 if ((phdl = malloc(sizeof(struct proc_handle))) == NULL) 55 return (ENOMEM); 56 57 memset(phdl, 0, sizeof(struct proc_handle)); 58 phdl->pid = pid; 59 phdl->flags = flags; 60 phdl->status = PS_RUN; |
61 elf_version(EV_CURRENT); |
|
61 | 62 |
62 EV_SET(&kev, pid, EVFILT_PROC, EV_ADD | EV_ONESHOT, NOTE_EXIT, 63 0, NULL); 64 65 if ((phdl->kq = kqueue()) == -1) 66 err(1, "ERROR: cannot create kernel evet queue"); 67 68 if (kevent(phdl->kq, &kev, 1, NULL, 0, NULL) < 0) 69 err(2, "ERROR: cannot monitor child process %d", pid); 70 71 if (ptrace(PT_ATTACH, phdl->pid, NULL, 0) != 0) | 63 if (ptrace(PT_ATTACH, phdl->pid, 0, 0) != 0) { |
72 error = errno; | 64 error = errno; |
65 DPRINTF("ERROR: cannot ptrace child process %d", pid); 66 goto out; 67 } |
|
73 74 /* Wait for the child process to stop. */ | 68 69 /* Wait for the child process to stop. */ |
75 else if (waitpid(pid, &status, WUNTRACED) == -1) 76 err(3, "ERROR: child process %d didn't stop as expected", pid); | 70 if (waitpid(pid, &status, WUNTRACED) == -1) { 71 error = errno; 72 DPRINTF("ERROR: child process %d didn't stop as expected", pid); 73 goto out; 74 } |
77 78 /* Check for an unexpected status. */ | 75 76 /* Check for an unexpected status. */ |
79 else if (WIFSTOPPED(status) == 0) 80 err(4, "ERROR: child process %d status 0x%x", pid, status); | 77 if (WIFSTOPPED(status) == 0) 78 DPRINTF("ERROR: child process %d status 0x%x", pid, status); |
81 else 82 phdl->status = PS_STOP; 83 84 if (error) 85 proc_free(phdl); 86 else 87 *pphdl = phdl; | 79 else 80 phdl->status = PS_STOP; 81 82 if (error) 83 proc_free(phdl); 84 else 85 *pphdl = phdl; |
86out: |
|
88 89 return (error); 90} 91 92int 93proc_create(const char *file, char * const *argv, proc_child_func *pcf, 94 void *child_arg, struct proc_handle **pphdl) 95{ 96 struct proc_handle *phdl; | 87 88 return (error); 89} 90 91int 92proc_create(const char *file, char * const *argv, proc_child_func *pcf, 93 void *child_arg, struct proc_handle **pphdl) 94{ 95 struct proc_handle *phdl; |
97 struct kevent kev; | |
98 int error = 0; 99 int status; 100 pid_t pid; 101 102 /* 103 * Allocate memory for the process handle, a structure containing 104 * all things related to the process. 105 */ 106 if ((phdl = malloc(sizeof(struct proc_handle))) == NULL) 107 return (ENOMEM); 108 | 96 int error = 0; 97 int status; 98 pid_t pid; 99 100 /* 101 * Allocate memory for the process handle, a structure containing 102 * all things related to the process. 103 */ 104 if ((phdl = malloc(sizeof(struct proc_handle))) == NULL) 105 return (ENOMEM); 106 |
107 elf_version(EV_CURRENT); 108 |
|
109 /* Fork a new process. */ 110 if ((pid = vfork()) == -1) 111 error = errno; 112 else if (pid == 0) { 113 /* The child expects to be traced. */ 114 if (ptrace(PT_TRACE_ME, 0, 0, 0) != 0) 115 _exit(1); 116 --- 6 unchanged lines hidden (view full) --- 123 /* Couldn't execute the file. */ 124 _exit(2); 125 } else { 126 /* The parent owns the process handle. */ 127 memset(phdl, 0, sizeof(struct proc_handle)); 128 phdl->pid = pid; 129 phdl->status = PS_IDLE; 130 | 109 /* Fork a new process. */ 110 if ((pid = vfork()) == -1) 111 error = errno; 112 else if (pid == 0) { 113 /* The child expects to be traced. */ 114 if (ptrace(PT_TRACE_ME, 0, 0, 0) != 0) 115 _exit(1); 116 --- 6 unchanged lines hidden (view full) --- 123 /* Couldn't execute the file. */ 124 _exit(2); 125 } else { 126 /* The parent owns the process handle. */ 127 memset(phdl, 0, sizeof(struct proc_handle)); 128 phdl->pid = pid; 129 phdl->status = PS_IDLE; 130 |
131 EV_SET(&kev, pid, EVFILT_PROC, EV_ADD | EV_ONESHOT, NOTE_EXIT, 132 0, NULL); 133 134 if ((phdl->kq = kqueue()) == -1) 135 err(1, "ERROR: cannot create kernel evet queue"); 136 137 if (kevent(phdl->kq, &kev, 1, NULL, 0, NULL) < 0) 138 err(2, "ERROR: cannot monitor child process %d", pid); 139 | |
140 /* Wait for the child process to stop. */ | 131 /* Wait for the child process to stop. */ |
141 if (waitpid(pid, &status, WUNTRACED) == -1) 142 err(3, "ERROR: child process %d didn't stop as expected", pid); | 132 if (waitpid(pid, &status, WUNTRACED) == -1) { 133 error = errno; 134 DPRINTF("ERROR: child process %d didn't stop as expected", pid); 135 goto bad; 136 } |
143 144 /* Check for an unexpected status. */ | 137 138 /* Check for an unexpected status. */ |
145 if (WIFSTOPPED(status) == 0) 146 err(4, "ERROR: child process %d status 0x%x", pid, status); 147 else | 139 if (WIFSTOPPED(status) == 0) { 140 error = errno; 141 DPRINTF("ERROR: child process %d status 0x%x", pid, status); 142 goto bad; 143 } else |
148 phdl->status = PS_STOP; 149 } | 144 phdl->status = PS_STOP; 145 } |
150 | 146bad: |
151 if (error) 152 proc_free(phdl); 153 else 154 *pphdl = phdl; | 147 if (error) 148 proc_free(phdl); 149 else 150 *pphdl = phdl; |
155 | |
156 return (error); 157} 158 159void 160proc_free(struct proc_handle *phdl) 161{ 162 free(phdl); 163} | 151 return (error); 152} 153 154void 155proc_free(struct proc_handle *phdl) 156{ 157 free(phdl); 158} |