Change the handling of private functions, safe_malloc etc.

Rename guestfs_safe_malloc et al to guestfs___safe_malloc etc.

To use the private functions, code now has to define
-DGUESTFS_PRIVATE_FUNCTIONS=1.  This will make it easier for us in
future to work out which programs are using these functions and to
minimize both the number of programs and the functions they are
calling.

Note that the Perl, Python, OCaml, Ruby and Java bindings use
guestfs_safe_* calls.  None of the other bindings do.  This is a bug
(in the bindings using those functions): these functions will call the
out of memory callback on failure.  This function defaults to abort(),
and since this happens from a language binding, there is no way to
change this default.
This commit is contained in:
Richard W.M. Jones
2012-12-15 17:19:53 +00:00
parent 27f6878e2a
commit 62e775c350
21 changed files with 76 additions and 73 deletions

View File

@@ -41,6 +41,7 @@ virt_alignment_scan_SOURCES = \
virt_alignment_scan_CFLAGS = \
-DGUESTFS_WARN_DEPRECATED=1 \
-DGUESTFS_PRIVATE_FUNCTIONS=1 \
-I$(top_srcdir)/src -I$(top_builddir)/src \
-I$(top_srcdir)/fish \
-I$(srcdir)/../gnulib/lib -I../gnulib/lib \

View File

@@ -30,10 +30,6 @@
#include "progname.h"
#if defined(HAVE_LIBVIRT) && defined(HAVE_LIBXML2)
#define GUESTFS_PRIVATE_FOR_EACH_DISK 1
#endif
#include "guestfs.h"
#include "options.h"
#include "scan.h"

View File

@@ -44,6 +44,7 @@ virt_df_SOURCES = \
virt_df_CFLAGS = \
-DGUESTFS_WARN_DEPRECATED=1 \
-DGUESTFS_PRIVATE_FUNCTIONS=1 \
-I$(top_srcdir)/src -I$(top_builddir)/src \
-I$(top_srcdir)/fish \
-I$(srcdir)/../gnulib/lib -I../gnulib/lib \

View File

@@ -30,10 +30,6 @@
#include "progname.h"
#if defined(HAVE_LIBVIRT) && defined(HAVE_LIBXML2)
#define GUESTFS_PRIVATE_FOR_EACH_DISK 1
#endif
#include "guestfs.h"
#include "options.h"
#include "virt-df.h"

View File

@@ -368,9 +368,9 @@ example:
xxxx (+0): guestfs_create
xxxx (+29): guestfs_set_pgroup g=0x17a9de0 pgroup=0x1
xxxx (+9): guestfs_add_drive_opts_argv g=0x17a9de0 [...]
xxxx (+8): guestfs_safe_strdup g=0x17a9de0 str=0x7f8a153bed5d
xxxx (+19): guestfs_safe_malloc g=0x17a9de0 nbytes=0x38
xxxx (+5): guestfs_safe_strdup g=0x17a9de0 str=0x17a9f60
xxxx (+8): guestfs___safe_strdup g=0x17a9de0 str=0x7f8a153bed5d
xxxx (+19): guestfs___safe_malloc g=0x17a9de0 nbytes=0x38
xxxx (+5): guestfs___safe_strdup g=0x17a9de0 str=0x17a9f60
xxxx (+10): guestfs_launch g=0x17a9de0
xxxx (+4): launch_start
[etc]

View File

