public inbox for frysk-cvs@sourceware.org
help / color / mirror / Atom feed
From: scox@sourceware.org
To: frysk-cvs@sourceware.org
Subject: [SCM]  master: Add support for process environment variables.
Date: Tue, 15 Apr 2008 02:23:00 -0000	[thread overview]
Message-ID: <20080415022321.29037.qmail@sourceware.org> (raw)

The branch, master has been updated
       via  679ae166ae85307bd174477621c2548a8a17bf1e (commit)
      from  2498dec90f5284a9fdcabb3eea44d569a8802975 (commit)

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

- Log -----------------------------------------------------------------
commit 679ae166ae85307bd174477621c2548a8a17bf1e
Author: Stan Cox <scox@redhat.com>
Date:   Mon Apr 14 22:20:28 2008 -0400

    Add support for process environment variables.
    
    * Environ.java: New file.
    * cni/Environ.cxx: New file.
    * Fork.java (spawn): Use Environ.
    * cni/Fork.cxx (spawn): Use Environ.

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

Summary of changes:
 frysk-sys/frysk/sys/ChangeLog       |    7 ++
 frysk-sys/frysk/sys/Environ.java    |  110 +++++++++++++++++++++++++++++++++++
 frysk-sys/frysk/sys/Fork.java       |   17 +++--
 frysk-sys/frysk/sys/cni/Environ.cxx |   73 +++++++++++++++++++++++
 frysk-sys/frysk/sys/cni/Fork.cxx    |   20 ++-----
 5 files changed, 206 insertions(+), 21 deletions(-)
 create mode 100644 frysk-sys/frysk/sys/Environ.java
 create mode 100644 frysk-sys/frysk/sys/cni/Environ.cxx

First 500 lines of diff:
diff --git a/frysk-sys/frysk/sys/ChangeLog b/frysk-sys/frysk/sys/ChangeLog
index efba00c..aa729bd 100644
--- a/frysk-sys/frysk/sys/ChangeLog
+++ b/frysk-sys/frysk/sys/ChangeLog
@@ -1,3 +1,10 @@
+2008-04-14  Stan Cox  <scox@redhat.com>
+
+	* Environ.java: New file.
+	* cni/Environ.cxx: New file.
+	* Fork.java (spawn): Use Environ.
+	* cni/Fork.cxx (spawn): Use Environ.
+
 2008-04-11  Andrew Cagney  <cagney@redhat.com>
 
 	* jni/AuditLibs.cxx: New.
