From be2c9cf238ec1d048e3b2d05f1b56a9f2a267668 Mon Sep 17 00:00:00 2001 From: Victor Westerhuis Date: Tue, 7 Dec 2021 22:41:16 +0100 Subject: Use /proc/self/exe to detect colm build directory The canonical identity of a file on Unix is the combination of device and inode numbers. This code checks both possible paths for the colm executable: directly in the build directory (when colm is linked against libcolm statically) or in libtool's `objdir` subdirectory (when colm is linked against libcolm dynamically). This fails in two situations I can see: when /proc is not mounted or doesn't exist, or when colm is installed using a hard link. --- src/Makefile.am | 4 ++-- src/main.cc | 19 ++++++++++++++----- 2 files changed, 16 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 895a46d6..b336fdfe 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -54,7 +54,8 @@ common_CFLAGS = \ -Wall \ -DINCLUDEDIR='"$(includedir)"' \ -DLIBDIR='"$(libdir)"' \ - -DABS_TOP_BUILDDIR='"$(abs_top_builddir)"' + -DABS_TOP_BUILDDIR='"$(abs_top_builddir)"' \ + -DABS_BUILDDIR='"$(abs_builddir)"' libprog_a_SOURCES = \ buffer.h bytecode.h colm.h debug.h dotgen.h fsmcodegen.h fsmgraph.h \ @@ -214,4 +215,3 @@ colm-wrap: colm-wrap.sh loadinit.cc: gen/if1.h loadboot2.cc: gen/if2.h loadcolm.cc: gen/if3.h - diff --git a/src/main.cc b/src/main.cc index 482fb4a4..a54a6dd8 100644 --- a/src/main.cc +++ b/src/main.cc @@ -688,14 +688,23 @@ void defaultBuildDir() if ( buildDir != 0 ) return; - const char *ldlp = getenv("LD_LIBRARY_PATH"); - if ( ldlp != 0 ) { - size_t len_atbd = strlen(ABS_TOP_BUILDDIR); + struct stat self; + if ( stat( "/proc/self/exe", &self ) == 0 ) + { + struct stat colm; + + if ( stat ( ABS_BUILDDIR "/" LT_OBJDIR "colm", &colm ) == 0 && + self.st_dev == colm.st_dev && self.st_ino == colm.st_ino ) + { + buildDir = ABS_TOP_BUILDDIR; + return; + } - if ( strlen(ldlp) > len_atbd && - memcmp( ldlp, ABS_TOP_BUILDDIR "/", len_atbd+1) == 0 ) + if ( stat ( ABS_BUILDDIR "/colm", &colm ) == 0 && + self.st_dev == colm.st_dev && self.st_ino == colm.st_ino ) { buildDir = ABS_TOP_BUILDDIR; + return; } } } -- cgit v1.2.1