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

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

1.1     ! maekawa     1: /* nullserver01 */
        !             2: #include <stdio.h>
        !             3: #include <sys/types.h>
        !             4: #include <sys/socket.h>
        !             5: #include <sys/time.h>
        !             6: #include <netinet/in.h>
        !             7: #include <netdb.h>
        !             8: #include <signal.h>
        !             9: #include <setjmp.h>
        !            10: /* -lnsl -lsocket /usr/ucblib/libucb.a */
        !            11: #include "ox_kan.h"
        !            12: #include "serversm.h"
        !            13:
        !            14: #define SERVERNAME "/home/nobuki/kxx/ox_sm1"
        !            15: /*
        !            16: #define SERVERNAME "/home/nobuki/kxx/stdserver00"
        !            17: #define SERVERNAME "/home/ftp/pub/OHP/ox_asir"
        !            18: */
        !            19:
        !            20: int OxCritical = 0;
        !            21: int OxInterruptFlag = 0;
        !            22:
        !            23: int SerialCurrentControl;
        !            24:
        !            25: int MyServerPid;
        !            26: char ServerName[1024];
        !            27: int PacketMonitor = 0;
        !            28: int Quiet = 0;
        !            29:
        !            30: int LocalMode = 1;
        !            31: int NotifyPortnumber = 0;
        !            32:
        !            33:
        !            34: main(int argc, char *argv[]) {
        !            35:   int fd;
        !            36:   int size;
        !            37:   char sname[1024];
        !            38:   int tmp[1];
        !            39:   char *buf;
        !            40:   int i;
        !            41:   int fdControl = -1; int portControl = 1200;
        !            42:   int fdStream = -1;  int portStream = 1300;
        !            43:   int reverse = 0;
        !            44:   extern int OpenedSocket;
        !            45:   char portfile[1024];
        !            46:   char *pass;
        !            47:
        !            48:   strcpy(sname,"localhost");
        !            49:   strcpy(ServerName,SERVERNAME);
        !            50:   i = 1;
        !            51:   while (i<argc) {
        !            52:     if (strcmp(argv[i],"-host") == 0) {
        !            53:       i++;
        !            54:       if (i<argc) strcpy(sname,argv[i]);
        !            55:     }else if (strcmp(argv[i],"-data")==0) {
        !            56:       i++;
        !            57:       if (i<argc) sscanf(argv[i],"%d",&portStream);
        !            58:     }else if (strcmp(argv[i],"-control")==0) {
        !            59:       i++;
        !            60:       if (i<argc) sscanf(argv[i],"%d",&portControl);
        !            61:     }else if (strcmp(argv[i],"-ox") == 0) {
        !            62:       i++;
        !            63:       if (i<argc) strcpy(ServerName,argv[i]);
        !            64:     }else if (strcmp(argv[i],"-monitor") == 0) {
        !            65:       PacketMonitor = 1;
        !            66:     }else if (strcmp(argv[i],"-insecure") == 0) {
        !            67:       LocalMode = 0;
        !            68:     }else if (strcmp(argv[i],"-reverse") == 0) {
        !            69:       reverse = 1;
        !            70:     }else if (strcmp(argv[i],"-portfile") == 0) {
        !            71:       i++;
        !            72:       if (i<argc) {
        !            73:        sscanf(argv[i],"%s",portfile);
        !            74:        portControl = 0;
        !            75:        portStream = 0;
        !            76:        NotifyPortnumber = 1;
        !            77:       }
        !            78:     }else if (strcmp(argv[i],"-pass") == 0) {
        !            79:       i++;
        !            80:       if (i<argc) {
        !            81:        pass = argv[i];
        !            82:       }
        !            83:     }else {
        !            84:       fprintf(stderr,"Unknown option %s.\n",argv[i]);
        !            85:       oxmainUsage(); exit(10);
        !            86:     }
        !            87:     i++;
        !            88:   }
        !            89:
        !            90:   if (reverse) {
        !            91:     /* The order is very important. */
        !            92:     fdControl = socketConnectWithPass(sname,portControl,pass);
        !            93:     fdStream = socketConnectWithPass(sname,portStream,pass);
        !            94:
        !            95:     fprintf(stderr,"Connected: control = %d, data = %d.\n",fdControl,fdStream);
        !            96:     if (portControl != -1) {
        !            97:       MyServerPid = fork();
        !            98:       if (MyServerPid > 0 ) parentServerMain(fdControl,fdStream);
        !            99:       else childServerMain(fdControl,fdStream);
        !           100:     }else{
        !           101:       childServerMain(fdControl,fdStream);
        !           102:     }
        !           103:     /* This line will be never executed. */
        !           104:   }
        !           105:
        !           106:   /* non-reverse case. */
        !           107:   fprintf(stderr,"Hostname is %s \n",sname);
        !           108:   fprintf(stderr,"Port for data (-data) = %d \n",portStream);
        !           109:   fprintf(stderr,"Port for control message (-control) = %d \n",portControl);
        !           110:   fflush(NULL);
        !           111:
        !           112:
        !           113:   if (LocalMode) {
        !           114:     if (portControl != -1) {
        !           115:       fdControl = socketOpen(sname,portControl);
        !           116:       portControl = OpenedSocket;
        !           117:       if (NotifyPortnumber) {
        !           118:        oxWritePortFile(0,portControl,portfile);
        !           119:       }
        !           120:       fdControl = socketAcceptLocal(fdControl);
        !           121:       fprintf(stderr,"\n control port %d : Connected.\n",portControl);
        !           122:     }
        !           123:     if (portStream != -1) {
        !           124:       fdStream = socketOpen(sname,portStream);
        !           125:       portStream = OpenedSocket;
        !           126:       if (NotifyPortnumber) {
        !           127:        oxWritePortFile(1,portStream,portfile);
        !           128:       }
        !           129:       fdStream = socketAcceptLocal(fdStream);
        !           130:       fprintf(stderr,"\n stream port %d : Connected.\n",portStream);
        !           131:     }
        !           132:   }else{
        !           133:     if (portControl != -1) {
        !           134:       fdControl = socketOpen(sname,portControl);
        !           135:       portControl = OpenedSocket;
        !           136:       if (NotifyPortnumber) {
        !           137:        oxWritePortFile(0,portControl,portfile);
        !           138:       }
        !           139:       fdControl = socketAccept(fdControl);
        !           140:       fprintf(stderr,"\n control port %d : Connected.\n",portControl);
        !           141:     }
        !           142:     if (portStream != -1) {
        !           143:       fdStream = socketOpen(sname,portStream);
        !           144:       portStream = OpenedSocket;
        !           145:       if (NotifyPortnumber) {
        !           146:        oxWritePortFile(1,portStream,portfile);
        !           147:       }
        !           148:       fdStream = socketAccept(fdStream);
        !           149:       fprintf(stderr,"\n stream port %d : Connected.\n",portStream);
        !           150:     }
        !           151:   }
        !           152:
        !           153:
        !           154:
        !           155:   if (portControl != -1) {
        !           156:     MyServerPid = fork();
        !           157:     if (MyServerPid > 0 ) parentServerMain(fdControl,fdStream);
        !           158:     else childServerMain(fdControl,fdStream);
        !           159:   }else{
        !           160:     childServerMain(fdControl,fdStream);
        !           161:   }
        !           162:
        !           163: }
        !           164:
        !           165: oxmainUsage() {
        !           166:   fprintf(stderr,"Usage: \n");
        !           167:   fprintf(stderr,"  ox [-ox serverprogram -host name -data portnum -control portnum -monitor]\n");
        !           168:   fprintf(stderr," [-insecure -portfile fname -reverse -pass xxxyyyzzz]");
        !           169:   fprintf(stderr,"\n");
        !           170:   fprintf(stderr,"Example 1: dc1% ./ox -ox ox_sm1 -host dc1.math.kobe-u.ac.jp -insecure -control 1300 -data 1200\n");
        !           171:   fprintf(stderr,"           sm1>[(dc1.math.kobe-u.ac.jp) 1300 1200] oxconnect /ox.ccc set\n");
        !           172:   fprintf(stderr,"\n");
        !           173: }
        !           174:
        !           175: parentServerMain(int fdControl, int fdStream) {
        !           176:   int id;
        !           177:   int mtag;
        !           178:   int size;
        !           179:   int n;
        !           180:   int r;
        !           181:   int message = 1;
        !           182:   int controlByteOrder;
        !           183:
        !           184:   extern void myServerExit();
        !           185:
        !           186:   controlByteOrder = oxTellMyByteOrder(fdControl);
        !           187:   /* Set the network byte order. */
        !           188:   fprintf(stderr,"controlByteOrder=%x\n",controlByteOrder);
        !           189:
        !           190:
        !           191:   signal(SIGINT,myServerExit);
        !           192:   while(1) {
        !           193:     mtag = oxfdGetOXheader(fdControl,&SerialCurrentControl);
        !           194:     /* get the message_tag */
        !           195:     /* message_body */
        !           196:     id = oxfdGetInt32(fdControl);   /* get the function_id */
        !           197:     if (message) {printf("\n[control] control function_id is %d\n",id);}
        !           198:     switch( id ) {
        !           199:     case SM_control_kill:
        !           200:       if (message) printf("[control] control_kill\n");
        !           201:       oxSendResultOfControl(fdControl);
        !           202:       sleep(2);
        !           203:       myServerExit();
        !           204:       break;
        !           205:     case SM_control_reset_connection:
        !           206:       if (message) printf("[control] control_reset_connection.\n");
        !           207:       if (message) printf("Sending the SIGUSR1 signal to %d:  ",MyServerPid);
        !           208:       r=kill(MyServerPid,SIGUSR1);
        !           209:       if (message) printf("Result = %d\n",r);
        !           210:       fflush(NULL);
        !           211:       oxSendResultOfControlInt32(fdControl,0);
        !           212:       break;
        !           213:     default:
        !           214:       fprintf(stderr,"[control] Unknown control message.\n");
        !           215:       fprintf(stderr,"Shutdown the server.");
        !           216:       myServerExit();
        !           217:       break;
        !           218:     }
        !           219:   }
        !           220: }
        !           221:
        !           222: void myServerExit() {
        !           223:   printf("Sending the kill signal to the child.\n");
        !           224:   kill(MyServerPid,SIGKILL);
        !           225:   exit();
        !           226: }
        !           227:
        !           228: childServerMain(int fdControl, int fdStream) {
        !           229:   int i;
        !           230:   close(fdControl);   /* close(0); dup(fdStream); */
        !           231:   dup2(fdStream,3);
        !           232:   dup2(fdStream,4);
        !           233: /*close(0);
        !           234:   #include <sys/param.h>
        !           235:   for (i=5; i<NOFILE; i++) close(i);
        !           236: */
        !           237:   if (PacketMonitor) {
        !           238:     if (execl(ServerName,ServerName,"-monitor",NULL)) {
        !           239:       fprintf(stderr,"%s cannot be executed with -monitor.\n",ServerName);
        !           240:     }
        !           241:   }else {
        !           242:     if (execl(ServerName,ServerName,NULL)) {
        !           243:       fprintf(stderr,"%s cannot be executed.\n",ServerName);
        !           244:     }
        !           245:   }
        !           246: }
        !           247:
        !           248:
        !           249: /* These are dummy.  It is defined in stackmachine.c */
        !           250: unlockCtrlCForOx() { ; }
        !           251: restoreLockCtrlCForOx() { ; }
        !           252:
        !           253:
        !           254:
        !           255:
        !           256:
        !           257:
        !           258:

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