Merge remote-tracking branch 'upstream-git/master'

Conflicts:
	include/iscsi-private.h
	include/iscsi.h
	lib/connect.c
	lib/init.c
	lib/scsi-lowlevel.c
This commit is contained in:
Peter Lieven
2012-11-12 16:02:57 +01:00
21 changed files with 205 additions and 85 deletions

View File

@@ -179,7 +179,7 @@ int iscsi_reconnect(struct iscsi_context *old_iscsi)
{
struct iscsi_context *iscsi = old_iscsi;
DPRINTF(iscsi,2,"reconnect initiated");
ISCSI_LOG(iscsi, 2, "reconnect initiated");
/* This is mainly for tests, where we do not want to automatically
reconnect but rather want the commands to fail with an error
@@ -246,8 +246,8 @@ try_again:
strncpy(iscsi->bind_interfaces,old_iscsi->bind_interfaces,MAX_STRING_SIZE);
iscsi->bind_interfaces_cnt = old_iscsi->bind_interfaces_cnt;
iscsi->debug = old_iscsi->debug;
iscsi->log_level = old_iscsi->log_level;
iscsi->log_fn = old_iscsi->log_fn;
iscsi->tcp_user_timeout = old_iscsi->tcp_user_timeout;
iscsi->tcp_keepidle = old_iscsi->tcp_keepidle;
iscsi->tcp_keepcnt = old_iscsi->tcp_keepcnt;
@@ -263,7 +263,7 @@ try_again:
if (backoff > 30) {
backoff=30;
}
DPRINTF(iscsi,1,"reconnect try %d failed, waiting %d seconds",retry,backoff);
ISCSI_LOG(iscsi, 1, "reconnect try %d failed, waiting %d seconds",retry,backoff);
iscsi_destroy_context(iscsi);
sleep(backoff);
retry++;

View File

@@ -87,8 +87,8 @@ iscsi_create_context(const char *initiator_name)
strncpy(iscsi->initiator_name,initiator_name,MAX_STRING_SIZE);
iscsi->fd = -1;
srand(time(NULL) ^ getpid() ^ (u_int32_t) iscsi);
srand(time(NULL) ^ getpid() ^ (u_int32_t) ((uintptr_t) iscsi));
/* initialize to a "random" isid */
iscsi_set_isid_random(iscsi, rand(), 0);
@@ -113,7 +113,8 @@ iscsi_create_context(const char *initiator_name)
iscsi->tcp_keepidle=30;
if (getenv("LIBISCSI_DEBUG") != NULL) {
iscsi_set_debug(iscsi,atoi(getenv("LIBISCSI_DEBUG")));
iscsi_set_log_level(iscsi, atoi(getenv("LIBISCSI_DEBUG")));
iscsi_set_log_fn(iscsi, iscsi_log_to_stderr);
}
if (getenv("LIBISCSI_TCP_USER_TIMEOUT") != NULL) {
@@ -276,9 +277,9 @@ iscsi_destroy_context(struct iscsi_context *iscsi)
iscsi->connect_data = NULL;
if (iscsi->mallocs != iscsi->frees) {
DPRINTF(iscsi,1,"%d memory blocks lost at iscsi_destroy_context() after %d malloc(s), %d realloc(s) and %d free(s)",iscsi->mallocs-iscsi->frees,iscsi->mallocs,iscsi->reallocs,iscsi->frees);
ISCSI_LOG(iscsi,1,"%d memory blocks lost at iscsi_destroy_context() after %d malloc(s), %d realloc(s) and %d free(s)",iscsi->mallocs-iscsi->frees,iscsi->mallocs,iscsi->reallocs,iscsi->frees);
} else {
DPRINTF(iscsi,5,"memory is clean at iscsi_destroy_context() after %d mallocs, %d realloc(s) and %d frees",iscsi->mallocs,iscsi->reallocs,iscsi->frees);
ISCSI_LOG(iscsi,5,"memory is clean at iscsi_destroy_context() after %d mallocs, %d realloc(s) and %d frees",iscsi->mallocs,iscsi->reallocs,iscsi->frees);
}
memset(iscsi, 0, sizeof(struct iscsi_context));
@@ -291,28 +292,25 @@ void
iscsi_set_error(struct iscsi_context *iscsi, const char *error_string, ...)
{
va_list ap;
char errstr[MAX_STRING_SIZE+1] = {0};
char errstr[MAX_STRING_SIZE + 1] = {0};
va_start(ap, error_string);
if (vsnprintf(errstr, MAX_STRING_SIZE, error_string, ap) < 0) {
strncpy(errstr,"could not format error string!",MAX_STRING_SIZE);
strncpy(errstr, "could not format error string!", MAX_STRING_SIZE);
}
va_end(ap);
if (iscsi != NULL) {
strncpy(iscsi->error_string,errstr,MAX_STRING_SIZE);
DPRINTF(iscsi,1,"%s",iscsi->error_string);
}
else {
fprintf(stderr,"libiscsi: %s\n", errstr);
strncpy(iscsi->error_string, errstr,MAX_STRING_SIZE);
ISCSI_LOG(iscsi, 1, "%s",iscsi->error_string);
}
}
void
iscsi_set_debug(struct iscsi_context *iscsi, int level)
iscsi_set_log_level(struct iscsi_context *iscsi, int level)
{
iscsi->debug = level;
DPRINTF(iscsi,2,"set debug level to %d",level);
iscsi->log_level = level;
ISCSI_LOG(iscsi, 2, "set log level to %d", level);
}
const char *

View File

@@ -17,6 +17,7 @@ iscsi_get_target_address
iscsi_inquiry_sync
iscsi_inquiry_task
iscsi_is_logged_in
iscsi_log_to_stderr
iscsi_login_async
iscsi_login_sync
iscsi_logout_async
@@ -64,7 +65,8 @@ iscsi_scsi_command_sync
iscsi_scsi_cancel_task
iscsi_service
iscsi_set_alias
iscsi_set_debug
iscsi_set_log_level
iscsi_set_log_fn
iscsi_set_header_digest
iscsi_set_initiator_username_pwd
iscsi_set_isid_en

View File

@@ -15,6 +15,7 @@ iscsi_get_target_address
iscsi_inquiry_sync
iscsi_inquiry_task
iscsi_is_logged_in
iscsi_log_to_stderr
iscsi_login_async
iscsi_login_sync
iscsi_logout_async
@@ -62,7 +63,8 @@ iscsi_scsi_command_sync
iscsi_scsi_cancel_task
iscsi_service
iscsi_set_alias
iscsi_set_debug
iscsi_set_log_level
iscsi_set_log_fn
iscsi_set_header_digest
iscsi_set_initiator_username_pwd
iscsi_set_isid_en

64
lib/logging.c Normal file
View File

@@ -0,0 +1,64 @@
/*
Copyright (C) 2012 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#if defined(WIN32)
#else
#include <unistd.h>
#endif
#include <stdio.h>
#include <stdarg.h>
#include <arpa/inet.h>
#include "iscsi.h"
#include "iscsi-private.h"
#include "scsi-lowlevel.h"
void
iscsi_log_to_stderr(int level, const char *message)
{
fprintf(stderr, "libiscsi:%d %s\n", level, message);
}
void
iscsi_set_log_fn(struct iscsi_context *iscsi, iscsi_log_fn fn)
{
iscsi->log_fn = fn;
}
void
iscsi_log_message(struct iscsi_context *iscsi, int level, const char *format, ...)
{
va_list ap;
static char message[1024];
int ret;
if (iscsi->log_fn == NULL) {
return;
}
va_start(ap, format);
ret = vsnprintf(message, 1024, format, ap);
va_end(ap);
if (ret < 0) {
return;
}
iscsi->log_fn(level, message);
}

View File

@@ -1045,7 +1045,7 @@ iscsi_process_login_reply(struct iscsi_context *iscsi, struct iscsi_pdu *pdu,
}
if (status == SCSI_STATUS_REDIRECT && iscsi->target_address) {
DPRINTF(iscsi,2,"target requests redirect to %s",iscsi->target_address);
ISCSI_LOG(iscsi, 2, "target requests redirect to %s",iscsi->target_address);
pdu->callback(iscsi, SCSI_STATUS_REDIRECT, NULL,
pdu->private_data);
return 0;

View File

@@ -123,7 +123,7 @@ int
iscsi_add_data(struct iscsi_context *iscsi, struct iscsi_data *data,
unsigned char *dptr, int dsize, int pdualignment)
{
int len, aligned;
size_t len, aligned;
if (dsize == 0) {
iscsi_set_error(iscsi, "Trying to append zero size data to "
@@ -137,7 +137,7 @@ iscsi_add_data(struct iscsi_context *iscsi, struct iscsi_data *data,
aligned = (aligned+3)&0xfffffffc;
}
int new_alloc_size = data->alloc_size;
size_t new_alloc_size = data->alloc_size;
if (new_alloc_size < 64) new_alloc_size=64;
while (aligned > new_alloc_size) new_alloc_size<<=1;
@@ -154,7 +154,7 @@ iscsi_add_data(struct iscsi_context *iscsi, struct iscsi_data *data,
if (data->data == NULL) {
iscsi_set_error(iscsi, "failed to allocate buffer for %d "
"bytes", len);
"bytes", (int) len);
return -1;
}

View File

@@ -48,8 +48,8 @@ scsi_free_scsi_task(struct scsi_task *task)
struct scsi_allocated_memory *mem;
while ((mem = task->mem)) {
SLIST_REMOVE(&task->mem, mem);
free(mem);
SLIST_REMOVE(&task->mem, mem);
free(mem);
}
free(task->datain.data);
@@ -2555,3 +2555,4 @@ scsi_task_get_data_in_buffer(struct scsi_task *task, uint32_t pos, ssize_t *coun
return &sdb->data[pos];
}

View File

@@ -92,7 +92,7 @@ int set_tcp_user_timeout(struct iscsi_context *iscsi)
iscsi_set_error(iscsi, "TCP: Failed to set tcp user timeout. Error %s(%d)", strerror(errno), errno);
return -1;
}
DPRINTF(iscsi,3,"TCP_USER_TIMEOUT set to %d",iscsi->tcp_user_timeout);
ISCSI_LOG(iscsi, 3, "TCP_USER_TIMEOUT set to %d",iscsi->tcp_user_timeout);
return 0;
}
@@ -106,7 +106,7 @@ int set_tcp_syncnt(struct iscsi_context *iscsi)
iscsi_set_error(iscsi, "TCP: Failed to set tcp syn retries. Error %s(%d)", strerror(errno), errno);
return -1;
}
DPRINTF(iscsi,3,"TCP_SYNCNT set to %d",iscsi->tcp_syncnt);
ISCSI_LOG(iscsi, 3, "TCP_SYNCNT set to %d",iscsi->tcp_syncnt);
return 0;
}
@@ -120,7 +120,7 @@ iscsi_connect_async(struct iscsi_context *iscsi, const char *portal,
struct addrinfo *ai = NULL;
int socksize;
DPRINTF(iscsi,2,"connecting to portal %s",portal);
ISCSI_LOG(iscsi, 2, "connecting to portal %s",portal);
if (iscsi->fd != -1) {
iscsi_set_error(iscsi,
@@ -239,9 +239,9 @@ iscsi_connect_async(struct iscsi_context *iscsi, const char *portal,
int res = setsockopt(iscsi->fd, SOL_SOCKET, SO_BINDTODEVICE, pchr, strlen(pchr));
if (res < 0) {
DPRINTF(iscsi,1,"failed to bind to interface '%s': %s",pchr,strerror(errno));
ISCSI_LOG(iscsi,1,"failed to bind to interface '%s': %s",pchr,strerror(errno));
} else {
DPRINTF(iscsi,3,"successfully bound to interface '%s'",pchr);
ISCSI_LOG(iscsi,3,"successfully bound to interface '%s'",pchr);
}
if (pchr2) pchr2[0]=',';
}
@@ -275,8 +275,9 @@ iscsi_disconnect(struct iscsi_context *iscsi)
close(iscsi->fd);
if (iscsi->connected_portal[0])
DPRINTF(iscsi,2,"disconnected from portal %s",iscsi->connected_portal);
if (iscsi->connected_portal[0]) {
ISCSI_LOG(iscsi, 2, "disconnected from portal %s",iscsi->connected_portal);
}
iscsi->fd = -1;
iscsi->is_connected = 0;
@@ -540,7 +541,7 @@ iscsi_service(struct iscsi_context *iscsi, int revents)
return iscsi_service_reconnect_if_loggedin(iscsi);
}
DPRINTF(iscsi,2,"connection to %s established",iscsi->connected_portal);
ISCSI_LOG(iscsi, 2, "connection to %s established",iscsi->connected_portal);
iscsi->is_connected = 1;
if (iscsi->socket_status_cb) {
@@ -617,31 +618,31 @@ iscsi_free_iscsi_inqueue(struct iscsi_context *iscsi, struct iscsi_in_pdu *inque
void iscsi_set_tcp_syncnt(struct iscsi_context *iscsi, int value)
{
iscsi->tcp_syncnt=value;
DPRINTF(iscsi,2,"TCP_SYNCNT will be set to %d on next socket creation",value);
ISCSI_LOG(iscsi, 2, "TCP_SYNCNT will be set to %d on next socket creation",value);
}
void iscsi_set_tcp_user_timeout(struct iscsi_context *iscsi, int value)
{
iscsi->tcp_user_timeout=value;
DPRINTF(iscsi,2,"TCP_USER_TIMEOUT will be set to %dms on next socket creation",value);
ISCSI_LOG(iscsi, 2, "TCP_USER_TIMEOUT will be set to %dms on next socket creation",value);
}
void iscsi_set_tcp_keepidle(struct iscsi_context *iscsi, int value)
{
iscsi->tcp_keepidle=value;
DPRINTF(iscsi,2,"TCP_KEEPIDLE will be set to %d on next socket creation",value);
ISCSI_LOG(iscsi, 2, "TCP_KEEPIDLE will be set to %d on next socket creation",value);
}
void iscsi_set_tcp_keepcnt(struct iscsi_context *iscsi, int value)
{
iscsi->tcp_keepcnt=value;
DPRINTF(iscsi,2,"TCP_KEEPCNT will be set to %d on next socket creation",value);
ISCSI_LOG(iscsi, 2, "TCP_KEEPCNT will be set to %d on next socket creation",value);
}
void iscsi_set_tcp_keepintvl(struct iscsi_context *iscsi, int value)
{
iscsi->tcp_keepintvl=value;
DPRINTF(iscsi,2,"TCP_KEEPINTVL will be set to %d on next socket creation",value);
ISCSI_LOG(iscsi, 2, "TCP_KEEPINTVL will be set to %d on next socket creation",value);
}
int iscsi_set_tcp_keepalive(struct iscsi_context *iscsi, int idle, int count, int interval)
@@ -652,27 +653,27 @@ int iscsi_set_tcp_keepalive(struct iscsi_context *iscsi, int idle, int count, in
iscsi_set_error(iscsi, "TCP: Failed to set socket option SO_KEEPALIVE. Error %s(%d)", strerror(errno), errno);
return -1;
}
DPRINTF(iscsi,3,"SO_KEEPALIVE set to %d",value);
ISCSI_LOG(iscsi, 3, "SO_KEEPALIVE set to %d",value);
#ifdef TCP_KEEPCNT
if (set_tcp_sockopt(iscsi->fd, TCP_KEEPCNT, count) != 0) {
iscsi_set_error(iscsi, "TCP: Failed to set tcp keepalive count. Error %s(%d)", strerror(errno), errno);
return -1;
}
DPRINTF(iscsi,3,"TCP_KEEPCNT set to %d",count);
ISCSI_LOG(iscsi, 3, "TCP_KEEPCNT set to %d",count);
#endif
#ifdef TCP_KEEPINTVL
if (set_tcp_sockopt(iscsi->fd, TCP_KEEPINTVL, interval) != 0) {
iscsi_set_error(iscsi, "TCP: Failed to set tcp keepalive interval. Error %s(%d)", strerror(errno), errno);
return -1;
}
DPRINTF(iscsi,3,"TCP_KEEPINTVL set to %d",interval);
ISCSI_LOG(iscsi, 3, "TCP_KEEPINTVL set to %d",interval);
#endif
#ifdef TCP_KEEPIDLE
if (set_tcp_sockopt(iscsi->fd, TCP_KEEPIDLE, idle) != 0) {
iscsi_set_error(iscsi, "TCP: Failed to set tcp keepalive idle. Error %s(%d)", strerror(errno), errno);
return -1;
}
DPRINTF(iscsi,3,"TCP_KEEPIDLE set to %d",idle);
ISCSI_LOG(iscsi, 3, "TCP_KEEPIDLE set to %d",idle);
#endif
#endif
@@ -691,9 +692,9 @@ void iscsi_set_bind_interfaces(struct iscsi_context *iscsi, char * interfaces)
if (pchr2) {pchr=pchr2+1;}
iscsi->bind_interfaces_cnt++;
} while (pchr2);
DPRINTF(iscsi,2,"will bind to one of the following %d interface(s) on next socket creation: %s",iscsi->bind_interfaces_cnt,interfaces);
ISCSI_LOG(iscsi,2,"will bind to one of the following %d interface(s) on next socket creation: %s",iscsi->bind_interfaces_cnt,interfaces);
if (!iface_rr) iface_rr=rand()%iscsi->bind_interfaces_cnt+1;
#else
DPRINTF(iscsi,1,"binding to an interface is not supported on your OS");
ISCSI_LOG(iscsi,1,"binding to an interface is not supported on your OS");
#endif
}