=================================================================== RCS file: /home/cvs/OpenXM/src/ox_ntl/oxserv.c,v retrieving revision 1.2 retrieving revision 1.3 diff -u -p -r1.2 -r1.3 --- OpenXM/src/ox_ntl/oxserv.c 2003/11/08 12:34:00 1.2 +++ OpenXM/src/ox_ntl/oxserv.c 2003/11/15 09:06:20 1.3 @@ -1,18 +1,23 @@ -/* $OpenXM: OpenXM/src/ox_ntl/oxserv.c,v 1.1 2003/11/03 03:11:21 iwane Exp $ */ +/* $OpenXM: OpenXM/src/ox_ntl/oxserv.c,v 1.2 2003/11/08 12:34:00 iwane Exp $ */ #include #include #include #include #include +#include #include "oxserv.h" #include "oxstack.h" +#include "gc/gc.h" #define DPRINTF(x) printf x; fflush(stdout) -#if 0 +#define FP stdout +#define EPRINTF(x) fprintf x; fflush(FP) + +#if 1 /*===========================================================================* * for DEBUG *===========================================================================*/ @@ -38,19 +43,20 @@ dprintf(const char *fmt, ...) /*===========================================================================* * MACRO *===========================================================================*/ -#define MOXSERV_GET_CMO_TAG(m) ((G_getCmoTag == NULL) ? m->tag : G_getCmoTag(m)) +#define oxserv_get_cmo_tag(m) ((G_getCmoTag == NULL) ? m->tag : G_getCmoTag(m)) #define oxserv_delete_cmo(c) \ do { \ - if (G_DeleteCmo != NULL) \ + if (c != NULL) { \ + if (G_DeleteCmo != NULL) \ G_DeleteCmo(c); \ - else \ + else \ c = NULL; \ + } \ } while (0) - /*===========================================================================* * Global Variables. *===========================================================================*/ @@ -58,6 +64,10 @@ do { \ static OXFILE *G_oxfilep = NULL; static cmo_mathcap *G_oxserv_mathcap = NULL; +/* signal */ +sigset_t G_oxserv_sigusr1; +static jmp_buf G_jmpbuf; + /* User Function */ static void (*G_userExecuteFunction)(const char *, cmo **, int) = NULL; static void (*G_userExecuteStringParser)(const char *) = NULL; @@ -68,7 +78,56 @@ static void (*G_DeleteCmo)(cmo *) = NULL; static int (*G_getCmoTag)(cmo *) = NULL; + /*===========================================================================* + * CMO_ERROR2 + *===========================================================================*/ +#define new_cmo_error2_string(msg) new_cmo_error2((cmo *)new_cmo_string(msg)) + + +static void +oxserv_push_errormes(char *msg) +{ + EPRINTF((FP, "%s\n", msg)); + oxstack_push((cmo *)new_cmo_error2_string(msg)); +} + +/*===========================================================================* + * synchronized malloc + *===========================================================================*/ +void * +oxserv_malloc(size_t size) +{ + void *ptr; + + BLOCK_INPUT(); + ptr = GC_malloc(size); + UNBLOCK_INPUT(); + + return (ptr); +} + +void +oxserv_free(void *ptr, size_t size) +{ + /* nothing */ +} + +void * +oxserv_realloc(void *org, size_t old, size_t size) +{ + void *ptr; + + BLOCK_INPUT(); + ptr = GC_realloc(org, size); + UNBLOCK_INPUT(); + + return (ptr); +} + + + +/*===========================================================================* * OX_SERVER *===========================================================================*/ @@ -83,11 +142,16 @@ static int (*G_getCmoTag)(cmo *) = NULL; static void oxserv_sm_popCMO(OXFILE *fd) { - cmo *m = oxstack_pop(); + cmo *m, *n; + m = oxstack_pop(); if (m == NULL) { + EPRINTF((FP, "stack underflow in popCMO\n")); m = new_cmo_null(); } else if (G_convertCmo) { - m = G_convertCmo(m); + n = G_convertCmo(m); + if (m != n) + oxserv_delete_cmo(m); + m = n; } send_ox_cmo(fd, m); @@ -110,8 +174,10 @@ oxserv_sm_popString(OXFILE *fd) cmo *m = oxstack_pop(); cmo_string *m_str; - if (m == NULL) + if (m == NULL) { + EPRINTF((FP, "stack underflow in popString\n")); m = new_cmo_null(); + } str = new_string_set_cmo(m); @@ -141,6 +207,10 @@ oxserv_sm_pops() int i; c = (cmo_int32 *)oxstack_pop(); /* suppose */ + if (c == NULL) { + EPRINTF((FP, "stack underflow in pops\n")); + return ; + } n = oxstack_get_stack_pointer(); if (c->i < n) @@ -286,6 +356,10 @@ static void oxserv_sm_executeStringByLocalParserInBatchMode(void) { cmo_string *str = (cmo_string *)oxstack_peek(); + if (str == NULL) { + oxserv_push_errormes("stack underflow in executeStringByLocalParserInBatchMode"); + return ; + } G_userExecuteStringParser(str->s); } @@ -301,6 +375,10 @@ static void oxserv_sm_executeStringByLocalParser(void) { cmo_string *str = (cmo_string *)oxstack_pop(); + if (str == NULL) { + oxserv_push_errormes("stack underflow in executeStringByLocalParser"); + return ; + } G_userExecuteStringParser(str->s); } @@ -320,10 +398,21 @@ oxserv_sm_executeFunction(void) int i; cmo_string *name = (cmo_string *)oxstack_pop(); cmo_int32 *cnt = (cmo_int32 *)oxstack_pop(); - cmo **arg = (cmo **)malloc(cnt->i * sizeof(cmo *)); + cmo **arg; + arg = (cmo **)malloc(cnt->i * sizeof(cmo *)); + + if (name == NULL || cnt == NULL) { + oxserv_push_errormes("stack underflow in executeFunction"); + return ; + } + for (i = 0; i < cnt->i; i++) { arg[i] = oxstack_pop(); + if (arg[i] == NULL) { + oxserv_push_errormes("stack underflow in executeFunction"); + return ; + } } /* user function */ @@ -351,11 +440,17 @@ static void oxserv_sm_pushCMOtag() { cmo *c = oxstack_peek(); - cmo_int32 *tag = new_cmo_int32(MOXSERV_GET_CMO_TAG(c)); + cmo_int32 *tag = new_cmo_int32(oxserv_get_cmo_tag(c)); oxstack_push((cmo *)tag); } +/***************************************************************************** + * -- SM_dupErrors -- + * + * PARAM : NONE + * RETURN: NONE + *****************************************************************************/ static void oxserv_sm_dupErrors() { @@ -375,71 +470,99 @@ oxserv_sm_dupErrors() oxstack_push((cmo *)list); } + + + +/***************************************************************************** + * -- SM_control_reset_connection -- signal handler for SIGUSR1 -- + * + * PARAM : NONE + * RETURN: NONE + *****************************************************************************/ static void oxserv_sm_control_reset_connection(int sig) { int tag; OXFILE *fd = G_oxfilep; - DPRINTF(("reset -- start\n")); + DPRINTF(("reset -- start ==> ")); send_ox_tag(fd, OX_SYNC_BALL); oxstack_init_stack(); for (;;) { tag = receive_ox_tag(fd); - DPRINTF(("[%d=0x%x]", tag, tag)); + DPRINTF(("[OX:%d=0x%x]", tag, tag)); if (tag == OX_SYNC_BALL) break; if (tag == OX_DATA) receive_cmo(fd); + else + receive_int32(fd); } - DPRINTF(("-- end.\n")); + DPRINTF((" <== end.\n")); + + longjmp(G_jmpbuf, sig); } +/***************************************************************************** + * execute sm command + * + * PARAM : NONE + * RETURN: NONE + *****************************************************************************/ static int -oxserv_receive_and_execute_sm_command(OXFILE *fd) +oxserv_execute_sm_command(OXFILE *fd, int code) { - int code = receive_int32(fd); + DPRINTF(("[SM:%d=0x%x]", code, code)); + switch (code) { - case SM_popCMO: + case SM_popCMO: /* 262 */ oxserv_sm_popCMO(fd); break; - case SM_executeStringByLocalParser: + case SM_executeStringByLocalParser: /* 268 */ if (G_userExecuteStringParser) oxserv_sm_executeStringByLocalParser(); break; - case SM_executeStringByLocalParserInBatchMode: + case SM_executeStringByLocalParserInBatchMode: /* 274 */ if (G_userExecuteStringParser) oxserv_sm_executeStringByLocalParserInBatchMode(); break; - case SM_pops: + case SM_pops: /* 265 */ oxserv_sm_pops(); break; - case SM_popString: + case SM_popString: /* 263 */ oxserv_sm_popString(fd); break; - case SM_getsp: + case SM_getsp: /* 275 */ oxserv_sm_getsp(); break; - case SM_mathcap: + case SM_mathcap: /* 264 */ oxserv_sm_mathcap(); break; - case SM_setMathCap: + case SM_setMathCap: /* 273 */ /* dont support */ oxstack_pop(); break; - case SM_executeFunction: + case SM_executeFunction: /* 269 */ if (G_userExecuteFunction) oxserv_sm_executeFunction(); break; - case SM_pushCMOtag: + case SM_pushCMOtag: /* 277 */ oxserv_sm_pushCMOtag(); break; - case SM_dupErrors: + case SM_dupErrors: /* 276 */ oxserv_sm_dupErrors(); break; + case SM_popSerializedLocalObject: + case SM_setName: + case SM_evalName: + case SM_beginBlock: + case SM_endBlock: + case SM_shutdown: + case SM_executeFunctionAndPopCMO: + case SM_executeFunctionAndPopSerializedLocalObject: case SM_control_reset_connection: case SM_control_reset_connection_server: default: @@ -456,25 +579,26 @@ oxserv_receive_and_execute_sm_command(OXFILE *fd) * PARAM : fd : OXFILE * RETURN: NONE *****************************************************************************/ -int -oxserv_receive(OXFILE *fd) +static int +oxserv_ox_receive(OXFILE *fd) { int tag; cmo *c; int ret = OXSERV_SUCCESS; + int code; tag = receive_ox_tag(fd); switch (tag) { case OX_DATA: c = receive_cmo(fd); - DPRINTF(("[CMO:%d]", c->tag)); + DPRINTF(("[CMO:%d=0x%x]", c->tag, c->tag)); oxstack_push(c); break; case OX_COMMAND: - DPRINTF(("[SM:%d=0x%x]", tag, tag)); - ret = oxserv_receive_and_execute_sm_command(fd); + code = receive_int32(fd); + ret = oxserv_execute_sm_command(fd, code); break; default: @@ -485,7 +609,28 @@ oxserv_receive(OXFILE *fd) return (ret); } +int +oxserv_receive(OXFILE *fd) +{ + int i = 0; + int ret; + ret = setjmp(G_jmpbuf); + if (ret == 0) { + DPRINTF(("setjmp first time -- %d\n", ret)); + } else { + DPRINTF(("setjmp return from longjmp() -- %d -- \n", ret)); + } + + for (;; i++) { + ret = oxserv_ox_receive(fd); + if (ret != OXSERV_SUCCESS) + break; + } + return (ret); +} + + /***************************************************************************** * initialize oxserver * @@ -500,6 +645,8 @@ oxserv_init(OXFILE *oxfp, int ver, char *vstr, char *s { int ret; + DPRINTF(("init start\n")); + ret = oxstack_init_stack(); if (ret != OXSERV_SUCCESS) return (ret); @@ -508,8 +655,15 @@ oxserv_init(OXFILE *oxfp, int ver, char *vstr, char *s oxserv_mathcap_init(ver, vstr, sysname, cmos, sms); + /* signal */ + sigemptyset(&G_oxserv_sigusr1); + sigaddset(&G_oxserv_sigusr1, SIGUSR1); signal(SIGUSR1, oxserv_sm_control_reset_connection); + /* initialize GMP memory functions. */ + mp_set_memory_functions(oxserv_malloc, oxserv_realloc, oxserv_free); + + /* session start */ oxf_determine_byteorder_server(oxfp); return (OXSERV_SUCCESS); @@ -517,7 +671,7 @@ oxserv_init(OXFILE *oxfp, int ver, char *vstr, char *s /***************************************************************************** - * set oxserver + * set oxserver behavior * * PARAM : mode : mode * : @@ -605,7 +759,7 @@ main(int argc, char *argv[]) ox_stderr_init(stderr); - oxserv_init(oxfp, 0, "$Date: 2003/11/08 12:34:00 $", "oxserv", NULL, NULL); + oxserv_init(oxfp, 0, "$Date: 2003/11/15 09:06:20 $", "oxserv", NULL, NULL); DPRINTF(("main - start\n")); for (i = 0;; i++) {