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

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

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

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