[Sheepdog] [PATCH] sheepdog: use qemu socket helper functions

MORITA Kazutaka morita.kazutaka at lab.ntt.co.jp
Fri Apr 30 09:33:27 CEST 2010


Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
 Makefile         |    2 +-
 block/sheepdog.c |   82 +++--------------------------------------------------
 2 files changed, 6 insertions(+), 78 deletions(-)

diff --git a/Makefile b/Makefile
index 1637736..e9eb2d2 100644
--- a/Makefile
+++ b/Makefile
@@ -103,7 +103,7 @@ qobject-obj-y += qerror.o
 # block-obj-y is code used by both qemu system emulation and qemu-img
 
 block-obj-y = cutils.o cache-utils.o qemu-malloc.o qemu-option.o module.o
-block-obj-y += nbd.o block.o aio.o aes.o osdep.o
+block-obj-y += nbd.o block.o aio.o aes.o osdep.o qemu-sockets.o
 block-obj-$(CONFIG_POSIX) += posix-aio-compat.o
 block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o
 block-obj-$(CONFIG_POSIX) += compatfd.o
diff --git a/block/sheepdog.c b/block/sheepdog.c
index 8e9a4fa..785d990 100644
--- a/block/sheepdog.c
+++ b/block/sheepdog.c
@@ -8,22 +8,8 @@
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
-#include <netdb.h>
-#include <stdio.h>
-#include <stdint.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <arpa/inet.h>
-#include <netinet/in.h>
-#include <netinet/tcp.h>
-#include <sys/socket.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <poll.h>
-#include <pthread.h>
-
 #include "qemu-common.h"
+#include "qemu_socket.h"
 #include "block_int.h"
 
 #define DOG_PORT 7000
@@ -459,48 +445,12 @@ static int sd_schedule_bh(QEMUBHFunc *cb, struct sd_aiocb *acb)
 
 static int connect_to_vost(void)
 {
-	char buf[64];
-	char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
+	char buf[1024];
 	char name[] = "localhost";
-	int fd, ret;
-	struct addrinfo hints, *res, *res0;
 	int port = DOG_PORT;
 
-	memset(&hints, 0, sizeof(hints));
-	snprintf(buf, sizeof(buf), "%d", port);
-
-	hints.ai_socktype = SOCK_STREAM;
-
-	ret = getaddrinfo(name, buf, &hints, &res0);
-	if (ret) {
-		eprintf("unable to get address info %s, %m\n", name);
-		return -1;
-	}
-
-	for (res = res0; res; res = res->ai_next) {
-		ret = getnameinfo(res->ai_addr, res->ai_addrlen,
-				  hbuf, sizeof(hbuf), sbuf, sizeof(sbuf),
-				  NI_NUMERICHOST | NI_NUMERICSERV);
-		if (ret)
-			continue;
-
-		fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
-		if (fd < 0)
-			continue;
-
-reconnect:
-		ret = connect(fd, res->ai_addr, res->ai_addrlen);
-		if (!ret) {
-			dprintf("connected to %s:%d\n", name, port);
-			goto success;
-		} else if (ret == -1 && errno == EINTR)
-			goto reconnect;
-	}
-	fd = -1;
-	eprintf("failed connect to %s:%d\n", name, port);
-success:
-	freeaddrinfo(res0);
-	return fd;
+	snprintf(buf, sizeof(buf), "%s:%d", name, port);
+	return inet_connect(buf, SOCK_STREAM);
 }
 
 static int do_send_recv(int sockfd, struct iovec *iov, int len, int offset,
@@ -744,23 +694,6 @@ static int aio_flush_request(void *opaque)
 	return nr_outstanding_aio_req((struct bdrv_sd_state *)opaque);
 }
 
-static int set_nonblocking(int fd)
-{
-	int ret;
-
-	ret = fcntl(fd, F_GETFL);
-	if (ret < 0) {
-		eprintf("can't fcntl (F_GETFL), %m\n");
-		close(fd);
-	} else {
-		ret = fcntl(fd, F_SETFL, ret | O_NONBLOCK);
-		if (ret < 0)
-			eprintf("can't fcntl (O_NONBLOCK), %m\n");
-	}
-
-	return ret;
-}
-
 static int set_nodelay(int fd)
 {
 	int ret, opt;
@@ -780,12 +713,7 @@ static int get_sheep_fd(struct bdrv_sd_state *s)
 		return -1;
 	}
 
-	ret = set_nonblocking(fd);
-	if (ret) {
-		eprintf("%m\n");
-		close(fd);
-		return -1;
-	}
+	socket_set_nonblock(fd);
 
 	ret = set_nodelay(fd);
 	if (ret) {
-- 
1.5.6.5




More information about the sheepdog mailing list