diff options
Diffstat (limited to 'usr/src/cmd/lofiadm')
| -rw-r--r-- | usr/src/cmd/lofiadm/Makefile | 2 | ||||
| -rw-r--r-- | usr/src/cmd/lofiadm/main.c | 30 | ||||
| -rw-r--r-- | usr/src/cmd/lofiadm/utils.c | 32 | ||||
| -rw-r--r-- | usr/src/cmd/lofiadm/utils.h | 13 |
4 files changed, 61 insertions, 16 deletions
diff --git a/usr/src/cmd/lofiadm/Makefile b/usr/src/cmd/lofiadm/Makefile index 0c7e9bd303..a8a4a2b4a1 100644 --- a/usr/src/cmd/lofiadm/Makefile +++ b/usr/src/cmd/lofiadm/Makefile @@ -33,7 +33,7 @@ POFILES= $(OBJS:%.o=%.po) include ../Makefile.cmd -LDLIBS += -ldevinfo -lz +LDLIBS += -ldevinfo .KEEP_STATE: diff --git a/usr/src/cmd/lofiadm/main.c b/usr/src/cmd/lofiadm/main.c index 0663e4501e..40ec555e7f 100644 --- a/usr/src/cmd/lofiadm/main.c +++ b/usr/src/cmd/lofiadm/main.c @@ -49,7 +49,7 @@ #include <libdevinfo.h> #include <libgen.h> #include <ctype.h> -#include <zlib.h> +#include <dlfcn.h> #include "utils.h" static const char USAGE[] = @@ -85,13 +85,35 @@ lofi_compress_info_t lofi_compress_table[LOFI_COMPRESS_FUNCTIONS] = { #define KILOBYTE 1024 #define MEGABYTE (KILOBYTE * KILOBYTE) #define GIGABYTE (KILOBYTE * MEGABYTE) +#define LIBZ "libz.so" + +static int (*compress2p)(void *, ulong_t *, void *, size_t, int) = NULL; static int gzip_compress(void *src, size_t srclen, void *dst, size_t *dstlen, int level) { - if (compress2(dst, (ulong_t *)dstlen, src, srclen, level) != Z_OK) - return (-1); + void *libz_hdl = NULL; + /* + * The first time we are called, attempt to dlopen() + * libz.so and get a pointer to the compress2() function + */ + if (compress2p == NULL) { + if ((libz_hdl = openlib(LIBZ)) == NULL) + die(gettext("could not find %s. " + "gzip compression unavailable\n"), LIBZ); + + if ((compress2p = + (int (*)(void *, ulong_t *, void *, size_t, int)) + dlsym(libz_hdl, "compress2")) == NULL) { + closelib(); + die(gettext("could not find the correct %s. " + "gzip compression unavailable\n"), LIBZ); + } + } + + if ((*compress2p)(dst, (ulong_t *)dstlen, src, srclen, level) != 0) + return (-1); return (0); } @@ -993,6 +1015,8 @@ main(int argc, char *argv[]) print_one_mapping(lfd, devicename, filename); else print_mappings(lfd); + (void) close(lfd); + closelib(); return (E_SUCCESS); } diff --git a/usr/src/cmd/lofiadm/utils.c b/usr/src/cmd/lofiadm/utils.c index eee1e5b5d8..349fab1c2b 100644 --- a/usr/src/cmd/lofiadm/utils.c +++ b/usr/src/cmd/lofiadm/utils.c @@ -2,9 +2,8 @@ * CDDL HEADER START * * The contents of this file are subject to the terms of the - * Common Development and Distribution License, Version 1.0 only - * (the "License"). You may not use this file except in compliance - * with the License. + * Common Development and Distribution License (the "License"). + * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. @@ -20,8 +19,8 @@ * CDDL HEADER END */ /* - * Copyright (c) 1999-2000 by Sun Microsystems, Inc. - * All rights reserved. + * Copyright 2007 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. */ #pragma ident "%Z%%M% %I% %E% SMI" @@ -37,9 +36,13 @@ #include <stdarg.h> #include <stdio.h> #include <errno.h> +#include <dlfcn.h> +#include <link.h> #include "utils.h" +static void *lib_hdl = NULL; + static const char PNAME_FMT[] = "%s: "; static const char ERRNO_FMT[] = ": %s\n"; @@ -112,3 +115,22 @@ valid_abspath(const char *p) return (1); } + +/* + * Wrapper for dlopen'ing a library. + * The caller must call closelib() once + * access to the library is no longer needed. + */ +void * +openlib(const char *lib) +{ + lib_hdl = dlopen(lib, RTLD_LAZY); + return (lib_hdl); +} + +void +closelib() +{ + if (lib_hdl != NULL) + (void) dlclose(lib_hdl); +} diff --git a/usr/src/cmd/lofiadm/utils.h b/usr/src/cmd/lofiadm/utils.h index 3b14ac2392..627ac3b264 100644 --- a/usr/src/cmd/lofiadm/utils.h +++ b/usr/src/cmd/lofiadm/utils.h @@ -2,9 +2,8 @@ * CDDL HEADER START * * The contents of this file are subject to the terms of the - * Common Development and Distribution License, Version 1.0 only - * (the "License"). You may not use this file except in compliance - * with the License. + * Common Development and Distribution License (the "License"). + * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. @@ -20,8 +19,8 @@ * CDDL HEADER END */ /* - * Copyright (c) 1999-2000 by Sun Microsystems, Inc. - * All rights reserved. + * Copyright 2007 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. */ #ifndef _UTILS_H @@ -40,10 +39,10 @@ extern "C" { #define E_USAGE 2 /* Exit status for usage error */ extern void die(const char *, ...); - extern const char *getpname(const char *); - extern int valid_abspath(const char *); +extern void *openlib(const char *); +extern void closelib(); #ifdef __cplusplus } |
