Skip to content

Commit

Permalink
win32: replace closesocket() with close() wrapper
Browse files Browse the repository at this point in the history
Use a close() wrapper instead, so that we don't need to worry about
closesocket() vs close() anymore, let's hope.

Signed-off-by: Marc-André Lureau <[email protected]>
Reviewed-by: Stefan Berger <[email protected]>
Message-Id: <[email protected]>
  • Loading branch information
elmarco committed Mar 13, 2023
1 parent b7e5374 commit 25657fc
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 89 deletions.
6 changes: 3 additions & 3 deletions backends/tpm/tpm_emulator.c
Original file line number Diff line number Diff line change
Expand Up @@ -573,13 +573,13 @@ static int tpm_emulator_prepare_data_fd(TPMEmulator *tpm_emu)
goto err_exit;
}

closesocket(fds[1]);
close(fds[1]);

return 0;

err_exit:
closesocket(fds[0]);
closesocket(fds[1]);
close(fds[0]);
close(fds[1]);
return -1;
}

Expand Down
6 changes: 3 additions & 3 deletions crypto/afalg.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ qcrypto_afalg_socket_bind(const char *type, const char *name,

if (bind(sbind, (const struct sockaddr *)&salg, sizeof(salg)) != 0) {
error_setg_errno(errp, errno, "Failed to bind socket");
closesocket(sbind);
close(sbind);
return -1;
}

Expand Down Expand Up @@ -105,11 +105,11 @@ void qcrypto_afalg_comm_free(QCryptoAFAlg *afalg)
}

if (afalg->tfmfd != -1) {
closesocket(afalg->tfmfd);
close(afalg->tfmfd);
}

if (afalg->opfd != -1) {
closesocket(afalg->opfd);
close(afalg->opfd);
}

g_free(afalg);
Expand Down
4 changes: 2 additions & 2 deletions hw/hyperv/syndbg.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ static void hv_syndbg_realize(DeviceState *dev, Error **errp)
syndbg->servaddr.sin_family = AF_INET;
if (connect(syndbg->socket, (struct sockaddr *)&syndbg->servaddr,
sizeof(syndbg->servaddr)) < 0) {
closesocket(syndbg->socket);
close(syndbg->socket);
error_setg(errp, "%s failed to connect to socket", TYPE_HV_SYNDBG);
return;
}
Expand All @@ -357,7 +357,7 @@ static void hv_syndbg_unrealize(DeviceState *dev)

if (syndbg->socket > 0) {
qemu_set_fd_handler(syndbg->socket, NULL, NULL, NULL);
closesocket(syndbg->socket);
close(syndbg->socket);
}
}

Expand Down
2 changes: 0 additions & 2 deletions include/sysemu/os-posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ void os_daemonize(void);
void os_setup_post(void);
int os_mlock(void);

#define closesocket(s) close(s)

int os_set_daemonize(bool d);
bool is_daemonized(void);

Expand Down
8 changes: 4 additions & 4 deletions include/sysemu/os-win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ bool qemu_socket_unselect(int sockfd, Error **errp);
* set errno based on WSAGetLastError()
*/

#undef close
#define close qemu_close_wrap
int qemu_close_wrap(int fd);

#undef connect
#define connect qemu_connect_wrap
int qemu_connect_wrap(int sockfd, const struct sockaddr *addr,
Expand Down Expand Up @@ -206,10 +210,6 @@ int qemu_shutdown_wrap(int sockfd, int how);
#define ioctlsocket qemu_ioctlsocket_wrap
int qemu_ioctlsocket_wrap(int fd, int req, void *val);

#undef closesocket
#define closesocket qemu_closesocket_wrap
int qemu_closesocket_wrap(int fd);

#undef getsockopt
#define getsockopt qemu_getsockopt_wrap
int qemu_getsockopt_wrap(int sockfd, int level, int optname,
Expand Down
10 changes: 5 additions & 5 deletions io/channel-socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ int qio_channel_socket_connect_sync(QIOChannelSocket *ioc,

trace_qio_channel_socket_connect_complete(ioc, fd);
if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
closesocket(fd);
close(fd);
return -1;
}

Expand Down Expand Up @@ -233,7 +233,7 @@ int qio_channel_socket_listen_sync(QIOChannelSocket *ioc,

trace_qio_channel_socket_listen_complete(ioc, fd);
if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
closesocket(fd);
close(fd);
return -1;
}
qio_channel_set_feature(QIO_CHANNEL(ioc), QIO_CHANNEL_FEATURE_LISTEN);
Expand Down Expand Up @@ -310,7 +310,7 @@ int qio_channel_socket_dgram_sync(QIOChannelSocket *ioc,

trace_qio_channel_socket_dgram_complete(ioc, fd);
if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
closesocket(fd);
close(fd);
return -1;
}

Expand Down Expand Up @@ -444,7 +444,7 @@ static void qio_channel_socket_finalize(Object *obj)
#ifdef WIN32
qemu_socket_unselect(ioc->fd, NULL);
#endif
closesocket(ioc->fd);
close(ioc->fd);
ioc->fd = -1;
}
}
Expand Down Expand Up @@ -852,7 +852,7 @@ qio_channel_socket_close(QIOChannel *ioc,
socket_listen_cleanup(sioc->fd, errp);
}