diff --git a/frysk-sys/frysk/sys/Environ.java b/frysk-sys/frysk/sys/Environ.java
new file mode 100644
index 0000000..328eab1
--- /dev/null
+++ b/frysk-sys/frysk/sys/Environ.java
@@ -0,0 +1,110 @@
+// This file is part of the program FRYSK.
+//
+// Copyright 2007 Red Hat Inc.
+//
+// FRYSK is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// FRYSK is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// General Public License for more details.
+// 
+// You should have received a copy of the GNU General Public License
+// along with FRYSK; if not, write to the Free Software Foundation,
+// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+// 
+// In addition, as a special exception, Red Hat, Inc. gives You the
+// additional right to link the code of FRYSK with code not covered
+// under the GNU General Public License ("Non-GPL Code") and to
+// distribute linked combinations including the two, subject to the
+// limitations in this paragraph. Non-GPL Code permitted under this
+// exception must only link to the code of FRYSK through those well
+// defined interfaces identified in the file named EXCEPTION found in
+// the source code files (the "Approved Interfaces"). The files of
+// Non-GPL Code may instantiate templates or use macros or inline
+// functions from the Approved Interfaces without causing the
+// resulting work to be covered by the GNU General Public
+// License. Only Red Hat, Inc. may make changes or additions to the
+// list of Approved Interfaces. You must obey the GNU General Public
+// License in all respects for all of the FRYSK code and other code
+// used in conjunction with FRYSK except the Non-GPL Code covered by
+// this exception. If you modify this file, you may extend this
+// exception to your version of the file, but you are not obligated to
+// do so. If you do not wish to provide this exception without
+// modification, you must delete this exception statement from your
+// version and license this file solely under the GPL without
+// exception.
+
+package frysk.sys;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Set;
+
+/**
+ * Interface to the process environment.
+ */
+public class Environ
+{
+    private HashMap env;
+
+    public Environ ()
+    {
+	env = new HashMap();   
+	getEnvironment();
+    }
+    
+    /**
+     * Get an environment variable.
+     * @param name is the environment variable name.
+     * @return the value of the variable.
+     */
+    String getEnv(String name) {
+	return (String)env.get(name);
+    }
+    
+    /**
+     * Set the value of an environment variable.
+     * @param name is the environment variable name.
+     * @param value is the environment variable value.
+     */
+    void setEnv(String name, String value) {
+	env.put(name, value);
+    }
+
+    /**
+     * Used by CNI code to add an environment variable.
+     * @param name is the variable=value pair.
+     */
+    void addEnviron(String name) {
+	if (name.length() != 0) {
+	    String envMember [] = name.split("=");
+	    env.put(envMember[0], envMember.length == 2 ? envMember[1] : "");
+	}
+    }
+
+    /**
+     * Put the environment variables in env into char **environ form.
+     * @return the environ.
+     */
+    long putEnviron() {
+        Set keys = env.keySet();
+        Collection values= env.values();
+        Iterator valueIter = values.iterator();
+        Iterator keyIter= keys.iterator();
+        int idx = 0;
+        Object[] envs = new String[values.size()];
+
+        while (keyIter.hasNext()) {
+            envs[idx] = (String)keyIter.next() + "=" + valueIter.next();
+            idx += 1;
+        }
+        return putEnvironment(envs);
+    }
+
+    native void getEnvironment();
+    native long putEnvironment(Object[] envs);
+}
diff --git a/frysk-sys/frysk/sys/Fork.java b/frysk-sys/frysk/sys/Fork.java
index e82b506..5a74504 100644
--- a/frysk-sys/frysk/sys/Fork.java
+++ b/frysk-sys/frysk/sys/Fork.java
@@ -54,9 +54,9 @@ public final class Fork {
 						  String in, String out,
 						  String err,
 						  String[] args, 
-						  String libs, int trace);
+						  long environ, int trace);
     private static ProcessIdentifier spawn(String[] args, int trace) {
-	return spawn(new File(args[0]), null, null, null, args, "", trace);
+	return spawn(new File(args[0]), null, null, null, args, 0, trace);
     }
 
     /**
@@ -67,7 +67,7 @@ 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, "", NO_TRACE);
+	return spawn(exe, in, out, err, args, 0, NO_TRACE);
     }
     /**
      * Create a child process running EXE with arguments ARGS[0..].
@@ -76,7 +76,7 @@ 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, "", NO_TRACE);
+	return spawn(new File(args[0]), in, out, err, args, 0, NO_TRACE);
     }
     /**
      * Create a child process running ARGS[0] with arguments
@@ -93,9 +93,12 @@ public final class Fork {
      * Also wire up IN, OUT, and ERR.
      */
     public static ProcessIdentifier ptrace(File exe,
-					   String in, String out,
+					   String in, String out, 
 					   String err, String[] args, String libs) {
-	return spawn(exe, in, out, err, args, libs, PTRACE);
+	Environ environ = new Environ();
+	environ.setEnv("LD_LIBRARY_PATH", libs);
+	long env = environ.putEnviron();
+	return spawn(exe, in, out, err, args, env, PTRACE);
     }
     /**
      * Create a child process running ARGS[0] with arguments
@@ -114,7 +117,7 @@ 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, "", UTRACE);
+	return spawn(exe, in, out, err, args, 0, UTRACE);
     }
     /**
      * Create a child process running ARGS[0] with arguments
diff --git a/frysk-sys/frysk/sys/cni/Environ.cxx b/frysk-sys/frysk/sys/cni/Environ.cxx
new file mode 100644
index 0000000..9772dfd
--- /dev/null
+++ b/frysk-sys/frysk/sys/cni/Environ.cxx
@@ -0,0 +1,73 @@
+// This file is part of the program FRYSK.
+//
+// Copyright 2008, Red Hat Inc.
+//
+// FRYSK is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// FRYSK is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// General Public License for more details.
+// 
+// You should have received a copy of the GNU General Public License
+// along with FRYSK; if not, write to the Free Software Foundation,
+// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+// 
+// In addition, as a special exception, Red Hat, Inc. gives You the
+// additional right to link the code of FRYSK with code not covered
+// under the GNU General Public License ("Non-GPL Code") and to
+// distribute linked combinations including the two, subject to the
+// limitations in this paragraph. Non-GPL Code permitted under this
+// exception must only link to the code of FRYSK through those well
+// defined interfaces identified in the file named EXCEPTION found in
+// the source code files (the "Approved Interfaces"). The files of
+// Non-GPL Code may instantiate templates or use macros or inline
+// functions from the Approved Interfaces without causing the
+// resulting work to be covered by the GNU General Public
+// License. Only Red Hat, Inc. may make changes or additions to the
+// list of Approved Interfaces. You must obey the GNU General Public
+// License in all respects for all of the FRYSK code and other code
+// used in conjunction with FRYSK except the Non-GPL Code covered by
+// this exception. If you modify this file, you may extend this
+// exception to your version of the file, but you are not obligated to
+// do so. If you do not wish to provide this exception without
+// modification, you must delete this exception statement from your
+// version and license this file solely under the GPL without
+// exception.
+
+#include <gcj/cni.h>
+
+#include "frysk/sys/Environ.h"
+
+
+void
+frysk::sys::Environ::getEnvironment()
+{
+  extern char **environ;
+  int env_idx;
+
+  for (env_idx = 0; environ[env_idx]; env_idx++) 
+    frysk::sys::Environ::addEnviron (JvNewStringUTF (environ[env_idx]));
+}
+
+char **new_environ;
+
+jlong
+frysk::sys::Environ::putEnvironment(jobjectArray envs)
+{
+  jstring* env_member = (jstring*)elements(envs);
+  int envs_length = JvGetArrayLength(envs);
+  new_environ = (char**)JvMalloc(sizeof(void*) * envs_length);
+  for (int i = 0; i < envs_length; i++) 
+    {
+      int sym_len = env_member[i]->length ();
+      char *sym = (char*)JvMalloc(sym_len + 1);
+      JvGetStringUTFRegion (env_member[i], 0, sym_len, sym);
+      sym[sym_len] = '\0';
+      new_environ[i] = sym;
+    }
+  new_environ[envs_length] = NULL;
+  return (jlong)new_environ;
+}
diff --git a/frysk-sys/frysk/sys/cni/Fork.cxx b/frysk-sys/frysk/sys/cni/Fork.cxx
index c8b639e..c2f36dc 100644
--- a/frysk-sys/frysk/sys/cni/Fork.cxx
+++ b/frysk-sys/frysk/sys/cni/Fork.cxx
@@ -73,7 +73,7 @@ reopen (jstring file, const char *mode, FILE *stream)
 
 int
 spawn(java::io::File* exe, jstring in, jstring out, jstring err,
-      jstringArray args, jstring libs, jint trace)
+      jstringArray args, jlong environ, jint trace)
 {
   // Convert args into argv, argc, filename.
   char *filename = ALLOCA_STRING(exe->getPath());
@@ -120,16 +120,8 @@ spawn(java::io::File* exe, jstring in, jstring out, jstring err,
     case frysk::sys::Fork::NO_TRACE:
       break;
     }
-     if (libs->length() > 0) 
-       {
- 	char *libs_str = (char *) alloca (libs->length() + 1);
- 	JvGetStringUTFRegion (libs, 0, libs->length (), libs_str);
-	libs_str[libs->length()] = '\0';
-	static char* libenv;
-	if (asprintf (&libenv, "LD_LIBRARY_PATH=%s", libs_str) < 0)
-	  ::perror ("asprintf");
-	char * const env[] = {libenv, (char*)0};
- 	::execve (filename, argv, env);
+     if (environ != 0) {
+	::execve (filename, argv, (char **)environ);
        }
      else
       ::execv (filename, argv);
@@ -142,8 +134,8 @@ spawn(java::io::File* exe, jstring in, jstring out, jstring err,
 frysk::sys::ProcessIdentifier*
 frysk::sys::Fork::spawn(java::io::File* exe,
 			jstring in, jstring out, jstring err,
-			jstringArray args, jstring libs, jint trace) {
-  int pid = ::spawn(exe, in, out, err, args, libs, trace);
+			jstringArray args, jlong environ, jint trace) {
+  int pid = ::spawn(exe, in, out, err, args, environ, trace);
   return frysk::sys::ProcessIdentifierFactory::create(pid);
 }
 
@@ -160,7 +152,7 @@ frysk::sys::Fork::daemon (java::io::File* exe, jstring in, jstring out,
   // process id ends up in PID.
 
   if (v == 0) {
-    pid = ::spawn(exe, in, out, err, args, JvNewStringUTF (""), frysk::sys::Fork::NO_TRACE);
+    pid = ::spawn(exe, in, out, err, args, 0, frysk::sys::Fork::NO_TRACE);
     _exit (0);
   }
 


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


                 reply	other threads:[~2008-04-15  2:23 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20080415022321.29037.qmail@sourceware.org \
    --to=scox@sourceware.org \
    --cc=frysk-cvs@sourceware.org \
    --cc=frysk@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).