From a645ec0fc0dd917edaeaab3883b42ea94f335aa7 Mon Sep 17 00:00:00 2001 From: Eduard Shishkin Date: Fri, 18 Jun 2021 15:33:24 +0200 Subject: [PATCH] libutil: make TOOLS_LIBDIR and TOOLS_DATADIR configurable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add generic functions util_sysdir(); util_sysdir_path() and their special cases supplied for users: util_libdir(); util_libdir_path(); util_datadir(); util_datadir_path() The function util_sysdir() determines the absolute name of a s390-tools system directory. It could be data, or library directory. The function util_sysdir_path() determines the absolute name of a file installed in the s390-tools system directory. The function util_libdir() determines the absolute name of a s390-tools library directory. By default that name is defined by the compile-time macro TOOLS_LIBDIR (/lib/s390-tools). Users can specify an override by setting environment variable S390TOOLS_LIBDIR. The function util_libdir_path() determines the absolute name of a file installed in the s390-tools library directory. The function util_datadir() determines the absolute name of a s390-tools system data directory. By default the name is defined by the compile-time macro TOOLS_DATADIR (/usr/share/s390-tools/). Users can specify an override by setting environment variable S390TOOLS_DATADIR. The function util_datadir_path() determines the absolute name of a file installed in the s390-tools data directory. The ability to override the setting for TOOLS_LIBDIR and TOOLS_DATADIR is required for implementing tests on tools that are not installed in their default system path locations. Signed-off-by: Eduard Shishkin Reviewed-by: Jan Höppner Signed-off-by: Jan Höppner --- include/lib/util_base.h | 4 ++ include/lib/zt_common.h | 1 + libutil/util_base.c | 90 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) diff --git a/include/lib/util_base.h b/include/lib/util_base.h index b1c5c5aa..0f4b12c5 100644 --- a/include/lib/util_base.h +++ b/include/lib/util_base.h @@ -20,6 +20,10 @@ void util_hexdump(FILE *fh, const char *tag, const void *data, int cnt); void util_hexdump_grp(FILE *fh, const char *tag, const void *data, int group, int cnt, int indent); void util_print_indented(const char *str, int indent); +const char *util_libdir(void); +const char *util_libdir_path(const char *filename); +const char *util_datadir(void); +const char *util_datadir_path(const char *filename); static inline void util_ptr_vec_free(void **ptr_vec, int count) { diff --git a/include/lib/zt_common.h b/include/lib/zt_common.h index 8f4f1641..cb8e30e2 100644 --- a/include/lib/zt_common.h +++ b/include/lib/zt_common.h @@ -52,6 +52,7 @@ #define TOOLS_LIBDIR STRINGIFY (S390_TOOLS_LIBDIR) #define TOOLS_SYSCONFDIR STRINGIFY (S390_TOOLS_SYSCONFDIR) #define TOOLS_BINDIR STRINGIFY (S390_TOOLS_BINDIR) +#define TOOLS_DATADIR STRINGIFY (S390_TOOLS_DATADIR) #define __noreturn __attribute__((noreturn)) #define __packed __attribute__((packed)) diff --git a/libutil/util_base.c b/libutil/util_base.c index eafbcaa9..802ca773 100644 --- a/libutil/util_base.c +++ b/libutil/util_base.c @@ -9,6 +9,8 @@ * it under the terms of the MIT license. See LICENSE for details. */ +#include +#include #include #include "lib/util_base.h" @@ -91,3 +93,91 @@ void util_print_indented(const char *str, int indent) printf("\n"); free(desc_ptr); } + +/** + * Determines the absolute name of an s390-tools system directory + * (it could be data, or library directory) + */ +static const char *util_sysdir(const char *env_var, const char *default_dir) +{ + return secure_getenv(env_var) ?: default_dir; +} + +/** + * Determines the absolute name of a file installed in the s390-tools + * system directory + */ +static const char *util_sysdir_path(const char *filename, const char *dirname) +{ + static char libdir_pathname[PATH_MAX]; + int ret; + + ret = snprintf(libdir_pathname, sizeof(libdir_pathname), + "%s/%s", dirname, filename); + + if (ret < 0 || ret >= (int)sizeof(libdir_pathname)) + errx(EXIT_FAILURE, + "Could not compose absolute pathname of %s and %s", + dirname, filename); + return libdir_pathname; +} + +/** + * Determines the absolute name of a s390-tools library directory + * + * Resources are handled by the library + * + * @returns Pointer to a buffer, which contains the null-terminated name + */ +const char *util_libdir(void) +{ + return util_sysdir("S390TOOLS_LIBDIR", TOOLS_LIBDIR); +} + +/** + * Determines the absolute name of a file installed in the s390-tools + * library directory by its relative name. + * + * Resources are handled by the library. A second call of this function + * overwrites previously returned data. Care must be taken when attempting + * to do a second call. + * + * @param[in] filename Null-terminated file name relative to the s390-tools + * library directory + * @returns Pointer to a buffer of PATH_MAX size, which contains the + * null-terminated absolute name + */ +const char *util_libdir_path(const char *filename) +{ + return util_sysdir_path(filename, util_libdir()); +} + +/** + * Determines the absolute name of a s390-tools data directory + * + * Resources are handled by the library + * + * @returns Pointer to a buffer which contains the null-terminated name + */ +const char *util_datadir(void) +{ + return util_sysdir("S390TOOLS_DATADIR", TOOLS_DATADIR); +} + +/** + * Determines the absolute name of a file installed in the s390-tools + * data directory by its relative name. + * + * Resources are handled by the library. A second call of this function + * overwrites previously returned data. Care must be taken when attempting + * to do a second call. + * + * @param[in] filename Null-terminated file name relative to the s390-tools + * data directory + * @returns Pointer to a buffer of PATH_MAX size, which contains the + * null-terminated absolute name + */ +const char *util_datadir_path(const char *filename) +{ + return util_sysdir_path(filename, util_datadir()); +}