commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1165788 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/ native/os/linux/ native/os/solaris/ native/os/unix/ native/shared/ test/org/apache/commons/runtime/
Date Tue, 06 Sep 2011 19:07:03 GMT
Author: mturk
Date: Tue Sep  6 19:07:02 2011
New Revision: 1165788

URL: http://svn.apache.org/viewvc?rev=1165788&view=rev
Log:
Add Vm.arguments method

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Vm.java
    commons/sandbox/runtime/trunk/src/main/native/os/linux/os.c
    commons/sandbox/runtime/trunk/src/main/native/os/solaris/os.c
    commons/sandbox/runtime/trunk/src/main/native/os/unix/arch_opts.h
    commons/sandbox/runtime/trunk/src/main/native/os/unix/util.c
    commons/sandbox/runtime/trunk/src/main/native/shared/string.c
    commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMain.java

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Vm.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Vm.java?rev=1165788&r1=1165787&r2=1165788&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Vm.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Vm.java Tue Sep
 6 19:07:02 2011
@@ -41,7 +41,9 @@ public final class Vm
     public  static native int getParentPid();
 
     private static final File exec;
-    private static native String exe0();
+    private static native String   exe0();
+    private static native String[] arg0()
+        throws SystemException;
 
     static {
         exec = new File(exe0());
@@ -54,4 +56,14 @@ public final class Vm
     {
         return exec;
     }
+
+    /**
+     * Returns full path to the current JVM executable.
+     */
+    public  static final String[] arguments()
+        throws SystemException
+    {
+        return arg0();
+    }
+
 }

Modified: commons/sandbox/runtime/trunk/src/main/native/os/linux/os.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/linux/os.c?rev=1165788&r1=1165787&r2=1165788&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/linux/os.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/linux/os.c Tue Sep  6 19:07:02 2011
@@ -16,6 +16,8 @@
 
 #include "acr/jniapi.h"
 #include "acr/string.h"
+#include "arch_opts.h"
+
 #include <sys/utsname.h>
 
 static int            _sys_done = -1;
@@ -96,3 +98,19 @@ ACR_JNI_EXPORT(jstring, Vm, exe0)(JNI_ST
         return CSTR_TO_JSTRING(buf);
     }
 }
+
+ACR_JNI_EXPORT(jobjectArray, Vm, arg0)(JNI_STDARGS)
+{
+    jobjectArray args = 0;
+    char  *cmda;
+    size_t clen;
+
+    cmda = AcrFreadAll("/proc/self/cmdline", &clen);
+    if (cmda == 0) {
+        ACR_THROW_SYS_ERRNO();
+        return 0;
+    }
+    args = AcrMszStrToStringArrayA(env, cmda);
+    AcrFree(cmda);
+    return args;
+}

Modified: commons/sandbox/runtime/trunk/src/main/native/os/solaris/os.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/solaris/os.c?rev=1165788&r1=1165787&r2=1165788&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/solaris/os.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/solaris/os.c Tue Sep  6 19:07:02 2011
@@ -16,6 +16,7 @@
 
 #include "acr/jniapi.h"
 #include "acr/string.h"
+#include <procfs.h>
 #include <sys/utsname.h>
 #include <sys/systeminfo.h>
 
@@ -105,3 +106,78 @@ ACR_JNI_EXPORT(jstring, Vm, exe0)(JNI_ST
         return CSTR_TO_JSTRING(buf);
     }
 }