if (closesocket(sioc->fd) < 0) {
if (close(sioc->fd) < 0) {
sioc->fd = -1;
error_setg_errno(&err, errno, "Unable to close socket");
error_propagate(errp, err);
Expand Down
14 changes: 7 additions & 7 deletions net/dgram.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ static int net_dgram_mcast_create(struct sockaddr_in *mcastaddr,
return fd;
fail:
if (fd >= 0) {
closesocket(fd);
close(fd);
}
return -1;
}
Expand Down Expand Up @@ -352,22 +352,22 @@ static int net_dgram_mcast_init(NetClientState *peer,
if (convert_host_port(saddr, local->u.inet.host, local->u.inet.port,
errp) < 0) {
g_free(saddr);
closesocket(fd);
close(fd);
return -1;
}

/* must be bound */
if (saddr->sin_addr.s_addr == 0) {
error_setg(errp, "can't setup multicast destination address");
g_free(saddr);
closesocket(fd);
close(fd);
return -1;
}
/* clone dgram socket */
newfd = net_dgram_mcast_create(saddr, NULL, errp);
if (newfd < 0) {
g_free(saddr);
closesocket(fd);
close(fd);
return -1;
}
/* clone newfd to fd, close newfd */
Expand Down Expand Up @@ -494,14 +494,14 @@ int net_init_dgram(const Netdev *netdev, const char *name,
if (ret < 0) {
error_setg_errno(errp, errno,
"can't set socket option SO_REUSEADDR");
closesocket(fd);
close(fd);
return -1;
}
ret = bind(fd, (struct sockaddr *)&laddr_in, sizeof(laddr_in));
if (ret < 0) {
error_setg_errno(errp, errno, "can't bind ip=%s to socket",
inet_ntoa(laddr_in.sin_addr));
closesocket(fd);
close(fd);
return -1;
}
qemu_socket_set_nonblock(fd);
Expand Down Expand Up @@ -548,7 +548,7 @@ int net_init_dgram(const Netdev *netdev, const char *name,
if (ret < 0) {
error_setg_errno(errp, errno, "can't bind unix=%s to socket",
laddr_un.sun_path);
closesocket(fd);
close(fd);
return -1;
}
qemu_socket_set_nonblock(fd);
Expand Down
22 changes: 11 additions & 11 deletions net/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ static void net_socket_send(void *opaque)
if (s->listen_fd != -1) {
qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
}
closesocket(s->fd);
close(s->fd);

s->fd = -1;
net_socket_rs_init(&s->rs, net_socket_rs_finalize, false);
Expand Down Expand Up @@ -299,7 +299,7 @@ static int net_socket_mcast_create(struct sockaddr_in *mcastaddr,
return fd;
fail:
if (fd >= 0)
closesocket(fd);
close(fd);
return -1;
}

Expand All @@ -314,7 +314,7 @@ static void net_socket_cleanup(NetClientState *nc)
}
if (s->listen_fd != -1) {
qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
closesocket(s->listen_fd);
close(s->listen_fd);
s->listen_fd = -1;
}
}
Expand Down Expand Up @@ -399,7 +399,7 @@ static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,
return s;

err:
closesocket(fd);
close(fd);
return NULL;
}

Expand Down Expand Up @@ -456,7 +456,7 @@ static NetSocketState *net_socket_fd_init(NetClientState *peer,
if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
(socklen_t *)&optlen)< 0) {
error_setg(errp, "can't get socket option SO_TYPE");
closesocket(fd);
close(fd);
return NULL;
}
switch(so_type) {
Expand All @@ -468,7 +468,7 @@ static NetSocketState *net_socket_fd_init(NetClientState *peer,
default:
error_setg(errp, "socket type=%d for fd=%d must be either"
" SOCK_DGRAM or SOCK_STREAM", so_type, fd);
closesocket(fd);
close(fd);
}
return NULL;
}
Expand Down Expand Up @@ -526,13 +526,13 @@ static int net_socket_listen_init(NetClientState *peer,
if (ret < 0) {
error_setg_errno(errp, errno, "can't bind ip=%s to socket",
inet_ntoa(saddr.sin_addr));
closesocket(fd);
close(fd);
return -1;
}
ret = listen(fd, 0);
if (ret < 0) {
error_setg_errno(errp, errno, "can't listen on socket");
closesocket(fd);
close(fd);
return -1;
}

Expand Down Expand Up @@ -579,7 +579,7 @@ static int net_socket_connect_init(NetClientState *peer,
break;
} else {
error_setg_errno(errp, errno, "can't connect socket");
closesocket(fd);
close(fd);
return -1;
}
} else {
Expand Down Expand Up @@ -671,14 +671,14 @@ static int net_socket_udp_init(NetClientState *peer,
if (ret < 0) {
error_setg_errno(errp, errno,
"can't set socket option SO_REUSEADDR");
closesocket(fd);
close(fd);
return -1;
}
ret = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr));
if (ret < 0) {
error_setg_errno(errp, errno, "can't bind ip=%s to socket",
inet_ntoa(laddr.sin_addr));
closesocket(fd);
close(fd);
return -1;
}
qemu_socket_set_nonblock(fd);
Expand Down
8 changes: 4 additions & 4 deletions tests/qtest/libqtest.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ static int socket_accept(int sock)
(void *)&timeout, sizeof(timeout))) {
fprintf(stderr, "%s failed to set SO_RCVTIMEO: %s\n",
__func__, strerror(errno));
closesocket(sock);
close(sock);
return -1;
}

