From 029aef1d241564f510ea7d1cef888f3e2319d687 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Sat, 5 Jan 2008 16:06:36 +0000 Subject: [PATCH] Miscellaneous fixes for building on Windows (MinGW). * configure.in: xdr functions may require -lxdr. * configure.in, src/Makefile.am: Extra linking flags for building libvirt DLL. * src/socketcompat.h: Hide Windows Winsock incompatibilities in a separate header file. * src/remote_internal.c: Switch to using socketcompat.h / socket_errno(). * src/test.c: Switch to using socketcompat.h. * gnulib/lib/arpa/.cvsignore: Ignore another generated file. * src/qparams.c: Include . --- ChangeLog | 14 +++++++++ configure.in | 9 ++++-- gnulib/lib/arpa/.cvsignore | 1 + src/Makefile.am | 3 +- src/libvirt.c | 2 +- src/qparams.c | 2 ++ src/remote_internal.c | 35 ++++++++++------------ src/socketcompat.h | 60 ++++++++++++++++++++++++++++++++++++++ src/test.c | 8 +---- 9 files changed, 103 insertions(+), 31 deletions(-) create mode 100644 src/socketcompat.h diff --git a/ChangeLog b/ChangeLog index 1127a513a7..bf0f4fdc0b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +Wed Jan 5 16:02:00 UTC 2008 Richard W.M. Jones + + Miscellaneous fixes for building on Windows (MinGW). + * configure.in: xdr functions may require -lxdr. + * configure.in, src/Makefile.am: Extra linking flags for + building libvirt DLL. + * src/socketcompat.h: Hide Windows Winsock incompatibilities + in a separate header file. + * src/remote_internal.c: Switch to using socketcompat.h / + socket_errno(). + * src/test.c: Switch to using socketcompat.h. + * gnulib/lib/arpa/.cvsignore: Ignore another generated file. + * src/qparams.c: Include . + Wed Jan 2 17:45:53 EST 2008 Daniel P. Berrange * src/remote_internal.c: fix policykit auth handling of NULL diff --git a/configure.in b/configure.in index 26db6214e2..bc56c2c305 100644 --- a/configure.in +++ b/configure.in @@ -65,8 +65,8 @@ AC_CHECK_FUNCS([cfmakeraw regexec uname]) dnl Availability of various common headers (non-fatal if missing). AC_CHECK_HEADERS([pwd.h paths.h sys/syslimits.h sys/utsname.h sys/wait.h winsock2.h]) -dnl Need -lrpc? (Cygwin needs this) -AC_SEARCH_LIBS(xdrmem_create,rpc) +dnl Need -lrpc or -lxdr? (Cygwin and MinGW resp. need this) +AC_SEARCH_LIBS(xdrmem_create,[rpc xdr]) dnl Do we have rpcgen? AC_PATH_PROG(RPCGEN, rpcgen, no) @@ -650,6 +650,7 @@ dnl for now since I'm not supporting mingw at present. - RWMJ CYGWIN_EXTRA_LDFLAGS= CYGWIN_EXTRA_LIBADD= CYGWIN_EXTRA_PYTHON_LIBADD= +MINGW_EXTRA_LDFLAGS= case "$host" in *-*-cygwin*) CYGWIN_EXTRA_LDFLAGS="-no-undefined" @@ -658,10 +659,14 @@ case "$host" in CYGWIN_EXTRA_PYTHON_LIBADD="-L/usr/lib/python${PYTHON_VERSION}/config -lpython${PYTHON_VERSION}" fi ;; + *-*-mingw*) + MINGW_EXTRA_LDFLAGS="-no-undefined" + ;; esac AC_SUBST(CYGWIN_EXTRA_LDFLAGS) AC_SUBST(CYGWIN_EXTRA_LIBADD) AC_SUBST(CYGWIN_EXTRA_PYTHON_LIBADD) +AC_SUBST(MINGW_EXTRA_LDFLAGS) # very annoying rm -f COPYING diff --git a/gnulib/lib/arpa/.cvsignore b/gnulib/lib/arpa/.cvsignore index e69de29bb2..b9013ae267 100644 --- a/gnulib/lib/arpa/.cvsignore +++ b/gnulib/lib/arpa/.cvsignore @@ -0,0 +1 @@ +inet.h diff --git a/src/Makefile.am b/src/Makefile.am index 82194485ab..d058465abf 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -31,6 +31,7 @@ lib_LTLIBRARIES = libvirt.la CLIENT_SOURCES = \ libvirt.c internal.h \ gnutls_1_0_compat.h \ + socketcompat.h \ hash.c hash.h \ test.c test.h \ buf.c buf.h \ @@ -68,7 +69,7 @@ libvirt_la_LIBADD = $(LIBXML_LIBS) $(GNUTLS_LIBS) $(SASL_LIBS) \ libvirt_la_LDFLAGS = -Wl,--version-script=$(srcdir)/libvirt_sym.version \ -version-info @LIBVIRT_VERSION_INFO@ \ $(COVERAGE_CFLAGS:-f%=-Wc,-f%) \ - @CYGWIN_EXTRA_LDFLAGS@ + @CYGWIN_EXTRA_LDFLAGS@ @MINGW_EXTRA_LDFLAGS@ libvirt_la_CFLAGS = $(COVERAGE_CFLAGS) bin_PROGRAMS = virsh diff --git a/src/libvirt.c b/src/libvirt.c index 67d7e18c29..ca38c54754 100644 --- a/src/libvirt.c +++ b/src/libvirt.c @@ -25,7 +25,7 @@ #include #include "getpass.h" -#if HAVE_WINSOCK2_H +#ifdef HAVE_WINSOCK2_H #include #endif diff --git a/src/qparams.c b/src/qparams.c index 526c50e633..1c06fb1a4c 100644 --- a/src/qparams.c +++ b/src/qparams.c @@ -20,6 +20,8 @@ * Utility functions to help parse and assemble query strings. */ +#include + #include #include #include diff --git a/src/remote_internal.c b/src/remote_internal.c index a184685982..d9d4ba95f1 100644 --- a/src/remote_internal.c +++ b/src/remote_internal.c @@ -23,12 +23,14 @@ #include "config.h" +/* Windows socket compatibility functions. */ +#include "socketcompat.h" + #include #include #include #include #include -#include #include #include #include @@ -46,15 +48,6 @@ #include #endif -#ifndef HAVE_WINSOCK2_H -#include -#include -#include -#include -#else -#include -#endif - #include #include #include @@ -525,7 +518,7 @@ doRemoteOpen (virConnectPtr conn, priv->sock = socket (r->ai_family, SOCK_STREAM, 0); if (priv->sock == -1) { - error (conn, VIR_ERR_SYSTEM_ERROR, strerror (errno)); + error (conn, VIR_ERR_SYSTEM_ERROR, strerror (socket_errno ())); continue; } @@ -535,7 +528,7 @@ doRemoteOpen (virConnectPtr conn, sizeof no_slow_start); if (connect (priv->sock, r->ai_addr, r->ai_addrlen) == -1) { - error (conn, VIR_ERR_SYSTEM_ERROR, strerror (errno)); + error (conn, VIR_ERR_SYSTEM_ERROR, strerror (socket_errno ())); close (priv->sock); continue; } @@ -3036,7 +3029,7 @@ remoteAuthSASL (virConnectPtr conn, struct private_data *priv, int in_open, __virRaiseError (in_open ? NULL : conn, NULL, NULL, VIR_FROM_REMOTE, VIR_ERR_AUTH_FAILED, VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, "failed to get sock address %d (%s)", - errno, strerror(errno)); + socket_errno (), strerror(socket_errno ())); goto cleanup; } if ((localAddr = addrToString(&sa, salen)) == NULL) @@ -3048,7 +3041,7 @@ remoteAuthSASL (virConnectPtr conn, struct private_data *priv, int in_open, __virRaiseError (in_open ? NULL : conn, NULL, NULL, VIR_FROM_REMOTE, VIR_ERR_AUTH_FAILED, VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, "failed to get peer address %d (%s)", - errno, strerror(errno)); + socket_errno (), strerror(socket_errno ())); goto cleanup; } if ((remoteAddr = addrToString(&sa, salen)) == NULL) @@ -3601,12 +3594,13 @@ really_write_buf (virConnectPtr conn, struct private_data *priv, while (len > 0); } else { do { - err = write (priv->sock, p, len); + err = send (priv->sock, p, len, 0); if (err == -1) { - if (errno == EINTR || errno == EAGAIN) + int errno_ = socket_errno (); + if (errno_ == EINTR || errno_ == EAGAIN) continue; error (in_open ? NULL : conn, - VIR_ERR_SYSTEM_ERROR, strerror (errno)); + VIR_ERR_SYSTEM_ERROR, strerror (errno_)); return -1; } len -= err; @@ -3683,12 +3677,13 @@ really_read_buf (virConnectPtr conn, struct private_data *priv, return err; } else { reread: - err = read (priv->sock, bytes, len); + err = recv (priv->sock, bytes, len, 0); if (err == -1) { - if (errno == EINTR) + int errno_ = socket_errno (); + if (errno_ == EINTR) goto reread; error (in_open ? NULL : conn, - VIR_ERR_SYSTEM_ERROR, strerror (errno)); + VIR_ERR_SYSTEM_ERROR, strerror (errno_)); return -1; } if (err == 0) { diff --git a/src/socketcompat.h b/src/socketcompat.h new file mode 100644 index 0000000000..0a8884cb50 --- /dev/null +++ b/src/socketcompat.h @@ -0,0 +1,60 @@ +/* + * socketcompat.h: Socket compatibility for Windows, making it slightly + * less painful to use. + * + * Use this header under the following circumstances: + * (a) Instead of including any of: , , + * , , , AND + * (b) The file will be part of what is built on Windows (basically + * just remote client stuff). + * + * You need to use socket_errno() instead of errno to get socket + * errors. + * + * Copyright (C) 2008 Red Hat, Inc. + * + * See COPYING.LIB for the License of this software + * + * Richard W.M. Jones + */ + +#ifndef __SOCKETCOMPAT_H__ +#define __SOCKETCOMPAT_H__ + +#include + +#include + +#ifndef HAVE_WINSOCK2_H /* Unix & Cygwin. */ + +#include +#include +#include +#include + +static inline int +socket_errno (void) +{ + return errno; +} + +#else /* MinGW & Win32 */ + +#include + +/* Socket functions in Windoze don't set errno. Instead of using errno + * to test for socket errors, call this function to get the errno. + */ +static inline int +socket_errno (void) +{ + return WSAGetLastError (); +} + +/* Compatibility. */ +#define EWOULDBLOCK WSAEWOULDBLOCK +#define ECONNREFUSED WSAECONNREFUSED + +#endif /* HAVE_WINSOCK2_H */ + +#endif /* __WINSOCKWRAPPER_H__ */ diff --git a/src/test.c b/src/test.c index 825707ce8f..d228b31966 100644 --- a/src/test.c +++ b/src/test.c @@ -28,7 +28,6 @@ #include #include #include -#include #include #include #include @@ -37,12 +36,7 @@ #include #include -#ifndef HAVE_WINSOCK2_H -#include -#include -#else -#include -#endif +#include "socketcompat.h" #include "internal.h" #include "test.h"