blob: c1cb64397e4a17c339dfcbc521272a493130d15a [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenef8b6c71999-10-20 08:05:35 +00002/*
Erik Andersen246cc6d2000-03-07 07:41:42 +00003 * Mini ps implementation(s) for busybox
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersenef8b6c71999-10-20 08:05:35 +00006 *
Rob Landleye9a7a622006-09-22 02:52:41 +00007 * Licensed under the GPL version 2, see the file LICENSE in this tarball.
Eric Andersenbdfd0d72001-10-24 05:00:29 +00008 */
9
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000010#include "busybox.h"
Erik Andersen2ac2fae2000-03-07 23:32:17 +000011
Denis Vlasenko94949192006-11-05 00:45:47 +000012#if ENABLE_DESKTOP
13
14/* Print value to buf, max size+1 chars (including trailing '\0') */
15
Denis Vlasenko769d1e02007-01-22 23:04:27 +000016static void func_user(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +000017{
18 safe_strncpy(buf, get_cached_username(ps->uid), size+1);
19}
20
Denis Vlasenko769d1e02007-01-22 23:04:27 +000021static void func_comm(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +000022{
23 safe_strncpy(buf, ps->comm, size+1);
24}
25
Denis Vlasenko769d1e02007-01-22 23:04:27 +000026static void func_args(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +000027{
28 buf[0] = '\0';
29 if (ps->cmd)
30 safe_strncpy(buf, ps->cmd, size+1);
31 else if (size >= 2)
32 snprintf(buf, size+1, "[%.*s]", size-2, ps->comm);
33}
34
Denis Vlasenko769d1e02007-01-22 23:04:27 +000035static void func_pid(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +000036{
37 snprintf(buf, size+1, "%*u", size, ps->pid);
38}
39
Denis Vlasenko769d1e02007-01-22 23:04:27 +000040static void func_ppid(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +000041{
42 snprintf(buf, size+1, "%*u", size, ps->ppid);
43}
44
Denis Vlasenko769d1e02007-01-22 23:04:27 +000045static void func_pgid(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +000046{
47 snprintf(buf, size+1, "%*u", size, ps->pgid);
48}
49
Mike Frysinger0aa6ba52007-02-08 08:21:58 +000050static void func_vsz(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +000051{
52 char buf5[5];
Mike Frysinger0aa6ba52007-02-08 08:21:58 +000053 smart_ulltoa5( ((unsigned long long)ps->vsz) << 10, buf5);
Denis Vlasenko94949192006-11-05 00:45:47 +000054 snprintf(buf, size+1, "%.*s", size, buf5);
55}
56
57/*
58void func_nice(char *buf, int size, const procps_status_t *ps)
59{
60 ps->???
61}
62
63void func_etime(char *buf, int size, const procps_status_t *ps)
64{
65 elapled time [[dd-]hh:]mm:ss
66}
67
68void func_time(char *buf, int size, const procps_status_t *ps)
69{
70 cumulative time [[dd-]hh:]mm:ss
71}
72
73void func_pcpu(char *buf, int size, const procps_status_t *ps)
74{
75}
76
77void func_tty(char *buf, int size, const procps_status_t *ps)
78{
79}
80*/
81
82typedef struct {
83 char name[8];
84 const char *header;
85 void (*f)(char *buf, int size, const procps_status_t *ps);
86 int ps_flags;
87 int width;
88} ps_out_t;
89
90static const ps_out_t out_spec[] = {
91// Mandated by POSIX:
92 { "user" ,"USER" ,func_user ,PSSCAN_UIDGID,8 },
93 { "comm" ,"COMMAND",func_comm ,PSSCAN_COMM ,16 },
94 { "args" ,"COMMAND",func_args ,PSSCAN_CMD|PSSCAN_COMM,256 },
95 { "pid" ,"PID" ,func_pid ,PSSCAN_PID ,5 },
96 { "ppid" ,"PPID" ,func_ppid ,PSSCAN_PPID ,5 },
97 { "pgid" ,"PGID" ,func_pgid ,PSSCAN_PGID ,5 },
98// { "etime" ,"ELAPSED",func_etime ,PSSCAN_ ,sizeof("ELAPSED")-1 },
99// { "group" ,"GROUP" ,func_group ,PSSCAN_UIDGID,sizeof("GROUP" )-1 },
100// { "nice" ,"NI" ,func_nice ,PSSCAN_ ,sizeof("NI" )-1 },
101// { "pcpu" ,"%CPU" ,func_pcpu ,PSSCAN_ ,sizeof("%CPU" )-1 },
102// { "rgroup","RGROUP" ,func_rgroup,PSSCAN_UIDGID,sizeof("RGROUP" )-1 },
103// { "ruser" ,"RUSER" ,func_ruser ,PSSCAN_UIDGID,sizeof("RUSER" )-1 },
104// { "time" ,"TIME" ,func_time ,PSSCAN_ ,sizeof("TIME" )-1 },
105// { "tty" ,"TT" ,func_tty ,PSSCAN_ ,sizeof("TT" )-1 },
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000106 { "vsz" ,"VSZ" ,func_vsz ,PSSCAN_VSZ ,4 },
Denis Vlasenko94949192006-11-05 00:45:47 +0000107// Not mandated by POSIX:
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000108// { "rss" ,"RSS" ,func_rss ,PSSCAN_RSS ,4 },
Denis Vlasenko94949192006-11-05 00:45:47 +0000109};
110
111#define VEC_SIZE(v) ( sizeof(v) / sizeof((v)[0]) )
112
113static ps_out_t* out;
114static int out_cnt;
115static int print_header;
116static int ps_flags;
117static char *buffer;
118static unsigned terminal_width;
119
120
121static ps_out_t* new_out_t(void)
122{
123 int i = out_cnt++;
124 out = xrealloc(out, out_cnt * sizeof(*out));
125 return &out[i];
126}
127
128static const ps_out_t* find_out_spec(const char *name)
129{
130 int i;
131 for (i = 0; i < VEC_SIZE(out_spec); i++) {
132 if (!strcmp(name, out_spec[i].name))
133 return &out_spec[i];
134 }
135 bb_error_msg_and_die("bad -o argument '%s'", name);
136}
137
138static void parse_o(char* opt)
139{
140 ps_out_t* new;
141 // POSIX: "-o is blank- or comma-separated list" (FIXME)
142 char *comma, *equal;
143 while (1) {
144 comma = strchr(opt, ',');
145 equal = strchr(opt, '=');
146 if (comma && (!equal || equal > comma)) {
147 *comma = '\0';
148 *new_out_t() = *find_out_spec(opt);
149 *comma = ',';
150 opt = comma + 1;
151 continue;
152 }
153 break;
154 }
155 new = new_out_t();
156 if (equal)
157 *equal = '\0';
158 *new = *find_out_spec(opt);
159 if (equal) {
160 *equal = '=';
161 new->header = equal + 1;
162 // POSIX: the field widths shall be ... at least as wide as
163 // the header text (default or overridden value).
164 // If the header text is null, such as -o user=,
165 // the field width shall be at least as wide as the
166 // default header text
167 if (new->header[0]) {
168 new->width = strlen(new->header);
169 print_header = 1;
170 }
171 } else
172 print_header = 1;
173}
174
175static void post_process(void)
176{
177 int i;
178 int width = 0;
179 for (i = 0; i < out_cnt; i++) {
180 ps_flags |= out[i].ps_flags;
181 if (out[i].header[0]) {
182 print_header = 1;
183 }
184 width += out[i].width + 1; /* "FIELD " */
185 }
186 buffer = xmalloc(width + 1); /* for trailing \0 */
187}
188
189static void format_header(void)
190{
191 int i;
192 ps_out_t* op;
193 char *p = buffer;
194 if (!print_header)
195 return;
196 i = 0;
197 if (out_cnt) {
198 while (1) {
199 op = &out[i];
200 if (++i == out_cnt) /* do not pad last field */
201 break;
202 p += sprintf(p, "%-*s ", op->width, op->header);
203 }
204 strcpy(p, op->header);
205 }
206 printf("%.*s\n", terminal_width, buffer);
207}
208
209static void format_process(const procps_status_t *ps)
210{
211 int i, len;
212 char *p = buffer;
213 i = 0;
214 if (out_cnt) while (1) {
215 out[i].f(p, out[i].width, ps);
216 // POSIX: Any field need not be meaningful in all
217 // implementations. In such a case a hyphen ( '-' )
218 // should be output in place of the field value.
Denis Vlasenko6dca70a2006-12-18 14:20:01 +0000219 if (!p[0]) {
220 p[0] = '-';
221 p[1] = '\0';
Denis Vlasenko94949192006-11-05 00:45:47 +0000222 }
223 len = strlen(p);
224 p += len;
225 len = out[i].width - len + 1;
226 if (++i == out_cnt) /* do not pad last field */
227 break;
Denis Vlasenko6dca70a2006-12-18 14:20:01 +0000228 p += sprintf(p, "%*s", len, "");
Denis Vlasenko94949192006-11-05 00:45:47 +0000229 }
230 printf("%.*s\n", terminal_width, buffer);
231}
232
233/* Cannot be const: parse_o() will choke */
234static char default_o[] = "pid,user" /* TODO: ,vsz,stat */ ",args";
235
Denis Vlasenko06af2162007-02-03 17:28:39 +0000236int ps_main(int argc, char **argv);
Denis Vlasenko94949192006-11-05 00:45:47 +0000237int ps_main(int argc, char **argv)
238{
239 procps_status_t *p;
240 llist_t* opt_o = NULL;
241
242 // POSIX:
243 // -a Write information for all processes associated with terminals
244 // Implementations may omit session leaders from this list
245 // -A Write information for all processes
246 // -d Write information for all processes, except session leaders
247 // -e Write information for all processes (equivalent to -A.)
248 // -f Generate a full listing
249 // -l Generate a long listing
250 // -o col1,col2,col3=header
251 // Select which columns to distplay
252 /* We allow (and ignore) most of the above. FIXME */
253 opt_complementary = "o::";
254 getopt32(argc, argv, "o:aAdefl", &opt_o);
255 if (opt_o) {
Denis Vlasenko54cf5112007-02-17 18:11:45 +0000256 opt_o = llist_rev(opt_o);
Denis Vlasenko94949192006-11-05 00:45:47 +0000257 do {
258 parse_o(opt_o->data);
259 opt_o = opt_o->link;
260 } while (opt_o);
261 } else
262 parse_o(default_o);
263 post_process();
264
265 terminal_width = INT_MAX;
266 if (isatty(1)) {
267 get_terminal_width_height(1, &terminal_width, NULL);
268 terminal_width--;
269 }
270 format_header();
271
272 p = NULL;
273 while ((p = procps_scan(p, ps_flags))) {
274 format_process(p);
275 }
276
277 return EXIT_SUCCESS;
278}
279
280
281#else /* !ENABLE_DESKTOP */
282
283
Denis Vlasenko06af2162007-02-03 17:28:39 +0000284int ps_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +0000285int ps_main(int argc, char **argv)
Eric Andersenef8b6c71999-10-20 08:05:35 +0000286{
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000287 procps_status_t *p = NULL;
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000288 int i, len;
Denis Vlasenkod3ada322006-11-01 09:14:52 +0000289 SKIP_SELINUX(const) int use_selinux = 0;
290 USE_SELINUX(security_context_t sid = NULL;)
291#if !ENABLE_FEATURE_PS_WIDE
292 enum { terminal_width = 79 };
293#else
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000294 int terminal_width;
295 int w_count = 0;
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000296#endif
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000297
298#if ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000299#if ENABLE_FEATURE_PS_WIDE
Denis Vlasenkod3ada322006-11-01 09:14:52 +0000300 opt_complementary = "-:ww";
Bernhard Reutner-Fischer26a80162007-03-14 08:52:28 +0000301 USE_SELINUX(i =) getopt32(argc, argv, USE_SELINUX("Z") "w", &w_count);
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000302 /* if w is given once, GNU ps sets the width to 132,
303 * if w is given more than once, it is "unlimited"
304 */
Denis Vlasenko621204b2006-10-27 09:03:24 +0000305 if (w_count) {
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000306 terminal_width = (w_count==1) ? 132 : INT_MAX;
307 } else {
"Vladimir N. Oleynik"35939d92005-10-05 10:52:47 +0000308 get_terminal_width_height(1, &terminal_width, NULL);
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000309 /* Go one less... */
310 terminal_width--;
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000311 }
Denis Vlasenkod3ada322006-11-01 09:14:52 +0000312#else /* only ENABLE_SELINUX */
Bernhard Reutner-Fischer26a80162007-03-14 08:52:28 +0000313 i = getopt32(argc, argv, "Z");
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000314#endif
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000315#if ENABLE_SELINUX
Denis Vlasenkodd539f72006-11-01 20:20:37 +0000316 if ((i & 1) && is_selinux_enabled())
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000317 use_selinux = 1;
318#endif
Denis Vlasenkod3ada322006-11-01 09:14:52 +0000319#endif /* ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX */
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000320
Rob Landley60158cb2005-05-03 06:25:50 +0000321 if (use_selinux)
Denis Vlasenkobe905d52006-09-27 14:17:31 +0000322 puts(" PID Context Stat Command");
Eric Andersen9e480452003-07-03 10:07:04 +0000323 else
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000324 puts(" PID Uid VSZ Stat Command");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000325
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000326 while ((p = procps_scan(p, 0
327 | PSSCAN_PID
328 | PSSCAN_UIDGID
329 | PSSCAN_STATE
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000330 | PSSCAN_VSZ
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000331 | PSSCAN_CMD
332 ))) {
Rob Landley60158cb2005-05-03 06:25:50 +0000333 char *namecmd = p->cmd;
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000334#if ENABLE_SELINUX
Denis Vlasenkobe905d52006-09-27 14:17:31 +0000335 if (use_selinux) {
Eric Andersen9e480452003-07-03 10:07:04 +0000336 char sbuf[128];
337 len = sizeof(sbuf);
Eric Andersen9e480452003-07-03 10:07:04 +0000338
Rob Landley60158cb2005-05-03 06:25:50 +0000339 if (is_selinux_enabled()) {
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000340 if (getpidcon(p->pid, &sid) < 0)
Denis Vlasenkobe905d52006-09-27 14:17:31 +0000341 sid = NULL;
Rob Landley60158cb2005-05-03 06:25:50 +0000342 }
343
344 if (sid) {
Denis Vlasenkod3ada322006-11-01 09:14:52 +0000345 /* I assume sid initialized with NULL */
Denis Vlasenkofa076802006-11-05 00:38:51 +0000346 len = strlen(sid) + 1;
Denis Vlasenkobe905d52006-09-27 14:17:31 +0000347 safe_strncpy(sbuf, sid, len);
348 freecon(sid);
349 sid = NULL;
350 } else {
351 safe_strncpy(sbuf, "unknown", 7);
Rob Landley60158cb2005-05-03 06:25:50 +0000352 }
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000353 len = printf("%5u %-32s %s ", p->pid, sbuf, p->state);
Denis Vlasenko92258542006-11-01 10:25:35 +0000354 } else
Eric Andersen9e480452003-07-03 10:07:04 +0000355#endif
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000356 {
357 const char *user = get_cached_username(p->uid);
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000358 if (p->vsz == 0)
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000359 len = printf("%5u %-8s %s ",
360 p->pid, user, p->state);
Denis Vlasenkobe905d52006-09-27 14:17:31 +0000361 else
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000362 len = printf("%5u %-8s %6ld %s ",
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000363 p->pid, user, p->vsz, p->state);
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000364 }
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000365
Eric Andersen44608e92002-10-22 12:21:15 +0000366 i = terminal_width-len;
367
Denis Vlasenko621204b2006-10-27 09:03:24 +0000368 if (namecmd && namecmd[0]) {
369 if (i < 0)
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000370 i = 0;
Denis Vlasenko621204b2006-10-27 09:03:24 +0000371 if (strlen(namecmd) > (size_t)i)
Eric Andersen44608e92002-10-22 12:21:15 +0000372 namecmd[i] = 0;
Denis Vlasenkod3ada322006-11-01 09:14:52 +0000373 puts(namecmd);
Eric Andersen44608e92002-10-22 12:21:15 +0000374 } else {
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000375 namecmd = p->comm;
Denis Vlasenko621204b2006-10-27 09:03:24 +0000376 if (i < 2)
Eric Andersen44608e92002-10-22 12:21:15 +0000377 i = 2;
Denis Vlasenko621204b2006-10-27 09:03:24 +0000378 if (strlen(namecmd) > ((size_t)i-2))
Eric Andersen44608e92002-10-22 12:21:15 +0000379 namecmd[i-2] = 0;
380 printf("[%s]\n", namecmd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000381 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000382 }
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000383 if (ENABLE_FEATURE_CLEAN_UP)
384 clear_username_cache();
Matt Kraai3e856ce2000-12-01 02:55:13 +0000385 return EXIT_SUCCESS;
Eric Andersenef8b6c71999-10-20 08:05:35 +0000386}
Denis Vlasenko94949192006-11-05 00:45:47 +0000387
Denis Vlasenko40215162006-11-05 00:47:38 +0000388#endif /* ENABLE_DESKTOP */