aboutsummaryrefslogtreecommitdiffstats
path: root/common/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'common/util.c')
-rw-r--r--common/util.c31
1 files changed, 30 insertions, 1 deletions
diff --git a/common/util.c b/common/util.c
index 13397437..12cb7470 100644
--- a/common/util.c
+++ b/common/util.c
@@ -1,6 +1,10 @@
1#include <math.h> 1#include <math.h>
2 2#include <stdio.h>
3#include <string.h>
4#include <stdlib.h>
5#include "readline.h"
3#include "util.h" 6#include "util.h"
7#include "log.h"
4 8
5int wrap(int i, int max) { 9int wrap(int i, int max) {
6 return ((i % max) + max) % max; 10 return ((i % max) + max) % max;
@@ -64,3 +68,28 @@ int get_modifier_names(const char **names, uint32_t modifier_masks) {
64 68
65 return length; 69 return length;
66} 70}
71
72pid_t get_parent_pid(pid_t child) {
73 pid_t parent;
74 char file_name[100];
75 char *buffer = NULL;
76 char *token = NULL;
77 const char *sep = " ";
78 FILE *stat = NULL;
79
80 sprintf(file_name, "/proc/%d/stat", child);
81
82 if ((stat = fopen(file_name, "r")) && (buffer = read_line(stat))) {
83 fclose(stat);
84
85 token = strtok(buffer, sep); // pid
86 token = strtok(NULL, sep); // executable name
87 token = strtok(NULL, sep); // state
88 token = strtok(NULL, sep); // parent pid
89
90 parent = strtol(token, NULL, 10);
91 return (parent == child) ? -1 : parent;
92 }
93
94 return -1;
95}