From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from [140.186.70.92] (port=49361 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1POcIm-0003UG-0L for qemu-devel@nongnu.org; Fri, 03 Dec 2010 15:32:24 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1POcIk-0007K7-AG for qemu-devel@nongnu.org; Fri, 03 Dec 2010 15:32:19 -0500 Received: from mail-ww0-f67.google.com ([74.125.82.67]:59028) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1POcIk-0007Jl-2j for qemu-devel@nongnu.org; Fri, 03 Dec 2010 15:32:18 -0500 Received: by wwb28 with SMTP id 28so1099954wwb.10 for ; Fri, 03 Dec 2010 12:32:17 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <1291374593-17448-2-git-send-email-ronniesahlberg@gmail.com> References: <1291374593-17448-1-git-send-email-ronniesahlberg@gmail.com> <1291374593-17448-2-git-send-email-ronniesahlberg@gmail.com> Date: Fri, 3 Dec 2010 20:32:17 +0000 Message-ID: Subject: Re: [Qemu-devel] [PATCH 01/14] ./block/iscsi/init.c From: Stefan Hajnoczi Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: ronniesahlberg@gmail.com Cc: qemu-devel@nongnu.org On Fri, Dec 3, 2010 at 11:09 AM, wrote: > @@ -0,0 +1,215 @@ > +/* > + =A0 Copyright (C) 2010 by Ronnie Sahlberg > + > + =A0 This program is free software; you can redistribute it and/or modif= y > + =A0 it under the terms of the GNU General Public License as published b= y > + =A0 the Free Software Foundation; either version 2 of the License, or > + =A0 (at your option) any later version. You want the library to be GPL, not LGPL? > +struct iscsi_context * > +iscsi_create_context(const char *initiator_name) > +{ > + =A0 =A0 =A0 struct iscsi_context *iscsi; > + > + =A0 =A0 =A0 iscsi =3D malloc(sizeof(struct iscsi_context)); > + =A0 =A0 =A0 if (iscsi =3D=3D NULL) { > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 return NULL; > + =A0 =A0 =A0 } > + > + =A0 =A0 =A0 bzero(iscsi, sizeof(struct iscsi_context)); > + > + =A0 =A0 =A0 iscsi->initiator_name =3D strdup(initiator_name); > + =A0 =A0 =A0 if (iscsi->initiator_name =3D=3D NULL) { > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 free(iscsi); > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 return NULL; > + =A0 =A0 =A0 } > + > + =A0 =A0 =A0 iscsi->fd =3D -1; > + > + =A0 =A0 =A0 /* use a "random" isid */ > + =A0 =A0 =A0 srandom(getpid() ^ time(NULL)); The random number generator has global state and the library may interfere if the program also uses the srandom() interface. > + =A0 =A0 =A0 iscsi_set_random_isid(iscsi); > + > + =A0 =A0 =A0 return iscsi; > +} > + > +int > +iscsi_set_random_isid(struct iscsi_context *iscsi) > +{ > + =A0 =A0 =A0 iscsi->isid[0] =3D 0x80; > + =A0 =A0 =A0 iscsi->isid[1] =3D random()&0xff; > + =A0 =A0 =A0 iscsi->isid[2] =3D random()&0xff; > + =A0 =A0 =A0 iscsi->isid[3] =3D random()&0xff; > + =A0 =A0 =A0 iscsi->isid[4] =3D 0; > + =A0 =A0 =A0 iscsi->isid[5] =3D 0; > + > + =A0 =A0 =A0 return 0; > +} Is there an interface to set a specific isid value? Users will probably want to use a permanent value. > +int > +iscsi_destroy_context(struct iscsi_context *iscsi) > +{ > + =A0 =A0 =A0 struct iscsi_pdu *pdu; > + > + =A0 =A0 =A0 if (iscsi =3D=3D NULL) { > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 return 0; > + =A0 =A0 =A0 } > + =A0 =A0 =A0 if (iscsi->initiator_name !=3D NULL) { > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 free(discard_const(iscsi->initiator_name)); > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 iscsi->initiator_name =3D NULL; > + =A0 =A0 =A0 } > + =A0 =A0 =A0 if (iscsi->target_name !=3D NULL) { > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 free(discard_const(iscsi->target_name)); > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 iscsi->target_name =3D NULL; > + =A0 =A0 =A0 } > + =A0 =A0 =A0 if (iscsi->alias !=3D NULL) { > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 free(discard_const(iscsi->alias)); > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 iscsi->alias =3D NULL; > + =A0 =A0 =A0 } All these if statements are unnecessary. free(NULL) is a nop. > + =A0 =A0 =A0 if (iscsi->fd !=3D -1) { > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 iscsi_disconnect(iscsi); Perhaps disconnect before freeing struct members? > + =A0 =A0 =A0 } > + > + =A0 =A0 =A0 if (iscsi->inbuf !=3D NULL) { > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 free(iscsi->inbuf); > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 iscsi->inbuf =3D NULL; > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 iscsi->insize =3D 0; > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 iscsi->inpos =3D 0; > + =A0 =A0 =A0 } > + > + =A0 =A0 =A0 while ((pdu =3D iscsi->outqueue)) { > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 SLIST_REMOVE(&iscsi->outqueue, pdu); > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 pdu->callback(iscsi, SCSI_STATUS_CANCELLED,= NULL, > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 pdu->private_da= ta); > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 iscsi_free_pdu(iscsi, pdu); > + =A0 =A0 =A0 } > + =A0 =A0 =A0 while ((pdu =3D iscsi->waitpdu)) { > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 SLIST_REMOVE(&iscsi->waitpdu, pdu); > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 pdu->callback(iscsi, SCSI_STATUS_CANCELLED,= NULL, > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 pdu->private_da= ta); > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 iscsi_free_pdu(iscsi, pdu); > + =A0 =A0 =A0 } Could these callbacks expect iscsi to still be valid? A safer order would seem to be: disconnect, cancel all, free. > + > + =A0 =A0 =A0 if (iscsi->error_string !=3D NULL) { > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 free(iscsi->error_string); > + =A0 =A0 =A0 } > + > + =A0 =A0 =A0 free(iscsi); > + > + =A0 =A0 =A0 return 0; > +} > + > + > + > +void > +iscsi_set_error(struct iscsi_context *iscsi, const char *error_string, .= ..) > +{ > + =A0 =A0 =A0 va_list ap; > + =A0 =A0 =A0 char *str; > + > + =A0 =A0 =A0 va_start(ap, error_string); > + =A0 =A0 =A0 if (vasprintf(&str, error_string, ap) < 0) { This function is available in GNU and BSD. Not sure what level of portability you are targeting (e.g. what about Windows)? > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 /* not much we can do here */ You need to assign str =3D NULL here. Otherwise its value is undefined on GNU systems. > + =A0 =A0 =A0 } > + =A0 =A0 =A0 if (iscsi->error_string !=3D NULL) { > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 free(iscsi->error_string); > + =A0 =A0 =A0 } > + =A0 =A0 =A0 iscsi->error_string =3D str; > + =A0 =A0 =A0 va_end(ap); > +} > + > + > +char * const char *? > +iscsi_get_error(struct iscsi_context *iscsi) > +{ > + =A0 =A0 =A0 return iscsi->error_string; > +} Stefan