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