Expand All @@ -135,7 +135,7 @@ static int socket_accept(int sock)
if (ret == -1) {
fprintf(stderr, "%s failed: %s\n", __func__, strerror(errno));
}
closesocket(sock);
close(sock);

return ret;
}
Expand Down Expand Up @@ -564,8 +564,8 @@ void qtest_quit(QTestState *s)
qtest_remove_abrt_handler(s);

qtest_kill_qemu(s);
closesocket(s->fd);
closesocket(s->qmp_fd);
close(s->fd);
close(s->qmp_fd);
g_string_free(s->rx, true);

for (GList *it = s->pending_events; it != NULL; it = it->next) {
Expand Down
2 changes: 1 addition & 1 deletion tests/qtest/microbit-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ static void test_nrf51_uart(void)
g_assert_true(recv(sock_fd, s, 10, 0) == 5);
g_assert_true(memcmp(s, "world", 5) == 0);

closesocket(sock_fd);
close(sock_fd);

qtest_quit(qts);
}
Expand Down
10 changes: 5 additions & 5 deletions tests/qtest/netdev-socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ static int inet_get_free_port_multiple(int nb, int *port, bool ipv6)

nb = i;
for (i = 0; i < nb; i++) {
closesocket(sock[i]);
close(sock[i]);
}

return nb;
Expand Down Expand Up @@ -361,8 +361,8 @@ static void test_stream_fd(void)
qtest_quit(qts1);
qtest_quit(qts0);

closesocket(sock[0]);
closesocket(sock[1]);
close(sock[0]);
close(sock[1]);
}
#endif

Expand Down Expand Up @@ -487,8 +487,8 @@ static void test_dgram_fd(void)
qtest_quit(qts1);
qtest_quit(qts0);

closesocket(sv[0]);
closesocket(sv[1]);
close(sv[0]);
close(sv[1]);
}
#endif

Expand Down
8 changes: 4 additions & 4 deletions tests/unit/socket-helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ static int socket_can_bind_connect(const char *hostname, int family)

cleanup:
if (afd != -1) {
closesocket(afd);
close(afd);
}
if (cfd != -1) {
closesocket(cfd);
close(cfd);
}
if (lfd != -1) {
closesocket(lfd);
close(lfd);
}
if (res) {
freeaddrinfo(res);
Expand Down Expand Up @@ -160,7 +160,7 @@ void socket_check_afunix_support(bool *has_afunix)
int fd;

fd = socket(PF_UNIX, SOCK_STREAM, 0);
closesocket(fd);
close(fd);

#ifdef _WIN32
*has_afunix = (fd != (int)INVALID_SOCKET);
Expand Down
Loading

0 comments on commit 25657fc

Please sign in to comment.