proc_create.c revision 179185
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: head/lib/libproc/proc_create.c 179185 2008-05-22 02:09:21Z jb $
27 */
28
29#include "_libproc.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;
43	struct kevent kev;
44	int error = 0;
45	int status;
46
47	if (pid == 0 || pphdl == NULL)
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
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)
72		error = errno;
73
74	/* 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);
77
78	/* Check for an unexpected status. */
79	else if (WIFSTOPPED(status) == 0)
80		err(4, "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;
88
89	return (error);
90}
91
92int
93proc_create(const char *file, char * const *argv, struct proc_handle **pphdl)
94{
95	struct proc_handle *phdl;
96	struct kevent kev;
97	int error = 0;
98	int status;
99	pid_t pid;
100
101	/*
102	 * Allocate memory for the process handle, a structure containing
103	 * all things related to the process.
104	 */
105	if ((phdl = malloc(sizeof(struct proc_handle))) == NULL)
106		return (ENOMEM);
107
108	/* Fork a new process. */
109	if ((pid = fork()) == -1)
110		error = errno;
111	else if (pid == 0) {
112		/* The child expects to be traced. */
113		if (ptrace(PT_TRACE_ME, 0, 0, 0) != 0)
114			_exit(1);
115
116		/* Execute the specified file: */
117		execvp(file, argv);
118
119		/* Couldn't execute the file. */
120		_exit(2);
121	} else {
122		/* The parent owns the process handle. */
123		memset(phdl, 0, sizeof(struct proc_handle));
124		phdl->pid = pid;
125		phdl->status = PS_IDLE;
126
127		EV_SET(&kev, pid, EVFILT_PROC, EV_ADD | EV_ONESHOT, NOTE_EXIT,
128		    0, NULL);
129
130		if ((phdl->kq = kqueue()) == -1)
131                	err(1, "ERROR: cannot create kernel evet queue");
132
133        	if (kevent(phdl->kq, &kev, 1, NULL, 0, NULL) < 0)
134                	err(2, "ERROR: cannot monitor child process %d", pid);
135
136		/* Wait for the child process to stop. */
137		if (waitpid(pid, &status, WUNTRACED) == -1)
138                	err(3, "ERROR: child process %d didn't stop as expected", pid);
139
140		/* Check for an unexpected status. */
141		if (WIFSTOPPED(status) == 0)
142                	err(4, "ERROR: child process %d status 0x%x", pid, status);
143		else
144			phdl->status = PS_STOP;
145	}
146
147	if (error)
148		proc_free(phdl);
149	else
150		*pphdl = phdl;
151
152	return (error);
153}
154
155void
156proc_free(struct proc_handle *phdl)
157{
158	free(phdl);
159}
160