Author: mturk
Date: Mon Sep 5 15:15:59 2011
New Revision: 1165328
URL: http://svn.apache.org/viewvc?rev=1165328&view=rev
Log:
Add User class
Added:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/User.java
(with props)
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/UserIterator.java
(with props)
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/UserIteratorImpl.java
(with props)
commons/sandbox/runtime/trunk/src/main/native/os/unix/user.c (with props)
commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestUser.java
(with props)
Modified:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/LocalStrings.properties
commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in
commons/sandbox/runtime/trunk/src/main/native/include/acr/misc.h
commons/sandbox/runtime/trunk/src/main/native/shared/array.c
commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c
Modified:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/LocalStrings.properties
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/LocalStrings.properties?rev=1165328&r1=1165327&r2=1165328&view=diff
==============================================================================
---
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/LocalStrings.properties
(original)
+++
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/LocalStrings.properties
Mon Sep 5 15:15:59 2011
@@ -20,3 +20,6 @@ mutex.ENOTIMPL=Apache Commons Runtime do
sharedmem.ENOTIMPL=Apache Commons Runtime does not support shared memory on
this platform
semaphore.ENOTIMPL=Apache Commons Runtime does not support semaphores on this
platform
execmem.ENOTIMPL=Apache Commons Runtime does not support executable memory on
this platform
+user.ENOTFOUND=User '{0}' not found
+user.ENOCURRENT=Current user not found
+group.ENOTFOUND=Group '{0}' not found
Added:
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=1165328&view=auto
==============================================================================
---
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/User.java
(added)
+++
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/User.java
Mon Sep 5 15:15:59 2011
@@ -0,0 +1,260 @@
+/* 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.
+ */
+
+package org.apache.commons.runtime;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * User Information.
+ *
+ */
+public final class User
+{
+
+ private byte[] bb;
+ private static final int BSIZE;
+ // Singleton access to users database
+ private static Object lock;
+ static {
+ BSIZE = init0();
+ lock = new Object();
+ }
+
+ private User()
+ {
+ bb = null;
+ Id = 0L;
+ }
+
+ private User(long id, byte[] b)
+ {
+ bb = b;
+ Id = id;
+ }
+ private static native int init0();
+ private static native User get0(String name, byte[] buff)
+ throws SystemException, SecurityException;
+ private static native User get1(byte[] buff)
+ throws SystemException, SecurityException;
+ private static native User get2(byte[] buff)
+ throws SystemException, SecurityException;
+ private static native boolean equals0(long a, long b);
+ private static native void enum0(ArrayList<String> uset)
+ throws SystemException, SecurityException;
+ private static native void enum1(ArrayList<String> uset)
+ throws SystemException, SecurityException;
+
+ /**
+ * Create the {@code User} object from the {@code name}.
+ *
+ * @return {@code User} obect.
+ * @throws SecurityException if access to an internal user database
+ * is forbidden.
+ * @throws SystemException in case of error.
+ * @throws NoSuchObjectException if the user {@code name} doesn't exist.
+ */
+ public static User get(String name)
+ throws SystemException, SecurityException, NoSuchObjectException
+ {
+ byte[] b = new byte[BSIZE];
+ User u = get0(name, b);
+ if (u == null)
+ throw new NoSuchObjectException(Local.sm.get("user.ENOTFOUND",
name));
+ return u;
+ }
+
+ /**
+ * Get the current {@code User} object from the system.
+ *
+ * @return current {@code User} obect.
+ * @throws SecurityException if access to an internal user database
+ * is forbidden.
+ * @throws SystemException in case of error.
+ * @throws NoSuchObjectException if the user {@code name} doesn't exist.
+ */
+ public static User get()
+ throws SystemException, SecurityException, NoSuchObjectException
+ {
+ byte[] b = new byte[BSIZE];
+ User u = get1(b);
+ if (u == null)
+ throw new NoSuchObjectException(Local.sm.get("user.ENOCURRENT"));
+ return u;
+ }
+
+ /**
+ * Get the effective {@code User} object from the system.
+ *
+ * @return effective {@code User} obect.
+ * @throws SecurityException if access to an internal user database
+ * is forbidden.
+ * @throws SystemException in case of error.
+ * @throws NoSuchObjectException if the user {@code name} doesn't exist.
+ */
+ public static User getEffective()
+ throws SystemException, SecurityException, NoSuchObjectException
+ {
+ byte[] b = new byte[BSIZE];
+ User u = get2(b);
+ if (u == null) {
+ u = get1(b);
+ if (u == null)
+ throw new
NoSuchObjectException(Local.sm.get("user.ENOCURRENT"));
+ }
+ return u;
+ }
+
+ /**
+ * Get the {@link UserIterator} of all users defined on the system.
+ *
+ * @return UserIterator containing all users.
+ * @throws SystemException in case of error.
+ * @throws SecurityException if the current user is not allowed to
+ * access the system user database.
+ */
+ public static UserIterator getUsers()
+ throws SystemException, SecurityException
+ {
+ UserIterator iter;
+ synchronized(lock) {
+ ArrayList<String> users = new ArrayList<String>();
+ enum0(users);
+ iter = new UserIteratorImpl(users);
+ }
+ return iter;
+ }
+
+ /**
+ * Get the {@link UserIterator} of all users that are currently
+ * logged on to the system.
+ *
+ * @return UserIterator containing all logged users.
+ * @throws SystemException in case of error.
+ * @throws SecurityException if the current user is not allowed to
+ * access the system user database.
+ */
+ public static UserIterator getLoggedUsers()
+ throws SystemException, SecurityException
+ {
+ UserIterator iter;
+ synchronized(lock) {
+ ArrayList<String> users = new ArrayList<String>();
+ enum1(users);
+ iter = new UserIteratorImpl(users);
+ }
+ return iter;
+ }
+
+ /**
+ * String that specifies the name of the user account.
+ */
+ public String getName()
+ {
+ return Name;
+ }
+ private String Name;
+
+ /**
+ * String that contains the full name of the user. This string can be
+ * a null string.
+ */
+ public String getFullName()
+ {
+ return FullName;
+ }
+ private String FullName;
+
+ /**
+ * String that contains a comment associated with the user. This string can
+ * be a null string.
+ */
+ public String getComment()
+ {
+ return Comment;
+ }
+ private String Comment;
+
+ /**
+ * String that contains a comment users Home directory.
+ */
+ public String getHome()
+ {
+ return Home;
+ }
+ private String Home;
+
+ /**
+ * String that contains a user shell.
+ */
+ public String getShell()
+ {
+ return Shell;
+ }
+ private String Shell;
+
+ /**
+ * Returns a string representation of the User.
+ */
+ public String getUserId()
+ {
+ if (UserId == null)
+ return Long.toString(Id);
+ else
+ return UserId;
+ }
+ private String UserId;
+
+ /**
+ * Specifies the <em>uid_t</em> or <em>PSID</em>
+ * {@code Descriptor} identifier of the user.
+ */
+ public final long Id;
+
+ /**
+ * Compares {@code this} User to the specified object.
+ *
+ * @param other a {@code User}
+ * @return true if the class of this {@code User} object and the
+ * class of {@code other} are exactly equal, and they point
+ * to the same system user id.
+ */
+ @Override
+ public boolean equals(Object other)
+ {
+ if (other == null)
+ return false;
+ if (other == this)
+ return true;
+ if (User.class != other.getClass())
+ return false;
+ return equals0(Id, ((User)other).Id);
+ }
+
+ /**
+ * Returns a string representation of the User.
+ * The returned string is native representation of the underlying
+ * user descriptor.
+ * @return a string representation of the user id.
+ */
+ @Override
+ public String toString()
+ {
+ return UserId;
+ }
+
+}
Propchange:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/User.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/UserIterator.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/UserIterator.java?rev=1165328&view=auto
==============================================================================
---
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/UserIterator.java
(added)
+++
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/UserIterator.java
Mon Sep 5 15:15:59 2011
@@ -0,0 +1,71 @@
+/* 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.
+ */
+
+package org.apache.commons.runtime;
+
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+/**
+ * User Iterator.
+ *
+ * @since Runtime 1.0
+ */
+public abstract class UserIterator
+ implements Iterator<User>, Iterable<User>
+{
+ /**
+ * Returns {@code true} if the iteration has more elements.
+ * It returns {@code true} if the {@code next} would return
+ * an user rather then throwing and exception.
+ *
+ * @return {@code true} if the iteration has more elements.
+ */
+ public abstract boolean hasNext();
+
+ /**
+ * Returns an iterator over a set of {@link User} elements.
+ *
+ * return Iterator of type {@code User}.
+ */
+ public Iterator<User> iterator()
+ {
+ return this;
+ }
+
+ /**
+ * Returns the next {@link User} in the iteration.
+ *
+ * @return the next {@code User} in the iteration.
+ */
+ public abstract User next()
+ throws NoSuchElementException;
+
+ /**
+ * Removes from the underlying collection the last element returned
+ * by the iterator. This method is unsupported.
+ *
+ * @throws UnsupportedOperationException is always thrown.
+ * @throws IllegalStateException is the {@code next} method has not yet
+ * been called.
+ */
+ public void remove()
+ throws UnsupportedOperationException, IllegalStateException
+ {
+ throw new UnsupportedOperationException();
+ }
+
+}
Propchange:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/UserIterator.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/UserIteratorImpl.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/UserIteratorImpl.java?rev=1165328&view=auto
==============================================================================
---
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/UserIteratorImpl.java
(added)
+++
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/UserIteratorImpl.java
Mon Sep 5 15:15:59 2011
@@ -0,0 +1,57 @@
+/* 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.
+ */
+
+package org.apache.commons.runtime;
+
+import java.util.NoSuchElementException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * User Iterator implementation
+ *
+ * Package private
+ *
+ */
+class UserIteratorImpl extends UserIterator
+{
+
+ private ArrayList<String> users;
+ private int pos = 0;
+
+ protected UserIteratorImpl(ArrayList<String> users)
+ {
+ this.users = users;
+ }
+
+ public boolean hasNext()
+ {
+ if (pos < users.size()) {
+ return true;
+ }
+ else
+ return false;
+ }
+
+ public User next()
+ throws NoSuchElementException
+ {
+ if (hasNext())
+ return User.get(users.get(pos++));
+ else
+ throw new NoSuchElementException();
+ }
+}
Propchange:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/UserIteratorImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in?rev=1165328&r1=1165327&r2=1165328&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in Mon Sep 5
15:15:59 2011
@@ -80,6 +80,7 @@ UNIX_SOURCES=\
$(TOPDIR)/os/unix/sockopts.c \
$(TOPDIR)/os/unix/sockstream.c \
$(TOPDIR)/os/unix/time.c \
+ $(TOPDIR)/os/unix/user.c \
$(TOPDIR)/os/unix/util.c
BSDX_SOURCES=\
Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr/misc.h
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/misc.h?rev=1165328&r1=1165327&r2=1165328&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr/misc.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr/misc.h Mon Sep 5
15:15:59 2011
@@ -29,6 +29,8 @@ ACR_CLASS_DTOR(HashSet);
int AcrArrayListAdd(JNI_STDARGS, jobject e);
int AcrArrayListEnsureCapacity(JNI_STDARGS, jint size);
+int AcrArrayListContains(JNI_STDARGS, jobject e);
+
int AcrHashSetAdd(JNI_STDARGS, jobject e);
void AcrLibLockAcquire(void);
void AcrLibLockRelease(void);
Added: commons/sandbox/runtime/trunk/src/main/native/os/unix/user.c
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/user.c?rev=1165328&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/user.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/user.c Mon Sep 5
15:15:59 2011
@@ -0,0 +1,377 @@
+/* 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/string.h"
+#include "acr/clazz.h"
+#include "acr/jniapi.h"
+#include "acr/port.h"
+#include "acr/misc.h"
+#include "acr/users.h"
+#include "arch_opts.h"
+
+#include <pwd.h>
+#include <grp.h>
+#include <utmpx.h>
+
+J_DECLARE_CLAZZ = {
+ INVALID_FIELD_OFFSET,
+ 0,
+ 0,
+ 0,
+ ACR_CLASS_PATH "User"
+};
+
+J_DECLARE_M_ID(0000) = {
+ 0,
+ "<init>",
+ "(J[B)V"
+};
+
+J_DECLARE_F_ID(0000) = {
+ INVALID_FIELD_OFFSET,
+ INVALID_FIELD_OFFSET,
+ 0,
+ "Name",
+ "Ljava/lang/String;"
+};
+
+J_DECLARE_F_ID(0001) = {
+ INVALID_FIELD_OFFSET,
+ INVALID_FIELD_OFFSET,
+ 0,
+ "FullName",
+ "Ljava/lang/String;"
+};
+
+J_DECLARE_F_ID(0002) = {
+ INVALID_FIELD_OFFSET,
+ INVALID_FIELD_OFFSET,
+ 0,
+ "Comment",
+ "Ljava/lang/String;"
+};
+
+J_DECLARE_F_ID(0003) = {
+ INVALID_FIELD_OFFSET,
+ INVALID_FIELD_OFFSET,
+ 0,
+ "Home",
+ "Ljava/lang/String;"
+};
+
+J_DECLARE_F_ID(0004) = {
+ INVALID_FIELD_OFFSET,
+ INVALID_FIELD_OFFSET,
+ 0,
+ "Shell",
+ "Ljava/lang/String;"
+};
+
+J_DECLARE_F_ID(0005) = {
+ INVALID_FIELD_OFFSET,
+ INVALID_FIELD_OFFSET,
+ 0,
+ "UserId",
+ "Ljava/lang/String;"
+};
+
+ACR_CLASS_CTOR(User)
+{
+ if (AcrLoadClass(env, &_clazzn, 0) == JNI_FALSE)
+ return JNI_FALSE;
+ J_LOAD_METHOD(0000);
+ J_LOAD_IFIELD(0000);
+ J_LOAD_IFIELD(0001);
+ J_LOAD_IFIELD(0002);
+ J_LOAD_IFIELD(0003);
+ J_LOAD_IFIELD(0004);
+ J_LOAD_IFIELD(0005);
+
+ _clazzn.u = 1;
+ return JNI_TRUE;
+}
+
+ACR_CLASS_DTOR(User)
+{
+ AcrUnloadClass(env, &_clazzn);
+}
+
+jobjectArray AcrNewUserArray(JNI_STDENV, jsize len)
+{
+ if (_clazzn.i)
+ return (*env)->NewObjectArray(env, len, _clazzn.i, NULL);
+ else
+ return 0;
+}
+
+#define PWBUF_SIZE 8192
+static int getpwnam_s(const char *username,
+ struct passwd *pw,
+ char *pwbuf)
+{
+ struct passwd *pwptr;
+#if HAVE_GETPWNAM_R
+ int rc;
+
+ /* POSIX defines getpwnam_r() et al to return the error number
+ * rather than set errno, and requires pwptr to be set to NULL if
+ * the entry is not found, imply that "not found" is not an error
+ * condition; some implementations do return 0 with pwptr set to
+ * NULL.
+ */
+ rc = getpwnam_r(username, pw, pwbuf, PWBUF_SIZE, &pwptr);
+ if (rc != 0)
+ return rc;
+ if (pwptr == 0)
+ return ACR_ENOENT;
+#else
+ /* Some platforms (e.g. FreeBSD 4.x) do not set errno on NULL "not
+ * found" return values for the non-threadsafe function either.
+ */
+ errno = 0;
+ if ((pwptr = getpwnam(username)) != 0) {
+ /* TODO: fill pwbuf with pwptr content
+ * and update pointers if passwd is pointer based
+ */
+ memcpy(pw, pwptr, sizeof *pw);
+ }
+ else
+ return errno ? errno : ACR_ENOENT;
+#endif
+ return 0;
+}
+
+static int getpwuid_s(uid_t userid,
+ struct passwd *pw,
+ char *pwbuf)
+{
+ struct passwd *pwptr;
+#if HAVE_GETPWUID_R
+ int rc;
+
+ /* POSIX defines getpwnam_r() et al to return the error number
+ * rather than set errno, and requires pwptr to be set to NULL if
+ * the entry is not found, imply that "not found" is not an error
+ * condition; some implementations do return 0 with pwptr set to
+ * NULL.
+ */
+ rc = getpwuid_r(userid, pw, pwbuf, PWBUF_SIZE, &pwptr);
+ if (rc != 0)
+ return rc;
+ if (pwptr == 0)
+ return ACR_ENOENT;
+#else
+ /* Some platforms (e.g. FreeBSD 4.x) do not set errno on NULL "not
+ * found" return values for the non-threadsafe function either.
+ */
+ errno = 0;
+ if ((pwptr = getpwuid(username)) != 0)
+ memcpy(pw, pwptr, sizeof *pw);
+ else
+ return errno ? errno : ACR_ENOENT;
+#endif
+ return 0;
+}
+
+static int getpwent_s(struct passwd *pw,
+ char *pwbuf)
+{
+ struct passwd *pwptr;
+#if HAVE_GETPWENT_R
+# if defined(_SOLARIS)
+
+ errno = 0;
+ if ((pwptr = getpwent_r(pw, pwbuf, PWBUF_SIZE)) == 0)
+ return errno ? errno : ACR_ENOENT;
+# else
+ int rc;
+
+ /* POSIX defines getpwnam_r() et al to return the error number
+ * rather than set errno, and requires pwptr to be set to NULL if
+ * the entry is not found, imply that "not found" is not an error
+ * condition; some implementations do return 0 with pwptr set to
+ * NULL.
+ */
+ rc = getpwent_r(pw, pwbuf, PWBUF_SIZE, &pwptr);
+ if (rc != 0)
+ return rc;
+ if (pwptr == 0)
+ return ACR_ENOENT;
+# endif
+#else
+ /* Some platforms (e.g. FreeBSD 4.x) do not set errno on NULL "not
+ * found" return values for the non-threadsafe function either.
+ */
+ errno = 0;
+ if ((pwptr = getpwent()) != 0)
+ memcpy(pw, pwptr, sizeof *pw);
+ else
+ return errno ? errno : ACR_ENOENT;
+#endif
+ return 0;
+}
+
+static jobject
+user_byname(JNI_STDARGS, const char *name)
+{
+ jlong uid;
+ jobject usr;
+ int rc;
+ struct passwd pw;
+ char buffer[PWBUF_SIZE];
+
+ rc = getpwnam_s(name, &pw, buffer);
+ if (rc != 0) {
+ rc = ACR_GET_OS_ERROR();
+ if (!ACR_STATUS_IS_ENOENT(rc))
+ ACR_THROW_SYS_ERROR(rc);
+ return 0;
+ }
+ if (obj == 0)
+ obj = (*env)->NewByteArray(env, 4);
+ uid = pw.pw_uid;
+ usr = (*env)->NewObject(env, _clazzn.i, J4MID(0000), uid, obj);
+ if (usr == 0)
+ return 0;
+
+ SET_IFIELD_S(0000, usr, pw.pw_name);
+ SET_IFIELD_S(0001, usr, pw.pw_gecos);
+#if defined(DARWIN)
+ /* Use user class name for comment */
+ SET_IFIELD_N(0002, usr, pw.pw_class);
+#endif
+ SET_IFIELD_S(0003, usr, pw.pw_dir);
+ SET_IFIELD_S(0004, usr, pw.pw_shell);
+ return usr;
+}
+
+static jobject
+user_fromuid(JNI_STDARGS, jlong id)
+{
+ uid_t uid = (uid_t)id;
+ jobject usr;
+ int rc;
+ struct passwd pw;
+ char buffer[PWBUF_SIZE];
+
+ rc = getpwuid_s(uid, &pw, buffer);
+ if (rc != 0) {
+ rc = ACR_GET_OS_ERROR();
+ if (!ACR_STATUS_IS_ENOENT(rc))
+ ACR_THROW_SYS_ERROR(rc);
+ return 0;
+ }
+ if (obj == 0)
+ obj = (*env)->NewByteArray(env, 4);
+ usr = (*env)->NewObject(env, _clazzn.i, J4MID(0000), id, obj);
+ if (usr == 0)
+ return 0;
+
+ SET_IFIELD_S(0000, usr, pw.pw_name);
+ SET_IFIELD_S(0001, usr, pw.pw_gecos);
+#if defined(DARWIN)
+ /* Use user class name for comment */
+ SET_IFIELD_N(0002, usr, pw.pw_class);
+#endif
+ SET_IFIELD_S(0003, usr, pw.pw_dir);
+ SET_IFIELD_S(0004, usr, pw.pw_shell);
+ return usr;
+}
+
+ACR_JNI_EXPORT(int, User, init0)(JNI_STDARGS)
+{
+ return 4;
+}
+
+ACR_JNI_EXPORT(jobject, User, get0)(JNI_STDARGS, jstring name,
+ jbyteArray buf)
+{
+ jobject usr = 0;
+ WITH_CSTR(name) {
+ usr = user_byname(env, buf, J2S(name));
+ } DONE_WITH_STR(name);
+ return usr;
+}
+
+ACR_JNI_EXPORT(jobject, User, get1)(JNI_STDARGS, jbyteArray buf)
+{
+ return user_fromuid(env, buf, getuid());
+}
+
+ACR_JNI_EXPORT(jobject, User, get2)(JNI_STDARGS, jbyteArray buf)
+{
+ return user_fromuid(env, buf, geteuid());
+}
+
+ACR_JNI_EXPORT(void, User, enum0)(JNI_STDARGS, jobject ua)
+{
+ struct passwd pw;
+ char pwbuf[PWBUF_SIZE];
+
+ setpwent();
+ for (;;) {
+ jstring s;
+ if (getpwent_s(&pw, pwbuf) != 0)
+ break;
+ s = AcrNewJavaStringA(env, pw.pw_name);
+ if (s == 0)
+ break;
+ AcrArrayListAdd(env, ua, s);
+ if ((*env)->ExceptionCheck(env))
+ break;
+ (*env)->DeleteLocalRef(env, s);
+ }
+ endpwent();
+}
+
+ACR_JNI_EXPORT(void, User, enum1)(JNI_STDARGS, jobject ua)
+{
+ struct utmpx *ut;
+ int n = 0;
+
+ /* 1. stage - get the number of logged users */
+ setutxent();
+ while (1) {
+ errno = 0;
+ ut = getutxent();
+ if (ut == 0 && n == 0) {
+ ACR_THROW_SYS_ERRNO();
+ return;
+ }
+ if (ut == 0)
+ break;
+ else {
+ if (*ut->ut_user != '\0') {
+ jstring s;
+#ifdef USER_PROCESS
+ if (ut->ut_type != USER_PROCESS)
+ continue;
+#endif
+ s = AcrNewJavaStringA(env, ut->ut_user);
+ if (s == 0)
+ break;
+ if (AcrArrayListContains(env, ua, s) == 0) {
+ AcrArrayListAdd(env, ua, s);
+ n++;
+ }
+ if ((*env)->ExceptionCheck(env))
+ break;
+ (*env)->DeleteLocalRef(env, s);
+ }
+ }
+ }
+ endutxent();
+}
Propchange: commons/sandbox/runtime/trunk/src/main/native/os/unix/user.c
------------------------------------------------------------------------------
svn:eol-style = native
Modified: commons/sandbox/runtime/trunk/src/main/native/shared/array.c
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/array.c?rev=1165328&r1=1165327&r2=1165328&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/array.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/array.c Mon Sep 5
15:15:59 2011
@@ -43,6 +43,12 @@ J_DECLARE_M_ID(0001) = {
"(I)V"
};
+J_DECLARE_M_ID(0002) = {
+ 0,
+ "contains",
+ "(Ljava/lang/Object;)Z"
+};
+
ACR_CLASS_CTOR(ArrayList)
{
@@ -50,6 +56,7 @@ ACR_CLASS_CTOR(ArrayList)
return JNI_FALSE;
J_LOAD_METHOD(0000);
J_LOAD_METHOD(0001);
+ J_LOAD_METHOD(0002);
_clazzn.u = 1;
return JNI_TRUE;
}
@@ -87,6 +94,18 @@ AcrArrayListEnsureCapacity(JNI_STDARGS,
return rv;
}
+int
+AcrArrayListContains(JNI_STDARGS, jobject e)
+{
+ int rv = 0;
+ if (CLAZZ_LOADED) {
+ rv = CALL_METHOD1(Boolean, 0002, obj, e);
+ if ((*env)->ExceptionCheck(env) == JNI_TRUE)
+ rv = 0;
+ }
+ return rv;
+}
+
ACR_UTIL_EXPORT(jboolean, Array, memcpy0)(JNI_STDARGS,
jarray src,
jint srcPos,
Modified: commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c?rev=1165328&r1=1165327&r2=1165328&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c Mon Sep 5
15:15:59 2011
@@ -25,6 +25,7 @@
#include "acr/observer.h"
#include "acr/iofd.h"
#include "acr/pointer.h"
+#include "acr/users.h"
J_DECLARE_CLAZZ = {
INVALID_FIELD_BASE,
@@ -230,6 +231,7 @@ AcrLoadRuntimeClasses(JNI_STDENV)
ACR_CLASS_LOAD(HeapPointer);
ACR_CLASS_LOAD(SlicePointer);
ACR_CLASS_LOAD(FileDescriptor);
+ ACR_CLASS_LOAD(User);
#if defined(WINDOWS)
ACR_CLASS_LOAD(Win32Service);
@@ -241,6 +243,7 @@ void
AcrUnloadRuntimeClasses(JNI_STDENV)
{
+ ACR_CLASS_UNLOAD(User);
ACR_CLASS_UNLOAD(ConstPointer);
ACR_CLASS_UNLOAD(HeapPointer);
ACR_CLASS_UNLOAD(SlicePointer);
Added:
commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestUser.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestUser.java?rev=1165328&view=auto
==============================================================================
---
commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestUser.java
(added)
+++
commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestUser.java
Mon Sep 5 15:15:59 2011
@@ -0,0 +1,59 @@
+/* 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.
+ */
+
+package org.apache.commons.runtime;
+
+import org.testng.annotations.*;
+import org.testng.Assert;
+import java.lang.reflect.*;
+
+public class TestUser extends Assert
+{
+
+ @Test(groups = { "core" })
+ public void checkRoot()
+ {
+ User u = User.get("root");
+ assertNotNull(u);
+ }
+
+ @Test(groups = { "core" })
+ public void getUsers()
+ {
+ UserIterator users = User.getUsers();
+ assertNotNull(users);
+ int i = 0;
+ while (users.hasNext()) {
+ User u = users.next();
+ i++;
+ }
+ assertTrue(i > 0);
+ }
+
+ @Test(groups = { "core" })
+ public void getLoggedUsers()
+ {
+ UserIterator users = User.getLoggedUsers();
+ assertNotNull(users);
+ int i = 0;
+ while (users.hasNext()) {
+ User u = users.next();
+ i++;
+ }
+ assertTrue(i > 0);
+ }
+
+}
Propchange:
commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestUser.java
------------------------------------------------------------------------------
svn:eol-style = native