mirror of
https://gitlab.freedesktop.org/wayland/wayland.git
synced 2026-05-02 06:46:26 -04:00
os: Provide wl_os_socketpair_cloexec wrapper for systems without SOCK_CLOEXEC
Reviewed-by: Jeremy Huddleston Sequoia <jeremyhu@apple.com> Signed-off-by: Weijia Wang <contact@weijia.wang>
This commit is contained in:
parent
d8e90f4a16
commit
46c205d08b
6 changed files with 50 additions and 12 deletions
|
|
@ -87,6 +87,34 @@ wl_os_socket_cloexec(int domain, int type, int protocol)
|
||||||
return set_cloexec_or_close(fd);
|
return set_cloexec_or_close(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
wl_os_socketpair_cloexec(int domain, int type, int protocol, int sv[2])
|
||||||
|
{
|
||||||
|
int retval;
|
||||||
|
|
||||||
|
#ifdef SOCK_CLOEXEC
|
||||||
|
retval = socketpair(domain, type | SOCK_CLOEXEC, protocol, sv);
|
||||||
|
if (retval >= 0)
|
||||||
|
return retval;
|
||||||
|
if (errno != EINVAL)
|
||||||
|
return -1;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
retval = socketpair(domain, type, protocol, sv);
|
||||||
|
if (retval < 0)
|
||||||
|
return retval;
|
||||||
|
|
||||||
|
sv[0] = set_cloexec_or_close(sv[0]);
|
||||||
|
sv[1] = set_cloexec_or_close(sv[1]);
|
||||||
|
if (sv[0] == -1 || sv[1] == -1) {
|
||||||
|
close(sv[0]);
|
||||||
|
close(sv[1]);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
#if defined(__FreeBSD__)
|
#if defined(__FreeBSD__)
|
||||||
int
|
int
|
||||||
wl_os_socket_peercred(int sockfd, uid_t *uid, gid_t *gid, pid_t *pid)
|
wl_os_socket_peercred(int sockfd, uid_t *uid, gid_t *gid, pid_t *pid)
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,9 @@
|
||||||
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_socketpair_cloexec(int domain, int type, int protocol, int sv[2]);
|
||||||
|
|
||||||
int
|
int
|
||||||
wl_os_socket_peercred(int sockfd, uid_t *uid, gid_t *gid, pid_t *pid);
|
wl_os_socket_peercred(int sockfd, uid_t *uid, gid_t *gid, pid_t *pid);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,7 @@
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
#include "wayland-os.h"
|
||||||
#include "wayland-private.h"
|
#include "wayland-private.h"
|
||||||
#include "wayland-server.h"
|
#include "wayland-server.h"
|
||||||
#include "test-runner.h"
|
#include "test-runner.h"
|
||||||
|
|
@ -97,7 +98,7 @@ TEST(client_destroy_listener)
|
||||||
bool user_data_destroyed = false;
|
bool user_data_destroyed = false;
|
||||||
int s[2];
|
int s[2];
|
||||||
|
|
||||||
assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0);
|
assert(wl_os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, s) == 0);
|
||||||
display = wl_display_create();
|
display = wl_display_create();
|
||||||
assert(display);
|
assert(display);
|
||||||
client = wl_client_create(display, s[0]);
|
client = wl_client_create(display, s[0]);
|
||||||
|
|
@ -184,7 +185,7 @@ TEST(client_destroy_removes_link)
|
||||||
struct client_destroy_listener destroy_listener;
|
struct client_destroy_listener destroy_listener;
|
||||||
int s[2];
|
int s[2];
|
||||||
|
|
||||||
assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0);
|
assert(wl_os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, s) == 0);
|
||||||
display = wl_display_create();
|
display = wl_display_create();
|
||||||
assert(display);
|
assert(display);
|
||||||
client = wl_client_create(display, s[0]);
|
client = wl_client_create(display, s[0]);
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,7 @@
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <poll.h>
|
#include <poll.h>
|
||||||
|
|
||||||
|
#include "wayland-os.h"
|
||||||
#include "wayland-private.h"
|
#include "wayland-private.h"
|
||||||
#include "test-runner.h"
|
#include "test-runner.h"
|
||||||
#include "test-compositor.h"
|
#include "test-compositor.h"
|
||||||
|
|
@ -48,7 +49,7 @@ setup(int *s)
|
||||||
{
|
{
|
||||||
struct wl_connection *connection;
|
struct wl_connection *connection;
|
||||||
|
|
||||||
assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0);
|
assert(wl_os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, s) == 0);
|
||||||
|
|
||||||
connection = wl_connection_create(s[0], WL_BUFFER_DEFAULT_MAX_SIZE);
|
connection = wl_connection_create(s[0], WL_BUFFER_DEFAULT_MAX_SIZE);
|
||||||
assert(connection);
|
assert(connection);
|
||||||
|
|
@ -181,8 +182,8 @@ struct marshal_data {
|
||||||
static void
|
static void
|
||||||
setup_marshal_data(struct marshal_data *data)
|
setup_marshal_data(struct marshal_data *data)
|
||||||
{
|
{
|
||||||
assert(socketpair(AF_UNIX,
|
assert(wl_os_socketpair_cloexec(AF_UNIX,
|
||||||
SOCK_STREAM | SOCK_CLOEXEC, 0, data->s) == 0);
|
SOCK_STREAM, 0, data->s) == 0);
|
||||||
data->read_connection = wl_connection_create(data->s[0],
|
data->read_connection = wl_connection_create(data->s[0],
|
||||||
WL_BUFFER_DEFAULT_MAX_SIZE);
|
WL_BUFFER_DEFAULT_MAX_SIZE);
|
||||||
assert(data->read_connection);
|
assert(data->read_connection);
|
||||||
|
|
@ -885,7 +886,7 @@ TEST(request_bogus_size)
|
||||||
for (bogus_size = 11; bogus_size >= 0; bogus_size--) {
|
for (bogus_size = 11; bogus_size >= 0; bogus_size--) {
|
||||||
fprintf(stderr, "* bogus size %d\n", bogus_size);
|
fprintf(stderr, "* bogus size %d\n", bogus_size);
|
||||||
|
|
||||||
assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0);
|
assert(wl_os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, s) == 0);
|
||||||
display = wl_display_create();
|
display = wl_display_create();
|
||||||
assert(display);
|
assert(display);
|
||||||
client = wl_client_create(display, s[0]);
|
client = wl_client_create(display, s[0]);
|
||||||
|
|
|
||||||
|
|
@ -166,7 +166,11 @@ do_os_wrappers_socket_cloexec(int n)
|
||||||
* Must have 2 calls if falling back, but must also allow
|
* Must have 2 calls if falling back, but must also allow
|
||||||
* falling back without a forced fallback.
|
* falling back without a forced fallback.
|
||||||
*/
|
*/
|
||||||
|
#ifdef SOCK_CLOEXEC
|
||||||
assert(wrapped_calls_socket > n);
|
assert(wrapped_calls_socket > n);
|
||||||
|
#else
|
||||||
|
assert(wrapped_calls_socket == 1);
|
||||||
|
#endif
|
||||||
|
|
||||||
exec_fd_leak_check(nr_fds);
|
exec_fd_leak_check(nr_fds);
|
||||||
}
|
}
|
||||||
|
|
@ -240,8 +244,8 @@ struct marshal_data {
|
||||||
static void
|
static void
|
||||||
setup_marshal_data(struct marshal_data *data)
|
setup_marshal_data(struct marshal_data *data)
|
||||||
{
|
{
|
||||||
assert(socketpair(AF_UNIX,
|
assert(wl_os_socketpair_cloexec(AF_UNIX,
|
||||||
SOCK_STREAM | SOCK_CLOEXEC, 0, data->s) == 0);
|
SOCK_STREAM, 0, data->s) == 0);
|
||||||
|
|
||||||
data->read_connection = wl_connection_create(data->s[0],
|
data->read_connection = wl_connection_create(data->s[0],
|
||||||
WL_BUFFER_DEFAULT_MAX_SIZE);
|
WL_BUFFER_DEFAULT_MAX_SIZE);
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include "wayland-os.h"
|
||||||
#include "wayland-server.h"
|
#include "wayland-server.h"
|
||||||
#include "test-runner.h"
|
#include "test-runner.h"
|
||||||
|
|
||||||
|
|
@ -40,7 +41,7 @@ TEST(create_resource_tst)
|
||||||
int s[2];
|
int s[2];
|
||||||
uint32_t id;
|
uint32_t id;
|
||||||
|
|
||||||
assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0);
|
assert(wl_os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, s) == 0);
|
||||||
display = wl_display_create();
|
display = wl_display_create();
|
||||||
assert(display);
|
assert(display);
|
||||||
client = wl_client_create(display, s[0]);
|
client = wl_client_create(display, s[0]);
|
||||||
|
|
@ -111,7 +112,7 @@ TEST(destroy_res_tst)
|
||||||
.notify = &destroy_notify
|
.notify = &destroy_notify
|
||||||
};
|
};
|
||||||
|
|
||||||
assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0);
|
assert(wl_os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, s) == 0);
|
||||||
display = wl_display_create();
|
display = wl_display_create();
|
||||||
assert(display);
|
assert(display);
|
||||||
client = wl_client_create(display, s[0]);
|
client = wl_client_create(display, s[0]);
|
||||||
|
|
@ -159,7 +160,7 @@ TEST(create_resource_with_same_id)
|
||||||
int s[2];
|
int s[2];
|
||||||
uint32_t id;
|
uint32_t id;
|
||||||
|
|
||||||
assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0);
|
assert(wl_os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, s) == 0);
|
||||||
display = wl_display_create();
|
display = wl_display_create();
|
||||||
assert(display);
|
assert(display);
|
||||||
client = wl_client_create(display, s[0]);
|
client = wl_client_create(display, s[0]);
|
||||||
|
|
@ -243,7 +244,7 @@ TEST(resource_destroy_iteration)
|
||||||
.notify = &resource_destroy_notify
|
.notify = &resource_destroy_notify
|
||||||
};
|
};
|
||||||
|
|
||||||
assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0);
|
assert(wl_os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, s) == 0);
|
||||||
display = wl_display_create();
|
display = wl_display_create();
|
||||||
assert(display);
|
assert(display);
|
||||||
client = wl_client_create(display, s[0]);
|
client = wl_client_create(display, s[0]);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue