[PATCH 1/2] lib/procutils: general purpose procfs parsing functions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Davidlohr Bueso <dave@xxxxxxx>
Date: Mon, 2 May 2011 21:48:05 -0300

This patch introduces the first function in the file, proc_getthreads(), which obtains the threads for a given PID.

Signed-off-by: Davidlohr Bueso <dave@xxxxxxx>
---
 include/procutils.h |    6 ++++
 lib/Makefile.am     |    3 +-
 lib/procutils.c     |   84 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 92 insertions(+), 1 deletions(-)
 create mode 100644 include/procutils.h
 create mode 100644 lib/procutils.c

diff --git a/include/procutils.h b/include/procutils.h
new file mode 100644
index 0000000..6c0f39a
--- /dev/null
+++ b/include/procutils.h
@@ -0,0 +1,6 @@
+#ifndef UTIL_LINUX_PROCUTILS
+#define UTIL_LINUX_PROCUTILS
+
+pid_t *proc_getthreads(pid_t pid);
+
+#endif
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 2b64459..7d966ff 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -3,7 +3,7 @@ include $(top_srcdir)/config/include-Makefile.am
 AM_CPPFLAGS += -DTEST_PROGRAM
 
 noinst_PROGRAMS = test_blkdev test_ismounted test_wholedisk test_mangle \
-		  test_tt test_canonicalize test_at test_strutils
+		  test_tt test_canonicalize test_at test_strutils test_procutils
 if LINUX
 if HAVE_CPU_SET_T
 noinst_PROGRAMS += test_cpuset
@@ -16,6 +16,7 @@ test_wholedisk_SOURCES = wholedisk.c
 test_mangle_SOURCES = mangle.c
 test_at_SOURCES = at.c
 test_strutils_SOURCES = strutils.c
+test_procutils_SOURCES = procutils.c
 if LINUX
 test_cpuset_SOURCES = cpuset.c
 endif
diff --git a/lib/procutils.c b/lib/procutils.c
new file mode 100644
index 0000000..3f4e596
--- /dev/null
+++ b/lib/procutils.c
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2011 Davidlohr Bueso <dave@xxxxxxx>
+ * procutils.c: General purpose procfs parsing utilities
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Library Public License for more details.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <dirent.h>
+
+/*
+ * proc_getthreads() 
+ *    Obtain all threads for a given PID including the master thread.
+ *    Return NULL if PID doesn't exist or error.
+ */
+pid_t *proc_getthreads(pid_t pid)
+{
+	int i = 0;
+	pid_t tid, *list = NULL;
+	DIR *dir;
+	struct dirent *d;
+	char path[18]; /* lenght of /proc/<5digits-pid>/task/ */
+
+	sprintf(path, "/proc/%d/task/", pid);
+
+	dir = opendir(path);
+	if (!dir)
+		goto ret;
+
+	while ((d = readdir(dir))) {
+		if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
+			continue;
+		list = (i == 0) ? malloc(sizeof(pid_t)) : 
+			realloc(list, (i + 1) * sizeof(pid_t));
+		
+		list[i++] = (pid_t) strtol(d->d_name, (char **) NULL, 10);
+	}
+
+	list[i] = 0x0; /* end of array */
+	closedir(dir);
+ret:
+	return list;
+}
+
+#ifdef TEST_PROGRAM
+int main(int argc, char *argv[])
+{
+	int i = 0;
+	pid_t *threads, pid;
+
+	if (argc != 2) {
+		fprintf(stderr, "usage: %s <pid>\n", argv[0]);
+		exit(EXIT_FAILURE);
+	}
+
+	pid = strtol(argv[1], (char **) NULL, 10);
+	threads = proc_getthreads(pid);
+	if (!threads) {
+		fprintf(stderr, "%s: PID %d does not exist\n", argv[0], pid);
+		exit(EXIT_FAILURE);
+	}
+
+	printf("%d: ", pid);
+	while (threads[i]) {
+		printf("%d ", threads[i++]);
+	}
+	printf("\n");
+
+	free(threads);
+	return EXIT_SUCCESS;
+}
+#endif /* TEST_PROGRAM */
-- 
1.7.1




--
To unsubscribe from this list: send the line "unsubscribe util-linux" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux