diff --git a/src/libeis-socket.c b/src/libeis-socket.c index 0b53f1a..915360d 100644 --- a/src/libeis-socket.c +++ b/src/libeis-socket.c @@ -180,3 +180,20 @@ eis_setup_backend_socket(struct eis *eis, const char *socketpath) 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; +} diff --git a/src/libeis.h b/src/libeis.h index 4654db4..6a0600a 100644 --- a/src/libeis.h +++ b/src/libeis.h @@ -32,6 +32,7 @@ extern "C" { #include #include #include +#include /** * @defgroup libeis 🍦 EIS - The server API @@ -535,6 +536,18 @@ eis_backend_fd_add_client(struct eis *ctx); int 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 * monitored for events by the caller's mainloop, e.g. using select(). When