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