From 80321064ad85dace3e9b8672c59f2aa30f4b1f57 Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Thu, 5 Mar 2020 21:30:43 +0900 Subject: [PATCH] Read pid_max from kernel 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 --- memps.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/memps.c b/memps.c index 5d5d861..4709428 100644 --- 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) -- 2.7.4