public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH v3 1/1] lib/tst_pid.c: Count used pid by traversing /proc
@ 2023-02-19  8:52 Leo Yu-Chi Liang
  2023-02-20 16:44 ` Cyril Hrubis
  0 siblings, 1 reply; 3+ messages in thread
From: Leo Yu-Chi Liang @ 2023-02-19  8:52 UTC (permalink / raw)
  To: ltp; +Cc: peterlin

Calling shell code from C programs makes things less predictable
and possibly unstable, so we modify the calculating process to
plain C code.

Suggested-by: Petr Vorel <pvorel@suse.cz>
Suggested-by: Cyril Hrubis <chrubis@suse.cz>
Signed-off-by: Leo Yu-Chi Liang <ycliang@andestech.com>
---
 lib/tst_pid.c | 37 +++++++++++++++++++++++--------------
 1 file changed, 23 insertions(+), 14 deletions(-)

diff --git a/lib/tst_pid.c b/lib/tst_pid.c
index 5595e79bd..546526113 100644
--- a/lib/tst_pid.c
+++ b/lib/tst_pid.c
@@ -18,6 +18,7 @@
  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <limits.h>
@@ -111,23 +112,31 @@ static int get_session_pids_limit(void (*cleanup_fn) (void))
 	return max_pids;
 }
 
-int tst_get_free_pids_(void (*cleanup_fn) (void))
+static int get_used_pids(void (*cleanup_fn) (void))
 {
-	FILE *f;
-	int rc, used_pids, max_pids, max_session_pids, max_threads;
-
-	f = popen("ps -eT | wc -l", "r");
-	if (!f) {
-		tst_brkm(TBROK, cleanup_fn, "Could not run 'ps' to calculate used pids");
-		return -1;
+	DIR *dir_proc;
+	struct dirent *ent;
+	char status_path[PATH_MAX];
+	int used_thread, used_pids = 0;
+
+	dir_proc = SAFE_OPENDIR("/proc");
+
+	while ((ent = SAFE_READDIR(dir_proc))) {
+		if (isdigit(ent->d_name[0])) {
+			snprintf(status_path, sizeof(status_path), "/proc/%s/status", ent->d_name);
+			if (!FILE_LINES_SCANF(cleanup_fn, status_path, "Threads: %d", &used_threads))
+				used_pids += used_threads;
+		}
 	}
-	rc = fscanf(f, "%i", &used_pids);
-	pclose(f);
 
-	if (rc != 1 || used_pids < 0) {
-		tst_brkm(TBROK, cleanup_fn, "Could not read output of 'ps' to calculate used pids");
-		return -1;
-	}
+	SAFE_CLOSEDIR(dir_proc);
+
+	return used_pids;
+}
+
+int tst_get_free_pids_(void (*cleanup_fn) (void))
+{
+	int max_pids, max_session_pids, max_threads, used_pids = get_used_pids(cleanup_fn);
 
 	SAFE_FILE_SCANF(cleanup_fn, PID_MAX_PATH, "%d", &max_pids);
 	SAFE_FILE_SCANF(cleanup_fn, THREADS_MAX_PATH, "%d", &max_threads);
-- 
2.34.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [LTP] [PATCH v3 1/1] lib/tst_pid.c: Count used pid by traversing /proc
  2023-02-19  8:52 [LTP] [PATCH v3 1/1] lib/tst_pid.c: Count used pid by traversing /proc Leo Yu-Chi Liang
@ 2023-02-20 16:44 ` Cyril Hrubis
  2023-02-21 13:25   ` Petr Vorel
  0 siblings, 1 reply; 3+ messages in thread
From: Cyril Hrubis @ 2023-02-20 16:44 UTC (permalink / raw)
  To: Leo Yu-Chi Liang; +Cc: peterlin, ltp

Hi!
Reviewed-by: Cyril Hrubis <chrubis@suse.cz>

-- 
Cyril Hrubis
chrubis@suse.cz

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [LTP] [PATCH v3 1/1] lib/tst_pid.c: Count used pid by traversing /proc
  2023-02-20 16:44 ` Cyril Hrubis
@ 2023-02-21 13:25   ` Petr Vorel
  0 siblings, 0 replies; 3+ messages in thread
From: Petr Vorel @ 2023-02-21 13:25 UTC (permalink / raw)
  To: Cyril Hrubis; +Cc: peterlin, ltp

Hi all,

fixed typo in variable name and merged.
Thanks!

Kind regards,
Petr

+++ lib/tst_pid.c
@@ -117,7 +117,7 @@ static int get_used_pids(void (*cleanup_fn) (void))
 	DIR *dir_proc;
 	struct dirent *ent;
 	char status_path[PATH_MAX];
-	int used_thread, used_pids = 0;
+	int used_threads, used_pids = 0;
 
 	dir_proc = SAFE_OPENDIR("/proc");
 

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-02-21 13:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-02-19  8:52 [LTP] [PATCH v3 1/1] lib/tst_pid.c: Count used pid by traversing /proc Leo Yu-Chi Liang
2023-02-20 16:44 ` Cyril Hrubis
2023-02-21 13:25   ` Petr Vorel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox