Commit ae1a4c37 authored by Jacek Kalwas's avatar Jacek Kalwas Committed by Tomasz Zawadzki
Browse files

util/fd: add util to set/clear fd nonblock



Change-Id: I500923187eaba9adcfeedc6cd5c5bbbbd27ee05a
Signed-off-by: default avatarJacek Kalwas <jacek.kalwas@nutanix.com>
Reviewed-on: https://review.spdk.io/c/spdk/spdk/+/25793


Community-CI: Mellanox Build Bot
Reviewed-by: default avatarJim Harris <jim.harris@nvidia.com>
Reviewed-by: default avatarBen Walker <ben@nvidia.com>
Tested-by: default avatarSPDK Automated Test System <spdkbot@gmail.com>
Reviewed-by: default avatarTomasz Zawadzki <tomasz@tzawadzki.com>
parent 74e68d6f
Loading
Loading
Loading
Loading
+1 −8
Original line number Diff line number Diff line
@@ -318,14 +318,7 @@ hello_sock_connect(struct hello_context_t *ctx)

	SPDK_NOTICELOG("Connection accepted from (%s, %hu) to (%s, %hu)\n", caddr, cport, saddr, sport);

	rc = fcntl(STDIN_FILENO, F_GETFL);
	if (rc == -1) {
		SPDK_ERRLOG("Getting file status flag failed: %s\n", strerror(errno));
		goto err;
	}

	if (fcntl(STDIN_FILENO, F_SETFL, rc | O_NONBLOCK) == -1) {
		SPDK_ERRLOG("Setting file status flag failed: %s\n", strerror(errno));
	if (spdk_fd_set_nonblock(STDIN_FILENO) < 0) {
		goto err;
	}

+18 −0
Original line number Diff line number Diff line
@@ -34,6 +34,24 @@ uint64_t spdk_fd_get_size(int fd);
 */
uint32_t spdk_fd_get_blocklen(int fd);

/**
 * Set O_NONBLOCK file status flag.
 *
 * \param fd  File descriptor.
 *
 * \return    0 on success, negative on failure.
 */
int spdk_fd_set_nonblock(int fd);

/**
 * Clear O_NONBLOCK file status flag.
 *
 * \param fd  File descriptor.
 *
 * \return    0 on success, negative on failure.
 */
int spdk_fd_clear_nonblock(int fd);

#ifdef __cplusplus
}
#endif
+1 −0
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@
#define __STDC_WANT_LIB_EXT1__ 1

#include "spdk/stdinc.h"
#include "spdk/fd.h"

#ifdef __cplusplus
extern "C" {
+3 −10
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@

#include "spdk/log.h"
#include "spdk/env.h"
#include "spdk/util.h"

#ifdef __linux__

@@ -24,7 +25,7 @@ spdk_pci_event_listen(void)
	int size = SPDK_UEVENT_RECVBUF_SIZE;
	int buf_size;
	socklen_t opt_size;
	int flag, rc;
	int rc;

	memset(&addr, 0, sizeof(addr));
	addr.nl_family = AF_NETLINK;
@@ -58,16 +59,8 @@ spdk_pci_event_listen(void)
		}
	}

	flag = fcntl(netlink_fd, F_GETFL);
	if (flag < 0) {
		rc = errno;
		SPDK_ERRLOG("Failed to get socket flag, fd: %d\n", netlink_fd);
		goto error;
	}

	if (fcntl(netlink_fd, F_SETFL, flag | O_NONBLOCK) < 0) {
	if (spdk_fd_set_nonblock(netlink_fd) < 0) {
		rc = errno;
		SPDK_ERRLOG("Fcntl can't set nonblocking mode for socket, fd: %d\n", netlink_fd);
		goto error;
	}

+2 −5
Original line number Diff line number Diff line
@@ -182,7 +182,7 @@ static int
jsonrpc_server_accept(struct spdk_jsonrpc_server *server)
{
	struct spdk_jsonrpc_server_conn *conn;
	int rc, flag;
	int rc;

	rc = accept(server->sockfd, NULL, NULL);
	if (rc >= 0) {
@@ -204,10 +204,7 @@ jsonrpc_server_accept(struct spdk_jsonrpc_server *server)
			return -1;
		}

		flag = fcntl(conn->sockfd, F_GETFL);
		if (fcntl(conn->sockfd, F_SETFL, flag | O_NONBLOCK) < 0) {
			SPDK_ERRLOG("fcntl can't set nonblocking mode for socket, fd: %d (%s)\n",
				    conn->sockfd, spdk_strerror(errno));
		if (spdk_fd_set_nonblock(conn->sockfd) < 0) {
			close(conn->sockfd);
			pthread_spin_destroy(&conn->queue_lock);
			return -1;
Loading