procps/proc/readproc.h

316 lines
15 KiB
C
Raw Normal View History

2002-02-02 04:17:29 +05:30
#ifndef PROCPS_PROC_READPROC_H
#define PROCPS_PROC_READPROC_H
2003-09-28 22:53:29 +05:30
2003-09-28 08:15:05 +05:30
// New Interface to Process Table -- PROCTAB Stream (a la Directory streams)
// Copyright 1996 Charles L. Blake.
// Copyright 1998 Michael K. Johnson
// Copyright 1998-2003 Albert Cahalan
// May be distributed under the terms of the
// GNU Library General Public License, a copy of which is provided
// in the file COPYING
2002-02-02 04:17:29 +05:30
2002-12-03 14:37:59 +05:30
#include "procps.h"
2004-07-21 05:01:12 +05:30
#include "pwcache.h"
2002-02-02 04:17:29 +05:30
#define SIGNAL_STRING
//#define QUICK_THREADS /* copy (vs. read) some thread info from parent proc_t */
2002-02-02 04:17:29 +05:30
2002-12-09 12:30:07 +05:30
EXTERN_C_BEGIN
// ld cutime, cstime, priority, nice, timeout, alarm, rss,
2003-09-28 08:15:05 +05:30
// c state,
// d ppid, pgrp, session, tty, tpgid,
// s signal, blocked, sigignore, sigcatch,
// lu flags, min_flt, cmin_flt, maj_flt, cmaj_flt, utime, stime,
// lu rss_rlim, start_code, end_code, start_stack, kstk_esp, kstk_eip,
2004-04-13 09:26:52 +05:30
// lu start_time, vsize, wchan,
2002-02-02 04:17:29 +05:30
2003-09-20 13:59:55 +05:30
// This is to help document a transition from pid to tgid/tid caused
// by the introduction of thread support. It is used in cases where
// neither tgid nor tid seemed correct. (in other words, FIXME)
#define XXXID tid
enum ns_type {
IPCNS = 0,
MNTNS,
NETNS,
PIDNS,
USERNS,
UTSNS,
NUM_NS // total namespaces (fencepost)
};
extern const char *get_ns_name(int id);
extern int get_ns_id(const char *name);
2003-09-28 08:15:05 +05:30
// Basic data structure which holds all information we can get about a process.
// (unless otherwise specified, fields are read from /proc/#/stat)
//
// Most of it comes from task_struct in linux/sched.h
//
2002-05-30 12:38:15 +05:30
typedef struct proc_t {
2002-10-13 13:53:50 +05:30
// 1st 16 bytes
int
2003-09-28 08:15:05 +05:30
tid, // (special) task id, the POSIX thread ID (see also: tgid)
ppid; // stat,status pid of parent process
unsigned long // next 2 fields are NOT filled in by readproc
maj_delta, // stat (special) major page faults since last update
min_delta; // stat (special) minor page faults since last update
unsigned
pcpu; // stat (special) %CPU usage (is not filled in by readproc!!!)
2002-10-13 13:53:50 +05:30
char
2003-09-28 08:15:05 +05:30
state, // stat,status single-char code for process state (S=sleeping)
#ifdef QUICK_THREADS
pad_1, // n/a padding (psst, also used if multi-threaded)
#else
pad_1, // n/a padding
#endif
2003-09-28 08:15:05 +05:30
pad_2, // n/a padding
pad_3; // n/a padding
2002-10-13 13:53:50 +05:30
// 2nd 16 bytes
unsigned long long
2003-09-28 08:15:05 +05:30
utime, // stat user-mode CPU time accumulated by process
stime, // stat kernel-mode CPU time accumulated by process
2002-10-13 13:53:50 +05:30
// and so on...
2003-09-28 08:15:05 +05:30
cutime, // stat cumulative utime of process and reaped children
cstime, // stat cumulative stime of process and reaped children
start_time; // stat start time of process -- seconds since system boot
2002-02-02 04:17:29 +05:30
#ifdef SIGNAL_STRING
char
2003-09-28 08:15:05 +05:30
// Linux 2.1.7x and up have 64 signals. Allow 64, plus '\0' and padding.
signal[18], // status mask of pending signals, per-task for readtask() but per-proc for readproc()
2003-09-28 08:15:05 +05:30
blocked[18], // status mask of blocked signals
sigignore[18], // status mask of ignored signals
sigcatch[18], // status mask of caught signals
_sigpnd[18]; // status mask of PER TASK pending signals
2002-02-02 04:17:29 +05:30
#else
long long
2003-09-28 08:15:05 +05:30
// Linux 2.1.7x and up have 64 signals.
signal, // status mask of pending signals, per-task for readtask() but per-proc for readproc()
2003-09-28 08:15:05 +05:30
blocked, // status mask of blocked signals
sigignore, // status mask of ignored signals
sigcatch, // status mask of caught signals
_sigpnd; // status mask of PER TASK pending signals
2002-02-02 04:17:29 +05:30
#endif
unsigned KLONG
start_code, // stat address of beginning of code segment
end_code, // stat address of end of code segment
start_stack, // stat address of the bottom of stack for the process
kstk_esp, // stat kernel stack pointer
kstk_eip, // stat kernel instruction pointer
wchan; // stat (special) address of kernel wait channel proc is sleeping in
2002-05-28 09:48:55 +05:30
long
2003-09-28 08:15:05 +05:30
priority, // stat kernel scheduling priority
nice, // stat standard unix nice level of process
rss, // stat identical to 'resident'
alarm, // stat ?
2003-09-28 08:15:05 +05:30
// the next 7 members come from /proc/#/statm
size, // statm total virtual memory (as # pages)
resident, // statm resident non-swapped memory (as # pages)
share, // statm shared (mmap'd) memory (as # pages)
trs, // statm text (exe) resident set (as # pages)
lrs, // statm library resident set (always 0 w/ 2.6)
drs, // statm data+stack resident set (as # pages)
dt; // statm dirty pages (always 0 w/ 2.6)
2002-02-02 04:17:29 +05:30
unsigned long
vm_size, // status equals 'size' (as kb)
vm_lock, // status locked pages (as kb)
vm_rss, // status equals 'rss' and/or 'resident' (as kb)
vm_rss_anon, // status the 'anonymous' portion of vm_rss (as kb)
vm_rss_file, // status the 'file-backed' portion of vm_rss (as kb)
vm_rss_shared, // status the 'shared' portion of vm_rss (as kb)
vm_data, // status data only size (as kb)
vm_stack, // status stack only size (as kb)
vm_swap, // status based on linux-2.6.34 "swap ents" (as kb)
vm_exe, // status equals 'trs' (as kb)
vm_lib, // status total, not just used, library pages (as kb)
2003-09-28 08:15:05 +05:30
rtprio, // stat real-time priority
sched, // stat scheduling class
vsize, // stat number of pages of virtual memory ...
rss_rlim, // stat resident set size limit?
flags, // stat kernel flags for the process
min_flt, // stat number of minor page faults since process start
maj_flt, // stat number of major page faults since process start
cmin_flt, // stat cumulative min_flt of process and child processes
2004-04-13 09:26:52 +05:30
cmaj_flt; // stat cumulative maj_flt of process and child processes
2002-02-02 04:17:29 +05:30
char
enhanced libproc cgroup/cmdline support, exploited by top Library Changes . added PROC_EDITCMDLCVT flag . added an internal (static) fill_cmdline_cvt function: - reads and "escapes" /proc/#/cmdline - returns result as a single string in a single vector - callers are guaranteed a cmdline (no more NULL) . added vectorize_this_str function, exploited by fill_cgroup_cvt, fill_cmdline_cvt . generalized read_cmdline function as read_unvectored, now exploited by fill_cgroup_cvt, fill_cmdline_cvt, read_cmdline ( cgroup and cmdline no longer need be converted to string ) ( vectors before being transformed to final representation ) . fixed bug regarding skipped group numbers (when enabled) . escape_str made responsible for all single byte translation with distinction between control chars + other unprintable . added escaped_copy function for already escaped strings . reorganized parts of proc_t to restore formatting standards ( displacement changes shouldn't matter with new version # ) . former ZAP_SUSEONLY #define now OOMEM_ENABLE . added to library.map: escaped_copy; read_cmdline Top Program Changes . exploited the new PROC_EDITCMDLCVT provision . eliminated now obsolete #include "proc/escape.h" . changed the P_WCH display format if no kernel symbol table . fixed very old bug in lflgs for out-of-view sort fields . former ZAP_SUSEONLY #define now OOMEM_ENABLE Ps Program Changes . exploited the new PROC_EDITCMDLCVT provision . exploited the new escaped_copy function . consolidated pr_args and pr_comm into pr_argcom Signed-off-by: Jan Görig <jgorig@redhat.com>
2011-05-18 14:03:44 +05:30
**environ, // (special) environment string vector (/proc/#/environ)
**cmdline, // (special) command line string vector (/proc/#/cmdline)
**cgroup, // (special) cgroup string vector (/proc/#/cgroup)
*cgname, // (special) name portion of above (if possible)
*supgid, // status supplementary gids as comma delimited str
*supgrp; // supp grp names as comma delimited str, derived from supgid
2002-02-02 04:17:29 +05:30
char
2003-09-28 08:15:05 +05:30
// Be compatible: Digital allows 16 and NT allows 14 ???
2004-07-21 05:01:12 +05:30
euser[P_G_SZ], // stat(),status effective user name
ruser[P_G_SZ], // status real user name
suser[P_G_SZ], // status saved user name
fuser[P_G_SZ], // status filesystem user name
rgroup[P_G_SZ], // status real group name
egroup[P_G_SZ], // status effective group name
sgroup[P_G_SZ], // status saved group name
fgroup[P_G_SZ], // status filesystem group name
cmd[64]; // stat,status basename of executable file in call to exec(2)
2003-09-18 07:48:43 +05:30
struct proc_t
2003-09-28 08:15:05 +05:30
*ring, // n/a thread group ring
*next; // n/a various library uses
2002-02-02 04:17:29 +05:30
int
2003-09-28 08:15:05 +05:30
pgrp, // stat process group id
session, // stat session id
2003-10-16 09:00:41 +05:30
nlwp, // stat,status number of threads, or 0 if no clue
tgid, // (special) thread group ID, the POSIX PID (see also: tid)
2021-10-26 05:08:10 +05:30
tty; // stat full device number of controlling terminal
uid_t euid; gid_t egid; // stat(),status effective
uid_t ruid; gid_t rgid; // status real
uid_t suid; gid_t sgid; // status saved
uid_t fuid; gid_t fgid; // status fs (used for file access only)
int
2003-09-28 08:15:05 +05:30
tpgid, // stat terminal process group id
exit_signal, // stat might not be SIGCHLD
processor; // stat current (or most recent?) CPU
enhanced libproc cgroup/cmdline support, exploited by top Library Changes . added PROC_EDITCMDLCVT flag . added an internal (static) fill_cmdline_cvt function: - reads and "escapes" /proc/#/cmdline - returns result as a single string in a single vector - callers are guaranteed a cmdline (no more NULL) . added vectorize_this_str function, exploited by fill_cgroup_cvt, fill_cmdline_cvt . generalized read_cmdline function as read_unvectored, now exploited by fill_cgroup_cvt, fill_cmdline_cvt, read_cmdline ( cgroup and cmdline no longer need be converted to string ) ( vectors before being transformed to final representation ) . fixed bug regarding skipped group numbers (when enabled) . escape_str made responsible for all single byte translation with distinction between control chars + other unprintable . added escaped_copy function for already escaped strings . reorganized parts of proc_t to restore formatting standards ( displacement changes shouldn't matter with new version # ) . former ZAP_SUSEONLY #define now OOMEM_ENABLE . added to library.map: escaped_copy; read_cmdline Top Program Changes . exploited the new PROC_EDITCMDLCVT provision . eliminated now obsolete #include "proc/escape.h" . changed the P_WCH display format if no kernel symbol table . fixed very old bug in lflgs for out-of-view sort fields . former ZAP_SUSEONLY #define now OOMEM_ENABLE Ps Program Changes . exploited the new PROC_EDITCMDLCVT provision . exploited the new escaped_copy function . consolidated pr_args and pr_comm into pr_argcom Signed-off-by: Jan Görig <jgorig@redhat.com>
2011-05-18 14:03:44 +05:30
int
oom_score, // oom_score (badness for OOM killer)
oom_adj; // oom_adj (adjustment to OOM score)
long
ns[NUM_NS]; // (ns subdir) inode number of namespaces
char
*sd_mach, // n/a systemd vm/container name
*sd_ouid, // n/a systemd session owner uid
*sd_seat, // n/a systemd login session seat
*sd_sess, // n/a systemd login session id
*sd_slice, // n/a systemd slice unit
*sd_unit, // n/a systemd system unit id
*sd_uunit; // n/a systemd user unit id
const char
*lxcname; // n/a lxc container name
2002-02-02 04:17:29 +05:30
} proc_t;
2003-09-28 08:15:05 +05:30
// PROCTAB: data structure holding the persistent information readproc needs
// from openproc(). The setup is intentionally similar to the dirent interface
// and other system table interfaces (utmp+wtmp come to mind).
2002-02-02 04:17:29 +05:30
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
2003-09-20 13:59:55 +05:30
#define PROCPATHLEN 64 // must hold /proc/2000222000/task/2000222000/cmdline
2002-05-30 12:38:15 +05:30
typedef struct PROCTAB {
2002-02-02 04:17:29 +05:30
DIR* procfs;
2003-10-20 05:07:47 +05:30
// char deBug0[64];
2003-09-20 13:59:55 +05:30
DIR* taskdir; // for threads
2003-10-20 05:07:47 +05:30
// char deBug1[64];
pid_t taskdir_user; // for threads
2003-09-20 13:59:55 +05:30
int did_fake; // used when taskdir is missing
int(*finder)(struct PROCTAB *__restrict const, proc_t *__restrict const);
proc_t*(*reader)(struct PROCTAB *__restrict const, proc_t *__restrict const);
int(*taskfinder)(struct PROCTAB *__restrict const, const proc_t *__restrict const, proc_t *__restrict const, char *__restrict const);
proc_t*(*taskreader)(struct PROCTAB *__restrict const, const proc_t *__restrict const, proc_t *__restrict const, char *__restrict const);
2003-09-28 08:15:05 +05:30
pid_t* pids; // pids of the procs
uid_t* uids; // uids of procs
int nuid; // cannot really sentinel-terminate unsigned short[]
2003-09-18 03:28:32 +05:30
int i; // generic
unsigned flags;
2003-09-18 03:28:32 +05:30
unsigned u; // generic
void * vp; // generic
2003-09-20 13:59:55 +05:30
char path[PROCPATHLEN]; // must hold /proc/2000222000/task/2000222000/cmdline
unsigned pathlen; // length of string in the above (w/o '\0')
2002-02-02 04:17:29 +05:30
} PROCTAB;
// Initialize a PROCTAB structure holding needed call-to-call persistent data
2002-05-30 12:38:15 +05:30
extern PROCTAB* openproc(int flags, ... /* pid_t*|uid_t*|dev_t*|char* [, int n] */ );
2002-02-02 04:17:29 +05:30
typedef struct proc_data_t { // valued by: (else zero)
proc_t **tab; // readproctab2, readproctab3
proc_t **proc; // readproctab2
proc_t **task; // * readproctab2
int n; // readproctab2, readproctab3
int nproc; // readproctab2
int ntask; // * readproctab2
} proc_data_t; // * when PROC_LOOSE_TASKS set
2003-10-16 09:00:41 +05:30
extern proc_data_t *readproctab2(int(*want_proc)(proc_t *buf), int(*want_task)(proc_t *buf), PROCTAB *__restrict const PT);
extern proc_data_t *readproctab3(int(*want_task)(proc_t *buf), PROCTAB *__restrict const PT);
2002-02-02 04:17:29 +05:30
2003-09-28 08:15:05 +05:30
// Convenient wrapper around openproc and readproc to slurp in the whole process
// table subset satisfying the constraints of flags and the optional PID list.
// Free allocated memory with exit(). Access via tab[N]->member. The pointer
// list is NULL terminated.
2002-05-30 12:38:15 +05:30
extern proc_t** readproctab(int flags, ... /* same as openproc */ );
2002-02-02 04:17:29 +05:30
// Clean-up open files, etc from the openproc()
2002-05-30 12:38:15 +05:30
extern void closeproc(PROCTAB* PT);
2002-02-02 04:17:29 +05:30
// Retrieve the next process or task matching the criteria set by the openproc().
//
// Note: When NULL is used as the readproc 'p', readtask 't' or readeither 'x'
// parameter, the library will allocate the necessary proc_t storage.
//
// Alternatively, you may provide your own reuseable buffer address
// in which case that buffer *MUST* be initialized to zero one time
// only before first use. Thereafter, the library will manage such
// a passed proc_t, freeing any additional acquired memory associated
// with the previous process or thread.
extern proc_t* readproc(PROCTAB *__restrict const PT, proc_t *__restrict p);
extern proc_t* readtask(PROCTAB *__restrict const PT, const proc_t *__restrict const p, proc_t *__restrict t);
extern proc_t* readeither(PROCTAB *__restrict const PT, proc_t *__restrict x);
2002-02-02 04:17:29 +05:30
2002-12-03 14:37:59 +05:30
// warning: interface may change
extern int read_cmdline(char *__restrict const dst, unsigned sz, unsigned pid);
2002-12-03 14:37:59 +05:30
2002-05-30 12:38:15 +05:30
extern void look_up_our_self(proc_t *p);
2002-02-02 04:17:29 +05:30
// Deallocate space allocated by readproc
2002-05-30 12:38:15 +05:30
extern void freeproc(proc_t* p);
2002-02-02 04:17:29 +05:30
// Fill out a proc_t for a single task
2004-01-27 01:31:56 +05:30
extern proc_t * get_proc_stats(pid_t pid, proc_t *p);
2003-09-28 08:15:05 +05:30
// openproc/readproctab:
//
// Return PROCTAB* / *proc_t[] or NULL on error ((probably) "/proc" cannot be
// opened.) By default readproc will consider all processes as valid to parse
// and return, but not actually fill in the cmdline, environ, and /proc/#/statm
// derived memory fields.
//
// `flags' (a bitwise-or of PROC_* below) modifies the default behavior. The
// "fill" options will cause more of the proc_t to be filled in. The "filter"
// options all use the second argument as the pointer to a list of objects:
// process status', process id's, user id's. The third
// argument is the length of the list (currently only used for lists of user
// id's since uid_t supports no convenient termination sentinel.)
#define PROC_FILLMEM 0x0001 // read statm
#define PROC_FILLCOM 0x0002 // alloc and fill in `cmdline'
#define PROC_FILLENV 0x0004 // alloc and fill in `environ'
#define PROC_FILLUSR 0x0008 // resolve user id number -> user name
#define PROC_FILLGRP 0x0010 // resolve group id number -> group name
#define PROC_FILLSTATUS 0x0020 // read status
#define PROC_FILLSTAT 0x0040 // read stat
#define PROC_FILLARG 0x0100 // alloc and fill in `cmdline'
#define PROC_FILLCGROUP 0x0200 // alloc and fill in `cgroup`
#define PROC_FILLSUPGRP 0x0400 // resolve supplementary group id -> group name
#define PROC_FILLOOM 0x0800 // fill in proc_t oom_score and oom_adj
#define PROC_FILLNS 0x8000 // fill in proc_t namespace information
#define PROC_FILLSYSTEMD 0x80000 // fill in proc_t systemd information
#define PROC_FILL_LXC 0x800000 // fill in proc_t lxcname, if possible
#define PROC_LOOSE_TASKS 0x2000 // treat threads as if they were processes
2003-09-28 08:15:05 +05:30
// consider only processes with one of the passed:
#define PROC_PID 0x1000 // process id numbers ( 0 terminated)
#define PROC_UID 0x4000 // user id numbers ( length needed )
2002-02-02 04:17:29 +05:30
#define PROC_EDITCGRPCVT 0x10000 // edit `cgroup' as single vector
enhanced libproc cgroup/cmdline support, exploited by top Library Changes . added PROC_EDITCMDLCVT flag . added an internal (static) fill_cmdline_cvt function: - reads and "escapes" /proc/#/cmdline - returns result as a single string in a single vector - callers are guaranteed a cmdline (no more NULL) . added vectorize_this_str function, exploited by fill_cgroup_cvt, fill_cmdline_cvt . generalized read_cmdline function as read_unvectored, now exploited by fill_cgroup_cvt, fill_cmdline_cvt, read_cmdline ( cgroup and cmdline no longer need be converted to string ) ( vectors before being transformed to final representation ) . fixed bug regarding skipped group numbers (when enabled) . escape_str made responsible for all single byte translation with distinction between control chars + other unprintable . added escaped_copy function for already escaped strings . reorganized parts of proc_t to restore formatting standards ( displacement changes shouldn't matter with new version # ) . former ZAP_SUSEONLY #define now OOMEM_ENABLE . added to library.map: escaped_copy; read_cmdline Top Program Changes . exploited the new PROC_EDITCMDLCVT provision . eliminated now obsolete #include "proc/escape.h" . changed the P_WCH display format if no kernel symbol table . fixed very old bug in lflgs for out-of-view sort fields . former ZAP_SUSEONLY #define now OOMEM_ENABLE Ps Program Changes . exploited the new PROC_EDITCMDLCVT provision . exploited the new escaped_copy function . consolidated pr_args and pr_comm into pr_argcom Signed-off-by: Jan Görig <jgorig@redhat.com>
2011-05-18 14:03:44 +05:30
#define PROC_EDITCMDLCVT 0x20000 // edit `cmdline' as single vector
#define PROC_EDITENVRCVT 0x40000 // edit `environ' as single vector
2002-11-25 15:52:41 +05:30
// it helps to give app code a few spare bits
#define PROC_SPARE_1 0x01000000
#define PROC_SPARE_2 0x02000000
#define PROC_SPARE_3 0x04000000
#define PROC_SPARE_4 0x08000000
2002-11-25 15:52:41 +05:30
2002-12-09 12:30:07 +05:30
EXTERN_C_END
2002-02-02 04:17:29 +05:30
#endif