summaryrefslogtreecommitdiff
path: root/src/procfs.c
diff options
context:
space:
mode:
authorLukasz Stanislawski <l.stanislaws@samsung.com>2018-07-11 20:02:29 +0200
committerLukasz Stanislawski <l.stanislaws@samsung.com>2018-07-16 16:32:28 +0200
commitd9cdf0617f87029e9525479388a13b1a844b488f (patch)
tree5f77ee6ee19f22691c836afc910301eefb22b080 /src/procfs.c
parent6470182364f780033db8fa401ad5b3da31251d06 (diff)
downloadttsd-worker-task-d9cdf0617f87029e9525479388a13b1a844b488f.tar.gz
ttsd-worker-task-d9cdf0617f87029e9525479388a13b1a844b488f.tar.bz2
ttsd-worker-task-d9cdf0617f87029e9525479388a13b1a844b488f.zip
report-generator: implement top report generator
This patch adds additional objects: proc-scanner - for scanning whole /proc/ dir process - to gather information from /proc/<pid>/* Change-Id: Ic0ad58fb9a0ae59e9390b3d109440d91d25e90da
Diffstat (limited to 'src/procfs.c')
-rw-r--r--src/procfs.c79
1 files changed, 22 insertions, 57 deletions
diff --git a/src/procfs.c b/src/procfs.c
index 2e64900..1296809 100644
--- a/src/procfs.c
+++ b/src/procfs.c
@@ -19,6 +19,7 @@
#include <dirent.h>
#include <stdlib.h>
#include <unistd.h>
+#include <ctype.h>
#include "procfs.h"
#include "log.h"
@@ -35,12 +36,6 @@
#define PROC_PID_EXE_PATH "/proc/%d/exe"
#define PROC_PID_CMDLINE_PATH "/proc/%d/cmdline"
-struct procfs_pid_iterator
-{
- DIR *dir;
- int current_pid;
-};
-
int procfs_read_system_load_average(struct procfs_load_average_info *info)
{
float a1, a5, a15;
@@ -227,6 +222,7 @@ int procfs_read_process_cpu_usage(int pid, struct procfs_process_cpu_usage_info
usage->utime = utime;
usage->stime = stime;
+ fclose(stat_fp);
return 0;
}
@@ -283,6 +279,9 @@ bool _procfs_dirname_parse_pid(const char *dirname, int *pid)
{
int parsed_pid;
+ if (!isdigit(*dirname))
+ return false;
+
if (sscanf(dirname, "%d", &parsed_pid) != 1) {
return false;
}
@@ -290,68 +289,34 @@ bool _procfs_dirname_parse_pid(const char *dirname, int *pid)
return true;
}
-/**
- * @brief returns true if pid_iterator could successfully read
- * next pid from /proc directory, false otherwise
- */
-bool _procfs_pid_iterator_next_internal(procfs_pid_iterator_t *iter)
+int procfs_iterate_pids(procfs_pid_iterator_cb iterator, void *user_data)
{
+ ON_NULL_RETURN_VAL(iterator, -1);
+
struct dirent *entry;
int pid;
- bool ret = false;
+
+ DIR *dir = opendir(PROC_DIR_PATH);
+ if (!dir) {
+ ERR("opendir failed.");
+ return -1;
+ }
// According to POSIX docs readdir is not-thread safe.
// however in glib recent implementations readdir
// is thread safe, so we can avoid using locks here.
- while ((entry = readdir(iter->dir)) != NULL) {
- if (_procfs_dirname_parse_pid(entry->d_name, &pid)) {
- iter->current_pid = pid;
- ret = true;
- break;
- } else {
+ while ((entry = readdir(dir)) != NULL) {
+ if (!_procfs_dirname_parse_pid(entry->d_name, &pid))
continue;
- }
- }
-
- return ret;
-}
-procfs_pid_iterator_t *procfs_get_pid_iterator()
-{
- procfs_pid_iterator_t *ret = calloc(1, sizeof(struct procfs_pid_iterator));
- if (!ret) {
- ERR("calloc failed.");
- return NULL;
- }
- ret->dir = opendir(PROC_DIR_PATH);
- if (!ret->dir) {
- ERR("opendir failed.");
- procfs_pid_iterator_free(ret);
- return NULL;
- }
- if (!_procfs_pid_iterator_next_internal(ret)) {
- ERR("_procfs_pid_iterator_next_internal failed");
- procfs_pid_iterator_free(ret);
- return NULL;
+ if (iterator(pid, user_data))
+ continue;
+ else
+ break;;
}
- return ret;
-}
-
-bool procfs_pid_iterator_next(procfs_pid_iterator_t *iterator)
-{
- return _procfs_pid_iterator_next_internal(iterator);
-}
-int procfs_pid_iterator_get_pid(procfs_pid_iterator_t *iterator)
-{
- return iterator->current_pid;
-}
-
-void procfs_pid_iterator_free(procfs_pid_iterator_t *iterator)
-{
- if (!iterator) return;
- if (iterator->dir) closedir(iterator->dir);
- free(iterator);
+ closedir(dir);
+ return 0;
}
int procfs_read_exe(int pid, char **exe)