/[classpath]/classpath/native/jni/java-lang/java_lang_VMProcess.c
ViewVC logotype

Diff of /classpath/native/jni/java-lang/java_lang_VMProcess.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.2.2.3 by gnu_andrew, Mon Apr 18 01:37:42 2005 UTC revision 1.2.2.4 by tromey, Wed May 4 22:59:44 2005 UTC
# Line 1  Line 1 
1  /* java_lang_VMProcess.c -- native code for java.lang.VMProcess  /* java_lang_VMProcess.c -- native code for java.lang.VMProcess
2     Copyright (C) 1998, 1999, 2000, 2002, 2004 Free Software Foundation, Inc.     Copyright (C) 1998, 1999, 2000, 2002, 2004, 2005 Free Software Foundation, Inc.
3    
4  This file is part of GNU Classpath.  This file is part of GNU Classpath.
5    
# Line 135  copy_elem (JNIEnv * env, jobject stringA Line 135  copy_elem (JNIEnv * env, jobject stringA
135  JNIEXPORT void JNICALL  JNIEXPORT void JNICALL
136  Java_java_lang_VMProcess_nativeSpawn (JNIEnv * env, jobject this,  Java_java_lang_VMProcess_nativeSpawn (JNIEnv * env, jobject this,
137                                        jobjectArray cmdArray,                                        jobjectArray cmdArray,
138                                        jobjectArray envArray, jobject dirFile)                                        jobjectArray envArray, jobject dirFile,
139                                          jboolean redirect)
140  {  {
141    int fds[3][2] = { {-1, -1}, {-1, -1}, {-1, -1} };    int fds[3][2] = { {-1, -1}, {-1, -1}, {-1, -1} };
142    jobject streams[3] = { NULL, NULL, NULL };    jobject streams[3] = { NULL, NULL, NULL };
# Line 151  Java_java_lang_VMProcess_nativeSpawn (JN Line 152  Java_java_lang_VMProcess_nativeSpawn (JN
152    jmethodID method;    jmethodID method;
153    jclass clazz;    jclass clazz;
154    int i;    int i;
155      int pipe_count = redirect ? 2 : 3;
156    
157    /* Check for null */    /* Check for null */
158    if (cmdArray == NULL)    if (cmdArray == NULL)
# Line 218  Java_java_lang_VMProcess_nativeSpawn (JN Line 220  Java_java_lang_VMProcess_nativeSpawn (JN
220      }      }
221    
222    /* Create inter-process pipes */    /* Create inter-process pipes */
223    for (i = 0; i < 3; i++)    for (i = 0; i < pipe_count; i++)
224      {      {
225        if (pipe (fds[i]) == -1)        if (pipe (fds[i]) == -1)
226          {          {
# Line 232  Java_java_lang_VMProcess_nativeSpawn (JN Line 234  Java_java_lang_VMProcess_nativeSpawn (JN
234    /* Set close-on-exec flag for parent's ends of pipes */    /* Set close-on-exec flag for parent's ends of pipes */
235    (void) fcntl (fds[0][1], F_SETFD, 1);    (void) fcntl (fds[0][1], F_SETFD, 1);
236    (void) fcntl (fds[1][0], F_SETFD, 1);    (void) fcntl (fds[1][0], F_SETFD, 1);
237    (void) fcntl (fds[2][0], F_SETFD, 1);    if (pipe_count == 3)
238        (void) fcntl (fds[2][0], F_SETFD, 1);
239    
240    /* Fork into parent and child processes */    /* Fork into parent and child processes */
241    if ((pid = fork ()) == (pid_t) - 1)    if ((pid = fork ()) == (pid_t) - 1)
# Line 267  Java_java_lang_VMProcess_nativeSpawn (JN Line 270  Java_java_lang_VMProcess_nativeSpawn (JN
270              }              }
271            close (fds[1][1]);            close (fds[1][1]);
272          }          }
273        if (fds[2][1] != 2)        if (pipe_count == 3)
274            {
275              /* Duplicate stdout to stderr.  */
276              if (dup2 (1, 2) == -1)
277                {
278                  fprintf (stderr, "dup2: %s", strerror (errno));
279                  exit (127);
280                }
281            }
282          else if (fds[2][1] != 2)
283          {          {
284            if (dup2 (fds[2][1], 2) == -1)            if (dup2 (fds[2][1], 2) == -1)
285              {              {
# Line 308  Java_java_lang_VMProcess_nativeSpawn (JN Line 320  Java_java_lang_VMProcess_nativeSpawn (JN
320    method = (*env)->GetMethodID (env, clazz, "<init>", "(II)V");    method = (*env)->GetMethodID (env, clazz, "<init>", "(II)V");
321    if ((*env)->ExceptionOccurred (env))    if ((*env)->ExceptionOccurred (env))
322      goto done;      goto done;
323    for (i = 0; i < 3; i++)    for (i = 0; i < pipe_count; i++)
324      {      {
       /* Mode is WRITE (2) for in and READ (1) for out and err. */  
325        const int fd = fds[i][i == 0];        const int fd = fds[i][i == 0];
326        const int mode = (i == 0) ? 2 : 1;        const int mode = ((i == 0)
327                            ? gnu_java_nio_channels_FileChannelImpl_WRITE
328                            : gnu_java_nio_channels_FileChannelImpl_READ);
329        jclass sclazz;        jclass sclazz;
330        jmethodID smethod;        jmethodID smethod;
331    
# Line 320  Java_java_lang_VMProcess_nativeSpawn (JN Line 333  Java_java_lang_VMProcess_nativeSpawn (JN
333        if ((*env)->ExceptionOccurred (env))        if ((*env)->ExceptionOccurred (env))
334          goto done;          goto done;
335    
336        if (mode == 2)        if (mode == gnu_java_nio_channels_FileChannelImpl_WRITE)
337          sclazz = (*env)->FindClass (env, "java/io/FileOutputStream");          sclazz = (*env)->FindClass (env, "java/io/FileOutputStream");
338        else        else
339          sclazz = (*env)->FindClass (env, "java/io/FileInputStream");          sclazz = (*env)->FindClass (env, "java/io/FileInputStream");
# Line 360  done: Line 373  done:
373     */     */
374    
375    /* Close child's ends of pipes */    /* Close child's ends of pipes */
376    for (i = 0; i < 3; i++)    for (i = 0; i < pipe_count; i++)
377      {      {
378        const int fd = fds[i][i != 0];        const int fd = fds[i][i != 0];
379    
# Line 374  done: Line 387  done:
387     * was created for a file descriptor, we don't close it because it     * was created for a file descriptor, we don't close it because it
388     * will get closed when the Stream object is finalized.     * will get closed when the Stream object is finalized.
389     */     */
390    for (i = 0; i < 3; i++)    for (i = 0; i < pipe_count; i++)
391      {      {
392        const int fd = fds[i][i == 0];        const int fd = fds[i][i == 0];
393    

Legend:
Removed from v.1.2.2.3  
changed lines
  Added in v.1.2.2.4

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26