Commit | Line | Data |
---|---|---|
59f0d218 FF |
1 | // termux-api.c - helper binary for calling termux api classes |
2 | // Usage: termux-api ${API_METHOD} ${ADDITIONAL_FLAGS} | |
3 | // This executes | |
4 | // am broadcast com.termux.api/.TermuxApiReceiver --es socket_input ${INPUT_SOCKET} | |
5 | // --es socket_output ${OUTPUT_SOCKET} | |
6 | // --es ${API_METHOD} | |
7 | // ${ADDITIONAL_FLAGS} | |
8 | // where ${INPUT_SOCKET} and ${OUTPUT_SOCKET} are addresses to linux abstract namespace sockets, | |
9 | // used to pass on stdin to the java implementation and pass back output from java to stdout. | |
10 | #define _POSIX_SOURCE | |
11 | #define _GNU_SOURCE | |
12 | #include <fcntl.h> | |
13 | #include <pthread.h> | |
14 | #include <signal.h> | |
15 | #include <stdbool.h> | |
16 | #include <stdio.h> | |
17 | #include <stdlib.h> | |
18 | #include <string.h> | |
19 | #include <sys/socket.h> | |
20 | #include <sys/stat.h> | |
21 | #include <sys/types.h> | |
22 | #include <sys/un.h> | |
23 | #include <time.h> | |
24 | #include <unistd.h> | |
25 | ||
26 | // Function which execs "am broadcast ..". | |
27 | void exec_am_broadcast(int argc, char** argv, char* input_address_string, char* output_address_string) | |
28 | { | |
bb3266ca FF |
29 | // Redirect stdout to /dev/null (but leave stderr open): |
30 | close(STDOUT_FILENO); | |
31 | open("/dev/null", O_RDONLY); | |
32 | // Close stdin: | |
33 | close(STDIN_FILENO); | |
34 | ||
35 | // The user is calculated from the uid in android.os.UserHandle#getUserId(int uid) as "uid / 100000", so we do the same: | |
36 | uid_t current_uid = getuid(); | |
37 | int android_user_id = current_uid / 100000; | |
38 | char* android_user_id_string; | |
39 | if (asprintf(&android_user_id_string, "%d", android_user_id) == -1) { | |
40 | fprintf(stderr, "asprintf() error"); | |
41 | return; | |
42 | } | |
43 | ||
44 | int const extra_args = 15; // Including ending NULL. | |
45 | char** child_argv = malloc((sizeof(char*)) * (argc + extra_args)); | |
46 | ||
47 | child_argv[0] = "am"; | |
48 | child_argv[1] = "broadcast"; | |
49 | child_argv[2] = "--user"; | |
50 | child_argv[3] = android_user_id_string; | |
51 | child_argv[4] = "-n"; | |
52 | child_argv[5] = "com.termux.api/.TermuxApiReceiver"; | |
53 | child_argv[6] = "--es"; | |
54 | // Input/output are reversed for the java process (our output is its input): | |
55 | child_argv[7] = "socket_input"; | |
56 | child_argv[8] = output_address_string; | |
57 | child_argv[9] = "--es"; | |
58 | child_argv[10] = "socket_output"; | |
59 | child_argv[11] = input_address_string; | |
60 | child_argv[12] = "--es"; | |
61 | child_argv[13] = "api_method"; | |
62 | child_argv[14] = argv[1]; | |
63 | ||
64 | // Copy the remaining arguments -2 for first binary and second api name: | |
65 | memcpy(child_argv + extra_args, argv + 2, (argc-1) * sizeof(char*)); | |
66 | ||
67 | // End with NULL: | |
68 | child_argv[argc + extra_args] = NULL; | |
69 | ||
70 | // Use an a executable taking care of PATH and LD_LIBRARY_PATH: | |
71 | char const* const am_executable = "/data/data/com.termux/files/usr/bin/am"; | |
72 | execv(am_executable, child_argv); | |
73 | ||
74 | perror("execv(\"/system/bin/am\")"); | |
75 | exit(1); | |
59f0d218 FF |
76 | } |
77 | ||
78 | void generate_uuid(char* str) { | |
bb3266ca FF |
79 | sprintf(str, "%x%x-%x-%x-%x-%x%x%x", |
80 | rand(), rand(), // Generates a 64-bit Hex number | |
81 | (uint32_t) getpid(), // Generates a 32-bit Hex number | |
82 | ((rand() & 0x0fff) | 0x4000), // Generates a 32-bit Hex number of the form 4xxx (4 indicates the UUID version) | |
83 | rand() % 0x3fff + 0x8000, // Generates a 32-bit Hex number in the range [0x8000, 0xbfff] | |
84 | rand(), rand(), rand()); // Generates a 96-bit Hex number | |
59f0d218 FF |
85 | } |
86 | ||
87 | // Thread function which reads from stdin and writes to socket. | |
88 | void* transmit_stdin_to_socket(void* arg) { | |
bb3266ca FF |
89 | int output_server_socket = *((int*) arg); |
90 | struct sockaddr_un remote_addr; | |
91 | socklen_t addrlen = sizeof(remote_addr); | |
92 | int output_client_socket = accept(output_server_socket, (struct sockaddr*) &remote_addr, &addrlen); | |
93 | ||
94 | int len; | |
95 | char buffer[1024]; | |
96 | while (len = read(STDIN_FILENO, &buffer, sizeof(buffer)-1), len > 0) { | |
97 | if (write(output_client_socket, buffer, len) < 0) break; | |
98 | } | |
99 | // Close output socket on end of input: | |
100 | close(output_client_socket); | |
101 | return NULL; | |
59f0d218 FF |
102 | } |
103 | ||
104 | // Main thread function which reads from input socket and writes to stdout. | |
105 | void transmit_socket_to_stdout(int input_socket_fd) { | |
bb3266ca FF |
106 | int len; |
107 | char buffer[1024]; | |
108 | while ((len = read(input_socket_fd, &buffer, sizeof(buffer)-1)) > 0) { | |
109 | buffer[len] = 0; | |
110 | write(STDOUT_FILENO, buffer, len); | |
111 | } | |
112 | if (len < 0) perror("read()"); | |
59f0d218 FF |
113 | } |
114 | ||
115 | int main(int argc, char** argv) { | |
bb3266ca FF |
116 | // Do not transform children into zombies when they terminate: |
117 | struct sigaction sigchld_action = { .sa_handler = SIG_DFL, .sa_flags = SA_RESTART | SA_NOCLDSTOP | SA_NOCLDWAIT }; | |
118 | sigaction(SIGCHLD, &sigchld_action, NULL); | |
119 | ||
120 | char input_address_string[100]; // This program reads from it. | |
121 | char output_address_string[100]; // This program writes to it. | |
122 | ||
123 | // Seed the random number generator: | |
124 | struct timeval time; | |
125 | gettimeofday(&time,NULL); | |
126 | srand((time.tv_sec * 1000) + (time.tv_usec / 1000)); | |
127 | ||
128 | generate_uuid(input_address_string); | |
129 | generate_uuid(output_address_string); | |
130 | ||
131 | struct sockaddr_un input_address = { .sun_family = AF_UNIX }; | |
132 | struct sockaddr_un output_address = { .sun_family = AF_UNIX }; | |
133 | // Leave struct sockaddr_un.sun_path[0] as 0 and use the UUID string as abstract linux namespace: | |
134 | strncpy(&input_address.sun_path[1], input_address_string, strlen(input_address_string)); | |
135 | strncpy(&output_address.sun_path[1], output_address_string, strlen(output_address_string)); | |
136 | ||
137 | int input_server_socket = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0); | |
138 | if (input_server_socket == -1) { perror("socket()"); return 1; } | |
139 | int output_server_socket = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0); | |
140 | if (output_server_socket == -1) { perror("socket()"); return 1; } | |
141 | ||
142 | if (bind(input_server_socket, (struct sockaddr*) &input_address, sizeof(sa_family_t) + strlen(input_address_string) + 1) == -1) { | |
143 | perror("bind(input)"); | |
144 | return 1; | |
145 | } | |
146 | if (bind(output_server_socket, (struct sockaddr*) &output_address, sizeof(sa_family_t) + strlen(output_address_string) + 1) == -1) { | |
147 | perror("bind(output)"); | |
148 | return 1; | |
149 | } | |
150 | ||
151 | if (listen(input_server_socket, 1) == -1) { perror("listen()"); return 1; } | |
152 | if (listen(output_server_socket, 1) == -1) { perror("listen()"); return 1; } | |
153 | ||
154 | pid_t fork_result = fork(); | |
155 | switch (fork_result) { | |
156 | case -1: perror("fork()"); return 1; | |
157 | case 0: exec_am_broadcast(argc, argv, input_address_string, output_address_string); return 0; | |
158 | } | |
159 | ||
160 | struct sockaddr_un remote_addr; | |
161 | socklen_t addrlen = sizeof(remote_addr); | |
162 | int input_client_socket = accept(input_server_socket, (struct sockaddr*) &remote_addr, &addrlen); | |
163 | ||
164 | pthread_t transmit_thread; | |
165 | pthread_create(&transmit_thread, NULL, transmit_stdin_to_socket, &output_server_socket); | |
166 | ||
167 | transmit_socket_to_stdout(input_client_socket); | |
168 | ||
169 | return 0; | |
59f0d218 FF |
170 | } |
171 |