aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/c/exec.c
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/c/exec.c')
-rw-r--r--app/src/main/c/exec.c60
1 files changed, 0 insertions, 60 deletions
diff --git a/app/src/main/c/exec.c b/app/src/main/c/exec.c
deleted file mode 100644
index c335b20..0000000
--- a/app/src/main/c/exec.c
+++ /dev/null
@@ -1,60 +0,0 @@
1/*
2 * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon
3 * Copyright (C) 2017-2018 Pacien TRAN-GIRARD
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19#include <jni.h>
20#include <unistd.h>
21#include <stdlib.h>
22#include <sys/wait.h>
23
24static inline const char **to_string_array(JNIEnv *env, jobjectArray ja) {
25 const int len = (*env)->GetArrayLength(env, ja);
26 const char **ca = calloc((size_t) len + 1, sizeof(char *));
27
28 for (int i = 0; i < len; ++i) {
29 jstring jstr = (jstring) (*env)->GetObjectArrayElement(env, ja, i);
30 ca[i] = (*env)->GetStringUTFChars(env, jstr, NULL);
31 }
32
33 ca[len] = NULL;
34 return ca;
35}
36
37static inline void exec(const char **argcv) {
38 execv(argcv[0], (char *const *) argcv);
39 exit(1);
40}
41
42JNIEXPORT jint JNICALL
43Java_org_pacien_tincapp_commands_Executor_forkExec(JNIEnv *env, __attribute__((unused)) jclass class, jobjectArray args) {
44 pid_t pid = fork();
45 switch (pid) {
46 case 0:
47 exec(to_string_array(env, args));
48 return 0;
49
50 default:
51 return pid;
52 }
53}
54
55JNIEXPORT jint JNICALL
56Java_org_pacien_tincapp_commands_Executor_wait(__attribute__((unused))JNIEnv *env, __attribute__((unused)) jclass class, jint pid) {
57 int status;
58 waitpid(pid, &status, 0);
59 return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
60}