blob: 0452a5046118106e77a8f62f275786c7c8c1e155 [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;
"Vladimir N. Oleynik"35939d92005-10-05 10:52:47 +000016
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +000017#if ENABLE_SELINUX
Eric Andersen9e480452003-07-03 10:07:04 +000018 int use_selinux = 0;
Denis Vlasenko266bc172006-09-29 17:16:39 +000019 security_context_t sid = NULL;
Eric Andersen9e480452003-07-03 10:07:04 +000020#endif
"Vladimir N. Oleynik"35939d92005-10-05 10:52:47 +000021
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +000022#if ENABLE_FEATURE_PS_WIDE
23 int terminal_width;
24 int w_count = 0;
"Vladimir N. Oleynik"35939d92005-10-05 10:52:47 +000025
Denis Vlasenko67b23e62006-10-03 21:00:06 +000026 opt_complementary = "-:ww";
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +000027#else
"Vladimir N. Oleynik"35939d92005-10-05 10:52:47 +000028# define terminal_width 79
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +000029#endif
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +000030
31#if ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX
32 /* handle arguments */
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +000033#if ENABLE_FEATURE_PS_WIDE && ENABLE_SELINUX
Denis Vlasenko67b23e62006-10-03 21:00:06 +000034 i = getopt32(argc, argv, "wc", &w_count);
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +000035#elif ENABLE_FEATURE_PS_WIDE && !ENABLE_SELINUX
Denis Vlasenko67b23e62006-10-03 21:00:06 +000036 getopt32(argc, argv, "w", &w_count);
"Vladimir N. Oleynik"9c44dce2005-10-04 17:09:50 +000037#else /* !ENABLE_FEATURE_PS_WIDE && ENABLE_SELINUX */
Denis Vlasenko67b23e62006-10-03 21:00:06 +000038 i = getopt32(argc, argv, "c");
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +000039#endif
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +000040#if ENABLE_FEATURE_PS_WIDE
41 /* if w is given once, GNU ps sets the width to 132,
42 * if w is given more than once, it is "unlimited"
43 */
"Vladimir N. Oleynik"35939d92005-10-05 10:52:47 +000044 if(w_count) {
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +000045 terminal_width = (w_count==1) ? 132 : INT_MAX;
46 } else {
"Vladimir N. Oleynik"35939d92005-10-05 10:52:47 +000047 get_terminal_width_height(1, &terminal_width, NULL);
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +000048 /* Go one less... */
49 terminal_width--;
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +000050 }
51#endif
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +000052#if ENABLE_SELINUX
"Vladimir N. Oleynik"9c44dce2005-10-04 17:09:50 +000053 if ((i & (1+ENABLE_FEATURE_PS_WIDE)) && is_selinux_enabled())
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +000054 use_selinux = 1;
55#endif
56#endif /* ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX */
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +000057
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +000058#if ENABLE_SELINUX
Rob Landley60158cb2005-05-03 06:25:50 +000059 if (use_selinux)
Denis Vlasenkobe905d52006-09-27 14:17:31 +000060 puts(" PID Context Stat Command");
Eric Andersen9e480452003-07-03 10:07:04 +000061 else
62#endif
Denis Vlasenkobe905d52006-09-27 14:17:31 +000063 puts(" PID Uid VmSize Stat Command");
Erik Andersene49d5ec2000-02-08 19:58:47 +000064
Rob Landley60158cb2005-05-03 06:25:50 +000065 while ((p = procps_scan(1)) != 0) {
66 char *namecmd = p->cmd;
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +000067#if ENABLE_SELINUX
Denis Vlasenkobe905d52006-09-27 14:17:31 +000068 if (use_selinux) {
Eric Andersen9e480452003-07-03 10:07:04 +000069 char sbuf[128];
70 len = sizeof(sbuf);
Eric Andersen9e480452003-07-03 10:07:04 +000071
Rob Landley60158cb2005-05-03 06:25:50 +000072 if (is_selinux_enabled()) {
Denis Vlasenkobe905d52006-09-27 14:17:31 +000073 if (getpidcon(p->pid,&sid) < 0)
74 sid = NULL;
Rob Landley60158cb2005-05-03 06:25:50 +000075 }
76
77 if (sid) {
Denis Vlasenkobe905d52006-09-27 14:17:31 +000078 /* I assume sid initilized with NULL */
79 len = strlen(sid)+1;
80 safe_strncpy(sbuf, sid, len);
81 freecon(sid);
82 sid = NULL;
83 } else {
84 safe_strncpy(sbuf, "unknown", 7);
Rob Landley60158cb2005-05-03 06:25:50 +000085 }
Eric Andersen9e480452003-07-03 10:07:04 +000086 len = printf("%5d %-32s %s ", p->pid, sbuf, p->state);
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +000087 }
Eric Andersen9e480452003-07-03 10:07:04 +000088 else
89#endif
Denis Vlasenkobe905d52006-09-27 14:17:31 +000090 if(p->rss == 0)
91 len = printf("%5d %-8s %s ", p->pid, p->user, p->state);
92 else
93 len = printf("%5d %-8s %6ld %s ", p->pid, p->user, p->rss, p->state);
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +000094
Eric Andersen44608e92002-10-22 12:21:15 +000095 i = terminal_width-len;
96
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +000097 if(namecmd && namecmd[0]) {
Eric Andersen44608e92002-10-22 12:21:15 +000098 if(i < 0)
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +000099 i = 0;
"Vladimir N. Oleynik"2d604582006-01-30 14:02:06 +0000100 if(strlen(namecmd) > (size_t)i)
Eric Andersen44608e92002-10-22 12:21:15 +0000101 namecmd[i] = 0;
102 printf("%s\n", namecmd);
103 } else {
104 namecmd = p->short_cmd;
105 if(i < 2)
106 i = 2;
"Vladimir N. Oleynik"2d604582006-01-30 14:02:06 +0000107 if(strlen(namecmd) > ((size_t)i-2))
Eric Andersen44608e92002-10-22 12:21:15 +0000108 namecmd[i-2] = 0;
109 printf("[%s]\n", namecmd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000110 }
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000111 /* no check needed, but to make valgrind happy.. */
112 if (ENABLE_FEATURE_CLEAN_UP && p->cmd)
113 free(p->cmd);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000114 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000115 return EXIT_SUCCESS;
Eric Andersenef8b6c71999-10-20 08:05:35 +0000116}