You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2009/04/29 08:20:40 UTC

svn commit: r769674 - in /commons/sandbox/runtime/trunk/src: main/java/org/apache/commons/runtime/ main/native/ main/native/os/darwin/ main/native/os/linux/ main/native/os/solaris/ test/org/apache/commons/runtime/

Author: mturk
Date: Wed Apr 29 06:20:38 2009
New Revision: 769674

URL: http://svn.apache.org/viewvc?rev=769674&view=rev
Log:
Add users enum

Added:
    commons/sandbox/runtime/trunk/src/main/native/os/darwin/puser.c   (with props)
    commons/sandbox/runtime/trunk/src/main/native/os/linux/puser.c   (with props)
    commons/sandbox/runtime/trunk/src/main/native/os/solaris/puser.c   (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/User.java
    commons/sandbox/runtime/trunk/src/main/native/Makefile.in
    commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestGroup.java
    commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestUser.java

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/User.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/User.java?rev=769674&r1=769673&r2=769674&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/User.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/User.java Wed Apr 29 06:20:38 2009
@@ -39,8 +39,11 @@
         Id          = id;
     }
 
-    private static native User    get0(String name);
+    private static native User    get0(String name)
+        throws IOException, SecurityException;
     private static native boolean equals0(Descriptor a, Descriptor b);
+    private static native User[] enum0()
+        throws IOException, SecurityException, UnsupportedOperationException;
 
     /**
      * Create the {@code User} object from the {@code name}.
@@ -60,6 +63,12 @@
         return u;
     }
 
+    public static User[] getUsers()
+        throws IOException, SecurityException, UnsupportedOperationException
+    {
+        return enum0();
+    }
+
     /**
      * String that specifies the name of the user account.
      */

Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.in
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.in?rev=769674&r1=769673&r2=769674&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.in Wed Apr 29 06:20:38 2009
@@ -87,6 +87,7 @@
 	$(SRCDIR)/os/unix/uutils.$(OBJ) \
 	$(SRCDIR)/os/linux/platform.$(OBJ) \
 	$(SRCDIR)/os/linux/pgroup.$(OBJ) \
+	$(SRCDIR)/os/linux/puser.$(OBJ) \
 	$(SRCDIR)/os/linux/os.$(OBJ)
 
 SOLARIS_OBJS= \
@@ -99,6 +100,7 @@
 	$(SRCDIR)/os/unix/uutils.$(OBJ) \
 	$(SRCDIR)/os/solaris/platform.$(OBJ) \
 	$(SRCDIR)/os/solaris/pgroup.$(OBJ) \
+	$(SRCDIR)/os/solaris/puser.$(OBJ) \
 	$(SRCDIR)/os/solaris/os.$(OBJ)
 
 DARWIN_OBJS= \
@@ -111,6 +113,7 @@
 	$(SRCDIR)/os/unix/uutils.$(OBJ) \
 	$(SRCDIR)/os/darwin/platform.$(OBJ) \
 	$(SRCDIR)/os/darwin/pgroup.$(OBJ) \
+	$(SRCDIR)/os/darwin/puser.$(OBJ) \
 	$(SRCDIR)/os/darwin/os.$(OBJ)
 
 HPUX_OBJS= \

