diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2023-07-22 16:24:26 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2023-07-22 16:24:26 +0200 |
| commit | 896bb1e6576fe32b754341ddf0b49fbcfd1c116a (patch) | |
| tree | dff8e0e16a344a0a27153e3f6b7b23aad4e96227 /compositor.c | |
| parent | 1b9769db524afe3deb987e95db9fe6651104ffc0 (diff) | |
make it also working on other compositors
Diffstat (limited to 'compositor.c')
| -rw-r--r-- | compositor.c | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/compositor.c b/compositor.c new file mode 100644 index 0000000..6470611 --- /dev/null +++ b/compositor.c @@ -0,0 +1,51 @@ +#define _GNU_SOURCE + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <wayland-client.h> +#include <sys/socket.h> +#include <errno.h> + +#define PROCESS_NAME_MAX_LENGTH 1024 + +char name[PROCESS_NAME_MAX_LENGTH] = ""; + +static pid_t +pid_from_fd(int fd) +{ + struct ucred ucred; + socklen_t len = sizeof(struct ucred); + if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &len) == -1) { + perror("getsockopt failed"); + exit(-1); + } + return ucred.pid; +} + +static char * +process_name_from_pid(const pid_t pid) { + char proc_buf[64]; + sprintf(proc_buf, "/proc/%d/comm", pid); + FILE *fp; + if ((fp = fopen(proc_buf, "r")) == NULL) { + fprintf(stderr, "opening '%s' failed: %s\n", proc_buf, strerror(errno)); + exit(-1); + } + if (fgets(name, PROCESS_NAME_MAX_LENGTH, fp) == NULL) { + fprintf(stderr, "reading '%s' failed\n", proc_buf); + exit(-1); + } + name[strcspn(name, "\n")] = 0; + fclose(fp); + return name; +} + +char * +process_name_from_fd(struct wl_display *display) +{ + int fd = wl_display_get_fd(display); + pid_t pid = pid_from_fd(fd); + char *process_name = process_name_from_pid(pid); + return process_name; +} |