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

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

1.20    ! takayama    1: /*  $OpenXM: OpenXM/src/kxx/oxmain.c,v 1.19 2004/09/17 08:46:10 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.20    ! takayama  160:     if ((fdControl < 0) ||  (fdStream < 0)) {
        !           161:       fprintf(stderr,"Waiting for 10 seconds to show an error.\n");
        !           162:       sleep(10);
        !           163:     }
        !           164:
1.1       maekawa   165:     if (portControl != -1) {
                    166:       MyServerPid = fork();
                    167:       if (MyServerPid > 0 ) parentServerMain(fdControl,fdStream);
1.4       takayama  168:       else result=childServerMain(fdControl,fdStream);
1.1       maekawa   169:     }else{
1.4       takayama  170:       result=childServerMain(fdControl,fdStream);
1.1       maekawa   171:     }
1.4       takayama  172:     /* This line will be never executed in case of success */
1.7       takayama  173:     if (result < 0 ) {
                    174:       errorToStartEngine();
                    175:     }
1.1       maekawa   176:   }
                    177:
                    178:   /* non-reverse case. */
                    179:   fprintf(stderr,"Hostname is %s \n",sname);
                    180:   fprintf(stderr,"Port for data (-data) = %d \n",portStream);
                    181:   fprintf(stderr,"Port for control message (-control) = %d \n",portControl);
                    182:   fflush(NULL);
                    183:
                    184:
                    185:   if (LocalMode) {
                    186:     if (portControl != -1) {
                    187:       fdControl = socketOpen(sname,portControl);
                    188:       portControl = OpenedSocket;
                    189:       if (NotifyPortnumber) {
1.7       takayama  190:         oxWritePortFile(0,portControl,portfile);
1.1       maekawa   191:       }
                    192:       fdControl = socketAcceptLocal(fdControl);
                    193:       fprintf(stderr,"\n control port %d : Connected.\n",portControl);
                    194:     }
                    195:     if (portStream != -1) {
                    196:       fdStream = socketOpen(sname,portStream);
                    197:       portStream = OpenedSocket;
                    198:       if (NotifyPortnumber) {
1.7       takayama  199:         oxWritePortFile(1,portStream,portfile);
1.1       maekawa   200:       }
                    201:       fdStream = socketAcceptLocal(fdStream);
                    202:       fprintf(stderr,"\n stream port %d : Connected.\n",portStream);
                    203:     }
                    204:   }else{
                    205:     if (portControl != -1) {
                    206:       fdControl = socketOpen(sname,portControl);
                    207:       portControl = OpenedSocket;
                    208:       if (NotifyPortnumber) {
1.7       takayama  209:         oxWritePortFile(0,portControl,portfile);
1.1       maekawa   210:       }
                    211:       fdControl = socketAccept(fdControl);
                    212:       fprintf(stderr,"\n control port %d : Connected.\n",portControl);
                    213:     }
                    214:     if (portStream != -1) {
                    215:       fdStream = socketOpen(sname,portStream);
                    216:       portStream = OpenedSocket;
                    217:       if (NotifyPortnumber) {
1.7       takayama  218:         oxWritePortFile(1,portStream,portfile);
1.1       maekawa   219:       }
                    220:       fdStream = socketAccept(fdStream);
                    221:       fprintf(stderr,"\n stream port %d : Connected.\n",portStream);
                    222:     }
                    223:   }
                    224:
