Skip to content

Commit 4d4eff9

Browse files
committed
termux-usb: add option to pass file descriptor as env var
And remove termux-callback shell script, we can implement that function in termux-api.c.
1 parent 715cc4d commit 4d4eff9

4 files changed

Lines changed: 28 additions & 11 deletions

File tree

Makefile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ termux-api: termux-api.c
66
install: termux-api
77
mkdir -p $(PREFIX)/bin/ $(PREFIX)/libexec/
88
install termux-api $(PREFIX)/libexec/
9-
install termux-callback $(PREFIX)/libexec/
109
cd scripts; for i in *; do \
11-
sed -e "s|@TERMUX_PREFIX@|$(PREFIX)|g" $$i > $(PREFIX)/bin/$$i; \
12-
chmod 700 $(PREFIX)/bin/$$i; \
10+
sed -e "s|@TERMUX_PREFIX@|$(PREFIX)|g" $$i > $(PREFIX)/bin/$$i; \
11+
chmod 700 $(PREFIX)/bin/$$i; \
1312
done
1413

1514
.PHONY: install

scripts/termux-usb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ show_usage () {
99
echo " -l list available devices"
1010
echo " -r show permission request dialog if necessary"
1111
echo " -e command execute the specified command with a file descriptor"
12-
echo " referring to the device as its argument"
12+
echo " referring to the device as an argument (unless -E"
13+
echo " argument is given)"
14+
echo " -E transfer file descriptor as env var instead of as"
15+
echo " command line argument"
1316
exit 0
1417
}
1518

@@ -23,6 +26,7 @@ do
2326
l) ACTION="list"; ((MASK |= 1));;
2427
r) PARAMS="$PARAMS --ez request true"; ((MASK |= 2));;
2528
e) ACTION="open"; export TERMUX_CALLBACK="$OPTARG"; ((MASK |= 2));;
29+
E) export TERMUX_EXPORT_FD=true;;
2630
?) echo "$SCRIPTNAME: illegal option -$OPTARG"; exit 1;
2731
esac
2832
done

termux-api.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,27 @@ _Noreturn void exec_am_broadcast(int argc, char** argv, char* input_address_stri
6969
_Noreturn void exec_callback(int fd)
7070
{
7171
char *fds;
72-
if(asprintf(&fds, "%d", fd) == -1) { perror("asprintf"); }
73-
execl(PREFIX "/libexec/termux-callback", "termux-callback", fds, NULL);
74-
perror("execl(\"" PREFIX "/libexec/termux-callback\")");
72+
char *callback_fun = getenv("TERMUX_CALLBACK");
73+
if (callback_fun == NULL) {
74+
perror("TERMUX_CALLBACK is not set");
75+
exit(1);
76+
}
77+
78+
if (asprintf(&fds, "%d", fd) == -1)
79+
perror("asprintf");
80+
81+
char errmsg[256];
82+
char *export_to_env = getenv("TERMUX_EXPORT_FD");
83+
if (strncmp(export_to_env, "true", 4) == 0) {
84+
if (setenv("TERMUX_USB_FD", fds, true) == -1)
85+
perror("setenv");
86+
execl(callback_fun, callback_fun, NULL);
87+
sprintf(errmsg, "execl(\"%s\", %s)", callback_fun, fds);
88+
} else {
89+
execl(callback_fun, callback_fun, fds, NULL);
90+
sprintf(errmsg, "execl(\"%s\")", callback_fun);
91+
}
92+
perror(errmsg);
7593
exit(1);
7694
}
7795

termux-callback

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)