@@ -677,20 +677,23 @@ extern GUESTFS_DLL_PUBLIC void *guestfs_next_private (guestfs_h *g, const char *
pr "\
#if GUESTFS_PRIVATE_FUNCTIONS
/* Private functions.
*
* These are NOT part of the public, stable API, and can change at any
* time! We export them because they are used by some of the language
* bindings.
*/
extern GUESTFS_DLL_PUBLIC void *guestfs_safe_malloc (guestfs_h *g, size_t nbytes);
extern GUESTFS_DLL_PUBLIC void *guestfs_safe_calloc (guestfs_h *g, size_t n, size_t s);
extern GUESTFS_DLL_PUBLIC char *guestfs_safe_strdup (guestfs_h *g, const char *str);
extern GUESTFS_DLL_PUBLIC void *guestfs_safe_memdup (guestfs_h *g, const void *ptr, size_t size);
#ifdef GUESTFS_PRIVATE_FOR_EACH_DISK
extern GUESTFS_DLL_PUBLIC int guestfs___for_each_disk (guestfs_h *g, virDomainPtr dom, int (*)(guestfs_h *g, const char *filename, const char *format, int readonly, void *data), void *data);
#endif
/* End of private functions. */
extern GUESTFS_DLL_PUBLIC void *guestfs___safe_malloc (guestfs_h *g, size_t nbytes);
extern GUESTFS_DLL_PUBLIC void *guestfs___safe_calloc (guestfs_h *g, size_t n, size_t s);
extern GUESTFS_DLL_PUBLIC char *guestfs___safe_strdup (guestfs_h *g, const char *str);
extern GUESTFS_DLL_PUBLIC void *guestfs___safe_memdup (guestfs_h *g, const void *ptr, size_t size);
extern GUESTFS_DLL_PUBLIC int guestfs___for_each_disk (guestfs_h *g, /* virDomainPtr */ void *dom, int (*)(guestfs_h *g, const char *filename, const char *format, int readonly, void *data), void *data);
#endif /* End of private functions. */
#ifdef __cplusplus
}
@@ -1594,10 +1597,10 @@ and generate_linker_script () =
(* Unofficial parts of the API: the bindings code use these
* functions, so it is useful to export them.
*)
"guestfs_safe_calloc";
"guestfs_safe_malloc";
"guestfs_safe_strdup";
"guestfs_safe_memdup";
"guestfs___safe_calloc";
"guestfs___safe_malloc";
"guestfs___safe_strdup";
"guestfs___safe_memdup";
"guestfs___for_each_disk";
] in
let functions =

View File

@@ -622,7 +622,7 @@ Java_com_redhat_et_libguestfs_GuestFS__1close
pr " %s_size = (*env)->GetArrayLength (env, j%s);\n" n n
| StringList n | DeviceList n ->
pr " %s_len = (*env)->GetArrayLength (env, j%s);\n" n n;
pr " %s = guestfs_safe_malloc (g, sizeof (char *) * (%s_len+1));\n" n n;
pr " %s = guestfs___safe_malloc (g, sizeof (char *) * (%s_len+1));\n" n n;
pr " for (i = 0; i < %s_len; ++i) {\n" n;
pr " jobject o = (*env)->GetObjectArrayElement (env, j%s, i);\n"
n;
@@ -648,7 +648,7 @@ Java_com_redhat_et_libguestfs_GuestFS__1close
n n
| OStringList n ->
pr " %s_len = (*env)->GetArrayLength (env, j%s);\n" n n;
pr " %s = guestfs_safe_malloc (g, sizeof (char *) * (%s_len+1));\n" n n;
pr " %s = guestfs___safe_malloc (g, sizeof (char *) * (%s_len+1));\n" n n;
pr " for (i = 0; i < %s_len; ++i) {\n" n;
pr " jobject o = (*env)->GetObjectArrayElement (env, j%s, i);\n"
n;

View File

@@ -503,14 +503,14 @@ copy_table (char * const * argv)
| FileOut n
| Key n ->
(* Copy strings in case the GC moves them: RHBZ#604691 *)
pr " char *%s = guestfs_safe_strdup (g, String_val (%sv));\n" n n
pr " char *%s = guestfs___safe_strdup (g, String_val (%sv));\n" n n
| OptString n ->
pr " char *%s =\n" n;
pr " %sv != Val_int (0) ?\n" n;
pr " guestfs_safe_strdup (g, String_val (Field (%sv, 0))) : NULL;\n" n
pr " guestfs___safe_strdup (g, String_val (Field (%sv, 0))) : NULL;\n" n
| BufferIn n ->
pr " size_t %s_size = caml_string_length (%sv);\n" n n;
pr " char *%s = guestfs_safe_memdup (g, String_val (%sv), %s_size);\n" n n n
pr " char *%s = guestfs___safe_memdup (g, String_val (%sv), %s_size);\n" n n n
| StringList n | DeviceList n ->
pr " char **%s = ocaml_guestfs_strings_val (g, %sv);\n" n n
| Bool n ->
@@ -539,7 +539,7 @@ copy_table (char * const * argv)
| OInt _ -> pr "Int_val (Field (%sv, 0))" n
| OInt64 _ -> pr "Int64_val (Field (%sv, 0))" n
| OString _ ->
pr "guestfs_safe_strdup (g, String_val (Field (%sv, 0)))" n
pr "guestfs___safe_strdup (g, String_val (Field (%sv, 0)))" n
| OStringList n ->
pr "ocaml_guestfs_strings_val (g, Field (%sv, 0))\n" n
);

View File

