libxl/save-helper: Code motion of logging functions
PATCH v2 1/2 libxl/save-helper: Code motion of logging functions
... in preparation for a subsequent functional fix
--- tools/libxl/libxl_save_helper.c | 48 +++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 24 deletions(-)
tools/libxl/libxl_save_helper.c +24 -24
@@ -47,6 +47,30 @@#include "xenguest.h" #include "_libxl_save_msgs_helper.h" +/*----- logger -----*/ + +static void tellparent_vmessage (xentoollog_logger *logger_in, +xentoollog_level level, +int errnoval, +const char *context, +const char *format, +va_list al) +{ +char *formatted; +int r =vasprintf (&formatted, format, al); +if (r <0 ) {perror ("memory allocation failed during logging" );exit (-1 ); } +helper_stub_log (level, errnoval, context, formatted,0 ); +free (formatted); +} + +static void tellparent_progress (struct xentoollog_logger *logger_in, +const char *context, +const char *doing_what,int percent, +unsigned long done,unsigned long total) +{ +helper_stub_progress (context, doing_what, done, total,0 ); +} +/*----- globals -----*/ static const char *program ="libxl-save-helper" ;
@@ -86,34 +110,10 @@ static void *xmalloc(size_t sz)return r; } -/*----- logger -----*/ -typedef struct {xentoollog_logger vtable; }xentoollog_logger_tellparent ; -static void tellparent_vmessage (xentoollog_logger *logger_in, -xentoollog_level level, -int errnoval, -const char *context, -const char *format, -va_list al) -{ -char *formatted; -int r =vasprintf (&formatted, format, al); -if (r <0 ) {perror ("memory allocation failed during logging" );exit (-1 ); } -helper_stub_log (level, errnoval, context, formatted,0 ); -free (formatted); -} - -static void tellparent_progress (struct xentoollog_logger *logger_in, -const char *context, -const char *doing_what,int percent, -unsigned long done,unsigned long total) -{ -helper_stub_progress (context, doing_what, done, total,0 ); -} -static void tellparent_destroy (struct xentoollog_logger *logger_in) {abort ();
Signature · 1 line
1.7.10.4
PATCH v2 2/2 tools/libxl: Improvements to libxl-save-helper when using valgrind
Fix two unfree()'d allocations in libxl-save-helper, to get them out of the way of other legitimate complains from valgrind.
The first is easy; close the interface to libxc when done with it.
The second can be fixed by removing the complexity of creating the logging instance. Initalise the global 'logger' in place rather than as an allocation, which requires changing the indrection of its use in 5 locations.
struct xentoollog_logger_tellparent and functions tellparent_destroy() and createlogger_tellparent() are now unused and removed.
--- tools/libxl/libxl_save_helper.c | 35 +++++++++-------------------------- 1 file changed, 9 insertions(+), 26 deletions(-)
tools/libxl/libxl_save_helper.c +9 -26
@@ -74,7 +74,10 @@ static void tellparent_progress(struct xentoollog_logger *logger_in,/*----- globals -----*/ static const char *program ="libxl-save-helper" ; -static xentoollog_logger *logger; +static xentoollog_logger logger = { + tellparent_vmessage, + tellparent_progress +};static xc_interface *xch;/*----- error handling -----*/
@@ -85,7 +88,7 @@ static void fail(int errnoval, const char *fmt, ...) {va_list al;va_start (al,fmt); -xtl_logv (logger,XTL_ERROR ,errnoval,program,fmt,al); +xtl_logv (&logger,XTL_ERROR ,errnoval,program,fmt,al);exit (-1 ); }
@@ -110,21 +113,6 @@ static void *xmalloc(size_t sz)return r; } -typedef struct { -xentoollog_logger vtable; -}xentoollog_logger_tellparent ; - -static void tellparent_destroy (struct xentoollog_logger *logger_in) -{ -abort (); -} - -static xentoollog_logger_tellparent *createlogger_tellparent (void ) -{ -xentoollog_logger_tellparent newlogger; -return XTL_NEW_LOGGER (tellparent, newlogger); -} -/*----- helper functions called by autogenerated stubs -----*/ unsigned char *helper_allocbuf (int len,void *user)
@@ -184,22 +172,17 @@ static int toolstack_save_cb(uint32_t domid, uint8_t **buf, }static void startup (const char *op) { - logger = (xentoollog_logger *)createlogger_tellparent (); -if (!logger) { -fprintf (stderr,"%s: cannot initialise logger\n" , program); -exit (-1 ); - } - -xtl_log (logger,XTL_DEBUG ,0 ,program,"starting %s" ,op); +xtl_log (&logger,XTL_DEBUG ,0 ,program,"starting %s" ,op); - xch =xc_interface_open (logger,logger,0 ); + xch =xc_interface_open (&logger,&logger,0 );if (!xch)fail (errno,"xc_interface_open failed" ); }static void complete (int retval) {int errnoval = retval ? errno :0 ;/* suppress irrelevant errnos */ -xtl_log (logger,XTL_DEBUG ,errnoval,program,"complete r=%d" ,retval); +xtl_log (&logger,XTL_DEBUG ,errnoval,program,"complete r=%d" ,retval);helper_stub_complete (retval,errnoval,0 ); +xc_interface_close (xch);exit (0 ); }
Signature · 1 line
1.7.10.4
PATCH v3 2/2 tools/libxl: Improvements to libxl-save-helper when using valgrind
Fix two unfree()'d allocations in libxl-save-helper, to get them out of the way of other legitimate complaints from valgrind.
The first is easy; close the interface to libxc when done with it.
The second can be fixed by removing the complexity of creating the logging instance. Initialise the global 'logger' in place rather than as an allocation, which requires changing the indirection of its use in 5 locations.
struct xentoollog_logger_tellparent and functions tellparent_destroy() and createlogger_tellparent() are now unused and removed.
---
v3: Better spelling in commit message
--- tools/libxl/libxl_save_helper.c | 35 +++++++++-------------------------- 1 file changed, 9 insertions(+), 26 deletions(-)
tools/libxl/libxl_save_helper.c +9 -26
@@ -74,7 +74,10 @@ static void tellparent_progress(struct xentoollog_logger *logger_in,/*----- globals -----*/ static const char *program ="libxl-save-helper" ; -static xentoollog_logger *logger; +static xentoollog_logger logger = { + tellparent_vmessage, + tellparent_progress +};static xc_interface *xch;/*----- error handling -----*/
@@ -85,7 +88,7 @@ static void fail(int errnoval, const char *fmt, ...) {va_list al;va_start (al,fmt); -xtl_logv (logger,XTL_ERROR ,errnoval,program,fmt,al); +xtl_logv (&logger,XTL_ERROR ,errnoval,program,fmt,al);exit (-1 ); }
@@ -110,21 +113,6 @@ static void *xmalloc(size_t sz)return r; } -typedef struct { -xentoollog_logger vtable; -}xentoollog_logger_tellparent ; - -static void tellparent_destroy (struct xentoollog_logger *logger_in) -{ -abort (); -} - -static xentoollog_logger_tellparent *createlogger_tellparent (void ) -{ -xentoollog_logger_tellparent newlogger; -return XTL_NEW_LOGGER (tellparent, newlogger); -} -/*----- helper functions called by autogenerated stubs -----*/ unsigned char *helper_allocbuf (int len,void *user)
@@ -184,22 +172,17 @@ static int toolstack_save_cb(uint32_t domid, uint8_t **buf, }static void startup (const char *op) { - logger = (xentoollog_logger *)createlogger_tellparent (); -if (!logger) { -fprintf (stderr,"%s: cannot initialise logger\n" , program); -exit (-1 ); - } - -xtl_log (logger,XTL_DEBUG ,0 ,program,"starting %s" ,op); +xtl_log (&logger,XTL_DEBUG ,0 ,program,"starting %s" ,op); - xch =xc_interface_open (logger,logger,0 ); + xch =xc_interface_open (&logger,&logger,0 );if (!xch)fail (errno,"xc_interface_open failed" ); }static void complete (int retval) {int errnoval = retval ? errno :0 ;/* suppress irrelevant errnos */ -xtl_log (logger,XTL_DEBUG ,errnoval,program,"complete r=%d" ,retval); +xtl_log (&logger,XTL_DEBUG ,errnoval,program,"complete r=%d" ,retval);helper_stub_complete (retval,errnoval,0 ); +xc_interface_close (xch);exit (0 ); }
Signature · 1 line
1.7.10.4