1 #ifndef PROCPS_PROC_READPROC_H
2 #define PROCPS_PROC_READPROC_H
3 /*
4  * New Interface to Process Table -- PROCTAB Stream (a la Directory streams)
5  * Copyright 1996 Charles L. Blake.
6  * Copyright 1998 Michael K. Johnson
7  * Copyright 1998-2002 Albert Cahalan
8  * May be distributed under the terms of the
9  * GNU Library General Public License, a copy of which is provided
10  * in the file COPYING
11  */
12 
13 #include "procps.h"
14 
15 #define SIGNAL_STRING
16 
17 #ifdef FLASK_LINUX
18 #include <fs_secure.h>
19 #endif
20 
21 EXTERN_C_BEGIN
22 
23 /*
24  ld	cutime, cstime, priority, nice, timeout, it_real_value, rss,
25  c	state,
26  d	ppid, pgrp, session, tty, tpgid,
27  s	signal, blocked, sigignore, sigcatch,
28  lu	flags, min_flt, cmin_flt, maj_flt, cmaj_flt, utime, stime,
29  lu	rss_rlim, start_code, end_code, start_stack, kstk_esp, kstk_eip,
30  lu	start_time, vsize, wchan, nswap, cnswap,
31 */
32 
33 /* Basic data structure which holds all information we can get about a process.
34  * (unless otherwise specified, fields are read from /proc/#/stat)
35  *
36  * Most of it comes from task_struct in linux/sched.h
37  */
38 typedef struct proc_t {
39 // 1st 16 bytes
40     int
41         pid,		/* process id */
42     	ppid;		/* pid of parent process */
43     unsigned
44         pcpu;           /* %CPU usage (is not filled in by readproc!!!) */
45     char
46     	state,		/* single-char code for process state (S=sleeping) */
47     	pad_1,		/* padding */
48     	pad_2,		/* padding */
49     	pad_3;		/* padding */
50 // 2nd 16 bytes
51     unsigned long long
52 	utime,		/* user-mode CPU time accumulated by process */
53 	stime,		/* kernel-mode CPU time accumulated by process */
54 // and so on...
55 	cutime,		/* cumulative utime of process and reaped children */
56 	cstime,		/* cumulative stime of process and reaped children */
57 	start_time;	/* start time of process -- seconds since 1-1-70 */
58 #ifdef SIGNAL_STRING
59     char
60 	/* Linux 2.1.7x and up have 64 signals. Allow 64, plus '\0' and padding. */
61 	signal[18],	/* mask of pending signals */
62 	blocked[18],	/* mask of blocked signals */
63 	sigignore[18],	/* mask of ignored signals */
64 	sigcatch[18];	/* mask of caught  signals */
65 #else
66     long long
67 	/* Linux 2.1.7x and up have 64 signals. */
68 	signal,		/* mask of pending signals */
69 	blocked,	/* mask of blocked signals */
70 	sigignore,	/* mask of ignored signals */
71 	sigcatch;	/* mask of caught  signals */
72 #endif
73     long
74 	priority,	/* kernel scheduling priority */
75 	timeout,	/* ? */
76 	nice,		/* standard unix nice level of process */
77 	rss,		/* resident set size from /proc/#/stat (pages) */
78 	it_real_value,	/* ? */
79     /* the next 7 members come from /proc/#/statm */
80 	size,		/* total # of pages of memory */
81 	resident,	/* number of resident set (non-swapped) pages (4k) */
82 	share,		/* number of pages of shared (mmap'd) memory */
83 	trs,		/* text resident set size */
84 	lrs,		/* shared-lib resident set size */
85 	drs,		/* data resident set size */
86 	dt;		/* dirty pages */
87     unsigned long
88 	vm_size,        /* same as vsize in kb */
89 	vm_lock,        /* locked pages in kb */
90 	vm_rss,         /* same as rss in kb */
91 	vm_data,        /* data size */
92 	vm_stack,       /* stack size */
93 	vm_exe,         /* executable size */
94 	vm_lib,         /* library size (all pages, not just used ones) */
95 	rtprio,		/* real-time priority */
96 	sched,		/* scheduling class */
97 	vsize,		/* number of pages of virtual memory ... */
98 	rss_rlim,	/* resident set size limit? */
99 	flags,		/* kernel flags for the process */
100 	min_flt,	/* number of minor page faults since process start */
101 	maj_flt,	/* number of major page faults since process start */
102 	cmin_flt,	/* cumulative min_flt of process and child processes */
103 	cmaj_flt,	/* cumulative maj_flt of process and child processes */
104 	nswap,		/* ? */
105 	cnswap,		/* cumulative nswap ? */
106 	start_code,	/* address of beginning of code segment */
107 	end_code,	/* address of end of code segment */
108 	start_stack,	/* address of the bottom of stack for the process */
109 	kstk_esp,	/* kernel stack pointer */
110 	kstk_eip,	/* kernel instruction pointer */
111 	wchan;		/* address of kernel wait channel proc is sleeping in */
112     char
113 	**environ,	/* environment string vector (/proc/#/environ) */
114 	**cmdline;	/* command line string vector (/proc/#/cmdline) */
115     char
116 	/* Be compatible: Digital allows 16 and NT allows 14 ??? */
117     	ruser[16],	/* real user name */
118     	euser[16],	/* effective user name */
119     	suser[16],	/* saved user name */
120     	fuser[16],	/* filesystem user name */
121     	rgroup[16],	/* real group name */
122     	egroup[16],	/* effective group name */
123     	sgroup[16],	/* saved group name */
124     	fgroup[16],	/* filesystem group name */
125     	cmd[16];	/* basename of executable file in call to exec(2) */
126     int
127         ruid, rgid,     /* real      */
128         euid, egid,     /* effective */
129         suid, sgid,     /* saved     */
130         fuid, fgid,     /* fs (used for file access only) */
131 	pgrp,		/* process group id */
132 	session,	/* session id */
133 	tty,		/* full device number of controlling terminal */
134 	tpgid,		/* terminal process group id */
135 	exit_signal,	/* might not be SIGCHLD */
136 	processor;      /* current (or most recent?) CPU */
137 #ifdef FLASK_LINUX
138 	security_id_t secsid;
139 #endif
140 } proc_t;
141 
142 /* PROCTAB: data structure holding the persistent information readproc needs
143  * from openproc().  The setup is intentionally similar to the dirent interface
144  * and other system table interfaces (utmp+wtmp come to mind).
145  */
146 #include <sys/types.h>
147 #include <dirent.h>
148 #include <unistd.h>
149 typedef struct PROCTAB {
150     DIR*	procfs;
151     int		flags;
152     pid_t*	pids;	/* pids of the procs */
153     uid_t*	uids;	/* uids of procs */
154     int		nuid;	/* cannot really sentinel-terminate unsigned short[] */
155 #ifdef FLASK_LINUX
156     security_id_t* sids; /* SIDs of the procs */
157 #endif
158 } PROCTAB;
159 
160 /* initialize a PROCTAB structure holding needed call-to-call persistent data
161  */
162 extern PROCTAB* openproc(int flags, ... /* pid_t*|uid_t*|dev_t*|char* [, int n] */ );
163 
164 
165 /* Convenient wrapper around openproc and readproc to slurp in the whole process
166  * table subset satisfying the constraints of flags and the optional PID list.
167  * Free allocated memory with freeproctab().  Access via tab[N]->member.  The
168  * pointer list is NULL terminated.
169  */
170 extern proc_t** readproctab(int flags, ... /* same as openproc */ );
171 
172 /* clean-up open files, etc from the openproc()
173  */
174 extern void closeproc(PROCTAB* PT);
175 
176 /* retrieve the next process matching the criteria set by the openproc()
177  */
178 extern proc_t* readproc(PROCTAB* PT, proc_t* return_buf);
179 extern proc_t* ps_readproc(PROCTAB* PT, proc_t* return_buf);
180 
181 // warning: interface may change
182 extern int read_cmdline(char *restrict const dst, unsigned sz, unsigned pid);
183 
184 extern void look_up_our_self(proc_t *p);
185 
186 /* deallocate space allocated by readproc
187  */
188 extern void freeproc(proc_t* p);
189 
190 /* openproc/readproctab:
191  *
192  * Return PROCTAB* / *proc_t[] or NULL on error ((probably) "/proc" cannot be
193  * opened.)  By default readproc will consider all processes as valid to parse
194  * and return, but not actually fill in the cmdline, environ, and /proc/#/statm
195  * derived memory fields.
196  *
197  * `flags' (a bitwise-or of PROC_* below) modifies the default behavior.  The
198  * "fill" options will cause more of the proc_t to be filled in.  The "filter"
199  * options all use the second argument as the pointer to a list of objects:
200  * process status', process id's, user id's.  The third
201  * argument is the length of the list (currently only used for lists of user
202  * id's since uid_t supports no convenient termination sentinel.)
203  */
204 #define PROC_FILLMEM    0x0001 /* read statm */
205 #define PROC_FILLCOM    0x0002 /* alloc and fill in `cmdline' */
206 #define PROC_FILLENV    0x0004 /* alloc and fill in `environ' */
207 #define PROC_FILLUSR    0x0008 /* resolve user id number -> user name */
208 #define PROC_FILLGRP    0x0010 /* resolve group id number -> group name */
209 #define PROC_FILLSTATUS 0x0020 /* read status -- currently unconditional */
210 #define PROC_FILLSTAT   0x0040 /* read stat -- currently unconditional */
211 #define PROC_FILLWCHAN  0x0080 /* look up WCHAN name */
212 #define PROC_FILLARG    0x0100 /* alloc and fill in `cmdline' */
213 
214 #define PROC_FILLBUG    0x0fff /* No idea what we need */
215 #define PROC_FILLANY    0x0000 /* either stat or status will do */
216 
217 /* Obsolete, consider only processes with one of the passed: */
218 #define PROC_PID     0x1000  /* process id numbers ( 0   terminated) */
219 #define PROC_UID     0x4000  /* user id numbers    ( length needed ) */
220 
221 // it helps to give app code a few spare bits
222 #define PROC_SPARE_1 0x01000000
223 #define PROC_SPARE_2 0x02000000
224 #define PROC_SPARE_3 0x04000000
225 #define PROC_SPARE_4 0x08000000
226 
227 EXTERN_C_END
228 #endif
229