Skip to content

Commit

Permalink
Fix some basic issues
Browse files Browse the repository at this point in the history
  • Loading branch information
fornwall committed Sep 17, 2017
1 parent d2eed94 commit 48f9e3a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ for f in tests/*.sh; do
ACTUAL_FILE=$f-actual

rm -f $ACTUAL_FILE
$f $ACTUAL_FILE > $ACTUAL_FILE
$f myarg1 myarg2 > $ACTUAL_FILE

if cmp --silent $ACTUAL_FILE $EXPECTED_FILE; then
printf " OK\n"
Expand Down
33 changes: 19 additions & 14 deletions termux-exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <string.h>
#include <unistd.h>

const char* termux_rewrite_executable(const char* filename, char* buffer, int buffer_len)
static const char* termux_rewrite_executable(const char* filename, char* buffer, int buffer_len)
{
strcpy(buffer, "/data/data/com.termux/files/usr/bin/");
char* bin_match = strstr(filename, "/bin/");
Expand All @@ -27,6 +27,7 @@ const char* termux_rewrite_executable(const char* filename, char* buffer, int bu
int execve(const char* filename, char* const* argv, char *const envp[])
{
int fd = -1;
const char** new_argv = NULL;

char filename_buffer[512];
filename = termux_rewrite_executable(filename, filename_buffer, sizeof(filename_buffer));
Expand Down Expand Up @@ -62,34 +63,38 @@ int execve(const char* filename, char* const* argv, char *const envp[])
*whitespace_pos = 0;

// Find start of argument:
arg = whitespace_pos + 1;;
arg = whitespace_pos + 1;
while (*arg != 0 && *arg == ' ') arg++;
if (arg == newline_location) {
// Only whitespace after interpreter.
arg = NULL;
}
}

const char* new_argv[4];
new_argv[0] = basename(interpreter);

char interp_buf[512];
const char* new_interpreter = termux_rewrite_executable(interpreter, interp_buf, sizeof(interp_buf));
if (new_interpreter == interpreter) goto final;

if (arg) {
new_argv[1] = arg;
new_argv[2] = filename;
new_argv[3] = NULL;
} else {
new_argv[1] = filename;
new_argv[2] = NULL;
}
int orig_argv_count = 0;
while (argv[orig_argv_count] != NULL) orig_argv_count++;

new_argv = malloc(sizeof(char*) * (4 + orig_argv_count));

int current_argc = 0;
new_argv[current_argc++] = basename(interpreter);
if (arg) new_argv[current_argc++] = arg;
new_argv[current_argc++] = filename;
int i = 1;
while (orig_argv_count-- > 1) new_argv[current_argc++] = argv[i++];
new_argv[current_argc] = NULL;

filename = new_interpreter;
argv = (char**) new_argv;

final:
if (fd != -1) close(fd);
int (*real_execve)(const char*, char *const[], char *const[]) = dlsym(RTLD_NEXT, "execve");
return real_execve(filename, argv, envp);
int ret = real_execve(filename, argv, envp);
free(new_argv);
return ret;
}
2 changes: 1 addition & 1 deletion tests/args-with-spaces.sh-expected
Original file line number Diff line number Diff line change
@@ -1 +1 @@
hello world bye tests/args-with-spaces.sh
hello world bye tests/args-with-spaces.sh myarg1 myarg2
Empty file modified tests/usr-bin-env.sh
100644 → 100755
Empty file.

0 comments on commit 48f9e3a

Please sign in to comment.