1 /* vi: set sw=4 ts=4: */
3 * Mini ps implementation(s) for busybox
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Licensed under the GPL v2, see the file LICENSE in this tarball.
19 #include <sys/ioctl.h>
22 #include <selinux/selinux.h> /* for is_selinux_enabled() */
25 int ps_main(int argc, char **argv)
32 security_context_t sid=NULL;
35 #if ENABLE_FEATURE_PS_WIDE
39 bb_opt_complementally="-:ww";
41 # define terminal_width 79
44 #if ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX
45 /* handle arguments */
46 #if ENABLE_FEATURE_PS_WIDE && ENABLE_SELINUX
47 i = bb_getopt_ulflags(argc, argv, "wc", &w_count);
48 #elif ENABLE_FEATURE_PS_WIDE && !ENABLE_SELINUX
49 bb_getopt_ulflags(argc, argv, "w", &w_count);
50 #else /* !ENABLE_FEATURE_PS_WIDE && ENABLE_SELINUX */
51 i = bb_getopt_ulflags(argc, argv, "c");
53 #if ENABLE_FEATURE_PS_WIDE
54 /* if w is given once, GNU ps sets the width to 132,
55 * if w is given more than once, it is "unlimited"
58 terminal_width = (w_count==1) ? 132 : INT_MAX;
60 get_terminal_width_height(1, &terminal_width, NULL);
66 if ((i & (1+ENABLE_FEATURE_PS_WIDE)) && is_selinux_enabled())
69 #endif /* ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX */
73 printf(" PID Context Stat Command\n");
76 printf(" PID Uid VmSize Stat Command\n");
78 while ((p = procps_scan(1)) != 0) {
79 char *namecmd = p->cmd;
86 if (is_selinux_enabled()) {
87 if (getpidcon(p->pid,&sid)<0)
92 /* I assume sid initilized with NULL */
94 safe_strncpy(sbuf, sid, len);
98 safe_strncpy(sbuf, "unknown",7);
100 len = printf("%5d %-32s %s ", p->pid, sbuf, p->state);
105 len = printf("%5d %-8s %s ", p->pid, p->user, p->state);
107 len = printf("%5d %-8s %6ld %s ", p->pid, p->user, p->rss, p->state);
109 i = terminal_width-len;
111 if(namecmd && namecmd[0]) {
114 if(strlen(namecmd) > (size_t)i)
116 printf("%s\n", namecmd);
118 namecmd = p->short_cmd;
121 if(strlen(namecmd) > ((size_t)i-2))
123 printf("[%s]\n", namecmd);
125 /* no check needed, but to make valgrind happy.. */
126 if (ENABLE_FEATURE_CLEAN_UP && p->cmd)