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