diff options
author | Zandr Martin <zandrmartin+git@gmail.com> | 2016-06-11 12:43:34 -0500 |
---|---|---|
committer | Zandr Martin <zandrmartin+git@gmail.com> | 2016-06-11 12:43:34 -0500 |
commit | 2298143d09ce8810d9772f95e1cb605fb6b08536 (patch) | |
tree | d1b4fb33848d09c71602005c57919a903ce3d72d /common | |
parent | 66caee645cc276bf747ae492df02c08d978ee90d (diff) |
cleanup + add timeouts for pid_workspace list
Diffstat (limited to 'common')
-rw-r--r-- | common/util.c | 28 |
1 files changed, 10 insertions, 18 deletions
diff --git a/common/util.c b/common/util.c index e760443a..12cb7470 100644 --- a/common/util.c +++ b/common/util.c @@ -74,30 +74,22 @@ pid_t get_parent_pid(pid_t child) { char file_name[100]; char *buffer = NULL; char *token = NULL; - const char sep[2] = " "; + const char *sep = " "; FILE *stat = NULL; - sway_log(L_DEBUG, "trying to get parent pid for child pid %d", child); - sprintf(file_name, "/proc/%d/stat", child); - if (!(stat = fopen(file_name, "r")) || !(buffer = read_line(stat))) { - return -1; - } - - fclose(stat); + if ((stat = fopen(file_name, "r")) && (buffer = read_line(stat))) { + fclose(stat); - sway_log(L_DEBUG, "buffer string is %s", buffer); + token = strtok(buffer, sep); // pid + token = strtok(NULL, sep); // executable name + token = strtok(NULL, sep); // state + token = strtok(NULL, sep); // parent pid - token = strtok(buffer, sep); - - for (int i = 0; i < 3; i++) { - token = strtok(NULL, sep); + parent = strtol(token, NULL, 10); + return (parent == child) ? -1 : parent; } - parent = strtol(token, NULL, 10); - - sway_log(L_DEBUG, "found parent pid %d for child pid %d", parent, child); - - return (parent == child) ? -1 : parent; + return -1; } |