Updated with Tizen:Base source codes
[external/procps.git] / proc / readproc.h
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-2003 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 #include "pwcache.h"
15
16 #define SIGNAL_STRING
17
18 EXTERN_C_BEGIN
19
20 // ld   cutime, cstime, priority, nice, timeout, alarm, rss,
21 // c    state,
22 // d    ppid, pgrp, session, tty, tpgid,
23 // s    signal, blocked, sigignore, sigcatch,
24 // lu   flags, min_flt, cmin_flt, maj_flt, cmaj_flt, utime, stime,
25 // lu   rss_rlim, start_code, end_code, start_stack, kstk_esp, kstk_eip,
26 // lu   start_time, vsize, wchan,
27
28 // This is to help document a transition from pid to tgid/tid caused
29 // by the introduction of thread support. It is used in cases where
30 // neither tgid nor tid seemed correct. (in other words, FIXME)
31 #define XXXID tid
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         tid,            // (special)       task id, the POSIX thread ID (see also: tgid)
42         ppid;           // stat,status     pid of parent process
43     unsigned
44         pcpu;           // stat (special)  %CPU usage (is not filled in by readproc!!!)
45     char
46         state,          // stat,status     single-char code for process state (S=sleeping)
47         pad_1,          // n/a             padding
48         pad_2,          // n/a             padding
49         pad_3;          // n/a             padding
50 // 2nd 16 bytes
51     unsigned long long
52         utime,          // stat            user-mode CPU time accumulated by process
53         stime,          // stat            kernel-mode CPU time accumulated by process
54 // and so on...
55         cutime,         // stat            cumulative utime of process and reaped children
56         cstime,         // stat            cumulative stime of process and reaped children
57         start_time;     // stat            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],     // status          mask of pending signals, per-task for readtask() but per-proc for readproc()
62         blocked[18],    // status          mask of blocked signals
63         sigignore[18],  // status          mask of ignored signals
64         sigcatch[18],   // status          mask of caught  signals
65         _sigpnd[18];    // status          mask of PER TASK pending signals
66 #else
67     long long
68         // Linux 2.1.7x and up have 64 signals.
69         signal,         // status          mask of pending signals, per-task for readtask() but per-proc for readproc()
70         blocked,        // status          mask of blocked signals
71         sigignore,      // status          mask of ignored signals
72         sigcatch,       // status          mask of caught  signals
73         _sigpnd;        // status          mask of PER TASK pending signals
74 #endif
75     unsigned KLONG
76         start_code,     // stat            address of beginning of code segment
77         end_code,       // stat            address of end of code segment
78         start_stack,    // stat            address of the bottom of stack for the process
79         kstk_esp,       // stat            kernel stack pointer
80         kstk_eip,       // stat            kernel instruction pointer
81         wchan;          // stat (special)  address of kernel wait channel proc is sleeping in
82     long
83         priority,       // stat            kernel scheduling priority
84         nice,           // stat            standard unix nice level of process
85         rss,            // stat            resident set size from /proc/#/stat (pages)
86         alarm,          // stat            ?
87     // the next 7 members come from /proc/#/statm
88         size,           // statm           total # of pages of memory
89         resident,       // statm           number of resident set (non-swapped) pages (4k)
90         share,          // statm           number of pages of shared (mmap'd) memory
91         trs,            // statm           text resident set size
92         lrs,            // statm           shared-lib resident set size
93         drs,            // statm           data resident set size
94         dt;             // statm           dirty pages
95     unsigned long
96         vm_size,        // status          same as vsize in kb
97         vm_lock,        // status          locked pages in kb
98         vm_rss,         // status          same as rss in kb
99         vm_data,        // status          data size
100         vm_stack,       // status          stack size
101         vm_exe,         // status          executable size
102         vm_lib,         // status          library size (all pages, not just used ones)
103         rtprio,         // stat            real-time priority
104         sched,          // stat            scheduling class
105         vsize,          // stat            number of pages of virtual memory ...
106         rss_rlim,       // stat            resident set size limit?
107         flags,          // stat            kernel flags for the process
108         min_flt,        // stat            number of minor page faults since process start
109         maj_flt,        // stat            number of major page faults since process start
110         cmin_flt,       // stat            cumulative min_flt of process and child processes
111         cmaj_flt;       // stat            cumulative maj_flt of process and child processes
112     char
113         **environ,      // (special)       environment string vector (/proc/#/environ)
114         **cmdline;      // (special)       command line string vector (/proc/#/cmdline)
115     char
116         // Be compatible: Digital allows 16 and NT allows 14 ???
117         euser[P_G_SZ],  // stat(),status   effective user name
118         ruser[P_G_SZ],  // status          real user name
119         suser[P_G_SZ],  // status          saved user name
120         fuser[P_G_SZ],  // status          filesystem user name
121         rgroup[P_G_SZ], // status          real group name
122         egroup[P_G_SZ], // status          effective group name
123         sgroup[P_G_SZ], // status          saved group name
124         fgroup[P_G_SZ], // status          filesystem group name
125         cmd[16];        // stat,status     basename of executable file in call to exec(2)
126     struct proc_t
127         *ring,          // n/a             thread group ring
128         *next;          // n/a             various library uses
129     int
130         pgrp,           // stat            process group id
131         session,        // stat            session id
132         nlwp,           // stat,status     number of threads, or 0 if no clue
133         tgid,           // (special)       task group ID, the POSIX PID (see also: tid)
134         tty,            // stat            full device number of controlling terminal
135         euid, egid,     // stat(),status   effective
136         ruid, rgid,     // status          real
137         suid, sgid,     // status          saved
138         fuid, fgid,     // status          fs (used for file access only)
139         tpgid,          // stat            terminal process group id
140         exit_signal,    // stat            might not be SIGCHLD
141         processor;      // stat            current (or most recent?) CPU
142 } proc_t;
143
144 // PROCTAB: data structure holding the persistent information readproc needs
145 // from openproc().  The setup is intentionally similar to the dirent interface
146 // and other system table interfaces (utmp+wtmp come to mind).
147
148 #include <sys/types.h>
149 #include <dirent.h>
150 #include <unistd.h>
151
152 #define PROCPATHLEN 64  // must hold /proc/2000222000/task/2000222000/cmdline
153
154 typedef struct PROCTAB {
155     DIR*        procfs;
156 //    char deBug0[64];
157     DIR*        taskdir;  // for threads
158 //    char deBug1[64];
159     pid_t       taskdir_user;  // for threads
160     int         did_fake; // used when taskdir is missing
161     int(*finder)(struct PROCTAB *restrict const, proc_t *restrict const);
162     proc_t*(*reader)(struct PROCTAB *restrict const, proc_t *restrict const);
163     int(*taskfinder)(struct PROCTAB *restrict const, const proc_t *restrict const, proc_t *restrict const, char *restrict const);
164     proc_t*(*taskreader)(struct PROCTAB *restrict const, const proc_t *restrict const, proc_t *restrict const, char *restrict const);
165     pid_t*      pids;   // pids of the procs
166     uid_t*      uids;   // uids of procs
167     int         nuid;   // cannot really sentinel-terminate unsigned short[]
168     int         i;  // generic
169     unsigned    flags;
170     unsigned    u;  // generic
171     void *      vp; // generic
172     char        path[PROCPATHLEN];  // must hold /proc/2000222000/task/2000222000/cmdline
173     unsigned pathlen;        // length of string in the above (w/o '\0')
174 } PROCTAB;
175
176 // initialize a PROCTAB structure holding needed call-to-call persistent data
177 extern PROCTAB* openproc(int flags, ... /* pid_t*|uid_t*|dev_t*|char* [, int n] */ );
178
179 typedef struct proc_data_t {
180     proc_t **tab;
181     proc_t **proc;
182     proc_t **task;
183     int n;
184     int nproc;
185     int ntask;
186 } proc_data_t;
187
188 extern proc_data_t *readproctab2(int(*want_proc)(proc_t *buf), int(*want_task)(proc_t *buf), PROCTAB *restrict const PT);
189
190 // Convenient wrapper around openproc and readproc to slurp in the whole process
191 // table subset satisfying the constraints of flags and the optional PID list.
192 // Free allocated memory with exit().  Access via tab[N]->member.  The pointer
193 // list is NULL terminated.
194
195 extern proc_t** readproctab(int flags, ... /* same as openproc */ );
196
197 // clean-up open files, etc from the openproc()
198 extern void closeproc(PROCTAB* PT);
199
200 // retrieve the next process matching the criteria set by the openproc()
201 extern proc_t* readproc(PROCTAB *restrict const PT, proc_t *restrict p);
202 extern proc_t* readtask(PROCTAB *restrict const PT, const proc_t *restrict const p, proc_t *restrict t);
203
204 // warning: interface may change
205 extern int read_cmdline(char *restrict const dst, unsigned sz, unsigned pid);
206
207 extern void look_up_our_self(proc_t *p);
208
209 // deallocate space allocated by readproc
210
211 extern void freeproc(proc_t* p);
212
213 //fill out a proc_t for a single task
214 extern proc_t * get_proc_stats(pid_t pid, proc_t *p);
215
216 // openproc/readproctab:
217 //
218 // Return PROCTAB* / *proc_t[] or NULL on error ((probably) "/proc" cannot be
219 // opened.)  By default readproc will consider all processes as valid to parse
220 // and return, but not actually fill in the cmdline, environ, and /proc/#/statm
221 // derived memory fields.
222 //
223 // `flags' (a bitwise-or of PROC_* below) modifies the default behavior.  The
224 // "fill" options will cause more of the proc_t to be filled in.  The "filter"
225 // options all use the second argument as the pointer to a list of objects:
226 // process status', process id's, user id's.  The third
227 // argument is the length of the list (currently only used for lists of user
228 // id's since uid_t supports no convenient termination sentinel.)
229
230 #define PROC_FILLMEM         0x0001 // read statm
231 #define PROC_FILLCOM         0x0002 // alloc and fill in `cmdline'
232 #define PROC_FILLENV         0x0004 // alloc and fill in `environ'
233 #define PROC_FILLUSR         0x0008 // resolve user id number -> user name
234 #define PROC_FILLGRP         0x0010 // resolve group id number -> group name
235 #define PROC_FILLSTATUS      0x0020 // read status -- currently unconditional
236 #define PROC_FILLSTAT        0x0040 // read stat -- currently unconditional
237 #define PROC_FILLWCHAN       0x0080 // look up WCHAN name
238 #define PROC_FILLARG         0x0100 // alloc and fill in `cmdline'
239
240 #define PROC_LOOSE_TASKS     0x0200 // threat threads as if they were processes
241
242 // Obsolete, consider only processes with one of the passed:
243 #define PROC_PID             0x1000  // process id numbers ( 0   terminated)
244 #define PROC_UID             0x4000  // user id numbers    ( length needed )
245
246 // it helps to give app code a few spare bits
247 #define PROC_SPARE_1     0x01000000
248 #define PROC_SPARE_2     0x02000000
249 #define PROC_SPARE_3     0x04000000
250 #define PROC_SPARE_4     0x08000000
251
252 EXTERN_C_END
253 #endif