fix printf warning
[oweals/busybox.git] / libbb / procps.c
index eea6aa681a1037b45f62421a6a3b20ea9bfd7a46..e405fb7efcc1ce5f3efd03b2701b11cfecdce610 100644 (file)
@@ -8,7 +8,6 @@
  *
  */
 
-#if ! defined CONFIG_FEATURE_USE_DEVPS_PATCH
 #include <dirent.h>
 #include <string.h>
 #include <stdlib.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;
@@ -35,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) {
@@ -51,12 +54,22 @@ extern procps_status_t * procps_scan(int save_user_arg0)
                pid = atoi(name);
                curstatus.pid = pid;
 
+               sprintf(status, "/proc/%d", pid);
+               if(stat(status, &sb))
+                       continue;
+               my_getpwuid(curstatus.user, sb.st_uid, sizeof(curstatus.user));
+
                sprintf(status, "/proc/%d/stat", pid);
                if((fp = fopen(status, "r")) == NULL)
                        continue;
-               if(fstat(fileno(fp), &sb))
-                       continue;
-               my_getpwuid(curstatus.user, sb.st_uid);
+#ifdef CONFIG_SELINUX
+               if(use_selinux)
+               {
+                       if(fstat_secure(fileno(fp), &sb, sid))
+                               continue;
+               }
+               else
+#endif
                name = fgets(buf, sizeof(buf), fp);
                fclose(fp);
                if(name == NULL)
@@ -104,16 +117,27 @@ extern procps_status_t * procps_scan(int save_user_arg0)
                else
                        curstatus.state[2] = ' ';
 
+#ifdef PAGE_SHIFT
                curstatus.rss <<= (PAGE_SHIFT - 10);     /* 2**10 = 1kb */
+#else
+               curstatus.rss *= (getpagesize() >> 10);     /* 2**10 = 1kb */
+#endif
 
-               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 */
@@ -124,8 +148,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: