aboutsummaryrefslogtreecommitdiff
path: root/procps/ps.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-06-06 07:40:16 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-06-06 07:40:16 +0000
commit0696b8aae8c5e84d47893a78d6b7aeb416cba38e (patch)
treefc94b703c836f020e3b0bab10373057e75e6822f /procps/ps.c
parentcd0df46d8cb937ef277659ea339a77925c443d3d (diff)
downloadbusybox-w32-0696b8aae8c5e84d47893a78d6b7aeb416cba38e.tar.gz
busybox-w32-0696b8aae8c5e84d47893a78d6b7aeb416cba38e.tar.bz2
busybox-w32-0696b8aae8c5e84d47893a78d6b7aeb416cba38e.zip
ps: fix -Z (by Yuichi Nakamura <ynakam@hitachisoft.jp>)
Diffstat (limited to 'procps/ps.c')
-rw-r--r--procps/ps.c73
1 files changed, 45 insertions, 28 deletions
diff --git a/procps/ps.c b/procps/ps.c
index 968a6fe99..c3023cf5e 100644
--- a/procps/ps.c
+++ b/procps/ps.c
@@ -3,6 +3,8 @@
3 * Mini ps implementation(s) for busybox 3 * Mini ps implementation(s) for busybox
4 * 4 *
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> 5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6 * Fix for SELinux Support:(c)2007 Hiroshi Shinji <shiroshi@my.email.ne.jp>
7 (c)2007 Yuichi Nakamura <ynakam@hitachisoft.jp>
6 * 8 *
7 * Licensed under the GPL version 2, see the file LICENSE in this tarball. 9 * Licensed under the GPL version 2, see the file LICENSE in this tarball.
8 */ 10 */
@@ -68,6 +70,14 @@ static void func_tty(char *buf, int size, const procps_status_t *ps)
68{ 70{
69 safe_strncpy(buf, ps->tty_str, size+1); 71 safe_strncpy(buf, ps->tty_str, size+1);
70} 72}
73
74#if ENABLE_SELINUX
75static void func_label(char *buf, int size, const procps_status_t *ps)
76{
77 safe_strncpy(buf, ps->context ? ps->context : "unknown", size+1);
78}
79#endif
80
71/* 81/*
72static void func_nice(char *buf, int size, const procps_status_t *ps) 82static void func_nice(char *buf, int size, const procps_status_t *ps)
73{ 83{
@@ -116,11 +126,19 @@ static const ps_out_t out_spec[] = {
116 { 4 , "vsz" ,"VSZ" ,func_vsz ,PSSCAN_VSZ }, 126 { 4 , "vsz" ,"VSZ" ,func_vsz ,PSSCAN_VSZ },
117// Not mandated by POSIX, but useful: 127// Not mandated by POSIX, but useful:
118 { 4 , "rss" ,"RSS" ,func_rss ,PSSCAN_RSS }, 128 { 4 , "rss" ,"RSS" ,func_rss ,PSSCAN_RSS },
129#if ENABLE_SELINUX
130 { 35 , "label" ,"LABEL" ,func_label ,PSSCAN_CONTEXT },
131#endif
119}; 132};
120 133
121#define VEC_SIZE(v) ( sizeof(v) / sizeof((v)[0]) ) 134#define VEC_SIZE(v) ( sizeof(v) / sizeof((v)[0]) )
122 135
123#define DEFAULT_O_STR "pid,user" /* TODO: ,vsz,stat */ ",args" 136#if ENABLE_SELINUX
137#define SELINIX_O_PREFIX "label,"
138#define DEFAULT_O_STR SELINIX_O_PREFIX "pid,user" /* TODO: ,vsz,stat */ ",args"
139#else
140#define DEFAULT_O_STR "pid,user" /* TODO: ,vsz,stat */ ",args"
141#endif
124 142
125struct globals { 143struct globals {
126 ps_out_t* out; 144 ps_out_t* out;
@@ -207,6 +225,10 @@ static void post_process(void)
207 } 225 }
208 width += out[i].width + 1; /* "FIELD " */ 226 width += out[i].width + 1; /* "FIELD " */
209 } 227 }
228#if ENABLE_SELINUX
229 if (!is_selinux_enabled())
230 need_flags &= ~PSSCAN_CONTEXT;
231#endif
210 buffer = xmalloc(width + 1); /* for trailing \0 */ 232 buffer = xmalloc(width + 1); /* for trailing \0 */
211} 233}
212 234
@@ -261,9 +283,7 @@ int ps_main(int argc, char **argv)
261{ 283{
262 procps_status_t *p; 284 procps_status_t *p;
263 llist_t* opt_o = NULL; 285 llist_t* opt_o = NULL;
264 286 USE_SELINUX(int opt;)
265 /* Cannot be const: parse_o() will choke */
266 strcpy(default_o, DEFAULT_O_STR);
267 287
268 // POSIX: 288 // POSIX:
269 // -a Write information for all processes associated with terminals 289 // -a Write information for all processes associated with terminals
@@ -277,14 +297,25 @@ int ps_main(int argc, char **argv)
277 // Select which columns to display 297 // Select which columns to display
278 /* We allow (and ignore) most of the above. FIXME */ 298 /* We allow (and ignore) most of the above. FIXME */
279 opt_complementary = "o::"; 299 opt_complementary = "o::";
280 getopt32(argc, argv, "o:aAdefl", &opt_o); 300 USE_SELINUX(opt =) getopt32(argc, argv, "Zo:aAdefl", &opt_o);
281 if (opt_o) { 301 if (opt_o) {
282 do { 302 do {
283 parse_o(opt_o->data); 303 parse_o(opt_o->data);
284 opt_o = opt_o->link; 304 opt_o = opt_o->link;
285 } while (opt_o); 305 } while (opt_o);
286 } else 306 } else {
307 /* Below: parse_o() needs char*, NOT const char*... */
308#if ENABLE_SELINUX
309 if (!(opt & 1) || !is_selinux_enabled()) {
310 /* no -Z or no SELinux: do not show LABEL */
311 strcpy(default_o, DEFAULT_O_STR + sizeof(SELINIX_O_PREFIX)-1);
312 } else
313#endif
314 {
315 strcpy(default_o, DEFAULT_O_STR);
316 }
287 parse_o(default_o); 317 parse_o(default_o);
318 }
288 post_process(); 319 post_process();
289 320
290 /* Was INT_MAX, but some libc's go belly up with printf("%.*s") 321 /* Was INT_MAX, but some libc's go belly up with printf("%.*s")
@@ -314,7 +345,6 @@ int ps_main(int argc, char **argv)
314 procps_status_t *p = NULL; 345 procps_status_t *p = NULL;
315 int i, len; 346 int i, len;
316 SKIP_SELINUX(const) int use_selinux = 0; 347 SKIP_SELINUX(const) int use_selinux = 0;
317 USE_SELINUX(security_context_t sid = NULL;)
318#if !ENABLE_FEATURE_PS_WIDE 348#if !ENABLE_FEATURE_PS_WIDE
319 enum { terminal_width = 79 }; 349 enum { terminal_width = 79 };
320#else 350#else
@@ -341,7 +371,7 @@ int ps_main(int argc, char **argv)
341#endif 371#endif
342#if ENABLE_SELINUX 372#if ENABLE_SELINUX
343 if ((i & 1) && is_selinux_enabled()) 373 if ((i & 1) && is_selinux_enabled())
344 use_selinux = 1; 374 use_selinux = PSSCAN_CONTEXT;
345#endif 375#endif
346#endif /* ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX */ 376#endif /* ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX */
347 377
@@ -356,28 +386,15 @@ int ps_main(int argc, char **argv)
356 | PSSCAN_STATE 386 | PSSCAN_STATE
357 | PSSCAN_VSZ 387 | PSSCAN_VSZ
358 | PSSCAN_CMD 388 | PSSCAN_CMD
389 | use_selinux
359 ))) { 390 ))) {
360 char *namecmd = p->cmd; 391 char *namecmd = p->cmd;
361#if ENABLE_SELINUX 392#if ENABLE_SELINUX
362 if (use_selinux) { 393 if (use_selinux) {
363 char sbuf[128]; 394 len = printf("%5u %-32s %s ",
364 len = sizeof(sbuf); 395 p->pid,
365 396 p->context ? p->context : "unknown",
366 if (is_selinux_enabled()) { 397 p->state);
367 if (getpidcon(p->pid, &sid) < 0)
368 sid = NULL;
369 }
370
371 if (sid) {
372 /* I assume sid initialized with NULL */
373 len = strlen(sid) + 1;
374 safe_strncpy(sbuf, sid, len);
375 freecon(sid);
376 sid = NULL;
377 } else {
378 safe_strncpy(sbuf, "unknown", 7);
379 }
380 len = printf("%5u %-32s %s ", p->pid, sbuf, p->state);
381 } else 398 } else
382#endif 399#endif
383 { 400 {
@@ -396,14 +413,14 @@ int ps_main(int argc, char **argv)
396 if (i < 0) 413 if (i < 0)
397 i = 0; 414 i = 0;
398 if (strlen(namecmd) > (size_t)i) 415 if (strlen(namecmd) > (size_t)i)
399 namecmd[i] = 0; 416 namecmd[i] = '\0';
400 puts(namecmd); 417 puts(namecmd);
401 } else { 418 } else {
402 namecmd = p->comm; 419 namecmd = p->comm;
403 if (i < 2) 420 if (i < 2)
404 i = 2; 421 i = 2;
405 if (strlen(namecmd) > ((size_t)i-2)) 422 if (strlen(namecmd) > ((size_t)i-2))
406 namecmd[i-2] = 0; 423 namecmd[i-2] = '\0';
407 printf("[%s]\n", namecmd); 424 printf("[%s]\n", namecmd);
408 } 425 }
409 } 426 }