Files
scst/iscsi-scst/usr/misc.c
Vladislav Bolkhovitin 5c2cde6757 Cleanup: move misc routines in misc.c
This patch moves the following utility functions from iscsi_scstd.c to misc.c
and makes them public (i.e. non-static):
* set_non_blocking
* sock_set_keepalive

Signed-off-by: Sergey Myasnikov <tigra564@gmail.com>



git-svn-id: http://svn.code.sf.net/p/scst/svn/trunk@4160 d57e44dd-8a1f-0410-8b47-8ef2f437770f
2012-03-12 21:52:35 +00:00

57 lines
1.7 KiB
C

/*
* 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 <fcntl.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <string.h>
#include <errno.h>
#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));
}
}