Add a sysctl that returns the full path of a process' text file.

This information is needed by things like `gdb -p' and Sun's javac,
and previously it could only be obtained via procfs
This commit is contained in:
das 2005-04-18 02:10:37 +00:00
parent 757f59e8d6
commit 5aec008257
3 changed files with 55 additions and 3 deletions

View File

@ -436,14 +436,14 @@ The version of
with which the system
attempts to comply.
.It Li KERN_PROC
Return the entire process table, or a subset of it.
An array of pairs of
Return selected information about specific running processes.
.Pp
For the following names, an array of pairs of
.Va struct proc
followed by corresponding
.Va struct eproc
structures is returned,
whose size depends on the current number of such objects in the system.
The third and fourth level names are as follows:
.Bl -column "Third level nameXXXXXX" "Fourth level is:XXXXXX" -offset indent
.It "Third level name Fourth level is:"
.It "KERN_PROC_ALL None"
@ -459,9 +459,15 @@ array is returned in a flattened form, i.e., zero-terminated arguments
follow each other.
The total size of array is returned.
It is also possible for a process to set its own process title this way.
If the third level name is KERN_PROC_PATHNAME, the path of the
process' text file is stored.
For KERN_PROC_PATHNAME, a process ID of
.Li -1
implies the current process.
.Bl -column "Third level nameXXXXXX" "Fourth level is:XXXXXX" -offset indent
.It Sy "Third level name Fourth level is:"
.It "KERN_PROC_ARGS A process ID"
.It "KERN_PROC_PATHNAME A process ID"
.El
.It Li KERN_PROF
Return profiling information about the kernel.

View File

@ -53,6 +53,7 @@ __FBSDID("$FreeBSD$");
#include <sys/sx.h>
#include <sys/user.h>
#include <sys/jail.h>
#include <sys/vnode.h>
#ifdef KTRACE
#include <sys/uio.h>
#include <sys/ktrace.h>
@ -1183,6 +1184,47 @@ sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
return (0);
}
/*
* This sysctl allows a process to retrieve the path of the executable for
* itself or another process.
*/
static int
sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
{
pid_t *pidp = (pid_t *)arg1;
unsigned int arglen = arg2;
struct proc *p;
struct vnode *vp;
char *retbuf, *freebuf;
int error;
if (arglen != 1)
return (EINVAL);
if (*pidp == -1) { /* -1 means this process */
p = req->td->td_proc;
} else {
p = pfind(*pidp);
if (p == NULL)
return (ESRCH);
if ((error = p_cansee(curthread, p)) != 0) {
PROC_UNLOCK(p);
return (error);
}
}
vp = p->p_textvp;
vref(vp);
if (*pidp != -1)
PROC_UNLOCK(p);
error = vn_fullpath(req->td, vp, &retbuf, &freebuf);
vrele(vp);
if (error)
return (error);
error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
free(freebuf, M_TEMP);
return (error);
}
static int
sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
{
@ -1245,6 +1287,9 @@ static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
CTLFLAG_RW | CTLFLAG_ANYBODY,
sysctl_kern_proc_args, "Process argument list");
static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD,
sysctl_kern_proc_pathname, "Process executable path");
static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD,
sysctl_kern_proc_sv_name, "Process syscall vector name (ABI type)");

View File

@ -428,6 +428,7 @@ TAILQ_HEAD(sysctl_ctx_list, sysctl_ctx_entry);
#define KERN_PROC_SV_NAME 9 /* get syscall vector name */
#define KERN_PROC_RGID 10 /* by real group id */
#define KERN_PROC_GID 11 /* by effective group id */
#define KERN_PROC_PATHNAME 12 /* path to executable */
#define KERN_PROC_INC_THREAD 0x10 /*
* modifier for pid, pgrp, tty,
* uid, ruid, gid, rgid and proc