[BACK]Return to oxmain.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / kxx

Diff for /OpenXM/src/kxx/oxmain.c between version 1.5 and 1.20

version 1.5, 2000/07/30 09:55:40 version 1.20, 2004/09/17 12:32:11
Line 1 
Line 1 
 /*  $OpenXM: OpenXM/src/kxx/oxmain.c,v 1.4 2000/01/31 01:34:23 takayama Exp $  */  /*  $OpenXM: OpenXM/src/kxx/oxmain.c,v 1.19 2004/09/17 08:46:10 takayama Exp $  */
 /* nullserver01 */  /* nullserver01 */
 #include <stdio.h>  #include <stdio.h>
 #include <fcntl.h>  #include <fcntl.h>
   #include <unistd.h>
 #include <sys/types.h>  #include <sys/types.h>
 #include <sys/socket.h>  #include <sys/socket.h>
 #include <sys/time.h>  #include <sys/time.h>
Line 9 
Line 10 
 #include <netdb.h>  #include <netdb.h>
 #include <signal.h>  #include <signal.h>
 #include <setjmp.h>  #include <setjmp.h>
   #include <stdlib.h>
 /* -lnsl -lsocket /usr/ucblib/libucb.a */  /* -lnsl -lsocket /usr/ucblib/libucb.a */
 #include "ox_kan.h"  #include "ox_kan.h"
 #include "serversm.h"  #include "serversm.h"
   
 #define SERVERNAME "ox_sm1"  #define SERVERNAME "ox_sm1"
   
   extern char **environ;
 int OxCritical = 0;  int OxCritical = 0;
 int OxInterruptFlag = 0;  int OxInterruptFlag = 0;
   int OxTerminateMode = 0;
   
 int SerialCurrentControl;  int SerialCurrentControl;
   
