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