public inbox for frysk-cvs@sourceware.org
help / color / mirror / Atom feed
* [SCM]  master: Eliminate constants in Fork.java; use malloc, not alloca.
@ 2008-05-10 16:12 cagney
  0 siblings, 0 replies; only message in thread
From: cagney @ 2008-05-10 16:12 UTC (permalink / raw)
  To: frysk-cvs

The branch, master has been updated
       via  d76e0603fee48fc72eca9b45c3ccb6fc781cbffa (commit)
      from  88e037617835894d075066f6abfdb41fd7175eb6 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email.

- Log -----------------------------------------------------------------
commit d76e0603fee48fc72eca9b45c3ccb6fc781cbffa
Author: Andrew Cagney <cagney@redhat.com>
Date:   Sat May 10 08:05:09 2008 -0400

    Eliminate constants in Fork.java; use malloc, not alloca.
    
    frysk-core/frysk/testbed/ChangeLog
    2008-05-09  Andrew Cagney  <cagney@redhat.com>
    
    	* OffspringType.java: Pass File to Fork.daemon.
    
    frysk-sys/frysk/sys/ChangeLog
    2008-05-10  Andrew Cagney  <cagney@redhat.com>
    
    	* Fork.java (NO_TRACE, PTRACE, UTRACE): Delete.
    	(daemon(File,String,String,String,String[],String[])): New.
    	(spawn(File,String,String,String,String[],String[])): New.
    	(ptrace(File,String,String,String,String[],String[])): New.
    	(utrace(File,String,String,String,String[],String[])): New.
    	(spawn(File,String,String,String,String[],long,int)): Delete.
    	(spawn(String[],int)): Delete.
    	* cni/Fork.cxx: Update.
    	* cni/Errno.hxx (MALLOC_STRING, MALLOC_ARGV): Define.

-----------------------------------------------------------------------

Summary of changes:
 frysk-core/frysk/testbed/ChangeLog          |    4 +
 frysk-core/frysk/testbed/OffspringType.java |    5 +-
 frysk-sys/frysk/sys/ChangeLog               |   12 ++
 frysk-sys/frysk/sys/Fork.java               |   67 +++++-----
 frysk-sys/frysk/sys/cni/Errno.hxx           |    2 +
 frysk-sys/frysk/sys/cni/Fork.cxx            |  195 ++++++++++++++++-----------
 6 files changed, 176 insertions(+), 109 deletions(-)

First 500 lines of diff:
diff --git a/frysk-core/frysk/testbed/ChangeLog b/frysk-core/frysk/testbed/ChangeLog
index e34a170..68af896 100644
--- a/frysk-core/frysk/testbed/ChangeLog
+++ b/frysk-core/frysk/testbed/ChangeLog
@@ -1,3 +1,7 @@
+2008-05-09  Andrew Cagney  <cagney@redhat.com>
+
+	* OffspringType.java: Pass File to Fork.daemon.
+
 2008-04-17  Andrew Cagney  <cagney@redhat.com>
 
 	* FunitThreadsOffspring.java: Use frysk.config.Prefix.
diff --git a/frysk-core/frysk/testbed/OffspringType.java b/frysk-core/frysk/testbed/OffspringType.java
index 832de16..23739f5 100644
--- a/frysk-core/frysk/testbed/OffspringType.java
+++ b/frysk-core/frysk/testbed/OffspringType.java
@@ -39,6 +39,7 @@
 
 package frysk.testbed;
 
+import java.io.File;
 import frysk.proc.TaskAttachedObserverXXX;
 import frysk.sys.Fork;
 import frysk.proc.Manager;
