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