svn commit: r309592 - head/lib/libproc

Mark Johnston markj at FreeBSD.org
Tue Dec 6 04:14:21 UTC 2016


Author: markj
Date: Tue Dec  6 04:14:20 2016
New Revision: 309592
URL: https://svnweb.freebsd.org/changeset/base/309592

Log:
  libproc: Make proc_getpid() an accessor for struct proc_handle.
  
  This allows librtld_db to fetch the PID from a handle without calling into
  libproc. Together with r303531, this means that librtld_db no longer
  references symbols from libproc.

Modified:
  head/lib/libproc/_libproc.h
  head/lib/libproc/libproc.h
  head/lib/libproc/proc_create.c
  head/lib/libproc/proc_sym.c
  head/lib/libproc/proc_util.c

Modified: head/lib/libproc/_libproc.h
==============================================================================
--- head/lib/libproc/_libproc.h	Tue Dec  6 04:13:02 2016	(r309591)
+++ head/lib/libproc/_libproc.h	Tue Dec  6 04:14:20 2016	(r309592)
@@ -39,7 +39,7 @@
 struct procstat;
 
 struct proc_handle {
-	pid_t	pid;			/* Process ID. */
+	struct proc_handle_public public; /* Public fields. */
 	int	flags;			/* Process flags. */
 	int	status;			/* Process status (PS_*). */
 	int	wstat;			/* Process wait status. */

Modified: head/lib/libproc/libproc.h
==============================================================================
--- head/lib/libproc/libproc.h	Tue Dec  6 04:13:02 2016	(r309591)
+++ head/lib/libproc/libproc.h	Tue Dec  6 04:14:20 2016	(r309592)
@@ -116,6 +116,12 @@ typedef struct lwpstatus {
 #define	PR_MODEL_ILP32	1
 #define	PR_MODEL_LP64	2
 
+struct proc_handle_public {
+	pid_t		pid;
+};
+
+#define	proc_getpid(phdl)	(((struct proc_handle_public *)(phdl))->pid)
+
 /* Function prototype definitions. */
 __BEGIN_DECLS
 
@@ -140,7 +146,6 @@ struct ctf_file *proc_name2ctf(struct pr
 int	proc_setflags(struct proc_handle *, int);
 int	proc_state(struct proc_handle *);
 int	proc_getmodel(struct proc_handle *);
-pid_t	proc_getpid(struct proc_handle *);
 int	proc_wstatus(struct proc_handle *);
 int	proc_getwstat(struct proc_handle *);
 char *	proc_signame(int, char *, size_t);

Modified: head/lib/libproc/proc_create.c
==============================================================================
--- head/lib/libproc/proc_create.c	Tue Dec  6 04:13:02 2016	(r309591)
+++ head/lib/libproc/proc_create.c	Tue Dec  6 04:14:20 2016	(r309592)
@@ -79,7 +79,7 @@ proc_init(pid_t pid, int flags, int stat
 		goto out;
 
 	memset(phdl, 0, sizeof(*phdl));
-	phdl->pid = pid;
+	phdl->public.pid = pid;
 	phdl->flags = flags;
 	phdl->status = status;
 	phdl->procstat = procstat_open_sysctl();
@@ -140,7 +140,7 @@ proc_attach(pid_t pid, int flags, struct
 	if (error != 0)
 		goto out;
 
-	if (ptrace(PT_ATTACH, phdl->pid, 0, 0) != 0) {
+	if (ptrace(PT_ATTACH, proc_getpid(phdl), 0, 0) != 0) {
 		error = errno;
 		DPRINTF("ERROR: cannot ptrace child process %d", pid);
 		goto out;

Modified: head/lib/libproc/proc_sym.c
==============================================================================
--- head/lib/libproc/proc_sym.c	Tue Dec  6 04:13:02 2016	(r309591)
+++ head/lib/libproc/proc_sym.c	Tue Dec  6 04:14:20 2016	(r309592)
@@ -208,7 +208,7 @@ proc_addr2map(struct proc_handle *p, uin
 	 * it ourselves.
 	 */
 	if (p->nobjs == 0) {
-		if ((kves = kinfo_getvmmap(p->pid, &cnt)) == NULL)
+		if ((kves = kinfo_getvmmap(proc_getpid(p), &cnt)) == NULL)
 			return (NULL);
 		for (i = 0; i < (size_t)cnt; i++) {
 			kve = kves + i;

Modified: head/lib/libproc/proc_util.c
==============================================================================
--- head/lib/libproc/proc_util.c	Tue Dec  6 04:13:02 2016	(r309591)
+++ head/lib/libproc/proc_util.c	Tue Dec  6 04:14:20 2016	(r309592)
@@ -70,7 +70,8 @@ proc_continue(struct proc_handle *phdl)
 		pending = WSTOPSIG(phdl->wstat);
 	else
 		pending = 0;
-	if (ptrace(PT_CONTINUE, phdl->pid, (caddr_t)(uintptr_t)1, pending) != 0)
+	if (ptrace(PT_CONTINUE, proc_getpid(phdl), (caddr_t)(uintptr_t)1,
+	    pending) != 0)
 		return (-1);
 
 	phdl->status = PS_RUN;
@@ -82,20 +83,22 @@ int
 proc_detach(struct proc_handle *phdl, int reason)
 {
 	int status;
+	pid_t pid;
 
 	if (phdl == NULL)
 		return (EINVAL);
 	if (reason == PRELEASE_KILL) {
-		kill(phdl->pid, SIGKILL);
+		kill(proc_getpid(phdl), SIGKILL);
 		return (0);
 	}
-	if (ptrace(PT_DETACH, phdl->pid, 0, 0) != 0 && errno == ESRCH)
+	pid = proc_getpid(phdl);
+	if (ptrace(PT_DETACH, pid, 0, 0) != 0 && errno == ESRCH)
 		return (0);
 	if (errno == EBUSY) {
-		kill(phdl->pid, SIGSTOP);
-		waitpid(phdl->pid, &status, WUNTRACED);
-		ptrace(PT_DETACH, phdl->pid, 0, 0);
-		kill(phdl->pid, SIGCONT);
+		kill(pid, SIGSTOP);
+		waitpid(pid, &status, WUNTRACED);
+		ptrace(PT_DETACH, pid, 0, 0);
+		kill(pid, SIGCONT);
 		return (0);
 	}
 
@@ -134,16 +137,6 @@ proc_state(struct proc_handle *phdl)
 	return (phdl->status);
 }
 
-pid_t
-proc_getpid(struct proc_handle *phdl)
-{
-
-	if (phdl == NULL)
-		return (-1);
-
-	return (phdl->pid);
-}
-
 int
 proc_getmodel(struct proc_handle *phdl)
 {
@@ -161,7 +154,7 @@ proc_wstatus(struct proc_handle *phdl)
 
 	if (phdl == NULL)
 		return (-1);
-	if (waitpid(phdl->pid, &status, WUNTRACED) < 0) {
+	if (waitpid(proc_getpid(phdl), &status, WUNTRACED) < 0) {
 		if (errno != EINTR)
 			DPRINTF("waitpid");
 		return (-1);
@@ -206,7 +199,7 @@ proc_read(struct proc_handle *phdl, void
 	piod.piod_addr = (void *)buf;
 	piod.piod_offs = (void *)addr;
 
-	if (ptrace(PT_IO, phdl->pid, (caddr_t)&piod, 0) < 0)
+	if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0)
 		return (-1);
 	return (piod.piod_len);
 }
@@ -220,7 +213,7 @@ proc_getlwpstatus(struct proc_handle *ph
 
 	if (phdl == NULL)
 		return (NULL);
-	if (ptrace(PT_LWPINFO, phdl->pid, (caddr_t)&lwpinfo,
+	if (ptrace(PT_LWPINFO, proc_getpid(phdl), (caddr_t)&lwpinfo,
 	    sizeof(lwpinfo)) < 0)
 		return (NULL);
 	siginfo = &lwpinfo.pl_siginfo;


More information about the svn-src-head mailing list