=================================================================== RCS file: /home/cvs/OpenXM/src/kxx/oxmain.c,v retrieving revision 1.3 retrieving revision 1.24 diff -u -p -r1.3 -r1.24 --- OpenXM/src/kxx/oxmain.c 1999/11/04 02:12:31 1.3 +++ OpenXM/src/kxx/oxmain.c 2006/06/05 00:25:50 1.24 @@ -1,32 +1,49 @@ -/* $OpenXM: OpenXM/src/kxx/oxmain.c,v 1.2 1999/10/30 02:24:27 takayama Exp $ */ +/* $OpenXM: OpenXM/src/kxx/oxmain.c,v 1.23 2006/02/25 09:11:10 takayama Exp $ */ /* nullserver01 */ #include +#include +#include #include #include #include +#include #include #include #include #include +#include /* -lnsl -lsocket /usr/ucblib/libucb.a */ #include "ox_kan.h" #include "serversm.h" -#define SERVERNAME "/usr/local/OpenXM/bin/ox_sm1" +#define SERVERNAME "ox_sm1" +extern char **environ; int OxCritical = 0; int OxInterruptFlag = 0; +int OxTerminateMode = 0; int SerialCurrentControl; int MyServerPid; -char ServerName[1024]; +#define SERVERNAME_SIZE 4096 +char ServerName[SERVERNAME_SIZE]; int PacketMonitor = 0; int Quiet = 0; int LocalMode = 1; int NotifyPortnumber = 0; +int Do_not_use_control_stream_to_tell_no_server = 1; +int IgnoreSIGINT = 1; +static void errorToStartEngine(void); +static int findOxServer(char *server); +static void couldNotFind(char *s); +/* gcc -v -c hoge.c */ +static void mywait(); +void *sGC_malloc(int n) { + return (void *)malloc(n); +} main(int argc, char *argv[]) { int fd; @@ -40,14 +57,23 @@ main(int argc, char *argv[]) { int reverse = 0; extern int OpenedSocket; char portfile[1024]; - char *pass; + char *pass = NULL; + char *passControl = NULL; + char *passData = NULL; + int result; + int sleepingTime = 0; + int authEncoding=0; + FILE *fp; + char *stmp; + extern int OxTerminateMode; + signal(SIGHUP,SIG_IGN); /* ignore x of xterm */ strcpy(sname,"localhost"); strcpy(ServerName,SERVERNAME); i = 1; if (argc == 1) { oxmainUsage(); - exit(); + exit(10); } while (i 0 ) parentServerMain(fdControl,fdStream); - else childServerMain(fdControl,fdStream); + else result=childServerMain(fdControl,fdStream); }else{ - childServerMain(fdControl,fdStream); + result=childServerMain(fdControl,fdStream); } - /* This line will be never executed. */ + /* This line will be never executed in case of success */ + if (result < 0 ) { + errorToStartEngine(); + } } /* non-reverse case. */ @@ -116,7 +221,7 @@ main(int argc, char *argv[]) { fdControl = socketOpen(sname,portControl); portControl = OpenedSocket; if (NotifyPortnumber) { - oxWritePortFile(0,portControl,portfile); + oxWritePortFile(0,portControl,portfile); } fdControl = socketAcceptLocal(fdControl); fprintf(stderr,"\n control port %d : Connected.\n",portControl); @@ -125,7 +230,7 @@ main(int argc, char *argv[]) { fdStream = socketOpen(sname,portStream); portStream = OpenedSocket; if (NotifyPortnumber) { - oxWritePortFile(1,portStream,portfile); + oxWritePortFile(1,portStream,portfile); } fdStream = socketAcceptLocal(fdStream); fprintf(stderr,"\n stream port %d : Connected.\n",portStream); @@ -135,7 +240,7 @@ main(int argc, char *argv[]) { fdControl = socketOpen(sname,portControl); portControl = OpenedSocket; if (NotifyPortnumber) { - oxWritePortFile(0,portControl,portfile); + oxWritePortFile(0,portControl,portfile); } fdControl = socketAccept(fdControl); fprintf(stderr,"\n control port %d : Connected.\n",portControl); @@ -144,38 +249,94 @@ main(int argc, char *argv[]) { fdStream = socketOpen(sname,portStream); portStream = OpenedSocket; if (NotifyPortnumber) { - oxWritePortFile(1,portStream,portfile); + oxWritePortFile(1,portStream,portfile); } fdStream = socketAccept(fdStream); fprintf(stderr,"\n stream port %d : Connected.\n",portStream); } } + if (passControl != NULL) { + char *s; int mm; + fprintf(stderr,"passControl\n"); + mm = strlen(passControl); + s = (char *) malloc(mm+1); + if (s == NULL) {fprintf(stderr,"No more memory.\n"); exit(1); } + if (read(fdControl,s,mm+1) < 0) { + fprintf(stderr,"Read error to read passControl\n"); sleep(5); exit(1); + } + s[mm] = 0; + if (strcmp(s,passControl) != 0) { + fprintf(stderr,"s=%s and passControl=%s do not match.\n",s,passControl); sleep(5); exit(1); + } + free(s); + } + if (passData != NULL) { + char *s; int mm; + mm = strlen(passData); + fprintf(stderr,"passData\n"); + s = (char *) malloc(mm+1); + if (s == NULL) {fprintf(stderr,"No more memory.\n"); exit(1); } + if (read(fdStream,s,mm+1) < 0) { + fprintf(stderr,"Read error to read passData\n"); + errorToStartEngine(); + } + if (strcmp(s,passData) != 0) { + fprintf(stderr,"s=%s and passData=%s do not match.\n",s,passData); + errorToStartEngine(); + } + free(s); + } + if ((fdControl < 0) || (fdStream < 0)) { + fprintf(stderr,"Waiting for 10 seconds to show an error.\n"); + sleep(10); + } + + result = 0; if (portControl != -1) { MyServerPid = fork(); if (MyServerPid > 0 ) parentServerMain(fdControl,fdStream); - else childServerMain(fdControl,fdStream); + else result = childServerMain(fdControl,fdStream); }else{ - childServerMain(fdControl,fdStream); + result = childServerMain(fdControl,fdStream); } + if (result < 0) errorToStartEngine(); +} +static void errorToStartEngine(void) { + fprintf(stderr,"Failed to start the engine. Childing process is terminating.\n"); + /* You have to tell to the control server that there is no engine. + And, the control server must tell the client that there is no + engine. + This part has not yet been implemented. + If you implement this, set Do_not_use_control_stream_to_tell_no_server to + zero. + */ + sleep(10); + exit(-1); } oxmainUsage() { fprintf(stderr,"Usage: \n"); fprintf(stderr," ox [-ox serverprogram -host name -data portnum -control portnum -monitor]\n"); - fprintf(stderr," [-insecure -portfile fname -reverse -pass xxxyyyzzz]"); + fprintf(stderr," [-insecure -portfile fname -reverse -passControl xxxyyyzzz -passData pppqqqrrr]"); + fprintf(stderr," [-finish] [-wait seconds] [-authEncoding [file]]"); + fprintf(stderr," [-ignoreSIGINT [1|0]]"); fprintf(stderr,"\n"); fprintf(stderr,"-reverse: ox server connects to the client.\n"); - fprintf(stderr," The client must give a one time password to ox server to connect to the client with -pass option.\n"); + fprintf(stderr," The client must give a one time password to ox server to connect to the client with -pass* option.\n"); fprintf(stderr," The one time password can be seen by ps command, so you must not use this one time password system on an untrustful host.\n"); fprintf(stderr," The one time password should be sent by a safe communication line like ssh and the ox server should be started by ssh. Do not use rsh\n"); + fprintf(stderr," (The option -pass is obsolete.)\n"); fprintf(stderr," If -reverse is not given, the client connect to the ox server\n"); fprintf(stderr," See OpenXM/src/SSkan/Doc/ox.sm1, /sm1connectr\n"); fprintf(stderr,"-insecure : \n"); fprintf(stderr," If you access to the server from a localhost, you do not need one time password. However, if you access outside of the localhost, a one time password is required. To turn off this restriction, -insecure option is used.\n"); + fprintf(stderr,"\n"); + fprintf(stderr,"If ox fails to find the serverprogram, it tries to look for it in /usr/local/OpenXM/bin and $OpenXM_HOME/bin.\n"); + fprintf(stderr,"\n"); fprintf(stderr,"Example 1:\n"); fprintf(stderr,"(Start the ox server): dc1%% ox -ox ~/OpenXM/bin/ox_sm1 -host dc1.math.kobe-u.ac.jp -insecure -control 1200 -data 1300\n"); fprintf(stderr,"(client): sm1\n "); @@ -197,10 +358,22 @@ parentServerMain(int fdControl, int fdStream) { int r; int message = 1; int controlByteOrder; - + extern OxTerminateMode; extern void myServerExit(); - controlByteOrder = oxTellMyByteOrder(fdControl); + signal(SIGCHLD,mywait); + if (OxTerminateMode) { + /* + OxTerminateMode cannot be used if you run ox by xterm -exec ox ... + */ + if (fork()) { + close(fdControl); close(fdStream); + /* Parent */ + exit(0); /*Tell the caller that launching is successfully finished.*/ + } + } + + controlByteOrder = oxTellMyByteOrder(fdControl,fdControl); /* Set the network byte order. */ fprintf(stderr,"controlByteOrder=%x\n",controlByteOrder); @@ -225,7 +398,7 @@ parentServerMain(int fdControl, int fdStream) { r=kill(MyServerPid,SIGUSR1); if (message) printf("Result = %d\n",r); fflush(NULL); - oxSendResultOfControlInt32(fdControl,0); + /* oxSendResultOfControlInt32(fdControl,0); */ break; default: fprintf(stderr,"[control] Unknown control message.\n"); @@ -239,27 +412,74 @@ parentServerMain(int fdControl, int fdStream) { void myServerExit() { printf("Sending the kill signal to the child.\n"); kill(MyServerPid,SIGKILL); - exit(); + exit(0); } childServerMain(int fdControl, int fdStream) { int i; + struct rlimit res; close(fdControl); /* close(0); dup(fdStream); */ dup2(fdStream,3); dup2(fdStream,4); -/*close(0); - #include - for (i=5; i + for (i=5; i