diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-06-06 07:40:16 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-06-06 07:40:16 +0000 |
commit | 0696b8aae8c5e84d47893a78d6b7aeb416cba38e (patch) | |
tree | fc94b703c836f020e3b0bab10373057e75e6822f | |
parent | cd0df46d8cb937ef277659ea339a77925c443d3d (diff) | |
download | busybox-w32-0696b8aae8c5e84d47893a78d6b7aeb416cba38e.tar.gz busybox-w32-0696b8aae8c5e84d47893a78d6b7aeb416cba38e.tar.bz2 busybox-w32-0696b8aae8c5e84d47893a78d6b7aeb416cba38e.zip |
ps: fix -Z (by Yuichi Nakamura <ynakam@hitachisoft.jp>)
-rw-r--r-- | include/libbb.h | 4 | ||||
-rw-r--r-- | libbb/procps.c | 11 | ||||
-rw-r--r-- | procps/ps.c | 73 |
3 files changed, 59 insertions, 29 deletions
diff --git a/include/libbb.h b/include/libbb.h index dd0f5c1cc..1ba122a2d 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -830,6 +830,9 @@ typedef struct { | |||
830 | DIR *dir; | 830 | DIR *dir; |
831 | /* Fields are set to 0/NULL if failed to determine (or not requested) */ | 831 | /* Fields are set to 0/NULL if failed to determine (or not requested) */ |
832 | char *cmd; | 832 | char *cmd; |
833 | USE_SELINUX(char *context;) | ||
834 | /* Everything below must contain no ptrs to malloc'ed data: | ||
835 | * it is memset(0) for each process in procps_scan() */ | ||
833 | unsigned vsz, rss; /* we round it to kbytes */ | 836 | unsigned vsz, rss; /* we round it to kbytes */ |
834 | unsigned long stime, utime; | 837 | unsigned long stime, utime; |
835 | unsigned pid; | 838 | unsigned pid; |
@@ -859,6 +862,7 @@ enum { | |||
859 | PSSCAN_STIME = 1 << 10, | 862 | PSSCAN_STIME = 1 << 10, |
860 | PSSCAN_UTIME = 1 << 11, | 863 | PSSCAN_UTIME = 1 << 11, |
861 | PSSCAN_TTY = 1 << 12, | 864 | PSSCAN_TTY = 1 << 12, |
865 | USE_SELINUX(PSSCAN_CONTEXT = 1 << 13,) | ||
862 | /* These are all retrieved from proc/NN/stat in one go: */ | 866 | /* These are all retrieved from proc/NN/stat in one go: */ |
863 | PSSCAN_STAT = PSSCAN_PPID | PSSCAN_PGID | PSSCAN_SID | 867 | PSSCAN_STAT = PSSCAN_PPID | PSSCAN_PGID | PSSCAN_SID |
864 | | PSSCAN_COMM | PSSCAN_STATE | 868 | | PSSCAN_COMM | PSSCAN_STATE |
diff --git a/libbb/procps.c b/libbb/procps.c index 946f569f5..be0d61bd3 100644 --- a/libbb/procps.c +++ b/libbb/procps.c | |||
@@ -4,7 +4,8 @@ | |||
4 | * | 4 | * |
5 | * Copyright 1998 by Albert Cahalan; all rights reserved. | 5 | * Copyright 1998 by Albert Cahalan; all rights reserved. |
6 | * Copyright (C) 2002 by Vladimir Oleynik <dzo@simtreas.ru> | 6 | * Copyright (C) 2002 by Vladimir Oleynik <dzo@simtreas.ru> |
7 | * | 7 | * SELinux support: (c) 2007 by Yuichi Nakamura <ynakam@hitachisoft.jp> |
8 | * | ||
8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | 9 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
9 | */ | 10 | */ |
10 | 11 | ||
@@ -95,6 +96,7 @@ void free_procps_scan(procps_status_t* sp) | |||
95 | { | 96 | { |
96 | closedir(sp->dir); | 97 | closedir(sp->dir); |
97 | free(sp->cmd); | 98 | free(sp->cmd); |
99 | USE_SELINUX(free(sp->context);) | ||
98 | free(sp); | 100 | free(sp); |
99 | } | 101 | } |
100 | 102 | ||
@@ -132,6 +134,13 @@ procps_status_t* procps_scan(procps_status_t* sp, int flags) | |||
132 | sp->pid = pid; | 134 | sp->pid = pid; |
133 | if (!(flags & ~PSSCAN_PID)) break; | 135 | if (!(flags & ~PSSCAN_PID)) break; |
134 | 136 | ||
137 | #if ENABLE_SELINUX | ||
138 | if (flags & PSSCAN_CONTEXT) { | ||
139 | if (getpidcon(sp->pid, &sp->context) < 0) | ||
140 | sp->context = NULL; | ||
141 | } | ||
142 | #endif | ||
143 | |||
135 | filename_tail = filename + sprintf(filename, "/proc/%d", pid); | 144 | filename_tail = filename + sprintf(filename, "/proc/%d", pid); |
136 | 145 | ||
137 | if (flags & PSSCAN_UIDGID) { | 146 | if (flags & PSSCAN_UIDGID) { |
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 | ||
75 | static 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 | /* |
72 | static void func_nice(char *buf, int size, const procps_status_t *ps) | 82 | static 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 | ||
125 | struct globals { | 143 | struct 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 | } |