1999-05-12 19:23:44 +08:00
|
|
|
// natPosixProcess.cc - Native side of POSIX process code.
|
|
|
|
|
2000-09-07 02:14:15 +08:00
|
|
|
/* Copyright (C) 1998, 1999, 2000 Free Software Foundation
|
1999-05-12 19:23:44 +08:00
|
|
|
|
|
|
|
This file is part of libgcj.
|
|
|
|
|
|
|
|
This software is copyrighted work licensed under the terms of the
|
|
|
|
Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
1999-06-10 01:42:26 +08:00
|
|
|
#include <stdio.h>
|
1999-05-12 19:23:44 +08:00
|
|
|
|
1999-09-11 06:03:10 +08:00
|
|
|
#include <gcj/cni.h>
|
1999-05-12 19:23:44 +08:00
|
|
|
#include <jvm.h>
|
|
|
|
|
|
|
|
#include <java/lang/ConcreteProcess.h>
|
|
|
|
#include <java/lang/IllegalThreadStateException.h>
|
|
|
|
#include <java/lang/InterruptedException.h>
|
|
|
|
#include <java/lang/NullPointerException.h>
|
|
|
|
#include <java/lang/Thread.h>
|
|
|
|
#include <java/io/FileDescriptor.h>
|
|
|
|
#include <java/io/FileInputStream.h>
|
|
|
|
#include <java/io/FileOutputStream.h>
|
|
|
|
#include <java/io/IOException.h>
|
2000-09-07 02:14:15 +08:00
|
|
|
#include <java/lang/OutOfMemoryError.h>
|
1999-05-12 19:23:44 +08:00
|
|
|
|
|
|
|
extern char **environ;
|
|
|
|
|
|
|
|
void
|
|
|
|
java::lang::ConcreteProcess::destroy (void)
|
|
|
|
{
|
|
|
|
if (! hasExited)
|
|
|
|
{
|
|
|
|
// Really kill it.
|
|
|
|
kill ((pid_t) pid, SIGKILL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
jint
|
|
|
|
java::lang::ConcreteProcess::exitValue (void)
|
|
|
|
{
|
|
|
|
if (! hasExited)
|
|
|
|
{
|
|
|
|
int wstat;
|
|
|
|
pid_t r = waitpid ((pid_t) pid, &wstat, WNOHANG);
|
|
|
|
if (r == -1)
|
|
|
|
{
|
|
|
|
jstring x = JvNewStringLatin1 (strerror (errno));
|
exception.cc (java_eh_info): Make value type jthrowable.
* exception.cc (java_eh_info): Make value type jthrowable.
(_Jv_type_matcher): Remove now unneeded cast.
(_Jv_Throw): Make argument type jthrowable. Munge name
for SJLJ_EXCEPTIONS here ...
* gcj/cni.h: ... not here.
(JvThrow): Remove.
* gcj/javaprims.h (_Jv_Throw, _Jv_Sjlj_Throw): Update declarations.
* defineclass.cc, interpret.cc, jni.cc, posix-threads.cc,
prims.cc, resolve.cc, gnu/gcj/runtime/natFirstThread.cc,
gnu/gcj/xlib/natDrawable.cc, gnu/gcj/xlib/natFont.cc,
gnu/gcj/xlib/natWMSizeHints.cc, gnu/gcj/xlib/natWindowAttributes.cc,
gnu/gcj/xlib/natXImage.cc, java/io/natFile.cc,
java/io/natFileDescriptorEcos.cc, java/io/natFileDescriptorPosix.cc,
java/io/natFileDescriptorWin32.cc, java/io/natFileWin32.cc,
java/lang/natClass.cc, java/lang/natClassLoader.cc,
java/lang/natDouble.cc, java/lang/natObject.cc,
java/lang/natPosixProcess.cc, java/lang/natRuntime.cc,
java/lang/natString.cc, java/lang/natSystem.cc,
java/lang/natThread.cc, java/lang/reflect/natArray.cc,
java/lang/reflect/natConstructor.cc, java/lang/reflect/natField.cc,
java/lang/reflect/natMethod.cc, java/util/zip/natDeflater.cc,
java/util/zip/natInflater.cc:
Use throw, not JvThrow or _Jv_Throw.
From-SVN: r40838
2001-03-26 15:05:32 +08:00
|
|
|
throw new IllegalThreadStateException (x);
|
1999-05-12 19:23:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
hasExited = true;
|
|
|
|
// Just use the raw status. FIXME: what is right?
|
|
|
|
status = wstat;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
jint
|
|
|
|
java::lang::ConcreteProcess::waitFor (void)
|
|
|
|
{
|
|
|
|
if (! hasExited)
|
|
|
|
{
|
|
|
|
int wstat;
|
|
|
|
int r = waitpid ((pid_t) pid, &wstat, 0);
|
|
|
|
|
|
|
|
if (r != -1)
|
|
|
|
{
|
|
|
|
hasExited = true;
|
|
|
|
// Just use the raw status. FIXME: what is right?
|
|
|
|
status = wstat;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (java::lang::Thread::interrupted())
|
exception.cc (java_eh_info): Make value type jthrowable.
* exception.cc (java_eh_info): Make value type jthrowable.
(_Jv_type_matcher): Remove now unneeded cast.
(_Jv_Throw): Make argument type jthrowable. Munge name
for SJLJ_EXCEPTIONS here ...
* gcj/cni.h: ... not here.
(JvThrow): Remove.
* gcj/javaprims.h (_Jv_Throw, _Jv_Sjlj_Throw): Update declarations.
* defineclass.cc, interpret.cc, jni.cc, posix-threads.cc,
prims.cc, resolve.cc, gnu/gcj/runtime/natFirstThread.cc,
gnu/gcj/xlib/natDrawable.cc, gnu/gcj/xlib/natFont.cc,
gnu/gcj/xlib/natWMSizeHints.cc, gnu/gcj/xlib/natWindowAttributes.cc,
gnu/gcj/xlib/natXImage.cc, java/io/natFile.cc,
java/io/natFileDescriptorEcos.cc, java/io/natFileDescriptorPosix.cc,
java/io/natFileDescriptorWin32.cc, java/io/natFileWin32.cc,
java/lang/natClass.cc, java/lang/natClassLoader.cc,
java/lang/natDouble.cc, java/lang/natObject.cc,
java/lang/natPosixProcess.cc, java/lang/natRuntime.cc,
java/lang/natString.cc, java/lang/natSystem.cc,
java/lang/natThread.cc, java/lang/reflect/natArray.cc,
java/lang/reflect/natConstructor.cc, java/lang/reflect/natField.cc,
java/lang/reflect/natMethod.cc, java/util/zip/natDeflater.cc,
java/util/zip/natInflater.cc:
Use throw, not JvThrow or _Jv_Throw.
From-SVN: r40838
2001-03-26 15:05:32 +08:00
|
|
|
throw new InterruptedException (JvNewStringLatin1 ("wait interrupted"));
|
1999-05-12 19:23:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
new_string (jstring string)
|
|
|
|
{
|
|
|
|
jsize s = _Jv_GetStringUTFLength (string);
|
|
|
|
char *buf = (char *) _Jv_Malloc (s + 1);
|
|
|
|
_Jv_GetStringUTFRegion (string, 0, s, buf);
|
|
|
|
buf[s] = '\0';
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2000-09-07 02:14:15 +08:00
|
|
|
static void
|
|
|
|
cleanup (char **args, char **env)
|
|
|
|
{
|
|
|
|
if (args != NULL)
|
|
|
|
{
|
|
|
|
for (int i = 0; args[i] != NULL; ++i)
|
|
|
|
_Jv_Free (args[i]);
|
|
|
|
_Jv_Free (args);
|
|
|
|
}
|
|
|
|
if (env != NULL)
|
|
|
|
{
|
|
|
|
for (int i = 0; env[i] != NULL; ++i)
|
|
|
|
_Jv_Free (env[i]);
|
|
|
|
_Jv_Free (env);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fail (int error_value, char **args, char **env,
|
|
|
|
int *one = NULL, int *two = NULL,
|
|
|
|
int *three = NULL, int *four = NULL,
|
|
|
|
java::lang::Throwable *t = NULL)
|
|
|
|
{
|
|
|
|
cleanup (args, env);
|
|
|
|
if (one != NULL)
|
|
|
|
{
|
|
|
|
close (one[0]);
|
|
|
|
close (one[1]);
|
|
|
|
}
|
|
|
|
if (two != NULL)
|
|
|
|
{
|
|
|
|
close (two[0]);
|
|
|
|
close (two[1]);
|
|
|
|
}
|
|
|
|
if (three != NULL)
|
|
|
|
{
|
|
|
|
close (three[0]);
|
|
|
|
close (three[1]);
|
|
|
|
}
|
|
|
|
if (four != NULL)
|
|
|
|
{
|
|
|
|
close (four[0]);
|
|
|
|
close (four[1]);
|
|
|
|
}
|
|
|
|
if (t == NULL)
|
|
|
|
t = new java::io::IOException (JvNewStringLatin1 (strerror (error_value)));
|
|
|
|
throw t;
|
|
|
|
}
|
|
|
|
|
1999-05-12 19:23:44 +08:00
|
|
|
void
|
|
|
|
java::lang::ConcreteProcess::startProcess (jstringArray progarray,
|
|
|
|
jstringArray envp)
|
|
|
|
{
|
|
|
|
using namespace java::io;
|
|
|
|
|
|
|
|
hasExited = false;
|
|
|
|
|
|
|
|
if (! progarray)
|
2000-09-07 02:14:15 +08:00
|
|
|
throw new NullPointerException;
|
1999-05-12 19:23:44 +08:00
|
|
|
|
|
|
|
// Transform arrays to native form.
|
|
|
|
char **args = (char **) _Jv_Malloc ((progarray->length + 1)
|
|
|
|
* sizeof (char *));
|
|
|
|
char **env = NULL;
|
|
|
|
|
2000-09-07 02:14:15 +08:00
|
|
|
// Initialize so we can gracefully recover.
|
1999-05-12 19:23:44 +08:00
|
|
|
jstring *elts = elements (progarray);
|
2000-09-07 02:14:15 +08:00
|
|
|
for (int i = 0; i <= progarray->length; ++i)
|
|
|
|
args[i] = NULL;
|
1999-05-12 19:23:44 +08:00
|
|
|
|
2000-09-07 02:14:15 +08:00
|
|
|
try
|
1999-05-12 19:23:44 +08:00
|
|
|
{
|
2000-09-07 02:14:15 +08:00
|
|
|
for (int i = 0; i < progarray->length; ++i)
|
|
|
|
args[i] = new_string (elts[i]);
|
|
|
|
args[progarray->length] = NULL;
|
|
|
|
|
|
|
|
if (envp)
|
|
|
|
{
|
|
|
|
env = (char **) _Jv_Malloc ((envp->length + 1) * sizeof (char *));
|
|
|
|
elts = elements (envp);
|
1999-05-12 19:23:44 +08:00
|
|
|
|
2000-09-07 02:14:15 +08:00
|
|
|
// Initialize so we can gracefully recover.
|
|
|
|
for (int i = 0; i <= envp->length; ++i)
|
|
|
|
env[i] = NULL;
|
1999-05-12 19:23:44 +08:00
|
|
|
|
2000-09-07 02:14:15 +08:00
|
|
|
for (int i = 0; i < envp->length; ++i)
|
|
|
|
env[i] = new_string (elts[i]);
|
|
|
|
env[envp->length] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (java::lang::OutOfMemoryError *oome)
|
1999-05-12 19:23:44 +08:00
|
|
|
{
|
2000-09-07 02:14:15 +08:00
|
|
|
fail (0, args, env, NULL, NULL, NULL, NULL, oome);
|
|
|
|
throw oome;
|
1999-05-12 19:23:44 +08:00
|
|
|
}
|
|
|
|
|
2000-09-07 02:14:15 +08:00
|
|
|
// Create pipes for I/O. MSGP is for communicating exec() status.
|
|
|
|
int inp[2], outp[2], errp[2], msgp[2];
|
|
|
|
|
|
|
|
if (pipe (inp))
|
|
|
|
fail (errno, args, env);
|
|
|
|
if (pipe (outp))
|
|
|
|
fail (errno, args, env, inp);
|
|
|
|
if (pipe (errp))
|
|
|
|
fail (errno, args, env, inp, outp);
|
|
|
|
if (pipe (msgp))
|
|
|
|
fail (errno, args, env, inp, outp, errp);
|
|
|
|
if (fcntl (msgp[1], F_SETFD, FD_CLOEXEC))
|
|
|
|
fail (errno, args, env, inp, outp, errp, msgp);
|
|
|
|
|
1999-05-12 19:23:44 +08:00
|
|
|
// We create the streams before forking. Otherwise if we had an
|
|
|
|
// error while creating the streams we would have run the child with
|
|
|
|
// no way to communicate with it.
|
2000-09-07 02:14:15 +08:00
|
|
|
try
|
|
|
|
{
|
|
|
|
errorStream = new FileInputStream (new FileDescriptor (errp[0]));
|
|
|
|
inputStream = new FileInputStream (new FileDescriptor (inp[0]));
|
|
|
|
outputStream = new FileOutputStream (new FileDescriptor (outp[1]));
|
|
|
|
}
|
|
|
|
catch (java::lang::Throwable *t)
|
|
|
|
{
|
|
|
|
fail (0, args, env, inp, outp, errp, msgp, t);
|
|
|
|
}
|
1999-05-12 19:23:44 +08:00
|
|
|
|
|
|
|
// We don't use vfork() because that would cause the local
|
|
|
|
// environment to be set by the child.
|
|
|
|
if ((pid = (jlong) fork ()) == -1)
|
2000-09-07 02:14:15 +08:00
|
|
|
fail (errno, args, env, inp, outp, errp, msgp);
|
1999-05-12 19:23:44 +08:00
|
|
|
|
|
|
|
if (pid == 0)
|
|
|
|
{
|
|
|
|
// Child process, so remap descriptors and exec.
|
|
|
|
|
|
|
|
if (envp)
|
1999-06-10 01:42:26 +08:00
|
|
|
{
|
|
|
|
// preserve PATH unless specified explicitly
|
1999-12-18 00:06:17 +08:00
|
|
|
char *path_val = getenv ("PATH");
|
1999-06-10 01:42:26 +08:00
|
|
|
environ = env;
|
1999-12-18 00:06:17 +08:00
|
|
|
if (getenv ("PATH") == NULL)
|
1999-06-10 01:42:26 +08:00
|
|
|
{
|
1999-12-18 00:06:17 +08:00
|
|
|
char *path_env = (char *) _Jv_Malloc (strlen (path_val) + 5 + 1);
|
|
|
|
strcpy (path_env, "PATH=");
|
|
|
|
strcat (path_env, path_val);
|
1999-06-10 01:42:26 +08:00
|
|
|
putenv (path_env);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-05-12 19:23:44 +08:00
|
|
|
// We ignore errors from dup2 because they should never occur.
|
|
|
|
dup2 (outp[0], 0);
|
|
|
|
dup2 (inp[1], 1);
|
|
|
|
dup2 (errp[1], 2);
|
|
|
|
|
|
|
|
close (inp[0]);
|
|
|
|
close (inp[1]);
|
|
|
|
close (errp[0]);
|
|
|
|
close (errp[1]);
|
|
|
|
close (outp[0]);
|
|
|
|
close (outp[1]);
|
2000-09-07 02:14:15 +08:00
|
|
|
close (msgp[0]);
|
1999-05-12 19:23:44 +08:00
|
|
|
|
|
|
|
execvp (args[0], args);
|
2000-09-07 02:14:15 +08:00
|
|
|
|
|
|
|
// Send the parent notification that the exec failed.
|
|
|
|
char c = errno;
|
|
|
|
write (msgp[1], &c, 1);
|
1999-05-12 19:23:44 +08:00
|
|
|
_exit (127);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parent. Close extra file descriptors and mark ours as
|
|
|
|
// close-on-exec.
|
|
|
|
close (outp[0]);
|
|
|
|
close (inp[1]);
|
|
|
|
close (errp[1]);
|
2000-09-07 02:14:15 +08:00
|
|
|
close (msgp[1]);
|
|
|
|
|
|
|
|
char c;
|
|
|
|
int r = read (msgp[0], &c, 1);
|
|
|
|
if (r == -1)
|
|
|
|
fail (errno, args, env, inp, outp, errp, msgp);
|
|
|
|
else if (r != 0)
|
|
|
|
fail (c, args, env, inp, outp, errp, msgp);
|
|
|
|
|
|
|
|
close (msgp[0]);
|
|
|
|
cleanup (args, env);
|
1999-05-12 19:23:44 +08:00
|
|
|
|
|
|
|
fcntl (outp[1], F_SETFD, 1);
|
|
|
|
fcntl (inp[0], F_SETFD, 1);
|
|
|
|
fcntl (errp[0], F_SETFD, 1);
|
|
|
|
}
|