Author: mladen.turk(a)jboss.com
Date: 2007-09-11 10:32:36 -0400 (Tue, 11 Sep 2007)
New Revision: 1019
Modified:
trunk/sight/native/os/posix/user.c
Log:
Implement Posix who
Modified: trunk/sight/native/os/posix/user.c
===================================================================
--- trunk/sight/native/os/posix/user.c 2007-09-11 06:50:09 UTC (rev 1018)
+++ trunk/sight/native/os/posix/user.c 2007-09-11 14:32:36 UTC (rev 1019)
@@ -35,6 +35,20 @@
#include "sight_types.h"
#include "sight_private.h"
+#if defined(_sun)
+#include <utmpx.h>
+#define SIGHT_UTMP_FILE _UTMPX_FILE
+#define u_time ut_tv.tv_sec
+#else
+#include <utmp.h>
+#ifdef UTMP_FILE
+#define SIGHT_UTMP_FILE UTMP_FILE
+#else
+#define SIGHT_UTMP_FILE _PATH_UTMP
+#endif
+#endif
+
+
static const char etc_usr[] = "/etc/passwd";
/*
@@ -236,7 +250,100 @@
SIGHT_EXPORT_DECLARE(jobjectArray, User, who0)(SIGHT_STDARGS)
{
+ FILE *fu;
+#if defined(_sun)
+ struct futmpx su;
+#else
+ struct utmp su;
+#endif
+ jsize i, j, nusers = 0;
+ jobjectArray users = NULL;
+ cache_table_t *tuc;
+ cache_entry_t *e;
+ sight_arr_t *tusr = NULL;
- UNREFERENCED_STDARGS;
- return NULL;
+
+ UNREFERENCED_O;
+ if (!(tuc = cache_new(16))) {
+ return NULL;
+ }
+ if (!(tusr = sight_arr_cload(etc_usr, "#"))) {
+ goto cleanup;
+ }
+ if (!(fu = fopen(SIGHT_UTMP_FILE, "r"))) {
+ goto cleanup;
+ }
+ /* Read the user sessions */
+ while (fread(&su, sizeof(su), 1, fu)) {
+ if (!*su.ut_name)
+ continue;
+#ifdef USER_PROCESS
+ if (su.ut_type != USER_PROCESS)
+ continue;
+#endif
+ e = cache_add(tuc, su.ut_name);
+ }
+ fclose(fu);
+ if ((nusers = tuc->siz) > 0)
+ users = (*_E)->NewObjectArray(_E, nusers, _clazzn.a, NULL);
+ if (!users) {
+ goto cleanup;
+ }
+ for (j = 0; j < nusers; j++) {
+ jobject u;
+ posix_user_t *nu;
+ if (!(nu = (posix_user_t *)sight_malloc(_E,
+ sizeof(posix_user_t),
+ THROW_FMARK))) {
+ users = NULL;
+ goto cleanup;
+ }
+ nu->uid = -1;
+ nu->gid = -1;
+ if (!(u = new_user_class(_E, _O, P2J(nu)))) {
+ free(nu);
+ users = NULL;
+ goto cleanup;
+ }
+
+ for (i = 0; i < tusr->siz; i++) {
+ int uid;
+ char *uname;
+ char *token;
+ char *titer;
+
+ /* 1. UserName */
+ uname = sight_strtok_c(tusr->arr.ca[i], ':', &titer);
+ if (strcmp(uname, tuc->list[j]->key)) {
+ continue;
+ }
+ else {
+ /* 2. Password */
+ token = sight_strtok_c(NULL, ':', &titer);
+ /* 3. UID */
+ token = sight_strtok_c(NULL, ':', &titer);
+ nu->uid = sight_strtoi32(token);
+ SET_IFIELD_S(0000, u, uname);
+ SET_IFIELD_J(0003, u, (jlong) nu->uid);
+ /* 4. GID */
+ token = sight_strtok_c(NULL, ':', &titer);
+ nu->gid = sight_strtoi32(token);
+ /* 5. FullName */
+ token = sight_strtok_c(NULL, ':', &titer);
+ SET_IFIELD_N(0001, u, token);
+ /* 6. Home */
+ token = sight_strtok_c(NULL, ':', &titer);
+ SET_IFIELD_N(0004, u, token);
+ break;
+ }
+ }
+ (*_E)->SetObjectArrayElement(_E, users, j, u);
+ (*_E)->DeleteLocalRef(_E, u);
+ }
+
+cleanup:
+ if (tusr)
+ sight_arr_free(tusr);
+ cache_free(tuc, NULL);
+ return users;
}