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