Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 13 Jan 2008 03:29:47 GMT
From:      John Birrell <jb@FreeBSD.org>
To:        Perforce Change Reviews <perforce@freebsd.org>
Subject:   PERFORCE change 133154 for review
Message-ID:  <200801130329.m0D3Tlus037323@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=133154

Change 133154 by jb@jb_freebsd1 on 2008/01/13 03:29:46

	Implement proc_attach to allocate a handle like proc_create does.
	
	Add clear, get and set flags.
	
	Move proc_free() into the same file as create/attach.

Affected files ...

.. //depot/projects/dtrace/src/lib/libproc/Makefile#3 edit
.. //depot/projects/dtrace/src/lib/libproc/_libproc.h#3 edit
.. //depot/projects/dtrace/src/lib/libproc/libproc.h#2 edit
.. //depot/projects/dtrace/src/lib/libproc/proc_create.c#3 edit
.. //depot/projects/dtrace/src/lib/libproc/proc_free.c#3 delete
.. //depot/projects/dtrace/src/lib/libproc/proc_util.c#2 edit

Differences ...

==== //depot/projects/dtrace/src/lib/libproc/Makefile#3 (text+ko) ====

@@ -4,7 +4,6 @@
 
 SRCS=				\
 	proc_create.c		\
-	proc_free.c		\
 	proc_util.c
 
 INCS=	libproc.h

==== //depot/projects/dtrace/src/lib/libproc/_libproc.h#3 (text+ko) ====

@@ -37,6 +37,7 @@
 struct proc_handle {
 	pid_t	pid;			/* Process ID. */
 	int	kq;			/* Kernel event queue ID. */
+	int	flags;			/* Process flags. */
 	int	status;			/* Process status (PS_*). */
 };
 

==== //depot/projects/dtrace/src/lib/libproc/libproc.h#2 (text+ko) ====

@@ -42,10 +42,13 @@
 /* Function prototype definitions. */
 __BEGIN_DECLS
 
-int	proc_attach(struct proc_handle *);
+int	proc_attach(pid_t pid, int flags, struct proc_handle **pphdl);
 int	proc_continue(struct proc_handle *);
+int	proc_clearflags(struct proc_handle *, int);
+int	proc_create(const char *, char * const *, struct proc_handle **);
 int	proc_detach(struct proc_handle *);
-int	proc_create(const char *, char * const *, struct proc_handle **);
+int	proc_getflags(struct proc_handle *);
+int	proc_setflags(struct proc_handle *, int);
 int	proc_state(struct proc_handle *);
 int	proc_wait(struct proc_handle *);
 pid_t	proc_getpid(struct proc_handle *);

==== //depot/projects/dtrace/src/lib/libproc/proc_create.c#3 (text+ko) ====

@@ -37,11 +37,63 @@
 #include <sys/wait.h>
 
 int
+proc_attach(pid_t pid, int flags, struct proc_handle **pphdl)
+{
+	struct proc_handle *phdl;
+	struct kevent kev;
+	int error = 0;
+	int status;
+
+	if (pid == 0 || pphdl == NULL)
+		return (EINVAL);
+
+	/*
+	 * Allocate memory for the process handle, a structure containing
+	 * all things related to the process.
+	 */
+	if ((phdl = malloc(sizeof(struct proc_handle))) == NULL)
+		return (ENOMEM);
+
+	memset(phdl, 0, sizeof(struct proc_handle));
+	phdl->pid = pid;
+	phdl->flags = flags;
+	phdl->status = PS_RUN;
+
+	EV_SET(&kev, pid, EVFILT_PROC, EV_ADD | EV_ONESHOT, NOTE_EXIT,
+	    0, NULL);
+
+	if ((phdl->kq = kqueue()) == -1)
+		err(1, "ERROR: cannot create kernel evet queue");
+
+	if (kevent(phdl->kq, &kev, 1, NULL, 0, NULL) < 0)
+		err(2, "ERROR: cannot monitor child process %d", pid);
+
+	if (ptrace(PT_ATTACH, phdl->pid, NULL, 0) != 0)
+		error = errno;
+
+	/* Wait for the child process to stop. */
+	else if (waitpid(pid, &status, WUNTRACED) == -1)
+		err(3, "ERROR: child process %d didn't stop as expected", pid);
+
+	/* Check for an unexpected status. */
+	else if (WIFSTOPPED(status) == 0)
+		err(4, "ERROR: child process %d status 0x%x", pid, status);
+	else
+		phdl->status = PS_STOP;
+
+	if (error)
+		proc_free(phdl);
+	else
+		*pphdl = phdl;
+
+	return (error);
+}
+
+int
 proc_create(const char *file, char * const *argv, struct proc_handle **pphdl)
 {
 	struct proc_handle *phdl;
 	struct kevent kev;
-	char errbuf[_POSIX2_LINE_MAX];
 	int error = 0;
 	int status;
 	pid_t pid;
@@ -99,3 +151,9 @@
 
 	return (error);
 }
+
+void
+proc_free(struct proc_handle *phdl)
+{
+	free(phdl);
+}

==== //depot/projects/dtrace/src/lib/libproc/proc_util.c#2 (text+ko) ====

@@ -34,13 +34,12 @@
 #include <stdio.h>
 
 int
-proc_attach(struct proc_handle *phdl)
+proc_clearflags(struct proc_handle *phdl, int mask)
 {
 	if (phdl == NULL)
 		return (EINVAL);
 
-	if (ptrace(PT_ATTACH, phdl->pid, NULL, 0) != 0)
-		return (errno);
+	phdl->flags &= ~mask;
 
 	return (0);
 }
@@ -72,6 +71,26 @@
 }
 
 int
+proc_getflags(struct proc_handle *phdl)
+{
+	if (phdl == NULL)
+		return (-1);
+
+	return(phdl->flags);
+}
+
+int
+proc_setflags(struct proc_handle *phdl, int mask)
+{
+	if (phdl == NULL)
+		return (EINVAL);
+
+	phdl->flags |= mask;
+
+	return (0);
+}
+
+int
 proc_state(struct proc_handle *phdl)
 {
 	if (phdl == NULL)



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200801130329.m0D3Tlus037323>