mirror of
https://gitlab.freedesktop.org/wayland/wayland.git
synced 2026-04-07 08:21:33 -04:00
Support reading ucred from the socket on FreeBSD
On FreeBSD we have to use getsockopt(fd, SOL_LOCAL, LOCAL_PEERCRED) instead. This change is based on a downstream patch in FreeBSD ports. Co-authored-by: Greg V <greg@unrelenting.technology> Co-authored-by: Koop Mast <kwm@rainbow-runner.nl> Signed-off-by: Alex Richardson <Alexander.Richardson@cl.cam.ac.uk>
This commit is contained in:
parent
f1552700ce
commit
54b237a612
4 changed files with 63 additions and 11 deletions
|
|
@ -26,7 +26,7 @@ add_project_arguments(
|
||||||
language: 'c'
|
language: 'c'
|
||||||
)
|
)
|
||||||
|
|
||||||
foreach h: [ 'sys/prctl.h' ]
|
foreach h: [ 'sys/prctl.h', 'sys/ucred.h' ]
|
||||||
config_h.set('HAVE_' + h.underscorify().to_upper(), cc.has_header(h))
|
config_h.set('HAVE_' + h.underscorify().to_upper(), cc.has_header(h))
|
||||||
endforeach
|
endforeach
|
||||||
|
|
||||||
|
|
@ -41,6 +41,7 @@ have_funcs = [
|
||||||
foreach f: have_funcs
|
foreach f: have_funcs
|
||||||
config_h.set('HAVE_' + f.underscorify().to_upper(), cc.has_function(f))
|
config_h.set('HAVE_' + f.underscorify().to_upper(), cc.has_function(f))
|
||||||
endforeach
|
endforeach
|
||||||
|
config_h.set10('HAVE_XUCRED_CR_PID', cc.has_member('struct xucred', 'cr_pid', prefix : '#include <sys/ucred.h>'))
|
||||||
|
|
||||||
if get_option('libraries')
|
if get_option('libraries')
|
||||||
if host_machine.system() == 'freebsd'
|
if host_machine.system() == 'freebsd'
|
||||||
|
|
|
||||||
|
|
@ -25,14 +25,19 @@
|
||||||
|
|
||||||
#define _GNU_SOURCE
|
#define _GNU_SOURCE
|
||||||
|
|
||||||
|
#include "../config.h"
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <sys/epoll.h>
|
#include <sys/epoll.h>
|
||||||
|
#include <sys/un.h>
|
||||||
|
#ifdef HAVE_SYS_UCRED_H
|
||||||
|
#include <sys/ucred.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "../config.h"
|
|
||||||
#include "wayland-os.h"
|
#include "wayland-os.h"
|
||||||
|
|
||||||
static int
|
static int
|
||||||
|
|
@ -72,6 +77,46 @@ wl_os_socket_cloexec(int domain, int type, int protocol)
|
||||||
return set_cloexec_or_close(fd);
|
return set_cloexec_or_close(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if defined(__FreeBSD__)
|
||||||
|
int
|
||||||
|
wl_os_socket_peercred(int sockfd, uid_t *uid, gid_t *gid, pid_t *pid)
|
||||||
|
{
|
||||||
|
socklen_t len;
|
||||||
|
struct xucred ucred;
|
||||||
|
|
||||||
|
len = sizeof(ucred);
|
||||||
|
if (getsockopt(sockfd, SOL_LOCAL, LOCAL_PEERCRED, &ucred, &len) < 0 ||
|
||||||
|
ucred.cr_version != XUCRED_VERSION)
|
||||||
|
return -1;
|
||||||
|
*uid = ucred.cr_uid;
|
||||||
|
*gid = ucred.cr_gid;
|
||||||
|
#if HAVE_XUCRED_CR_PID
|
||||||
|
/* Since https://cgit.freebsd.org/src/commit/?id=c5afec6e895a */
|
||||||
|
*pid = ucred.cr_pid;
|
||||||
|
#else
|
||||||
|
*pid = 0;
|
||||||
|
#endif
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#elif defined(SO_PEERCRED)
|
||||||
|
int
|
||||||
|
wl_os_socket_peercred(int sockfd, uid_t *uid, gid_t *gid, pid_t *pid)
|
||||||
|
{
|
||||||
|
socklen_t len;
|
||||||
|
struct ucred ucred;
|
||||||
|
|
||||||
|
len = sizeof(ucred);
|
||||||
|
if (getsockopt(sockfd, SOL_SOCKET, SO_PEERCRED, &ucred, &len) < 0)
|
||||||
|
return -1;
|
||||||
|
*uid = ucred.uid;
|
||||||
|
*gid = ucred.gid;
|
||||||
|
*pid = ucred.pid;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
#error "Don't know how to read ucred on this platform"
|
||||||
|
#endif
|
||||||
|
|
||||||
int
|
int
|
||||||
wl_os_dupfd_cloexec(int fd, int minfd)
|
wl_os_dupfd_cloexec(int fd, int minfd)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -26,9 +26,15 @@
|
||||||
#ifndef WAYLAND_OS_H
|
#ifndef WAYLAND_OS_H
|
||||||
#define WAYLAND_OS_H
|
#define WAYLAND_OS_H
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
|
||||||
int
|
int
|
||||||
wl_os_socket_cloexec(int domain, int type, int protocol);
|
wl_os_socket_cloexec(int domain, int type, int protocol);
|
||||||
|
|
||||||
|
int
|
||||||
|
wl_os_socket_peercred(int sockfd, uid_t *uid, gid_t *gid, pid_t *pid);
|
||||||
|
|
||||||
int
|
int
|
||||||
wl_os_dupfd_cloexec(int fd, int minfd);
|
wl_os_dupfd_cloexec(int fd, int minfd);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -78,7 +78,9 @@ struct wl_client {
|
||||||
struct wl_list link;
|
struct wl_list link;
|
||||||
struct wl_map objects;
|
struct wl_map objects;
|
||||||
struct wl_priv_signal destroy_signal;
|
struct wl_priv_signal destroy_signal;
|
||||||
struct ucred ucred;
|
pid_t pid;
|
||||||
|
uid_t uid;
|
||||||
|
gid_t gid;
|
||||||
int error;
|
int error;
|
||||||
struct wl_priv_signal resource_created_signal;
|
struct wl_priv_signal resource_created_signal;
|
||||||
};
|
};
|
||||||
|
|
@ -314,7 +316,7 @@ wl_resource_post_error(struct wl_resource *resource,
|
||||||
static void
|
static void
|
||||||
destroy_client_with_error(struct wl_client *client, const char *reason)
|
destroy_client_with_error(struct wl_client *client, const char *reason)
|
||||||
{
|
{
|
||||||
wl_log("%s (pid %u)\n", reason, client->ucred.pid);
|
wl_log("%s (pid %u)\n", reason, client->pid);
|
||||||
wl_client_destroy(client);
|
wl_client_destroy(client);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -513,7 +515,6 @@ WL_EXPORT struct wl_client *
|
||||||
wl_client_create(struct wl_display *display, int fd)
|
wl_client_create(struct wl_display *display, int fd)
|
||||||
{
|
{
|
||||||
struct wl_client *client;
|
struct wl_client *client;
|
||||||
socklen_t len;
|
|
||||||
|
|
||||||
client = zalloc(sizeof *client);
|
client = zalloc(sizeof *client);
|
||||||
if (client == NULL)
|
if (client == NULL)
|
||||||
|
|
@ -528,9 +529,8 @@ wl_client_create(struct wl_display *display, int fd)
|
||||||
if (!client->source)
|
if (!client->source)
|
||||||
goto err_client;
|
goto err_client;
|
||||||
|
|
||||||
len = sizeof client->ucred;
|
if (wl_os_socket_peercred(fd, &client->uid, &client->gid,
|
||||||
if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED,
|
&client->pid) != 0)
|
||||||
&client->ucred, &len) < 0)
|
|
||||||
goto err_source;
|
goto err_source;
|
||||||
|
|
||||||
client->connection = wl_connection_create(fd);
|
client->connection = wl_connection_create(fd);
|
||||||
|
|
@ -586,11 +586,11 @@ wl_client_get_credentials(struct wl_client *client,
|
||||||
pid_t *pid, uid_t *uid, gid_t *gid)
|
pid_t *pid, uid_t *uid, gid_t *gid)
|
||||||
{
|
{
|
||||||
if (pid)
|
if (pid)
|
||||||
*pid = client->ucred.pid;
|
*pid = client->pid;
|
||||||
if (uid)
|
if (uid)
|
||||||
*uid = client->ucred.uid;
|
*uid = client->uid;
|
||||||
if (gid)
|
if (gid)
|
||||||
*gid = client->ucred.gid;
|
*gid = client->gid;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get the file descriptor for the client
|
/** Get the file descriptor for the client
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue