Skip to content

Commit

Permalink
Added versioning to library
Browse files Browse the repository at this point in the history
  • Loading branch information
dtcooper committed Jan 5, 2018
1 parent e24b3ad commit 74a0792
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ all: $(LIB_FILE) $(CMD_FILE)
%.$(LIB_SUFFIX): %.c
$(CC) $(CFLAGS) $(CLIBFLAGS) $(CDEFS) $< -o $@ $(LDLIBS)

$(CMD_FILE): $(CMD_FILE).c
$(CC) $(CFLAGS) $(CDEFS) $< -o $@ $(LDLIBS)

%: %.c
$(CC) $(CFLAGS) $(CDEFS) $< -o $@

Expand Down
7 changes: 6 additions & 1 deletion extras/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ if [ "$IS_LINUX" ]; then
else
ACTUAL="$(DYLD_INSERT_LIBRARIES="./time_preload.dylib" DYLD_FORCE_FLAT_NAMESPACE=1 ./fakehostname hi ./example all | strip_newlines)"
fi
run_test "Preserve pre-existing preload (time)" "$EXPECTED" "$ACTUAL"
run_test "preserve pre-existing preload (time)" "$EXPECTED" "$ACTUAL"

GIT_VER="$(git describe --tags --always --dirty 2>/dev/null || echo unknown)"
EXPECTED="fakehostname version: $GIT_VER|libfakehostname version: $GIT_VER (./libfakehostname.$LIB_SUFFIX)"
ACTUAL="$(./fakehostname -V | strip_newlines)"
run_test "version check" "$EXPECTED" "$ACTUAL"

echo
if [ "$FAILED" -gt 0 ]; then
Expand Down
32 changes: 29 additions & 3 deletions src/fakehostname.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <dlfcn.h>
#include <errno.h>
#include <getopt.h>
#include <libgen.h>
Expand Down Expand Up @@ -62,11 +63,30 @@ char *get_lib_path() {
exit(EXIT_FAILURE);
}

void print_version() {
printf("fakehostname version: " FAKE_HOSTNAME_VERSION "\n");

char *lib_path = get_lib_path();
void *lib_handle = dlopen(lib_path, RTLD_LAZY);
if (!lib_handle) {
printf("Error opening library %s: \"%s\"\n", lib_path, dlerror());
return;
}

char **lib_version = dlsym(lib_handle, "version");

char *error;
if ((error = dlerror()) != NULL) {
printf("Error loading \"version\" from %s: \"%s\"\n", lib_path, error);
return;
}

printf("libfakehostname version: %s (%s)\n", *lib_version, lib_path);
}

void usage(char *cmd_name, int exit_code) {
printf(
"fakehostname version " FAKE_HOSTNAME_VERSION "\n\n"
"Usage: %s [-h"
"\nUsage: %s [-h"
#ifdef ENABLE_VERBOSE
"v"
#endif
Expand All @@ -86,6 +106,10 @@ void usage(char *cmd_name, int exit_code) {
" -V, --version Print version information and exit\n\n"
"...and remember kids, have fun!\n\n",
basename(cmd_name));
#ifdef ENABLE_VERBOSE
verbose = 0; //shutup possible verbose output in print_version() here
#endif
print_version();
exit(exit_code);
}

Expand Down Expand Up @@ -122,7 +146,7 @@ int parse_options(int argc, char **argv) {
break;
#endif
case 'V':
printf("fakehostname version " FAKE_HOSTNAME_VERSION "\n");
print_version();
exit(EXIT_SUCCESS);
case '?':
default:
Expand All @@ -134,6 +158,8 @@ int parse_options(int argc, char **argv) {
usage(argv[0], EXIT_FAILURE);
}

VERBOSE("Version: " FAKE_HOSTNAME_VERSION "\n");

new_hostname = argv[optind];
VERBOSE("Faking hostname: %s\n", new_hostname)
return optind + 1;
Expand Down
6 changes: 5 additions & 1 deletion src/libfakehostname.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@
static int (*__orig_gethostname)(char *name, size_t len) = NULL;
static int (*__orig_uname)(struct utsname *buf) = NULL;

char *version = FAKE_HOSTNAME_VERSION;

#ifdef ENABLE_VERBOSE
static int verbose = -1;
#define VERBOSE(...) \
if (verbose == -1) { \
verbose = ((getenv(ENV_VARNAME_ENABLE_VERBOSE) == NULL) ? 0 : 1); \
if (verbose) \
if (verbose) { \
fprintf(stderr, "libfakehostname: Version %s\n", version); \
fprintf(stderr, "libfakehostname: \"" ENV_VARNAME_ENABLE_VERBOSE \
"\" set. Enabling verbose mode.\n"); \
} \
} \
if (verbose) \
fprintf(stderr, "libfakehostname: " __VA_ARGS__);
Expand Down

0 comments on commit 74a0792

Please sign in to comment.