Author: mladen.turk(a)jboss.com
Date: 2007-09-01 03:46:21 -0400 (Sat, 01 Sep 2007)
New Revision: 959
Modified:
trunk/sight/native/os/linux/console.c
trunk/sight/native/os/linux/module.c
Log:
Implement Console and Module for linux platform
Modified: trunk/sight/native/os/linux/console.c
===================================================================
--- trunk/sight/native/os/linux/console.c 2007-08-30 09:22:15 UTC (rev 958)
+++ trunk/sight/native/os/linux/console.c 2007-09-01 07:46:21 UTC (rev 959)
@@ -192,8 +192,42 @@
TTY_STRUCT ttyo;
TTY_STRUCT ttyn;
int is_a_tty;
+ int echo_char;
} sight_console_t;
+/* Internal functions to read a string without echoing */
+static void read_till_nl(FILE *in)
+{
+#define SIZE 4
+ char buf[SIZE+1];
+
+ do {
+ fgets(buf, SIZE, in);
+ } while (strchr(buf, '\n') == NULL);
+}
+
+static int echo_console(sight_console_t *con, jboolean on)
+{
+
+#ifdef TTY_FLAGS
+ memcpy(&(con->ttyn), &(con->ttyo), sizeof(con->ttyo));
+ if (on) {
+ con->ttyn.TTY_FLAGS |= ECHO;
+ }
+ else {
+ con->ttyn.TTY_FLAGS &= ~ECHO;
+ }
+#endif
+
+#if defined(TTY_set)
+ if (con->is_a_tty && (TTY_set(fileno(con->coni), &(con->ttyn))
== -1)) {
+ return errno;
+ }
+#endif
+ return 0;
+}
+
+
SIGHT_EXPORT_DECLARE(jlong, Console, alloc0)(SIGHT_STDARGS)
{
sight_console_t *con;
@@ -216,12 +250,12 @@
if (!con)
return;
con->is_a_tty = 1;
- if ((con->coni = fopen(DEV_TTY,"r")) == NULL)
- con->coni = stdin;
- if ((con->cono = fopen(DEV_TTY,"r")) == NULL)
- con->cono = stdout;
+ if ((con->coni = fopen(DEV_TTY, "r")) == NULL)
+ con->coni = stdin;
+ if ((con->cono = fopen(DEV_TTY, "w")) == NULL)
+ con->cono = stdout;
- if (TTY_get(fileno(con->coni), &con->ttyo) == -1) {
+ if (TTY_get(fileno(con->coni), &con->ttyo) == -1) {
#ifdef ENOTTY
if (errno == ENOTTY)
con->is_a_tty = 0;
@@ -248,7 +282,7 @@
{
UNREFERENCED_STDARGS;
UNREFERENCED(pid);
- return APR_ENOTIMPL;
+ return APR_SUCCESS;
}
SIGHT_EXPORT_DECLARE(void, Console, close0)(SIGHT_STDARGS,
@@ -260,9 +294,22 @@
if (!con)
return;
- if (con->coni != stdin)
+ if (con->coni && con->echo_char) {
+
+#ifdef TTY_FLAGS
+ memcpy(&(con->ttyn), &(con->ttyo), sizeof(con->ttyo));
+ con->ttyn.TTY_FLAGS |= ECHO;
+#endif
+
+#if defined(TTY_set)
+ if (con->is_a_tty && (TTY_set(fileno(con->coni),
&(con->ttyn)) == -1)) {
+ /* TODO: Do we need throw on error here? */
+ }
+#endif
+ }
+ if (con->coni && con->coni != stdin)
fclose(con->coni);
- if (con->cono != stdout)
+ if (con->cono && con->cono != stdout)
fclose(con->cono);
free(con);
}
@@ -274,20 +321,10 @@
sight_console_t *con = J2P(instance, sight_console_t *);
UNREFERENCED_STDARGS;
-#ifdef TTY_FLAGS
- memcpy(&(con->ttyn), &(con->ttyo), sizeof(con->ttyo));
if (on)
- con->ttyn.TTY_FLAGS |= ECHO;
+ con->echo_char = 0;
else
- con->ttyn.TTY_FLAGS &= ~ECHO;
-#endif
-
-#if defined(TTY_set)
- if (con->is_a_tty && (TTY_set(fileno(con->coni), &(con->ttyn))
== -1)) {
- return;
- }
-#endif
-
+ con->echo_char = '*';
}
SIGHT_EXPORT_DECLARE(void, Console, stitle0)(SIGHT_STDARGS,
@@ -307,6 +344,7 @@
{
UNREFERENCED_STDARGS;
+ kill(getpid(), SIGQUIT);
}
@@ -314,16 +352,89 @@
jlong instance)
{
sight_console_t *con = J2P(instance, sight_console_t *);
- UNREFERENCED_STDARGS;
- return NULL;
+ static int ps;
+ char buff[SIGHT_HBUFFER_SIZ];
+ char *p;
+ jstring rv = NULL;
+
+ UNREFERENCED_O;
+ if (!con) {
+ return NULL;
+ }
+
+ fflush(stdout);
+ ps = 0;
+ pushsig();
+ ps = 1;
+ if (con->echo_char && echo_console(con, JNI_FALSE))
+ goto cleanup;
+ ps = 2;
+ p = fgets(buff, SIGHT_HBUFFER_LEN, con->coni);
+ if (!p)
+ goto cleanup;
+
+ if (feof(con->coni)) {
+ goto cleanup;
+ }
+ if (ferror(con->coni)) {
+ throwAprIOException(_E, apr_get_os_error());
+ goto cleanup;
+ }
+ if ((p = (char *)strchr(buff, '\n')) != NULL)
+ *p = '\0';
+ else
+ read_till_nl(con->coni);
+
+ rv = CSTR_TO_JSTRING(buff);
+
+cleanup:
+ if (intr_signal == SIGINT) {
+
+ /* TODO: Throw some exception */
+ }
+ if (ps >= 2 && con->echo_char)
+ echo_console(con, JNI_TRUE);
+ if (ps >= 1)
+ popsig();
+ return rv;
}
SIGHT_EXPORT_DECLARE(jint, Console, getc0)(SIGHT_STDARGS,
jlong instance)
{
sight_console_t *con = J2P(instance, sight_console_t *);
- UNREFERENCED_STDARGS;
- return -1;
+ int ch;
+ static int ps;
+ UNREFERENCED_O;
+ if (!con) {
+ return -1;
+ }
+
+ ps = 0;
+ pushsig();
+ ps = 1;
+ if (con->echo_char && echo_console(con, JNI_FALSE))
+ goto cleanup;
+ ps = 2;
+
+ ch = fgetc(con->coni);
+ if (ch == -1) {
+ if (ferror(con->coni)) {
+ throwAprIOException(_E, apr_get_os_error());
+ }
+ }
+
+ if (intr_signal == SIGINT) {
+ /* TODO: Throw some exception */
+ ch = -1;
+ }
+
+cleanup:
+ if (ps >= 2 && con->echo_char)
+ echo_console(con, JNI_TRUE);
+ if (ps >= 1)
+ popsig();
+ return ch;
}
SIGHT_EXPORT_DECLARE(void, Console, putc0)(SIGHT_STDARGS,
@@ -366,7 +477,7 @@
{
UNREFERENCED_STDARGS;
UNREFERENCED(instance);
- return APR_ENOTIMPL;
+ return APR_SUCCESS;
}
SIGHT_EXPORT_DECLARE(void, Console, ddisable0)(SIGHT_STDARGS,
Modified: trunk/sight/native/os/linux/module.c
===================================================================
--- trunk/sight/native/os/linux/module.c 2007-08-30 09:22:15 UTC (rev 958)
+++ trunk/sight/native/os/linux/module.c 2007-09-01 07:46:21 UTC (rev 959)
@@ -107,7 +107,64 @@
SIGHT_EXPORT_DECLARE(jobjectArray, Module, enum0)(SIGHT_STDARGS,
jint pid)
{
- UNREFERENCED_STDARGS;
- UNREFERENCED(pid);
- return NULL;
+ char spath[SIGHT_SBUFFER_SIZ];
+ jsize j, i = 0, nmods = 0;
+ jobjectArray mods = NULL;
+ sight_arr_t *amods;
+
+ UNREFERENCED_O;
+ if (pid < 0)
+ strcpy(spath, "/proc/self/maps");
+ else
+ sprintf(spath, "/proc/%d/maps", pid);
+ if (!(amods = sight_arr_rload(spath))) {
+ throwAprIOException(_E, apr_get_os_error());
+ goto cleanup;
+ }
+ for (j = 0; j < amods->siz; j++) {
+ if (strchr(amods->arr.ca[j], '/'))
+ nmods++;
+ }
+
+ mods = (*_E)->NewObjectArray(_E, nmods, _clazzn.a, NULL);
+ if (!mods || (*_E)->ExceptionCheck(_E)) {
+ goto cleanup;
+ }
+ for (j = 0; j < amods->siz; j++) {
+ jobject m;
+ char *bp = NULL;
+ char *bn = NULL;
+ char *p;
+ unsigned long long b, o;
+ if ((bp = strchr(amods->arr.ca[j], '/'))) {
+ if ((bn = strrchr(bp, '/')))
+ bn++;
+ }
+ else
+ continue;
+ m = new_module_class(_E, _O, pid, i);
+ if (!m || (*_E)->ExceptionCheck(_E)) {
+ mods = NULL;
+ goto cleanup;
+ }
+ SET_IFIELD_S(0000, m, bn);
+ SET_IFIELD_S(0001, m, bp);
+ b = strtoull(amods->arr.ca[j], &p, 16);
+ if (p && *p == '-')
+ o = strtoull(p + 1, NULL, 16) - b;
+ else
+ o = 0;
+ SET_IFIELD_J(0002, m, b);
+ SET_IFIELD_J(0003, m, o);
+
+ (*_E)->SetObjectArrayElement(_E, mods, i++, m);
+ (*_E)->DeleteLocalRef(_E, m);
+ if (i > nmods)
+ break;
+ }
+
+cleanup:
+ if (amods)
+ sight_arr_free(amods);
+ return mods;
}