From f2b90b374fe7d8e111cd9f4a0e840fa55b91f4f6 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Thu, 11 Apr 2013 12:19:00 +0100 Subject: [PATCH] New APIs: set-program, get-program. These set or get the program name in the handle. Most programs will never need to call this, since we set this, if possible, using the glibc 'program_invocation_short_name(3)' feature. --- configure.ac | 3 +++ generator/actions.ml | 26 ++++++++++++++++++++++++++ src/guestfs-internal.h | 2 ++ src/handle.c | 34 +++++++++++++++++++++++++++++++++- test-tool/test-tool.c | 1 + 5 files changed, 65 insertions(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 32cdaab77..f94a4117c 100644 --- a/configure.ac +++ b/configure.ac @@ -272,6 +272,9 @@ AC_CHECK_MEMBER([struct stat.st_blksize],[ dnl Define a C symbol for the host CPU architecture. AC_DEFINE_UNQUOTED([host_cpu],["$host_cpu"],[Host architecture.]) +dnl Check if libc has program_invocation_short_name. +AC_CHECK_DECLS([program_invocation_short_name], [], [], [#include ]) + dnl Headers. AC_CHECK_HEADERS([\ attr/xattr.h \ diff --git a/generator/actions.ml b/generator/actions.ml index 233590f59..2429789f2 100644 --- a/generator/actions.ml +++ b/generator/actions.ml @@ -2851,6 +2851,32 @@ In a graphical program, when the main thread is displaying a progress bar with a cancel button, wire up the cancel button to call this function." }; + { defaults with + name = "set_program"; + style = RErr, [String "program"], []; + fish_alias = ["program"]; + blocking = false; + shortdesc = "set the program name"; + longdesc = "\ +Set the program name. This is an informative string which the +main program may optionally set in the handle. + +When the handle is created, the program name in the handle is +set to the basename from C. If that was not possible, +it is set to the empty string (but never C)." }; + + { defaults with + name = "get_program"; + style = RConstString "program", [], []; + blocking = false; + tests = [ + InitNone, Always, TestRun ( + [["get_program"]]) + ]; + shortdesc = "get the program name"; + longdesc = "\ +Get the program name. See C." }; + ] (* daemon_functions are any functions which cause some action diff --git a/src/guestfs-internal.h b/src/guestfs-internal.h index 34d3bd78e..15326fc13 100644 --- a/src/guestfs-internal.h +++ b/src/guestfs-internal.h @@ -274,6 +274,8 @@ struct guestfs_h struct qemu_param *qemu_params; /* Extra qemu parameters. */ + char *program; /* Program name. */ + /* Array of drives added by add-drive* APIs. * * Before launch this list can be empty or contain some drives. diff --git a/src/handle.c b/src/handle.c index 5cc6e90c6..1c3fb9656 100644 --- a/src/handle.c +++ b/src/handle.c @@ -22,6 +22,7 @@ #include #include #include +#include #ifdef HAVE_LIBVIRT #include @@ -115,6 +116,19 @@ guestfs_create_flags (unsigned flags, ...) g->qemu = strdup (QEMU); if (!g->qemu) goto error; + /* Get program name. */ +#if defined(HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME) && \ + HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME == 1 + if (STRPREFIX (program_invocation_short_name, "lt-")) + /* Remove libtool (lt-*) prefix from short name. */ + g->program = strdup (program_invocation_short_name + 3); + else + g->program = strdup (program_invocation_short_name); +#else + g->program = strdup (""); +#endif + if (!g->program) goto error; + if (parse_backend (g, DEFAULT_BACKEND) == -1) { warning (g, _("libguestfs was built with an invalid default backend, using 'direct' instead")); g->backend = BACKEND_DIRECT; @@ -137,12 +151,14 @@ guestfs_create_flags (unsigned flags, ...) gl_lock_unlock (handles_lock); } - debug (g, "create: flags = %u, handle = %p", flags, g); + debug (g, "create: flags = %u, handle = %p, program = %s", + flags, g, g->program); return g; error: free (g->backend_arg); + free (g->program); free (g->path); free (g->qemu); free (g->append); @@ -338,6 +354,7 @@ guestfs_close (guestfs_h *g) free (g->int_tmpdir); free (g->int_cachedir); free (g->last_error); + free (g->program); free (g->path); free (g->qemu); free (g->append); @@ -561,6 +578,21 @@ guestfs__get_network (guestfs_h *g) return g->enable_network; } +int +guestfs__set_program (guestfs_h *g, const char *program) +{ + free (g->program); + g->program = safe_strdup (g, program); + + return 0; +} + +const char * +guestfs__get_program (guestfs_h *g) +{ + return g->program; +} + static int parse_backend (guestfs_h *g, const char *method) { diff --git a/test-tool/test-tool.c b/test-tool/test-tool.c index 3f4d36644..88d8aacd4 100644 --- a/test-tool/test-tool.c +++ b/test-tool/test-tool.c @@ -256,6 +256,7 @@ main (int argc, char *argv[]) printf ("guestfs_get_network: %d\n", guestfs_get_network (g)); printf ("guestfs_get_path: %s\n", guestfs_get_path (g) ? : "(null)"); printf ("guestfs_get_pgroup: %d\n", guestfs_get_pgroup (g)); + printf ("guestfs_get_program: %s\n", guestfs_get_program (g)); printf ("guestfs_get_qemu: %s\n", guestfs_get_qemu (g)); printf ("guestfs_get_recovery_proc: %d\n", guestfs_get_recovery_proc (g)); printf ("guestfs_get_selinux: %d\n", guestfs_get_selinux (g));