Skip to content

Commit

Permalink
lightningd: fix up installs in subdirectories.
Browse files Browse the repository at this point in the history
Commit a1fdeee "Makefile: clean up install path handling."
broke the ability to configure with one path and then run in a
different path.  Turns out people actually do this!  So, we have
to use relative paths, compared to our existing binary.

And we can't use path_rel, because that requires that the path
exist (thanks @Lagrang3!).

Fixes: ElementsProject#7595
Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Aug 28, 2024
1 parent 48fa438 commit 624d8b7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ CPATH := /usr/local/include
LIBRARY_PATH := /usr/local/lib
endif

CPPFLAGS += -DCLN_NEXT_VERSION="\"$(CLN_NEXT_VERSION)\"" -DPKGLIBEXECDIR="\"$(pkglibexecdir)\"" -DPLUGINDIR="\"$(plugindir)\"" -DCCAN_TAL_NEVER_RETURN_NULL=1
CPPFLAGS += -DCLN_NEXT_VERSION="\"$(CLN_NEXT_VERSION)\"" -DPKGLIBEXECDIR="\"$(pkglibexecdir)\"" -DBINDIR="\"$(bindir)\"" -DPLUGINDIR="\"$(plugindir)\"" -DCCAN_TAL_NEVER_RETURN_NULL=1
CFLAGS = $(CPPFLAGS) $(CWARNFLAGS) $(CDEBUGFLAGS) $(COPTFLAGS) -I $(CCANDIR) $(EXTERNAL_INCLUDE_FLAGS) -I . -I$(CPATH) $(SQLITE3_CFLAGS) $(POSTGRES_INCLUDE) $(FEATURES) $(COVFLAGS) $(DEV_CFLAGS) -DSHACHAIN_BITS=48 -DJSMN_PARENT_LINKS $(PIE_CFLAGS) $(COMPAT_CFLAGS) $(CSANFLAGS)

# If CFLAGS is already set in the environment of make (to whatever value, it
Expand Down Expand Up @@ -886,7 +886,7 @@ uninstall:
installcheck: all-programs
@rm -rf testinstall || true
$(MAKE) DESTDIR=$$(pwd)/testinstall install
DEV_LIGHTNINGD_DESTDIR_PREFIX=$$(pwd)/testinstall/ testinstall$(bindir)/lightningd --test-daemons-only --lightning-dir=testinstall
testinstall$(bindir)/lightningd --test-daemons-only --lightning-dir=testinstall
$(MAKE) DESTDIR=$$(pwd)/testinstall uninstall
@if test `find testinstall '!' -type d | wc -l` -ne 0; then \
echo 'make uninstall left some files in testinstall directory!'; \
Expand Down
44 changes: 36 additions & 8 deletions lightningd/lightningd.c
Original file line number Diff line number Diff line change
Expand Up @@ -530,11 +530,37 @@ static const char *find_my_directory(const tal_t *ctx, const char *argv0)
return path_dirname(ctx, take(me));
}

/* How to get from abspath dir1 to abspath dir2? */
static const char *relative(const tal_t *ctx, const char *dir1, const char *dir2)
{
char **dir1_parts, **dir2_parts;
size_t common;
char *backwards;

/* Collapse double /, and split into parts */
dir1_parts = path_split(tmpctx, take(path_simplify(NULL, dir1)));
dir2_parts = path_split(tmpctx, take(path_simplify(NULL, dir2)));

for (common = 0;
dir1_parts[common]
&& dir2_parts[common]
&& streq(dir1_parts[common], dir2_parts[common]);
common++);

/* We append .. for every non-shared part in dir1_parts */
for (size_t i = common; dir1_parts[i]; i++)
dir1_parts[i] = "..";

backwards = tal_strjoin(tmpctx, dir1_parts + common, PATH_SEP_STR, STR_TRAIL);
return tal_fmt(ctx, "%s%s", backwards,
tal_strjoin(tmpctx, dir2_parts + common, PATH_SEP_STR, STR_NO_TRAIL));
}

/* Determine the correct daemon dir. */
static void find_subdaemons_and_plugins(struct lightningd *ld, const char *argv0)
{
const char *my_path = find_my_directory(tmpctx, argv0);
const char *prefix;
const char *rel;

/* If we're running in-tree, all the subdaemons are with lightningd. */
if (has_all_subdaemons(my_path)) {
Expand All @@ -547,14 +573,16 @@ static void find_subdaemons_and_plugins(struct lightningd *ld, const char *argv0
return;
}

/* Assume we're running the installed version. Override
* for "make installccheck" though. */
prefix = getenv("DEV_LIGHTNINGD_DESTDIR_PREFIX");
if (!prefix)
prefix = "";
ld->subdaemon_dir = tal_fmt(ld, "%s%s", prefix, PKGLIBEXECDIR);
/* OK, we're running the installed version? But we used to allow
* running in an arbitrary subtree, and people still do (plus,
* installcheck does this). So we use relative paths here. path_rel
* doesn't work if the paths don't exist, so we use our own function. */
rel = relative(NULL, BINDIR, PKGLIBEXECDIR);
ld->subdaemon_dir = path_join(ld, my_path, take(rel));

rel = relative(NULL, BINDIR, PLUGINDIR);
plugins_set_builtin_plugins_dir(ld->plugins,
tal_fmt(tmpctx, "%s%s", prefix, PLUGINDIR));
path_join(tmpctx, my_path, take(rel)));
}

/*~ We like to free everything on exit, so valgrind doesn't complain (valgrind
Expand Down

0 comments on commit 624d8b7

Please sign in to comment.