Add procstat_getpathname function to retrieve a process executable.

MFC after:	1 month
This commit is contained in:
Mikolaj Golub 2013-04-20 08:02:43 +00:00
parent 21b4f75a2c
commit 4cdf979641
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=249676
4 changed files with 90 additions and 2 deletions

View file

@ -20,6 +20,7 @@ FBSD_1.3 {
procstat_freevmmap;
procstat_get_shm_info;
procstat_getgroups;
procstat_getpathname;
procstat_getrlimit;
procstat_getumask;
procstat_getvmmap;

View file

@ -34,6 +34,7 @@
.Nm procstat_close ,
.Nm procstat_getfiles ,
.Nm procstat_getgroups ,
.Nm procstat_getpathname ,
.Nm procstat_getprocs ,
.Nm procstat_getumask ,
.Nm procstat_getvmmap ,
@ -128,6 +129,13 @@
.Fa "unsigned int *count"
.Fc
.Ft "int"
.Fo procstat_getpathname
.Fa "struct procstat *procstat"
.Fa "struct kinfo_proc *kp"
.Fa "char *pathname"
.Fa "size_t maxlen"
.Fc
.Ft "int"
.Fo procstat_getrlimit
.Fa "struct procstat *procstat"
.Fa "struct kinfo_proc *kp"
@ -268,6 +276,18 @@ The caller is responsible to free the allocated memory with a subsequent
function call.
.Pp
The
.Fn procstat_getpathname
function gets a pointer to the
.Vt procstat
structure, a pointer to
.Vt kinfo_proc
structure, and copies the path of the process executable to
.Fa pathname
buffer, limiting to
.Fa maxlen
characters.
.Pp
The
.Fn procstat_getrlimit
function gets a pointer to the
.Vt procstat

View file

@ -136,6 +136,10 @@ static int procstat_get_vnode_info_sysctl(struct filestat *fst,
static gid_t *procstat_getgroups_core(struct procstat_core *core,
unsigned int *count);
static gid_t *procstat_getgroups_sysctl(pid_t pid, unsigned int *count);
static int procstat_getpathname_core(struct procstat_core *core,
char *pathname, size_t maxlen);
static int procstat_getpathname_sysctl(pid_t pid, char *pathname,
size_t maxlen);
static int procstat_getrlimit_core(struct procstat_core *core, int which,
struct rlimit* rlimit);
static int procstat_getrlimit_sysctl(pid_t pid, int which,
@ -1780,3 +1784,64 @@ procstat_getrlimit(struct procstat *procstat, struct kinfo_proc *kp, int which,
return (-1);
}
}
static int
procstat_getpathname_sysctl(pid_t pid, char *pathname, size_t maxlen)
{
int error, name[4];
size_t len;
name[0] = CTL_KERN;
name[1] = KERN_PROC;
name[2] = KERN_PROC_PATHNAME;
name[3] = pid;
len = maxlen;
error = sysctl(name, 4, pathname, &len, NULL, 0);
if (error != 0 && errno != ESRCH)
warn("sysctl: kern.proc.pathname: %d", pid);
if (len == 0)
pathname[0] = '\0';
return (error);
}
static int
procstat_getpathname_core(struct procstat_core *core, char *pathname,
size_t maxlen)
{
struct kinfo_file *files;
int cnt, i, result;
files = kinfo_getfile_core(core, &cnt);
if (files == NULL)
return (-1);
result = -1;
for (i = 0; i < cnt; i++) {
if (files[i].kf_fd != KF_FD_TYPE_TEXT)
continue;
strncpy(pathname, files[i].kf_path, maxlen);
result = 0;
break;
}
free(files);
return (result);
}
int
procstat_getpathname(struct procstat *procstat, struct kinfo_proc *kp,
char *pathname, size_t maxlen)
{
switch(procstat->type) {
case PROCSTAT_KVM:
warnx("kvm method is not supported");
return (-1);
case PROCSTAT_SYSCTL:
return (procstat_getpathname_sysctl(kp->ki_pid, pathname,
maxlen));
case PROCSTAT_CORE:
return (procstat_getpathname_core(procstat->core, pathname,
maxlen));
default:
warnx("unknown access method: %d", procstat->type);
return (-1);
}
}

View file

@ -169,10 +169,12 @@ int procstat_get_vnode_info(struct procstat *procstat, struct filestat *fst,
struct vnstat *vn, char *errbuf);
gid_t *procstat_getgroups(struct procstat *procstat, struct kinfo_proc *kp,
unsigned int *count);
int procstat_getumask(struct procstat *procstat, struct kinfo_proc *kp,
unsigned short* umask);
int procstat_getpathname(struct procstat *procstat, struct kinfo_proc *kp,
char *pathname, size_t maxlen);
int procstat_getrlimit(struct procstat *procstat, struct kinfo_proc *kp,
int which, struct rlimit* rlimit);
int procstat_getumask(struct procstat *procstat, struct kinfo_proc *kp,
unsigned short* umask);
struct kinfo_vmentry *procstat_getvmmap(struct procstat *procstat,
struct kinfo_proc *kp, unsigned int *count);
struct procstat *procstat_open_core(const char *filename);