Patch from vodz:
[oweals/busybox.git] / libbb / procps.c
index 9ac5be092274fd7b6580c43f47db8584532baee6..44103fae8992d9b877967e3c577914c2b07d8708 100644 (file)
@@ -8,17 +8,19 @@
  *
  */
 
-#include "libbb.h"
-
-#if ! defined CONFIG_FEATURE_USE_DEVPS_PATCH
 #include <dirent.h>
 #include <string.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <asm/page.h>
 
+#include "libbb.h"
 
-extern procps_status_t * procps_scan(int save_user_arg0)
+extern procps_status_t * procps_scan(int save_user_arg0
+#ifdef CONFIG_SELINUX
+       , int use_selinux , security_id_t *sid
+#endif
+       )
 {
        static DIR *dir;
        struct dirent *entry;
@@ -36,7 +38,7 @@ extern procps_status_t * procps_scan(int save_user_arg0)
        if (!dir) {
                dir = opendir("/proc");
                if(!dir)
-                       error_msg_and_die("Can't open /proc");
+                       bb_error_msg_and_die("Can't open /proc");
        }
        for(;;) {
                if((entry = readdir(dir)) == NULL) {
@@ -55,6 +57,14 @@ extern procps_status_t * procps_scan(int save_user_arg0)
                sprintf(status, "/proc/%d/stat", pid);
                if((fp = fopen(status, "r")) == NULL)
                        continue;
+#ifdef CONFIG_SELINUX
+               if(use_selinux)
+               {
+                       if(fstat_secure(fileno(fp), &sb, sid))
+                               continue;
+               }
+               else
+#endif
                if(fstat(fileno(fp), &sb))
                        continue;
                my_getpwuid(curstatus.user, sb.st_uid);
@@ -107,14 +117,21 @@ extern procps_status_t * procps_scan(int save_user_arg0)
 
                curstatus.rss <<= (PAGE_SHIFT - 10);     /* 2**10 = 1kb */
 
-               sprintf(status, "/proc/%d/cmdline", pid);
                if(save_user_arg0) {
+                       sprintf(status, "/proc/%d/cmdline", pid);
                        if((fp = fopen(status, "r")) == NULL)
                                continue;
-                       if(fgets(buf, sizeof(buf), fp) != NULL) {
-                               name = strchr(buf, '\n');
-                               if(name != NULL)
-                                       *name = 0;
+                       if((n=fread(buf, 1, sizeof(buf)-1, fp)) > 0) {
+                               if(buf[n-1]=='\n')
+                                       buf[--n] = 0;
+                               name = buf;
+                               while(n) {
+                                       if(((unsigned char)*name) < ' ')
+                                               *name = ' ';
+                                       name++;
+                                       n--;
+                               }
+                               *name = 0;
                                if(buf[0])
                                        curstatus.cmd = strdup(buf);
                                /* if NULL it work true also */
@@ -125,8 +142,6 @@ extern procps_status_t * procps_scan(int save_user_arg0)
        }
 }
 
-#endif /* CONFIG_FEATURE_USE_DEVPS_PATCH. Else this file is empty */
-
 /* END CODE */
 /*
 Local Variables: