Skip to content

Commit

Permalink
moctl: add a shell wrapper
Browse files Browse the repository at this point in the history
This is a way to solve a problem when in the same source tree you have
one or more libraries, plus one or more binaries that are dynamically
linked against those libraries, and you want to be able to run those
binaries without installing those libraries first. Pretty natural
thing to do, but requires some minor trickery in the form of a shell
wrapper.

With such a wrapper, you don't have to worry about the above problem,
but there is a "side effect" that needs to be mentioned:

The binary is now not the real binary, but a shell wrapper.

The consequences are, for example:

* you can't use tools like ldd, gdb, strip against the non-installed binary
* you can't install the binary by mere copying -- use "make install"
* installed binary of course still requires installed libraries

Once you know this, you'll be fine and dandy.

Signed-off-by: Kir Kolyshkin <kir@openvz.org>
  • Loading branch information
kolyshkin committed Sep 17, 2015
1 parent dde0880 commit 271a4d2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
24 changes: 17 additions & 7 deletions moctl/Makefile
@@ -1,17 +1,27 @@
include ../Makefile.inc

TGT = moctl
LIB_PATH=../lib

BIN = moctl
REALBIN = .$(BIN)-real
OBJS =
OBJS += main.o
OBJS += create.o

CFLAGS += -fPIE -I../include

all: $(TGT)
all: $(BIN)
.PHONY: all

$(TGT): $(OBJS)
$(CC) $(CFLAGS) -o $@ $^ -lmosaic -L../lib/
$(BIN): wrapper.sh.in $(REALBIN)
sed \
-e 's|@REALBIN@|$(REALBIN)|g' \
-e 's|@LIB_PATH@|$(LIB_PATH)|g' \
< $< > $@ \
&& chmod a+x $@

$(REALBIN): $(OBJS)
$(CC) $(CFLAGS) -o $@ $^ -lmosaic -L $(LIB_PATH)

%.d: %.c
$(CC) $(CFLAGS) -M -MT $@ -MT $(patsubst %.d,%.o,$@) $< -o $@
Expand All @@ -23,11 +33,11 @@ endif
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<

install: $(TGT)
install: $(REALBIN)
install -d $(DESTDIR)$(BINDIR)
install -m 755 $(TGT) $(DESTDIR)$(BINDIR)
install -m 755 $(REALBIN) $(DESTDIR)$(BINDIR)/$(BIN)
.PHONY: install

clean:
rm -rf $(TGT) $(OBJS) *.d
rm -rf $(BIN) $(REALBIN) $(OBJS) *.d
.PHONY: clean
7 changes: 7 additions & 0 deletions moctl/wrapper.sh.in
@@ -0,0 +1,7 @@
#!/bin/sh

# This is a wrapper around @REALBIN@ to be able to run it with a library
# from @LIB_PATH@ that might not yet be installed into a proper place.

BASEDIR=$(dirname $0)
LD_LIBRARY_PATH=${BASEDIR}/@LIB_PATH@ exec ${BASEDIR}/@REALBIN@
1 change: 0 additions & 1 deletion test/env.sh
@@ -1,4 +1,3 @@
export LD_LIBRARY_PATH="$(pwd)/../lib/"
moctl="../moctl/moctl"
set -x

Expand Down

0 comments on commit 271a4d2

Please sign in to comment.