Added: commons/sandbox/runtime/trunk/src/main/native/os/darwin/puser.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/darwin/puser.c?rev=769674&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/darwin/puser.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/os/darwin/puser.c Wed Apr 29 06:20:38 2009
@@ -0,0 +1,106 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "acr.h"
+#include "acr_private.h"
+#include "acr_arch.h"
+#include "acr_clazz.h"
+#include "acr_error.h"
+#include "acr_memory.h"
+#include "acr_string.h"
+#include "acr_descriptor.h"
+#include "acr_users.h"
+
+#include <pwd.h>
+#include <grp.h>
+
+
+ACR_JNI_EXPORT_DECLARE(jobjectArray, Group, enum0)(ACR_JNISTDARGS)
+{
+    jobjectArray usrs = NULL;
+    struct passwd *pw;
+    jsize i, n = 0;
+    char **unames;
+
+    UNREFERENCED_O;
+
+    /* 1. stage - get the number of groups */
+    setpwent();
+    while (1) {
+        errno = 0;
+        pw = getpwent();
+        if (pw == NULL && n == 0) {
+            if  (ACR_STATUS_IS_EACCES(ACR_GET_OS_ERROR()))
+                ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ESECURITY, 0);
+            else
+                ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EIO,
+                                   ACR_GET_OS_ERROR());
+            return NULL;
+        }
+        if (pw == NULL)
+            break;
+        else
+            n++;
+    }
+    endpwent();
+    if (n == 0) {
+        /* No groups defined */
+        return NULL;
+    }
+    unames = (char **)ACR_Calloc(_E, THROW_FMARK, n * sizeof(char *));
+    if (!unames) {
+        return NULL;
+    }
+    usrs = ACR_NewUserArray(_E, n);
+    if (usrs == NULL) {
+        return NULL;
+    }
+    /* 2. stage - get the group names */
+    setpwent();
+    for (i = 0; i < n; i++) {
+        pw = getpwent();
+        if (gr) {
+            unames[i] = ACR_StrdupA(_E, THROW_FMARK, pw->pw_name);
+            if (!unames[i]) {
+                usrs = NULL;
+                endpwent();
+                goto cleanup;
+            }
+        }
+    }
+    endpwent();
+    /* 3. stage - create the groups */
+    for (i = 0; i < n; i++) {
+        if (unames[i]) {
+            jobject uid = ACR_UserObjectCreate(_E, unames[i]);
+            if ((*_E)->ExceptionCheck(_E) || gid == NULL) {
+                /* Object creation failed */
+                break;
+            }
+            (*_E)->SetObjectArrayElement(_E, usrs, i, uid);
+            (*_E)->DeleteLocalRef(_E, uid);
+        }
+    }
+cleanup:
+    /* 4. stage - free the temp storage */
+    for (i = 0; i < n; i++) {
+        if (unames[i])
+            free(unames[i]);
+    }
+    free(unames);
+    return usrs;
+}
+

Propchange: commons/sandbox/runtime/trunk/src/main/native/os/darwin/puser.c
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/sandbox/runtime/trunk/src/main/native/os/linux/puser.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/linux/puser.c?rev=769674&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/linux/puser.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/os/linux/puser.c Wed Apr 29 06:20:38 2009
@@ -0,0 +1,108 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "acr.h"
+#include "acr_private.h"
+#include "acr_arch.h"
+#include "acr_clazz.h"
+#include "acr_error.h"
+#include "acr_memory.h"
+#include "acr_string.h"
+#include "acr_descriptor.h"
+#include "acr_users.h"
+
+#include <pwd.h>
+#include <grp.h>
+
+
+ACR_JNI_EXPORT_DECLARE(jobjectArray, User, enum0)(ACR_JNISTDARGS)
+{
+    jobjectArray  usrs = NULL;
+    struct passwd usr;
+    struct passwd *pw;
+    jsize i, n = 0;
+    int rc;
+    char   buffer[4096];
+    char **unames;
+
+    UNREFERENCED_O;
+
+    /* 1. stage - get the number of groups */
+    setpwent();
+    while (1) {
+        rc = getpwent_r(&usr, buffer, sizeof(buffer), &pw);
+        if (rc && n == 0) {
+            if  (ACR_STATUS_IS_EACCES(ACR_FROM_OS_ERROR(rc)))
+                ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ESECURITY, 0);
+            else
+                ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EIO,
+                                    ACR_FROM_OS_ERROR(rc));
+            return NULL;
+        }
+        if (rc || pw == NULL)
+            break;
+        else
+            n++;
+    }
+    endpwent();
+    if (n == 0) {
+        /* No groups defined */
+        return NULL;
+    }
+    unames = (char **)ACR_Calloc(_E, THROW_FMARK, n * sizeof(char *));
+    if (!unames) {
+        return NULL;
+    }
+    usrs = ACR_NewUserArray(_E, n);
+    if (usrs == NULL) {
+        return NULL;
+    }
+    /* 2. stage - get the group names */
+    setpwent();
+    for (i = 0; i < n; i++) {
+        rc = getpwent_r(&usr, buffer, sizeof(buffer), &pw);
+        if (rc == 0 && pw) {
+            unames[i] = ACR_StrdupA(_E, THROW_FMARK, pw->pw_name);
+            if (!unames[i]) {
+                usrs = NULL;
+                endpwent();
+                goto cleanup;
+            }
+        }
+    }
+    endpwent();
+    /* 3. stage - create the groups */
+    for (i = 0; i < n; i++) {
+        if (unames[i]) {
+            jobject uid = ACR_UserObjectCreate(_E, unames[i]);
+            if ((*_E)->ExceptionCheck(_E) || uid == NULL) {
+                /* Object creation failed */
+                break;
+            }
+            (*_E)->SetObjectArrayElement(_E, usrs, i, uid);
+            (*_E)->DeleteLocalRef(_E, uid);
+        }
+    }
+cleanup:
+    /* 4. stage - free the temp storage */
+    for (i = 0; i < n; i++) {
+        if (unames[i])
+            free(unames[i]);
+    }
+    free(unames);
+    return usrs;
+}
+

