blob: 12dd57e9ae1c48a4845484e5e1f64a5b344da266 [file] [log] [blame]
Rob Landley9a733fc2015-04-02 15:07:36 -05001/* ps.c - show process list
Rob Landley7459c342013-08-20 15:37:42 -05002 *
Rob Landley9a733fc2015-04-02 15:07:36 -05003 * Copyright 2015 Rob Landley <rob@landley.net>
Rob Landley3fdbdac2014-05-18 14:05:13 -05004 *
Rob Landley7459c342013-08-20 15:37:42 -05005 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html
Rob Landley9a733fc2015-04-02 15:07:36 -05006 * And http://kernel.org/doc/Documentation/filesystems/proc.txt Table 1-4
7 * And linux kernel source fs/proc/array.c function do_task_stat()
8 *
Rob Landley97c536c2015-04-03 11:46:44 -05009 * Deviation from posix: no -n, "-o tty" called "TTY" not "TT",
Rob Landley7459c342013-08-20 15:37:42 -050010
Rob Landley97c536c2015-04-03 11:46:44 -050011USE_PS(NEWTOY(ps, "aAdeflo*", TOYFLAG_USR|TOYFLAG_BIN))
Rob Landley7459c342013-08-20 15:37:42 -050012
13config PS
14 bool "ps"
15 default n
16 help
Rob Landley9a733fc2015-04-02 15:07:36 -050017 usage: ps [-Aade] [-fl] [-gG GROUP] [-o FIELD] [-p PID] [-t TTY] [-u USER]
Rob Landley3fdbdac2014-05-18 14:05:13 -050018
Rob Landley9a733fc2015-04-02 15:07:36 -050019 List processes.
20
21 -A All processes
22 -a Processes with terminals, except session leaders
23 -d Processes that aren't session leaders
24 -e Same as -A
Rob Landley9a733fc2015-04-02 15:07:36 -050025 -f Full listing
Rob Landley97c536c2015-04-03 11:46:44 -050026 -l Long listing
27
Rob Landley9a733fc2015-04-02 15:07:36 -050028 -g Processes belonging to these session leaders
29 -G Processes with these real group IDs
Rob Landley9a733fc2015-04-02 15:07:36 -050030 -o Show FIELDS for each process
31 -p select by PID
32 -t select by TTY
33 -u select by USER
34 -U select by USER
35
36 GROUP, FIELD, PID, TTY, and USER are comma separated lists.
37
38 OUTPUT (-o) FIELDS:
39
40 S Linux defines the process state letters as:
41 R (running) S (sleeping) D (disk sleep) T (stopped) t (tracing stop)
42 Z (zombie) X (dead) x (dead) K (wakekill) W (waking)
43 F Process flags (PF_*) from linux source file include/sched.h
44 (in octal rather than hex because posix inexplicably says so)
45
46 Default is PID,TTY,TIME,CMD With -f UID,PID,PPID,C,STIME,TTY,TIME,CMD
47 With -l F,S,UID,PID,PPID,C,PRI,NI,ADDR,SZ,WCHAN,TTY,TIME,CMD
Rob Landley7459c342013-08-20 15:37:42 -050048*/
Rob Landley3fdbdac2014-05-18 14:05:13 -050049
Rob Landley7459c342013-08-20 15:37:42 -050050#define FOR_ps
51#include "toys.h"
52
53GLOBALS(
Rob Landley9a733fc2015-04-02 15:07:36 -050054 struct arg_list *o;
Rob Landley3db34142015-02-27 12:17:30 -060055
Rob Landley9a733fc2015-04-02 15:07:36 -050056 unsigned width;
57 dev_t tty;
Rob Landley97c536c2015-04-03 11:46:44 -050058 void *fields;
Rob Landley08089372015-04-04 01:26:58 -050059 long uptime;
Rob Landley7459c342013-08-20 15:37:42 -050060)
61
Rob Landley9a733fc2015-04-02 15:07:36 -050062/*
63 l l fl a fl fl l l l l l f a a a
64 F S UID PID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME CMD
65 ruser user rgroup group pid ppid pgid pcpu vsz nice etime time tty comm args
Rob Landley7459c342013-08-20 15:37:42 -050066
Rob Landley9a733fc2015-04-02 15:07:36 -050067 todo: thread support /proc/$d/task/%d/stat
68 man page: F flags mean...
69*/
70
Rob Landley97c536c2015-04-03 11:46:44 -050071struct strawberry {
72 struct strawberry *next, *prev;
73 short which, len;
74 char title[];
75};
76
Rob Landley9a733fc2015-04-02 15:07:36 -050077// dirtree callback.
78// toybuf used as: 1024 /proc/$PID/stat, 1024 slot[], 2048 /proc/$PID/cmdline
79static int do_ps(struct dirtree *new)
Rob Landley7459c342013-08-20 15:37:42 -050080{
Rob Landley08089372015-04-04 01:26:58 -050081 struct strawberry *field;
Rob Landley9a733fc2015-04-02 15:07:36 -050082 long long *slot = (void *)(toybuf+1024);
83 char *name, *s, state;
Rob Landley08089372015-04-04 01:26:58 -050084 int nlen, i, fd, len, width = TT.width;
Rob Landley7459c342013-08-20 15:37:42 -050085
Rob Landley9a733fc2015-04-02 15:07:36 -050086 if (!new->parent) return DIRTREE_RECURSE;
87 if (!(*slot = atol(new->name))) return 0;
Rob Landley7459c342013-08-20 15:37:42 -050088
Rob Landley9a733fc2015-04-02 15:07:36 -050089 // name field limited to 256 bytes by VFS, plus 40 fields * max 20 chars:
90 // 1000-ish total, but some forced zero so actually there's headroom.
91 sprintf(toybuf, "%lld/stat", *slot);
92 if (!readfileat(dirtree_parentfd(new), toybuf, toybuf, 1024)) return 0;
Rob Landley7459c342013-08-20 15:37:42 -050093
Rob Landley9a733fc2015-04-02 15:07:36 -050094 // parse oddball fields (name and state)
95 if (!(s = strchr(toybuf, '('))) return 0;
96 for (name = ++s; *s != ')'; s++) if (!*s) return 0;
97 nlen = s++-name;
98 if (1>sscanf(++s, " %c%n", &state, &i)) return 0;
Rob Landley7459c342013-08-20 15:37:42 -050099
Rob Landley9a733fc2015-04-02 15:07:36 -0500100 // parse numeric fields
Rob Landley08089372015-04-04 01:26:58 -0500101 for (len = 1; len<100; len++)
102 if (1>sscanf(s += i, " %lld%n", slot+len, &i)) break;
Rob Landley7459c342013-08-20 15:37:42 -0500103
Rob Landley9a733fc2015-04-02 15:07:36 -0500104 // skip entries we don't care about.
105 if ((toys.optflags&(FLAG_a|FLAG_d)) && getsid(*slot)==*slot) return 0;
106 if ((toys.optflags&FLAG_a) && !slot[4]) return 0;
107 if (!(toys.optflags*(FLAG_a|FLAG_d|FLAG_A|FLAG_e)) && TT.tty!=slot[4])
108 return 0;
Rob Landley7459c342013-08-20 15:37:42 -0500109
Rob Landley97c536c2015-04-03 11:46:44 -0500110 // 0 "F", "S", "UID", "PID", "PPID", "C", "PRI",
111 // 7 "NI", "ADDR", "SZ", "WCHAN", "STIME", "TTY", "TIME", "CMD",
112 //15 "COMMAND", "ELAPSED", "GROUP", "%CPU", "PGID", "RGROUP",
113 //21 "RUSER", "USER", "VSZ"
Rob Landley9a733fc2015-04-02 15:07:36 -0500114
Rob Landley08089372015-04-04 01:26:58 -0500115 for (field = TT.fields; field; field = field->next) {
Rob Landley9a733fc2015-04-02 15:07:36 -0500116 char *out = toybuf+2048;
Rob Landley97c536c2015-04-03 11:46:44 -0500117
Rob Landley08089372015-04-04 01:26:58 -0500118 // Default: unsupported (5 "C")
Rob Landley9a733fc2015-04-02 15:07:36 -0500119 sprintf(out, "-");
120
Rob Landley97c536c2015-04-03 11:46:44 -0500121 // PID, PPID, PRI, NI, ADDR, SZ
Rob Landley08089372015-04-04 01:26:58 -0500122 if (-1 != (i = stridx((char[]){3,4,6,7,8,9,0}, field->which)))
Rob Landley97c536c2015-04-03 11:46:44 -0500123 sprintf(out, ((1<<i)&0x10) ? "%llx" : "%lld",
124 slot[((char[]){0,2,16,17,22})[i]]>>(((1<<i)&0x20) ? 12 : 0));
Rob Landley08089372015-04-04 01:26:58 -0500125 // F
126 else if (!(i = field->which)) sprintf(out, "%llo", slot[7]);
Rob Landley97c536c2015-04-03 11:46:44 -0500127 // S
Rob Landley08089372015-04-04 01:26:58 -0500128 else if (i == 1)
Rob Landley97c536c2015-04-03 11:46:44 -0500129 sprintf(out, "%c", state);
Rob Landley08089372015-04-04 01:26:58 -0500130 // UID and USER
131 else if (i == 2 || i == 22) {
Rob Landley9a733fc2015-04-02 15:07:36 -0500132 sprintf(out, "%d", new->st.st_uid);
Rob Landley97c536c2015-04-03 11:46:44 -0500133 if (i == 2 || (toys.optflags&FLAG_f)) {
Rob Landley9a733fc2015-04-02 15:07:36 -0500134 struct passwd *pw = getpwuid(new->st.st_uid);
135
136 if (pw) out = pw->pw_name;
137 }
Rob Landley97c536c2015-04-03 11:46:44 -0500138 // WCHAN
Rob Landley08089372015-04-04 01:26:58 -0500139 } else if (i==10) {
Rob Landley97c536c2015-04-03 11:46:44 -0500140 sprintf(toybuf+512, "%lld/wchan", *slot);
Rob Landley9a733fc2015-04-02 15:07:36 -0500141 readfileat(dirtree_parentfd(new), toybuf+512, out, 2047);
142
Rob Landley08089372015-04-04 01:26:58 -0500143 // STIME
144 // TTY
145 } else if (i==12) {
Rob Landley9a733fc2015-04-02 15:07:36 -0500146
Rob Landley08089372015-04-04 01:26:58 -0500147 // Can we readlink() our way to a name?
148 for (i=0; i<3; i++) {
149 struct stat st;
150
151 sprintf(toybuf+512, "%lld/fd/%i", *slot, i);
152 fd = dirtree_parentfd(new);
153 if (!fstatat(fd, toybuf+512, &st, 0) && S_ISCHR(st.st_mode)
154 && st.st_rdev == slot[4]
155 && 0<(len = readlinkat(fd, toybuf+512, out, 2047)))
156 {
157 out[len] = 0;
158 if (!strncmp(out, "/dev/", 5)) out += 5;
159
160 break;
161 }
162 }
163
164 // Couldn't find it, show major:minor
165 if (i==3) {
166 i = slot[4];
167 sprintf(out, "%d:%d", (i>>8)&0xfff, ((i>>12)&0xfff00)|(i&0xff));
168 }
169
170 // TIME
171 } else if (i==13 || i==16) {
172 long seconds = (i==16) ? slot[20] : slot[11]+slot[12], ll = 60*60*24;
173
174 seconds /= sysconf(_SC_CLK_TCK);
175 if (i==16) seconds = TT.uptime-seconds;
Rob Landley9a733fc2015-04-02 15:07:36 -0500176 for (s = out, i = 0; i<4; i++) {
177 if (i>1 || seconds > ll)
Rob Landley08089372015-04-04 01:26:58 -0500178 s += sprintf(s, (i==3) ? "%02ld" : "%ld%c", seconds/ll, "-::"[i]);
179 seconds %= ll;
Rob Landley9a733fc2015-04-02 15:07:36 -0500180 ll /= i ? 60 : 24;
181 }
182
Rob Landley08089372015-04-04 01:26:58 -0500183 //16 "ELAPSED", "GROUP", "%CPU", "PGID", "RGROUP",
184 //21 "RUSER", -, "VSZ"
185
186
Rob Landley9a733fc2015-04-02 15:07:36 -0500187 // Command line limited to 2k displayable. We could dynamically malloc, but
188 // it'd almost never get used, querying length of a proc file is awkward,
Rob Landley08089372015-04-04 01:26:58 -0500189 // fixed buffer is nommu friendly... Wait for somebody to complain. :)
190 } else if (i == 14 || i == 15) {
191 int fd;
Rob Landley9a733fc2015-04-02 15:07:36 -0500192
Rob Landley08089372015-04-04 01:26:58 -0500193 len = 0;
Rob Landley9a733fc2015-04-02 15:07:36 -0500194 sprintf(out, "%lld/cmdline", *slot);
195 fd = openat(dirtree_parentfd(new), out, O_RDONLY);
196
197 if (fd != -1) {
198 if (0<(len = read(fd, out, 2047))) {
199 out[len] = 0;
200 for (i = 0; i<len; i++) if (out[i] < ' ') out[i] = ' ';
Rob Landley7459c342013-08-20 15:37:42 -0500201 }
Rob Landley9a733fc2015-04-02 15:07:36 -0500202 close(fd);
203 }
204 if (len<1) sprintf(out, "[%.*s]", nlen, name);
Rob Landley7459c342013-08-20 15:37:42 -0500205 }
Rob Landley9a733fc2015-04-02 15:07:36 -0500206
Rob Landley08089372015-04-04 01:26:58 -0500207 i = width<field->len ? width : field->len;
208 width -= printf(" %*.*s", i, field->next ? i : width, out);
Rob Landley7459c342013-08-20 15:37:42 -0500209 }
Rob Landley7459c342013-08-20 15:37:42 -0500210 xputc('\n');
Rob Landley7459c342013-08-20 15:37:42 -0500211
Rob Landley9a733fc2015-04-02 15:07:36 -0500212 return 0;
Rob Landley7459c342013-08-20 15:37:42 -0500213}
214
215void ps_main(void)
216{
Rob Landley97c536c2015-04-03 11:46:44 -0500217 struct strawberry *field;
Rob Landley9a733fc2015-04-02 15:07:36 -0500218 // Octal output code followed by header name
219 char *typos[] = {
Rob Landley97c536c2015-04-03 11:46:44 -0500220 "F", "S", "UID", "PID", "PPID", "C", "PRI",
221 "NI", "ADDR", "SZ", "WCHAN", "STIME", "TTY", "TIME", "CMD",
222 "COMMAND", "ELAPSED", "GROUP", "%CPU", "PGID", "RGROUP",
223 "RUSER", "USER", "VSZ"
Rob Landley3fdbdac2014-05-18 14:05:13 -0500224 };
Rob Landley97c536c2015-04-03 11:46:44 -0500225 int i, fd = -1;
Rob Landleyd0bc1a32015-03-14 12:34:14 -0500226
Rob Landley9a733fc2015-04-02 15:07:36 -0500227 // l l fl a fl fl l l l l l f a a a
228 // F S UID PID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME CMD
229 // 7
Rob Landleyd0bc1a32015-03-14 12:34:14 -0500230
Rob Landley9a733fc2015-04-02 15:07:36 -0500231 TT.width = 80;
232 terminal_size(&TT.width, 0);
Rob Landley08089372015-04-04 01:26:58 -0500233 TT.width--;
Rob Landley7459c342013-08-20 15:37:42 -0500234
Rob Landley9a733fc2015-04-02 15:07:36 -0500235 // find controlling tty, falling back to /dev/tty if none
236 for (i = fd = 0; i < 4; i++) {
237 struct stat st;
238
239 if (i != 3 || -1 != (i = fd = open("/dev/tty", O_RDONLY))) {
240 if (isatty(i) && !fstat(i, &st)) {
241 TT.tty = st.st_rdev;
242 break;
243 }
244 }
Rob Landley7459c342013-08-20 15:37:42 -0500245 }
Rob Landley9a733fc2015-04-02 15:07:36 -0500246 if (fd != -1) close(fd);
Rob Landley3db34142015-02-27 12:17:30 -0600247
Rob Landley08089372015-04-04 01:26:58 -0500248 sysinfo((void *)toybuf);
249 // Because "TT.uptime = *(long *)toybuf;" triggers a bug in gcc.
250 {
251 long *sigh = (long *)toybuf;
252 TT.uptime = *sigh;
253 }
254
Rob Landley97c536c2015-04-03 11:46:44 -0500255 // Select fields
Rob Landley08089372015-04-04 01:26:58 -0500256 if (toys.optflags&FLAG_o) {
Rob Landley9a733fc2015-04-02 15:07:36 -0500257 printf("todo\n");
258 } else {
Rob Landley08089372015-04-04 01:26:58 -0500259 unsigned short def = 0x7008;
Rob Landley3db34142015-02-27 12:17:30 -0600260
Rob Landley08089372015-04-04 01:26:58 -0500261 if (toys.optflags&FLAG_f) def = 0x783c;
262 if (toys.optflags&FLAG_l) def = 0x77ff;
Rob Landley9a733fc2015-04-02 15:07:36 -0500263
264 // order of fields[] matches posix STDOUT section, so add enabled XSI
265 // defaults according to bitmask
Rob Landley9a733fc2015-04-02 15:07:36 -0500266
Rob Landley97c536c2015-04-03 11:46:44 -0500267 for (i=0; def>>i; i++) {
268 int len = strlen(typos[i]);
269
270 if (!((def>>i)&1)) continue;
271
272 field = xmalloc(sizeof(struct strawberry)+len+1);
273 field->which = i;
274 field->len = len;
275 strcpy(field->title, typos[i]);
Rob Landley08089372015-04-04 01:26:58 -0500276 dlist_add_nomalloc((void *)&TT.fields, (void *)field);
Rob Landley97c536c2015-04-03 11:46:44 -0500277 }
278 }
279 dlist_terminate(TT.fields);
280
Rob Landley08089372015-04-04 01:26:58 -0500281 for (field = TT.fields; field; field = field->next)
282 printf(" %*s", field->len, field->title);
283 xputc('\n');
284
Rob Landley9a733fc2015-04-02 15:07:36 -0500285 dirtree_read("/proc", do_ps);
Rob Landley7459c342013-08-20 15:37:42 -0500286}