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