@@ -61,7 +62,9 @@ public abstract class OffspringType {
 						    String stdout,
 						    String stderr,
 						    String[] argv) {
-		return Fork.daemon(stdin, stdout, stderr, argv);
+		return Fork.daemon(new File(argv[0]),
+				   stdin, stdout, stderr,
+				   argv);
 	    }
 	};
     /**
diff --git a/frysk-sys/frysk/sys/ChangeLog b/frysk-sys/frysk/sys/ChangeLog
index a8cd1cd..01f3ea6 100644
--- a/frysk-sys/frysk/sys/ChangeLog
+++ b/frysk-sys/frysk/sys/ChangeLog
@@ -1,3 +1,15 @@
+2008-05-10  Andrew Cagney  <cagney@redhat.com>
+
+	* Fork.java (NO_TRACE, PTRACE, UTRACE): Delete.
+	(daemon(File,String,String,String,String[],String[])): New.
+	(spawn(File,String,String,String,String[],String[])): New.
+	(ptrace(File,String,String,String,String[],String[])): New.
+	(utrace(File,String,String,String,String[],String[])): New.
+	(spawn(File,String,String,String,String[],long,int)): Delete.
+	(spawn(String[],int)): Delete.
+	* cni/Fork.cxx: Update.
+	* cni/Errno.hxx (MALLOC_STRING, MALLOC_ARGV): Define.
+	
 2008-05-09  Andrew Cagney  <cagney@redhat.com>
 
 	* jni/PseudoTerminal.cxx: Use String's JNI methods.
diff --git a/frysk-sys/frysk/sys/Fork.java b/frysk-sys/frysk/sys/Fork.java
index 5a74504..99dc9a2 100644
--- a/frysk-sys/frysk/sys/Fork.java
+++ b/frysk-sys/frysk/sys/Fork.java
@@ -47,17 +47,18 @@ import java.io.File;
  */
 
 public final class Fork {
-    static final int NO_TRACE = 0;
-    static final int UTRACE = 1;
-    static final int PTRACE = 2;
-    private static native ProcessIdentifier spawn(File exe,
-						  String in, String out,
-						  String err,
-						  String[] args, 
-						  long environ, int trace);
-    private static ProcessIdentifier spawn(String[] args, int trace) {
-	return spawn(new File(args[0]), null, null, null, args, 0, trace);
-    }
+    private static native int spawn(File exe,
+				    String in, String out, String err,
+				    String[] args, long environ);
+    private static native int ptrace(File exe,
+				     String in, String out, String err,
+				     String[] args, long environ);
+    private static native int utrace(File exe,
+				     String in, String out, String err,
+				     String[] args, long environ);
+    private static native int daemon(File exe,
+				     String in, String out, String err,
+				     String[] args, long environ);
 
     /**
      * Create a child process running EXE with arguments ARGS[0..].
@@ -67,7 +68,8 @@ public final class Fork {
     public static ProcessIdentifier exec(File exe,
 					 String in, String out,
 					 String err, String[] args) {
-	return spawn(exe, in, out, err, args, 0, NO_TRACE);
+	return ProcessIdentifierFactory.create
+	    (spawn(exe, in, out, err, args, 0));
     }
     /**
      * Create a child process running EXE with arguments ARGS[0..].
@@ -76,14 +78,16 @@ public final class Fork {
      */
     public static ProcessIdentifier exec(String in, String out,
 					 String err, String[] args) {
-	return spawn(new File(args[0]), in, out, err, args, 0, NO_TRACE);
+	return ProcessIdentifierFactory.create
+	    (spawn(new File(args[0]), in, out, err, args, 0));
     }
     /**
      * Create a child process running ARGS[0] with arguments
      * ARGS[0..].
      */
     public static ProcessIdentifier exec(String[] args) {
-	return spawn(args, NO_TRACE);
+	return ProcessIdentifierFactory.create
+	    (spawn(new File(args[0]), null, null, null, args, 0));
     }
 
     /**
@@ -93,19 +97,22 @@ public final class Fork {
      * Also wire up IN, OUT, and ERR.
      */
     public static ProcessIdentifier ptrace(File exe,
-					   String in, String out, 
-					   String err, String[] args, String libs) {
+					   String in, String out, String err,
+					   String[] args,
+					   String libs) {
 	Environ environ = new Environ();
 	environ.setEnv("LD_LIBRARY_PATH", libs);
 	long env = environ.putEnviron();
-	return spawn(exe, in, out, err, args, env, PTRACE);
+	return ProcessIdentifierFactory.create
+	    (ptrace(exe, in, out, err, args, env));
     }
     /**
      * Create a child process running ARGS[0] with arguments
      * ARGS[0...]; mark the process for tracing.
      */
     public static ProcessIdentifier ptrace(String[] args) {
-	return spawn(args, PTRACE);
+	return ProcessIdentifierFactory.create
+	    (ptrace(new File(args[0]), null, null, null, args, 0));
     }
 
     /**
@@ -117,38 +124,34 @@ public final class Fork {
     public static ProcessIdentifier utrace(File exe,
 					   String in, String out,
 					   String err, String[] args) {
-	return spawn(exe, in, out, err, args, 0, UTRACE);
+	return ProcessIdentifierFactory.create
+	    (utrace(exe, in, out, err, args, 0));
     }
     /**
      * Create a child process running ARGS[0] with arguments
      * ARGV[0...]; mark the process for utracing.
      */
     public static ProcessIdentifier utrace(String[] args) {
-	return spawn(args, UTRACE);
+	return ProcessIdentifierFactory.create
+	    (utrace(new File(args[0]), null, null, null, args, 0));
     }
 
     /**
      * Create a "daemon" process running ARGV[0] with arguments
      * ARGV[1...]; a daemon has process ID 1 as its parent.
-     *
-     * Also wire up IN, OUT, and ERR.
-     */
-    public static native ProcessIdentifier daemon(File exe,
-						  String in, String out,
-						  String err, String[] argv);
-    /**
-     * Create a "daemon" process running ARGV[0] with arguments
-     * ARGV[1...]; a daemon has process ID 1 as its parent.
      */
-    public static ProcessIdentifier daemon(String in, String out, String err,
+    public static ProcessIdentifier daemon(File exe,
+					   String in, String out, String err,
 					   String[] argv) {
-	return daemon(new File(argv[0]), in, out, err, argv);
+	return ProcessIdentifierFactory.create
+	    (daemon(exe, in, out, err, argv, 0));
     }
     /**
      * Create a "daemon" process running ARGV[0] with arguments
      * ARGV[1...]; a daemon has process ID 1 as its parent.
      */
     public static ProcessIdentifier daemon(String[] argv) {
-	return daemon(new File(argv[0]), null, null, null, argv);
+	return ProcessIdentifierFactory.create
+	    (daemon(new File(argv[0]), null, null, null, argv, 0));
     }
 }
diff --git a/frysk-sys/frysk/sys/cni/Errno.hxx b/frysk-sys/frysk/sys/cni/Errno.hxx
index 6631668..6bcde43 100644
--- a/frysk-sys/frysk/sys/cni/Errno.hxx
+++ b/frysk-sys/frysk/sys/cni/Errno.hxx
@@ -74,12 +74,14 @@ extern int tryOpen (const char *file, int flags, int mode);
 extern size_t sizeof_argv (jstringArray argv);
 extern char** fill_argv (void* p, jstringArray argv);
 #define ALLOCA_ARGV(ARGV) (fill_argv (alloca (sizeof_argv (ARGV)), (ARGV)))
+#define MALLOC_ARGV(ARGV) (fill_argv (JvMalloc (sizeof_argv (ARGV)), (ARGV)))
 /**
  * Convert S, a String, into a C char* alocated on the stack.
  */
 extern size_t sizeof_string (jstring s);
 extern char* fill_string (void* p, jstring s);
 #define ALLOCA_STRING(S) (fill_string (alloca (sizeof_string (S)), (S)))
+#define MALLOC_STRING(S) (fill_string (JvMalloc (sizeof_string (S)), (S)))
 
 /**
  * Throw an ArrayIndexOutOfBounds exception if START and LENGTH do not
diff --git a/frysk-sys/frysk/sys/cni/Fork.cxx b/frysk-sys/frysk/sys/cni/Fork.cxx
index c2f36dc..74db54c 100644
--- a/frysk-sys/frysk/sys/cni/Fork.cxx
+++ b/frysk-sys/frysk/sys/cni/Fork.cxx
@@ -38,7 +38,6 @@
 // exception.
 
 #include <stdio.h>
-#include <alloca.h>
 #include <errno.h>
 #include <unistd.h>
 #include <sys/ptrace.h>
@@ -54,46 +53,71 @@
 #include "frysk/sys/ProcessIdentifier.h"
 #include "frysk/sys/ProcessIdentifierFactory.h"
 
+enum tracing {
+  DAEMON,
+  NO_TRACE,
+  PTRACE,
+  UTRACE,
+};
+
 static void
-reopen (jstring file, const char *mode, FILE *stream)
-{
+reopen(const char* file, const char* mode, FILE *stream) {
   if (file == NULL)
     return;
-  int len = JvGetStringUTFLength (file);
-  char *fileName = (char *) alloca (len + 1);
-  JvGetStringUTFRegion (file, 0, file->length (), fileName);
-  fileName[len] = '\0';
   errno = 0;
-  ::freopen (fileName, mode, stream);
+  ::freopen(file, mode, stream);
   if (errno != 0) {
-    ::perror ("freopen");
-    ::_exit (errno);
+    // Should not happen!
+    ::perror("freopen");
+    ::_exit(errno);
   }
 }
 
-int
-spawn(java::io::File* exe, jstring in, jstring out, jstring err,
-      jstringArray args, jlong environ, jint trace)
-{
-  // Convert args into argv, argc, filename.
-  char *filename = ALLOCA_STRING(exe->getPath());
-  int argc = JvGetArrayLength (args);
-  char **argv = (char **) alloca ((argc + 1) * sizeof (void*));
-  for (int i = 0; i < argc; i++) {
-    jstring arg = elements (args)[i];
-    int len = JvGetStringUTFLength (arg);
-    argv[i] = (char *) alloca (len + 1);
-    JvGetStringUTFRegion (arg, 0, arg->length (), argv[i]);
-    argv[i][len] = '\0';
+/**
+ * Spawn a child, return the PID or the -ERROR.
+ */
+static int
+spawn(const char* exePath,
+      const char* inPath, const char* outPath, const char* errPath,
+      int argc, char** argv, char** environ, tracing trace) {
+
+  if (trace == DAEMON) {
+    // Do a vfork(), fork(), exec() which lets the top level process
+    // capture the middle level fork()'s return value in a volatile.
+    volatile int pid = -1;
+    register int v;
+    errno = 0;
+    v = vfork ();
+    switch (v) {
+    case 0:
+      // This is executed by the child with the parent blocked, the
+      // final process id ends up in PID.
+      pid = ::spawn(exePath, inPath, outPath, errPath, argc, argv, 0, NO_TRACE);
+      _exit (0);
+    case -1:
+      // This is executed after a vfork barfs.
+      return -errno;
+    default:
+      // This is executed after the child has set PID with a FORK and
+      // then exited (which helps guarentee that the waitpid, below,
+      // doesn't block.
+      if (pid < 0)
+	return -errno;
+      // Consume the middle players wait.
+      int status;
+      errno = 0;
+      if (waitpid (v, &status, 0) < 0)
+	return -errno;
+      return pid;
+    }
   }
-  argv[argc] = 0;
 
   // Fork/exec
   errno = 0;
   pid_t pid = fork ();
   switch (pid) {
   case -1: // Fork failed.
-    throwErrno (errno, "fork");
+    return -errno;
   default: // Parent
     return pid;
   case 0: // Child
@@ -102,75 +126,94 @@ spawn(java::io::File* exe, jstring in, jstring out, jstring err,
     sigfillset(&mask);
     ::sigprocmask(SIG_UNBLOCK, &mask, NULL);
     // Redirect stdio.
-    reopen (in, "r", stdin);
-    reopen (out, "w", stdout);
-    reopen (err, "w", stderr);
+    reopen(inPath, "r", stdin);
+    reopen(outPath, "w", stdout);
+    reopen(errPath, "w", stderr);
     switch (trace) {
-    case frysk::sys::Fork::PTRACE:
+    case PTRACE:
       errno = 0;
-      ::ptrace ((enum __ptrace_request) PTRACE_TRACEME, 0, 0, 0);
+      ::ptrace((enum __ptrace_request) PTRACE_TRACEME, 0, 0, 0);
       if (errno != 0) {
 	::perror ("ptrace.traceme");
-	::_exit (errno);
+	::_exit(errno);
       }
       break;
-    case frysk::sys::Fork::UTRACE:
+    case UTRACE:
       fprintf(stderr, "\n\n>>>>> in spawn(...utrace)\n\n");
       break;
-    case frysk::sys::Fork::NO_TRACE:
+    case NO_TRACE:
+      break;
+    case DAEMON:
       break;
     }
-     if (environ != 0) {
-	::execve (filename, argv, (char **)environ);
-       }
-     else
-      ::execv (filename, argv);
+    if (environ != NULL) {
+      ::execve(exePath, argv, environ);
+    } else
+      ::execv(exePath, argv);
     // This should not happen.
-    ::perror ("execvp");
+    ::perror("execvp");
     ::_exit (errno);
   }
 }
 
-frysk::sys::ProcessIdentifier*
+/**
+ * Convert convert to native and then spawn.
+ */
+static int
+spawn(java::io::File* exe,
+      jstring in, jstring out, jstring err,
+      jstringArray args, jlong environ, tracing trace) {
+  char* exePath = MALLOC_STRING(exe->getPath());
+  char* inPath = MALLOC_STRING(in);
+  char* outPath = MALLOC_STRING(out);
+  char* errPath = MALLOC_STRING(err);
+  int argc = args->length;
+  char** argv = MALLOC_ARGV(args);
+  int pid = ::spawn(exePath, inPath, outPath, errPath,
+		    argc, argv, (char**)environ, trace);
+  JvFree(exePath);
+  JvFree(inPath);
+  JvFree(outPath);
+  JvFree(errPath);
+  JvFree(argv);
+  if (pid < 0) {
+    switch (trace) {
+    case NO_TRACE:
+      throwErrno(-pid, "fork/exec");
+    case DAEMON:
+      throwErrno(-pid, "vfork/wait");
+    case PTRACE:
+      throwErrno(-pid, "fork/ptrace/exec");
+    case UTRACE:
+      throwErrno(-pid, "utrace");
+    }
+  }
+  return pid;
+}
+
+jint
 frysk::sys::Fork::spawn(java::io::File* exe,
 			jstring in, jstring out, jstring err,
-			jstringArray args, jlong environ, jint trace) {
-  int pid = ::spawn(exe, in, out, err, args, environ, trace);
-  return frysk::sys::ProcessIdentifierFactory::create(pid);
+			jstringArray args, jlong environ) {
+  return ::spawn(exe, in, out, err, args, environ, NO_TRACE);
 }
 
-frysk::sys::ProcessIdentifier*
-frysk::sys::Fork::daemon (java::io::File* exe, jstring in, jstring out,
-			  jstring err, jstringArray args)
-{
-  volatile int pid = -1;
-  register int v;
-  errno = 0;
-  v = vfork ();
-
-  // This is executed by the child with the parent blocked, the final
-  // process id ends up in PID.
-
-  if (v == 0) {
-    pid = ::spawn(exe, in, out, err, args, 0, frysk::sys::Fork::NO_TRACE);
-    _exit (0);
-  }
-
-  // This is executed after the child has exited (which helps
-  // guarentee that the waitpid, below, doesn't block.
-
-  if (v < 0)
-    throwErrno (errno, "vfork");
-  if (pid < 0)
-    throwErrno (errno, "fork");
-  
-  // Consume the middle players wait.
-  int status;
-  errno = 0;
-  if (waitpid (v, &status, 0) < 0)
-    throwErrno (errno, "waitpid", "process %d", v);
+jint
+frysk::sys::Fork::ptrace(java::io::File* exe,
+			jstring in, jstring out, jstring err,
+			jstringArray args, jlong environ) {
+  return ::spawn(exe, in, out, err, args, environ, PTRACE);
+}
 
-  // printf ("v %d pid %d\n", v, pid);
+jint
+frysk::sys::Fork::utrace(java::io::File* exe,
+			jstring in, jstring out, jstring err,
+			jstringArray args, jlong environ) {
+  return ::spawn(exe, in, out, err, args, environ, UTRACE);
+}
 
-  return frysk::sys::ProcessIdentifierFactory::create(pid);
+jint
+frysk::sys::Fork::daemon (java::io::File* exe, jstring in, jstring out,
+			  jstring err, jstringArray args, jlong environ) {
+  return ::spawn(exe, in, out, err, args, environ, DAEMON);
 }


hooks/post-receive
--
frysk system monitor/debugger


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2008-05-10 16:12 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-05-10 16:12 [SCM] master: Eliminate constants in Fork.java; use malloc, not alloca cagney

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).