diff --git a/iscsi-scst/usr/Makefile b/iscsi-scst/usr/Makefile index 8e0fa14f3..6122c3d90 100644 --- a/iscsi-scst/usr/Makefile +++ b/iscsi-scst/usr/Makefile @@ -16,7 +16,8 @@ # GNU General Public License for more details. SRCS_D = iscsid.c iscsi_scstd.c conn.c session.c target.c message.c ctldev.c \ - log.c chap.c event.c param.c config.c isns.c md5.c sha1.c + log.c chap.c event.c param.c config.c isns.c md5.c sha1.c \ + misc.c OBJS_D = $(SRCS_D:.c=.o) SRCS_ADM = iscsi_adm.c param.c diff --git a/iscsi-scst/usr/iscsi_scstd.c b/iscsi-scst/usr/iscsi_scstd.c index 5680b545d..9da69c8f2 100644 --- a/iscsi-scst/usr/iscsi_scstd.c +++ b/iscsi-scst/usr/iscsi_scstd.c @@ -92,39 +92,6 @@ iSCSI target daemon.\n\ exit(1); } -static void set_non_blocking(int fd) -{ - int res = fcntl(fd, F_GETFL); - - if (res != -1) { - res = fcntl(fd, F_SETFL, res | O_NONBLOCK); - if (res) - log_warning("unable to set fd flags (%s)!", strerror(errno)); - } else - log_warning("unable to get fd flags (%s)!", strerror(errno)); -} - -static void sock_set_keepalive(int sock, int timeout) -{ - if (timeout) { /* timeout [s] */ - int opt = 2; - - if (setsockopt(sock, SOL_TCP, TCP_KEEPCNT, &opt, sizeof(opt))) - log_warning("unable to set TCP_KEEPCNT on server socket (%s)!", strerror(errno)); - - if (setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, &timeout, sizeof(timeout))) - log_warning("unable to set TCP_KEEPIDLE on server socket (%s)!", strerror(errno)); - - opt = 3; - if (setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, &opt, sizeof(opt))) - log_warning("unable to set KEEPINTVL on server socket (%s)!", strerror(errno)); - - opt = 1; - if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof(opt))) - log_warning("unable to set SO_KEEPALIVE on server socket (%s)!", strerror(errno)); - } -} - const char *get_error_str(int error) { if (error == EAI_SYSTEM) diff --git a/iscsi-scst/usr/misc.c b/iscsi-scst/usr/misc.c new file mode 100644 index 000000000..5c7d67609 --- /dev/null +++ b/iscsi-scst/usr/misc.c @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2007 - 2011 Vladislav Bolkhovitin + * Copyright (C) 2010 - 2011 SCST Ltd. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, version 2 + * of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include +#include +#include +#include +#include + +#include "iscsid.h" + +void set_non_blocking(int fd) +{ + int res = fcntl(fd, F_GETFL); + + if (res != -1) { + res = fcntl(fd, F_SETFL, res | O_NONBLOCK); + if (res) + log_warning("unable to set fd flags (%s)!", strerror(errno)); + } else + log_warning("unable to get fd flags (%s)!", strerror(errno)); +} + +void sock_set_keepalive(int sock, int timeout) +{ + if (timeout) { /* timeout [s] */ + int opt = 2; + + if (setsockopt(sock, SOL_TCP, TCP_KEEPCNT, &opt, sizeof(opt))) + log_warning("unable to set TCP_KEEPCNT on server socket (%s)!", strerror(errno)); + + if (setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, &timeout, sizeof(timeout))) + log_warning("unable to set TCP_KEEPIDLE on server socket (%s)!", strerror(errno)); + + opt = 3; + if (setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, &opt, sizeof(opt))) + log_warning("unable to set KEEPINTVL on server socket (%s)!", strerror(errno)); + + opt = 1; + if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof(opt))) + log_warning("unable to set SO_KEEPALIVE on server socket (%s)!", strerror(errno)); + } +} + diff --git a/iscsi-scst/usr/misc.h b/iscsi-scst/usr/misc.h index 437524a4c..a9ec21202 100644 --- a/iscsi-scst/usr/misc.h +++ b/iscsi-scst/usr/misc.h @@ -106,4 +106,7 @@ static inline int list_length_is_one(const struct __qelem *head) #define IPV6_V6ONLY 26 #endif +extern void set_non_blocking(int fd); +extern void sock_set_keepalive(int sock, int timeout); + #endif