+
+/*
+ * Read the psinfo_t structure from /proc/<pid>/psinfo
+ */
+static int selfpsinfo(psinfo_t *psinfo)
+{
+    int  fd;
+    int  rc;
+
+    if ((fd = open("/proc/self/psinfo", O_RDONLY)) == -1)
+        return errno;
+    if (r_read(fd, psinfo, sizeof(psinfo_t)) != sizeof(psinfo_t)) {
+        r_close(fd);
+        return ACR_EBADF;
+    }
+    r_close(fd);
+    return 0;
+}
+
+ACR_JNI_EXPORT(jobjectArray, Vm, arg0)(JNI_STDARGS)
+{
+    jobjectArray args = 0;
+    psinfo_t pi;
+    int n, fd, rc;
+    char   **sa;
+
+    if ((rc = selfpsinfo(-1, &pi)) != 0) {
+        ACR_THROW_SYS_ERROR(rc);
+        return 0;
+    }
+    if (pi.pr_argc == 0) {
+        /* No arguments ? */
+        return 0;
+    }
+    if ((fd = open("/proc/self/as", O_RDONLY)) == -1) {
+        ACR_THROW_SYS_ERRNO();
+        return 0;
+    }
+    sa = ACR_MALLOC(char *, pi.pr_argc + 1);
+    if (sa == 0)
+        goto failed;
+    if (lseek(fd, pi.pr_argv, SEEK_SET) == -1) {
+        rc = ACR_GET_OS_ERROR();
+        goto failed;
+    }
+    if (read(fd, sa, sizeof(char *) * (pi.pr_argc + 1)) < 0) {
+        rc = ACR_GET_OS_ERROR();
+        goto failed;
+    }
+    args = AcrNewCoreObjectArray(env, ACR_CC_STRING, pi.pr_argc);
+    if (args == 0)
+        goto failed;
+    for (n = 0; n < pi.pr_argc; n++) {
+        jstring s;
+        char pname[8192] = "";
+        if (lseek(fd, (off_t) sa[n], SEEK_SET) != -1) {
+            if (read(fd, pname, sizeof(pname)) == -1)
+                pname[0] = '\0';
+        }
+        s = AcrNewJavaStringA(pname);
+        if (s != 0) {
+            (*env)->SetObjectArrayElement(env, args, n, s);
+            (*env)->DeleteLocalRef(env, s);
+        }
+    }
+    close(fd);
+    AcrFree(args);
+    return args;
+failed:
+    close(fd);
+    AcrFree(sa);
+    if (rc != 0)
+        ACR_THROW_SYS_ERROR(rc);
+    return 0;
+}

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/arch_opts.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/arch_opts.h?rev=1165788&r1=1165787&r2=1165788&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/arch_opts.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/arch_opts.h Tue Sep  6 19:07:02
2011
@@ -165,5 +165,6 @@ int     AcrSigIgnore(int signo);
 int     AcrSigDefault(int signo);
 void    AcrDrainPipe(int fd);
 const char *AcrTempPathGet(const char *srch);
+char       *AcrFreadAll(const char *name, size_t *readed);
 
 #endif /* _ACR_ARCH_OPTS_H_ */

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/util.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/util.c?rev=1165788&r1=1165787&r2=1165788&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/util.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/util.c Tue Sep  6 19:07:02 2011
@@ -569,3 +569,89 @@ const char *AcrTempPathGet(const char *s
     else
         return 0;
 }
+
+#define RD_BUFFERSIZE 4096
+
+char *AcrFreadAll(const char *name, size_t *readed)
+{
+    int    fd;
+    size_t prd;
+    size_t rdd = 0;
+    size_t len = RD_BUFFERSIZE;
+    size_t siz = RD_BUFFERSIZE;
+    char  *buf;
+    char  *pos;
+    int    rc = 0;
+
+    if (readed == 0)
+        readed = &prd;
+    *readed = 0;
+    if ((fd = open(name, O_RDONLY)) == -1)
+        return 0;
+    pos = buf = (char *)malloc(len);
+    if (buf != 0) {
+        for (;;) {
+            ssize_t nrd;
+
+            nrd = r_read(fd, pos, siz);
+            if (nrd == -1) {
+                /* Read failed */
+                rc = errno;
+                if (rdd == 0) {
+                    free(buf);
+                    goto failed;
+                }
+                else {
+                    /* We read something already */
+                    *(buf + rdd) = '\0';
+                    break;
+                }
+            }
+            rdd += nrd;
+            if (nrd == siz) {
+                /* Try with larger buffer size.
+                 */
+                char *nbb;
+                siz = RD_BUFFERSIZE;
+                len = len + siz;
+                nbb = (char *)realloc(buf, len);
+                if (nbb) {
+                    buf = nbb;
+                    pos = nbb + rdd;
+                }
+                else {
+                    /* Failed re-allocating memory */
+                    rc = errno;
+                    free(buf);
+                    goto failed;
+                }
+            }
+            else {
+                if (nrd == 0)
+                    break;
+                pos += nrd;
+                siz -= nrd;
+            }
+        }
+    }
+    else {
+        rc = errno;
+        goto failed;
+    }
+    r_close(fd);
+    pos = (char *)realloc(buf, rdd + 1);
+    if (pos == 0) {
+        free(buf);
+        errno = ENOMEM;
+        return 0;
+    }
+    buf = pos;
+    *(buf + rdd) = '\0';
+    *readed = rdd + 1;
+    return buf;
+
+failed:
+    r_close(fd);
+    errno = rc;
+    return 0;
+}

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/string.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/string.c?rev=1165788&r1=1165787&r2=1165788&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/string.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/string.c Tue Sep  6 19:07:02 2011
@@ -1397,6 +1397,87 @@ char *AcrItoa(char *buf, int n)
 #undef BUFFER_SIZE
 }
 