@@ -170,7 +170,7 @@ get_all_event_callbacks (guestfs_h *g, size_t *len_rtn)
}
/* Copy them into the return array. */
r = guestfs_safe_malloc (g, sizeof (SV *) * (*len_rtn));
r = guestfs___safe_malloc (g, sizeof (SV *) * (*len_rtn));
i = 0;
cb = guestfs_first_private (g, &key);
@@ -458,7 +458,7 @@ user_cancel (g)
pr " /* Note av_len returns index of final element. */\n";
pr " len = av_len (av) + 1;\n";
pr "\n";
pr " r = guestfs_safe_malloc (g, (len+1) * sizeof (char *));\n";
pr " r = guestfs___safe_malloc (g, (len+1) * sizeof (char *));\n";
pr " for (i = 0; i < len; ++i) {\n";
pr " svp = av_fetch (av, i, 0);\n";
pr " r[i] = SvPV_nolen (*svp);\n";

View File

@@ -207,7 +207,7 @@ ruby_set_event_callback (VALUE gv, VALUE cbv, VALUE event_bitmaskv)
event_bitmask = NUM2ULL (event_bitmaskv);
root = guestfs_safe_malloc (g, sizeof *root);
root = guestfs___safe_malloc (g, sizeof *root);
*root = cbv;
eh = guestfs_set_event_callback (g, ruby_event_callback_wrapper,
@@ -352,7 +352,7 @@ get_all_event_callbacks (guestfs_h *g, size_t *len_rtn)
}
/* Copy them into the return array. */
r = guestfs_safe_malloc (g, sizeof (VALUE *) * (*len_rtn));
r = guestfs___safe_malloc (g, sizeof (VALUE *) * (*len_rtn));
i = 0;
root = guestfs_first_private (g, &key);

View File

@@ -77,9 +77,10 @@ libguestfs_jni_la_SOURCES = \
libguestfs_jni_la_LIBADD = $(top_builddir)/src/libguestfs.la
libguestfs_jni_la_LDFLAGS = -version-info $(JNI_VERSION_INFO) -shared
libguestfs_jni_la_CFLAGS = \
$(WARN_CFLAGS) $(WERROR_CFLAGS) \
-I$(top_srcdir)/src -I$(top_builddir)/src \
$(JNI_CFLAGS)
-DGUESTFS_PRIVATE_FUNCTIONS=1 \
$(WARN_CFLAGS) $(WERROR_CFLAGS) \
-I$(top_srcdir)/src -I$(top_builddir)/src \
$(JNI_CFLAGS)
BUILT_SOURCES = com_redhat_et_libguestfs_GuestFS.h

View File

@@ -64,6 +64,7 @@ mlguestfs.cmxa: libguestfsocaml.a guestfs.cmx
-L$(top_builddir)/src/.libs -lguestfs
libguestfsocaml_a_CFLAGS = \
-DGUESTFS_PRIVATE_FUNCTIONS=1 \
-I$(top_builddir) -I$(OCAMLLIB) -I$(top_srcdir)/ocaml \
-I$(top_srcdir)/src -I$(top_builddir)/src \
$(WARN_CFLAGS) $(WERROR_CFLAGS) \

View File

@@ -168,7 +168,7 @@ ocaml_guestfs_create (value environmentv, value close_on_exitv, value unitv)
/* Store the OCaml handle into the C handle. This is only so we can
* map the C handle to the OCaml handle in event_callback_wrapper.
*/
v = guestfs_safe_malloc (g, sizeof *v);
v = guestfs___safe_malloc (g, sizeof *v);
*v = gv;
/* XXX This global root is generational, but we cannot rely on every
* user having the OCaml 3.11 version which supports this.
@@ -201,9 +201,9 @@ ocaml_guestfs_strings_val (guestfs_h *g, value sv)
char **r;
size_t i;
r = guestfs_safe_malloc (g, sizeof (char *) * (Wosize_val (sv) + 1));
r = guestfs___safe_malloc (g, sizeof (char *) * (Wosize_val (sv) + 1));
for (i = 0; i < Wosize_val (sv); ++i)
r[i] = guestfs_safe_strdup (g, String_val (Field (sv, i)));
r[i] = guestfs___safe_strdup (g, String_val (Field (sv, i)));
r[i] = NULL;
CAMLreturnT (char **, r);
@@ -246,7 +246,7 @@ ocaml_guestfs_set_event_callback (value gv, value closure, value events)
event_bitmask = event_bitmask_of_event_list (events);
value *root = guestfs_safe_malloc (g, sizeof *root);
value *root = guestfs___safe_malloc (g, sizeof *root);
*root = closure;
eh = guestfs_set_event_callback (g, event_callback_wrapper,
@@ -309,7 +309,7 @@ get_all_event_callbacks (guestfs_h *g, size_t *len_rtn)
}
/* Copy them into the return array. */
r = guestfs_safe_malloc (g, sizeof (value *) * (*len_rtn));
r = guestfs___safe_malloc (g, sizeof (value *) * (*len_rtn));
i = 0;
root = guestfs_first_private (g, &key);

