From 03d79b41c71f091f61f4712963a3760fd24fdb62 Mon Sep 17 00:00:00 2001 From: Zandr Martin Date: Fri, 10 Jun 2016 06:08:59 -0500 Subject: semi-working (only non-client/server wayland apps) --- common/util.c | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'common') diff --git a/common/util.c b/common/util.c index 13397437..e760443a 100644 --- a/common/util.c +++ b/common/util.c @@ -1,6 +1,10 @@ #include - +#include +#include +#include +#include "readline.h" #include "util.h" +#include "log.h" int wrap(int i, int max) { return ((i % max) + max) % max; @@ -64,3 +68,36 @@ int get_modifier_names(const char **names, uint32_t modifier_masks) { return length; } + +pid_t get_parent_pid(pid_t child) { + pid_t parent; + char file_name[100]; + char *buffer = NULL; + char *token = NULL; + const char sep[2] = " "; + 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); + + sway_log(L_DEBUG, "buffer string is %s", buffer); + + token = strtok(buffer, sep); + + for (int i = 0; i < 3; i++) { + token = strtok(NULL, sep); + } + + parent = strtol(token, NULL, 10); + + sway_log(L_DEBUG, "found parent pid %d for child pid %d", parent, child); + + return (parent == child) ? -1 : parent; +} -- cgit v1.2.3 From 2298143d09ce8810d9772f95e1cb605fb6b08536 Mon Sep 17 00:00:00 2001 From: Zandr Martin Date: Sat, 11 Jun 2016 12:43:34 -0500 Subject: cleanup + add timeouts for pid_workspace list --- common/util.c | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) (limited to 'common') 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; } -- cgit v1.2.3