Annotation of OpenXM/src/kxx/oxmain.c, Revision 1.21
1.21 ! takayama 1: /* $OpenXM: OpenXM/src/kxx/oxmain.c,v 1.20 2004/09/17 12:32:11 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.21 ! takayama 40: static void mywait();
1.15 takayama 41:
42: void *sGC_malloc(int n) {
43: return (void *)malloc(n);
44: }
1.1 maekawa 45:
46: main(int argc, char *argv[]) {
47: int fd;
48: int size;
49: char sname[1024];
50: int tmp[1];
51: char *buf;
52: int i;
53: int fdControl = -1; int portControl = 1200;
54: int fdStream = -1; int portStream = 1300;
55: int reverse = 0;
56: extern int OpenedSocket;
57: char portfile[1024];
1.12 takayama 58: char *pass = NULL;
1.18 takayama 59: char *passControl = NULL;
60: char *passData = NULL;
1.4 takayama 61: int result;
1.9 takayama 62: int sleepingTime = 0;
1.12 takayama 63: extern int OxTerminateMode;
1.1 maekawa 64:
1.17 takayama 65: signal(SIGHUP,SIG_IGN); /* ignore x of xterm */
1.1 maekawa 66: strcpy(sname,"localhost");
67: strcpy(ServerName,SERVERNAME);
68: i = 1;
1.2 takayama 69: if (argc == 1) {
70: oxmainUsage();
1.12 takayama 71: exit(10);
1.2 takayama 72: }
1.1 maekawa 73: while (i<argc) {
74: if (strcmp(argv[i],"-host") == 0) {
75: i++;
76: if (i<argc) strcpy(sname,argv[i]);
77: }else if (strcmp(argv[i],"-data")==0) {
78: i++;
79: if (i<argc) sscanf(argv[i],"%d",&portStream);
80: }else if (strcmp(argv[i],"-control")==0) {
81: i++;
82: if (i<argc) sscanf(argv[i],"%d",&portControl);
83: }else if (strcmp(argv[i],"-ox") == 0) {
84: i++;
85: if (i<argc) strcpy(ServerName,argv[i]);
86: }else if (strcmp(argv[i],"-monitor") == 0) {
87: PacketMonitor = 1;
88: }else if (strcmp(argv[i],"-insecure") == 0) {
89: LocalMode = 0;
90: }else if (strcmp(argv[i],"-reverse") == 0) {
91: reverse = 1;
1.12 takayama 92: }else if (strcmp(argv[i],"-finish") == 0) {
93: OxTerminateMode = 1;
1.1 maekawa 94: }else if (strcmp(argv[i],"-portfile") == 0) {
95: i++;
96: if (i<argc) {
1.7 takayama 97: sscanf(argv[i],"%s",portfile);
98: portControl = 0;
99: portStream = 0;
100: NotifyPortnumber = 1;
1.1 maekawa 101: }
102: }else if (strcmp(argv[i],"-pass") == 0) {
103: i++;
104: if (i<argc) {
1.7 takayama 105: pass = argv[i];
1.1 maekawa 106: }
1.18 takayama 107: }else if (strcmp(argv[i],"-passData") == 0) {
108: i++;
109: if (i<argc) {
110: passData = argv[i];
111: }
112: }else if (strcmp(argv[i],"-passControl") == 0) {
113: i++;
114: if (i<argc) {
115: passControl = argv[i];
116: }
1.9 takayama 117: }else if (strcmp(argv[i],"-wait") == 0) {
118: i++;
119: if (i<argc) {
120: sscanf(argv[i],"%d",&sleepingTime);
121: }
1.1 maekawa 122: }else {
123: fprintf(stderr,"Unknown option %s.\n",argv[i]);
124: oxmainUsage(); exit(10);
125: }
126: i++;
127: }
128:
1.4 takayama 129: if (Do_not_use_control_stream_to_tell_no_server) {
1.7 takayama 130: if (findOxServer(ServerName) < 0) {
131: fprintf(stderr,"Sleeping five seconds...\n");
132: sleep(5);
133: exit(-1);
134: }
1.9 takayama 135: }
136:
137: if (sleepingTime) {
138: fprintf(stderr,"Waiting to connect for %d seconds...\n",sleepingTime);
139: sleep(sleepingTime);
140: fprintf(stderr,"\nTrying to connect\n");
1.4 takayama 141: }
142:
1.18 takayama 143: if ((pass != NULL) && (passData == NULL)) {
144: passData = pass;
145: }
146: if ((pass != NULL) && (passControl == NULL)) {
147: passControl = pass;
148: }
149:
1.19 takayama 150: /* Decrypt passControl and passData, here. Lookup cryptmethod. */
151:
1.1 maekawa 152: if (reverse) {
153: /* The order is very important. */
1.18 takayama 154: fdControl = socketConnectWithPass(sname,portControl,passControl);
155: fdStream = socketConnectWithPass(sname,portStream,passData);
1.1 maekawa 156:
157: fprintf(stderr,"Connected: control = %d, data = %d.\n",fdControl,fdStream);
1.7 takayama 158: result = 0;
1.5 takayama 159:
160:
1.20 takayama 161: if ((fdControl < 0) || (fdStream < 0)) {
162: fprintf(stderr,"Waiting for 10 seconds to show an error.\n");
163: sleep(10);
164: }
165:
1.1 maekawa 166: if (portControl != -1) {
167: MyServerPid = fork();
168: if (MyServerPid > 0 ) parentServerMain(fdControl,fdStream);
1.4 takayama 169: else result=childServerMain(fdControl,fdStream);
1.1 maekawa 170: }else{
1.4 takayama 171: result=childServerMain(fdControl,fdStream);
1.1 maekawa 172: }
1.4 takayama 173: /* This line will be never executed in case of success */
1.7 takayama 174: if (result < 0 ) {
175: errorToStartEngine();
176: }
1.1 maekawa 177: }
178:
179: /* non-reverse case. */
180: fprintf(stderr,"Hostname is %s \n",sname);
181: fprintf(stderr,"Port for data (-data) = %d \n",portStream);
182: fprintf(stderr,"Port for control message (-control) = %d \n",portControl);
183: fflush(NULL);
184:
185:
186: if (LocalMode) {
187: if (portControl != -1) {
188: fdControl = socketOpen(sname,portControl);
189: portControl = OpenedSocket;
190: if (NotifyPortnumber) {
1.7 takayama 191: oxWritePortFile(0,portControl,portfile);
1.1 maekawa 192: }
193: fdControl = socketAcceptLocal(fdControl);
194: fprintf(stderr,"\n control port %d : Connected.\n",portControl);
195: }
196: if (portStream != -1) {
197: fdStream = socketOpen(sname,portStream);
198: portStream = OpenedSocket;
199: if (NotifyPortnumber) {
1.7 takayama 200: oxWritePortFile(1,portStream,portfile);
1.1 maekawa 201: }
202: fdStream = socketAcceptLocal(fdStream);
203: fprintf(stderr,"\n stream port %d : Connected.\n",portStream);
204: }
205: }else{
206: if (portControl != -1) {
207: fdControl = socketOpen(sname,portControl);
208: portControl = OpenedSocket;
209: if (NotifyPortnumber) {
1.7 takayama 210: oxWritePortFile(0,portControl,portfile);
1.1 maekawa 211: }
212: fdControl = socketAccept(fdControl);
213: fprintf(stderr,"\n control port %d : Connected.\n",portControl);
214: }
215: if (portStream != -1) {
216: fdStream = socketOpen(sname,portStream);
217: portStream = OpenedSocket;
218: if (NotifyPortnumber) {
1.7 takayama 219: oxWritePortFile(1,portStream,portfile);
1.1 maekawa 220: }
221: fdStream = socketAccept(fdStream);
222: fprintf(stderr,"\n stream port %d : Connected.\n",portStream);
223: }
224: }
225:
1.19 takayama 226: if (passControl != NULL) {
227: char *s; int mm;
228: fprintf(stderr,"passControl\n");
229: mm = strlen(passControl);
230: s = (char *) malloc(mm+1);
231: if (s == NULL) {fprintf(stderr,"No more memory.\n"); exit(1); }
232: if (read(fdControl,s,mm+1) < 0) {
233: fprintf(stderr,"Read error to read passControl\n"); sleep(5); exit(1);
234: }
235: s[mm] = 0;
236: if (strcmp(s,passControl) != 0) {
237: fprintf(stderr,"s=%s and passControl=%s do not match.\n",s,passControl); sleep(5); exit(1);
238: }
239: free(s);
240: }
241: if (passData != NULL) {
242: char *s; int mm;
243: mm = strlen(passData);
244: fprintf(stderr,"passData\n");
245: s = (char *) malloc(mm+1);
246: if (s == NULL) {fprintf(stderr,"No more memory.\n"); exit(1); }
247: if (read(fdStream,s,mm+1) < 0) {
248: fprintf(stderr,"Read error to read passData\n");
249: errorToStartEngine();
250: }
251: if (strcmp(s,passData) != 0) {
252: fprintf(stderr,"s=%s and passData=%s do not match.\n",s,passData);
253: errorToStartEngine();
254: }
255: free(s);
256: }
1.1 maekawa 257:
1.20 takayama 258: if ((fdControl < 0) || (fdStream < 0)) {
259: fprintf(stderr,"Waiting for 10 seconds to show an error.\n");
260: sleep(10);
261: }
262:
263:
1.4 takayama 264: result = 0;
1.1 maekawa 265: if (portControl != -1) {
266: MyServerPid = fork();
267: if (MyServerPid > 0 ) parentServerMain(fdControl,fdStream);
1.4 takayama 268: else result = childServerMain(fdControl,fdStream);
1.1 maekawa 269: }else{
1.4 takayama 270: result = childServerMain(fdControl,fdStream);
1.1 maekawa 271: }
1.4 takayama 272: if (result < 0) errorToStartEngine();
273: }
1.1 maekawa 274:
1.4 takayama 275: static void errorToStartEngine(void) {
276: fprintf(stderr,"Failed to start the engine. Childing process is terminating.\n");
277: /* You have to tell to the control server that there is no engine.
1.7 takayama 278: And, the control server must tell the client that there is no
279: engine.
280: This part has not yet been implemented.
281: If you implement this, set Do_not_use_control_stream_to_tell_no_server to
282: zero.
283: */
1.20 takayama 284: sleep(10);
1.4 takayama 285: exit(-1);
1.1 maekawa 286: }
287:
288: oxmainUsage() {
289: fprintf(stderr,"Usage: \n");
290: fprintf(stderr," ox [-ox serverprogram -host name -data portnum -control portnum -monitor]\n");
1.18 takayama 291: fprintf(stderr," [-insecure -portfile fname -reverse -passControl xxxyyyzzz -passData pppqqqrrr]");
1.12 takayama 292: fprintf(stderr," [-finish]");
1.1 maekawa 293: fprintf(stderr,"\n");
1.2 takayama 294: fprintf(stderr,"-reverse: ox server connects to the client.\n");
1.18 takayama 295: 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 296: 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");
297: 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 298: fprintf(stderr," (The option -pass is obsolete.)\n");
1.2 takayama 299: fprintf(stderr," If -reverse is not given, the client connect to the ox server\n");
300: fprintf(stderr," See OpenXM/src/SSkan/Doc/ox.sm1, /sm1connectr\n");
301: fprintf(stderr,"-insecure : \n");
302: 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 303: fprintf(stderr,"\n");
304: 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");
305: fprintf(stderr,"\n");
1.2 takayama 306: fprintf(stderr,"Example 1:\n");
1.3 takayama 307: 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 308: fprintf(stderr,"(client): sm1\n ");
309: fprintf(stderr," (ox.sm1) run ; \n");
1.1 maekawa 310: fprintf(stderr," sm1>[(dc1.math.kobe-u.ac.jp) 1300 1200] oxconnect /ox.ccc set\n");
1.2 takayama 311: fprintf(stderr,"Example 2:\n");
312: fprintf(stderr,"(Start the ox server): dc1%% ox -ox ~/OpenXM/bin/ox_sm1\n");
313: fprintf(stderr,"(client): dc1%% sm1\n ");
314: fprintf(stderr," (ox.sm1) run ; \n");
315: fprintf(stderr," sm1>[(localhost) 1300 1200] oxconnect /ox.ccc set\n");
1.1 maekawa 316: fprintf(stderr,"\n");
317: }
318:
319: parentServerMain(int fdControl, int fdStream) {
320: int id;
321: int mtag;
322: int size;
323: int n;
324: int r;
325: int message = 1;
326: int controlByteOrder;
1.12 takayama 327: extern OxTerminateMode;
1.1 maekawa 328: extern void myServerExit();
329:
1.21 ! takayama 330: signal(SIGCHLD,mywait);
1.12 takayama 331: if (OxTerminateMode) {
332: /*
333: OxTerminateMode cannot be used if you run ox by xterm -exec ox ...
334: */
335: if (fork()) {
336: close(fdControl); close(fdStream);
337: /* Parent */
338: exit(0); /*Tell the caller that launching is successfully finished.*/
339: }
340: }
341:
1.13 takayama 342: controlByteOrder = oxTellMyByteOrder(fdControl,fdControl);
1.1 maekawa 343: /* Set the network byte order. */
344: fprintf(stderr,"controlByteOrder=%x\n",controlByteOrder);
345:
346:
347: signal(SIGINT,myServerExit);
348: while(1) {
349: mtag = oxfdGetOXheader(fdControl,&SerialCurrentControl);
350: /* get the message_tag */
351: /* message_body */
352: id = oxfdGetInt32(fdControl); /* get the function_id */
353: if (message) {printf("\n[control] control function_id is %d\n",id);}
354: switch( id ) {
355: case SM_control_kill:
356: if (message) printf("[control] control_kill\n");
357: oxSendResultOfControl(fdControl);
358: sleep(2);
359: myServerExit();
360: break;
361: case SM_control_reset_connection:
362: if (message) printf("[control] control_reset_connection.\n");
363: if (message) printf("Sending the SIGUSR1 signal to %d: ",MyServerPid);
364: r=kill(MyServerPid,SIGUSR1);
365: if (message) printf("Result = %d\n",r);
366: fflush(NULL);
1.7 takayama 367: /* oxSendResultOfControlInt32(fdControl,0); */
1.1 maekawa 368: break;
369: default:
370: fprintf(stderr,"[control] Unknown control message.\n");
371: fprintf(stderr,"Shutdown the server.");
372: myServerExit();
373: break;
374: }
375: }
376: }
377:
378: void myServerExit() {
379: printf("Sending the kill signal to the child.\n");
380: kill(MyServerPid,SIGKILL);
1.12 takayama 381: exit(0);
1.1 maekawa 382: }
383:
384: childServerMain(int fdControl, int fdStream) {
385: int i;
386: close(fdControl); /* close(0); dup(fdStream); */
387: dup2(fdStream,3);
388: dup2(fdStream,4);
1.4 takayama 389: /*close(0);
1.7 takayama 390: #include <sys/param.h>
391: for (i=5; i<NOFILE; i++) close(i);
392: */
1.4 takayama 393: if (!Do_not_use_control_stream_to_tell_no_server) {
1.7 takayama 394: if (findOxServer(ServerName) < 0) {
395: return(-1);
396: }
1.4 takayama 397: }
1.5 takayama 398: fprintf(stderr,"childServerMain: Starting the server %s\n",ServerName); fflush(NULL);
1.14 takayama 399:
400: /*
401: {
402: int i;
403: i=0;
404: while (environ[i] != NULL) {
405: fprintf(stderr,"%s ",environ[i++]);
406: }
407: fprintf(stderr,"\n");
408: }
409: */
410: /* bug: xterm of potato does not seem to pass the LD_LIBRARY_PATH.
411: So, the new gc does not work.
412: it is an workaround for OpenXM */
413: if (getenv("LD_LIBRARY_PATH") == (char *)NULL) {
414: char *s,*o;
415: fprintf(stderr,"Hmm... LD_LIBRARY_PATH does not seem to be set.\n");
416: o = getenv("OpenXM_HOME");
417: if (o == NULL) {
418: fprintf(stderr,"Giving up to set the LD_LIBRARY_PATH variable.\n");
419: }else{
420: s = (char *)malloc(strlen(o)+64);
421: sprintf(s,"LD_LIBRARY_PATH=%s/lib",o);
422: putenv(s);
423: }
424: }
425:
1.1 maekawa 426: if (PacketMonitor) {
1.14 takayama 427: if (execle(ServerName,ServerName,"-monitor",NULL,environ)) {
1.1 maekawa 428: fprintf(stderr,"%s cannot be executed with -monitor.\n",ServerName);
1.7 takayama 429: fflush(NULL);
430: return(-1);
1.1 maekawa 431: }
432: }else {
1.14 takayama 433: if (execle(ServerName,ServerName,NULL,environ)) {
1.1 maekawa 434: fprintf(stderr,"%s cannot be executed.\n",ServerName);
1.7 takayama 435: fflush(NULL);
436: return(-1);
1.1 maekawa 437: }
438: }
1.4 takayama 439: /* never reached. */
1.1 maekawa 440: }
441:
442:
443: /* These are dummy. It is defined in stackmachine.c */
444: unlockCtrlCForOx() { ; }
445: restoreLockCtrlCForOx() { ; }
446:
1.4 takayama 447: static int findOxServer(char *server) {
448: char *p;
449: char *p2;
450: if (strlen(server) == 0) return(-1);
1.10 takayama 451: /* fd = open(server,O_RDONLY); */
1.11 ohara 452: if (access(server,X_OK&R_OK) == 0) {
1.7 takayama 453: fprintf(stderr,"Starting OX server : %s\n",server);
454: return(0);
1.4 takayama 455: }
456: if (server[0] == '/') {
1.7 takayama 457: couldNotFind(server);
458: return(-1);
1.4 takayama 459: }
460: fprintf(stderr,"The server %s was not found. Trying to find it under OpenXM/bin\n",server);
461: p = getenv("OpenXM_HOME");
462: if (p == NULL) {
463: p = "/usr/local/OpenXM";
464: }
465: p2 = (char *) malloc(sizeof(char)*(strlen(p)+strlen("/bin/")+3+strlen(server)));
466: if (p2 == NULL) { fprintf(stderr,"No more memory.\n"); exit(10); }
467: strcpy(p2,p); strcat(p2,"/bin/"); strcat(p2,server);
1.10 takayama 468: /* fd = open(p2,O_RDONLY); */
1.11 ohara 469: if (access(p2,X_OK&R_OK) == 0) {
1.7 takayama 470: fprintf(stderr,"Starting OX server : %s\n",p2);
471: if (strlen(p2) < SERVERNAME_SIZE) strcpy(server,p2);
472: else {
473: couldNotFind("Too long ox server name.");
474: return(-1);
475: }
476: return(0);
1.4 takayama 477: }
478: couldNotFind(p2);
479: return(-1);
480: }
1.1 maekawa 481:
1.4 takayama 482: static void couldNotFind(char *s) {
483: fprintf(stderr,"OX server %s could not be found.\n",s);
484: }
1.1 maekawa 485:
486:
1.21 ! takayama 487: static void mywait() {
! 488: int status;
! 489: int pid;
! 490: int i,j;
! 491: /* signal(SIGCHLD,SIG_IGN); */
! 492: pid = wait(&status);
! 493: fprintf(stderr,"Control: child process %d is exiting.\n",pid);
! 494: fprintf(stderr,"Control: Shutting down the control server.\n");
! 495: sleep(2);
! 496: exit(0);
! 497: }
1.1 maekawa 498:
499:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>