os: cross platform ioctl() wrapper

Reducing the ifdef-zoo a bit by moving the platform specific socket
ioctl calls into separate function. On win32, this also checks the
retval and potentially query for error. On Unix, just calling ioctl().

Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
This commit is contained in:
Enrico Weigelt, metux IT consult
2025-07-30 12:11:39 +02:00
committed by Enrico Weigelt
parent fe795fa7b8
commit ec20354aec
4 changed files with 26 additions and 18 deletions

View File

@@ -117,13 +117,6 @@ Xtransport_table Xtransports[] = {
#define NUMTRANS (sizeof(Xtransports)/sizeof(Xtransport_table))
#ifdef WIN32
#define ioctl ioctlsocket
#endif
/*
* These are a few utility function used by the public interface functions.
*/
@@ -564,7 +557,7 @@ int _XSERVTransNonBlock(XtransConnInfo ciptr)
{
int arg;
arg = 1;
ret = ioctl (fd, FIOSNBIO, &arg);
ret = ossock_ioctl (fd, FIOSNBIO, &arg);
}
#else
#if defined(WIN32)
@@ -572,7 +565,7 @@ int _XSERVTransNonBlock(XtransConnInfo ciptr)
u_long arg_ret = 1;
/* IBM TCP/IP understands this option too well: it causes _XSERVTransRead to fail
* eventually with EWOULDBLOCK */
ret = ioctl (fd, FIONBIO, &arg_ret);
ret = ossock_ioctl (fd, FIONBIO, &arg_ret);
}
#else
ret = fcntl (fd, F_GETFL, 0);

View File

@@ -74,6 +74,8 @@ from the copyright holders.
#include <X11/Xthreads.h>
#endif
#include "os/ossock.h"
#ifndef WIN32
#if defined(TCPCONN) || defined(UNIXCONN)
@@ -1178,15 +1180,7 @@ static int _XSERVTransSocketBytesReadable (
{
prmsg (2,"SocketBytesReadable(%p,%d,%p)\n",
(void *) ciptr, ciptr->fd, (void *) pend);
#ifdef WIN32
{
int ret = ioctlsocket ((SOCKET) ciptr->fd, FIONREAD, (u_long *) pend);
if (ret == SOCKET_ERROR) errno = WSAGetLastError();
return ret;
}
#else
return ioctl (ciptr->fd, FIONREAD, (char *) pend);
#endif /* WIN32 */
return ossock_ioctl (ciptr->fd, FIONREAD, pend);
}
#if XTRANS_SEND_FDS

View File

@@ -6,6 +6,8 @@
#ifdef WIN32
#include <X11/Xwinsock.h>
#else
#include <sys/ioctl.h>
#endif
#include "os/ossock.h"
@@ -18,3 +20,15 @@ void ossock_init(void)
WSAStartup(0x0202, &wsadata);
#endif
}
int ossock_ioctl(int fd, unsigned long request, void *arg)
{
#ifdef WIN32
int ret = ioctlsocket(fd, request, arg);
if (ret == SOCKET_ERROR)
ret = WSAGetLastError();
return ret;
#else
return ioctl(fd, request,arg);
#endif
}

View File

@@ -5,9 +5,16 @@
#ifndef _XSERVER_OS_OSSOCK_H_
#define _XSERVER_OS_OSSOCK_H_
#include <errno.h>
/*
* os specific initialization of the socket layer
*/
void ossock_init(void);
/*
* os specific socket ioctl function
*/
int ossock_ioctl(int fd, unsigned long request, void *arg);
#endif /* _XSERVER_OS_OSSOCK_H_ */