+static jsize mszstrsizeA(const char *s)
+{
+    jsize n = 0;
+    const char *p;
+
+    for (p = s; p && *p; p++) {
+        n++;
+        while (*p)
+            p++;
+    }
+    return n;
+}
+
+jobjectArray AcrMszStrToStringArrayA(JNI_STDENV, const char *str)
+{
+    jobjectArray arr = NULL;
+    jsize n = 0;
+    const char *p;
+
+    if (str != 0) {
+        if ((n = mszstrsizeA(str)) > 0)
+            arr = AcrNewCoreObjectArray(env, ACR_CC_STRING, n);
+    }
+    if (arr == 0)
+        return 0;
+
+    n = 0;
+    for (p = str; p && *p; p++) {
+        jstring s = AcrNewJavaStringA(env, p);
+        if (s) {
+            (*env)->SetObjectArrayElement(env, arr, n, s);
+            (*env)->DeleteLocalRef(env, s);
+        }
+        n++;
+        while (*p)
+            p++;
+    }
+    return arr;
+}
+
+#if defined(WINDOWS)
+jobjectArray AcrMszStrToStringArrayW(JNI_STDENV, const wchar_t *str)
+{
+    jobjectArray arr = NULL;
+    jsize n = 0;
+    const wchar_t *p;
+
+    if (str != 0) {
+        if ((n = mszstrsizeW(str)) > 0)
+            arr = AcrNewCoreObjectArray(env, ACR_CC_STRING, n);
+    }
+    if (arr == 0)
+        return 0;
+
+    n = 0;
+    for (p = str; p && *p; p++) {
+        jstring s = AcrNewJavaStringW(env, p);
+        if (s) {
+            (*env)->SetObjectArrayElement(env, arr, n, s);
+            (*env)->DeleteLocalRef(env, s);
+        }
+        n++;
+        while (*p)
+            p++;
+    }
+    return arr;
+}
+static jsize mszstrsizeW(const wchar_t *s)
+{
+    jsize n = 0;
+    const wchar_t *p;
+
+    for (p = s; p && *p; p++) {
+        n++;
+        while (*p)
+            p++;
+    }
+    return n;
+}
+
+#endif
 #if defined(ENABLE_TEST_PRIVATE)
 
 ACR_JNI_EXPORT(jint, TestString, test0)(JNI_STDARGS, jstring s)

Modified: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMain.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMain.java?rev=1165788&r1=1165787&r2=1165788&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMain.java (original)
+++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMain.java Tue
Sep  6 19:07:02 2011
@@ -42,6 +42,8 @@ public class TestMain extends Assert
         if (Native.HAS_OPENSSL) {
             assertTrue(Native.ldopenssl());
         }
+        String[] args = Vm.arguments();
+        assertNotNull(args[0]);
     }
     
     @AfterSuite(groups = { "init" })



Mime
View raw message