Read pid_max from kernel 46/243346/2
authorHyeongsik Min <hyeongsik.min@samsung.com>
Thu, 5 Mar 2020 12:30:43 +0000 (21:30 +0900)
committerSeung-Woo Kim <sw0312.kim@samsung.com>
Tue, 8 Sep 2020 07:22:49 +0000 (07:22 +0000)
From systemd v243, 'kernel.pid_max' is bumped to 4194304 on 64-bit
system. This change reads pid_max from proc and uses it for pid
validation.

Change-Id: Ief105ef09c7d89bc443878c6d66478f9c5ccc61d
Signed-off-by: Hyeongsik Min <hyeongsik.min@samsung.com>
memps.c

diff --git a/memps.c b/memps.c
index 5d5d861..4709428 100644 (file)
--- a/memps.c
+++ b/memps.c
@@ -1,4 +1,4 @@
-/* Copyright 2014 Samsung Electronics Co., Ltd All Rights Reserved
+/* Copyright 2014-2020 Samsung Electronics Co., Ltd All Rights Reserved
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -118,6 +118,7 @@ struct geminfo {
 static int sum;
 static int verbos;
 static int use_gpu_mem_info = 0;
+static int pid_max = 0;
 
 /* reads file contents into memory */
 static char* cread(const char* path)
@@ -180,6 +181,21 @@ static inline char* cgets(char** contents)
        return NULL;
 } /* cgets */
 
+/* get pid_max value */
+static inline int get_pid_max(void)
+{
+       static const char pid_max_path[] = "/proc/sys/kernel/pid_max";
+       char* line;
+
+       line = cread(pid_max_path);
+       if (line == NULL) {
+               fprintf(stderr, "cannot open %s\n", pid_max_path);
+               return 0;
+       }
+
+       return strtoul(line, NULL, 10);
+}
+
 
 static unsigned get_peak_rss(unsigned int pid)
 {
@@ -1002,7 +1018,7 @@ static void show_rss(int output_type, char *output_path)
        errno = 0;
        while ((curdir = readdir(pDir)) != NULL && !errno) {
                pid = atoi(curdir->d_name);
-               if (pid < 1 || pid > 32768 || pid == getpid())
+               if (pid < 1 || pid > pid_max || pid == getpid())
                        continue;
 
                if (get_cmdline(pid, cmdline) < 0)
@@ -1100,7 +1116,7 @@ static int show_map_all_new(int output_type, char *output_path)
        errno = 0;
        while ((curdir = readdir(pDir)) != NULL && !errno) {
                pid = atoi(curdir->d_name);
-               if (pid < 1 || pid > 32768 || pid == getpid())
+               if (pid < 1 || pid > pid_max || pid == getpid())
                        continue;
 
                if (get_cmdline(pid, cmdline) < 0)
@@ -1346,6 +1362,8 @@ int main(int argc, char *argv[])
        int usage = 1;
        sum = 0;
 
+       pid_max = get_pid_max();
+
        if (argc > 1) {
                if (!strncmp(argv[1], "-r", strlen("-r")+1)) {
                        if (argc >= 3)