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 12:32:19 UTC
svn commit: r769737 - in /commons/sandbox/runtime/trunk/src:
main/java/org/apache/commons/runtime/ main/native/include/
main/native/os/unix/ main/native/shared/ main/native/test/
test/org/apache/commons/runtime/
Author: mturk
Date: Wed Apr 29 10:32:18 2009
New Revision: 769737
URL: http://svn.apache.org/viewvc?rev=769737&view=rev
Log:
Add logged users
Modified:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/User.java
commons/sandbox/runtime/trunk/src/main/native/include/acr_tables.h
commons/sandbox/runtime/trunk/src/main/native/os/unix/user.c
commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c
commons/sandbox/runtime/trunk/src/main/native/shared/tables.c
commons/sandbox/runtime/trunk/src/main/native/test/testcase.c
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=769737&r1=769736&r2=769737&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 10:32:18 2009
@@ -51,6 +51,8 @@
private static native boolean equals0(Descriptor a, Descriptor b);
private static native User[] enum0()
throws IOException, SecurityException;
+ private static native User[] enum1()
+ throws IOException, SecurityException;
/**
* Create the {@code User} object from the {@code name}.
@@ -89,6 +91,24 @@
}
/**
+ * Get the {@link UserIterator} of all users defined on the system.
+ *
+ * @return UserIterator containing all users.
+ * @throws IOException in case of I/O error.
+ * @throws SecurityException if the current user is not allowed to
+ * access the system user database.
+ */
+ public static UserIterator getLoggedUsers()
+ throws IOException, SecurityException
+ {
+ UserIterator iter;
+ synchronized(lock) {
+ iter = new UserIteratorImpl(enum1());
+ }
+ return iter;
+ }
+
+ /**
* String that specifies the name of the user account.
*/
public String getName()
Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_tables.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_tables.h?rev=769737&r1=769736&r2=769737&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_tables.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_tables.h Wed Apr 29 10:32:18 2009
@@ -186,7 +186,7 @@
* @param dlen Pointer where to store the data length. Can be NULL
* @return ACR_EOF on end.
*/
-ACR_DECLARE(int) ACR_TableIndex(acr_table_t *t, int idx,
+ACR_DECLARE(int) ACR_TableIndex(acr_table_t *t, int idx, char **key,
void **data, acr_size_t *dlen);
/**
@@ -203,7 +203,7 @@
* @param dlen Pointer where to store the data length. Can be NULL
* @return ACR_EOF on end.
*/
-ACR_DECLARE(int) ACR_TableNext(acr_table_t *t,
+ACR_DECLARE(int) ACR_TableNext(acr_table_t *t, char **key,
void **data, acr_size_t *dlen);
/**
@@ -216,14 +216,15 @@
/** Callback function used in ACR_TableForEach.
* If the function returns non zero the for each loop will break.
*/
-typedef int (acr_table_callback_fn_t)(char *key, void *data, acr_size_t dlen);
+typedef int (acr_table_callback_fn_t)(void *opaque, char *key, void *data,
+ acr_size_t dlen);
/**
* Execute callback for each table entry.
* @param t Table to use.
* @param callback Callback function
*/
-ACR_DECLARE(void) ACR_TableForEach(acr_table_t *t,
+ACR_DECLARE(void) ACR_TableForEach(acr_table_t *t, void *opaque,
acr_table_callback_fn_t *callback);
#ifdef __cplusplus
Modified: 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=769737&r1=769736&r2=769737&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/user.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/user.c Wed Apr 29 10:32:18 2009
@@ -21,11 +21,13 @@
#include "acr_error.h"
#include "acr_memory.h"
#include "acr_string.h"
+#include "acr_tables.h"
#include "acr_descriptor.h"
#include "acr_users.h"
#include <pwd.h>
#include <grp.h>
+#include <utmpx.h>
J_DECLARE_CLAZZ = {
NULL,
@@ -142,7 +144,7 @@
SET_IFIELD_S(0000, usr, pw->pw_name);
SET_IFIELD_S(0001, usr, pw->pw_gecos);
-#ifdef DARWIN
+#ifdef DARWIN
/* Use user class name for comment */
SET_IFIELD_N(0002, usr, pw->pw_class);
#endif
@@ -177,3 +179,66 @@
return V2Z(uida == uidb);
}
+
+ACR_JNI_EXPORT_DECLARE(jobjectArray, User, enum1)(ACR_JNISTDARGS)
+{
+ jobjectArray usrs = NULL;
+ struct utmpx *ut;
+ acr_table_t *utbl;
+ jsize i, n = 0;
+
+ UNREFERENCED_O;
+
+ utbl = ACR_TableMake(_E, THROW_FMARK, 128);
+ if (!utbl)
+ return NULL;
+ /* 1. stage - get the number of logged users */
+ setutxent();
+ while (1) {
+ errno = 0;
+ ut = getutxent();
+ if (ut == 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 (ut == NULL)
+ break;
+ else {
+ if (*ut->ut_user) {
+#ifdef USER_PROCESS
+ if (ut->ut_type != USER_PROCESS)
+ continue;
+#endif
+ if (!ACR_TableSet(_E, THROW_FMARK, utbl, ut->ut_user, _E, 0)) {
+ n++;
+ }
+ }
+ }
+ }
+ endutxent();
+ if (n) {
+ usrs = ACR_NewUserArray(_E, n);
+ }
+ if (usrs == NULL) {
+ return NULL;
+ }
+ /* 2. stage - get the user names */
+ for (i = 0; i < n; i++) {
+ char *uname;
+ if (ACR_TableNext(utbl, &uname, NULL, NULL) == ACR_SUCCESS) {
+ jobject uid = ACR_UserObjectCreate(_E, uname);
+ if ((*_E)->ExceptionCheck(_E) || uid == NULL) {
+ /* Object creation failed */
+ break;
+ }
+ (*_E)->SetObjectArrayElement(_E, usrs, i, uid);
+ (*_E)->DeleteLocalRef(_E, uid);
+ }
+ }
+ ACR_TableFree(_E, THROW_FMARK, utbl);
+ return usrs;
+}
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=769737&r1=769736&r2=769737&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c Wed Apr 29 10:32:18 2009
@@ -109,7 +109,7 @@
return ACR_SUCCESS;
for (i = 0; ; i++) {
void *c;
- if (ACR_TableIndex(clazz_cache, i, &c, NULL) == ACR_SUCCESS) {
+ if (ACR_TableIndex(clazz_cache, i, NULL, &c, NULL) == ACR_SUCCESS) {
(*_E)->DeleteGlobalRef(_E, (jobject)c);
}
else
@@ -288,7 +288,7 @@
if (!clazz_cache)
return 0;
for (i = 0; ; i++) {
- if (ACR_TableIndex(clazz_cache, i, NULL, NULL) != ACR_SUCCESS)
+ if (ACR_TableIndex(clazz_cache, i, NULL, NULL, NULL) != ACR_SUCCESS)
break;
}
return i;
Modified: commons/sandbox/runtime/trunk/src/main/native/shared/tables.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/tables.c?rev=769737&r1=769736&r2=769737&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/tables.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/tables.c Wed Apr 29 10:32:18 2009
@@ -188,7 +188,7 @@
*/
return NULL;
}
- e->key = (char *)key;
+ e->key = ACR_StrdupA(_E, file, line, key);
/* Insert new bucket into the list */
e->next = t->hash[hash];
t->hash[hash] = e;
@@ -222,7 +222,7 @@
*/
return;
}
- e->key = (char *)key;
+ e->key = ACR_StrdupA(_E, file, line, key);
e->data = (void *)data;
e->dlen = dlen;
/* Insert new bucket into the list */
@@ -260,8 +260,15 @@
acr_table_t *tbl)
{
if (tbl) {
- if (tbl->a.elts)
+ if (tbl->a.elts) {
+ acr_size_t i;
+ table_entry_t *e = (table_entry_t *)tbl->a.elts;
+ for (i = 0; i < tbl->a.nelts; i++) {
+ if (e[i].key)
+ free(e[i].key);
+ }
free(tbl->a.elts);
+ }
free(tbl);
}
else {
@@ -270,7 +277,7 @@
}
}
-ACR_DECLARE(int) ACR_TableIndex(acr_table_t *t, int idx,
+ACR_DECLARE(int) ACR_TableIndex(acr_table_t *t, int idx, char **key,
void **data, acr_size_t *dlen)
{
table_entry_t *e = NULL;
@@ -279,6 +286,8 @@
return ACR_EOF;
}
e = (table_entry_t *)t->a.elts;
+ if (key)
+ *key = e[t->i].key;
if (data)
*data = e[idx].data;
if (dlen)
@@ -295,7 +304,7 @@
return ACR_SUCCESS;
}
-ACR_DECLARE(int) ACR_TableNext(acr_table_t *t,
+ACR_DECLARE(int) ACR_TableNext(acr_table_t *t, char **key,
void **data, acr_size_t *dlen)
{
table_entry_t *e = NULL;
@@ -304,6 +313,8 @@
return ACR_EOF;
}
e = (table_entry_t *)t->a.elts;
+ if (key)
+ *key = e[t->i].key;
if (data)
*data = e[t->i].data;
if (dlen)
@@ -320,7 +331,7 @@
return 1;
}
-ACR_DECLARE(void) ACR_TableForEach(acr_table_t *t,
+ACR_DECLARE(void) ACR_TableForEach(acr_table_t *t, void *opaque,
acr_table_callback_fn_t *callback)
{
@@ -328,7 +339,7 @@
acr_size_t i;
table_entry_t *e = (table_entry_t *)t->a.elts;
for (i = 0; i < t->a.nelts; i++) {
- if ((*callback)(e[i].key, e[i].data, e[i].dlen))
+ if ((*callback)(opaque, e[i].key, e[i].data, e[i].dlen))
break;
}
}
Modified: commons/sandbox/runtime/trunk/src/main/native/test/testcase.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/test/testcase.c?rev=769737&r1=769736&r2=769737&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/test/testcase.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/test/testcase.c Wed Apr 29 10:32:18 2009
@@ -127,7 +127,7 @@
}
if (rc) {
for (ACR_TableHasNext(t); ACR_TableHasNext(t);) {
- int r = ACR_TableNext(t, &d, &l);
+ int r = ACR_TableNext(t, NULL, &d, &l);
if (r != ACR_SUCCESS)
break;
}
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=769737&r1=769736&r2=769737&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 10:32:18 2009
@@ -75,7 +75,7 @@
{
UserIterator users = User.getUsers();
- assertNotNull("User" + users);
+ assertNotNull("Users" + users);
assertTrue("Size", users.hasNext());
int i = 0;
while (users.hasNext()) {
@@ -86,4 +86,20 @@
System.out.println("Users " + i);
}
+ public void testLoggedUsers()
+ throws Exception
+ {
+ UserIterator users = User.getLoggedUsers();
+
+ assertNotNull("Users" + users);
+ assertTrue("Size", users.hasNext());
+ int i = 0;
+ while (users.hasNext()) {
+ User u = users.next();
+ i++;
+ }
+ System.out.println();
+ System.out.println("Logged " + i);
+ }
+
}