source: MondoRescue/trunk/mindi-busybox/procps/ps.c@ 863

Last change on this file since 863 was 821, checked in by Bruno Cornec, 18 years ago

Addition of busybox 1.2.1 as a mindi-busybox new package
This should avoid delivering binary files in mindi not built there (Fedora and Debian are quite serious about that)

File size: 2.9 KB
Line 
1/* vi: set sw=4 ts=4: */
2/*
3 * Mini ps implementation(s) for busybox
4 *
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6 *
7 * Licensed under the GPL v2, see the file LICENSE in this tarball.
8 */
9
10#include "busybox.h"
11#include <stdio.h>
12#include <stdlib.h>
13#include <unistd.h>
14#include <dirent.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <ctype.h>
18#include <string.h>
19#include <termios.h>
20#include <sys/ioctl.h>
21#if ENABLE_SELINUX
22#include <selinux/selinux.h> /* for is_selinux_enabled() */
23#endif
24
25int ps_main(int argc, char **argv)
26{
27 procps_status_t * p;
28 int i, len;
29
30#if ENABLE_SELINUX
31 int use_selinux = 0;
32 security_context_t sid=NULL;
33#endif
34
35#if ENABLE_FEATURE_PS_WIDE
36 int terminal_width;
37 int w_count = 0;
38
39 bb_opt_complementally="-:ww";
40#else
41# define terminal_width 79
42#endif
43
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");
52#endif
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"
56 */
57 if(w_count) {
58 terminal_width = (w_count==1) ? 132 : INT_MAX;
59 } else {
60 get_terminal_width_height(1, &terminal_width, NULL);
61 /* Go one less... */
62 terminal_width--;
63 }
64#endif
65#if ENABLE_SELINUX
66 if ((i & (1+ENABLE_FEATURE_PS_WIDE)) && is_selinux_enabled())
67 use_selinux = 1;
68#endif
69#endif /* ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX */
70
71#if ENABLE_SELINUX
72 if (use_selinux)
73 printf(" PID Context Stat Command\n");
74 else
75#endif
76 printf(" PID Uid VmSize Stat Command\n");
77
78 while ((p = procps_scan(1)) != 0) {
79 char *namecmd = p->cmd;
80#if ENABLE_SELINUX
81 if (use_selinux)
82 {
83 char sbuf[128];
84 len = sizeof(sbuf);
85
86 if (is_selinux_enabled()) {
87 if (getpidcon(p->pid,&sid)<0)
88 sid=NULL;
89 }
90
91 if (sid) {
92 /* I assume sid initilized with NULL */
93 len = strlen(sid)+1;
94 safe_strncpy(sbuf, sid, len);
95 freecon(sid);
96 sid=NULL;
97 }else {
98 safe_strncpy(sbuf, "unknown",7);
99 }
100 len = printf("%5d %-32s %s ", p->pid, sbuf, p->state);
101 }
102 else
103#endif
104 if(p->rss == 0)
105 len = printf("%5d %-8s %s ", p->pid, p->user, p->state);
106 else
107 len = printf("%5d %-8s %6ld %s ", p->pid, p->user, p->rss, p->state);
108
109 i = terminal_width-len;
110
111 if(namecmd && namecmd[0]) {
112 if(i < 0)
113 i = 0;
114 if(strlen(namecmd) > (size_t)i)
115 namecmd[i] = 0;
116 printf("%s\n", namecmd);
117 } else {
118 namecmd = p->short_cmd;
119 if(i < 2)
120 i = 2;
121 if(strlen(namecmd) > ((size_t)i-2))
122 namecmd[i-2] = 0;
123 printf("[%s]\n", namecmd);
124 }
125 /* no check needed, but to make valgrind happy.. */
126 if (ENABLE_FEATURE_CLEAN_UP && p->cmd)
127 free(p->cmd);
128 }
129 return EXIT_SUCCESS;
130}
Note: See TracBrowser for help on using the repository browser.