View File

@@ -27,5 +27,5 @@ WriteMakefile (
LIBS => '-L@top_builddir@/src/.libs -lguestfs',
INC => '-I@top_builddir@/src -I@top_srcdir@/src',
TYPEMAPS => [ '@srcdir@/typemap' ],
CCFLAGS => $Config{ccflags} . ' @CFLAGS@',
CCFLAGS => $Config{ccflags} . ' -DGUESTFS_PRIVATE_FUNCTIONS=1 @CFLAGS@',
);

View File

@@ -40,8 +40,11 @@ python_DATA = guestfs.py
python_LTLIBRARIES = libguestfsmod.la
libguestfsmod_la_SOURCES = guestfs-py.c guestfs-py.h guestfs-py-byhand.c
libguestfsmod_la_CFLAGS = -Wall -I$(PYTHON_INCLUDEDIR) \
-I$(top_srcdir)/src -I$(top_builddir)/src
libguestfsmod_la_CFLAGS = \
-DGUESTFS_PRIVATE_FUNCTIONS=1 \
-Wall \
-I$(PYTHON_INCLUDEDIR) \
-I$(top_srcdir)/src -I$(top_builddir)/src
libguestfsmod_la_LIBADD = $(top_builddir)/src/libguestfs.la
libguestfsmod_la_LDFLAGS = -avoid-version -shared

View File

@@ -219,7 +219,7 @@ get_all_event_callbacks (guestfs_h *g, size_t *len_rtn)
}
/* Copy them into the return array. */
r = guestfs_safe_malloc (g, sizeof (PyObject *) * (*len_rtn));
r = guestfs___safe_malloc (g, sizeof (PyObject *) * (*len_rtn));
i = 0;
cb = guestfs_first_private (g, &key);

View File

@@ -195,6 +195,7 @@ libguestfs_la_LIBADD += liberrnostring.la libprotocol.la
libguestfs_la_CFLAGS = \
-DGUESTFS_DEFAULT_PATH='"$(libdir)/guestfs"' \
-DGUESTFS_WARN_DEPRECATED=1 \
-DGUESTFS_PRIVATE_FUNCTIONS=1 \
-DLIBOSINFO_DB_PATH='"$(datadir)/libosinfo/db"' \
$(WARN_CFLAGS) $(WERROR_CFLAGS) \
$(GPROF_CFLAGS) $(GCOV_CFLAGS) \

View File

