Merge pull request #31 from plieven/master

memory leak fixes + suggestion for iscsi context + qemu-kvm bug
This commit is contained in:
Ronnie Sahlberg
2012-10-30 18:54:38 -07:00
10 changed files with 126 additions and 293 deletions

View File

@@ -62,14 +62,18 @@ enum iscsi_immediate_data {
}; };
struct iscsi_context { struct iscsi_context {
const char *initiator_name; char initiator_name[MAX_STRING_SIZE+1];
const char *target_name; char target_name[MAX_STRING_SIZE+1];
const char *target_address; /* If a redirect */ char target_address[MAX_STRING_SIZE+1]; /* If a redirect */
const char *connected_portal; char connected_portal[MAX_STRING_SIZE+1];
const char *alias; char portal[MAX_STRING_SIZE+1];
char alias[MAX_STRING_SIZE+1];
const char *user; char user[MAX_STRING_SIZE+1];
const char *passwd; char passwd[MAX_STRING_SIZE+1];
char chap_c[MAX_STRING_SIZE+1];
char error_string[MAX_STRING_SIZE+1];
enum iscsi_session_type session_type; enum iscsi_session_type session_type;
unsigned char isid[6]; unsigned char isid[6];
@@ -80,8 +84,6 @@ struct iscsi_context {
enum iscsi_header_digest want_header_digest; enum iscsi_header_digest want_header_digest;
enum iscsi_header_digest header_digest; enum iscsi_header_digest header_digest;
char *error_string;
int fd; int fd;
int is_connected; int is_connected;
@@ -103,7 +105,6 @@ struct iscsi_context {
int chap_a; int chap_a;
int chap_i; int chap_i;
char *chap_c;
iscsi_command_cb socket_status_cb; iscsi_command_cb socket_status_cb;
void *connect_data; void *connect_data;
@@ -124,7 +125,6 @@ struct iscsi_context {
enum iscsi_immediate_data use_immediate_data; enum iscsi_immediate_data use_immediate_data;
int lun; int lun;
const char *portal;
int no_auto_reconnect; int no_auto_reconnect;
int reconnect_deferred; int reconnect_deferred;
int debug; int debug;

View File

@@ -32,6 +32,8 @@ extern "C" {
struct iscsi_context; struct iscsi_context;
struct sockaddr; struct sockaddr;
#define MAX_STRING_SIZE (255)
/* /*
* Syntax for normal and portal/discovery URLs. * Syntax for normal and portal/discovery URLs.
*/ */
@@ -69,13 +71,11 @@ EXTERN int iscsi_queue_length(struct iscsi_context *iscsi);
*/ */
int iscsi_set_tcp_keepalive(struct iscsi_context *iscsi, int idle, int count, int interval); int iscsi_set_tcp_keepalive(struct iscsi_context *iscsi, int idle, int count, int interval);
struct iscsi_url { struct iscsi_url {
const char *portal; char portal[MAX_STRING_SIZE+1];
const char *target; char target[MAX_STRING_SIZE+1];
const char *user; char user[MAX_STRING_SIZE+1];
const char *passwd; char passwd[MAX_STRING_SIZE+1];
int lun; int lun;
}; };
@@ -971,7 +971,7 @@ iscsi_scsi_cancel_all_tasks(struct iscsi_context *iscsi);
if ((iscsi)->debug >= level) { \ if ((iscsi)->debug >= level) { \
fprintf(stderr,"libiscsi: "); \ fprintf(stderr,"libiscsi: "); \
fprintf(stderr, (fmt), ##args); \ fprintf(stderr, (fmt), ##args); \
if (iscsi->target_name) { \ if (iscsi->target_name[0]) { \
fprintf(stderr," [%s]",iscsi->target_name); \ fprintf(stderr," [%s]",iscsi->target_name); \
} \ } \
fprintf(stderr,"\n"); \ fprintf(stderr,"\n"); \
@@ -1019,6 +1019,12 @@ iscsi_set_tcp_keepcnt(struct iscsi_context *iscsi, int value);
EXTERN void EXTERN void
iscsi_set_tcp_keepintvl(struct iscsi_context *iscsi, int value); iscsi_set_tcp_keepintvl(struct iscsi_context *iscsi, int value);
/*
* This function is to set the TCP_SYNCNT option. It has to be called after iscsi
* context creation.
*/
EXTERN void
iscsi_set_tcp_syncnt(struct iscsi_context *iscsi, int value);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@@ -61,6 +61,7 @@ iscsi_testunitready_cb(struct iscsi_context *iscsi, int status,
"failed."); "failed.");
ct->cb(iscsi, SCSI_STATUS_ERROR, NULL, ct->cb(iscsi, SCSI_STATUS_ERROR, NULL,
ct->private_data); ct->private_data);
free(ct);
} }
scsi_free_scsi_task(task); scsi_free_scsi_task(task);
return; return;
@@ -79,6 +80,7 @@ iscsi_testunitready_cb(struct iscsi_context *iscsi, int status,
ct->cb(iscsi, status?SCSI_STATUS_ERROR:SCSI_STATUS_GOOD, NULL, ct->cb(iscsi, status?SCSI_STATUS_ERROR:SCSI_STATUS_GOOD, NULL,
ct->private_data); ct->private_data);
scsi_free_scsi_task(task); scsi_free_scsi_task(task);
free(ct);
} }
static void static void
@@ -87,9 +89,10 @@ iscsi_login_cb(struct iscsi_context *iscsi, int status, void *command_data _U_,
{ {
struct connect_task *ct = private_data; struct connect_task *ct = private_data;
if (status == SCSI_STATUS_REDIRECT && iscsi->target_address) { if (status == SCSI_STATUS_REDIRECT && iscsi->target_address[0]) {
iscsi_disconnect(iscsi); iscsi_disconnect(iscsi);
if (iscsi_connect_async(iscsi, iscsi->target_address, iscsi_connect_cb, iscsi->connect_data) != 0) { if (iscsi_connect_async(iscsi, iscsi->target_address, iscsi_connect_cb, iscsi->connect_data) != 0) {
free(ct);
return; return;
} }
return; return;
@@ -97,6 +100,7 @@ iscsi_login_cb(struct iscsi_context *iscsi, int status, void *command_data _U_,
if (status != 0) { if (status != 0) {
ct->cb(iscsi, SCSI_STATUS_ERROR, NULL, ct->private_data); ct->cb(iscsi, SCSI_STATUS_ERROR, NULL, ct->private_data);
free(ct);
return; return;
} }
@@ -117,12 +121,14 @@ iscsi_connect_cb(struct iscsi_context *iscsi, int status, void *command_data _U_
iscsi_set_error(iscsi, "Failed to connect to iSCSI socket. " iscsi_set_error(iscsi, "Failed to connect to iSCSI socket. "
"%s", iscsi_get_error(iscsi)); "%s", iscsi_get_error(iscsi));
ct->cb(iscsi, SCSI_STATUS_ERROR, NULL, ct->private_data); ct->cb(iscsi, SCSI_STATUS_ERROR, NULL, ct->private_data);
free(ct);
return; return;
} }
if (iscsi_login_async(iscsi, iscsi_login_cb, ct) != 0) { if (iscsi_login_async(iscsi, iscsi_login_cb, ct) != 0) {
iscsi_set_error(iscsi, "iscsi_login_async failed."); iscsi_set_error(iscsi, "iscsi_login_async failed.");
ct->cb(iscsi, SCSI_STATUS_ERROR, NULL, ct->private_data); ct->cb(iscsi, SCSI_STATUS_ERROR, NULL, ct->private_data);
free(ct);
} }
} }
@@ -134,7 +140,7 @@ iscsi_full_connect_async(struct iscsi_context *iscsi, const char *portal,
struct connect_task *ct; struct connect_task *ct;
iscsi->lun = lun; iscsi->lun = lun;
iscsi->portal = strdup(portal); strncpy(iscsi->portal,portal,MAX_STRING_SIZE);
ct = malloc(sizeof(struct connect_task)); ct = malloc(sizeof(struct connect_task));
if (ct == NULL) { if (ct == NULL) {
@@ -221,7 +227,7 @@ try_again:
iscsi_set_header_digest(iscsi, old_iscsi->want_header_digest); iscsi_set_header_digest(iscsi, old_iscsi->want_header_digest);
if (old_iscsi->user != NULL) { if (old_iscsi->user[0]) {
iscsi_set_initiator_username_pwd(iscsi, old_iscsi->user, old_iscsi->passwd); iscsi_set_initiator_username_pwd(iscsi, old_iscsi->user, old_iscsi->passwd);
} }
@@ -229,7 +235,7 @@ try_again:
iscsi->lun = old_iscsi->lun; iscsi->lun = old_iscsi->lun;
iscsi->portal = strdup(old_iscsi->portal); strncpy(iscsi->portal,old_iscsi->portal,MAX_STRING_SIZE);
iscsi->debug = old_iscsi->debug; iscsi->debug = old_iscsi->debug;
@@ -311,26 +317,12 @@ try_again:
goto try_again; goto try_again;
} }
free(discard_const(old_iscsi->initiator_name));
free(discard_const(old_iscsi->target_name));
free(discard_const(old_iscsi->target_address));
free(discard_const(old_iscsi->alias));
free(discard_const(old_iscsi->portal));
if (old_iscsi->incoming != NULL) { if (old_iscsi->incoming != NULL) {
iscsi_free_iscsi_in_pdu(old_iscsi->incoming); iscsi_free_iscsi_in_pdu(old_iscsi->incoming);
} }
if (old_iscsi->inqueue != NULL) { if (old_iscsi->inqueue != NULL) {
iscsi_free_iscsi_inqueue(old_iscsi->inqueue); iscsi_free_iscsi_inqueue(old_iscsi->inqueue);
} }
free(old_iscsi->error_string);
free(discard_const(old_iscsi->user));
free(discard_const(old_iscsi->passwd));
free(discard_const(old_iscsi->chap_c));
if (old_iscsi->connected_portal != NULL) {
free(discard_const(old_iscsi->connected_portal));
}
close(iscsi->fd); close(iscsi->fd);
iscsi->fd = old_iscsi->fd; iscsi->fd = old_iscsi->fd;

View File

@@ -45,8 +45,8 @@ iscsi_create_context(const char *initiator_name)
memset(iscsi, 0, sizeof(struct iscsi_context)); memset(iscsi, 0, sizeof(struct iscsi_context));
iscsi->initiator_name = strdup(initiator_name); strncpy(iscsi->initiator_name,initiator_name,MAX_STRING_SIZE);
if (iscsi->initiator_name == NULL) { if (!iscsi->initiator_name[0]) {
free(iscsi); free(iscsi);
return NULL; return NULL;
} }
@@ -169,14 +169,7 @@ iscsi_set_alias(struct iscsi_context *iscsi, const char *alias)
return -1; return -1;
} }
free(discard_const(iscsi->alias)); strncpy(iscsi->alias,alias,MAX_STRING_SIZE);
iscsi->alias = strdup(alias);
if (iscsi->alias == NULL) {
iscsi_set_error(iscsi, "Failed to allocate alias name");
return -1;
}
return 0; return 0;
} }
@@ -189,13 +182,7 @@ iscsi_set_targetname(struct iscsi_context *iscsi, const char *target_name)
return -1; return -1;
} }
free(discard_const(iscsi->target_name)); strncpy(iscsi->target_name,target_name,MAX_STRING_SIZE);
iscsi->target_name = strdup(target_name);
if (iscsi->target_name == NULL) {
iscsi_set_error(iscsi, "Failed to allocate target name");
return -1;
}
return 0; return 0;
} }
@@ -238,21 +225,6 @@ iscsi_destroy_context(struct iscsi_context *iscsi)
iscsi_free_pdu(iscsi, pdu); iscsi_free_pdu(iscsi, pdu);
} }
free(discard_const(iscsi->initiator_name));
iscsi->initiator_name = NULL;
free(discard_const(iscsi->target_name));
iscsi->target_name = NULL;
free(discard_const(iscsi->target_address));
iscsi->target_address = NULL;
free(discard_const(iscsi->alias));
iscsi->alias = NULL;
free(discard_const(iscsi->portal));
iscsi->portal = NULL;
if (iscsi->incoming != NULL) { if (iscsi->incoming != NULL) {
iscsi_free_iscsi_in_pdu(iscsi->incoming); iscsi_free_iscsi_in_pdu(iscsi->incoming);
} }
@@ -260,23 +232,6 @@ iscsi_destroy_context(struct iscsi_context *iscsi)
iscsi_free_iscsi_inqueue(iscsi->inqueue); iscsi_free_iscsi_inqueue(iscsi->inqueue);
} }
free(iscsi->error_string);
iscsi->error_string = NULL;
free(discard_const(iscsi->user));
iscsi->user = NULL;
free(discard_const(iscsi->passwd));
iscsi->passwd = NULL;
free(discard_const(iscsi->chap_c));
iscsi->chap_c = NULL;
if (iscsi->connected_portal != NULL) {
free(discard_const(iscsi->connected_portal));
iscsi->connected_portal = NULL;
}
iscsi->connect_data = NULL; iscsi->connect_data = NULL;
free(iscsi); free(iscsi);
@@ -288,23 +243,16 @@ void
iscsi_set_error(struct iscsi_context *iscsi, const char *error_string, ...) iscsi_set_error(struct iscsi_context *iscsi, const char *error_string, ...)
{ {
va_list ap; va_list ap;
char *str; char errstr[MAX_STRING_SIZE+1] = {0};
va_start(ap, error_string); va_start(ap, error_string);
str = malloc(1024); if (vsnprintf(errstr, MAX_STRING_SIZE, error_string, ap) < 0) {
if (vsnprintf(str, 1024, error_string, ap) < 0) { strncpy(errstr,"could not format error string!",MAX_STRING_SIZE);
/* not much we can do here */
free(str);
str = NULL;
} }
free(iscsi->error_string);
iscsi->error_string = str;
va_end(ap); va_end(ap);
DPRINTF(iscsi,1,"%s",str); strncpy(iscsi->error_string,errstr,MAX_STRING_SIZE);
DPRINTF(iscsi,1,"%s",iscsi->error_string);
} }
void void
@@ -353,10 +301,10 @@ iscsi_is_logged_in(struct iscsi_context *iscsi)
} }
struct iscsi_url * struct iscsi_url *
iscsi_parse_full_url(struct iscsi_context *iscsi, const char *url) iscsi_parse_url(struct iscsi_context *iscsi, const char *url, int full)
{ {
struct iscsi_url *iscsi_url; struct iscsi_url *iscsi_url;
char *str; char str[MAX_STRING_SIZE+1];
char *portal; char *portal;
char *user = NULL; char *user = NULL;
char *passwd = NULL; char *passwd = NULL;
@@ -366,18 +314,16 @@ iscsi_parse_full_url(struct iscsi_context *iscsi, const char *url)
int l; int l;
if (strncmp(url, "iscsi://", 8)) { if (strncmp(url, "iscsi://", 8)) {
if (full) {
iscsi_set_error(iscsi, "Invalid URL %s\niSCSI URL must be of " iscsi_set_error(iscsi, "Invalid URL %s\niSCSI URL must be of "
"the form: %s", "the form: %s",url,ISCSI_URL_SYNTAX); }
url, else {
ISCSI_URL_SYNTAX); iscsi_set_error(iscsi, "Invalid URL %s\niSCSI Portal URL must be of "
"the form: %s",url,ISCSI_PORTAL_URL_SYNTAX); }
return NULL; return NULL;
} }
str = strdup(url + 8); strncpy(str,url + 8,MAX_STRING_SIZE);
if (str == NULL) {
iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup url %s", url);
return NULL;
}
portal = str; portal = str;
user = getenv("LIBISCSI_CHAP_USERNAME"); user = getenv("LIBISCSI_CHAP_USERNAME");
@@ -397,190 +343,91 @@ iscsi_parse_full_url(struct iscsi_context *iscsi, const char *url)
*tmp++ = 0; *tmp++ = 0;
passwd = tmp; passwd = tmp;
} }
} }
target = strchr(portal, '/'); if (full) {
if (target == NULL) { target = strchr(portal, '/');
iscsi_set_error(iscsi, "Invalid URL %s\nCould not parse " if (target == NULL) {
iscsi_set_error(iscsi, "Invalid URL %s\nCould not parse "
"'<target-iqn>'\niSCSI URL must be of the " "'<target-iqn>'\niSCSI URL must be of the "
"form: %s", "form: %s",
url, url,
ISCSI_URL_SYNTAX); ISCSI_URL_SYNTAX);
free(str); return NULL;
return NULL; }
} *target++ = 0;
*target++ = 0;
if (*target == 0) { if (*target == 0) {
iscsi_set_error(iscsi, "Invalid URL %s\nCould not parse " iscsi_set_error(iscsi, "Invalid URL %s\nCould not parse "
"<target-iqn>\n" "<target-iqn>\n"
"iSCSI URL must be of the form: %s", "iSCSI URL must be of the form: %s",
url, url,
ISCSI_URL_SYNTAX); ISCSI_URL_SYNTAX);
free(str); return NULL;
return NULL; }
}
lun = strchr(target, '/'); lun = strchr(target, '/');
if (lun == NULL) { if (lun == NULL) {
iscsi_set_error(iscsi, "Invalid URL %s\nCould not parse <lun>\n" iscsi_set_error(iscsi, "Invalid URL %s\nCould not parse <lun>\n"
"iSCSI URL must be of the form: %s", "iSCSI URL must be of the form: %s",
url, url,
ISCSI_URL_SYNTAX); ISCSI_URL_SYNTAX);
free(str); return NULL;
return NULL; }
} *lun++ = 0;
*lun++ = 0;
l = strtol(lun, &tmp, 10); l = strtol(lun, &tmp, 10);
if (*lun == 0 || *tmp != 0) { if (*lun == 0 || *tmp != 0) {
iscsi_set_error(iscsi, "Invalid URL %s\nCould not parse <lun>\n" iscsi_set_error(iscsi, "Invalid URL %s\nCould not parse <lun>\n"
"iSCSI URL must be of the form: %s", "iSCSI URL must be of the form: %s",
url, url,
ISCSI_URL_SYNTAX); ISCSI_URL_SYNTAX);
free(str); return NULL;
return NULL; }
} }
else
{
tmp=strchr(portal,'/');
if (tmp) *tmp=0;
}
iscsi_url = malloc(sizeof(struct iscsi_url)); iscsi_url = malloc(sizeof(struct iscsi_url));
if (iscsi_url == NULL) { if (iscsi_url == NULL) {
iscsi_set_error(iscsi, "Out-of-memory: Failed to allocate iscsi_url structure"); iscsi_set_error(iscsi, "Out-of-memory: Failed to allocate iscsi_url structure");
free(str);
return NULL; return NULL;
} }
memset(iscsi_url, 0, sizeof(struct iscsi_url)); memset(iscsi_url, 0, sizeof(struct iscsi_url));
iscsi_url->portal = strdup(portal); strncpy(iscsi_url->portal,portal,MAX_STRING_SIZE);
if (iscsi_url->portal == NULL) {
iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup portal string");
iscsi_destroy_url(iscsi_url);
free(str);
return NULL;
}
iscsi_url->target = strdup(target);
if (iscsi_url->target == NULL) {
iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup target string");
iscsi_destroy_url(iscsi_url);
free(str);
return NULL;
}
if (user != NULL && passwd != NULL) { if (user != NULL && passwd != NULL) {
iscsi_url->user = strdup(user); strncpy(iscsi_url->user,user,MAX_STRING_SIZE);
if (iscsi_url->user == NULL) { strncpy(iscsi_url->passwd,passwd,MAX_STRING_SIZE);
iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup username string"); }
iscsi_destroy_url(iscsi_url);
free(str); if (full) {
return NULL; strncpy(iscsi_url->target,target,MAX_STRING_SIZE);
} iscsi_url->lun = l;
iscsi_url->passwd = strdup(passwd);
if (iscsi_url->passwd == NULL) {
iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup password string");
iscsi_destroy_url(iscsi_url);
free(str);
return NULL;
}
} }
iscsi_url->lun = l;
free(str);
return iscsi_url; return iscsi_url;
} }
struct iscsi_url *
iscsi_parse_full_url(struct iscsi_context *iscsi, const char *url)
{
return iscsi_parse_url(iscsi,url,1);
}
struct iscsi_url * struct iscsi_url *
iscsi_parse_portal_url(struct iscsi_context *iscsi, const char *url) iscsi_parse_portal_url(struct iscsi_context *iscsi, const char *url)
{ {
struct iscsi_url *iscsi_url; return iscsi_parse_url(iscsi,url,0);
char *str;
char *portal;
char *user = NULL;
char *passwd = NULL;
char *tmp;
if (strncmp(url, "iscsi://", 8)) {
iscsi_set_error(iscsi, "Invalid URL %s\niSCSI Portal URL must be of "
"the form: %s",
url,
ISCSI_PORTAL_URL_SYNTAX);
return NULL;
}
str = strdup(url + 8);
if (str == NULL) {
iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup url %s", url);
return NULL;
}
portal = str;
user = getenv("LIBISCSI_CHAP_USERNAME");
passwd = getenv("LIBISCSI_CHAP_PASSWORD");
tmp = strchr(portal, '@');
if (tmp != NULL) {
user = portal;
*tmp++ = 0;
portal = tmp;
tmp = strchr(user, '%');
if (tmp != NULL) {
*tmp++ = 0;
passwd = tmp;
}
}
iscsi_url = malloc(sizeof(struct iscsi_url));
if (iscsi_url == NULL) {
iscsi_set_error(iscsi, "Out-of-memory: Failed to allocate iscsi_url structure");
free(str);
return NULL;
}
memset(iscsi_url, 0, sizeof(struct iscsi_url));
iscsi_url->portal = strdup(portal);
if (iscsi_url->portal == NULL) {
iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup portal string");
iscsi_destroy_url(iscsi_url);
free(str);
return NULL;
}
if (user != NULL && passwd != NULL) {
iscsi_url->user = strdup(user);
if (iscsi_url->user == NULL) {
iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup username string");
iscsi_destroy_url(iscsi_url);
free(str);
return NULL;
}
iscsi_url->passwd = strdup(passwd);
if (iscsi_url->passwd == NULL) {
iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup password string");
iscsi_destroy_url(iscsi_url);
free(str);
return NULL;
}
}
free(str);
return iscsi_url;
} }
void void
iscsi_destroy_url(struct iscsi_url *iscsi_url) iscsi_destroy_url(struct iscsi_url *iscsi_url)
{ {
if (iscsi_url == NULL) {
return;
}
free(discard_const(iscsi_url->portal));
free(discard_const(iscsi_url->target));
free(discard_const(iscsi_url->user));
free(discard_const(iscsi_url->passwd));
free(iscsi_url); free(iscsi_url);
} }
@@ -589,19 +436,7 @@ int
iscsi_set_initiator_username_pwd(struct iscsi_context *iscsi, iscsi_set_initiator_username_pwd(struct iscsi_context *iscsi,
const char *user, const char *passwd) const char *user, const char *passwd)
{ {
free(discard_const(iscsi->user)); strncpy(iscsi->user,user,MAX_STRING_SIZE);
iscsi->user = strdup(user); strncpy(iscsi->passwd,passwd,MAX_STRING_SIZE);
if (iscsi->user == NULL) {
iscsi_set_error(iscsi, "Out-of-memory: failed to strdup username");
return -1;
}
free(discard_const(iscsi->passwd));
iscsi->passwd = strdup(passwd);
if (iscsi->passwd == NULL) {
iscsi_set_error(iscsi, "Out-of-memory: failed to strdup password");
return -1;
}
return 0; return 0;
} }

