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