Skip to content

Commit be2c9cf

Browse files
viccie30adrian-thurston
authored andcommitted
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.
1 parent 4e5cb4f commit be2c9cf

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

Diff for: src/Makefile.am

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ common_CFLAGS = \
5454
-Wall \
5555
-DINCLUDEDIR='"$(includedir)"' \
5656
-DLIBDIR='"$(libdir)"' \
57-
-DABS_TOP_BUILDDIR='"$(abs_top_builddir)"'
57+
-DABS_TOP_BUILDDIR='"$(abs_top_builddir)"' \
58+
-DABS_BUILDDIR='"$(abs_builddir)"'
5859

5960
libprog_a_SOURCES = \
6061
buffer.h bytecode.h colm.h debug.h dotgen.h fsmcodegen.h fsmgraph.h \
@@ -214,4 +215,3 @@ colm-wrap: colm-wrap.sh
214215
loadinit.cc: gen/if1.h
215216
loadboot2.cc: gen/if2.h
216217
loadcolm.cc: gen/if3.h
217-

Diff for: src/main.cc

+14-5
Original file line numberDiff line numberDiff line change
@@ -688,14 +688,23 @@ void defaultBuildDir()
688688
if ( buildDir != 0 )
689689
return;
690690

691-
const char *ldlp = getenv("LD_LIBRARY_PATH");
692-
if ( ldlp != 0 ) {
693-
size_t len_atbd = strlen(ABS_TOP_BUILDDIR);
691+
struct stat self;
692+
if ( stat( "/proc/self/exe", &self ) == 0 )
693+
{
694+
struct stat colm;
695+
696+
if ( stat ( ABS_BUILDDIR "/" LT_OBJDIR "colm", &colm ) == 0 &&
697+
self.st_dev == colm.st_dev && self.st_ino == colm.st_ino )
698+
{
699+
buildDir = ABS_TOP_BUILDDIR;
700+
return;
701+
}
694702

695-
if ( strlen(ldlp) > len_atbd &&
696-
memcmp( ldlp, ABS_TOP_BUILDDIR "/", len_atbd+1) == 0 )
703+
if ( stat ( ABS_BUILDDIR "/colm", &colm ) == 0 &&
704+
self.st_dev == colm.st_dev && self.st_ino == colm.st_ino )
697705
{
698706
buildDir = ABS_TOP_BUILDDIR;
707+
return;
699708
}
700709
}
701710
}

0 commit comments

Comments
 (0)