Make it possible to fetch the pid of a client in socket mode

This is useful to get some more info about clients that try to
connect.

Part-of: <https://gitlab.freedesktop.org/libinput/libei/-/merge_requests/339>
This commit is contained in:
David Redondo 2025-05-21 16:33:11 +02:00
parent 54e71e6dd5
commit 70cfc6eed2
2 changed files with 30 additions and 0 deletions

View file

@ -180,3 +180,20 @@ eis_setup_backend_socket(struct eis *eis, const char *socketpath)
return rc; return rc;
} }
_public_ pid_t
eis_backend_socket_get_client_pid(struct eis_client* client)
{
struct eis *eis = eis_client_get_context(client);
if (eis->backend_interface.destroy != interface_socket_destroy) {
log_bug_client(eis, "Not a socket backend");
return -EINVAL;
}
struct ucred ucred;
socklen_t len = sizeof(ucred);
int rc = getsockopt(source_get_fd(client->source), SOL_SOCKET, SO_PEERCRED, &ucred, &len);
if (rc < 0) {
return -errno;
}
return ucred.pid;
}

View file

@ -32,6 +32,7 @@ extern "C" {
#include <stddef.h> #include <stddef.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <sys/types.h>
/** /**
* @defgroup libeis 🍦 EIS - The server API * @defgroup libeis 🍦 EIS - The server API
@ -535,6 +536,18 @@ eis_backend_fd_add_client(struct eis *ctx);
int int
eis_setup_backend_socket(struct eis *ctx, const char *path); eis_setup_backend_socket(struct eis *ctx, const char *path);
/**
* Return the pid of the client.
* The pid is retrieved via SO_PEERCRED.
* This function should only be called if the context was set up via
* eis_setup_backend_socket.
*
* @return The PID of the client or a negative errno on failure
*/
pid_t
eis_backend_socket_get_client_pid(struct eis_client *client);
/** /**
* libeis keeps a single file descriptor for all events. This fd should be * libeis keeps a single file descriptor for all events. This fd should be
* monitored for events by the caller's mainloop, e.g. using select(). When * monitored for events by the caller's mainloop, e.g. using select(). When