Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
CC ?= clang
TERMUX_BASE_DIR ?= /data/data/com.termux/files
CFLAGS += -Wall -Wextra -Werror -Wshadow -fvisibility=hidden -std=c23 -D__USE_GNU
C_SOURCE := src/termux-exec.c src/exec-variants.c src/termux-readlink.c
C_SOURCE := src/termux-exec.c src/exec-variants.c src/termux-readlink.c src/termux-realpath.c
CLANG_FORMAT := clang-format --sort-includes --style="{ColumnLimit: 120}" $(C_SOURCE) tests/fexecve.c tests/system-uname.c tests/print-argv0.c tests/popen.c
CLANG_TIDY ?= clang-tidy
TEST_BINARIES = tests/execl tests/exec-directory tests/fexecve tests/popen tests/system-uname tests/readlink-proc-self-exe
TEST_BINARIES = tests/execl tests/exec-directory tests/fexecve tests/popen tests/system-uname tests/readlink-proc-self-exe tests/realpath

ifeq ($(SANITIZE),1)
CFLAGS += -O1 -g -fsanitize=address -fno-omit-frame-pointer
Expand Down Expand Up @@ -39,6 +39,9 @@ tests/system-uname: tests/system-uname.c
tests/readlink-proc-self-exe: tests/readlink-proc-self-exe.c
$(CC) $(CFLAGS) -DTERMUX_BASE_DIR=\"$(TERMUX_BASE_DIR)\" $< -o $@

tests/realpath: tests/realpath.c
$(CC) $(CFLAGS) -DTERMUX_BASE_DIR=\"$(TERMUX_BASE_DIR)\" $< -o $@

$(TERMUX_BASE_DIR)/usr/bin/termux-exec-test-print-argv0: tests/print-argv0.c
$(CC) $(CFLAGS) $< -o $@

Expand Down
21 changes: 21 additions & 0 deletions src/termux-realpath.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#define _GNU_SOURCE
#include <dlfcn.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/syscall.h>
#include <unistd.h>

__attribute__((visibility("default"))) char *_Nullable realpath(const char *_Nonnull path, char *_Nullable resolved) {
char *(*orig_realpath)(const char *, char *);
orig_realpath = dlsym(RTLD_NEXT, "realpath");

if (strcmp(path, "/proc/self/exe") == 0) {
const char *termux_self_exe = getenv("TERMUX_EXEC__PROC_SELF_EXE");
if (termux_self_exe) {
path = termux_self_exe;
}
}

return orig_realpath(path, resolved);
}
20 changes: 20 additions & 0 deletions tests/realpath.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#define _POSIX_C_SOURCE 1
#define _XOPEN_SOURCE 500
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Wrong arguments\n");
}
char buf[PATH_MAX];
char *res = realpath(argv[1], buf);
if (res) {
printf("%s\n", buf);
} else {
perror("realpath()");
exit(EXIT_FAILURE);
}
return 0;
}
27 changes: 27 additions & 0 deletions tests/realpath.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
TMPDIR=$(mktemp -d)

cp tests/realpath $TMPDIR

cd $TMPDIR

ACTUAL_PATH_TO_SELF=$(./realpath /proc/self/exe)
EXPECTED_PATH_TO_SELF=$TMPDIR/realpath

if [ "$ACTUAL_PATH_TO_SELF" != "$EXPECTED_PATH_TO_SELF" ]; then
echo "ERROR(1): Expected '$EXPECTED_PATH_TO_SELF', was '$ACTUAL_PATH_TO_SELF'"
exit 1
fi

ACTUAL_PATH_TO_SELF=$(./realpath /$TMPDIR)
EXPECTED_PATH_TO_SELF=$TMPDIR

if [ "$ACTUAL_PATH_TO_SELF" != "$EXPECTED_PATH_TO_SELF" ]; then
echo "ERROR(1): Expected '$EXPECTED_PATH_TO_SELF', was '$ACTUAL_PATH_TO_SELF'"
exit 1
fi

cd - > /dev/null

rm -rf $TMPDIR

echo ok
1 change: 1 addition & 0 deletions tests/realpath.sh-expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ok