aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/util.c28
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;
}