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