aboutsummaryrefslogtreecommitdiffstats
path: root/common/util.c
diff options
context:
space:
mode:
authorLibravatar Zandr Martin <zandrmartin+git@gmail.com>2016-06-10 06:08:59 -0500
committerLibravatar Zandr Martin <zandrmartin+git@gmail.com>2016-06-10 06:08:59 -0500
commit03d79b41c71f091f61f4712963a3760fd24fdb62 (patch)
tree4f21665edd8078f60b7afacba9680672b87af597 /common/util.c
parentmessy, unfinished version (diff)
downloadsway-03d79b41c71f091f61f4712963a3760fd24fdb62.tar.gz
sway-03d79b41c71f091f61f4712963a3760fd24fdb62.tar.zst
sway-03d79b41c71f091f61f4712963a3760fd24fdb62.zip
semi-working (only non-client/server wayland apps)
Diffstat (limited to 'common/util.c')
-rw-r--r--common/util.c39
1 files changed, 38 insertions, 1 deletions
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 @@
1#include <math.h> 1#include <math.h>
2 2#include <stdio.h>
3#include <string.h>
4#include <stdlib.h>
5#include "readline.h"
3#include "util.h" 6#include "util.h"
7#include "log.h"
4 8
5int wrap(int i, int max) { 9int wrap(int i, int max) {
6 return ((i % max) + max) % max; 10 return ((i % max) + max) % max;
@@ -64,3 +68,36 @@ int get_modifier_names(const char **names, uint32_t modifier_masks) {
64 68
65 return length; 69 return length;
66} 70}
71
72pid_t get_parent_pid(pid_t child) {
73 pid_t parent;
74 char file_name[100];
75 char *buffer = NULL;
76 char *token = NULL;
77 const char sep[2] = " ";
78 FILE *stat = NULL;
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);
83
84 if (!(stat = fopen(file_name, "r")) || !(buffer = read_line(stat))) {
85 return -1;
86 }
87
88 fclose(stat);
89
90 sway_log(L_DEBUG, "buffer string is %s", buffer);
91
92 token = strtok(buffer, sep);
93
94 for (int i = 0; i < 3; i++) {
95 token = strtok(NULL, sep);
96 }
97
98 parent = strtol(token, NULL, 10);
99
100 sway_log(L_DEBUG, "found parent pid %d for child pid %d", parent, child);
101
102 return (parent == child) ? -1 : parent;
103}