Home | History | Annotate | Line # | Download | only in dist
proc_create.c revision 1.1.1.1
      1  1.1  christos /*-
      2  1.1  christos  * Copyright (c) 2008 John Birrell (jb (at) freebsd.org)
      3  1.1  christos  * All rights reserved.
      4  1.1  christos  *
      5  1.1  christos  * Redistribution and use in source and binary forms, with or without
      6  1.1  christos  * modification, are permitted provided that the following conditions
      7  1.1  christos  * are met:
      8  1.1  christos  * 1. Redistributions of source code must retain the above copyright
      9  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     10  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  christos  *    documentation and/or other materials provided with the distribution.
     13  1.1  christos  *
     14  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     15  1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     16  1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     17  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     18  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     19  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     20  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     21  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     22  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     23  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     24  1.1  christos  * SUCH DAMAGE.
     25  1.1  christos  *
     26  1.1  christos  * $FreeBSD: head/lib/libproc/proc_create.c 265255 2014-05-03 04:44:03Z markj $
     27  1.1  christos  */
     28  1.1  christos 
     29  1.1  christos #include <sys/types.h>
     30  1.1  christos #include <sys/sysctl.h>
     31  1.1  christos #include <sys/wait.h>
     32  1.1  christos 
     33  1.1  christos #include <err.h>
     34  1.1  christos #include <errno.h>
     35  1.1  christos #include <fcntl.h>
     36  1.1  christos #include <limits.h>
     37  1.1  christos #include <stdlib.h>
     38  1.1  christos #include <string.h>
     39  1.1  christos #include <unistd.h>
     40  1.1  christos 
     41  1.1  christos #include "_libproc.h"
     42  1.1  christos 
     43  1.1  christos static int	proc_init(pid_t, int, int, struct proc_handle *);
     44  1.1  christos 
     45  1.1  christos static int
     46  1.1  christos proc_init(pid_t pid, int flags, int status, struct proc_handle *phdl)
     47  1.1  christos {
     48  1.1  christos 	int mib[4], error;
     49  1.1  christos 	size_t len;
     50  1.1  christos 
     51  1.1  christos 	memset(phdl, 0, sizeof(*phdl));
     52  1.1  christos 	phdl->pid = pid;
     53  1.1  christos 	phdl->flags = flags;
     54  1.1  christos 	phdl->status = status;
     55  1.1  christos 
     56  1.1  christos 	mib[0] = CTL_KERN;
     57  1.1  christos 	mib[1] = KERN_PROC;
     58  1.1  christos 	mib[2] = KERN_PROC_PATHNAME;
     59  1.1  christos 	mib[3] = pid;
     60  1.1  christos 	len = sizeof(phdl->execname);
     61  1.1  christos 	if (sysctl(mib, 4, phdl->execname, &len, NULL, 0) != 0) {
     62  1.1  christos 		error = errno;
     63  1.1  christos 		DPRINTF("ERROR: cannot get pathname for child process %d", pid);
     64  1.1  christos 		return (error);
     65  1.1  christos 	}
     66  1.1  christos 	if (len == 0)
     67  1.1  christos 		phdl->execname[0] = '\0';
     68  1.1  christos 
     69  1.1  christos 	return (0);
     70  1.1  christos }
     71  1.1  christos 
     72  1.1  christos int
     73  1.1  christos proc_attach(pid_t pid, int flags, struct proc_handle **pphdl)
     74  1.1  christos {
     75  1.1  christos 	struct proc_handle *phdl;
     76  1.1  christos 	int error = 0;
     77  1.1  christos 	int status;
     78  1.1  christos 
     79  1.1  christos 	if (pid == 0 || pid == getpid())
     80  1.1  christos 		return (EINVAL);
     81  1.1  christos 
     82  1.1  christos 	/*
     83  1.1  christos 	 * Allocate memory for the process handle, a structure containing
     84  1.1  christos 	 * all things related to the process.
     85  1.1  christos 	 */
     86  1.1  christos 	if ((phdl = malloc(sizeof(struct proc_handle))) == NULL)
     87  1.1  christos 		return (ENOMEM);
     88  1.1  christos 
     89  1.1  christos 	elf_version(EV_CURRENT);
     90  1.1  christos 
     91  1.1  christos 	error = proc_init(pid, flags, PS_RUN, phdl);
     92  1.1  christos 	if (error != 0)
     93  1.1  christos 		goto out;
     94  1.1  christos 
     95  1.1  christos 	if (ptrace(PT_ATTACH, phdl->pid, 0, 0) != 0) {
     96  1.1  christos 		error = errno;
     97  1.1  christos 		DPRINTF("ERROR: cannot ptrace child process %d", pid);
     98  1.1  christos 		goto out;
     99  1.1  christos 	}
    100  1.1  christos 
    101  1.1  christos 	/* Wait for the child process to stop. */
    102  1.1  christos 	if (waitpid(pid, &status, WUNTRACED) == -1) {
    103  1.1  christos 		error = errno;
    104  1.1  christos 		DPRINTF("ERROR: child process %d didn't stop as expected", pid);
    105  1.1  christos 		goto out;
    106  1.1  christos 	}
    107  1.1  christos 
    108  1.1  christos 	/* Check for an unexpected status. */
    109  1.1  christos 	if (WIFSTOPPED(status) == 0)
    110  1.1  christos 		DPRINTFX("ERROR: child process %d status 0x%x", pid, status);
    111  1.1  christos 	else
    112  1.1  christos 		phdl->status = PS_STOP;
    113  1.1  christos 
    114  1.1  christos out:
    115  1.1  christos 	if (error)
    116  1.1  christos 		proc_free(phdl);
    117  1.1  christos 	else
    118  1.1  christos 		*pphdl = phdl;
    119  1.1  christos 	return (error);
    120  1.1  christos }
    121  1.1  christos 
    122  1.1  christos int
    123  1.1  christos proc_create(const char *file, char * const *argv, proc_child_func *pcf,
    124  1.1  christos     void *child_arg, struct proc_handle **pphdl)
    125  1.1  christos {
    126  1.1  christos 	struct proc_handle *phdl;
    127  1.1  christos 	int error = 0;
    128  1.1  christos 	int status;
    129  1.1  christos 	pid_t pid;
    130  1.1  christos 
    131  1.1  christos 	/*
    132  1.1  christos 	 * Allocate memory for the process handle, a structure containing
    133  1.1  christos 	 * all things related to the process.
    134  1.1  christos 	 */
    135  1.1  christos 	if ((phdl = malloc(sizeof(struct proc_handle))) == NULL)
    136  1.1  christos 		return (ENOMEM);
    137  1.1  christos 
    138  1.1  christos 	elf_version(EV_CURRENT);
    139  1.1  christos 
    140  1.1  christos 	/* Fork a new process. */
    141  1.1  christos 	if ((pid = vfork()) == -1)
    142  1.1  christos 		error = errno;
    143  1.1  christos 	else if (pid == 0) {
    144  1.1  christos 		/* The child expects to be traced. */
    145  1.1  christos 		if (ptrace(PT_TRACE_ME, 0, 0, 0) != 0)
    146  1.1  christos 			_exit(1);
    147  1.1  christos 
    148  1.1  christos 		if (pcf != NULL)
    149  1.1  christos 			(*pcf)(child_arg);
    150  1.1  christos 
    151  1.1  christos 		/* Execute the specified file: */
    152  1.1  christos 		execvp(file, argv);
    153  1.1  christos 
    154  1.1  christos 		/* Couldn't execute the file. */
    155  1.1  christos 		_exit(2);
    156  1.1  christos 	} else {
    157  1.1  christos 		/* The parent owns the process handle. */
    158  1.1  christos 		error = proc_init(pid, 0, PS_IDLE, phdl);
    159  1.1  christos 		if (error != 0)
    160  1.1  christos 			goto bad;
    161  1.1  christos 
    162  1.1  christos 		/* Wait for the child process to stop. */
    163  1.1  christos 		if (waitpid(pid, &status, WUNTRACED) == -1) {
    164  1.1  christos 			error = errno;
    165  1.1  christos 			DPRINTF("ERROR: child process %d didn't stop as expected", pid);
    166  1.1  christos 			goto bad;
    167  1.1  christos 		}
    168  1.1  christos 
    169  1.1  christos 		/* Check for an unexpected status. */
    170  1.1  christos 		if (WIFSTOPPED(status) == 0) {
    171  1.1  christos 			error = errno;
    172  1.1  christos 			DPRINTFX("ERROR: child process %d status 0x%x", pid, status);
    173  1.1  christos 			goto bad;
    174  1.1  christos 		} else
    175  1.1  christos 			phdl->status = PS_STOP;
    176  1.1  christos 	}
    177  1.1  christos bad:
    178  1.1  christos 	if (error)
    179  1.1  christos 		proc_free(phdl);
    180  1.1  christos 	else
    181  1.1  christos 		*pphdl = phdl;
    182  1.1  christos 	return (error);
    183  1.1  christos }
    184  1.1  christos 
    185  1.1  christos void
    186  1.1  christos proc_free(struct proc_handle *phdl)
    187  1.1  christos {
    188  1.1  christos 	free(phdl);
    189  1.1  christos }
    190