summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJim Warner <james.warner@comcast.net>2022-01-04 00:00:00 +0100
committerCraig Small <csmall@dropbear.xyz>2022-01-07 19:19:15 +1100
commit2a7ec67ac806d91520f59c384684e2332774368b (patch)
treedb2bfbf94420d374fe8ac3cee2445c2ef8522de7
parentcbff1dd10645cb8c7fc55dda08b06fbafed05932 (diff)
downloadprocps-ng-2a7ec67ac806d91520f59c384684e2332774368b.tar.gz
library: add support for the 'LIBPROC_HIDE_KERNEL' var
This patch was prompted by Björn Fischer's merge #147 request referenced below. It has been generalized such that it now embraces both of those 'pids_fetch' types. [ and thanks to Björn for initiating this extension ] Reference(s): https://gitlab.com/procps-ng/procps/-/merge_requests/147 Prototyped-by: Björn Fischer <bf@CeBiTec.Uni-Bielefeld.DE> Signed-off-by: Jim Warner <james.warner@comcast.net>
-rw-r--r--proc/readproc.c15
-rw-r--r--proc/readproc.h1
2 files changed, 15 insertions, 1 deletions
diff --git a/proc/readproc.c b/proc/readproc.c
index eb3504d..4420364 100644
--- a/proc/readproc.c
+++ b/proc/readproc.c
@@ -1225,7 +1225,10 @@ static proc_t *simple_readproc(PROCTAB *restrict const PT, proc_t *restrict cons
if (flags & PROC_FILLAUTOGRP) // value the 2 autogroup fields
autogroup_fill(path, p);
- if (rc == 0) return p;
+ // openproc() ensured that a ppid will be present when needed ...
+ if (rc == 0)
+ return (PT->hide_kernel && (p->ppid == 2 || p->tid == 2)) ? NULL : p;
+
errno = ENOMEM;
next_proc:
return NULL;
@@ -1514,10 +1517,13 @@ PROCTAB *openproc(unsigned flags, ...) {
va_list ap;
struct stat sbuf;
static __thread int did_stat;
+ static __thread int hide_kernel = -1;
PROCTAB *PT = calloc(1, sizeof(PROCTAB));
if (!PT)
return NULL;
+ if (hide_kernel < 0)
+ hide_kernel = (NULL != getenv("LIBPROC_HIDE_KERNEL"));
if (!did_stat){
task_dir_missing = stat("/proc/self/task", &sbuf);
did_stat = 1;
@@ -1547,6 +1553,13 @@ PROCTAB *openproc(unsigned flags, ...) {
}
va_end(ap);
+ if (hide_kernel > 0) {
+ PT->hide_kernel = 1;
+ // we'll need the ppid, ensure it's obtained via cheapest means ...
+ if (!(PT->flags & (PROC_FILLSTAT | PROC_FILLSTATUS)))
+ PT->flags |= PROC_FILLSTAT;
+ }
+
if (!src_buffer
&& !(src_buffer = malloc(MAX_BUFSZ))) {
closedir(PT->procfs);
diff --git a/proc/readproc.h b/proc/readproc.h
index 55453c9..87bb53c 100644
--- a/proc/readproc.h
+++ b/proc/readproc.h
@@ -203,6 +203,7 @@ typedef struct PROCTAB {
uid_t *uids; // uids of procs
int nuid; // cannot really sentinel-terminate unsigned short[]
int i; // generic
+ int hide_kernel; // getenv LIBPROC_HIDE_KERNEL was set
unsigned flags;
unsigned u; // generic
void * vp; // generic