blob: de141fc34c4b09fb8720055a415d08443865786d [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001#include <stdio.h>
2#include <stdlib.h>
3#include <ctype.h>
4#include <fcntl.h>
5
6#include <string.h>
7
8#include <sys/stat.h>
9#include <sys/types.h>
10#include <dirent.h>
11
12#include <pwd.h>
13
San Mehat39274412009-10-27 11:53:22 -070014#include <cutils/sched_policy.h>
15
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080016static char *nexttoksep(char **strp, char *sep)
17{
18 char *p = strsep(strp,sep);
19 return (p == 0) ? "" : p;
20}
21static char *nexttok(char **strp)
22{
23 return nexttoksep(strp, " ");
24}
25
26#define SHOW_PRIO 1
27#define SHOW_TIME 2
San Mehat39274412009-10-27 11:53:22 -070028#define SHOW_POLICY 4
Dmitry Shmidt8b37c912010-08-18 17:26:26 -070029#define SHOW_CPU 8
Stephen Smalley8290d102012-01-13 08:53:56 -050030#define SHOW_MACLABEL 16
Marco Nelissen377cb2a2013-10-25 08:13:46 -070031#define SHOW_NUMERIC_UID 32
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032
33static int display_flags = 0;
34
35static int ps_line(int pid, int tid, char *namefilter)
36{
37 char statline[1024];
38 char cmdline[1024];
Stephen Smalley8290d102012-01-13 08:53:56 -050039 char macline[1024];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040 char user[32];
41 struct stat stats;
42 int fd, r;
43 char *ptr, *name, *state;
44 int ppid, tty;
45 unsigned wchan, rss, vss, eip;
46 unsigned utime, stime;
Dmitry Shmidt8b37c912010-08-18 17:26:26 -070047 int prio, nice, rtprio, sched, psr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048 struct passwd *pw;
Marco Nelissen377cb2a2013-10-25 08:13:46 -070049
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080050 sprintf(statline, "/proc/%d", pid);
51 stat(statline, &stats);
52
53 if(tid) {
54 sprintf(statline, "/proc/%d/task/%d/stat", pid, tid);
55 cmdline[0] = 0;
Stephen Smalley8290d102012-01-13 08:53:56 -050056 snprintf(macline, sizeof(macline), "/proc/%d/task/%d/attr/current", pid, tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057 } else {
58 sprintf(statline, "/proc/%d/stat", pid);
Stephen Smalley8290d102012-01-13 08:53:56 -050059 sprintf(cmdline, "/proc/%d/cmdline", pid);
60 snprintf(macline, sizeof(macline), "/proc/%d/attr/current", pid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061 fd = open(cmdline, O_RDONLY);
62 if(fd == 0) {
63 r = 0;
64 } else {
65 r = read(fd, cmdline, 1023);
66 close(fd);
67 if(r < 0) r = 0;
68 }
69 cmdline[r] = 0;
70 }
Marco Nelissen377cb2a2013-10-25 08:13:46 -070071
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072 fd = open(statline, O_RDONLY);
73 if(fd == 0) return -1;
74 r = read(fd, statline, 1023);
75 close(fd);
76 if(r < 0) return -1;
77 statline[r] = 0;
78
79 ptr = statline;
80 nexttok(&ptr); // skip pid
81 ptr++; // skip "("
82
83 name = ptr;
84 ptr = strrchr(ptr, ')'); // Skip to *last* occurence of ')',
85 *ptr++ = '\0'; // and null-terminate name.
86
87 ptr++; // skip " "
88 state = nexttok(&ptr);
89 ppid = atoi(nexttok(&ptr));
90 nexttok(&ptr); // pgrp
91 nexttok(&ptr); // sid
92 tty = atoi(nexttok(&ptr));
Marco Nelissen377cb2a2013-10-25 08:13:46 -070093
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080094 nexttok(&ptr); // tpgid
95 nexttok(&ptr); // flags
96 nexttok(&ptr); // minflt
97 nexttok(&ptr); // cminflt
98 nexttok(&ptr); // majflt
99 nexttok(&ptr); // cmajflt
100#if 1
101 utime = atoi(nexttok(&ptr));
102 stime = atoi(nexttok(&ptr));
103#else
104 nexttok(&ptr); // utime
105 nexttok(&ptr); // stime
106#endif
107 nexttok(&ptr); // cutime
108 nexttok(&ptr); // cstime
109 prio = atoi(nexttok(&ptr));
110 nice = atoi(nexttok(&ptr));
111 nexttok(&ptr); // threads
112 nexttok(&ptr); // itrealvalue
113 nexttok(&ptr); // starttime
114 vss = strtoul(nexttok(&ptr), 0, 10); // vsize
115 rss = strtoul(nexttok(&ptr), 0, 10); // rss
116 nexttok(&ptr); // rlim
117 nexttok(&ptr); // startcode
118 nexttok(&ptr); // endcode
119 nexttok(&ptr); // startstack
120 nexttok(&ptr); // kstkesp
121 eip = strtoul(nexttok(&ptr), 0, 10); // kstkeip
122 nexttok(&ptr); // signal
123 nexttok(&ptr); // blocked
124 nexttok(&ptr); // sigignore
125 nexttok(&ptr); // sigcatch
126 wchan = strtoul(nexttok(&ptr), 0, 10); // wchan
127 nexttok(&ptr); // nswap
128 nexttok(&ptr); // cnswap
129 nexttok(&ptr); // exit signal
Dmitry Shmidt8b37c912010-08-18 17:26:26 -0700130 psr = atoi(nexttok(&ptr)); // processor
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800131 rtprio = atoi(nexttok(&ptr)); // rt_priority
132 sched = atoi(nexttok(&ptr)); // scheduling policy
Marco Nelissen377cb2a2013-10-25 08:13:46 -0700133
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800134 tty = atoi(nexttok(&ptr));
Marco Nelissen377cb2a2013-10-25 08:13:46 -0700135
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136 if(tid != 0) {
137 ppid = pid;
138 pid = tid;
139 }
140
141 pw = getpwuid(stats.st_uid);
Marco Nelissen377cb2a2013-10-25 08:13:46 -0700142 if(pw == 0 || (display_flags & SHOW_NUMERIC_UID)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143 sprintf(user,"%d",(int)stats.st_uid);
144 } else {
145 strcpy(user,pw->pw_name);
146 }
Marco Nelissen377cb2a2013-10-25 08:13:46 -0700147
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800148 if(!namefilter || !strncmp(name, namefilter, strlen(namefilter))) {
Stephen Smalley8290d102012-01-13 08:53:56 -0500149 if (display_flags & SHOW_MACLABEL) {
150 fd = open(macline, O_RDONLY);
151 strcpy(macline, "-");
152 if (fd >= 0) {
153 r = read(fd, macline, sizeof(macline)-1);
154 close(fd);
155 if (r > 0)
156 macline[r] = 0;
157 }
158 printf("%-30s %-9s %-5d %-5d %s\n", macline, user, pid, ppid, cmdline[0] ? cmdline : name);
159 return 0;
160 }
161
San Mehat39274412009-10-27 11:53:22 -0700162 printf("%-9s %-5d %-5d %-6d %-5d", user, pid, ppid, vss / 1024, rss * 4);
Dmitry Shmidt8b37c912010-08-18 17:26:26 -0700163 if (display_flags & SHOW_CPU)
164 printf(" %-2d", psr);
165 if (display_flags & SHOW_PRIO)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166 printf(" %-5d %-5d %-5d %-5d", prio, nice, rtprio, sched);
San Mehat39274412009-10-27 11:53:22 -0700167 if (display_flags & SHOW_POLICY) {
168 SchedPolicy p;
169 if (get_sched_policy(pid, &p) < 0)
170 printf(" un ");
Glenn Kasten86c7cc82012-03-05 16:14:39 -0800171 else
172 printf(" %.2s ", get_sched_policy_name(p));
San Mehat39274412009-10-27 11:53:22 -0700173 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174 printf(" %08x %08x %s %s", wchan, eip, state, cmdline[0] ? cmdline : name);
175 if(display_flags&SHOW_TIME)
176 printf(" (u:%d, s:%d)", utime, stime);
San Mehat39274412009-10-27 11:53:22 -0700177
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178 printf("\n");
179 }
180 return 0;
181}
182
183
184void ps_threads(int pid, char *namefilter)
185{
186 char tmp[128];
187 DIR *d;
188 struct dirent *de;
189
190 sprintf(tmp,"/proc/%d/task",pid);
191 d = opendir(tmp);
192 if(d == 0) return;
Marco Nelissen377cb2a2013-10-25 08:13:46 -0700193
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800194 while((de = readdir(d)) != 0){
195 if(isdigit(de->d_name[0])){
196 int tid = atoi(de->d_name);
197 if(tid == pid) continue;
198 ps_line(pid, tid, namefilter);
199 }
200 }
Marco Nelissen377cb2a2013-10-25 08:13:46 -0700201 closedir(d);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202}
203
204int ps_main(int argc, char **argv)
205{
206 DIR *d;
207 struct dirent *de;
208 char *namefilter = 0;
209 int pidfilter = 0;
210 int threads = 0;
Marco Nelissen377cb2a2013-10-25 08:13:46 -0700211
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800212 d = opendir("/proc");
213 if(d == 0) return -1;
214
215 while(argc > 1){
216 if(!strcmp(argv[1],"-t")) {
217 threads = 1;
Marco Nelissen377cb2a2013-10-25 08:13:46 -0700218 } else if(!strcmp(argv[1],"-n")) {
219 display_flags |= SHOW_NUMERIC_UID;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220 } else if(!strcmp(argv[1],"-x")) {
221 display_flags |= SHOW_TIME;
Stephen Smalley8290d102012-01-13 08:53:56 -0500222 } else if(!strcmp(argv[1], "-Z")) {
223 display_flags |= SHOW_MACLABEL;
San Mehat39274412009-10-27 11:53:22 -0700224 } else if(!strcmp(argv[1],"-P")) {
225 display_flags |= SHOW_POLICY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800226 } else if(!strcmp(argv[1],"-p")) {
227 display_flags |= SHOW_PRIO;
Dmitry Shmidt8b37c912010-08-18 17:26:26 -0700228 } else if(!strcmp(argv[1],"-c")) {
229 display_flags |= SHOW_CPU;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800230 } else if(isdigit(argv[1][0])){
231 pidfilter = atoi(argv[1]);
232 } else {
233 namefilter = argv[1];
234 }
235 argc--;
236 argv++;
237 }
238
Stephen Smalley8290d102012-01-13 08:53:56 -0500239 if (display_flags & SHOW_MACLABEL) {
240 printf("LABEL USER PID PPID NAME\n");
241 } else {
242 printf("USER PID PPID VSIZE RSS %s%s %s WCHAN PC NAME\n",
243 (display_flags&SHOW_CPU)?"CPU ":"",
244 (display_flags&SHOW_PRIO)?"PRIO NICE RTPRI SCHED ":"",
245 (display_flags&SHOW_POLICY)?"PCY " : "");
246 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800247 while((de = readdir(d)) != 0){
248 if(isdigit(de->d_name[0])){
249 int pid = atoi(de->d_name);
250 if(!pidfilter || (pidfilter == pid)) {
251 ps_line(pid, 0, namefilter);
252 if(threads) ps_threads(pid, namefilter);
253 }
254 }
255 }
256 closedir(d);
257 return 0;
258}
259