Files
libguestfs/tests/c-api/test-dlopen.c
Richard W.M. Jones 2990db8d1a tests/c-api: Test that the library can really be loaded with dlopen.
Answer is yes, but it was good to know.
2014-11-28 22:26:57 +00:00

95 lines
2.5 KiB
C

/* libguestfs
* Copyright (C) 2014 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/* Test the library can be loaded and unloaded using dlopen etc. */
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <dlfcn.h>
/* We don't need the <guestfs.h> header file here. */
typedef struct guestfs_h guestfs_h;
typedef guestfs_h *(*guestfs_create_t) (void);
typedef const char * (*guestfs_get_program_t) (guestfs_h *);
typedef void (*guestfs_close_t) (guestfs_h *);
#ifndef LIBRARY
#error "-DLIBRARY was not defined"
#endif
static void *
read_symbol (void *lib, const char *symbol)
{
void *symval;
const char *err;
dlerror (); /* Clear error indicator. */
symval = dlsym (lib, symbol);
if ((err = dlerror ()) != NULL) {
fprintf (stderr, "could not read symbol: %s: %s\n", symbol, err);
exit (EXIT_FAILURE);
}
return symval;
}
int
main (int argc, char *argv[])
{
void *lib;
guestfs_create_t guestfs_create;
guestfs_get_program_t guestfs_get_program;
guestfs_close_t guestfs_close;
guestfs_h *g;
if (access (LIBRARY, X_OK) == -1) {
fprintf (stderr, "test skipped because %s cannot be accessed: %m\n",
LIBRARY);
exit (77);
}
lib = dlopen (LIBRARY, RTLD_LAZY);
if (lib == NULL) {
fprintf (stderr, "could not open %s: %s\n", LIBRARY, dlerror ());
exit (EXIT_FAILURE);
}
guestfs_create = read_symbol (lib, "guestfs_create");
guestfs_get_program = read_symbol (lib, "guestfs_get_program");
guestfs_close = read_symbol (lib, "guestfs_close");
g = guestfs_create ();
if (g == NULL) {
fprintf (stderr, "failed to create handle\n");
exit (EXIT_FAILURE);
}
printf ("program = %s\n", guestfs_get_program (g));
guestfs_close (g);
if (dlclose (lib) != 0) {
fprintf (stderr, "could not close %s: %s\n", LIBRARY, dlerror ());
exit (EXIT_FAILURE);
}
exit (EXIT_SUCCESS);
}