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

Annotation of OpenXM/src/kxx/oxmain.c, Revision 1.19

1.19    ! takayama    1: /*  $OpenXM: OpenXM/src/kxx/oxmain.c,v 1.18 2004/09/17 07:27:28 takayama Exp $  */
1.1       maekawa     2: /* nullserver01 */
                      3: #include <stdio.h>
1.4       takayama    4: #include <fcntl.h>
1.10      takayama    5: #include <unistd.h>
1.1       maekawa     6: #include <sys/types.h>
                      7: #include <sys/socket.h>
                      8: #include <sys/time.h>
                      9: #include <netinet/in.h>
                     10: #include <netdb.h>
                     11: #include <signal.h>
                     12: #include <setjmp.h>
1.14      takayama   13: #include <stdlib.h>
1.1       maekawa    14: /* -lnsl -lsocket /usr/ucblib/libucb.a */
                     15: #include "ox_kan.h"
                     16: #include "serversm.h"
                     17:
1.4       takayama   18: #define SERVERNAME "ox_sm1"
1.1       maekawa    19:
1.14      takayama   20: extern char **environ;
1.1       maekawa    21: int OxCritical = 0;
                     22: int OxInterruptFlag = 0;
1.12      takayama   23: int OxTerminateMode = 0;
1.1       maekawa    24:
                     25: int SerialCurrentControl;
                     26:
                     27: int MyServerPid;
1.4       takayama   28: #define SERVERNAME_SIZE 4096
                     29: char ServerName[SERVERNAME_SIZE];
1.1       maekawa    30: int PacketMonitor = 0;
                     31: int Quiet = 0;
                     32:
                     33: int LocalMode = 1;
                     34: int NotifyPortnumber = 0;
1.4       takayama   35: int Do_not_use_control_stream_to_tell_no_server = 1;
                     36: static void errorToStartEngine(void);
                     37: static int findOxServer(char *server);
                     38: static void couldNotFind(char *s);
1.8       takayama   39: /*  gcc -v -c hoge.c */
1.15      takayama   40:
                     41: void *sGC_malloc(int n) {
                     42:   return (void *)malloc(n);
                     43: }