1.19      takayama  225:   if (passControl != NULL) {
                    226:        char *s; int mm;
                    227:        fprintf(stderr,"passControl\n");
                    228:        mm = strlen(passControl);
                    229:        s = (char *) malloc(mm+1);
                    230:     if (s == NULL) {fprintf(stderr,"No more memory.\n"); exit(1); }
                    231:        if (read(fdControl,s,mm+1) < 0) {
                    232:          fprintf(stderr,"Read error to read passControl\n"); sleep(5); exit(1);
                    233:        }
                    234:     s[mm] = 0;
                    235:        if (strcmp(s,passControl) != 0) {
                    236:          fprintf(stderr,"s=%s and passControl=%s  do not match.\n",s,passControl); sleep(5); exit(1);
                    237:        }
                    238:        free(s);
                    239:   }
                    240:   if (passData != NULL) {
                    241:        char *s; int mm;
                    242:        mm = strlen(passData);
                    243:        fprintf(stderr,"passData\n");
                    244:        s = (char *) malloc(mm+1);
                    245:     if (s == NULL) {fprintf(stderr,"No more memory.\n"); exit(1); }
                    246:        if (read(fdStream,s,mm+1) < 0) {
                    247:          fprintf(stderr,"Read error to read passData\n");
                    248:          errorToStartEngine();
                    249:        }
                    250:        if (strcmp(s,passData) != 0) {
                    251:          fprintf(stderr,"s=%s and passData=%s  do not match.\n",s,passData);
                    252:          errorToStartEngine();
                    253:        }
                    254:     free(s);
                    255:   }
1.1       maekawa   256:
1.20    ! takayama  257:   if ((fdControl < 0) ||  (fdStream < 0)) {
        !           258:     fprintf(stderr,"Waiting for 10 seconds to show an error.\n");
        !           259:     sleep(10);
        !           260:   }
        !           261:
        !           262:
1.4       takayama  263:   result = 0;
1.1       maekawa   264:   if (portControl != -1) {
                    265:     MyServerPid = fork();
                    266:     if (MyServerPid > 0 ) parentServerMain(fdControl,fdStream);
1.4       takayama  267:     else result = childServerMain(fdControl,fdStream);
1.1       maekawa   268:   }else{
1.4       takayama  269:     result = childServerMain(fdControl,fdStream);
1.1       maekawa   270:   }
1.4       takayama  271:   if (result < 0) errorToStartEngine();
                    272: }
