#define _GNU_SOURCE #include #include #include #include #include #include #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; }