1.1       maekawa    44:
                     45: main(int argc, char *argv[]) {
                     46:   int fd;
                     47:   int size;
                     48:   char sname[1024];
                     49:   int tmp[1];
                     50:   char *buf;
                     51:   int i;
                     52:   int fdControl = -1; int portControl = 1200;
                     53:   int fdStream = -1;  int portStream = 1300;
                     54:   int reverse = 0;
                     55:   extern int OpenedSocket;
                     56:   char portfile[1024];
1.12      takayama   57:   char *pass = NULL;
1.18      takayama   58:   char *passControl = NULL;
                     59:   char *passData = NULL;
1.4       takayama   60:   int result;
1.9       takayama   61:   int sleepingTime = 0;
1.12      takayama   62:   extern int OxTerminateMode;
1.1       maekawa    63:
1.17      takayama   64:   signal(SIGHUP,SIG_IGN);  /* ignore x of xterm */
1.1       maekawa    65:   strcpy(sname,"localhost");
                     66:   strcpy(ServerName,SERVERNAME);
                     67:   i = 1;
1.2       takayama   68:   if (argc == 1) {
                     69:     oxmainUsage();
1.12      takayama   70:     exit(10);
1.2       takayama   71:   }
1.1       maekawa    72:   while (i<argc) {
                     73:     if (strcmp(argv[i],"-host") == 0) {
                     74:       i++;
                     75:       if (i<argc) strcpy(sname,argv[i]);
                     76:     }else if (strcmp(argv[i],"-data")==0) {
                     77:       i++;
                     78:       if (i<argc) sscanf(argv[i],"%d",&portStream);
                     79:     }else if (strcmp(argv[i],"-control")==0) {
                     80:       i++;
                     81:       if (i<argc) sscanf(argv[i],"%d",&portControl);
                     82:     }else if (strcmp(argv[i],"-ox") == 0) {
                     83:       i++;
                     84:       if (i<argc) strcpy(ServerName,argv[i]);
                     85:     }else if (strcmp(argv[i],"-monitor") == 0) {
                     86:       PacketMonitor = 1;
                     87:     }else if (strcmp(argv[i],"-insecure") == 0) {
                     88:       LocalMode = 0;
                     89:     }else if (strcmp(argv[i],"-reverse") == 0) {
                     90:       reverse = 1;
1.12      takayama   91:     }else if (strcmp(argv[i],"-finish") == 0) {
                     92:       OxTerminateMode = 1;
1.1       maekawa    93:     }else if (strcmp(argv[i],"-portfile") == 0) {
                     94:       i++;
                     95:       if (i<argc) {
1.7       takayama   96:         sscanf(argv[i],"%s",portfile);
                     97:         portControl = 0;
                     98:         portStream = 0;
                     99:         NotifyPortnumber = 1;
1.1       maekawa   100:       }
                    101:     }else if (strcmp(argv[i],"-pass") == 0) {
                    102:       i++;
                    103:       if (i<argc) {
1.7       takayama  104:         pass = argv[i];
1.1       maekawa   105:       }
1.18      takayama  106:     }else if (strcmp(argv[i],"-passData") == 0) {
                    107:       i++;
                    108:       if (i<argc) {
                    109:         passData = argv[i];
                    110:       }
                    111:     }else if (strcmp(argv[i],"-passControl") == 0) {
                    112:       i++;
                    113:       if (i<argc) {
                    114:         passControl = argv[i];
                    115:       }
1.9       takayama  116:     }else if (strcmp(argv[i],"-wait") == 0) {
                    117:       i++;
                    118:       if (i<argc) {
                    119:         sscanf(argv[i],"%d",&sleepingTime);
                    120:       }
1.1       maekawa   121:     }else {
                    122:       fprintf(stderr,"Unknown option %s.\n",argv[i]);
                    123:       oxmainUsage(); exit(10);
                    124:     }
                    125:     i++;
                    126:   }
                    127:
1.4       takayama  128:   if (Do_not_use_control_stream_to_tell_no_server) {
1.7       takayama  129:     if (findOxServer(ServerName) < 0) {
                    130:       fprintf(stderr,"Sleeping five seconds...\n");
                    131:       sleep(5);
                    132:       exit(-1);
                    133:     }
1.9       takayama  134:   }
                    135:
                    136:   if (sleepingTime) {
                    137:     fprintf(stderr,"Waiting to connect for %d seconds...\n",sleepingTime);
                    138:     sleep(sleepingTime);
                    139:     fprintf(stderr,"\nTrying to connect\n");
1.4       takayama  140:   }
                    141:
1.18      takayama  142:   if ((pass != NULL) && (passData == NULL)) {
                    143:        passData = pass;
                    144:   }
                    145:   if ((pass != NULL) && (passControl == NULL)) {
                    146:     passControl = pass;
                    147:   }
                    148:
1.19    ! takayama  149:   /* Decrypt passControl and passData, here. Lookup cryptmethod. */
        !           150:
1.1       maekawa   151:   if (reverse) {
                    152:     /* The order is very important. */
1.18      takayama  153:     fdControl = socketConnectWithPass(sname,portControl,passControl);
                    154:     fdStream = socketConnectWithPass(sname,portStream,passData);
1.1       maekawa   155:
                    156:     fprintf(stderr,"Connected: control = %d, data = %d.\n",fdControl,fdStream);
1.7       takayama  157:     result = 0;
1.5       takayama  158:
                    159:
1.1       maekawa   160:     if (portControl != -1) {
                    161:       MyServerPid = fork();
                    162:       if (MyServerPid > 0 ) parentServerMain(fdControl,fdStream);
1.4       takayama  163:       else result=childServerMain(fdControl,fdStream);
1.1       maekawa   164:     }else{
1.4       takayama  165:       result=childServerMain(fdControl,fdStream);
1.1       maekawa   166:     }
1.4       takayama  167:     /* This line will be never executed in case of success */
1.7       takayama  168:     if (result < 0 ) {
                    169:       errorToStartEngine();
                    170:     }
1.1       maekawa   171:   }
                    172:
                    173:   /* non-reverse case. */
                    174:   fprintf(stderr,"Hostname is %s \n",sname);
                    175:   fprintf(stderr,"Port for data (-data) = %d \n",portStream);
                    176:   fprintf(stderr,"Port for control message (-control) = %d \n",portControl);
                    177:   fflush(NULL);
                    178:
                    179:
                    180:   if (LocalMode) {
                    181:     if (portControl != -1) {
                    182:       fdControl = socketOpen(sname,portControl);
                    183:       portControl = OpenedSocket;
                    184:       if (NotifyPortnumber) {
1.7       takayama  185:         oxWritePortFile(0,portControl,portfile);
1.1       maekawa   186:       }
                    187:       fdControl = socketAcceptLocal(fdControl);
                    188:       fprintf(stderr,"\n control port %d : Connected.\n",portControl);
                    189:     }
                    190:     if (portStream != -1) {
                    191:       fdStream = socketOpen(sname,portStream);
                    192:       portStream = OpenedSocket;
                    193:       if (NotifyPortnumber) {
1.7       takayama  194:         oxWritePortFile(1,portStream,portfile);
1.1       maekawa   195:       }
                    196:       fdStream = socketAcceptLocal(fdStream);
                    197:       fprintf(stderr,"\n stream port %d : Connected.\n",portStream);
                    198:     }
                    199:   }else{
                    200:     if (portControl != -1) {
                    201:       fdControl = socketOpen(sname,portControl);
                    202:       portControl = OpenedSocket;
                    203:       if (NotifyPortnumber) {
1.7       takayama  204:         oxWritePortFile(0,portControl,portfile);
1.1       maekawa   205:       }
                    206:       fdControl = socketAccept(fdControl);
                    207:       fprintf(stderr,"\n control port %d : Connected.\n",portControl);
                    208:     }
                    209:     if (portStream != -1) {
                    210:       fdStream = socketOpen(sname,portStream);
                    211:       portStream = OpenedSocket;
                    212:       if (NotifyPortnumber) {
1.7       takayama  213:         oxWritePortFile(1,portStream,portfile);
1.1       maekawa   214:       }
                    215:       fdStream = socketAccept(fdStream);
                    216:       fprintf(stderr,"\n stream port %d : Connected.\n",portStream);
                    217:     }
                    218:   }
                    219:
1.19    ! takayama  220:   if (passControl != NULL) {
        !           221:        char *s; int mm;
        !           222:        fprintf(stderr,"passControl\n");
        !           223:        mm = strlen(passControl);
        !           224:        s = (char *) malloc(mm+1);
        !           225:     if (s == NULL) {fprintf(stderr,"No more memory.\n"); exit(1); }
        !           226:        if (read(fdControl,s,mm+1) < 0) {
        !           227:          fprintf(stderr,"Read error to read passControl\n"); sleep(5); exit(1);
        !           228:        }
        !           229:     s[mm] = 0;
        !           230:        if (strcmp(s,passControl) != 0) {
        !           231:          fprintf(stderr,"s=%s and passControl=%s  do not match.\n",s,passControl); sleep(5); exit(1);
        !           232:        }
        !           233:        free(s);
        !           234:   }
        !           235:   if (passData != NULL) {
        !           236:        char *s; int mm;
        !           237:        mm = strlen(passData);
        !           238:        fprintf(stderr,"passData\n");
        !           239:        s = (char *) malloc(mm+1);
        !           240:     if (s == NULL) {fprintf(stderr,"No more memory.\n"); exit(1); }
        !           241:        if (read(fdStream,s,mm+1) < 0) {
        !           242:          fprintf(stderr,"Read error to read passData\n");
        !           243:          errorToStartEngine();
        !           244:        }
        !           245:        if (strcmp(s,passData) != 0) {
        !           246:          fprintf(stderr,"s=%s and passData=%s  do not match.\n",s,passData);
        !           247:          errorToStartEngine();
        !           248:        }
        !           249:     free(s);
        !           250:   }
1.1       maekawa   251:
1.4       takayama  252:   result = 0;
1.1       maekawa   253:   if (portControl != -1) {
                    254:     MyServerPid = fork();
                    255:     if (MyServerPid > 0 ) parentServerMain(fdControl,fdStream);
1.4       takayama  256:     else result = childServerMain(fdControl,fdStream);
1.1       maekawa   257:   }else{
1.4       takayama  258:     result = childServerMain(fdControl,fdStream);
1.1       maekawa   259:   }
1.4       takayama  260:   if (result < 0) errorToStartEngine();
                    261: }