1.1       maekawa   273:
1.4       takayama  274: static void errorToStartEngine(void) {
                    275:   fprintf(stderr,"Failed to start the engine. Childing process is terminating.\n");
                    276:   /* You have to tell to the control server that there is no engine.
1.7       takayama  277:      And, the control server must tell the client that there is no
                    278:      engine.
                    279:      This part has not yet been implemented.
                    280:      If you implement this, set Do_not_use_control_stream_to_tell_no_server to
                    281:      zero.
                    282:   */
1.20    ! takayama  283:   sleep(10);
1.4       takayama  284:   exit(-1);
1.1       maekawa   285: }
                    286:
                    287: oxmainUsage() {
                    288:   fprintf(stderr,"Usage: \n");
                    289:   fprintf(stderr,"  ox [-ox serverprogram -host name -data portnum -control portnum -monitor]\n");
1.18      takayama  290:   fprintf(stderr," [-insecure -portfile fname -reverse -passControl xxxyyyzzz -passData pppqqqrrr]");
1.12      takayama  291:   fprintf(stderr," [-finish]");
1.1       maekawa   292:   fprintf(stderr,"\n");
1.2       takayama  293:   fprintf(stderr,"-reverse: ox server connects to the client.\n");
1.18      takayama  294:   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  295:   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");
                    296:   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  297:   fprintf(stderr,"          (The option -pass is obsolete.)\n");
1.2       takayama  298:   fprintf(stderr,"          If -reverse is not given, the client connect to the ox server\n");
                    299:   fprintf(stderr,"          See OpenXM/src/SSkan/Doc/ox.sm1, /sm1connectr\n");
                    300:   fprintf(stderr,"-insecure : \n");
                    301:   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  302:   fprintf(stderr,"\n");
                    303:   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");
                    304:   fprintf(stderr,"\n");
1.2       takayama  305:   fprintf(stderr,"Example 1:\n");
1.3       takayama  306:   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  307:   fprintf(stderr,"(client):  sm1\n ");
                    308:   fprintf(stderr,"           (ox.sm1) run ; \n");
1.1       maekawa   309:   fprintf(stderr,"           sm1>[(dc1.math.kobe-u.ac.jp) 1300 1200] oxconnect /ox.ccc set\n");
1.2       takayama  310:   fprintf(stderr,"Example 2:\n");
                    311:   fprintf(stderr,"(Start the ox server): dc1%% ox -ox ~/OpenXM/bin/ox_sm1\n");
                    312:   fprintf(stderr,"(client): dc1%% sm1\n ");
                    313:   fprintf(stderr,"           (ox.sm1) run ; \n");
                    314:   fprintf(stderr,"           sm1>[(localhost) 1300 1200] oxconnect /ox.ccc set\n");
1.1       maekawa   315:   fprintf(stderr,"\n");
                    316: }
                    317:
                    318: parentServerMain(int fdControl, int fdStream) {
                    319:   int id;
                    320:   int mtag;
                    321:   int size;
                    322:   int n;
                    323:   int r;
                    324:   int message = 1;
                    325:   int controlByteOrder;
1.12      takayama  326:   extern OxTerminateMode;
1.1       maekawa   327:   extern void myServerExit();
                    328:
1.12      takayama  329:   if (OxTerminateMode) {
                    330:        /*
                    331:          OxTerminateMode cannot be used if you run ox by xterm -exec ox ...
                    332:         */
                    333:        if (fork()) {
                    334:          close(fdControl); close(fdStream);
                    335:          /* Parent */
                    336:          exit(0);  /*Tell the caller that launching is successfully finished.*/
                    337:        }
                    338:   }
                    339:
1.13      takayama  340:   controlByteOrder = oxTellMyByteOrder(fdControl,fdControl);
1.1       maekawa   341:   /* Set the network byte order. */
                    342:   fprintf(stderr,"controlByteOrder=%x\n",controlByteOrder);
                    343:
                    344:
                    345:   signal(SIGINT,myServerExit);
                    346:   while(1) {
                    347:     mtag = oxfdGetOXheader(fdControl,&SerialCurrentControl);
                    348:     /* get the message_tag */
                    349:     /* message_body */
                    350:     id = oxfdGetInt32(fdControl);   /* get the function_id */
                    351:     if (message) {printf("\n[control] control function_id is %d\n",id);}
                    352:     switch( id ) {
                    353:     case SM_control_kill:
                    354:       if (message) printf("[control] control_kill\n");
                    355:       oxSendResultOfControl(fdControl);
                    356:       sleep(2);
                    357:       myServerExit();
                    358:       break;
                    359:     case SM_control_reset_connection:
                    360:       if (message) printf("[control] control_reset_connection.\n");
                    361:       if (message) printf("Sending the SIGUSR1 signal to %d:  ",MyServerPid);
                    362:       r=kill(MyServerPid,SIGUSR1);
                    363:       if (message) printf("Result = %d\n",r);
                    364:       fflush(NULL);
1.7       takayama  365:       /*      oxSendResultOfControlInt32(fdControl,0); */
1.1       maekawa   366:       break;
                    367:     default:
                    368:       fprintf(stderr,"[control] Unknown control message.\n");
                    369:       fprintf(stderr,"Shutdown the server.");
                    370:       myServerExit();
                    371:       break;
                    372:     }
                    373:   }
                    374: }
                    375:
                    376: void myServerExit() {
                    377:   printf("Sending the kill signal to the child.\n");
                    378:   kill(MyServerPid,SIGKILL);
1.12      takayama  379:   exit(0);
1.1       maekawa   380: }
                    381:
                    382: childServerMain(int fdControl, int fdStream) {
                    383:   int i;
                    384:   close(fdControl);   /* close(0); dup(fdStream); */
                    385:   dup2(fdStream,3);
                    386:   dup2(fdStream,4);
1.4       takayama  387:   /*close(0);
1.7       takayama  388:     #include <sys/param.h>
                    389:     for (i=5; i<NOFILE; i++) close(i);
                    390:   */
1.4       takayama  391:   if (!Do_not_use_control_stream_to_tell_no_server) {
1.7       takayama  392:     if (findOxServer(ServerName) < 0) {
                    393:       return(-1);
                    394:     }
1.4       takayama  395:   }
1.5       takayama  396:   fprintf(stderr,"childServerMain: Starting the server %s\n",ServerName); fflush(NULL);
1.14      takayama  397:
                    398:   /*
                    399:   {
                    400:        int i;
                    401:        i=0;
                    402:        while (environ[i] != NULL) {
                    403:          fprintf(stderr,"%s ",environ[i++]);
                    404:        }
                    405:        fprintf(stderr,"\n");
                    406:   }
                    407:   */
                    408:   /* bug: xterm of potato does not seem to pass the LD_LIBRARY_PATH.
                    409:       So, the new gc does not work.
                    410:      it is an workaround for OpenXM */
                    411:   if (getenv("LD_LIBRARY_PATH") == (char *)NULL) {
                    412:        char *s,*o;
                    413:     fprintf(stderr,"Hmm... LD_LIBRARY_PATH does not seem to be set.\n");
                    414:     o = getenv("OpenXM_HOME");
                    415:        if (o == NULL) {
                    416:          fprintf(stderr,"Giving up to set the LD_LIBRARY_PATH variable.\n");
                    417:        }else{
                    418:          s = (char *)malloc(strlen(o)+64);
                    419:          sprintf(s,"LD_LIBRARY_PATH=%s/lib",o);
                    420:          putenv(s);
                    421:        }
                    422:   }
                    423:
1.1       maekawa   424:   if (PacketMonitor) {
1.14      takayama  425:     if (execle(ServerName,ServerName,"-monitor",NULL,environ)) {
1.1       maekawa   426:       fprintf(stderr,"%s cannot be executed with -monitor.\n",ServerName);
1.7       takayama  427:       fflush(NULL);
                    428:       return(-1);
1.1       maekawa   429:     }
                    430:   }else {
1.14      takayama  431:     if (execle(ServerName,ServerName,NULL,environ)) {
1.1       maekawa   432:       fprintf(stderr,"%s cannot be executed.\n",ServerName);
1.7       takayama  433:       fflush(NULL);
                    434:       return(-1);
1.1       maekawa   435:     }
                    436:   }
1.4       takayama  437:   /* never reached. */
1.1       maekawa   438: }
                    439:
                    440:
                    441: /* These are dummy.  It is defined in stackmachine.c */
                    442: unlockCtrlCForOx() { ; }
                    443: restoreLockCtrlCForOx() { ; }
                    444:
