1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-06 09:16:02 +02:00

Bug 568079: Add missing bracers

Change-Id: Icd916d224d4900cf2227f868f8b94cdae13c3f6b
Signed-off-by: Torbjörn Svensson <azoff@svenskalinuxforeningen.se>
This commit is contained in:
Torbjörn Svensson 2020-10-31 16:26:19 +01:00
parent 82bc0b67e5
commit 3ae7b88630
19 changed files with 258 additions and 193 deletions

View file

@ -45,7 +45,7 @@ pid_t exec_pty(const char *path, char *const argv[], char *const envp[], const c
} }
/* /*
* Make sure we can create our pipes before forking. * Make sure we can create our pipes before forking.
*/ */
if (channels != NULL && console) { if (channels != NULL && console) {
if (pipe(pipe2) < 0) { if (pipe(pipe2) < 0) {
@ -112,8 +112,9 @@ pid_t exec_pty(const char *path, char *const argv[], char *const envp[], const c
int fdlimit = sysconf(_SC_OPEN_MAX); int fdlimit = sysconf(_SC_OPEN_MAX);
int fd = 3; int fd = 3;
while (fd < fdlimit) while (fd < fdlimit) {
close(fd++); close(fd++);
}
} }
if (envp[0] == NULL) { if (envp[0] == NULL) {
@ -133,8 +134,9 @@ pid_t exec_pty(const char *path, char *const argv[], char *const envp[], const c
channels[1] = fdm; /* Output Stream. */ channels[1] = fdm; /* Output Stream. */
if (console) { if (console) {
/* close the write end of pipe1 */ /* close the write end of pipe1 */
if (close(pipe2[1]) == -1) if (close(pipe2[1]) == -1) {
perror("close(pipe2[1])"); perror("close(pipe2[1])");
}
channels[2] = pipe2[0]; /* stderr Stream. */ channels[2] = pipe2[0]; /* stderr Stream. */
} else { } else {
channels[2] = fdm; /* Error Stream. */ channels[2] = fdm; /* Error Stream. */
@ -148,6 +150,7 @@ pid_t exec_pty(const char *path, char *const argv[], char *const envp[], const c
free(full_path); free(full_path);
return -1; /*NOT REACHED */ return -1; /*NOT REACHED */
} }
#ifdef __STAND_ALONE__ #ifdef __STAND_ALONE__
int main(int argc, char **argv, char **envp) { int main(int argc, char **argv, char **envp) {
const char *path = "./bufferring_test"; const char *path = "./bufferring_test";

View file

@ -41,7 +41,7 @@ pid_t exec0(const char *path, char *const argv[], char *const envp[], const char
} }
/* /*
* Make sure we can create our pipes before forking. * Make sure we can create our pipes before forking.
*/ */
if (channels != NULL) { if (channels != NULL) {
if (pipe(pipe0) < 0 || pipe(pipe1) < 0 || pipe(pipe2) < 0) { if (pipe(pipe0) < 0 || pipe(pipe1) < 0 || pipe(pipe2) < 0) {
@ -66,16 +66,19 @@ pid_t exec0(const char *path, char *const argv[], char *const envp[], const char
if (channels != NULL) { if (channels != NULL) {
/* Close the write end of pipe0 */ /* Close the write end of pipe0 */
if (close(pipe0[1]) == -1) if (close(pipe0[1]) == -1) {
perror("close(pipe0[1])"); perror("close(pipe0[1])");
}
/* Close the read end of pipe1 */ /* Close the read end of pipe1 */
if (close(pipe1[0]) == -1) if (close(pipe1[0]) == -1) {
perror("close(pipe1[0])"); perror("close(pipe1[0])");
}
/* Close the read end of pipe2 */ /* Close the read end of pipe2 */
if (close(pipe2[0]) == -1) if (close(pipe2[0]) == -1) {
perror("close(pipe2[0]))"); perror("close(pipe2[0]))");
}
/* redirections */ /* redirections */
dup2(pipe0[0], STDIN_FILENO); /* dup stdin */ dup2(pipe0[0], STDIN_FILENO); /* dup stdin */
@ -88,8 +91,9 @@ pid_t exec0(const char *path, char *const argv[], char *const envp[], const char
int fdlimit = sysconf(_SC_OPEN_MAX); int fdlimit = sysconf(_SC_OPEN_MAX);
int fd = 3; int fd = 3;
while (fd < fdlimit) while (fd < fdlimit) {
close(fd++); close(fd++);
}
} }
setpgid(getpid(), getpid()); setpgid(getpid(), getpid());
@ -108,16 +112,19 @@ pid_t exec0(const char *path, char *const argv[], char *const envp[], const char
if (channels != NULL) { if (channels != NULL) {
/* close the read end of pipe1 */ /* close the read end of pipe1 */
if (close(pipe0[0]) == -1) if (close(pipe0[0]) == -1) {
perror("close(pipe0[0])"); perror("close(pipe0[0])");
}
/* close the write end of pipe2 */ /* close the write end of pipe2 */
if (close(pipe1[1]) == -1) if (close(pipe1[1]) == -1) {
perror("close(pipe1[1])"); perror("close(pipe1[1])");
}
/* close the write end of pipe2 */ /* close the write end of pipe2 */
if (close(pipe2[1]) == -1) if (close(pipe2[1]) == -1) {
perror("close(pipe2[1])"); perror("close(pipe2[1])");
}
channels[0] = pipe0[1]; /* Output Stream. */ channels[0] = pipe0[1]; /* Output Stream. */
channels[1] = pipe1[0]; /* Input Stream. */ channels[1] = pipe1[0]; /* Input Stream. */
@ -136,8 +143,9 @@ int wait0(pid_t pid) {
int status; int status;
int val = -1; int val = -1;
if (pid < 0) if (pid < 0) {
return -1; return -1;
}
for (;;) { for (;;) {
if (waitpid(pid, &status, 0) < 0) { if (waitpid(pid, &status, 0) < 0) {

View file

@ -24,8 +24,9 @@
static void ThrowByName(JNIEnv *env, const char *name, const char *msg) { static void ThrowByName(JNIEnv *env, const char *name, const char *msg) {
jclass cls = (*env)->FindClass(env, name); jclass cls = (*env)->FindClass(env, name);
if (cls != 0) /* Otherwise an exception has already been thrown */ if (cls != 0) { /* Otherwise an exception has already been thrown */
(*env)->ThrowNew(env, cls, msg); (*env)->ThrowNew(env, cls, msg);
}
/* It's a good practice to clean up the local references. */ /* It's a good practice to clean up the local references. */
(*env)->DeleteLocalRef(env, cls); (*env)->DeleteLocalRef(env, cls);

View file

@ -43,24 +43,28 @@ int openpty(int *amaster, int *aslave, char *name, struct termios *termp, struct
char line[20]; char line[20];
line[0] = 0; line[0] = 0;
*amaster = ptym_open(line); *amaster = ptym_open(line);
if (*amaster < 0) if (*amaster < 0) {
return -1; return -1;
}
*aslave = ptys_open(*amaster, line); *aslave = ptys_open(*amaster, line);
if (*aslave < 0) { if (*aslave < 0) {
close(*amaster); close(*amaster);
return -1; return -1;
} }
if (name) if (name) {
strcpy(name, line); strcpy(name, line);
}
#ifndef TCSAFLUSH #ifndef TCSAFLUSH
#define TCSAFLUSH TCSETAF #define TCSAFLUSH TCSETAF
#endif #endif
if (termp) if (termp) {
(void) tcsetattr(*aslave, TCSAFLUSH, termp); (void) tcsetattr(*aslave, TCSAFLUSH, termp);
}
#ifdef TIOCSWINSZ #ifdef TIOCSWINSZ
if (winp) if (winp) {
(void) ioctl(*aslave, TIOCSWINSZ, (char *)winp); (void) ioctl(*aslave, TIOCSWINSZ, (char *)winp);
}
#endif #endif
return 0; return 0;
} }
@ -91,8 +95,9 @@ int ptym_open(char *pts_name) {
#else #else
fdm = getpt(); fdm = getpt();
#endif #endif
if (fdm < 0) if (fdm < 0) {
return -1; return -1;
}
if (grantpt(fdm) < 0) { /* grant access to slave */ if (grantpt(fdm) < 0) { /* grant access to slave */
close(fdm); close(fdm);
return -2; return -2;

View file

@ -34,8 +34,9 @@ const int path_def_len = 5; /* strlen(PATH_DEF); */
char* path_val(char *const envp[]) { char* path_val(char *const envp[]) {
int i; int i;
if (envp == NULL || envp[0] == NULL) if (envp == NULL || envp[0] == NULL) {
return getenv("PATH"); return getenv("PATH");
}
for (i = 0; envp[i] != NULL; i++) { for (i = 0; envp[i] != NULL; i++) {
char *p = envp[i]; char *p = envp[i];
@ -60,7 +61,7 @@ char* pfind(const char *name, char *const envp[]) {
return NULL; return NULL;
} }
/* For absolute name or name with a path, check if it is an executable. */ /* For absolute name or name with a path, check if it is an executable. */
if (name[0] == '/' || name[0] == '.') { if (name[0] == '/' || name[0] == '.') {
if (access(name, X_OK) == 0) { if (access(name, X_OK) == 0) {
return strdup(name); return strdup(name);
@ -68,7 +69,7 @@ char* pfind(const char *name, char *const envp[]) {
return NULL; return NULL;
} }
/* Search in the PATH environment. */ /* Search in the PATH environment. */
path = path_val(envp); path = path_val(envp);
if (path == NULL || strlen(path) <= 0) { if (path == NULL || strlen(path) <= 0) {
@ -76,7 +77,7 @@ char* pfind(const char *name, char *const envp[]) {
return NULL; return NULL;
} }
/* The value return by getenv() is readonly */ /* The value return by getenv() is read-only */
path = strdup(path); path = strdup(path);
tok = strtok_r(path, ":", &sp); tok = strtok_r(path, ":", &sp);
@ -103,12 +104,13 @@ int main(int argc, char **argv)
int i; int i;
char *fullpath; char *fullpath;
for (i=1; i<argc; i++) { for (i = 1; i < argc; i++) {
fullpath = pfind(argv[i], NULL); fullpath = pfind(argv[i], NULL);
if (fullpath == NULL) if (fullpath == NULL) {
printf("Unable to find %s in $PATH.\n", argv[i]); printf("Unable to find %s in $PATH.\n", argv[i]);
else } else {
printf("Found %s @ %s.\n", argv[i], fullpath); printf("Found %s @ %s.\n", argv[i], fullpath);
}
} }
} }
#endif #endif

View file

@ -44,8 +44,7 @@ Java_org_eclipse_cdt_utils_pty_PTYInputStream_read0(JNIEnv *env, jobject jobj, j
status = -1; status = -1;
} else if (status == -1) { } else if (status == -1) {
/* Error, toss an exception */ /* Error, toss an exception */
/* Ignore the error for now, the debugger will attempt /* Ignore the error for now, the debugger will attempt to close this multiple time. */
* to close this multiple time. */
#if 0 #if 0
jclass exception = (*env)->FindClass(env, "java/io/IOException"); jclass exception = (*env)->FindClass(env, "java/io/IOException");
if (exception == NULL) { if (exception == NULL) {

View file

@ -30,11 +30,9 @@
*/ */
#if DEBUGIT #if DEBUGIT
static void print_array(char **c_array) static void print_array(char **c_array) {
{
if (c_array) { if (c_array) {
char **p = c_array; for (char **p = c_array; *p; p++) {
for (; *p; p++) {
if (*p) { if (*p) {
fprintf(stderr, " %s", *p); fprintf(stderr, " %s", *p);
} }
@ -51,8 +49,9 @@ static char** alloc_c_array(JNIEnv *env, jobjectArray j_array) {
jint c_array_size = (*env)->GetArrayLength(env, j_array); jint c_array_size = (*env)->GetArrayLength(env, j_array);
char **c_array = calloc(c_array_size + 1, sizeof(*c_array)); char **c_array = calloc(c_array_size + 1, sizeof(*c_array));
if (c_array == NULL) if (c_array == NULL) {
return NULL; return NULL;
}
for (i = 0; i < c_array_size; i++) { for (i = 0; i < c_array_size; i++) {
jstring j_str = (jstring)(*env)->GetObjectArrayElement(env, j_array, i); jstring j_str = (jstring)(*env)->GetObjectArrayElement(env, j_array, i);
@ -67,8 +66,7 @@ static char** alloc_c_array(JNIEnv *env, jobjectArray j_array) {
static void free_c_array(char **c_array) { static void free_c_array(char **c_array) {
if (c_array) { if (c_array) {
char **p = c_array; for (char **p = c_array; *p; p++) {
for (; *p; p++) {
if (*p) { if (*p) {
free(*p); free(*p);
} }
@ -86,16 +84,19 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec2(JNIEnv *
int fd[3]; int fd[3];
pid_t pid = -1; pid_t pid = -1;
if (jchannels == NULL) if (jchannels == NULL) {
goto bail_out; goto bail_out;
}
cmd = alloc_c_array(env, jcmd); cmd = alloc_c_array(env, jcmd);
if (cmd == NULL) if (cmd == NULL) {
goto bail_out; goto bail_out;
}
envp = alloc_c_array(env, jenv); envp = alloc_c_array(env, jenv);
if (envp == NULL) if (envp == NULL) {
goto bail_out; goto bail_out;
}
#if DEBUGIT #if DEBUGIT
fprintf(stderr, "command:"); fprintf(stderr, "command:");
@ -107,8 +108,9 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec2(JNIEnv *
#endif #endif
pid = exec_pty(cmd[0], cmd, envp, dirpath, fd, pts_name, masterFD, console); pid = exec_pty(cmd[0], cmd, envp, dirpath, fd, pts_name, masterFD, console);
if (pid < 0) if (pid < 0) {
goto bail_out; goto bail_out;
}
jobject cls = (*env)->FindClass(env, "org/eclipse/cdt/utils/spawner/Spawner$UnixChannel"); jobject cls = (*env)->FindClass(env, "org/eclipse/cdt/utils/spawner/Spawner$UnixChannel");
jmethodID constructor = (*env)->GetMethodID(env, cls, "<init>", "(I)V"); jmethodID constructor = (*env)->GetMethodID(env, cls, "<init>", "(I)V");
@ -119,10 +121,12 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec2(JNIEnv *
bail_out: (*env)->ReleaseStringUTFChars(env, jdir, dirpath); bail_out: (*env)->ReleaseStringUTFChars(env, jdir, dirpath);
(*env)->ReleaseStringUTFChars(env, jslaveName, pts_name); (*env)->ReleaseStringUTFChars(env, jslaveName, pts_name);
if (cmd) if (cmd) {
free_c_array(cmd); free_c_array(cmd);
if (envp) }
if (envp) {
free_c_array(envp); free_c_array(envp);
}
return pid; return pid;
} }
@ -135,12 +139,14 @@ Java_org_eclipse_cdt_utils_spawner_Spawner_exec1(JNIEnv *env, jobject jobj, jobj
pid_t pid = -1; pid_t pid = -1;
cmd = alloc_c_array(env, jcmd); cmd = alloc_c_array(env, jcmd);
if (cmd == NULL) if (cmd == NULL) {
goto bail_out; goto bail_out;
}
envp = alloc_c_array(env, jenv); envp = alloc_c_array(env, jenv);
if (envp == NULL) if (envp == NULL) {
goto bail_out; goto bail_out;
}
#if DEBUGIT #if DEBUGIT
fprintf(stderr, "command:"); fprintf(stderr, "command:");
@ -151,14 +157,17 @@ Java_org_eclipse_cdt_utils_spawner_Spawner_exec1(JNIEnv *env, jobject jobj, jobj
#endif #endif
pid = exec0(cmd[0], cmd, envp, dirpath, NULL); pid = exec0(cmd[0], cmd, envp, dirpath, NULL);
if (pid < 0) if (pid < 0) {
goto bail_out; goto bail_out;
}
bail_out: (*env)->ReleaseStringUTFChars(env, jdir, dirpath); bail_out: (*env)->ReleaseStringUTFChars(env, jdir, dirpath);
if (cmd) if (cmd) {
free_c_array(cmd); free_c_array(cmd);
if (envp) }
if (envp) {
free_c_array(envp); free_c_array(envp);
}
return pid; return pid;
} }
@ -173,24 +182,29 @@ Java_org_eclipse_cdt_utils_spawner_Spawner_exec0(JNIEnv *env, jobject jobj, jobj
jclass channelClass = NULL; jclass channelClass = NULL;
jmethodID channelConstructor = NULL; jmethodID channelConstructor = NULL;
if (jchannels == NULL) if (jchannels == NULL) {
goto bail_out; goto bail_out;
}
channelClass = (*env)->FindClass(env, "org/eclipse/cdt/utils/spawner/Spawner$UnixChannel"); channelClass = (*env)->FindClass(env, "org/eclipse/cdt/utils/spawner/Spawner$UnixChannel");
if (channelClass == 0) if (channelClass == 0) {
goto bail_out; goto bail_out;
}
channelConstructor = (*env)->GetMethodID(env, channelClass, "<init>", "(I)V"); channelConstructor = (*env)->GetMethodID(env, channelClass, "<init>", "(I)V");
if (channelConstructor == 0) if (channelConstructor == 0) {
goto bail_out; goto bail_out;
}
cmd = alloc_c_array(env, jcmd); cmd = alloc_c_array(env, jcmd);
if (cmd == NULL) if (cmd == NULL) {
goto bail_out; goto bail_out;
}
envp = alloc_c_array(env, jenv); envp = alloc_c_array(env, jenv);
if (envp == NULL) if (envp == NULL) {
goto bail_out; goto bail_out;
}
#if DEBUGIT #if DEBUGIT
fprintf(stderr, "command:"); fprintf(stderr, "command:");
@ -200,8 +214,9 @@ Java_org_eclipse_cdt_utils_spawner_Spawner_exec0(JNIEnv *env, jobject jobj, jobj
fprintf(stderr, "dirpath: %s\n", dirpath); fprintf(stderr, "dirpath: %s\n", dirpath);
#endif #endif
pid = exec0(cmd[0], cmd, envp, dirpath, fd); pid = exec0(cmd[0], cmd, envp, dirpath, fd);
if (pid < 0) if (pid < 0) {
goto bail_out; goto bail_out;
}
for (jsize i = 0; i < 3; i++) { for (jsize i = 0; i < 3; i++) {
jobject chan = (*env)->NewObject(env, channelClass, channelConstructor, fd[i]); jobject chan = (*env)->NewObject(env, channelClass, channelConstructor, fd[i]);
@ -209,10 +224,12 @@ Java_org_eclipse_cdt_utils_spawner_Spawner_exec0(JNIEnv *env, jobject jobj, jobj
} }
bail_out: (*env)->ReleaseStringUTFChars(env, jdir, dirpath); bail_out: (*env)->ReleaseStringUTFChars(env, jdir, dirpath);
if (cmd) if (cmd) {
free_c_array(cmd); free_c_array(cmd);
if (envp) }
if (envp) {
free_c_array(envp); free_c_array(envp);
}
return pid; return pid;
} }

View file

@ -308,8 +308,7 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec0(JNIEnv *
jsize len = (*env)->GetStringLength(env, item); jsize len = (*env)->GetStringLength(env, item);
const wchar_t *str = (const wchar_t*) (*env)->GetStringChars(env, item, 0); const wchar_t *str = (const wchar_t*) (*env)->GetStringChars(env, item, 0);
if (NULL != str) { if (NULL != str) {
while ((nBlkSize - nPos) <= (len + 2)) // +2 for two '\0' while ((nBlkSize - nPos) <= (len + 2)) { // +2 for two '\0'
{
nBlkSize += MAX_ENV_SIZE; nBlkSize += MAX_ENV_SIZE;
szEnvBlock = (wchar_t*) realloc(szEnvBlock, nBlkSize * sizeof(wchar_t)); szEnvBlock = (wchar_t*) realloc(szEnvBlock, nBlkSize * sizeof(wchar_t));
if (NULL == szEnvBlock) { if (NULL == szEnvBlock) {
@ -373,17 +372,19 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec0(JNIEnv *
&si, /* (in) startup information */ &si, /* (in) startup information */
&pi); /* (out) process information */ &pi); /* (out) process information */
if (NULL != cwd) if (NULL != cwd) {
free((void*) cwd); free((void*) cwd);
}
if (NULL != szEnvBlock) if (NULL != szEnvBlock) {
free(szEnvBlock); free(szEnvBlock);
}
if (NULL != szCmdLine) if (NULL != szCmdLine) {
free(szCmdLine); free(szCmdLine);
}
if (!ret) // Launching error if (!ret) { // Launching error
{
char *lpMsgBuf; char *lpMsgBuf;
CloseHandle(stdHandles[0]); CloseHandle(stdHandles[0]);
CloseHandle(stdHandles[1]); CloseHandle(stdHandles[1]);
@ -407,8 +408,7 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec0(JNIEnv *
h[1] = pi.hProcess; h[1] = pi.hProcess;
what = WaitForMultipleObjects(2, h, FALSE, INFINITE); what = WaitForMultipleObjects(2, h, FALSE, INFINITE);
if (what != WAIT_OBJECT_0) // CreateProcess failed if (what != WAIT_OBJECT_0) { // CreateProcess failed
{
#ifdef DEBUG_MONITOR #ifdef DEBUG_MONITOR
swprintf(buffer, sizeof(buffer)/sizeof(buffer[0]), _T("Process %i failed\n"), pi.dwProcessId); swprintf(buffer, sizeof(buffer)/sizeof(buffer[0]), _T("Process %i failed\n"), pi.dwProcessId);
OutputDebugStringW(buffer); OutputDebugStringW(buffer);
@ -529,8 +529,7 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec1(JNIEnv *
jsize len = (*env)->GetStringLength(env, item); jsize len = (*env)->GetStringLength(env, item);
const wchar_t *str = (const wchar_t*) (*env)->GetStringChars(env, item, 0); const wchar_t *str = (const wchar_t*) (*env)->GetStringChars(env, item, 0);
if (NULL != str) { if (NULL != str) {
while ((nBlkSize - nPos) <= (len + 2)) // +2 for two '\0' while ((nBlkSize - nPos) <= (len + 2)) { // +2 for two '\0'
{
nBlkSize += MAX_ENV_SIZE; nBlkSize += MAX_ENV_SIZE;
szEnvBlock = (wchar_t*) realloc(szEnvBlock, nBlkSize * sizeof(wchar_t)); szEnvBlock = (wchar_t*) realloc(szEnvBlock, nBlkSize * sizeof(wchar_t));
if (NULL == szEnvBlock) { if (NULL == szEnvBlock) {
@ -573,15 +572,17 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec1(JNIEnv *
&si, /* (in) startup information */ &si, /* (in) startup information */
&pi); /* (out) process information */ &pi); /* (out) process information */
if (NULL != cwd) if (NULL != cwd) {
free(cwd); free(cwd);
if (NULL != szEnvBlock) }
if (NULL != szEnvBlock) {
free(szEnvBlock); free(szEnvBlock);
if (NULL != szCmdLine) }
if (NULL != szCmdLine) {
free(szCmdLine); free(szCmdLine);
}
if (!ret) // error if (!ret) { // error
{
char *lpMsgBuf; char *lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
@ -635,8 +636,9 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_raise(JNIEnv *
hProc = OpenProcess(SYNCHRONIZE, 0, pCurProcInfo->pid); hProc = OpenProcess(SYNCHRONIZE, 0, pCurProcInfo->pid);
if (NULL == hProc) if (NULL == hProc) {
return -1; return -1;
}
switch (signal) { switch (signal) {
case SIG_NOOP: case SIG_NOOP:
@ -649,26 +651,26 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_raise(JNIEnv *
break; break;
case SIG_TERM: case SIG_TERM:
#ifdef DEBUG_MONITOR #ifdef DEBUG_MONITOR
swprintf(buffer, sizeof(buffer)/sizeof(buffer[0]), _T("Spawner received TERM signal for process %i\n"), swprintf(buffer, sizeof(buffer)/sizeof(buffer[0]), _T("Spawner received TERM signal for process %i\n"),
pCurProcInfo -> pid); pCurProcInfo -> pid);
OutputDebugStringW(buffer); OutputDebugStringW(buffer);
#endif #endif
SetEvent(pCurProcInfo->eventTerminate); SetEvent(pCurProcInfo->eventTerminate);
#ifdef DEBUG_MONITOR #ifdef DEBUG_MONITOR
OutputDebugStringW(_T("Spawner signaled TERM event\n")); OutputDebugStringW(_T("Spawner signaled TERM event\n"));
#endif #endif
ret = 0; ret = 0;
break; break;
case SIG_KILL: case SIG_KILL:
#ifdef DEBUG_MONITOR #ifdef DEBUG_MONITOR
swprintf(buffer, sizeof(buffer)/sizeof(buffer[0]), _T("Spawner received KILL signal for process %i\n"), swprintf(buffer, sizeof(buffer)/sizeof(buffer[0]), _T("Spawner received KILL signal for process %i\n"),
pCurProcInfo -> pid); pCurProcInfo -> pid);
OutputDebugStringW(buffer); OutputDebugStringW(buffer);
#endif #endif
SetEvent(pCurProcInfo->eventKill); SetEvent(pCurProcInfo->eventKill);
#ifdef DEBUG_MONITOR #ifdef DEBUG_MONITOR
OutputDebugStringW(_T("Spawner signaled KILL event\n")); OutputDebugStringW(_T("Spawner signaled KILL event\n"));
#endif #endif
ret = 0; ret = 0;
break; break;
@ -705,13 +707,15 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_waitFor(JNIEnv
HANDLE hProc; HANDLE hProc;
pProcInfo_t pCurProcInfo = findProcInfo(uid); pProcInfo_t pCurProcInfo = findProcInfo(uid);
if (NULL == pCurProcInfo) if (NULL == pCurProcInfo) {
return -1; return -1;
}
hProc = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION, 0, pCurProcInfo->pid); hProc = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION, 0, pCurProcInfo->pid);
if (NULL == hProc) if (NULL == hProc) {
return -1; return -1;
}
what = WaitForSingleObject(hProc, INFINITE); what = WaitForSingleObject(hProc, INFINITE);
@ -719,8 +723,9 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_waitFor(JNIEnv
GetExitCodeProcess(hProc, &exit_code); GetExitCodeProcess(hProc, &exit_code);
} }
if (hProc) if (hProc) {
CloseHandle(hProc); CloseHandle(hProc);
}
return exit_code; return exit_code;
} }
@ -736,8 +741,9 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_waitFor(JNIEnv
void ThrowByName(JNIEnv *env, const char *name, const char *msg) { void ThrowByName(JNIEnv *env, const char *name, const char *msg) {
jclass cls = (*env)->FindClass(env, name); jclass cls = (*env)->FindClass(env, name);
if (cls != 0) /* Otherwise an exception has already been thrown */ if (cls != 0) { /* Otherwise an exception has already been thrown */
(*env)->ThrowNew(env, cls, msg); (*env)->ThrowNew(env, cls, msg);
}
/* It's a good practice to clean up the local references. */ /* It's a good practice to clean up the local references. */
(*env)->DeleteLocalRef(env, cls); (*env)->DeleteLocalRef(env, cls);
@ -781,8 +787,9 @@ pProcInfo_t createProcInfo() {
pProcInfo_t findProcInfo(int uid) { pProcInfo_t findProcInfo(int uid) {
int i; int i;
pProcInfo_t p = NULL; pProcInfo_t p = NULL;
if (NULL == pInfo) if (NULL == pInfo) {
return NULL; return NULL;
}
for (i = 0; i < MAX_PROCS; ++i) { for (i = 0; i < MAX_PROCS; ++i) {
if (pInfo[i].uid == uid) { if (pInfo[i].uid == uid) {
@ -832,8 +839,7 @@ void cleanUpProcBlock(pProcInfo_t pCurProcInfo) {
// pv - pointer to PROCESS_INFORMATION struct // pv - pointer to PROCESS_INFORMATION struct
// Return : no // Return : no
///////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////
void _cdecl waitProcTermination(void* pv) void _cdecl waitProcTermination(void* pv) {
{
PROCESS_INFORMATION *pi = (PROCESS_INFORMATION *)pv; PROCESS_INFORMATION *pi = (PROCESS_INFORMATION *)pv;
int i; int i;
#ifdef DEBUG_MONITOR #ifdef DEBUG_MONITOR
@ -843,10 +849,8 @@ void _cdecl waitProcTermination(void* pv)
// wait for process termination // wait for process termination
WaitForSingleObject(pi->hProcess, INFINITE); WaitForSingleObject(pi->hProcess, INFINITE);
for(i = 0; i < MAX_PROCS; ++i) for (i = 0; i < MAX_PROCS; ++i) {
{ if (pInfo[i].pid == pi->dwProcessId) {
if(pInfo[i].pid == pi->dwProcessId)
{
cleanUpProcBlock(pInfo + i); cleanUpProcBlock(pInfo + i);
#ifdef DEBUG_MONITOR #ifdef DEBUG_MONITOR
swprintf(buffer, sizeof(buffer)/sizeof(buffer[0]), _T("waitProcTermination: set PID %i to 0\n"), swprintf(buffer, sizeof(buffer)/sizeof(buffer[0]), _T("waitProcTermination: set PID %i to 0\n"),
@ -880,8 +884,9 @@ int copyTo(wchar_t *target, const wchar_t *source, int cpyLength, int availSpace
int nQuotationMode = 0; int nQuotationMode = 0;
if (availSpace <= cpyLength) // = to reserve space for final '\0' if (availSpace <= cpyLength) { // = to reserve space for final '\0'
return -1; return -1;
}
if ((_T('\"') == *source) && (_T('\"') == *(source + cpyLength - 1))) { if ((_T('\"') == *source) && (_T('\"') == *(source + cpyLength - 1))) {
nQuotationMode = QUOTATION_DONE; nQuotationMode = QUOTATION_DONE;
@ -896,16 +901,16 @@ int copyTo(wchar_t *target, const wchar_t *source, int cpyLength, int availSpace
} }
for (; i < cpyLength; ++i, ++j) { for (; i < cpyLength; ++i, ++j) {
if (source[i] == _T('\\')) if (source[i] == _T('\\')) {
bSlash = TRUE; bSlash = TRUE;
else { } else {
// Don't escape embracing quotation marks // Don't escape embracing quotation marks
if ((source[i] == _T('\"')) if ((source[i] == _T('\"'))
&& !((nQuotationMode == QUOTATION_DONE) && ((i == 0) || (i == (cpyLength - 1))))) { && !((nQuotationMode == QUOTATION_DONE) && ((i == 0) || (i == (cpyLength - 1))))) {
if (!bSlash) // If still not escaped if (!bSlash) { // If still not escaped
{ if (j == availSpace) {
if (j == availSpace)
return -1; return -1;
}
target[j] = _T('\\'); target[j] = _T('\\');
++j; ++j;
} }
@ -913,14 +918,16 @@ int copyTo(wchar_t *target, const wchar_t *source, int cpyLength, int availSpace
bSlash = FALSE; bSlash = FALSE;
} }
if (j == availSpace) if (j == availSpace) {
return -1; return -1;
}
target[j] = source[i]; target[j] = source[i];
} }
if (nQuotationMode == QUOTATION_DO) { if (nQuotationMode == QUOTATION_DO) {
if (j == availSpace) if (j == availSpace) {
return -1; return -1;
}
target[j] = _T('\"'); target[j] = _T('\"');
++j; ++j;
} }

View file

@ -98,22 +98,22 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_SpawnerInputStream_rea
if (err == ERROR_IO_PENDING) { if (err == ERROR_IO_PENDING) {
// asynchronous i/o is still in progress // asynchronous i/o is still in progress
// check on the results of the asynchronous read // check on the results of the asynchronous read
if (GetOverlappedResult(handle, &overlapped, &nNumberOfBytesRead, TRUE)) if (GetOverlappedResult(handle, &overlapped, &nNumberOfBytesRead, TRUE)) {
err = 0; err = 0;
// if there was a problem ... } else { // if there was a problem ...
else
err = GetLastError(); err = GetLastError();
}
} }
if (err == ERROR_BROKEN_PIPE) // Pipe was closed if (err == ERROR_BROKEN_PIPE) { // Pipe was closed
break; break;
}
if (err != 0) { if (err != 0) {
char *lpMsgBuf; char *lpMsgBuf;
#ifdef DEBUG_MONITOR #ifdef DEBUG_MONITOR
_stprintf(buffer, _T("Read failed - %i, error %i\n"), fd, err); _stprintf(buffer, _T("Read failed - %i, error %i\n"), fd, err);
OutputDebugStringW(buffer); OutputDebugStringW(buffer);
#endif #endif
if (err != ERROR_MORE_DATA) // Otherwise error means just that there are more data if (err != ERROR_MORE_DATA) { // Otherwise error means just that there are more data than buffer can accept
{ // than buffer can accept
FormatMessage( FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
@ -135,19 +135,21 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_SpawnerInputStream_rea
} }
} }
} }
if (nNumberOfBytesRead > 0) if (nNumberOfBytesRead > 0) {
(*env)->SetByteArrayRegion(env, buf, nBuffOffset, nNumberOfBytesRead, tmpBuf); (*env)->SetByteArrayRegion(env, buf, nBuffOffset, nNumberOfBytesRead, tmpBuf);
else } else {
break; break;
}
nBuffOffset += nNumberOfBytesRead; nBuffOffset += nNumberOfBytesRead;
if (nNumberOfBytesRead != nNumberOfBytesToRead) if (nNumberOfBytesRead != nNumberOfBytesToRead) {
break; break;
else { } else {
// Is there data left in the pipe? // Is there data left in the pipe?
DWORD bytesAvailable = 0; DWORD bytesAvailable = 0;
if (!PeekNamedPipe(handle, NULL, 0, NULL, &bytesAvailable, NULL) || bytesAvailable == 0) if (!PeekNamedPipe(handle, NULL, 0, NULL, &bytesAvailable, NULL) || bytesAvailable == 0) {
// No bytes left // No bytes left
break; break;
}
} }
} }
CloseHandle(overlapped.hEvent); CloseHandle(overlapped.hEvent);
@ -169,14 +171,14 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_SpawnerInputStream_clo
int rc; int rc;
HANDLE handle = channelToHandle(env, channel); HANDLE handle = channelToHandle(env, channel);
#ifdef DEBUG_MONITOR #ifdef DEBUG_MONITOR
_TCHAR buffer[1000]; _TCHAR buffer[1000];
_stprintf(buffer, _T("Close %i\n"), fd); _stprintf(buffer, _T("Close %i\n"), fd);
OutputDebugStringW(buffer); OutputDebugStringW(buffer);
#endif #endif
rc = (CloseHandle(handle) ? 0 : -1); rc = (CloseHandle(handle) ? 0 : -1);
#ifdef DEBUG_MONITOR #ifdef DEBUG_MONITOR
_stprintf(buffer, _T("Closed %i\n"), fd); _stprintf(buffer, _T("Closed %i\n"), fd);
OutputDebugStringW(buffer); OutputDebugStringW(buffer);
#endif #endif
return (rc ? GetLastError() : 0); return (rc ? GetLastError() : 0);
} }
@ -232,15 +234,15 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_SpawnerOutputStream_cl
int rc; int rc;
HANDLE handle = channelToHandle(env, channel); HANDLE handle = channelToHandle(env, channel);
#ifdef DEBUG_MONITOR #ifdef DEBUG_MONITOR
_TCHAR buffer[1000]; _TCHAR buffer[1000];
_stprintf(buffer, _T("Close %i\n"), fd); _stprintf(buffer, _T("Close %i\n"), fd);
OutputDebugStringW(buffer); OutputDebugStringW(buffer);
#endif #endif
FlushFileBuffers(handle); FlushFileBuffers(handle);
rc = (CloseHandle(handle) ? 0 : -1); rc = (CloseHandle(handle) ? 0 : -1);
#ifdef DEBUG_MONITOR #ifdef DEBUG_MONITOR
_stprintf(buffer, _T("Closed %i\n"), fd); _stprintf(buffer, _T("Closed %i\n"), fd);
OutputDebugStringW(buffer); OutputDebugStringW(buffer);
#endif #endif
return (rc ? GetLastError() : 0); return (rc ? GetLastError() : 0);
} }

View file

@ -53,8 +53,9 @@ JNIEXPORT jstring JNICALL Java_org_eclipse_cdt_utils_pty_PTY_openMaster(JNIEnv *
master = rand(); master = rand();
/* Make sure masterFD does not exist */ /* Make sure masterFD does not exist */
while (fd2pty.find(master) != fd2pty.end()) while (fd2pty.find(master) != fd2pty.end()) {
master++; master++;
}
sprintf(line, "winpty_%i", master); sprintf(line, "winpty_%i", master);
@ -86,8 +87,9 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_pty_PTY_change_1window_1size(J
fd2pty_Iter = fd2pty.find(fd); fd2pty_Iter = fd2pty.find(fd);
if (fd2pty_Iter != fd2pty.end()) { if (fd2pty_Iter != fd2pty.end()) {
winpty_t *winpty = fd2pty_Iter->second; winpty_t *winpty = fd2pty_Iter->second;
if (winpty != NULL) if (winpty != NULL) {
return winpty_set_size(winpty, width, height); return winpty_set_size(winpty, width, height);
}
} }
return 0; return 0;
@ -121,15 +123,18 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_pty_PTYInputStream_read0(JNIEn
BOOL ret = ReadFile(handle, buffer, buf_len, &amount, &over); BOOL ret = ReadFile(handle, buffer, buf_len, &amount, &over);
if (!ret) { if (!ret) {
DWORD error = GetLastError(); DWORD error = GetLastError();
if (error == ERROR_IO_PENDING) if (error == ERROR_IO_PENDING) {
ret = GetOverlappedResult(handle, &over, &amount, TRUE); ret = GetOverlappedResult(handle, &over, &amount, TRUE);
}
} }
if (ret && amount > 0) if (ret && amount > 0) {
memcpy(data, buffer, amount); memcpy(data, buffer, amount);
}
if (!ret || amount == 0) if (!ret || amount == 0) {
amount = -1; amount = -1;
}
if (!ret && fd2pty.find(fd) != fd2pty.end()) { if (!ret && fd2pty.find(fd) != fd2pty.end()) {
int rc = winpty_get_exit_code(winpty); int rc = winpty_get_exit_code(winpty);
@ -190,10 +195,12 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_pty_PTYOutputStream_write0(JNI
BOOL ret = WriteFile(handle, buffer, buf_len, &written, &over); BOOL ret = WriteFile(handle, buffer, buf_len, &written, &over);
env->ReleaseByteArrayElements(buf, data, 0); env->ReleaseByteArrayElements(buf, data, 0);
if (!ret && GetLastError() == ERROR_IO_PENDING) if (!ret && GetLastError() == ERROR_IO_PENDING) {
ret = GetOverlappedResult(handle, &over, &written, TRUE); ret = GetOverlappedResult(handle, &over, &written, TRUE);
if (!ret || (int) written != buf_len) }
if (!ret || (int) written != buf_len) {
written = -1; written = -1;
}
delete[] buffer; delete[] buffer;
} }
@ -227,10 +234,11 @@ static std::wstring convertSlashes(const wchar_t *path) {
std::wstring ret; std::wstring ret;
for (int i = 0; path[i] != L'\0'; ++i) { for (int i = 0; path[i] != L'\0'; ++i) {
if (path[i] == L'/') if (path[i] == L'/') {
ret.push_back(L'\\'); ret.push_back(L'\\');
else } else {
ret.push_back(path[i]); ret.push_back(path[i]);
}
} }
return ret; return ret;
@ -241,12 +249,14 @@ static std::wstring convertSlashes(const wchar_t *path) {
static std::wstring argvToCommandLine(const std::vector<std::wstring> &argv) { static std::wstring argvToCommandLine(const std::vector<std::wstring> &argv) {
std::wstring result; std::wstring result;
for (size_t argIndex = 0; argIndex < argv.size(); ++argIndex) { for (size_t argIndex = 0; argIndex < argv.size(); ++argIndex) {
if (argIndex > 0) if (argIndex > 0) {
result.push_back(L' '); result.push_back(L' ');
}
const wchar_t *arg = argv[argIndex].c_str(); const wchar_t *arg = argv[argIndex].c_str();
const bool quote = wcschr(arg, L' ') != NULL || wcschr(arg, L'\t') != NULL || *arg == L'\0'; const bool quote = wcschr(arg, L' ') != NULL || wcschr(arg, L'\t') != NULL || *arg == L'\0';
if (quote) if (quote) {
result.push_back(L'\"'); result.push_back(L'\"');
}
int bsCount = 0; int bsCount = 0;
for (const wchar_t *p = arg; *p != L'\0'; ++p) { for (const wchar_t *p = arg; *p != L'\0'; ++p) {
if (*p == L'\\') { if (*p == L'\\') {
@ -285,8 +295,9 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_pty_PTY_exec2(JNIEnv *env, job
jint argc = env->GetArrayLength(jcmd); jint argc = env->GetArrayLength(jcmd);
jint envc = env->GetArrayLength(jenv); jint envc = env->GetArrayLength(jenv);
if (jchannels == NULL || env->GetArrayLength(jchannels) != 3) if (jchannels == NULL || env->GetArrayLength(jchannels) != 3) {
goto bail_out; goto bail_out;
}
fd = masterFD; fd = masterFD;
fd2pty_Iter = fd2pty.find(fd); fd2pty_Iter = fd2pty.find(fd);
@ -298,10 +309,11 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_pty_PTY_exec2(JNIEnv *env, job
for (i = 0; i < argc; i++) { for (i = 0; i < argc; i++) {
jstring j_str = (jstring) env->GetObjectArrayElement(jcmd, i); jstring j_str = (jstring) env->GetObjectArrayElement(jcmd, i);
const wchar_t *w_str = (const wchar_t*) env->GetStringChars(j_str, NULL); const wchar_t *w_str = (const wchar_t*) env->GetStringChars(j_str, NULL);
if (i == 0) if (i == 0) {
argVector.push_back(convertSlashes(w_str)); argVector.push_back(convertSlashes(w_str));
else } else {
argVector.push_back(w_str); argVector.push_back(w_str);
}
env->ReleaseStringChars(j_str, (const jchar*) w_str); env->ReleaseStringChars(j_str, (const jchar*) w_str);
env->DeleteLocalRef(j_str); env->DeleteLocalRef(j_str);
} }
@ -352,8 +364,9 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_pty_PTY_waitFor(JNIEnv *env, j
BOOL success; BOOL success;
do { do {
success = GetHandleInformation(handle, &flags); success = GetHandleInformation(handle, &flags);
if (success) if (success) {
Sleep(500); Sleep(500);
}
} while (success); } while (success);
fd2rc_Iter = fd2rc.find(fd); fd2rc_Iter = fd2rc.find(fd);

View file

@ -49,8 +49,9 @@ HMODULE PTYExplicitLoadLibrary(LPCSTR pszModuleName) {
} }
FARPROC WINAPI PTYDliNotifyHook(unsigned dliNotify, PDelayLoadInfo pdli) { FARPROC WINAPI PTYDliNotifyHook(unsigned dliNotify, PDelayLoadInfo pdli) {
if (dliNotify == dliNotePreLoadLibrary) if (dliNotify == dliNotePreLoadLibrary) {
return (FARPROC) PTYExplicitLoadLibrary(pdli->szDll); return (FARPROC) PTYExplicitLoadLibrary(pdli->szDll);
}
return NULL; return NULL;
} }

View file

@ -91,8 +91,9 @@ int interruptProcess(int pid) {
FreeLibrary(hmod); FreeLibrary(hmod);
hmod = NULL; hmod = NULL;
if (success) if (success) {
return 0; // 0 == OK; if not, try old-school way return 0; // 0 == OK; if not, try old-school way
}
} }
#ifdef DEBUG_MONITOR #ifdef DEBUG_MONITOR
@ -102,14 +103,13 @@ int interruptProcess(int pid) {
consoleHWND = NULL; consoleHWND = NULL;
#ifdef DEBUG_MONITOR #ifdef DEBUG_MONITOR
_stprintf(buffer, _T("Try to interrupt process %i\n"), pid); _stprintf(buffer, _T("Try to interrupt process %i\n"), pid);
OutputDebugStringW(buffer); OutputDebugStringW(buffer);
#endif #endif
// Find console // Find console
EnumWindows(find_child_console, (LPARAM) pid); EnumWindows(find_child_console, (LPARAM) pid);
if (NULL != consoleHWND) // Yes, we found out it if (NULL != consoleHWND) { // Yes, we found out it
{
// We are going to switch focus to console, // We are going to switch focus to console,
// send Ctrl-C and then restore focus // send Ctrl-C and then restore focus
BYTE control_scan_code = (BYTE) MapVirtualKey(VK_CONTROL, 0); BYTE control_scan_code = (BYTE) MapVirtualKey(VK_CONTROL, 0);
@ -123,20 +123,23 @@ int interruptProcess(int pid) {
foreground_window = GetForegroundWindow(); foreground_window = GetForegroundWindow();
if (foreground_window) { if (foreground_window) {
/* NT 5.0, and apparently also Windows 98, will not allow /* NT 5.0, and apparently also Windows 98, will not allow
a Window to be set to foreground directly without the * a Window to be set to foreground directly without the
user's involvement. The workaround is to attach * user's involvement. The workaround is to attach
ourselves to the thread that owns the foreground * ourselves to the thread that owns the foreground
window, since that is the only thread that can set the * window, since that is the only thread that can set the
foreground window. */ * foreground window.
*/
DWORD foreground_thread, child_thread; DWORD foreground_thread, child_thread;
foreground_thread = GetWindowThreadProcessId(foreground_window, NULL); foreground_thread = GetWindowThreadProcessId(foreground_window, NULL);
if (foreground_thread == GetCurrentThreadId() if (foreground_thread == GetCurrentThreadId()
|| !AttachThreadInput(GetCurrentThreadId(), foreground_thread, TRUE)) || !AttachThreadInput(GetCurrentThreadId(), foreground_thread, TRUE)) {
foreground_thread = 0; foreground_thread = 0;
}
child_thread = GetWindowThreadProcessId(consoleHWND, NULL); child_thread = GetWindowThreadProcessId(consoleHWND, NULL);
if (child_thread == GetCurrentThreadId() || !AttachThreadInput(GetCurrentThreadId(), child_thread, TRUE)) if (child_thread == GetCurrentThreadId() || !AttachThreadInput(GetCurrentThreadId(), child_thread, TRUE)) {
child_thread = 0; child_thread = 0;
}
/* Set the foreground window to the child. */ /* Set the foreground window to the child. */
if (SetForegroundWindow(consoleHWND)) { if (SetForegroundWindow(consoleHWND)) {
@ -153,24 +156,24 @@ int interruptProcess(int pid) {
SetForegroundWindow(foreground_window); SetForegroundWindow(foreground_window);
} }
/* Detach from the foreground and child threads now that /* Detach from the foreground and child threads now that the foreground switching is over. */
the foreground switching is over. */ if (foreground_thread) {
if (foreground_thread)
AttachThreadInput(GetCurrentThreadId(), foreground_thread, FALSE); AttachThreadInput(GetCurrentThreadId(), foreground_thread, FALSE);
if (child_thread) }
if (child_thread) {
AttachThreadInput(GetCurrentThreadId(), child_thread, FALSE); AttachThreadInput(GetCurrentThreadId(), child_thread, FALSE);
}
#ifdef DEBUG_MONITOR #ifdef DEBUG_MONITOR
_stprintf(buffer, _T("Sent Ctrl-C & Ctrl-Break to process %i\n"), pid); _stprintf(buffer, _T("Sent Ctrl-C & Ctrl-Break to process %i\n"), pid);
OutputDebugStringW(buffer); OutputDebugStringW(buffer);
#endif #endif
} }
}
#ifdef DEBUG_MONITOR #ifdef DEBUG_MONITOR
else { } else {
_stprintf(buffer, _T("Cannot find console for process %i\n"), pid); _stprintf(buffer, _T("Cannot find console for process %i\n"), pid);
OutputDebugStringW(buffer); OutputDebugStringW(buffer);
}
#endif #endif
}
return rc; return rc;
} }

View file

@ -35,10 +35,11 @@ BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID lpRese
InitializeCriticalSection(&cs); InitializeCriticalSection(&cs);
GetModuleFileNameW(hModule, path, MAX_PATH); GetModuleFileNameW(hModule, path, MAX_PATH);
p = wcsrchr(path, _T('\\')); p = wcsrchr(path, _T('\\'));
if (NULL != p) if (NULL != p) {
*(p + 1) = _T('\0'); *(p + 1) = _T('\0');
else } else {
wcscat(path, L"\\"); wcscat(path, L"\\");
}
} }
break; break;
case DLL_THREAD_ATTACH: case DLL_THREAD_ATTACH:

View file

@ -36,8 +36,7 @@ void DisplayErrorMessage();
//BOOL KillProcessEx(DWORD dwProcessId); // Handle of the process //BOOL KillProcessEx(DWORD dwProcessId); // Handle of the process
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
BOOL WINAPI HandlerRoutine(DWORD dwCtrlType) // control signal type BOOL WINAPI HandlerRoutine(DWORD dwCtrlType) { // control signal type
{
BOOL ret = TRUE; BOOL ret = TRUE;
switch (dwCtrlType) { switch (dwCtrlType) {
case CTRL_C_EVENT: case CTRL_C_EVENT:
@ -66,8 +65,9 @@ bool _isCygwin = true;
bool isCygwin(HANDLE process) { bool isCygwin(HANDLE process) {
// Have we checked before? // Have we checked before?
if (cygwinBin != NULL || !_isCygwin) if (cygwinBin != NULL || !_isCygwin) {
return _isCygwin; return _isCygwin;
}
// See if this process loaded cygwin, need a different SIGINT for them // See if this process loaded cygwin, need a different SIGINT for them
HMODULE mods[1024]; HMODULE mods[1024];
@ -239,7 +239,7 @@ int main() {
CloseHandle(stdHandles[0]); CloseHandle(stdHandles[0]);
CloseHandle(stdHandles[1]); CloseHandle(stdHandles[1]);
CloseHandle(stdHandles[2]); CloseHandle(stdHandles[2]);
return -1;; return -1;
} }
SetHandleInformation(stdHandles[0], HANDLE_FLAG_INHERIT, TRUE); SetHandleInformation(stdHandles[0], HANDLE_FLAG_INHERIT, TRUE);
SetHandleInformation(stdHandles[1], HANDLE_FLAG_INHERIT, TRUE); SetHandleInformation(stdHandles[1], HANDLE_FLAG_INHERIT, TRUE);
@ -254,21 +254,21 @@ int main() {
CloseHandle(stdHandles[0]); CloseHandle(stdHandles[0]);
CloseHandle(stdHandles[1]); CloseHandle(stdHandles[1]);
CloseHandle(stdHandles[2]); CloseHandle(stdHandles[2]);
return -1;; return -1;
} }
#ifdef DEBUG_MONITOR_DETAILS #ifdef DEBUG_MONITOR_DETAILS
wchar_t * lpvEnv = GetEnvironmentStringsW(); wchar_t * lpvEnv = GetEnvironmentStringsW();
// If the returned pointer is NULL, exit. // If the returned pointer is NULL, exit.
if (lpvEnv == NULL) if (lpvEnv == NULL) {
OutputDebugStringW(_T("Cannot Read Environment\n")); OutputDebugStringW(_T("Cannot Read Environment\n"));
else { } else {
// Variable strings are separated by NULL byte, and the block is // Variable strings are separated by NULL byte, and the block is
// terminated by a NULL byte. // terminated by a NULL byte.
OutputDebugStringW(_T("Starter: Environment\n")); OutputDebugStringW(_T("Starter: Environment\n"));
for (wchar_t * lpszVariable = (wchar_t *) lpvEnv; *lpszVariable; lpszVariable+=wcslen(lpszVariable) + 1) { for (wchar_t * lpszVariable = (wchar_t *) lpvEnv; *lpszVariable; lpszVariable += wcslen(lpszVariable) + 1) {
swprintf(buffer, sizeof(buffer)/sizeof(buffer[0]), _T("%s\n"), lpszVariable); swprintf(buffer, sizeof(buffer)/sizeof(buffer[0]), _T("%s\n"), lpszVariable);
OutputDebugStringW(buffer); OutputDebugStringW(buffer);
} }
@ -307,8 +307,9 @@ int main() {
// to our own job object. // to our own job object.
BOOL f = CreateProcessW(NULL, szCmdLine, NULL, NULL, TRUE, CREATE_BREAKAWAY_FROM_JOB, NULL, NULL, &si, &pi); BOOL f = CreateProcessW(NULL, szCmdLine, NULL, NULL, TRUE, CREATE_BREAKAWAY_FROM_JOB, NULL, NULL, &si, &pi);
// If breaking away from job is not permitted, retry without breakaway flag // If breaking away from job is not permitted, retry without breakaway flag
if (!f) if (!f) {
f = CreateProcessW(NULL, szCmdLine, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi); f = CreateProcessW(NULL, szCmdLine, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
}
// We don't need them any more // We don't need them any more
CloseHandle(stdHandles[0]); CloseHandle(stdHandles[0]);
@ -462,8 +463,9 @@ int copyTo(wchar_t *target, const wchar_t *source, int cpyLength, int availSpace
#define QUOTATION_NONE 2 #define QUOTATION_NONE 2
int nQuotationMode = 0; int nQuotationMode = 0;
if (availSpace <= cpyLength) // = to reserve space for '\0' if (availSpace <= cpyLength) { // = to reserve space for '\0'
return -1; return -1;
}
if ((_T('\"') == *source) && (_T('\"') == *(source + cpyLength - 1))) { if ((_T('\"') == *source) && (_T('\"') == *(source + cpyLength - 1))) {
// Already done // Already done
@ -479,29 +481,35 @@ int copyTo(wchar_t *target, const wchar_t *source, int cpyLength, int availSpace
} }
for (; i < cpyLength; ++i, ++j) { for (; i < cpyLength; ++i, ++j) {
if (source[i] == _T('\\')) if (source[i] == _T('\\')) {
bSlash = TRUE; bSlash = TRUE;
else } else {
// Don't escape embracing quotation marks // Don't escape embracing quotation marks
if ((source[i] == _T('\"')) && !((nQuotationMode == QUOTATION_DONE) && ((i == 0) || (i == (cpyLength - 1))))) { if ((source[i] == _T('\"'))
if (!bSlash) { && !((nQuotationMode == QUOTATION_DONE) && ((i == 0) || (i == (cpyLength - 1))))) {
if (j == availSpace) if (!bSlash) {
return -1; if (j == availSpace) {
target[j] = _T('\\'); return -1;
++j; }
target[j] = _T('\\');
++j;
}
bSlash = FALSE;
} else {
bSlash = FALSE;
} }
bSlash = FALSE; }
} else
bSlash = FALSE;
if (j == availSpace) if (j == availSpace) {
return -1; return -1;
}
target[j] = source[i]; target[j] = source[i];
} }
if (nQuotationMode == QUOTATION_DO) { if (nQuotationMode == QUOTATION_DO) {
if (j == availSpace) if (j == availSpace) {
return -1; return -1;
}
target[j] = _T('\"'); target[j] = _T('\"');
++j; ++j;
} }

View file

@ -46,11 +46,10 @@
* IOException * IOException
*/ */
#ifndef __MINGW32__ #ifndef __MINGW32__
static void closeAndthrowIOException(int fd, JNIEnv *env, const char *msg) static void closeAndthrowIOException(int fd, JNIEnv *env, const char *msg) {
#else #else
static void closeAndthrowIOException(HANDLE handle, JNIEnv *env, const char *msg) static void closeAndthrowIOException(HANDLE handle, JNIEnv *env, const char *msg) {
#endif #endif
{
char buff[256]; char buff[256];
#ifndef __MINGW32__ #ifndef __MINGW32__
sprintf(buff, "%s: %s", msg, strerror(errno)); sprintf(buff, "%s: %s", msg, strerror(errno));
@ -342,8 +341,7 @@ JNIEXPORT jlong JNICALL FUNC(open0)(JNIEnv *env, jobject jobj, jstring portName,
} }
JNIEXPORT void JNICALL FUNC(close0) JNIEXPORT void JNICALL FUNC(close0)
(JNIEnv *env, jobject jobj, jlong handle) (JNIEnv *env, jobject jobj, jlong handle) {
{
#ifndef __MINGW32__ #ifndef __MINGW32__
close(handle); close(handle);
#else #else
@ -435,8 +433,7 @@ JNIEXPORT jint JNICALL FUNC(read1)(JNIEnv *env, jobject jobj, jlong jhandle, jby
} }
JNIEXPORT void JNICALL FUNC(write0) JNIEXPORT void JNICALL FUNC(write0)
(JNIEnv *env, jobject jobj, jlong jhandle, jint b) (JNIEnv *env, jobject jobj, jlong jhandle, jint b) {
{
#ifndef __MINGW32__ #ifndef __MINGW32__
char buff = b; char buff = b;
write(jhandle, &buff, 1); write(jhandle, &buff, 1);
@ -475,8 +472,7 @@ JNIEXPORT void JNICALL FUNC(write0)
#endif #endif
} }
JNIEXPORT void JNICALL FUNC(write1)(JNIEnv *env, jobject jobj, jlong jhandle, jbyteArray bytes, jint offset, jint size) JNIEXPORT void JNICALL FUNC(write1)(JNIEnv *env, jobject jobj, jlong jhandle, jbyteArray bytes, jint offset, jint size) {
{
#ifndef __MINGW32__ #ifndef __MINGW32__
while (size > 0) { while (size > 0) {
jbyte buff[256]; jbyte buff[256];
@ -512,8 +508,7 @@ JNIEXPORT void JNICALL FUNC(write1)(JNIEnv *env, jobject jobj, jlong jhandle, jb
if (GetLastError() != ERROR_IO_PENDING) { if (GetLastError() != ERROR_IO_PENDING) {
throwIOException(env, "Error writing to port"); throwIOException(env, "Error writing to port");
return; return;
} } else {
else {
switch (WaitForSingleObject(olp.hEvent, INFINITE)) { switch (WaitForSingleObject(olp.hEvent, INFINITE)) {
case WAIT_OBJECT_0: case WAIT_OBJECT_0:
if (!GetOverlappedResult(handle, &olp, &nwritten, FALSE)) { if (!GetOverlappedResult(handle, &olp, &nwritten, FALSE)) {