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