Annotation of OpenXM_contrib2/asir2000/io/ox_launch.c, Revision 1.21
1.3 noro 1: /*
2: * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
3: * All rights reserved.
4: *
5: * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
6: * non-exclusive and royalty-free license to use, copy, modify and
7: * redistribute, solely for non-commercial and non-profit purposes, the
8: * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
9: * conditions of this Agreement. For the avoidance of doubt, you acquire
10: * only a limited right to use the SOFTWARE hereunder, and FLL or any
11: * third party developer retains all rights, including but not limited to
12: * copyrights, in and to the SOFTWARE.
13: *
14: * (1) FLL does not grant you a license in any way for commercial
15: * purposes. You may use the SOFTWARE only for non-commercial and
16: * non-profit purposes only, such as academic, research and internal
17: * business use.
18: * (2) The SOFTWARE is protected by the Copyright Law of Japan and
19: * international copyright treaties. If you make copies of the SOFTWARE,
20: * with or without modification, as permitted hereunder, you shall affix
21: * to all such copies of the SOFTWARE the above copyright notice.
22: * (3) An explicit reference to this SOFTWARE and its copyright owner
23: * shall be made on your publication or presentation in any form of the
24: * results obtained by use of the SOFTWARE.
25: * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
1.4 noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.3 noro 27: * for such modification or the source code of the modified part of the
28: * SOFTWARE.
29: *
30: * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
31: * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
32: * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
33: * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
34: * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
35: * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
36: * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
37: * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
38: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
39: * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
40: * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
41: * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
42: * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
43: * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
44: * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
45: * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
46: * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
1.21 ! noro 47: * $OpenXM: OpenXM_contrib2/asir2000/io/ox_launch.c,v 1.20 2004/06/15 09:04:41 noro Exp $
1.3 noro 48: */
1.2 noro 49: #include <setjmp.h>
50: #include <signal.h>
51: #include <fcntl.h>
1.1 noro 52: #include "ca.h"
53: #include "com.h"
54: #include "ox.h"
55: #if defined(VISUAL)
1.7 noro 56: #include <stdlib.h>
1.1 noro 57: #include <windows.h>
58: #include <io.h>
59: #include <fcntl.h>
60: #include <process.h>
61: #else
62: #include <sys/file.h>
63: #include <sys/types.h>
64: #include <sys/stat.h>
65: #include <pwd.h>
66: #endif
67:
68: #if defined(SYSV)
69: #include <unistd.h>
70: #endif
71:
72: #if defined(VISUAL)
73: HANDLE hIntr,hReset,hKill;
74: #endif
75:
76: static void put_log(char *);
1.21 ! noro 77: static int ox_spawn(char *,int,char *,char *);
1.1 noro 78: static void launch_error(char *);
1.15 noro 79: static void ox_io_init(int);
1.1 noro 80: static void push_one(Obj);
81: static Obj pop_one();
82: static void do_cmd(int);
1.13 noro 83: static void terminate_server();
1.1 noro 84:
85: static Obj *asir_OperandStack;
86: static int asir_OperandStackPtr;
87:
88: static char hostname[BUFSIZ];
89: static int sindex;
90: static struct sockaddr_in peer;
91: static int cpid;
92:
93: static void put_log(str)
94: char *str;
95: {
96: static FILE *logfile;
97:
98: if ( !logfile )
99: logfile = fopen("/tmp/ox_log","w");
100: fprintf(logfile,"%s\n",str);
101: fflush(logfile);
102: }
103:
104: /*
105: argv[1] : host to connect
106: argv[2] : if 1, call try_bind_listen and try_accept
107: if 0, call try_connect
108: argv[3] : control_port
109: argv[4] : server_port
110: argv[5] : server
111: argv[6] : display or "0"
1.10 noro 112: argv[7] : if exists, it should be "-nolog"
1.21 ! noro 113: XXX : argv[7] is used to pass the path of engine.exe in Windows.
1.1 noro 114: */
115:
116: void launch_main(argc,argv)
117: int argc;
118: char **argv;
119: {
1.15 noro 120: #if !defined(VISUAL)
121: Obj p;
1.1 noro 122: char *name;
123: char buf[BUFSIZ];
1.15 noro 124: #endif
125: int id;
126: Obj obj;
1.1 noro 127: int cs,ss;
128: unsigned int cmd;
1.21 ! noro 129: int use_unix,accept_client;
1.1 noro 130: char *control_port_str,*server_port_str;
1.21 ! noro 131: char *rhost,*server,*dname,*nolog;
1.18 noro 132: char *e,*s;
1.1 noro 133:
134: GC_init(); nglob_init();
135: gethostname(hostname,BUFSIZ);
136: rhost = argv[1];
137: use_unix = !strcmp(rhost,".") ? 1 : 0;
138: accept_client = atoi(argv[2]) ? 1 : 0;
139: control_port_str = argv[3];
140: server_port_str = argv[4];
141: server = argv[5];
142: dname = argv[6];
1.21 ! noro 143: nolog = argc > 7 ? argv[7]: 0;
1.1 noro 144:
145: #if defined(VISUAL)
146: init_socket();
147: #endif
148:
149: signal(SIGINT,SIG_IGN);
1.19 noro 150: #if defined(SIGHUP)
1.17 noro 151: signal(SIGHUP,SIG_IGN);
1.19 noro 152: #endif
1.1 noro 153: #if defined(SIGUSR1)
154: signal(SIGUSR1,SIG_IGN);
155: #endif
1.13 noro 156: #if defined(SIGTERM)
157: signal(SIGTERM,terminate_server);
158: #endif
1.18 noro 159:
160: /* XXX a dirty hack */
161: if ( !getenv("LD_LIBRARY_PATH") ) {
162: if ( e = getenv("OpenXM_HOME") ) {
163: s = (char *)alloca(strlen(e)+100);
164: sprintf(s,"LD_LIBRARY_PATH=%s/lib",e);
165: putenv(s);
166: }
167: }
1.1 noro 168:
169: if ( accept_client ) {
170: cs = try_bind_listen(use_unix,control_port_str);
171: ss = try_bind_listen(use_unix,server_port_str);
172: cs = try_accept(use_unix,cs);
173: ss = try_accept(use_unix,ss);
174: } else {
175: cs = try_connect(use_unix,rhost,control_port_str);
176: ss = try_connect(use_unix,rhost,server_port_str);
177: }
178: ox_io_init(cs);
179: if ( cs < 0 || ss < 0 )
180: launch_error("cannot connect to the client");
1.10 noro 181: cpid = ox_spawn(server,ss,dname,nolog);
1.1 noro 182:
183: while ( 1 ) {
184: ox_recv(sindex,&id,&obj);
185: switch ( id ) {
186: case OX_COMMAND:
187: cmd = ((USINT)obj)->body;
188: do_cmd(cmd);
189: break;
190: case OX_DATA:
191: push_one(obj);
192: break;
193: case OX_SYNC_BALL:
194: break;
195: default:
196: break;
197: }
198: }
199: }
200:
201: #if defined(VISUAL)
202: static void do_cmd(cmd)
203: int cmd;
204: {
205: USINT t;
206:
207: switch ( cmd ) {
208: case SM_shutdown:
209: SetEvent(hKill);
1.8 noro 210: ExitProcess(0);
211: break;
1.1 noro 212: case SM_control_intr:
213: SetEvent(hIntr);
214: break;
215: case SM_control_kill:
216: SetEvent(hKill);
217: break;
218: case SM_control_reset_connection:
219: MKUSINT(t,0);
220: ox_send_data(sindex,t);
221: SetEvent(hReset);
222: break;
223: default:
224: break;
225: }
226: }
227: #else
228: static void do_cmd(cmd)
229: int cmd;
230: {
231: USINT t;
232: int id,cindex;
233: int bport,sport;
234: int bs,bs0;
235: int status;
236: STRING prog,dname;
237:
238: switch ( cmd ) {
239: case SM_shutdown:
240: kill(cpid,SIGKILL);
241: exit(0); break;
242: case SM_control_intr:
243: kill(cpid,SIGINT);
244: break;
245: case SM_control_kill:
246: kill(cpid,SIGKILL);
247: break;
248: case SM_control_reset_connection:
249: MKUSINT(t,0);
250: ox_send_data(sindex,t);
251: kill(cpid,SIGUSR1);
252: break;
253: default:
254: break;
255: }
256: }
257: #endif
258:
1.10 noro 259: static int ox_spawn(prog,bs,dname,nolog)
1.1 noro 260: char *prog;
261: int bs;
262: char *dname;
1.21 ! noro 263: char *nolog;
1.1 noro 264: {
265: #if defined(VISUAL)
266: char *av[BUFSIZ];
267: char sock_id[BUFSIZ],ox_intr[BUFSIZ],ox_reset[BUFSIZ],ox_kill[BUFSIZ];
268: char AsirExe[BUFSIZ];
1.15 noro 269: int hProc;
1.1 noro 270: STRING rootdir;
271: int mypid;
1.7 noro 272: int newbs;
1.1 noro 273:
1.7 noro 274: DuplicateHandle(GetCurrentProcess(),(HANDLE)bs,
275: GetCurrentProcess(),(HANDLE *)&newbs,
276: 0,TRUE,DUPLICATE_SAME_ACCESS);
277: close(bs);
278: bs = newbs;
279:
1.1 noro 280: mypid = GetCurrentProcessId();
281: sprintf(ox_intr,"ox_intr_%d",mypid);
282: sprintf(ox_reset,"ox_reset_%d",mypid);
283: sprintf(ox_kill,"ox_kill_%d",mypid);
284: hIntr = CreateEvent(NULL,TRUE,FALSE,ox_intr);
285: hReset = CreateEvent(NULL,TRUE,FALSE,ox_reset);
286: hKill = CreateEvent(NULL,TRUE,FALSE,ox_kill);
287: sprintf(sock_id,"%d",bs);
288: av[0] = prog;
289: av[1] = sock_id;
1.7 noro 290: av[2] = dname; /* if dname == "1" then a message window is opened */
291: av[3] = ox_intr;
292: av[4] = ox_reset;
293: av[5] = ox_kill;
294: av[6] = NULL;
1.21 ! noro 295: if ( nolog ) {
! 296: sprintf(AsirExe,"\"%s\"",nolog);
! 297: strcpy(AsirExe,nolog);
! 298: } else {
! 299: Pget_rootdir(&rootdir);
! 300: sprintf(AsirExe,"%s\\bin\\engine.exe",BDY(rootdir));
! 301: }
1.7 noro 302: _fileinfo = 1;
1.1 noro 303: hProc = _spawnv(_P_NOWAIT,AsirExe,av);
304: return (int)hProc;
305: #else /* VISUAL */
306: int b,s,i;
307: struct hostent *hp;
308: int pid;
309: char wname[BUFSIZ];
310: char buf[BUFSIZ];
1.5 noro 311: char errcode;
1.1 noro 312:
313: pid = fork();
1.17 noro 314: if ( pid ) {
1.1 noro 315: return pid;
1.17 noro 316: } else {
1.1 noro 317: setpgid(0,getpid());
1.16 noro 318: if ( bs != 3 && dup2(bs,3) != 3 )
1.1 noro 319: exit(1);
1.16 noro 320: if ( bs != 4 && dup2(bs,4) != 4 )
1.1 noro 321: exit(1);
322: {
1.16 noro 323: #if defined(linux) || defined(__NeXT__) || defined(ultrix) || defined(__CYGWIN__)
1.1 noro 324: #include <sys/param.h>
325: close(0);
326: for ( i = 5; i < NOFILE; i++ )
327: close(i);
328: #else
329: #include <sys/resource.h>
330: struct rlimit rl;
331:
332: getrlimit(RLIMIT_NOFILE,&rl);
333: close(0);
334: for ( i = 5; i < rl.rlim_cur; i++ )
335: close(i);
336: #endif
337: }
1.12 noro 338: if ( !nolog && !strcmp(dname,"1" ) ) /* XXX: for ssh */
1.6 noro 339: execl(prog,prog,0);
1.10 noro 340: else if ( nolog || !strcmp(dname,"0") ) {
1.1 noro 341: FILE *null;
342:
343: null = fopen("/dev/null","wb");
344: dup2(fileno(null),1);
345: dup2(fileno(null),2);
1.12 noro 346: if ( !strcmp(dname,"1") ) {
347: /* XXX */
348: putenv("DISPLAY=");
349: execl(prog,prog,0);
350: } else if ( strcmp(dname,"0") )
1.11 noro 351: execl(prog,prog,"-display",dname,0);
352: else {
1.10 noro 353: putenv("DISPLAY=");
354: execl(prog,prog,0);
1.11 noro 355: }
1.6 noro 356: } else
357: execl(prog,prog,"-display",dname,0);
1.5 noro 358: /* On failure */
359: errcode = 2;
360: write(4,&errcode,1);
361: exit(1);
1.1 noro 362: }
363: #endif
364: }
365:
366: static void launch_error(s)
367: char *s;
368: {
369: exit(0);
370: }
371:
1.15 noro 372: static void ox_io_init(sock)
1.1 noro 373: int sock;
374: {
375: endian_init();
376: /* server mode */
377: sindex = get_iofp(sock,0,1);
378: asir_OperandStack = (Obj *)CALLOC(BUFSIZ,sizeof(Obj));
379: asir_OperandStackPtr = -1;
380: }
381:
382: static void push_one(obj)
383: Obj obj;
384: {
385: if ( !obj || OID(obj) != O_VOID )
386: asir_OperandStack[++asir_OperandStackPtr] = obj;
387: }
388:
389: static Obj pop_one() {
390: if ( asir_OperandStackPtr >= 0 ) {
391: return asir_OperandStack[asir_OperandStackPtr--];
392: }
1.13 noro 393: }
394:
1.15 noro 395: static void terminate_server(int sig)
1.13 noro 396: {
1.14 noro 397: #if defined(SIGKILL)
1.13 noro 398: kill(cpid,SIGKILL);
1.14 noro 399: #endif
1.13 noro 400: exit(0);
1.1 noro 401: }
402:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>