Propchange: commons/sandbox/runtime/trunk/src/main/native/os/linux/puser.c
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/sandbox/runtime/trunk/src/main/native/os/solaris/puser.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/solaris/puser.c?rev=769674&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/solaris/puser.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/os/solaris/puser.c Wed Apr 29 06:20:38 2009
@@ -0,0 +1,108 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "acr.h"
+#include "acr_private.h"
+#include "acr_arch.h"
+#include "acr_clazz.h"
+#include "acr_error.h"
+#include "acr_memory.h"
+#include "acr_string.h"
+#include "acr_descriptor.h"
+#include "acr_users.h"
+
+#include <pwd.h>
+#include <grp.h>
+
+
+ACR_JNI_EXPORT_DECLARE(jobjectArray, User, enum0)(ACR_JNISTDARGS)
+{
+    jobjectArray usrs = NULL;
+    struct passwd usr;
+    struct passwd *pw;
+    jsize i, n = 0;
+    char   buffer[4096];
+    char **unames;
+
+    UNREFERENCED_O;
+
+    /* 1. stage - get the number of groups */
+    setpwent();
+    while (1) {
+        errno = 0;
+        pw = getpwent_r(&usr, buffer, sizeof(buffer));
+        if (pw == NULL && n == 0) {
+            if  (ACR_STATUS_IS_EACCES(ACR_GET_OS_ERROR()))
+                ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ESECURITY, 0);
+            else
+                ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EIO,
+                                   ACR_GET_OS_ERROR());
+            return NULL;
+        }
+        if (pw == NULL)
+            break;
+        else
+            n++;
+    }
+    endpwent();
+    if (n == 0) {
+        /* No groups defined */
+        return NULL;
+    }
+    unames = (char **)ACR_Calloc(_E, THROW_FMARK, n * sizeof(char *));
+    if (!unames) {
+        return NULL;
+    }
+    usrs = ACR_NewUserArray(_E, n);
+    if (usrs == NULL) {
+        return NULL;
+    }
+    /* 2. stage - get the group names */
+    setpwent();
+    for (i = 0; i < n; i++) {
+        pw = getpwent_r(&usr, buffer, sizeof(buffer));
+        if (pw) {
+            unames[i] = ACR_StrdupA(_E, THROW_FMARK, pw->pw_name);
+            if (!unames[i]) {
+                usrs = NULL;
+                endpwent();
+                goto cleanup;
+            }
+        }
+    }
+    endpwent();
+    /* 3. stage - create the groups */
+    for (i = 0; i < n; i++) {
+        if (unames[i]) {
+            jobject uid = ACR_UserObjectCreate(_E, unames[i]);
+            if ((*_E)->ExceptionCheck(_E) || uid == NULL) {
+                /* Object creation failed */
+                break;
+            }
+            (*_E)->SetObjectArrayElement(_E, usrs, i, uid);
+            (*_E)->DeleteLocalRef(_E, uid);
+        }
+    }
+cleanup:
+    /* 4. stage - free the temp storage */
+    for (i = 0; i < n; i++) {
+        if (unames[i])
+            free(unames[i]);
+    }
+    free(unames);
+    return usrs;
+}
+

Propchange: commons/sandbox/runtime/trunk/src/main/native/os/solaris/puser.c
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestGroup.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestGroup.java?rev=769674&r1=769673&r2=769674&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestGroup.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestGroup.java Wed Apr 29 06:20:38 2009
@@ -73,6 +73,8 @@
 
         assertNotNull("Group" + groups);
         assertTrue("Size", groups.length > 1);
+        System.out.println();
+        System.out.println("L Groups   " + groups.length);
     }
 
     public void testGlobalGroups()
@@ -83,6 +85,8 @@
 
             assertNotNull("Group" + groups);
             assertTrue("Size", groups.length > 1);
+            System.out.println();
+            System.out.println("G Groups   " + groups.length);
         } catch (UnsupportedOperationException ex) {
             if (OS.TYPE.contains(OsType.WINDOWS)) {
                 fail("Unexpected exception");

Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestUser.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestUser.java?rev=769674&r1=769673&r2=769674&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestUser.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestUser.java Wed Apr 29 06:20:38 2009
@@ -70,4 +70,15 @@
         }
     }
 
+    public void testUsers()
+        throws Exception
+    {
+        User[] users = User.getUsers();
+
+        assertNotNull("User" + users);
+        assertTrue("Size", users.length > 1);
+        System.out.println();
+        System.out.println("Users      " + users.length);
+    }
+
 }