diff options
author | Zandr Martin <zandrmartin@users.noreply.github.com> | 2016-06-11 17:10:38 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-06-11 17:10:38 -0500 |
commit | a6f4bf60f2a6b9aa2ae5cadee5c1aada2ff1f6b3 (patch) | |
tree | 978577aae4b59ae33012fb8c181ab61b82279a97 /common/util.c | |
parent | 889618d3eeb5786df45e1e4885debe610932fc31 (diff) | |
parent | f1268053a62a83c0169b61451d01649a3f3185e3 (diff) |
Merge branch 'master' into set-size-command
Diffstat (limited to 'common/util.c')
-rw-r--r-- | common/util.c | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/common/util.c b/common/util.c index 12cb7470..31a75a9b 100644 --- a/common/util.c +++ b/common/util.c @@ -70,7 +70,7 @@ int get_modifier_names(const char **names, uint32_t modifier_masks) { } pid_t get_parent_pid(pid_t child) { - pid_t parent; + pid_t parent = -1; char file_name[100]; char *buffer = NULL; char *token = NULL; @@ -79,15 +79,19 @@ pid_t get_parent_pid(pid_t child) { sprintf(file_name, "/proc/%d/stat", child); - if ((stat = fopen(file_name, "r")) && (buffer = read_line(stat))) { - fclose(stat); + if ((stat = fopen(file_name, "r"))) { + if ((buffer = read_line(stat))) { + token = strtok(buffer, sep); // pid + token = strtok(NULL, sep); // executable name + token = strtok(NULL, sep); // state + token = strtok(NULL, sep); // parent pid + parent = strtol(token, NULL, 10); + } - token = strtok(buffer, sep); // pid - token = strtok(NULL, sep); // executable name - token = strtok(NULL, sep); // state - token = strtok(NULL, sep); // parent pid + fclose(stat); + } - parent = strtol(token, NULL, 10); + if (parent) { return (parent == child) ? -1 : parent; } |