[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.14 and 1.28

version 1.14, 2003/11/20 07:18:41 version 1.28, 2016/08/27 00:11:27
Line 1 
Line 1 
 /*  $OpenXM: OpenXM/src/kxx/oxmain.c,v 1.13 2003/11/18 11:08:27 takayama Exp $  */  /*  $OpenXM: OpenXM/src/kxx/oxmain.c,v 1.27 2016/03/30 21:34:03 takayama Exp $  */
   /*  Note on IntelMac. [2006.06.05]
       SIGINT does not seem to be blocked on the rosetta emulator of ppc
       on the IntelMac's. "ox" should be universal binary.
      A dirty hack to generate a universal binary of ox is as follows.
      (1) Add -arch ppc -arch i386 to CFLAGS in src/kxx/Makefile
          and src/kan96xx/plugin/Makefile
      (2) Build ox
   */
 /* nullserver01 */  /* nullserver01 */
 #include <stdio.h>  #include <stdio.h>
 #include <fcntl.h>  #include <fcntl.h>
Line 6 
Line 14 
 #include <sys/types.h>  #include <sys/types.h>
 #include <sys/socket.h>  #include <sys/socket.h>
 #include <sys/time.h>  #include <sys/time.h>
   #include <sys/resource.h>
 #include <netinet/in.h>  #include <netinet/in.h>
 #include <netdb.h>  #include <netdb.h>
 #include <signal.h>  #include <signal.h>
Line 33  int Quiet = 0;
Line 42  int Quiet = 0;
 int LocalMode = 1;  int LocalMode = 1;
 int NotifyPortnumber = 0;  int NotifyPortnumber = 0;
 int Do_not_use_control_stream_to_tell_no_server = 1;  int Do_not_use_control_stream_to_tell_no_server = 1;
   int IgnoreSIGINT = 1;
   int Ox_protocol_1999 = 0;
 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);
 #if defined(__CYGWIN__)  
 int errno;  
 #endif  
 /*  gcc -v -c hoge.c */  /*  gcc -v -c hoge.c */
   static void mywait(int m);
   
   void *sGC_malloc(int n) {
     return (void *)malloc(n);
   }
   
 main(int argc, char *argv[]) {  main(int argc, char *argv[]) {
   int fd;    int fd;
   int size;    int size;
Line 54  main(int argc, char *argv[]) {
Line 67  main(int argc, char *argv[]) {
   extern int OpenedSocket;    extern int OpenedSocket;
   char portfile[1024];    char portfile[1024];
   char *pass = NULL;    char *pass = NULL;
     char *passControl = NULL;
     char *passData = NULL;
   int result;    int result;
   int sleepingTime = 0;    int sleepingTime = 0;
     int authEncoding=0;
     FILE *fp;
     char *stmp;
   extern int OxTerminateMode;    extern int OxTerminateMode;
   
     int sighup[]={SIGHUP,-1};
     block_signal(sighup);  /* ignore x of xterm */
   strcpy(sname,"localhost");    strcpy(sname,"localhost");
   strcpy(ServerName,SERVERNAME);    strcpy(ServerName,SERVERNAME);
   i = 1;    i = 1;
Line 99  main(int argc, char *argv[]) {
Line 119  main(int argc, char *argv[]) {
       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) {      }else if (strcmp(argv[i],"-wait") == 0) {
       i++;        i++;
       if (i<argc) {        if (i<argc) {
         sscanf(argv[i],"%d",&sleepingTime);          sscanf(argv[i],"%d",&sleepingTime);
       }        }
       }else if (strcmp(argv[i],"-authEncoding") == 0) {
         i++;
         if (strcmp(argv[i],"file") == 0) {
           authEncoding = 1;
         }else{
           fprintf(stderr,"Unknown -authEncoding %s.\n",argv[i]);
           oxmainUsage(); exit(10);
         }
       }else if (strcmp(argv[i],"-ignoreSIGINT") == 0) {
         i++;
         if (i<argc) {
           sscanf(argv[i],"%d",&IgnoreSIGINT);
         }
       }else if (strcmp(argv[i],"-protocol_1999") == 0) {
         Ox_protocol_1999=1;
     }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 125  main(int argc, char *argv[]) {
Line 170  main(int argc, char *argv[]) {
     fprintf(stderr,"\nTrying to connect\n");      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 (authEncoding == 1) {
           stmp = (char *)sGC_malloc(strlen(getenv("HOME"))+strlen(passControl)+
                                                             strlen(passData)+128);
           sprintf(stmp,"%s/.openxm/tmp.opt/%s",(char *)getenv("HOME"),passControl);
           fp = fopen(stmp,"r");
           if (fp == NULL) { fprintf(stderr,"passControl file %s is not found.\n",stmp); exit(1);}
           fgets(stmp,127,fp); passControl = stmp; fclose(fp);
   
           stmp = (char *)sGC_malloc(strlen(getenv("HOME"))+strlen(passControl)+
                                                             strlen(passData)+128);
           sprintf(stmp,"%s/.openxm/tmp.opt/%s",(char *)getenv("HOME"),passData);
           fp = fopen(stmp,"r");
           if (fp == NULL) { fprintf(stderr,"passData file %s is not found.\n",stmp); exit(1);}
           fgets(stmp,127,fp); passData = stmp; fclose(fp);
     }
   
   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 194  main(int argc, char *argv[]) {
Line 268  main(int argc, char *argv[]) {
     }      }
   }    }
   
     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 215  static void errorToStartEngine(void) {
Line 326  static void errorToStartEngine(void) {
      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," [-finish]  [-wait seconds] [-authEncoding [file]]");
     fprintf(stderr," [-ignoreSIGINT [1|0]]");
   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 258  parentServerMain(int fdControl, int fdStream) {
Line 371  parentServerMain(int fdControl, int fdStream) {
   int message = 1;    int message = 1;
   int controlByteOrder;    int controlByteOrder;
   extern OxTerminateMode;    extern OxTerminateMode;
   extern void myServerExit();    extern void myServerExit(int m);
   
     int sigchld[]={SIGCHLD,-1};
     unblock_signal(sigchld);
   if (OxTerminateMode) {    if (OxTerminateMode) {
         /*          /*
           OxTerminateMode cannot be used if you run ox by xterm -exec ox ...            OxTerminateMode cannot be used if you run ox by xterm -exec ox ...
Line 275  parentServerMain(int fdControl, int fdStream) {
Line 390  parentServerMain(int fdControl, int fdStream) {
   /* Set the network byte order. */    /* Set the network byte order. */
   fprintf(stderr,"controlByteOrder=%x\n",controlByteOrder);    fprintf(stderr,"controlByteOrder=%x\n",controlByteOrder);
   
     int sigint[]={SIGINT,-1};
   signal(SIGINT,myServerExit);    set_signal(sigint[0],myServerExit);
     unblock_signal(sigint);
   while(1) {    while(1) {
     mtag = oxfdGetOXheader(fdControl,&SerialCurrentControl);      mtag = oxfdGetOXheader(fdControl,&SerialCurrentControl);
     /* get the message_tag */      /* get the message_tag */
Line 286  parentServerMain(int fdControl, int fdStream) {
Line 402  parentServerMain(int fdControl, int fdStream) {
     switch( id ) {      switch( id ) {
     case SM_control_kill:      case SM_control_kill:
       if (message) printf("[control] control_kill\n");        if (message) printf("[control] control_kill\n");
       oxSendResultOfControl(fdControl);        /* oxSendResultOfControl(fdControl); */
       sleep(2);        sleep(2);
       myServerExit();        myServerExit(0);
       break;        break;
     case SM_control_reset_connection:      case SM_control_reset_connection:
       if (message) printf("[control] control_reset_connection.\n");        if (message) printf("[control] control_reset_connection.\n");
       if (message) printf("Sending the SIGUSR1 signal to %d:  ",MyServerPid);        if (message) printf("Sending the SIGUSR1 signal to %d:  ",MyServerPid);
       r=kill(MyServerPid,SIGUSR1);        r=kill(MyServerPid,SIGUSR1);
       if (message) printf("Result = %d\n",r);        if (message) printf("Result = %d\n",r);
         if (Ox_protocol_1999) {
           if (message) printf("[obsolete protocol of ox-rfc-100 in 1999] Sending the result packet to the control channel.\n",r);
           oxSendResultOfControlInt32(fdControl,0);
         }
       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");
       fprintf(stderr,"Shutdown the server.");        fprintf(stderr,"Shutdown the server.");
       myServerExit();        myServerExit(0);
       break;        break;
     }      }
   }    }
 }  }
   
 void myServerExit() {  void myServerExit(int m) {
   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(0);    exit(0);
Line 315  void myServerExit() {
Line 435  void myServerExit() {
   
 childServerMain(int fdControl, int fdStream) {  childServerMain(int fdControl, int fdStream) {
   int i;    int i;
     struct rlimit res;
   close(fdControl);   /* close(0); dup(fdStream); */    close(fdControl);   /* close(0); dup(fdStream); */
   dup2(fdStream,3);    dup2(fdStream,3);
   dup2(fdStream,4);    dup2(fdStream,4);
Line 354  childServerMain(int fdControl, int fdStream) {
Line 475  childServerMain(int fdControl, int fdStream) {
           putenv(s);            putenv(s);
         }          }
   }    }
     getrlimit(RLIMIT_STACK,&res);
     if (res.rlim_cur < 65536000) {
       fprintf(stderr,"RLIMIT_STACK is increased to 65Mbytes by setrlimit.\n");
           res.rlim_cur = 65536000;
           setrlimit(RLIMIT_STACK,&res);
     }
   
     int sigint[]={SIGINT,-1};
     if (IgnoreSIGINT) { block_signal(sigint); fprintf(stderr,"SIGING\n");}
   
   if (PacketMonitor) {    if (PacketMonitor) {
     if (execle(ServerName,ServerName,"-monitor",NULL,environ)) {      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);
Line 416  static void couldNotFind(char *s) {
Line 546  static void couldNotFind(char *s) {
 }  }
   
   
   static void mywait(int m) {
     int status;
     int pid;
     int i,j;
     /* signal(SIGCHLD,SIG_IGN); */
     pid = wait(&status);
     fprintf(stderr,"Control: child process %d is exiting.\n",pid);
     fprintf(stderr,"Control: Shutting down the control server.\n");
     sleep(2);
     exit(0);
   }
   
   

Legend:
Removed from v.1.14  
changed lines
  Added in v.1.28

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