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