Line 32  int Do_not_use_control_stream_to_tell_no_server = 1;
Line 36  int Do_not_use_control_stream_to_tell_no_server = 1;
 static void errorToStartEngine(void);  static void errorToStartEngine(void);
 static int findOxServer(char *server);  static int findOxServer(char *server);
 static void couldNotFind(char *s);  static void couldNotFind(char *s);
   /*  gcc -v -c hoge.c */
   
   void *sGC_malloc(int n) {
     return (void *)malloc(n);
   }
   
 main(int argc, char *argv[]) {  main(int argc, char *argv[]) {
   int fd;    int fd;
Line 46  main(int argc, char *argv[]) {
Line 54  main(int argc, char *argv[]) {
   int reverse = 0;    int reverse = 0;
   extern int OpenedSocket;    extern int OpenedSocket;
   char portfile[1024];    char portfile[1024];
   char *pass;    char *pass = NULL;
     char *passControl = NULL;
     char *passData = NULL;
   int result;    int result;
     int sleepingTime = 0;
     extern int OxTerminateMode;
   
     signal(SIGHUP,SIG_IGN);  /* ignore x of xterm */
   strcpy(sname,"localhost");    strcpy(sname,"localhost");
   strcpy(ServerName,SERVERNAME);    strcpy(ServerName,SERVERNAME);
   i = 1;    i = 1;
   if (argc == 1) {    if (argc == 1) {
     oxmainUsage();      oxmainUsage();
     exit();      exit(10);
   }    }
   while (i<argc) {    while (i<argc) {
     if (strcmp(argv[i],"-host") == 0) {      if (strcmp(argv[i],"-host") == 0) {
Line 75  main(int argc, char *argv[]) {
Line 88  main(int argc, char *argv[]) {
       LocalMode = 0;        LocalMode = 0;
     }else if (strcmp(argv[i],"-reverse") == 0) {      }else if (strcmp(argv[i],"-reverse") == 0) {
       reverse = 1;        reverse = 1;
       }else if (strcmp(argv[i],"-finish") == 0) {
         OxTerminateMode = 1;
     }else if (strcmp(argv[i],"-portfile") == 0) {      }else if (strcmp(argv[i],"-portfile") == 0) {
       i++;        i++;
       if (i<argc) {        if (i<argc) {
                 sscanf(argv[i],"%s",portfile);          sscanf(argv[i],"%s",portfile);
                 portControl = 0;          portControl = 0;
                 portStream = 0;          portStream = 0;
                 NotifyPortnumber = 1;          NotifyPortnumber = 1;
       }        }
     }else if (strcmp(argv[i],"-pass") == 0) {      }else if (strcmp(argv[i],"-pass") == 0) {
       i++;        i++;
       if (i<argc) {        if (i<argc) {
                 pass = argv[i];          pass = argv[i];
       }        }
       }else if (strcmp(argv[i],"-passData") == 0) {
         i++;
         if (i<argc) {
           passData = argv[i];
         }
       }else if (strcmp(argv[i],"-passControl") == 0) {
         i++;
         if (i<argc) {
           passControl = argv[i];
         }
       }else if (strcmp(argv[i],"-wait") == 0) {
         i++;
         if (i<argc) {
           sscanf(argv[i],"%d",&sleepingTime);
         }
     }else {      }else {
       fprintf(stderr,"Unknown option %s.\n",argv[i]);        fprintf(stderr,"Unknown option %s.\n",argv[i]);
       oxmainUsage(); exit(10);        oxmainUsage(); exit(10);
Line 96  main(int argc, char *argv[]) {
Line 126  main(int argc, char *argv[]) {
   }    }
   
   if (Do_not_use_control_stream_to_tell_no_server) {    if (Do_not_use_control_stream_to_tell_no_server) {
         if (findOxServer(ServerName) < 0) {      if (findOxServer(ServerName) < 0) {
           fprintf(stderr,"Sleeping five seconds...\n");        fprintf(stderr,"Sleeping five seconds...\n");
           sleep(5);        sleep(5);
           exit(-1);        exit(-1);
         }      }
   }    }
   
     if (sleepingTime) {
       fprintf(stderr,"Waiting to connect for %d seconds...\n",sleepingTime);
       sleep(sleepingTime);
       fprintf(stderr,"\nTrying to connect\n");
     }
   
     if ((pass != NULL) && (passData == NULL)) {
           passData = pass;
     }
     if ((pass != NULL) && (passControl == NULL)) {
       passControl = pass;
     }
   
     /* Decrypt passControl and passData, here. Lookup cryptmethod. */
   
   if (reverse) {    if (reverse) {
     /* The order is very important. */      /* The order is very important. */
     fdControl = socketConnectWithPass(sname,portControl,pass);      fdControl = socketConnectWithPass(sname,portControl,passControl);
     fdStream = socketConnectWithPass(sname,portStream,pass);      fdStream = socketConnectWithPass(sname,portStream,passData);
   
     fprintf(stderr,"Connected: control = %d, data = %d.\n",fdControl,fdStream);      fprintf(stderr,"Connected: control = %d, data = %d.\n",fdControl,fdStream);
         result = 0;      result = 0;
   
   
       if ((fdControl < 0) ||  (fdStream < 0)) {
         fprintf(stderr,"Waiting for 10 seconds to show an error.\n");
         sleep(10);
       }
   
     if (portControl != -1) {      if (portControl != -1) {
       MyServerPid = fork();        MyServerPid = fork();
       if (MyServerPid > 0 ) parentServerMain(fdControl,fdStream);        if (MyServerPid > 0 ) parentServerMain(fdControl,fdStream);
Line 120  main(int argc, char *argv[]) {
Line 170  main(int argc, char *argv[]) {
       result=childServerMain(fdControl,fdStream);        result=childServerMain(fdControl,fdStream);
     }      }
     /* This line will be never executed in case of success */      /* This line will be never executed in case of success */
         if (result < 0 ) {      if (result < 0 ) {
           errorToStartEngine();        errorToStartEngine();
         }      }
   }    }
   
   /* non-reverse case. */    /* non-reverse case. */
Line 137  main(int argc, char *argv[]) {
Line 187  main(int argc, char *argv[]) {
       fdControl = socketOpen(sname,portControl);        fdControl = socketOpen(sname,portControl);
       portControl = OpenedSocket;        portControl = OpenedSocket;
       if (NotifyPortnumber) {        if (NotifyPortnumber) {
                 oxWritePortFile(0,portControl,portfile);          oxWritePortFile(0,portControl,portfile);
       }        }
       fdControl = socketAcceptLocal(fdControl);        fdControl = socketAcceptLocal(fdControl);
       fprintf(stderr,"\n control port %d : Connected.\n",portControl);        fprintf(stderr,"\n control port %d : Connected.\n",portControl);
Line 146  main(int argc, char *argv[]) {
Line 196  main(int argc, char *argv[]) {
       fdStream = socketOpen(sname,portStream);        fdStream = socketOpen(sname,portStream);
       portStream = OpenedSocket;        portStream = OpenedSocket;
       if (NotifyPortnumber) {        if (NotifyPortnumber) {
                 oxWritePortFile(1,portStream,portfile);          oxWritePortFile(1,portStream,portfile);
       }        }
       fdStream = socketAcceptLocal(fdStream);        fdStream = socketAcceptLocal(fdStream);
       fprintf(stderr,"\n stream port %d : Connected.\n",portStream);        fprintf(stderr,"\n stream port %d : Connected.\n",portStream);
Line 156  main(int argc, char *argv[]) {
Line 206  main(int argc, char *argv[]) {
       fdControl = socketOpen(sname,portControl);        fdControl = socketOpen(sname,portControl);
       portControl = OpenedSocket;        portControl = OpenedSocket;
       if (NotifyPortnumber) {        if (NotifyPortnumber) {
                 oxWritePortFile(0,portControl,portfile);          oxWritePortFile(0,portControl,portfile);
       }        }
       fdControl = socketAccept(fdControl);        fdControl = socketAccept(fdControl);
       fprintf(stderr,"\n control port %d : Connected.\n",portControl);        fprintf(stderr,"\n control port %d : Connected.\n",portControl);
Line 165  main(int argc, char *argv[]) {
Line 215  main(int argc, char *argv[]) {
       fdStream = socketOpen(sname,portStream);        fdStream = socketOpen(sname,portStream);
       portStream = OpenedSocket;        portStream = OpenedSocket;
       if (NotifyPortnumber) {        if (NotifyPortnumber) {
                 oxWritePortFile(1,portStream,portfile);          oxWritePortFile(1,portStream,portfile);
       }        }
       fdStream = socketAccept(fdStream);        fdStream = socketAccept(fdStream);
       fprintf(stderr,"\n stream port %d : Connected.\n",portStream);        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;    result = 0;
   if (portControl != -1) {    if (portControl != -1) {
     MyServerPid = fork();      MyServerPid = fork();
Line 187  main(int argc, char *argv[]) {
Line 274  main(int argc, char *argv[]) {
 static void errorToStartEngine(void) {  static void errorToStartEngine(void) {
   fprintf(stderr,"Failed to start the engine. Childing process is terminating.\n");    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.    /* 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       And, the control server must tell the client that there is no
          engine.       engine.
          This part has not yet been implemented.       This part has not yet been implemented.
          If you implement this, set Do_not_use_control_stream_to_tell_no_server to       If you implement this, set Do_not_use_control_stream_to_tell_no_server to
          zero.       zero.
          */    */
   sleep(2);    sleep(10);
   exit(-1);    exit(-1);
 }  }
   
 oxmainUsage() {  oxmainUsage() {
   fprintf(stderr,"Usage: \n");    fprintf(stderr,"Usage: \n");
   fprintf(stderr,"  ox [-ox serverprogram -host name -data portnum -control portnum -monitor]\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]");
   fprintf(stderr,"\n");    fprintf(stderr,"\n");
   fprintf(stderr,"-reverse: ox server connects to the client.\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 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 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,"          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,"          See OpenXM/src/SSkan/Doc/ox.sm1, /sm1connectr\n");
   fprintf(stderr,"-insecure : \n");    fprintf(stderr,"-insecure : \n");
Line 234  parentServerMain(int fdControl, int fdStream) {
Line 323  parentServerMain(int fdControl, int fdStream) {
   int r;    int r;
   int message = 1;    int message = 1;
   int controlByteOrder;    int controlByteOrder;
     extern OxTerminateMode;
   extern void myServerExit();    extern void myServerExit();
   
   controlByteOrder = oxTellMyByteOrder(fdControl);    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. */    /* Set the network byte order. */
   fprintf(stderr,"controlByteOrder=%x\n",controlByteOrder);    fprintf(stderr,"controlByteOrder=%x\n",controlByteOrder);
   
Line 262  parentServerMain(int fdControl, int fdStream) {
Line 362  parentServerMain(int fdControl, int fdStream) {
       r=kill(MyServerPid,SIGUSR1);        r=kill(MyServerPid,SIGUSR1);
       if (message) printf("Result = %d\n",r);        if (message) printf("Result = %d\n",r);
       fflush(NULL);        fflush(NULL);
       oxSendResultOfControlInt32(fdControl,0);        /*      oxSendResultOfControlInt32(fdControl,0); */
       break;        break;
     default:      default:
       fprintf(stderr,"[control] Unknown control message.\n");        fprintf(stderr,"[control] Unknown control message.\n");
Line 276  parentServerMain(int fdControl, int fdStream) {
Line 376  parentServerMain(int fdControl, int fdStream) {
 void myServerExit() {  void myServerExit() {
   printf("Sending the kill signal to the child.\n");    printf("Sending the kill signal to the child.\n");
   kill(MyServerPid,SIGKILL);    kill(MyServerPid,SIGKILL);
   exit();    exit(0);
 }  }
   
 childServerMain(int fdControl, int fdStream) {  childServerMain(int fdControl, int fdStream) {
Line 285  childServerMain(int fdControl, int fdStream) {
Line 385  childServerMain(int fdControl, int fdStream) {
   dup2(fdStream,3);    dup2(fdStream,3);
   dup2(fdStream,4);    dup2(fdStream,4);
   /*close(0);    /*close(0);
         #include <sys/param.h>      #include <sys/param.h>
         for (i=5; i<NOFILE; i++) close(i);      for (i=5; i<NOFILE; i++) close(i);
         */    */
   if (!Do_not_use_control_stream_to_tell_no_server) {    if (!Do_not_use_control_stream_to_tell_no_server) {
         if (findOxServer(ServerName) < 0) {      if (findOxServer(ServerName) < 0) {
           return(-1);        return(-1);
         }      }
   }    }
   fprintf(stderr,"childServerMain: Starting the server %s\n",ServerName); fflush(NULL);    fprintf(stderr,"childServerMain: Starting the server %s\n",ServerName); fflush(NULL);
   
     /*
     {
           int i;
           i=0;
           while (environ[i] != NULL) {
             fprintf(stderr,"%s ",environ[i++]);
           }
           fprintf(stderr,"\n");
     }
     */
     /* bug: xterm of potato does not seem to pass the LD_LIBRARY_PATH.
         So, the new gc does not work.
        it is an workaround for OpenXM */
     if (getenv("LD_LIBRARY_PATH") == (char *)NULL) {
           char *s,*o;
       fprintf(stderr,"Hmm... LD_LIBRARY_PATH does not seem to be set.\n");
       o = getenv("OpenXM_HOME");
           if (o == NULL) {
             fprintf(stderr,"Giving up to set the LD_LIBRARY_PATH variable.\n");
           }else{
             s = (char *)malloc(strlen(o)+64);
             sprintf(s,"LD_LIBRARY_PATH=%s/lib",o);
             putenv(s);
           }
     }
   
   if (PacketMonitor) {    if (PacketMonitor) {
     if (execl(ServerName,ServerName,"-monitor",NULL)) {      if (execle(ServerName,ServerName,"-monitor",NULL,environ)) {
       fprintf(stderr,"%s cannot be executed with -monitor.\n",ServerName);        fprintf(stderr,"%s cannot be executed with -monitor.\n",ServerName);
           fflush(NULL);        fflush(NULL);
           return(-1);        return(-1);
     }      }
   }else {    }else {
     if (execl(ServerName,ServerName,NULL)) {      if (execle(ServerName,ServerName,NULL,environ)) {
       fprintf(stderr,"%s cannot be executed.\n",ServerName);        fprintf(stderr,"%s cannot be executed.\n",ServerName);
           fflush(NULL);        fflush(NULL);
           return(-1);        return(-1);
     }      }
   }    }
   /* never reached. */    /* never reached. */
Line 318  restoreLockCtrlCForOx() { ; }
Line 445  restoreLockCtrlCForOx() { ; }
 static int findOxServer(char *server) {  static int findOxServer(char *server) {
   char *p;    char *p;
   char *p2;    char *p2;
   int fd;  
   char *getenv(char *s);  
   if (strlen(server) == 0) return(-1);    if (strlen(server) == 0) return(-1);
   fd = open(server,O_RDONLY);    /* fd = open(server,O_RDONLY); */
   if (fd >= 0) {    if (access(server,X_OK&R_OK) == 0) {
         fprintf(stderr,"Starting OX server : %s\n",server);      fprintf(stderr,"Starting OX server : %s\n",server);
         close(fd);      return(0);
         return(0);  
   }    }
   if (server[0] == '/') {    if (server[0] == '/') {
         couldNotFind(server);      couldNotFind(server);
         return(-1);      return(-1);
   }    }
   fprintf(stderr,"The server %s was not found. Trying to find it under OpenXM/bin\n",server);    fprintf(stderr,"The server %s was not found. Trying to find it under OpenXM/bin\n",server);
   p = getenv("OpenXM_HOME");    p = getenv("OpenXM_HOME");
Line 339  static int findOxServer(char *server) {
Line 463  static int findOxServer(char *server) {
   p2 = (char *) malloc(sizeof(char)*(strlen(p)+strlen("/bin/")+3+strlen(server)));    p2 = (char *) malloc(sizeof(char)*(strlen(p)+strlen("/bin/")+3+strlen(server)));
   if (p2 == NULL) { fprintf(stderr,"No more memory.\n"); exit(10); }    if (p2 == NULL) { fprintf(stderr,"No more memory.\n"); exit(10); }
   strcpy(p2,p); strcat(p2,"/bin/"); strcat(p2,server);    strcpy(p2,p); strcat(p2,"/bin/"); strcat(p2,server);
   fd = open(p2,O_RDONLY);    /* fd = open(p2,O_RDONLY); */
   if (fd >= 0) {    if (access(p2,X_OK&R_OK) == 0) {
         fprintf(stderr,"Starting OX server : %s\n",p2);      fprintf(stderr,"Starting OX server : %s\n",p2);
         if (strlen(p2) < SERVERNAME_SIZE) strcpy(server,p2);      if (strlen(p2) < SERVERNAME_SIZE) strcpy(server,p2);
         else {      else {
           couldNotFind("Too long ox server name.");        couldNotFind("Too long ox server name.");
           return(-1);        return(-1);
         }      }
         close(fd);      return(0);
         return(0);  
   }    }
   couldNotFind(p2);    couldNotFind(p2);
   return(-1);    return(-1);

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.20

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>