proc_create.c revision 184697
1268896Sbapt/*- 2268896Sbapt * Copyright (c) 2008 John Birrell (jb@freebsd.org) 3268896Sbapt * All rights reserved. 4268896Sbapt * 5268896Sbapt * Redistribution and use in source and binary forms, with or without 6268896Sbapt * modification, are permitted provided that the following conditions 7268896Sbapt * are met: 8268896Sbapt * 1. Redistributions of source code must retain the above copyright 9268896Sbapt * notice, this list of conditions and the following disclaimer. 10268896Sbapt * 2. Redistributions in binary form must reproduce the above copyright 11268896Sbapt * notice, this list of conditions and the following disclaimer in the 12268896Sbapt * documentation and/or other materials provided with the distribution. 13268896Sbapt * 14268896Sbapt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15268896Sbapt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16268896Sbapt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17268896Sbapt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18268896Sbapt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19268896Sbapt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20268896Sbapt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21268896Sbapt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22268896Sbapt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23268896Sbapt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24268896Sbapt * SUCH DAMAGE. 25268896Sbapt * 26268896Sbapt * $FreeBSD: head/lib/libproc/proc_create.c 184697 2008-11-05 19:35:43Z rodrigc $ 27268896Sbapt */ 28268896Sbapt 29268896Sbapt#include "_libproc.h" 30268896Sbapt#include <err.h> 31268896Sbapt#include <errno.h> 32268896Sbapt#include <fcntl.h> 33268896Sbapt#include <limits.h> 34268896Sbapt#include <stdlib.h> 35268896Sbapt#include <string.h> 36268896Sbapt#include <unistd.h> 37268896Sbapt#include <sys/wait.h> 38268896Sbapt 39268896Sbaptint 40268896Sbaptproc_attach(pid_t pid, int flags, struct proc_handle **pphdl) 41268896Sbapt{ 42268896Sbapt struct proc_handle *phdl; 43268896Sbapt struct kevent kev; 44268896Sbapt int error = 0; 45268896Sbapt int status; 46268896Sbapt 47268896Sbapt if (pid == 0 || pphdl == NULL) 48268896Sbapt return (EINVAL); 49268896Sbapt 50268896Sbapt /* 51268896Sbapt * Allocate memory for the process handle, a structure containing 52268896Sbapt * all things related to the process. 53268896Sbapt */ 54268896Sbapt if ((phdl = malloc(sizeof(struct proc_handle))) == NULL) 55268896Sbapt return (ENOMEM); 56268896Sbapt 57268896Sbapt memset(phdl, 0, sizeof(struct proc_handle)); 58268896Sbapt phdl->pid = pid; 59268896Sbapt phdl->flags = flags; 60268896Sbapt phdl->status = PS_RUN; 61268896Sbapt 62268896Sbapt EV_SET(&kev, pid, EVFILT_PROC, EV_ADD | EV_ONESHOT, NOTE_EXIT, 63268896Sbapt 0, NULL); 64268896Sbapt 65268896Sbapt if ((phdl->kq = kqueue()) == -1) 66268896Sbapt err(1, "ERROR: cannot create kernel evet queue"); 67268896Sbapt 68268896Sbapt if (kevent(phdl->kq, &kev, 1, NULL, 0, NULL) < 0) 69268896Sbapt err(2, "ERROR: cannot monitor child process %d", pid); 70268896Sbapt 71268896Sbapt if (ptrace(PT_ATTACH, phdl->pid, NULL, 0) != 0) 72268896Sbapt error = errno; 73268896Sbapt 74268896Sbapt /* Wait for the child process to stop. */ 75268896Sbapt else if (waitpid(pid, &status, WUNTRACED) == -1) 76268896Sbapt err(3, "ERROR: child process %d didn't stop as expected", pid); 77268896Sbapt 78268896Sbapt /* Check for an unexpected status. */ 79268896Sbapt else if (WIFSTOPPED(status) == 0) 80268896Sbapt err(4, "ERROR: child process %d status 0x%x", pid, status); 81268896Sbapt else 82268896Sbapt phdl->status = PS_STOP; 83 84 if (error) 85 proc_free(phdl); 86 else 87 *pphdl = phdl; 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; 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 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 117 if (pcf != NULL) 118 (*pcf)(child_arg); 119 120 /* Execute the specified file: */ 121 execvp(file, argv); 122 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. */ 141 if (waitpid(pid, &status, WUNTRACED) == -1) 142 err(3, "ERROR: child process %d didn't stop as expected", pid); 143 144 /* Check for an unexpected status. */ 145 if (WIFSTOPPED(status) == 0) 146 err(4, "ERROR: child process %d status 0x%x", pid, status); 147 else 148 phdl->status = PS_STOP; 149 } 150 151 if (error) 152 proc_free(phdl); 153 else 154 *pphdl = phdl; 155 156 return (error); 157} 158 159void 160proc_free(struct proc_handle *phdl) 161{ 162 free(phdl); 163} 164