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