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

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

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

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