version 1.3, 2000/11/28 04:02:56 |
version 1.5, 2000/11/28 18:11:42 |
|
|
/* -*- mode: C; coding: euc-japan -*- */ |
/* -*- mode: C; coding: euc-japan -*- */ |
/* $OpenXM: OpenXM/src/oxc/sm_ext.c,v 1.2 2000/11/18 06:03:42 ohara Exp $ */ |
/* $OpenXM: OpenXM/src/oxc/sm_ext.c,v 1.4 2000/11/28 04:52:05 ohara Exp $ */ |
|
|
#include <stdio.h> |
#include <stdio.h> |
#include <stdlib.h> |
#include <stdlib.h> |
#include <unistd.h> |
#include <unistd.h> |
#include <string.h> |
#include <string.h> |
|
#include <sys/types.h> |
#include <signal.h> |
#include <signal.h> |
#include <ox_toolkit.h> |
#include <ox_toolkit.h> |
#include "sm.h" |
#include "sm.h" |
Line 18 static db db_localfunc[] = { |
|
Line 19 static db db_localfunc[] = { |
|
static db db_sm[] = { |
static db db_sm[] = { |
{sm_executeFunction, SM_executeFunction}, |
{sm_executeFunction, SM_executeFunction}, |
{sm_mathcap, SM_mathcap}, |
{sm_mathcap, SM_mathcap}, |
|
{sm_set_mathcap, SM_setMathCap}, |
{sm_popCMO, SM_popCMO}, |
{sm_popCMO, SM_popCMO}, |
{sm_pops, SM_pops}, |
{sm_pops, SM_pops}, |
|
{sm_control_reset_pid, SM_control_reset_connection_pid}, |
|
{sm_control_kill_pid, SM_control_kill_pid}, |
|
{sm_control_kill, SM_control_kill}, |
{NULL, NULL} |
{NULL, NULL} |
}; |
}; |
|
|
Line 48 the sm_executeFunction push an error object. |
|
Line 53 the sm_executeFunction push an error object. |
|
void sm_executeFunction(OXFILE *oxfp) |
void sm_executeFunction(OXFILE *oxfp) |
{ |
{ |
int (*func)(OXFILE *); |
int (*func)(OXFILE *); |
int retcode = 0; |
int retcode = 0; |
cmo *ob = pop(); |
cmo *ob = pop(); |
if (ob->tag == CMO_STRING) { |
if (ob->tag == CMO_STRING) { |
func = lookup_localfunction(((cmo_string *)ob)->s); |
func = lookup_localfunction(((cmo_string *)ob)->s); |
if (func != NULL) { |
if (func != NULL) { |
if ((retcode = func(oxfp)) > 0) { |
if ((retcode = func(oxfp)) > 0) { |
return; |
return; |
} |
} |
} |
} |
} |
} |
push_error(retcode, ob); |
push_error(retcode, ob); |
} |
} |
|
|
/* getargs() set number of popped objects to argc. */ |
/* getargs() set number of popped objects to argc. */ |
Line 67 static int getargs(cmo ***args) |
|
Line 72 static int getargs(cmo ***args) |
|
cmo **argv; |
cmo **argv; |
int i; |
int i; |
int argc = -1; |
int argc = -1; |
cmo_int32 *m = pop(); |
cmo_int32 *m = (cmo_int32 *)pop(); |
|
|
if (m->tag != CMO_INT32 || (argc = m->i) < 0) { |
if (m->tag != CMO_INT32 || (argc = m->i) < 0) { |
fprintf(stderr, "oxc: invalid arguments\n"); |
fprintf(stderr, "oxc: invalid arguments\n"); |
Line 78 static int getargs(cmo ***args) |
|
Line 83 static int getargs(cmo ***args) |
|
} |
} |
*args = argv; |
*args = argv; |
} |
} |
return argc; |
return argc; |
} |
} |
|
|
static int pids[1024] = {0}; |
#define MAX_PROCESS 1024 |
|
|
|
static pid_t *pids = NULL; |
static int pid_ptr = 0; |
static int pid_ptr = 0; |
|
static int pid_size = 0; |
|
|
int pid_lookup(int pid) |
void pids_extend() |
{ |
{ |
int i; |
int size2 = pid_size + MAX_PROCESS; |
for(i=0; i<pid_ptr; i++) { |
pid_t *pids2 = (pid_t *)malloc(sizeof(pid_t)*size2); |
if (pids[i] == pid) { |
if (pids != NULL) { |
return i; |
memcpy(pids2, pids, sizeof(pid_t)*pid_size); |
} |
free(pids); |
} |
} |
return -1; |
pid_size = size2; |
|
pids = pids2; |
} |
} |
|
|
int pid_registed(int pid) |
int pid_lookup(pid_t pid) |
{ |
{ |
return pid_lookup(pid)+1; |
int i; |
|
for(i=0; i<pid_ptr; i++) { |
|
if (pids[i] == pid) { |
|
return i; |
|
} |
|
} |
|
return -1; |
} |
} |
|
|
int pid_regist(int pid) |
int pid_registed(pid_t pid) |
{ |
{ |
if (pid_ptr < 1024) { |
return pid_lookup(pid)+1; |
pids[pid_ptr++] = pid; |
} |
return pid; |
|
|
void pid_regist(pid_t pid) |
|
{ |
|
if (pid_ptr >= pid_size) { |
|
pids_extend(); |
|
} |
|
pids[pid_ptr++] = pid; |
|
} |
|
|
|
void pid_delete(pid_t pid) |
|
{ |
|
int i = pid_lookup(pid); |
|
if (i >= 0 && i != --pid_ptr) { |
|
pids[i] = pids[pid_ptr]; |
|
} |
|
} |
|
|
|
int pid_reset(pid_t pid) |
|
{ |
|
if (pid_registed(pid)) { |
|
kill(pid, SIGUSR1); |
|
return 1; |
} |
} |
return 0; |
return 0; |
} |
} |
|
|
void pid_delete(int pid) |
int pid_kill(pid_t pid) |
{ |
{ |
int i = pid_lookup(pid); |
if (pid_registed(pid)) { |
if (i >= 0 && i != --pid_ptr) { |
kill(pid, SIGKILL); |
pids[i] = pids[pid_ptr]; |
pid_delete(pid); |
|
return 1; |
} |
} |
|
return 0; |
} |
} |
|
|
|
/* Killing all child processes */ |
|
void pid_kill_all() |
|
{ |
|
while(pid_ptr > 0) { |
|
kill(pids[--pid_ptr], SIGKILL); |
|
} |
|
} |
|
|
int lf_oxc_open() |
int lf_oxc_open() |
{ |
{ |
cmo **argv; |
cmo **argv; |
char *cmd; |
char *cmd; |
int port; |
int port; |
int pid; |
pid_t pid; |
|
|
if (getargs(&argv) != 2 || argv[0]->tag != CMO_STRING |
if (getargs(&argv) != 2 || argv[0]->tag != CMO_STRING |
|| argv[1]->tag != CMO_INT32) { |
|| argv[1]->tag != CMO_INT32) { |
fprintf(stderr, "oxc: invalid arguments\n"); |
fprintf(stderr, "oxc: invalid arguments\n"); |
return -1; |
return -1; |
} |
} |
|
|
cmd = ((cmo_string *)argv[0])->s; |
cmd = ((cmo_string *)argv[0])->s; |
port = ((cmo_int32 *)argv[1])->i; |
port = ((cmo_int32 *)argv[1])->i; |
pid = lf_oxc_open_main(cmd, port); |
pid = lf_oxc_open_main(cmd, port); |
if (pid > 0) { |
if (pid > 0) { |
push(new_cmo_int32(pid)); |
push(new_cmo_int32(pid)); |
pid_regist(pid); |
pid_regist(pid); |
} |
} |
return pid; |
return pid; |
} |
} |
|
|
void sm_mathcap(OXFILE *oxfp) |
void sm_mathcap(OXFILE *oxfp) |
{ |
{ |
cmo_mathcap *m = pop(); |
push((cmo *)oxf_cmo_mathcap(oxfp)); |
if (m->tag == CMO_MATHCAP) { |
|
oxf_mathcap_update(oxfp, m); |
|
}else { |
|
push_error(-1, m); |
|
/* an error object must be pushed */ |
|
} |
|
} |
} |
|
|
|
void sm_set_mathcap(OXFILE *oxfp) |
|
{ |
|
cmo_mathcap *m = (cmo_mathcap *)pop(); |
|
if (m->tag == CMO_MATHCAP) { |
|
oxf_mathcap_update(oxfp, m); |
|
}else { |
|
push_error(-1, m); |
|
/* an error object must be pushed */ |
|
} |
|
} |
|
|
void sm_control_kill(OXFILE *oxfp) |
void sm_control_kill(OXFILE *oxfp) |
{ |
{ |
cmo_int32 *m = pop(); |
pid_kill_all(); |
int pid = m->i; |
|
if (m->tag != CMO_INT32 || !pid_registed(pid)) { |
|
push_error(-1, m); |
|
}else { |
|
kill(pid, SIGKILL); |
|
pid_delete(pid); |
|
} |
|
} |
} |
|
|
void sm_control_reset(OXFILE *oxfp) |
void sm_control_kill_pid(OXFILE *oxfp) |
{ |
{ |
cmo_int32 *m = pop(); |
cmo_int32 *m = (cmo_int32 *)pop(); |
int pid = m->i; |
pid_t pid = m->i; |
if (m->tag != CMO_INT32 || !pid_registed(pid)) { |
if (m->tag != CMO_INT32 || !pid_kill(pid)) { |
push_error(-1, m); |
push_error(-1, m); |
}else { |
} |
kill(pid, SIGUSR1); |
|
} |
|
} |
} |
|
|
void sm_control_start_engin(OXFILE *oxfp); |
void sm_control_reset_pid(OXFILE *oxfp) |
|
{ |
|
cmo_int32 *m = (cmo_int32 *)pop(); |
|
pid_t pid = m->i; |
|
if (m->tag != CMO_INT32 || !pid_reset(pid)) { |
|
push_error(-1, m); |
|
return; |
|
} |
|
/* ... */ |
|
} |
|
|
|
void sm_control_spawn_server(OXFILE *oxfp); |
|
void sm_control_terminate_server(OXFILE *oxfp); |
|
|
static int intcmp(int key1, int key2) |
static int intcmp(int key1, int key2) |
{ |
{ |