blob: 9d6e42d79b655d3975cc5bc3414fd8f864175c44 [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
Rob Landleydfba7412006-03-06 20:47:33 +000012int ps_main(int argc, char **argv)
Eric Andersenef8b6c71999-10-20 08:05:35 +000013{
Eric Andersen44608e92002-10-22 12:21:15 +000014 procps_status_t * p;
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +000015 int i, len;
Denis Vlasenkod3ada322006-11-01 09:14:52 +000016 SKIP_SELINUX(const) int use_selinux = 0;
17 USE_SELINUX(security_context_t sid = NULL;)
18#if !ENABLE_FEATURE_PS_WIDE
19 enum { terminal_width = 79 };
20#else
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +000021 int terminal_width;
22 int w_count = 0;
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +000023#endif
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +000024
25#if ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +000026#if ENABLE_FEATURE_PS_WIDE
Denis Vlasenkod3ada322006-11-01 09:14:52 +000027 opt_complementary = "-:ww";
Denis Vlasenkodd539f72006-11-01 20:20:37 +000028 USE_SELINUX(i =) getopt32(argc, argv, USE_SELINUX("c") "w", &w_count);
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +000029 /* if w is given once, GNU ps sets the width to 132,
30 * if w is given more than once, it is "unlimited"
31 */
Denis Vlasenko621204b2006-10-27 09:03:24 +000032 if (w_count) {
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +000033 terminal_width = (w_count==1) ? 132 : INT_MAX;
34 } else {
"Vladimir N. Oleynik"35939d92005-10-05 10:52:47 +000035 get_terminal_width_height(1, &terminal_width, NULL);
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +000036 /* Go one less... */
37 terminal_width--;
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +000038 }
Denis Vlasenkod3ada322006-11-01 09:14:52 +000039#else /* only ENABLE_SELINUX */
40 i = getopt32(argc, argv, "c");
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +000041#endif
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +000042#if ENABLE_SELINUX
Denis Vlasenkodd539f72006-11-01 20:20:37 +000043 if ((i & 1) && is_selinux_enabled())
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +000044 use_selinux = 1;
45#endif
Denis Vlasenkod3ada322006-11-01 09:14:52 +000046#endif /* ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX */
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +000047
Rob Landley60158cb2005-05-03 06:25:50 +000048 if (use_selinux)
Denis Vlasenkobe905d52006-09-27 14:17:31 +000049 puts(" PID Context Stat Command");
Eric Andersen9e480452003-07-03 10:07:04 +000050 else
Denis Vlasenkobe905d52006-09-27 14:17:31 +000051 puts(" PID Uid VmSize Stat Command");
Erik Andersene49d5ec2000-02-08 19:58:47 +000052
Rob Landley60158cb2005-05-03 06:25:50 +000053 while ((p = procps_scan(1)) != 0) {
54 char *namecmd = p->cmd;
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +000055#if ENABLE_SELINUX
Denis Vlasenkobe905d52006-09-27 14:17:31 +000056 if (use_selinux) {
Eric Andersen9e480452003-07-03 10:07:04 +000057 char sbuf[128];
58 len = sizeof(sbuf);
Eric Andersen9e480452003-07-03 10:07:04 +000059
Rob Landley60158cb2005-05-03 06:25:50 +000060 if (is_selinux_enabled()) {
Denis Vlasenko35fb5122006-11-01 09:16:49 +000061 if (getpidcon(p->pid, &sid) < 0)
Denis Vlasenkobe905d52006-09-27 14:17:31 +000062 sid = NULL;
Rob Landley60158cb2005-05-03 06:25:50 +000063 }
64
65 if (sid) {
Denis Vlasenkod3ada322006-11-01 09:14:52 +000066 /* I assume sid initialized with NULL */
Denis Vlasenkofa076802006-11-05 00:38:51 +000067 len = strlen(sid) + 1;
Denis Vlasenkobe905d52006-09-27 14:17:31 +000068 safe_strncpy(sbuf, sid, len);
69 freecon(sid);
70 sid = NULL;
71 } else {
72 safe_strncpy(sbuf, "unknown", 7);
Rob Landley60158cb2005-05-03 06:25:50 +000073 }
Denis Vlasenko35fb5122006-11-01 09:16:49 +000074 len = printf("%5u %-32s %s ", (unsigned)p->pid, sbuf, p->state);
Denis Vlasenko92258542006-11-01 10:25:35 +000075 } else
Eric Andersen9e480452003-07-03 10:07:04 +000076#endif
Denis Vlasenko621204b2006-10-27 09:03:24 +000077 if (p->rss == 0)
Denis Vlasenko35fb5122006-11-01 09:16:49 +000078 len = printf("%5u %-8s %s ", (unsigned)p->pid, p->user, p->state);
Denis Vlasenkobe905d52006-09-27 14:17:31 +000079 else
Denis Vlasenko35fb5122006-11-01 09:16:49 +000080 len = printf("%5u %-8s %6ld %s ", (unsigned)p->pid, p->user, p->rss, p->state);
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +000081
Eric Andersen44608e92002-10-22 12:21:15 +000082 i = terminal_width-len;
83
Denis Vlasenko621204b2006-10-27 09:03:24 +000084 if (namecmd && namecmd[0]) {
85 if (i < 0)
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +000086 i = 0;
Denis Vlasenko621204b2006-10-27 09:03:24 +000087 if (strlen(namecmd) > (size_t)i)
Eric Andersen44608e92002-10-22 12:21:15 +000088 namecmd[i] = 0;
Denis Vlasenkod3ada322006-11-01 09:14:52 +000089 puts(namecmd);
Eric Andersen44608e92002-10-22 12:21:15 +000090 } else {
91 namecmd = p->short_cmd;
Denis Vlasenko621204b2006-10-27 09:03:24 +000092 if (i < 2)
Eric Andersen44608e92002-10-22 12:21:15 +000093 i = 2;
Denis Vlasenko621204b2006-10-27 09:03:24 +000094 if (strlen(namecmd) > ((size_t)i-2))
Eric Andersen44608e92002-10-22 12:21:15 +000095 namecmd[i-2] = 0;
96 printf("[%s]\n", namecmd);
Erik Andersene49d5ec2000-02-08 19:58:47 +000097 }
Denis Vlasenkod3ada322006-11-01 09:14:52 +000098 /* no check needed, but to make valgrind happy.. */
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +000099 if (ENABLE_FEATURE_CLEAN_UP && p->cmd)
100 free(p->cmd);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000101 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000102 return EXIT_SUCCESS;
Eric Andersenef8b6c71999-10-20 08:05:35 +0000103}