Message ID | af4268153bce149a9599e4079a0f8908e32fdef8.1504112061.git.jcody@redhat.com |
---|---|
State | New |
Headers | show |
Series | Code cleanup and minor fixes | expand |
On 08/30/2017 11:56 AM, Jeff Cody wrote: > We don't need libssh2 failure to be fatal (we could just opt to not > register the driver on failure). But, it is probably a good idea to > avoid external library calls during the block_init(), and call the > libssh2 global init function on the first usage, returning any errors. > > Signed-off-by: Jeff Cody <jcody@redhat.com> > --- > block/ssh.c | 40 +++++++++++++++++++++++++++++----------- > 1 file changed, 29 insertions(+), 11 deletions(-) > > +static int ssh_state_init(BDRVSSHState *s, Error **errp) > { > + int ret; > + > + if (!ssh_libinit_called) { > + ret = libssh2_init(0); > + if (ret) { > + error_setg(errp, "libssh2 initialization failed with %d", ret); > + return ret; Do we know if this number is always positive or negative? > @@ -772,8 +788,13 @@ static int ssh_file_open(BlockDriverState *bs, QDict *options, int bdrv_flags, > BDRVSSHState *s = bs->opaque; > int ret; > int ssh_flags; > + Error *local_err = NULL; > > - ssh_state_init(s); > + ret = ssh_state_init(s, &local_err); > + if (local_err) { > + error_propagate(errp, local_err); > + return ret; Is returning 'ret' from libssh2_init() wise? If 'ret' is not important, you could simplify this: if (ssh_state_init(s, errp)) { return -1; }
On Wed, Aug 30, 2017 at 02:40:16PM -0500, Eric Blake wrote: > On 08/30/2017 11:56 AM, Jeff Cody wrote: > > We don't need libssh2 failure to be fatal (we could just opt to not > > register the driver on failure). But, it is probably a good idea to > > avoid external library calls during the block_init(), and call the > > libssh2 global init function on the first usage, returning any errors. > > > > Signed-off-by: Jeff Cody <jcody@redhat.com> > > --- > > block/ssh.c | 40 +++++++++++++++++++++++++++++----------- > > 1 file changed, 29 insertions(+), 11 deletions(-) > > > > > +static int ssh_state_init(BDRVSSHState *s, Error **errp) > > { > > + int ret; > > + > > + if (!ssh_libinit_called) { > > + ret = libssh2_init(0); > > + if (ret) { > > + error_setg(errp, "libssh2 initialization failed with %d", ret); > > + return ret; > > Do we know if this number is always positive or negative? > From the documentation [1], it returns 0 on success, or a negative value for error. (I guess presumably that means a positive value is by definition an error, as well). [1] https://www.libssh2.org/libssh2_init.html > > @@ -772,8 +788,13 @@ static int ssh_file_open(BlockDriverState *bs, QDict *options, int bdrv_flags, > > BDRVSSHState *s = bs->opaque; > > int ret; > > int ssh_flags; > > + Error *local_err = NULL; > > > > - ssh_state_init(s); > > + ret = ssh_state_init(s, &local_err); > > + if (local_err) { > > + error_propagate(errp, local_err); > > + return ret; > > Is returning 'ret' from libssh2_init() wise? > > If 'ret' is not important, you could simplify this: > > if (ssh_state_init(s, errp)) { > return -1; > } Good point, not sure if a non-zero ret from libssh2_init() provides meaning beyond 'error' for us. Maybe return -EIO instead of -1, though?
On 08/30/2017 03:11 PM, Jeff Cody wrote: > On Wed, Aug 30, 2017 at 02:40:16PM -0500, Eric Blake wrote: >> On 08/30/2017 11:56 AM, Jeff Cody wrote: >>> We don't need libssh2 failure to be fatal (we could just opt to not >>> register the driver on failure). But, it is probably a good idea to >>> avoid external library calls during the block_init(), and call the >>> libssh2 global init function on the first usage, returning any errors. >>> >> >> Is returning 'ret' from libssh2_init() wise? >> >> If 'ret' is not important, you could simplify this: >> >> if (ssh_state_init(s, errp)) { >> return -1; >> } > > Good point, not sure if a non-zero ret from libssh2_init() provides meaning > beyond 'error' for us. Maybe return -EIO instead of -1, though? You used -EIO for 5/7, so that does sound a bit better.
On Wed, Aug 30, 2017 at 02:40:16PM -0500, Eric Blake wrote: > On 08/30/2017 11:56 AM, Jeff Cody wrote: > > We don't need libssh2 failure to be fatal (we could just opt to not > > register the driver on failure). But, it is probably a good idea to > > avoid external library calls during the block_init(), and call the > > libssh2 global init function on the first usage, returning any errors. > > > > Signed-off-by: Jeff Cody <jcody@redhat.com> > > --- > > block/ssh.c | 40 +++++++++++++++++++++++++++++----------- > > 1 file changed, 29 insertions(+), 11 deletions(-) > > > > > +static int ssh_state_init(BDRVSSHState *s, Error **errp) > > { > > + int ret; > > + > > + if (!ssh_libinit_called) { > > + ret = libssh2_init(0); > > + if (ret) { > > + error_setg(errp, "libssh2 initialization failed with %d", ret); > > + return ret; > > Do we know if this number is always positive or negative? FWIW documentation says: "Returns 0 if succeeded, or a negative value for error." I was holding off on reviewing this patch in general since there's a libssh-based alternative driver under development, which should be better. Rich.
diff --git a/block/ssh.c b/block/ssh.c index e8f0404..cbb0e34 100644 --- a/block/ssh.c +++ b/block/ssh.c @@ -83,12 +83,28 @@ typedef struct BDRVSSHState { bool unsafe_flush_warning; } BDRVSSHState; -static void ssh_state_init(BDRVSSHState *s) +static bool ssh_libinit_called; + +static int ssh_state_init(BDRVSSHState *s, Error **errp) { + int ret; + + if (!ssh_libinit_called) { + ret = libssh2_init(0); + if (ret) { + error_setg(errp, "libssh2 initialization failed with %d", ret); + return ret; + } + ssh_libinit_called = true; + } + + memset(s, 0, sizeof *s); s->sock = -1; s->offset = -1; qemu_co_mutex_init(&s->lock); + + return 0; } static void ssh_state_free(BDRVSSHState *s) @@ -772,8 +788,13 @@ static int ssh_file_open(BlockDriverState *bs, QDict *options, int bdrv_flags, BDRVSSHState *s = bs->opaque; int ret; int ssh_flags; + Error *local_err = NULL; - ssh_state_init(s); + ret = ssh_state_init(s, &local_err); + if (local_err) { + error_propagate(errp, local_err); + return ret; + } ssh_flags = LIBSSH2_FXF_READ; if (bdrv_flags & BDRV_O_RDWR) { @@ -821,8 +842,13 @@ static int ssh_create(const char *filename, QemuOpts *opts, Error **errp) BDRVSSHState s; ssize_t r2; char c[1] = { '\0' }; + Error *local_err = NULL; - ssh_state_init(&s); + ret = ssh_state_init(&s, &local_err); + if (local_err) { + error_propagate(errp, local_err); + return ret; + } /* Get desired file size. */ total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), @@ -1213,14 +1239,6 @@ static BlockDriver bdrv_ssh = { static void bdrv_ssh_init(void) { - int r; - - r = libssh2_init(0); - if (r != 0) { - fprintf(stderr, "libssh2 initialization failed, %d\n", r); - exit(EXIT_FAILURE); - } - bdrv_register(&bdrv_ssh); }
We don't need libssh2 failure to be fatal (we could just opt to not register the driver on failure). But, it is probably a good idea to avoid external library calls during the block_init(), and call the libssh2 global init function on the first usage, returning any errors. Signed-off-by: Jeff Cody <jcody@redhat.com> --- block/ssh.c | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-)