mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 06:32:10 +02:00
Let spawner kill subprocesses, bug 119387
This commit is contained in:
parent
17fe1a203d
commit
4cc10e41b6
7 changed files with 479 additions and 457 deletions
Binary file not shown.
Binary file not shown.
|
@ -1,172 +1,175 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2002 - 2005 QNX Software Systems and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* QNX Software Systems - initial API and implementation
|
||||
*******************************************************************************/
|
||||
#include "exec0.h"
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <libgen.h>
|
||||
#include <stdlib.h>
|
||||
#include <termios.h>
|
||||
#include <stropts.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
/* from pfind.c */
|
||||
extern char *pfind(const char *name);
|
||||
|
||||
pid_t
|
||||
exec_pty(const char *path, char *const argv[], char *const envp[],
|
||||
const char *dirpath, int channels[3], const char *pts_name, int fdm)
|
||||
{
|
||||
int pipe2[2];
|
||||
pid_t childpid;
|
||||
char *full_path;
|
||||
|
||||
/*
|
||||
* We use pfind() to check that the program exists and is an executable.
|
||||
* If not pass the error up. Also execve() wants a full path.
|
||||
*/
|
||||
full_path = pfind(path);
|
||||
if (full_path == NULL) {
|
||||
fprintf(stderr, "Unable to find full path for \"%s\"\n", (path) ? path : "");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Make sure we can create our pipes before forking.
|
||||
*/
|
||||
if (channels != NULL) {
|
||||
if (pipe(pipe2) < 0) {
|
||||
fprintf(stderr, "%s(%d): returning due to error: %s\n", __FUNCTION__, __LINE__, strerror(errno));
|
||||
free(full_path);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
childpid = fork();
|
||||
|
||||
if (childpid < 0) {
|
||||
fprintf(stderr, "%s(%d): returning due to error: %s\n", __FUNCTION__, __LINE__, strerror(errno));
|
||||
free(full_path);
|
||||
return -1;
|
||||
} else if (childpid == 0) { /* child */
|
||||
|
||||
chdir(dirpath);
|
||||
|
||||
if (channels != NULL) {
|
||||
int fds;
|
||||
|
||||
fds = ptys_open(fdm, pts_name);
|
||||
if (fds < 0) {
|
||||
fprintf(stderr, "%s(%d): returning due to error: %s\n", __FUNCTION__, __LINE__, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Close the read end of pipe2 */
|
||||
if (close(pipe2[0]) == -1)
|
||||
perror("close(pipe2[0]))");
|
||||
|
||||
/* close the master, no need in the child */
|
||||
close(fdm);
|
||||
|
||||
set_noecho(fds);
|
||||
/* redirections */
|
||||
dup2(fds, STDIN_FILENO); /* dup stdin */
|
||||
dup2(fds, STDOUT_FILENO); /* dup stdout */
|
||||
dup2(pipe2[1], STDERR_FILENO); /* dup stderr */
|
||||
close(fds); /* done with fds. */
|
||||
}
|
||||
|
||||
/* Close all the fd's in the child */
|
||||
{
|
||||
int fdlimit = sysconf(_SC_OPEN_MAX);
|
||||
int fd = 3;
|
||||
|
||||
while (fd < fdlimit)
|
||||
close(fd++);
|
||||
}
|
||||
|
||||
if (envp[0] == NULL) {
|
||||
execv(full_path, argv);
|
||||
} else {
|
||||
execve(full_path, argv, envp);
|
||||
}
|
||||
|
||||
_exit(127);
|
||||
|
||||
} else if (childpid != 0) { /* parent */
|
||||
|
||||
ioctl(fdm, I_PUSH, "ptem");
|
||||
set_noecho(fdm);
|
||||
if (channels != NULL) {
|
||||
/* close the write end of pipe1 */
|
||||
if (close(pipe2[1]) == -1)
|
||||
perror("close(pipe2[1])");
|
||||
|
||||
channels[0] = fdm; /* Input Stream. */
|
||||
channels[1] = fdm; /* Output Stream. */
|
||||
channels[2] = pipe2[0]; /* stderr Stream. */
|
||||
//channels[2] = fdm; /* Input Stream. */
|
||||
}
|
||||
|
||||
free(full_path);
|
||||
return childpid;
|
||||
}
|
||||
|
||||
free(full_path);
|
||||
return -1; /*NOT REACHED */
|
||||
}
|
||||
#ifdef __STAND_ALONE__
|
||||
int main(int argc, char **argv, char **envp) {
|
||||
const char *path = "./bufferring_test";
|
||||
int channels[3] = { -1, -1, -1};
|
||||
int status;
|
||||
FILE *app_stdin;
|
||||
FILE *app_stdout;
|
||||
FILE *app_stderr;
|
||||
char pts_name[32];
|
||||
int fdm;
|
||||
char buffer[32];
|
||||
|
||||
fdm = ptym_open(pts_name);
|
||||
status = exec_pty(path, argv, envp, ".", channels, pts_name, fdm);
|
||||
if (status >= 0) {
|
||||
//app_stdin = fdopen(channels[0], "w");
|
||||
app_stdout = fdopen(channels[1], "r");
|
||||
app_stderr = fdopen(channels[2], "r");
|
||||
if (app_stdout == NULL || app_stderr == NULL /*|| app_stdin == NULL*/) {
|
||||
fprintf(stderr, "PROBLEMS\n");
|
||||
} else {
|
||||
printf("PID %d\n", status);
|
||||
if (isatty(fdm)) {
|
||||
printf("Is atty\n");
|
||||
}
|
||||
write(fdm, "foo\n", 4);
|
||||
write(fdm, "bar\n", 4);
|
||||
//fputs("foo\n", app_stdin);
|
||||
//fputs("bar\n", app_stdin);
|
||||
//fflush(app_stdin);
|
||||
while(fgets(buffer, sizeof buffer, app_stdout) != NULL) {
|
||||
fprintf(stdout, "STDOUT: %s\n", buffer);
|
||||
}
|
||||
while(fgets(buffer, sizeof buffer, app_stderr) != NULL) {
|
||||
fprintf(stdout, "STDERR: %s\n", buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
fputs("bye\n", stdout);
|
||||
close(channels[0]);
|
||||
close(channels[1]);
|
||||
close(channels[2]);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2002 - 2005 QNX Software Systems and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* QNX Software Systems - initial API and implementation
|
||||
* Wind River Systems, Inc.
|
||||
*******************************************************************************/
|
||||
#include "exec0.h"
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <libgen.h>
|
||||
#include <stdlib.h>
|
||||
#include <termios.h>
|
||||
#include <stropts.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
/* from pfind.c */
|
||||
extern char *pfind(const char *name);
|
||||
|
||||
pid_t
|
||||
exec_pty(const char *path, char *const argv[], char *const envp[],
|
||||
const char *dirpath, int channels[3], const char *pts_name, int fdm)
|
||||
{
|
||||
int pipe2[2];
|
||||
pid_t childpid;
|
||||
char *full_path;
|
||||
|
||||
/*
|
||||
* We use pfind() to check that the program exists and is an executable.
|
||||
* If not pass the error up. Also execve() wants a full path.
|
||||
*/
|
||||
full_path = pfind(path);
|
||||
if (full_path == NULL) {
|
||||
fprintf(stderr, "Unable to find full path for \"%s\"\n", (path) ? path : "");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Make sure we can create our pipes before forking.
|
||||
*/
|
||||
if (channels != NULL) {
|
||||
if (pipe(pipe2) < 0) {
|
||||
fprintf(stderr, "%s(%d): returning due to error: %s\n", __FUNCTION__, __LINE__, strerror(errno));
|
||||
free(full_path);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
childpid = fork();
|
||||
|
||||
if (childpid < 0) {
|
||||
fprintf(stderr, "%s(%d): returning due to error: %s\n", __FUNCTION__, __LINE__, strerror(errno));
|
||||
free(full_path);
|
||||
return -1;
|
||||
} else if (childpid == 0) { /* child */
|
||||
|
||||
chdir(dirpath);
|
||||
|
||||
if (channels != NULL) {
|
||||
int fds;
|
||||
|
||||
fds = ptys_open(fdm, pts_name);
|
||||
if (fds < 0) {
|
||||
fprintf(stderr, "%s(%d): returning due to error: %s\n", __FUNCTION__, __LINE__, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Close the read end of pipe2 */
|
||||
if (close(pipe2[0]) == -1)
|
||||
perror("close(pipe2[0]))");
|
||||
|
||||
/* close the master, no need in the child */
|
||||
close(fdm);
|
||||
|
||||
set_noecho(fds);
|
||||
/* redirections */
|
||||
dup2(fds, STDIN_FILENO); /* dup stdin */
|
||||
dup2(fds, STDOUT_FILENO); /* dup stdout */
|
||||
dup2(pipe2[1], STDERR_FILENO); /* dup stderr */
|
||||
close(fds); /* done with fds. */
|
||||
}
|
||||
|
||||
/* Close all the fd's in the child */
|
||||
{
|
||||
int fdlimit = sysconf(_SC_OPEN_MAX);
|
||||
int fd = 3;
|
||||
|
||||
while (fd < fdlimit)
|
||||
close(fd++);
|
||||
}
|
||||
|
||||
setpgid(getpid(), getpid());
|
||||
|
||||
if (envp[0] == NULL) {
|
||||
execv(full_path, argv);
|
||||
} else {
|
||||
execve(full_path, argv, envp);
|
||||
}
|
||||
|
||||
_exit(127);
|
||||
|
||||
} else if (childpid != 0) { /* parent */
|
||||
|
||||
ioctl(fdm, I_PUSH, "ptem");
|
||||
set_noecho(fdm);
|
||||
if (channels != NULL) {
|
||||
/* close the write end of pipe1 */
|
||||
if (close(pipe2[1]) == -1)
|
||||
perror("close(pipe2[1])");
|
||||
|
||||
channels[0] = fdm; /* Input Stream. */
|
||||
channels[1] = fdm; /* Output Stream. */
|
||||
channels[2] = pipe2[0]; /* stderr Stream. */
|
||||
//channels[2] = fdm; /* Input Stream. */
|
||||
}
|
||||
|
||||
free(full_path);
|
||||
return childpid;
|
||||
}
|
||||
|
||||
free(full_path);
|
||||
return -1; /*NOT REACHED */
|
||||
}
|
||||
#ifdef __STAND_ALONE__
|
||||
int main(int argc, char **argv, char **envp) {
|
||||
const char *path = "./bufferring_test";
|
||||
int channels[3] = { -1, -1, -1};
|
||||
int status;
|
||||
FILE *app_stdin;
|
||||
FILE *app_stdout;
|
||||
FILE *app_stderr;
|
||||
char pts_name[32];
|
||||
int fdm;
|
||||
char buffer[32];
|
||||
|
||||
fdm = ptym_open(pts_name);
|
||||
status = exec_pty(path, argv, envp, ".", channels, pts_name, fdm);
|
||||
if (status >= 0) {
|
||||
//app_stdin = fdopen(channels[0], "w");
|
||||
app_stdout = fdopen(channels[1], "r");
|
||||
app_stderr = fdopen(channels[2], "r");
|
||||
if (app_stdout == NULL || app_stderr == NULL /*|| app_stdin == NULL*/) {
|
||||
fprintf(stderr, "PROBLEMS\n");
|
||||
} else {
|
||||
printf("PID %d\n", status);
|
||||
if (isatty(fdm)) {
|
||||
printf("Is atty\n");
|
||||
}
|
||||
write(fdm, "foo\n", 4);
|
||||
write(fdm, "bar\n", 4);
|
||||
//fputs("foo\n", app_stdin);
|
||||
//fputs("bar\n", app_stdin);
|
||||
//fflush(app_stdin);
|
||||
while(fgets(buffer, sizeof buffer, app_stdout) != NULL) {
|
||||
fprintf(stdout, "STDOUT: %s\n", buffer);
|
||||
}
|
||||
while(fgets(buffer, sizeof buffer, app_stderr) != NULL) {
|
||||
fprintf(stdout, "STDERR: %s\n", buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
fputs("bye\n", stdout);
|
||||
close(channels[0]);
|
||||
close(channels[1]);
|
||||
close(channels[2]);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
*
|
||||
* Contributors:
|
||||
* QNX Software Systems - initial API and implementation
|
||||
* Wind River Systems, Inc.
|
||||
*******************************************************************************/
|
||||
#include "exec0.h"
|
||||
#include <unistd.h>
|
||||
|
@ -90,6 +91,8 @@ exec0(const char *path, char *const argv[], char *const envp[],
|
|||
close(fd++);
|
||||
}
|
||||
|
||||
setpgid(getpid(), getpid());
|
||||
|
||||
if (envp[0] == NULL) {
|
||||
execv(full_path, argv);
|
||||
} else {
|
||||
|
|
|
@ -1,285 +1,301 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2002 - 2005 QNX Software Systems and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* QNX Software Systems - initial API and implementation
|
||||
*******************************************************************************/
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <signal.h>
|
||||
#include <string.h>
|
||||
#include <jni.h>
|
||||
|
||||
#include "exec0.h"
|
||||
#include <Spawner.h>
|
||||
|
||||
|
||||
#define DEBUGIT 0
|
||||
|
||||
|
||||
/*
|
||||
* Header for class org_eclipse_cdt_utils_spawner_Spawner
|
||||
*/
|
||||
|
||||
|
||||
#if DEBUGIT
|
||||
static void print_array(char **c_array)
|
||||
{
|
||||
if (c_array) {
|
||||
char **p = c_array;
|
||||
for (; *p; p++) {
|
||||
if (*p) {
|
||||
fprintf(stderr, " %s", *p);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "null");
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
static char **alloc_c_array(JNIEnv * env, jobjectArray j_array)
|
||||
{
|
||||
int i;
|
||||
jint c_array_size = (*env)->GetArrayLength(env, j_array);
|
||||
char **c_array = calloc(c_array_size + 1, sizeof(*c_array));
|
||||
|
||||
if (c_array == NULL)
|
||||
return NULL;
|
||||
|
||||
for (i = 0; i < c_array_size; i++) {
|
||||
jstring j_str =
|
||||
(jstring) (*env)->GetObjectArrayElement(env, j_array, i);
|
||||
const char *c_str = (*env)->GetStringUTFChars(env, j_str, NULL);
|
||||
c_array[i] = (char *) strdup(c_str);
|
||||
(*env)->ReleaseStringUTFChars(env, j_str, c_str);
|
||||
(*env)->DeleteLocalRef(env, j_str);
|
||||
}
|
||||
|
||||
return c_array;
|
||||
}
|
||||
|
||||
|
||||
static void free_c_array(char **c_array)
|
||||
{
|
||||
if (c_array) {
|
||||
char **p = c_array;
|
||||
for (; *p; p++) {
|
||||
if (*p) {
|
||||
free(*p);
|
||||
}
|
||||
}
|
||||
free(c_array);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Class: org_eclipse_cdt_utils_spawner_Spawner
|
||||
* Method: exec2
|
||||
* Signature: ([Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;[ILorg/eclipse/cdt/utils/pty/PTY;)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec2
|
||||
(JNIEnv *env, jobject jobj, jobjectArray jcmd, jobjectArray jenv, jstring jdir, jintArray jchannels,
|
||||
jstring jslaveName, jint masterFD)
|
||||
{
|
||||
jint *channels = (*env)->GetIntArrayElements(env, jchannels, 0);
|
||||
const char *dirpath = (*env)->GetStringUTFChars(env, jdir, NULL);
|
||||
const char *pts_name = (*env)->GetStringUTFChars(env, jslaveName, NULL);
|
||||
char **cmd;
|
||||
char **envp;
|
||||
int fd[3];
|
||||
pid_t pid = -1;
|
||||
|
||||
if (channels == NULL)
|
||||
goto bail_out;
|
||||
|
||||
cmd = alloc_c_array(env, jcmd);
|
||||
if (cmd == NULL)
|
||||
goto bail_out;
|
||||
|
||||
envp = alloc_c_array(env, jenv);
|
||||
if (envp == NULL)
|
||||
goto bail_out;
|
||||
|
||||
#if DEBUGIT
|
||||
fprintf(stderr, "command:");
|
||||
print_array(cmd);
|
||||
fprintf(stderr, "Envp:");
|
||||
print_array(envp);
|
||||
fprintf(stderr, "dirpath: %s\n", dirpath);
|
||||
fprintf(stderr, "pts_name: %s\n", pts_name);
|
||||
#endif
|
||||
|
||||
pid = exec_pty(cmd[0], cmd, envp, dirpath, fd, pts_name, masterFD);
|
||||
if (pid < 0)
|
||||
goto bail_out;
|
||||
|
||||
channels[0] = fd[0];
|
||||
channels[1] = fd[1];
|
||||
channels[2] = fd[2];
|
||||
|
||||
bail_out:
|
||||
(*env)->ReleaseIntArrayElements(env, jchannels, channels, 0);
|
||||
(*env)->ReleaseStringUTFChars(env, jdir, dirpath);
|
||||
(*env)->ReleaseStringUTFChars(env, jslaveName, pts_name);
|
||||
if (cmd)
|
||||
free_c_array(cmd);
|
||||
if (envp)
|
||||
free_c_array(envp);
|
||||
return pid;
|
||||
}
|
||||
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_org_eclipse_cdt_utils_spawner_Spawner_exec1(JNIEnv * env, jobject jobj,
|
||||
jobjectArray jcmd,
|
||||
jobjectArray jenv,
|
||||
jstring jdir)
|
||||
{
|
||||
const char *dirpath = (*env)->GetStringUTFChars(env, jdir, NULL);
|
||||
char **cmd;
|
||||
char **envp;
|
||||
pid_t pid = -1;
|
||||
|
||||
cmd = alloc_c_array(env, jcmd);
|
||||
if (cmd == NULL)
|
||||
goto bail_out;
|
||||
|
||||
envp = alloc_c_array(env, jenv);
|
||||
if (envp == NULL)
|
||||
goto bail_out;
|
||||
|
||||
#if DEBUGIT
|
||||
fprintf(stderr, "command:");
|
||||
print_array(cmd);
|
||||
fprintf(stderr, "Envp:");
|
||||
print_array(envp);
|
||||
fprintf(stderr, "dirpath: %s\n", dirpath);
|
||||
#endif
|
||||
|
||||
pid = exec0(cmd[0], cmd, envp, dirpath, NULL);
|
||||
if (pid < 0)
|
||||
goto bail_out;
|
||||
|
||||
bail_out:
|
||||
(*env)->ReleaseStringUTFChars(env, jdir, dirpath);
|
||||
if (cmd)
|
||||
free_c_array(cmd);
|
||||
if (envp)
|
||||
free_c_array(envp);
|
||||
return pid;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: org_eclipse_cdt_utils_spawner_Spawner
|
||||
* Method: exec0
|
||||
* Signature: ([Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;[I)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_org_eclipse_cdt_utils_spawner_Spawner_exec0(JNIEnv * env, jobject jobj,
|
||||
jobjectArray jcmd,
|
||||
jobjectArray jenv,
|
||||
jstring jdir,
|
||||
jintArray jchannels)
|
||||
{
|
||||
jint *channels = (*env)->GetIntArrayElements(env, jchannels, 0);
|
||||
const char *dirpath = (*env)->GetStringUTFChars(env, jdir, NULL);
|
||||
char **cmd;
|
||||
char **envp;
|
||||
int fd[3];
|
||||
pid_t pid = -1;
|
||||
|
||||
if (channels == NULL)
|
||||
goto bail_out;
|
||||
|
||||
cmd = alloc_c_array(env, jcmd);
|
||||
if (cmd == NULL)
|
||||
goto bail_out;
|
||||
|
||||
envp = alloc_c_array(env, jenv);
|
||||
if (envp == NULL)
|
||||
goto bail_out;
|
||||
|
||||
#if DEBUGIT
|
||||
fprintf(stderr, "command:");
|
||||
print_array(cmd);
|
||||
fprintf(stderr, "Envp:");
|
||||
print_array(envp);
|
||||
fprintf(stderr, "dirpath: %s\n", dirpath);
|
||||
#endif
|
||||
|
||||
pid = exec0(cmd[0], cmd, envp, dirpath, fd);
|
||||
if (pid < 0)
|
||||
goto bail_out;
|
||||
|
||||
channels[0] = fd[0];
|
||||
channels[1] = fd[1];
|
||||
channels[2] = fd[2];
|
||||
|
||||
bail_out:
|
||||
(*env)->ReleaseIntArrayElements(env, jchannels, channels, 0);
|
||||
(*env)->ReleaseStringUTFChars(env, jdir, dirpath);
|
||||
if (cmd)
|
||||
free_c_array(cmd);
|
||||
if (envp)
|
||||
free_c_array(envp);
|
||||
return pid;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: org_eclipse_cdt_utils_spawner_Spawner
|
||||
* Method: raise
|
||||
* Signature: (II)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_org_eclipse_cdt_utils_spawner_Spawner_raise(JNIEnv * env, jobject jobj,
|
||||
jint pid, jint sig)
|
||||
{
|
||||
int status = -1;
|
||||
|
||||
switch (sig) {
|
||||
case 0: /* NOOP */
|
||||
status = kill(pid, 0);
|
||||
break;
|
||||
|
||||
case 2: /* INTERRUPT */
|
||||
status = kill(pid, SIGINT);
|
||||
break;
|
||||
|
||||
case 9: /* KILL */
|
||||
status = kill(pid, SIGKILL);
|
||||
break;
|
||||
|
||||
case 15: /* TERM */
|
||||
status = kill(pid, SIGTERM);
|
||||
break;
|
||||
|
||||
default:
|
||||
status = kill(pid, sig); /* WHAT ?? */
|
||||
break;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Class: org_eclipse_cdt_utils_spawner_Spawner
|
||||
* Method: waitFor
|
||||
* Signature: (I)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_org_eclipse_cdt_utils_spawner_Spawner_waitFor(JNIEnv * env,
|
||||
jobject jobj, jint pid)
|
||||
{
|
||||
return wait0(pid);
|
||||
}
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2002 - 2005 QNX Software Systems and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* QNX Software Systems - initial API and implementation
|
||||
* Wind River Systems, Inc.
|
||||
*******************************************************************************/
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <signal.h>
|
||||
#include <string.h>
|
||||
#include <jni.h>
|
||||
|
||||
#include "exec0.h"
|
||||
#include <Spawner.h>
|
||||
|
||||
|
||||
#define DEBUGIT 0
|
||||
|
||||
|
||||
/*
|
||||
* Header for class org_eclipse_cdt_utils_spawner_Spawner
|
||||
*/
|
||||
|
||||
|
||||
#if DEBUGIT
|
||||
static void print_array(char **c_array)
|
||||
{
|
||||
if (c_array) {
|
||||
char **p = c_array;
|
||||
for (; *p; p++) {
|
||||
if (*p) {
|
||||
fprintf(stderr, " %s", *p);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "null");
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
static char **alloc_c_array(JNIEnv * env, jobjectArray j_array)
|
||||
{
|
||||
int i;
|
||||
jint c_array_size = (*env)->GetArrayLength(env, j_array);
|
||||
char **c_array = calloc(c_array_size + 1, sizeof(*c_array));
|
||||
|
||||
if (c_array == NULL)
|
||||
return NULL;
|
||||
|
||||
for (i = 0; i < c_array_size; i++) {
|
||||
jstring j_str =
|
||||
(jstring) (*env)->GetObjectArrayElement(env, j_array, i);
|
||||
const char *c_str = (*env)->GetStringUTFChars(env, j_str, NULL);
|
||||
c_array[i] = (char *) strdup(c_str);
|
||||
(*env)->ReleaseStringUTFChars(env, j_str, c_str);
|
||||
(*env)->DeleteLocalRef(env, j_str);
|
||||
}
|
||||
|
||||
return c_array;
|
||||
}
|
||||
|
||||
|
||||
static void free_c_array(char **c_array)
|
||||
{
|
||||
if (c_array) {
|
||||
char **p = c_array;
|
||||
for (; *p; p++) {
|
||||
if (*p) {
|
||||
free(*p);
|
||||
}
|
||||
}
|
||||
free(c_array);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Class: org_eclipse_cdt_utils_spawner_Spawner
|
||||
* Method: exec2
|
||||
* Signature: ([Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;[ILorg/eclipse/cdt/utils/pty/PTY;)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec2
|
||||
(JNIEnv *env, jobject jobj, jobjectArray jcmd, jobjectArray jenv, jstring jdir, jintArray jchannels,
|
||||
jstring jslaveName, jint masterFD)
|
||||
{
|
||||
jint *channels = (*env)->GetIntArrayElements(env, jchannels, 0);
|
||||
const char *dirpath = (*env)->GetStringUTFChars(env, jdir, NULL);
|
||||
const char *pts_name = (*env)->GetStringUTFChars(env, jslaveName, NULL);
|
||||
char **cmd;
|
||||
char **envp;
|
||||
int fd[3];
|
||||
pid_t pid = -1;
|
||||
|
||||
if (channels == NULL)
|
||||
goto bail_out;
|
||||
|
||||
cmd = alloc_c_array(env, jcmd);
|
||||
if (cmd == NULL)
|
||||
goto bail_out;
|
||||
|
||||
envp = alloc_c_array(env, jenv);
|
||||
if (envp == NULL)
|
||||
goto bail_out;
|
||||
|
||||
#if DEBUGIT
|
||||
fprintf(stderr, "command:");
|
||||
print_array(cmd);
|
||||
fprintf(stderr, "Envp:");
|
||||
print_array(envp);
|
||||
fprintf(stderr, "dirpath: %s\n", dirpath);
|
||||
fprintf(stderr, "pts_name: %s\n", pts_name);
|
||||
#endif
|
||||
|
||||
pid = exec_pty(cmd[0], cmd, envp, dirpath, fd, pts_name, masterFD);
|
||||
if (pid < 0)
|
||||
goto bail_out;
|
||||
|
||||
channels[0] = fd[0];
|
||||
channels[1] = fd[1];
|
||||
channels[2] = fd[2];
|
||||
|
||||
bail_out:
|
||||
(*env)->ReleaseIntArrayElements(env, jchannels, channels, 0);
|
||||
(*env)->ReleaseStringUTFChars(env, jdir, dirpath);
|
||||
(*env)->ReleaseStringUTFChars(env, jslaveName, pts_name);
|
||||
if (cmd)
|
||||
free_c_array(cmd);
|
||||
if (envp)
|
||||
free_c_array(envp);
|
||||
return pid;
|
||||
}
|
||||
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_org_eclipse_cdt_utils_spawner_Spawner_exec1(JNIEnv * env, jobject jobj,
|
||||
jobjectArray jcmd,
|
||||
jobjectArray jenv,
|
||||
jstring jdir)
|
||||
{
|
||||
const char *dirpath = (*env)->GetStringUTFChars(env, jdir, NULL);
|
||||
char **cmd;
|
||||
char **envp;
|
||||
pid_t pid = -1;
|
||||
|
||||
cmd = alloc_c_array(env, jcmd);
|
||||
if (cmd == NULL)
|
||||
goto bail_out;
|
||||
|
||||
envp = alloc_c_array(env, jenv);
|
||||
if (envp == NULL)
|
||||
goto bail_out;
|
||||
|
||||
#if DEBUGIT
|
||||
fprintf(stderr, "command:");
|
||||
print_array(cmd);
|
||||
fprintf(stderr, "Envp:");
|
||||
print_array(envp);
|
||||
fprintf(stderr, "dirpath: %s\n", dirpath);
|
||||
#endif
|
||||
|
||||
pid = exec0(cmd[0], cmd, envp, dirpath, NULL);
|
||||
if (pid < 0)
|
||||
goto bail_out;
|
||||
|
||||
bail_out:
|
||||
(*env)->ReleaseStringUTFChars(env, jdir, dirpath);
|
||||
if (cmd)
|
||||
free_c_array(cmd);
|
||||
if (envp)
|
||||
free_c_array(envp);
|
||||
return pid;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: org_eclipse_cdt_utils_spawner_Spawner
|
||||
* Method: exec0
|
||||
* Signature: ([Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;[I)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_org_eclipse_cdt_utils_spawner_Spawner_exec0(JNIEnv * env, jobject jobj,
|
||||
jobjectArray jcmd,
|
||||
jobjectArray jenv,
|
||||
jstring jdir,
|
||||
jintArray jchannels)
|
||||
{
|
||||
jint *channels = (*env)->GetIntArrayElements(env, jchannels, 0);
|
||||
const char *dirpath = (*env)->GetStringUTFChars(env, jdir, NULL);
|
||||
char **cmd;
|
||||
char **envp;
|
||||
int fd[3];
|
||||
pid_t pid = -1;
|
||||
|
||||
if (channels == NULL)
|
||||
goto bail_out;
|
||||
|
||||
cmd = alloc_c_array(env, jcmd);
|
||||
if (cmd == NULL)
|
||||
goto bail_out;
|
||||
|
||||
envp = alloc_c_array(env, jenv);
|
||||
if (envp == NULL)
|
||||
goto bail_out;
|
||||
|
||||
#if DEBUGIT
|
||||
fprintf(stderr, "command:");
|
||||
print_array(cmd);
|
||||
fprintf(stderr, "Envp:");
|
||||
print_array(envp);
|
||||
fprintf(stderr, "dirpath: %s\n", dirpath);
|
||||
#endif
|
||||
|
||||
pid = exec0(cmd[0], cmd, envp, dirpath, fd);
|
||||
if (pid < 0)
|
||||
goto bail_out;
|
||||
|
||||
channels[0] = fd[0];
|
||||
channels[1] = fd[1];
|
||||
channels[2] = fd[2];
|
||||
|
||||
bail_out:
|
||||
(*env)->ReleaseIntArrayElements(env, jchannels, channels, 0);
|
||||
(*env)->ReleaseStringUTFChars(env, jdir, dirpath);
|
||||
if (cmd)
|
||||
free_c_array(cmd);
|
||||
if (envp)
|
||||
free_c_array(envp);
|
||||
return pid;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: org_eclipse_cdt_utils_spawner_Spawner
|
||||
* Method: raise
|
||||
* Signature: (II)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_org_eclipse_cdt_utils_spawner_Spawner_raise(JNIEnv * env, jobject jobj,
|
||||
jint pid, jint sig)
|
||||
{
|
||||
int status = -1;
|
||||
|
||||
switch (sig) {
|
||||
case 0: /* NOOP */
|
||||
status = killpg(pid, 0);
|
||||
if(status == -1) {
|
||||
status = kill(pid, 0);
|
||||
}
|
||||
break;
|
||||
|
||||
case 2: /* INTERRUPT */
|
||||
status = killpg(pid, SIGINT);
|
||||
if(status == -1) {
|
||||
status = kill(pid, SIGINT);
|
||||
}
|
||||
break;
|
||||
|
||||
case 9: /* KILL */
|
||||
status = killpg(pid, SIGKILL);
|
||||
if(status == -1) {
|
||||
status = kill(pid, SIGKILL);
|
||||
}
|
||||
break;
|
||||
|
||||
case 15: /* TERM */
|
||||
status = killpg(pid, SIGTERM);
|
||||
if(status == -1) {
|
||||
status = kill(pid, SIGTERM);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
status = killpg(pid, sig); /* WHAT ?? */
|
||||
if(status == -1) {
|
||||
status = kill(pid, sig); /* WHAT ?? */
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Class: org_eclipse_cdt_utils_spawner_Spawner
|
||||
* Method: waitFor
|
||||
* Signature: (I)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_org_eclipse_cdt_utils_spawner_Spawner_waitFor(JNIEnv * env,
|
||||
jobject jobj, jint pid)
|
||||
{
|
||||
return wait0(pid);
|
||||
}
|
||||
|
|
Binary file not shown.
Binary file not shown.
Loading…
Add table
Reference in a new issue