1.4       takayama  445: static int findOxServer(char *server) {
                    446:   char *p;
                    447:   char *p2;
                    448:   if (strlen(server) == 0) return(-1);
1.10      takayama  449:   /* fd = open(server,O_RDONLY); */
1.11      ohara     450:   if (access(server,X_OK&R_OK) == 0) {
1.7       takayama  451:     fprintf(stderr,"Starting OX server : %s\n",server);
                    452:     return(0);
1.4       takayama  453:   }
                    454:   if (server[0] == '/') {
1.7       takayama  455:     couldNotFind(server);
                    456:     return(-1);
1.4       takayama  457:   }
                    458:   fprintf(stderr,"The server %s was not found. Trying to find it under OpenXM/bin\n",server);
                    459:   p = getenv("OpenXM_HOME");
                    460:   if (p == NULL) {
                    461:     p = "/usr/local/OpenXM";
                    462:   }
                    463:   p2 = (char *) malloc(sizeof(char)*(strlen(p)+strlen("/bin/")+3+strlen(server)));
                    464:   if (p2 == NULL) { fprintf(stderr,"No more memory.\n"); exit(10); }
                    465:   strcpy(p2,p); strcat(p2,"/bin/"); strcat(p2,server);
1.10      takayama  466:   /* fd = open(p2,O_RDONLY); */
1.11      ohara     467:   if (access(p2,X_OK&R_OK) == 0) {
1.7       takayama  468:     fprintf(stderr,"Starting OX server : %s\n",p2);
                    469:     if (strlen(p2) < SERVERNAME_SIZE) strcpy(server,p2);
                    470:     else {
                    471:       couldNotFind("Too long ox server name.");
                    472:       return(-1);
                    473:     }
                    474:     return(0);
1.4       takayama  475:   }
                    476:   couldNotFind(p2);
                    477:   return(-1);
                    478: }
1.1       maekawa   479:
1.4       takayama  480: static void couldNotFind(char *s) {
                    481:   fprintf(stderr,"OX server %s could not be found.\n",s);
                    482: }
1.1       maekawa   483:
                    484:
                    485:
                    486:

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