Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlain Magloire2002-10-17 19:18:20 +0000
committerAlain Magloire2002-10-17 19:18:20 +0000
commit367ffc297249df3c043a6fe2cdd38a9c0925481d (patch)
tree54d6cbeea3091f1495b617492e26b27300ff0e66 /core/org.eclipse.cdt.core.qnx
parentcbcaf432bb6cd00277f6a70b3dddcafb17ea10f7 (diff)
downloadorg.eclipse.cdt-367ffc297249df3c043a6fe2cdd38a9c0925481d.tar.gz
org.eclipse.cdt-367ffc297249df3c043a6fe2cdd38a9c0925481d.tar.xz
org.eclipse.cdt-367ffc297249df3c043a6fe2cdd38a9c0925481d.zip
Fix some memory leaks.
Diffstat (limited to 'core/org.eclipse.cdt.core.qnx')
-rw-r--r--core/org.eclipse.cdt.core.qnx/library/spawner.c223
1 files changed, 114 insertions, 109 deletions
diff --git a/core/org.eclipse.cdt.core.qnx/library/spawner.c b/core/org.eclipse.cdt.core.qnx/library/spawner.c
index 85c0515eed..6bc28ba8c4 100644
--- a/core/org.eclipse.cdt.core.qnx/library/spawner.c
+++ b/core/org.eclipse.cdt.core.qnx/library/spawner.c
@@ -17,115 +17,122 @@ typedef JNIEXPORT char * (JNICALL * JVM_NativePath)(const char *);
void ThrowByName(JNIEnv *env, const char *name, const char *msg);
void * GetJVMProc(char * vmlib, char * procName);
-
-static void * hVM = NULL; // Java Virtual Machine handler
-
+static void * hVM = NULL; /* Java Virtual Machine handler. */
JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec0
(JNIEnv * env, jobject proc, jobjectArray cmdArray, jobjectArray envp, jstring dir, jintArray channels)
{
- int fd_map[3]; // File descriptors
- int fd_ret[3]; // File descriptors that we return to Java
- int fd[2]; // Pipe open structure
- int i;
- int nParms = 0;// Number of parameters
- int nEnvs = 0;// Number of environment variables
- char ** pParms = NULL; // Parameters
- char ** pEnvs = NULL; // Environment variables
- char * pCommand = NULL; // Command to execute
- char * pwd = 0; // Process working directory
- char cwd[PATH_MAX + 1]; // Current working directory
- pid_t pid; // Process ID
- struct inheritance inherit;
-
-
+ int fd_map[3]; /* File descriptors. */
+ int fd_ret[3]; /* File descriptors that we return to Java. */
+ int fd[2]; /* Pipe open structure. */
+ int i;
+ int nParms = 0;/* Number of parameters. */
+ int nEnvs = 0;/* Number of environment variables. */
+ char ** pParms = NULL; /* Parameters. */
+ char ** pEnvs = NULL; /* Environment variables. */
+ char * pCommand = NULL; /* Command to execute. */
+ char cwd[PATH_MAX + 1]; /* Current working directory. */
+ pid_t pid; /* Process ID. */
+ struct inheritance inherit;
- if ((cmdArray == 0) || ((nParms = (*env) -> GetArrayLength(env, cmdArray)) == 0))
- ThrowByName(env, "java/lang/NullPointerException", "No command line specified");
- for(i = 0; i < 3; ++i)
- {
- if(EOK != pipe(fd))
- ThrowByName(env, "java/io/IOException", "Cannot create pipe for spawner");
- if(0 ==i)
- {
- fd_map[i] = fd[0];
- fd_ret[i] = fd[1];
- }
- else
- {
- fd_map[i] = fd[1];
- fd_ret[i] = fd[0];
- }
- }
-
-
-
- if(nParms > 0)
- {
- pParms = malloc(sizeof(char *) * (nParms + 1));
- for(i = 0; i < nParms; ++i)
- {
- jobject item = (*env) -> GetObjectArrayElement(env, cmdArray, i);
- const char * str = (*env) -> GetStringUTFChars(env, item, 0);
- if(i == 0)
- pCommand = strdup(str);
- pParms[i] = strdup(str);
- (*env) -> ReleaseStringUTFChars(env, item, str);
+ if ((cmdArray == NULL) || ((nParms = (*env)->GetArrayLength(env, cmdArray)) == 0))
+ ThrowByName(env, "java/lang/IOException", "No command line specified");
+
+ for (i = 0; i < 3; ++i) {
+ if (EOK != pipe(fd))
+ ThrowByName(env, "java/io/IOException", "Cannot create pipe for spawner");
+ if (0 == i) {
+ fd_map[i] = fd[0];
+ fd_ret[i] = fd[1];
+ } else {
+ fd_map[i] = fd[1];
+ fd_ret[i] = fd[0];
+ }
}
- pParms[i] = NULL;
- }
- nEnvs = (*env) -> GetArrayLength(env, envp);
- if(nEnvs > 0)
- {
- pEnvs = malloc(sizeof(char *) * (nEnvs + 1));
- for(i = 0; i < nEnvs; ++i)
- {
- jobject item = (*env) -> GetObjectArrayElement(env, envp, i);
- const char * str = (*env) -> GetStringUTFChars(env, item, 0);
- pEnvs[i] = strdup(str);
- (*env) -> ReleaseStringUTFChars(env, item, str);
+
+ if (nParms > 0) {
+ pParms = malloc(sizeof(char *) * (nParms + 1));
+ for (i = 0; i < nParms; ++i) {
+ jobject item = (*env)->GetObjectArrayElement(env, cmdArray, i);
+ const char *str = (*env)->GetStringUTFChars(env, item, 0);
+ if (i == 0)
+ pCommand = strdup(str);
+ pParms[i] = strdup(str);
+ (*env)->ReleaseStringUTFChars(env, item, str);
+ }
+ pParms[i] = NULL;
}
- pEnvs[i] = NULL;
- }
- if (dir != 0)
- {
- char * item;
- pwd = strdup(item = (char *)(*env) -> GetStringUTFChars(env, dir, 0));
- getcwd(cwd, sizeof(cwd));
- chdir(pwd);
- (*env) -> ReleaseStringUTFChars(env, dir, item);
- }
-
- // Nothing for now
- memset(&inherit, 0, sizeof(inherit));
- inherit.flags = SPAWN_SETGROUP;
- inherit.pgroup = SPAWN_NEWPGROUP;
-
- pid = spawnp(pCommand, 3, fd_map, &inherit, pParms, pEnvs);
+ nEnvs = (*env) -> GetArrayLength(env, envp);
+ if (nEnvs > 0) {
+ pEnvs = malloc(sizeof(char *) * (nEnvs + 1));
+ for (i = 0; i < nEnvs; ++i) {
+ jobject item = (*env)->GetObjectArrayElement(env, envp, i);
+ const char *str = (*env)->GetStringUTFChars(env, item, 0);
+ pEnvs[i] = strdup(str);
+ (*env)->ReleaseStringUTFChars(env, item, str);
+ }
+ pEnvs[i] = NULL;
+ }
- if(dir != 0) // Restore working directory
- chdir(cwd);
-
- for(i = 0; i < 3; ++i)
- {
- close(fd_map[i]);
- }
-
- if(-1 == pid) // Failed - close pipes
- {
- for(i = 0; i < 3; ++i)
- {
- close(fd_ret[i]);
- }
- }
- else // Success - return pipes to Java
- {
- (*env) -> SetIntArrayRegion(env, channels, 0, 3, fd_ret);
- }
-
- return pid;
+ if (dir != 0) {
+ char *item = (char *)(*env)->GetStringUTFChars(env, dir, 0);
+ getcwd(cwd, sizeof(cwd));
+ chdir(item);
+ (*env)->ReleaseStringUTFChars(env, dir, item);
+ }
+
+ /* Nothing for now. */
+ memset(&inherit, 0, sizeof(inherit));
+ inherit.flags = SPAWN_SETGROUP;
+ inherit.pgroup = SPAWN_NEWPGROUP;
+
+ pid = spawnp(pCommand, 3, fd_map, &inherit, pParms, pEnvs);
+
+ if (dir != 0) /* Restore working directory. */
+ chdir(cwd);
+
+ for (i = 0; i < 3; ++i) {
+ close(fd_map[i]);
+ }
+ if (-1 == pid) { /* Failed - close pipes. */
+ for (i = 0; i < 3; ++i) {
+ close(fd_ret[i]);
+ }
+ } else { /* Success - return pipes to Java. */
+ (*env) -> SetIntArrayRegion(env, channels, 0, 3, fd_ret);
+ }
+
+ /* Free Parameters. */
+ if (pParms != NULL) {
+ int j;
+ for (j = 0; pParms[j] != NULL; j++) {
+ if (pParms[j] != NULL) {
+ free(pParms[j]);
+ }
+ }
+ free(pParms);
+ }
+
+ /* Free Environment variables. */
+ if (pEnvs != NULL) {
+ int j;
+ for (j = 0; pEnvs[j] != NULL; j++) {
+ if (pEnvs[j] != NULL) {
+ free(pEnvs[j]);
+ }
+ }
+ free(pEnvs);
+ }
+
+ /* Free Command to execute. */
+ if (pCommand != NULL) {
+ free(pCommand);
+ }
+
+ return pid;
+
}
JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec1
@@ -142,11 +149,9 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec1
pid_t pid; // Process ID
struct inheritance inherit;
-
if ((cmdArray == 0) || ((nParms = (*env) -> GetArrayLength(env, cmdArray)) == 0))
ThrowByName(env, "java/lang/NullPointerException", "No command line specified");
-
if(nParms > 0)
{
pParms = malloc(sizeof(char *) * (nParms + 1));
@@ -208,7 +213,7 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec1
JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_raise
(JNIEnv * env, jobject proc, jint pid, jint sig)
{
- return kill(pid, sig);
+ return kill(pid, sig);
}
/*
@@ -219,8 +224,8 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_raise
JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_waitFor
(JNIEnv * env, jobject proc, jint pid)
{
- int stat_loc;
- return (waitpid(pid, &stat_loc, WEXITED));
+ int stat_loc;
+ return (waitpid(pid, &stat_loc, WEXITED));
}
@@ -228,13 +233,13 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_waitFor
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 */
- (*env)->ThrowNew(env, cls, msg);
+ if (cls != 0) /* Otherwise an exception has already been thrown */
+ (*env)->ThrowNew(env, cls, msg);
- /* It's a good practice to clean up the local references. */
- (*env)->DeleteLocalRef(env, cls);
+ /* It's a good practice to clean up the local references. */
+ (*env)->DeleteLocalRef(env, cls);
}

Back to the top