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.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/app/src/main/c/exec.c b/app/src/main/c/exec.c
new file mode 100644
index 0000000..fdaec0f
--- /dev/null
+++ b/app/src/main/c/exec.c
@@ -0,0 +1,34 @@
1#include <jni.h>
2#include <unistd.h>
3#include <stdlib.h>
4
5static inline const char **to_string_array(JNIEnv *env, jobjectArray ja) {
6 const int len = (*env)->GetArrayLength(env, ja);
7 const char **ca = calloc((size_t) len + 1, sizeof(char *));
8
9 for (int i = 0; i < len; ++i) {
10 jstring jstr = (jstring) (*env)->GetObjectArrayElement(env, ja, i);
11 ca[i] = (*env)->GetStringUTFChars(env, jstr, NULL);
12 }
13
14 ca[len] = NULL;
15 return ca;
16}
17
18static inline void exec(const char **argcv) {
19 execv(argcv[0], (char *const *) argcv);
20 exit(1);
21}
22
23JNIEXPORT jint JNICALL
24Java_org_pacien_tincapp_commands_Executor_forkExec(JNIEnv *env, jclass class, jobjectArray argcv) {
25 pid_t pid = fork();
26 switch (pid) {
27 case 0:
28 exec(to_string_array(env, argcv));
29 return 0;
30
31 default:
32 return pid;
33 }
34}