svn commit: r249676 - head/lib/libprocstat
Mikolaj Golub
trociny at FreeBSD.org
Sat Apr 20 08:02:45 UTC 2013
Author: trociny
Date: Sat Apr 20 08:02:43 2013
New Revision: 249676
URL: http://svnweb.freebsd.org/changeset/base/249676
Log:
Add procstat_getpathname function to retrieve a process executable.
MFC after: 1 month
Modified:
head/lib/libprocstat/Symbol.map
head/lib/libprocstat/libprocstat.3
head/lib/libprocstat/libprocstat.c
head/lib/libprocstat/libprocstat.h
Modified: head/lib/libprocstat/Symbol.map
==============================================================================
--- head/lib/libprocstat/Symbol.map Sat Apr 20 08:01:00 2013 (r249675)
+++ head/lib/libprocstat/Symbol.map Sat Apr 20 08:02:43 2013 (r249676)
@@ -20,6 +20,7 @@ FBSD_1.3 {
procstat_freevmmap;
procstat_get_shm_info;
procstat_getgroups;
+ procstat_getpathname;
procstat_getrlimit;
procstat_getumask;
procstat_getvmmap;
Modified: head/lib/libprocstat/libprocstat.3
==============================================================================
--- head/lib/libprocstat/libprocstat.3 Sat Apr 20 08:01:00 2013 (r249675)
+++ head/lib/libprocstat/libprocstat.3 Sat Apr 20 08:02:43 2013 (r249676)
@@ -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 al
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
Modified: head/lib/libprocstat/libprocstat.c
==============================================================================
--- head/lib/libprocstat/libprocstat.c Sat Apr 20 08:01:00 2013 (r249675)
+++ head/lib/libprocstat/libprocstat.c Sat Apr 20 08:02:43 2013 (r249676)
@@ -136,6 +136,10 @@ static int procstat_get_vnode_info_sysct
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 *proc
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);
+ }
+}
Modified: head/lib/libprocstat/libprocstat.h
==============================================================================
--- head/lib/libprocstat/libprocstat.h Sat Apr 20 08:01:00 2013 (r249675)
+++ head/lib/libprocstat/libprocstat.h Sat Apr 20 08:02:43 2013 (r249676)
@@ -169,10 +169,12 @@ int procstat_get_vnode_info(struct procs
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);
More information about the svn-src-head
mailing list