1.1       maekawa   262:
1.4       takayama  263: static void errorToStartEngine(void) {
                    264:   fprintf(stderr,"Failed to start the engine. Childing process is terminating.\n");
                    265:   /* You have to tell to the control server that there is no engine.
1.7       takayama  266:      And, the control server must tell the client that there is no
                    267:      engine.
                    268:      This part has not yet been implemented.
                    269:      If you implement this, set Do_not_use_control_stream_to_tell_no_server to
                    270:      zero.
                    271:   */
1.4       takayama  272:   sleep(2);
                    273:   exit(-1);
1.1       maekawa   274: }
                    275:
                    276: oxmainUsage() {
                    277:   fprintf(stderr,"Usage: \n");
                    278:   fprintf(stderr,"  ox [-ox serverprogram -host name -data portnum -control portnum -monitor]\n");
1.18      takayama  279:   fprintf(stderr," [-insecure -portfile fname -reverse -passControl xxxyyyzzz -passData pppqqqrrr]");
1.12      takayama  280:   fprintf(stderr," [-finish]");
1.1       maekawa   281:   fprintf(stderr,"\n");
1.2       takayama  282:   fprintf(stderr,"-reverse: ox server connects to the client.\n");
1.18      takayama  283:   fprintf(stderr,"          The client must give a one time password to ox server to connect to the client with -pass* option.\n");
1.2       takayama  284:   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");
                    285:   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");
1.18      takayama  286:   fprintf(stderr,"          (The option -pass is obsolete.)\n");
1.2       takayama  287:   fprintf(stderr,"          If -reverse is not given, the client connect to the ox server\n");
                    288:   fprintf(stderr,"          See OpenXM/src/SSkan/Doc/ox.sm1, /sm1connectr\n");
                    289:   fprintf(stderr,"-insecure : \n");
                    290:   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");
1.4       takayama  291:   fprintf(stderr,"\n");
                    292:   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");
                    293:   fprintf(stderr,"\n");
1.2       takayama  294:   fprintf(stderr,"Example 1:\n");
1.3       takayama  295:   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");
1.2       takayama  296:   fprintf(stderr,"(client):  sm1\n ");
                    297:   fprintf(stderr,"           (ox.sm1) run ; \n");
1.1       maekawa   298:   fprintf(stderr,"           sm1>[(dc1.math.kobe-u.ac.jp) 1300 1200] oxconnect /ox.ccc set\n");
1.2       takayama  299:   fprintf(stderr,"Example 2:\n");
                    300:   fprintf(stderr,"(Start the ox server): dc1%% ox -ox ~/OpenXM/bin/ox_sm1\n");
                    301:   fprintf(stderr,"(client): dc1%% sm1\n ");
                    302:   fprintf(stderr,"           (ox.sm1) run ; \n");
                    303:   fprintf(stderr,"           sm1>[(localhost) 1300 1200] oxconnect /ox.ccc set\n");
1.1       maekawa   304:   fprintf(stderr,"\n");
                    305: }
                    306:
                    307: parentServerMain(int fdControl, int fdStream) {
                    308:   int id;
                    309:   int mtag;
                    310:   int size;
                    311:   int n;
                    312:   int r;
                    313:   int message = 1;
                    314:   int controlByteOrder;
1.12      takayama  315:   extern OxTerminateMode;
1.1       maekawa   316:   extern void myServerExit();
                    317:
1.12      takayama  318:   if (OxTerminateMode) {
                    319:        /*
                    320:          OxTerminateMode cannot be used if you run ox by xterm -exec ox ...
                    321:         */
                    322:        if (fork()) {
                    323:          close(fdControl); close(fdStream);
                    324:          /* Parent */
                    325:          exit(0);  /*Tell the caller that launching is successfully finished.*/
                    326:        }
                    327:   }
                    328:
1.13      takayama  329:   controlByteOrder = oxTellMyByteOrder(fdControl,fdControl);
1.1       maekawa   330:   /* Set the network byte order. */
                    331:   fprintf(stderr,"controlByteOrder=%x\n",controlByteOrder);
                    332:
                    333:
                    334:   signal(SIGINT,myServerExit);
                    335:   while(1) {
                    336:     mtag = oxfdGetOXheader(fdControl,&SerialCurrentControl);
                    337:     /* get the message_tag */
                    338:     /* message_body */
                    339:     id = oxfdGetInt32(fdControl);   /* get the function_id */
                    340:     if (message) {printf("\n[control] control function_id is %d\n",id);}
                    341:     switch( id ) {
                    342:     case SM_control_kill:
                    343:       if (message) printf("[control] control_kill\n");
                    344:       oxSendResultOfControl(fdControl);
                    345:       sleep(2);
                    346:       myServerExit();
                    347:       break;
                    348:     case SM_control_reset_connection:
                    349:       if (message) printf("[control] control_reset_connection.\n");
                    350:       if (message) printf("Sending the SIGUSR1 signal to %d:  ",MyServerPid);
                    351:       r=kill(MyServerPid,SIGUSR1);
                    352:       if (message) printf("Result = %d\n",r);
                    353:       fflush(NULL);
1.7       takayama  354:       /*      oxSendResultOfControlInt32(fdControl,0); */
1.1       maekawa   355:       break;
                    356:     default:
                    357:       fprintf(stderr,"[control] Unknown control message.\n");
                    358:       fprintf(stderr,"Shutdown the server.");
                    359:       myServerExit();
                    360:       break;
                    361:     }
                    362:   }
                    363: }
                    364:
                    365: void myServerExit() {
                    366:   printf("Sending the kill signal to the child.\n");
                    367:   kill(MyServerPid,SIGKILL);
1.12      takayama  368:   exit(0);
1.1       maekawa   369: }
                    370:
                    371: childServerMain(int fdControl, int fdStream) {
                    372:   int i;
                    373:   close(fdControl);   /* close(0); dup(fdStream); */
                    374:   dup2(fdStream,3);
                    375:   dup2(fdStream,4);
1.4       takayama  376:   /*close(0);
1.7       takayama  377:     #include <sys/param.h>
                    378:     for (i=5; i<NOFILE; i++) close(i);
                    379:   */
1.4       takayama  380:   if (!Do_not_use_control_stream_to_tell_no_server) {
1.7       takayama  381:     if (findOxServer(ServerName) < 0) {
                    382:       return(-1);
                    383:     }
1.4       takayama  384:   }
1.5       takayama  385:   fprintf(stderr,"childServerMain: Starting the server %s\n",ServerName); fflush(NULL);
1.14      takayama  386:
                    387:   /*
                    388:   {
                    389:        int i;
                    390:        i=0;
                    391:        while (environ[i] != NULL) {
                    392:          fprintf(stderr,"%s ",environ[i++]);
                    393:        }
                    394:        fprintf(stderr,"\n");
                    395:   }
                    396:   */
                    397:   /* bug: xterm of potato does not seem to pass the LD_LIBRARY_PATH.
                    398:       So, the new gc does not work.
                    399:      it is an workaround for OpenXM */
                    400:   if (getenv("LD_LIBRARY_PATH") == (char *)NULL) {
                    401:        char *s,*o;
                    402:     fprintf(stderr,"Hmm... LD_LIBRARY_PATH does not seem to be set.\n");
                    403:     o = getenv("OpenXM_HOME");
                    404:        if (o == NULL) {
                    405:          fprintf(stderr,"Giving up to set the LD_LIBRARY_PATH variable.\n");
                    406:        }else{
                    407:          s = (char *)malloc(strlen(o)+64);
                    408:          sprintf(s,"LD_LIBRARY_PATH=%s/lib",o);
                    409:          putenv(s);
                    410:        }
                    411:   }
                    412:
1.1       maekawa   413:   if (PacketMonitor) {
1.14      takayama  414:     if (execle(ServerName,ServerName,"-monitor",NULL,environ)) {
1.1       maekawa   415:       fprintf(stderr,"%s cannot be executed with -monitor.\n",ServerName);
1.7       takayama  416:       fflush(NULL);
                    417:       return(-1);
1.1       maekawa   418:     }
                    419:   }else {
1.14      takayama  420:     if (execle(ServerName,ServerName,NULL,environ)) {
1.1       maekawa   421:       fprintf(stderr,"%s cannot be executed.\n",ServerName);
1.7       takayama  422:       fflush(NULL);
                    423:       return(-1);
1.1       maekawa   424:     }
                    425:   }
1.4       takayama  426:   /* never reached. */
1.1       maekawa   427: }
                    428:
                    429:
                    430: /* These are dummy.  It is defined in stackmachine.c */
                    431: unlockCtrlCForOx() { ; }
                    432: restoreLockCtrlCForOx() { ; }
                    433:
1.4       takayama  434: static int findOxServer(char *server) {
                    435:   char *p;
                    436:   char *p2;
                    437:   if (strlen(server) == 0) return(-1);
1.10      takayama  438:   /* fd = open(server,O_RDONLY); */
1.11      ohara     439:   if (access(server,X_OK&R_OK) == 0) {
1.7       takayama  440:     fprintf(stderr,"Starting OX server : %s\n",server);
                    441:     return(0);
1.4       takayama  442:   }
                    443:   if (server[0] == '/') {
1.7       takayama  444:     couldNotFind(server);
                    445:     return(-1);
1.4       takayama  446:   }
                    447:   fprintf(stderr,"The server %s was not found. Trying to find it under OpenXM/bin\n",server);
                    448:   p = getenv("OpenXM_HOME");
                    449:   if (p == NULL) {
                    450:     p = "/usr/local/OpenXM";
                    451:   }
                    452:   p2 = (char *) malloc(sizeof(char)*(strlen(p)+strlen("/bin/")+3+strlen(server)));
                    453:   if (p2 == NULL) { fprintf(stderr,"No more memory.\n"); exit(10); }
                    454:   strcpy(p2,p); strcat(p2,"/bin/"); strcat(p2,server);
1.10      takayama  455:   /* fd = open(p2,O_RDONLY); */
1.11      ohara     456:   if (access(p2,X_OK&R_OK) == 0) {
1.7       takayama  457:     fprintf(stderr,"Starting OX server : %s\n",p2);
                    458:     if (strlen(p2) < SERVERNAME_SIZE) strcpy(server,p2);
                    459:     else {
                    460:       couldNotFind("Too long ox server name.");
                    461:       return(-1);
                    462:     }
                    463:     return(0);
1.4       takayama  464:   }
                    465:   couldNotFind(p2);
                    466:   return(-1);
                    467: }
1.1       maekawa   468:
1.4       takayama  469: static void couldNotFind(char *s) {
                    470:   fprintf(stderr,"OX server %s could not be found.\n",s);
                    471: }
1.1       maekawa   472:
                    473:
                    474:
                    475:

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