View File

@@ -106,7 +106,7 @@ iscsi_login_add_targetname(struct iscsi_context *iscsi, struct iscsi_pdu *pdu)
return 0; return 0;
} }
if (iscsi->target_name == NULL) { if (!iscsi->target_name[0]) {
iscsi_set_error(iscsi, "Trying normal connect but " iscsi_set_error(iscsi, "Trying normal connect but "
"target name not set."); "target name not set.");
return -1; return -1;
@@ -663,7 +663,7 @@ iscsi_login_add_chap_response(struct iscsi_context *iscsi, struct iscsi_pdu *pdu
return 0; return 0;
} }
if (iscsi->chap_c == NULL) { if (!iscsi->chap_c[0]) {
iscsi_set_error(iscsi, "No CHAP challenge found"); iscsi_set_error(iscsi, "No CHAP challenge found");
return -1; return -1;
} }
@@ -748,7 +748,7 @@ iscsi_login_async(struct iscsi_context *iscsi, iscsi_command_cb cb,
/* login request */ /* login request */
iscsi_pdu_set_immediate(pdu); iscsi_pdu_set_immediate(pdu);
if (iscsi->user == NULL) { if (!iscsi->user[0]) {
iscsi->current_phase = ISCSI_PDU_LOGIN_CSG_OPNEG; iscsi->current_phase = ISCSI_PDU_LOGIN_CSG_OPNEG;
} }
@@ -1008,15 +1008,7 @@ iscsi_process_login_reply(struct iscsi_context *iscsi, struct iscsi_pdu *pdu,
/* parse the strings */ /* parse the strings */
if (!strncmp(ptr, "TargetAddress=", 14)) { if (!strncmp(ptr, "TargetAddress=", 14)) {
free(discard_const(iscsi->target_address)); strncpy(iscsi->target_address,ptr+14,MAX_STRING_SIZE);
iscsi->target_address = strdup(ptr+14);
if (iscsi->target_address == NULL) {
iscsi_set_error(iscsi, "Failed to allocate"
" target address");
pdu->callback(iscsi, SCSI_STATUS_ERROR, NULL,
pdu->private_data);
return -1;
}
} }
if (!strncmp(ptr, "HeaderDigest=", 13)) { if (!strncmp(ptr, "HeaderDigest=", 13)) {
@@ -1074,13 +1066,7 @@ iscsi_process_login_reply(struct iscsi_context *iscsi, struct iscsi_pdu *pdu,
} }
if (!strncmp(ptr, "CHAP_C=0x", 9)) { if (!strncmp(ptr, "CHAP_C=0x", 9)) {
free(iscsi->chap_c); strncpy(iscsi->chap_c,ptr+9,MAX_STRING_SIZE);
iscsi->chap_c = strdup(ptr+9);
if (iscsi->chap_c == NULL) {
iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup CHAP challenge.");
pdu->callback(iscsi, SCSI_STATUS_ERROR, NULL, pdu->private_data);
return -1;
}
iscsi->secneg_phase = ISCSI_LOGIN_SECNEG_PHASE_SEND_RESPONSE; iscsi->secneg_phase = ISCSI_LOGIN_SECNEG_PHASE_SEND_RESPONSE;
} }

View File

@@ -228,8 +228,7 @@ iscsi_connect_async(struct iscsi_context *iscsi, const char *portal,
freeaddrinfo(ai); freeaddrinfo(ai);
if (iscsi->connected_portal) free(discard_const(iscsi->connected_portal)); strncpy(iscsi->connected_portal,portal,MAX_STRING_SIZE);
iscsi->connected_portal=strdup(portal);
return 0; return 0;
} }
@@ -245,7 +244,7 @@ iscsi_disconnect(struct iscsi_context *iscsi)
close(iscsi->fd); close(iscsi->fd);
if (iscsi->connected_portal) if (iscsi->connected_portal[0])
DPRINTF(iscsi,2,"disconnected from portal %s",iscsi->connected_portal); DPRINTF(iscsi,2,"disconnected from portal %s",iscsi->connected_portal);
iscsi->fd = -1; iscsi->fd = -1;
@@ -522,12 +521,12 @@ iscsi_service(struct iscsi_context *iscsi, int revents)
return 0; return 0;
} }
if (revents & POLLOUT && iscsi->outqueue != NULL) { if (iscsi->is_connected && revents & POLLOUT && iscsi->outqueue != NULL) {
if (iscsi_write_to_socket(iscsi) != 0) { if (iscsi_write_to_socket(iscsi) != 0) {
return iscsi_service_reconnect_if_loggedin(iscsi); return iscsi_service_reconnect_if_loggedin(iscsi);
} }
} }
if (revents & POLLIN) { if (iscsi->is_connected && revents & POLLIN) {
if (iscsi_read_from_socket(iscsi) != 0) { if (iscsi_read_from_socket(iscsi) != 0) {
return iscsi_service_reconnect_if_loggedin(iscsi); return iscsi_service_reconnect_if_loggedin(iscsi);
} }

View File

@@ -216,7 +216,7 @@ int main(int argc, const char *argv[])
struct iscsi_context *iscsi; struct iscsi_context *iscsi;
const char **extra_argv; const char **extra_argv;
int extra_argc = 0; int extra_argc = 0;
const char *url = NULL; char *url = NULL;
struct iscsi_url *iscsi_url = NULL; struct iscsi_url *iscsi_url = NULL;
int evpd = 0, pagecode = 0; int evpd = 0, pagecode = 0;
int show_help = 0, show_usage = 0, debug = 0; int show_help = 0, show_usage = 0, debug = 0;
@@ -275,6 +275,9 @@ int main(int argc, const char *argv[])
exit(10); exit(10);
} }
iscsi_url = iscsi_parse_full_url(iscsi, url); iscsi_url = iscsi_parse_full_url(iscsi, url);
if (url) free(url);
if (iscsi_url == NULL) { if (iscsi_url == NULL) {
fprintf(stderr, "Failed to parse URL: %s\n", fprintf(stderr, "Failed to parse URL: %s\n",
iscsi_get_error(iscsi)); iscsi_get_error(iscsi));

View File

@@ -312,7 +312,7 @@ int main(int argc, const char *argv[])
struct client_state state; struct client_state state;
const char **extra_argv; const char **extra_argv;
int extra_argc = 0; int extra_argc = 0;
const char *url = NULL; char *url = NULL;
poptContext pc; poptContext pc;
int res; int res;
int show_help = 0, show_usage = 0, debug = 0; int show_help = 0, show_usage = 0, debug = 0;
@@ -367,11 +367,14 @@ int main(int argc, const char *argv[])
exit(10); exit(10);
} }
if (debug > 0) { if (debug > 0) {
iscsi_set_debug(iscsi, debug); iscsi_set_debug(iscsi, debug);
} }
iscsi_url = iscsi_parse_portal_url(iscsi, url); iscsi_url = iscsi_parse_portal_url(iscsi, url);
if (url) free(url);
if (iscsi_url == NULL) { if (iscsi_url == NULL) {
fprintf(stderr, "Failed to parse URL: %s\n", fprintf(stderr, "Failed to parse URL: %s\n",
iscsi_get_error(iscsi)); iscsi_get_error(iscsi));

View File

@@ -58,7 +58,7 @@ int main(int argc, const char *argv[])
struct iscsi_context *iscsi; struct iscsi_context *iscsi;
const char **extra_argv; const char **extra_argv;
int extra_argc = 0; int extra_argc = 0;
const char *url = NULL; char *url = NULL;
struct iscsi_url *iscsi_url = NULL; struct iscsi_url *iscsi_url = NULL;
int show_help = 0, show_usage = 0, debug = 0, size_only=0; int show_help = 0, show_usage = 0, debug = 0, size_only=0;
int res; int res;
@@ -117,6 +117,9 @@ int main(int argc, const char *argv[])
exit(10); exit(10);
} }
iscsi_url = iscsi_parse_full_url(iscsi, url); iscsi_url = iscsi_parse_full_url(iscsi, url);
if (url) free(url);
if (iscsi_url == NULL) { if (iscsi_url == NULL) {
fprintf(stderr, "Failed to parse URL: %s\n", fprintf(stderr, "Failed to parse URL: %s\n",
iscsi_get_error(iscsi)); iscsi_get_error(iscsi));

View File

@@ -437,6 +437,8 @@ int main(int argc, const char *argv[])
if (url == NULL) { if (url == NULL) {
fprintf(stderr, "You must specify the URL\n"); fprintf(stderr, "You must specify the URL\n");
print_usage(); print_usage();
free(skipname);
free(testname);
exit(10); exit(10);
} }
@@ -474,6 +476,10 @@ int main(int argc, const char *argv[])
printf("\n"); printf("\n");
} }
free(skipname);
free(testname);
free(url);
return num_failed ? num_failed : num_skipped ? 77 : 0; return num_failed ? num_failed : num_skipped ? 77 : 0;
} }