@@ -27,7 +27,7 @@
#include "guestfs-internal.h"
void *
guestfs_safe_malloc (guestfs_h *g, size_t nbytes)
guestfs___safe_malloc (guestfs_h *g, size_t nbytes)
{
void *ptr = malloc (nbytes);
if (nbytes > 0 && !ptr) g->abort_cb ();
@@ -60,7 +60,7 @@ guestfs_safe_malloc (guestfs_h *g, size_t nbytes)
/* Allocate zeroed memory for N elements of S bytes, with error
checking. S must be nonzero. */
void *
guestfs_safe_calloc (guestfs_h *g, size_t n, size_t s)
guestfs___safe_calloc (guestfs_h *g, size_t n, size_t s)
{
/* From gnulib's calloc function in xmalloc.c. */
void *p;
@@ -75,7 +75,7 @@ guestfs_safe_calloc (guestfs_h *g, size_t n, size_t s)
}
void *
guestfs_safe_realloc (guestfs_h *g, void *ptr, size_t nbytes)
guestfs___safe_realloc (guestfs_h *g, void *ptr, size_t nbytes)
{
void *p = realloc (ptr, nbytes);
if (nbytes > 0 && !p) g->abort_cb ();
@@ -83,7 +83,7 @@ guestfs_safe_realloc (guestfs_h *g, void *ptr, size_t nbytes)
}
char *
guestfs_safe_strdup (guestfs_h *g, const char *str)
guestfs___safe_strdup (guestfs_h *g, const char *str)
{
char *s = strdup (str);
if (!s) g->abort_cb ();
@@ -91,7 +91,7 @@ guestfs_safe_strdup (guestfs_h *g, const char *str)
}
char *
guestfs_safe_strndup (guestfs_h *g, const char *str, size_t n)
guestfs___safe_strndup (guestfs_h *g, const char *str, size_t n)
{
char *s = strndup (str, n);
if (!s) g->abort_cb ();
@@ -99,7 +99,7 @@ guestfs_safe_strndup (guestfs_h *g, const char *str, size_t n)
}
void *
guestfs_safe_memdup (guestfs_h *g, const void *ptr, size_t size)
guestfs___safe_memdup (guestfs_h *g, const void *ptr, size_t size)
{
void *p = malloc (size);
if (!p) g->abort_cb ();
@@ -108,7 +108,7 @@ guestfs_safe_memdup (guestfs_h *g, const void *ptr, size_t size)
}
char *
guestfs_safe_asprintf (guestfs_h *g, const char *fs, ...)
guestfs___safe_asprintf (guestfs_h *g, const char *fs, ...)
{
va_list args;
char *msg;

View File

@@ -57,8 +57,8 @@ guestfs_set_event_callback (guestfs_h *g,
int event_handle = (int) g->nr_events;
g->events =
guestfs_safe_realloc (g, g->events,
(g->nr_events+1) * sizeof (struct event));
safe_realloc (g, g->events,
(g->nr_events+1) * sizeof (struct event));
g->nr_events++;
g->events[event_handle].event_bitmask = event_bitmask;
@@ -242,8 +242,8 @@ replace_old_style_event_callback (guestfs_h *g,
/* i == g->nr_events */
g->events =
guestfs_safe_realloc (g, g->events,
(g->nr_events+1) * sizeof (struct event));
safe_realloc (g, g->events,
(g->nr_events+1) * sizeof (struct event));
g->nr_events++;
replace:

View File

@@ -459,20 +459,20 @@ struct guestfs_message_error;
struct guestfs_progress;
/* alloc.c */
extern void *guestfs_safe_realloc (guestfs_h *g, void *ptr, size_t nbytes);
extern char *guestfs_safe_strdup (guestfs_h *g, const char *str);
extern char *guestfs_safe_strndup (guestfs_h *g, const char *str, size_t n);
extern void *guestfs_safe_memdup (guestfs_h *g, const void *ptr, size_t size);
extern char *guestfs_safe_asprintf (guestfs_h *g, const char *fs, ...)
extern void *guestfs___safe_realloc (guestfs_h *g, void *ptr, size_t nbytes);
extern char *guestfs___safe_strdup (guestfs_h *g, const char *str);
extern char *guestfs___safe_strndup (guestfs_h *g, const char *str, size_t n);
extern void *guestfs___safe_memdup (guestfs_h *g, const void *ptr, size_t size);
extern char *guestfs___safe_asprintf (guestfs_h *g, const char *fs, ...)
__attribute__((format (printf,2,3)));
#define safe_calloc guestfs_safe_calloc
#define safe_malloc guestfs_safe_malloc
#define safe_realloc guestfs_safe_realloc
#define safe_strdup guestfs_safe_strdup
#define safe_strndup guestfs_safe_strndup
#define safe_memdup guestfs_safe_memdup
#define safe_asprintf guestfs_safe_asprintf
#define safe_calloc guestfs___safe_calloc
#define safe_malloc guestfs___safe_malloc
#define safe_realloc guestfs___safe_realloc
#define safe_strdup guestfs___safe_strdup
#define safe_strndup guestfs___safe_strndup
#define safe_memdup guestfs___safe_memdup
#define safe_asprintf guestfs___safe_asprintf
/* errors.c */
extern void guestfs___init_error_handler (guestfs_h *g);

View File

@@ -33,10 +33,6 @@
#include <libxml/tree.h>
#endif
#if defined(HAVE_LIBVIRT) && defined(HAVE_LIBXML2)
#define GUESTFS_PRIVATE_FOR_EACH_DISK 1
#endif
#include "guestfs.h"
#include "guestfs-internal.h"
#include "guestfs-internal-actions.h"
@@ -161,16 +157,20 @@ guestfs__add_domain (guestfs_h *g, const char *domain_name,
* The callback function 'f' is called once for each disk.
*
* Returns number of disks, or -1 if there was an error.
*
* 'domv' is declared void* for convenience so it can appear as a
* private function in the public <guestfs.h> header.
*/
int
guestfs___for_each_disk (guestfs_h *g,
virDomainPtr dom,
void *domv,
int (*f) (guestfs_h *g,
const char *filename, const char *format,
int readonly,
void *data),
void *data)
{
virDomainPtr dom = domv;
int i, nr_added = 0, r = -1;
virErrorPtr err;
xmlDocPtr doc = NULL;