diff options
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) { | |||
74 | char file_name[100]; | 74 | char file_name[100]; |
75 | char *buffer = NULL; | 75 | char *buffer = NULL; |
76 | char *token = NULL; | 76 | char *token = NULL; |
77 | const char sep[2] = " "; | 77 | const char *sep = " "; |
78 | FILE *stat = NULL; | 78 | FILE *stat = NULL; |
79 | 79 | ||
80 | sway_log(L_DEBUG, "trying to get parent pid for child pid %d", child); | ||
81 | |||
82 | sprintf(file_name, "/proc/%d/stat", child); | 80 | sprintf(file_name, "/proc/%d/stat", child); |
83 | 81 | ||
84 | if (!(stat = fopen(file_name, "r")) || !(buffer = read_line(stat))) { | 82 | if ((stat = fopen(file_name, "r")) && (buffer = read_line(stat))) { |
85 | return -1; | 83 | fclose(stat); |
86 | } | ||
87 | |||
88 | fclose(stat); | ||
89 | 84 | ||
90 | sway_log(L_DEBUG, "buffer string is %s", buffer); | 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 | ||
91 | 89 | ||
92 | token = strtok(buffer, sep); | 90 | parent = strtol(token, NULL, 10); |
93 | 91 | return (parent == child) ? -1 : parent; | |
94 | for (int i = 0; i < 3; i++) { | ||
95 | token = strtok(NULL, sep); | ||
96 | } | 92 | } |
97 | 93 | ||
98 | parent = strtol(token, NULL, 10); | 94 | return -1; |
99 | |||
100 | sway_log(L_DEBUG, "found parent pid %d for child pid %d", parent, child); | ||
101 | |||
102 | return (parent == child) ? -1 : parent; | ||
103 | } | 95 | } |