Annotation of OpenXM_contrib2/asir2000/io/ox_launch.c, Revision 1.26
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.26 ! fujimoto 47: * $OpenXM: OpenXM_contrib2/asir2000/io/ox_launch.c,v 1.25 2015/08/06 10:01:52 fujimoto 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"
1.26 ! fujimoto 55: #if defined(VISUAL) || defined(__MINGW32__)
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:
1.26 ! fujimoto 72: #if defined(VISUAL) || defined(__MINGW32__)
1.1 noro 73: HANDLE hIntr,hReset,hKill;
1.25 fujimoto 74: extern int _fileinfo;
1.1 noro 75: #endif
76:
77: static void put_log(char *);
1.21 noro 78: static int ox_spawn(char *,int,char *,char *);
1.1 noro 79: static void launch_error(char *);
1.15 noro 80: static void ox_io_init(int);
1.1 noro 81: static void push_one(Obj);
82: static Obj pop_one();
83: static void do_cmd(int);
1.13 noro 84: static void terminate_server();
1.1 noro 85:
86: static Obj *asir_OperandStack;
87: static int asir_OperandStackPtr;
88:
89: static char hostname[BUFSIZ];
90: static int sindex;
91: static struct sockaddr_in peer;
92: static int cpid;
93:
94: static void put_log(str)
95: char *str;
96: {
97: static FILE *logfile;
98:
99: if ( !logfile )
100: logfile = fopen("/tmp/ox_log","w");
101: fprintf(logfile,"%s\n",str);
102: fflush(logfile);
103: }
104:
105: /*
106: argv[1] : host to connect
107: argv[2] : if 1, call try_bind_listen and try_accept
108: if 0, call try_connect
109: argv[3] : control_port
110: argv[4] : server_port
111: argv[5] : server
112: argv[6] : display or "0"
1.10 noro 113: argv[7] : if exists, it should be "-nolog"
1.21 noro 114: XXX : argv[7] is used to pass the path of engine.exe in Windows.
1.1 noro 115: */
116:
117: void launch_main(argc,argv)
118: int argc;
119: char **argv;
120: {
1.26 ! fujimoto 121: #if !defined(VISUAL) && !defined(__MINGW32__)
1.15 noro 122: Obj p;
1.1 noro 123: char *name;
124: char buf[BUFSIZ];
1.15 noro 125: #endif
126: int id;
127: Obj obj;
1.1 noro 128: int cs,ss;
129: unsigned int cmd;
1.21 noro 130: int use_unix,accept_client;
1.1 noro 131: char *control_port_str,*server_port_str;
1.21 noro 132: char *rhost,*server,*dname,*nolog;
1.18 noro 133: char *e,*s;
1.1 noro 134:
135: GC_init(); nglob_init();
136: gethostname(hostname,BUFSIZ);
137: rhost = argv[1];
138: use_unix = !strcmp(rhost,".") ? 1 : 0;
139: accept_client = atoi(argv[2]) ? 1 : 0;
140: control_port_str = argv[3];
141: server_port_str = argv[4];
142: server = argv[5];
143: dname = argv[6];
1.21 noro 144: nolog = argc > 7 ? argv[7]: 0;
1.1 noro 145:
1.26 ! fujimoto 146: #if defined(VISUAL) || defined(__MINGW32__)
1.1 noro 147: init_socket();
148: #endif
149:
150: signal(SIGINT,SIG_IGN);
1.19 noro 151: #if defined(SIGHUP)
1.17 noro 152: signal(SIGHUP,SIG_IGN);
1.19 noro 153: #endif
1.1 noro 154: #if defined(SIGUSR1)
155: signal(SIGUSR1,SIG_IGN);
156: #endif
1.13 noro 157: #if defined(SIGTERM)
158: signal(SIGTERM,terminate_server);
159: #endif
1.18 noro 160:
161: /* XXX a dirty hack */
162: if ( !getenv("LD_LIBRARY_PATH") ) {
163: if ( e = getenv("OpenXM_HOME") ) {
164: s = (char *)alloca(strlen(e)+100);
165: sprintf(s,"LD_LIBRARY_PATH=%s/lib",e);
166: putenv(s);
167: }
168: }
1.1 noro 169:
170: if ( accept_client ) {
171: cs = try_bind_listen(use_unix,control_port_str);
172: ss = try_bind_listen(use_unix,server_port_str);
173: cs = try_accept(use_unix,cs);
174: ss = try_accept(use_unix,ss);
175: } else {
176: cs = try_connect(use_unix,rhost,control_port_str);
177: ss = try_connect(use_unix,rhost,server_port_str);
178: }
179: ox_io_init(cs);
180: if ( cs < 0 || ss < 0 )
181: launch_error("cannot connect to the client");
1.10 noro 182: cpid = ox_spawn(server,ss,dname,nolog);
1.1 noro 183:
184: while ( 1 ) {
185: ox_recv(sindex,&id,&obj);
186: switch ( id ) {
187: case OX_COMMAND:
188: cmd = ((USINT)obj)->body;
189: do_cmd(cmd);
190: break;
191: case OX_DATA:
192: push_one(obj);
193: break;
194: case OX_SYNC_BALL:
195: break;
196: default:
197: break;
198: }
199: }
200: }
201:
1.26 ! fujimoto 202: #if defined(VISUAL) || defined(__MINGW32__)
1.1 noro 203: static void do_cmd(cmd)
204: int cmd;
205: {
206: USINT t;
207:
208: switch ( cmd ) {
209: case SM_shutdown:
210: SetEvent(hKill);
1.8 noro 211: ExitProcess(0);
212: break;
1.1 noro 213: case SM_control_intr:
214: SetEvent(hIntr);
215: break;
216: case SM_control_kill:
217: SetEvent(hKill);
218: break;
219: case SM_control_reset_connection:
220: MKUSINT(t,0);
221: ox_send_data(sindex,t);
222: SetEvent(hReset);
223: break;
224: default:
225: break;
226: }
227: }
228: #else
229: static void do_cmd(cmd)
230: int cmd;
231: {
232: USINT t;
233: int id,cindex;
234: int bport,sport;
235: int bs,bs0;
236: int status;
237: STRING prog,dname;
238:
239: switch ( cmd ) {
240: case SM_shutdown:
241: kill(cpid,SIGKILL);
242: exit(0); break;
243: case SM_control_intr:
244: kill(cpid,SIGINT);
245: break;
246: case SM_control_kill:
247: kill(cpid,SIGKILL);
248: break;
249: case SM_control_reset_connection:
250: MKUSINT(t,0);
251: ox_send_data(sindex,t);
252: kill(cpid,SIGUSR1);
253: break;
254: default:
255: break;
256: }
257: }
258: #endif
259:
1.10 noro 260: static int ox_spawn(prog,bs,dname,nolog)
1.1 noro 261: char *prog;
262: int bs;
263: char *dname;
1.21 noro 264: char *nolog;
1.1 noro 265: {
1.26 ! fujimoto 266: #if defined(VISUAL) || defined(__MINGW32__)
1.1 noro 267: char *av[BUFSIZ];
268: char sock_id[BUFSIZ],ox_intr[BUFSIZ],ox_reset[BUFSIZ],ox_kill[BUFSIZ];
269: char AsirExe[BUFSIZ];
1.15 noro 270: int hProc;
1.1 noro 271: STRING rootdir;
272: int mypid;
1.7 noro 273: int newbs;
1.1 noro 274:
1.7 noro 275: DuplicateHandle(GetCurrentProcess(),(HANDLE)bs,
276: GetCurrentProcess(),(HANDLE *)&newbs,
277: 0,TRUE,DUPLICATE_SAME_ACCESS);
1.24 ohara 278: closesocket(bs);
1.7 noro 279: bs = newbs;
280:
1.1 noro 281: mypid = GetCurrentProcessId();
282: sprintf(ox_intr,"ox_intr_%d",mypid);
283: sprintf(ox_reset,"ox_reset_%d",mypid);
284: sprintf(ox_kill,"ox_kill_%d",mypid);
285: hIntr = CreateEvent(NULL,TRUE,FALSE,ox_intr);
286: hReset = CreateEvent(NULL,TRUE,FALSE,ox_reset);
287: hKill = CreateEvent(NULL,TRUE,FALSE,ox_kill);
288: sprintf(sock_id,"%d",bs);
289: av[0] = prog;
290: av[1] = sock_id;
1.7 noro 291: av[2] = dname; /* if dname == "1" then a message window is opened */
292: av[3] = ox_intr;
293: av[4] = ox_reset;
294: av[5] = ox_kill;
295: av[6] = NULL;
1.21 noro 296: if ( nolog ) {
297: sprintf(AsirExe,"\"%s\"",nolog);
298: strcpy(AsirExe,nolog);
299: } else {
300: Pget_rootdir(&rootdir);
301: sprintf(AsirExe,"%s\\bin\\engine.exe",BDY(rootdir));
302: }
1.22 ohara 303: #if _MSC_VER < 1400
1.7 noro 304: _fileinfo = 1;
1.22 ohara 305: #endif
1.1 noro 306: hProc = _spawnv(_P_NOWAIT,AsirExe,av);
307: return (int)hProc;
308: #else /* VISUAL */
309: int b,s,i;
310: struct hostent *hp;
311: int pid;
312: char wname[BUFSIZ];
313: char buf[BUFSIZ];
1.5 noro 314: char errcode;
1.1 noro 315:
316: pid = fork();
1.17 noro 317: if ( pid ) {
1.1 noro 318: return pid;
1.17 noro 319: } else {
1.1 noro 320: setpgid(0,getpid());
1.16 noro 321: if ( bs != 3 && dup2(bs,3) != 3 )
1.1 noro 322: exit(1);
1.16 noro 323: if ( bs != 4 && dup2(bs,4) != 4 )
1.1 noro 324: exit(1);
325: {
1.16 noro 326: #if defined(linux) || defined(__NeXT__) || defined(ultrix) || defined(__CYGWIN__)
1.1 noro 327: #include <sys/param.h>
328: close(0);
329: for ( i = 5; i < NOFILE; i++ )
330: close(i);
331: #else
332: #include <sys/resource.h>
333: struct rlimit rl;
334:
335: getrlimit(RLIMIT_NOFILE,&rl);
336: close(0);
337: for ( i = 5; i < rl.rlim_cur; i++ )
338: close(i);
339: #endif
340: }
1.12 noro 341: if ( !nolog && !strcmp(dname,"1" ) ) /* XXX: for ssh */
1.23 noro 342: execl(prog,prog,(char *)0);
1.10 noro 343: else if ( nolog || !strcmp(dname,"0") ) {
1.1 noro 344: FILE *null;
345:
346: null = fopen("/dev/null","wb");
347: dup2(fileno(null),1);
348: dup2(fileno(null),2);
1.12 noro 349: if ( !strcmp(dname,"1") ) {
350: /* XXX */
351: putenv("DISPLAY=");
1.23 noro 352: execl(prog,prog,(char *)0);
1.12 noro 353: } else if ( strcmp(dname,"0") )
1.23 noro 354: execl(prog,prog,"-display",dname,(char *)0);
1.11 noro 355: else {
1.10 noro 356: putenv("DISPLAY=");
1.23 noro 357: execl(prog,prog,(char *)0);
1.11 noro 358: }
1.6 noro 359: } else
1.23 noro 360: execl(prog,prog,"-display",dname,(char *)0);
1.5 noro 361: /* On failure */
362: errcode = 2;
363: write(4,&errcode,1);
364: exit(1);
1.1 noro 365: }
366: #endif
367: }
368:
369: static void launch_error(s)
370: char *s;
371: {
372: exit(0);
373: }
374:
1.15 noro 375: static void ox_io_init(sock)
1.1 noro 376: int sock;
377: {
378: endian_init();
379: /* server mode */
380: sindex = get_iofp(sock,0,1);
381: asir_OperandStack = (Obj *)CALLOC(BUFSIZ,sizeof(Obj));
382: asir_OperandStackPtr = -1;
383: }
384:
385: static void push_one(obj)
386: Obj obj;
387: {
388: if ( !obj || OID(obj) != O_VOID )
389: asir_OperandStack[++asir_OperandStackPtr] = obj;
390: }
391:
392: static Obj pop_one() {
393: if ( asir_OperandStackPtr >= 0 ) {
394: return asir_OperandStack[asir_OperandStackPtr--];
395: }
1.13 noro 396: }
397:
1.15 noro 398: static void terminate_server(int sig)
1.13 noro 399: {
1.14 noro 400: #if defined(SIGKILL)
1.13 noro 401: kill(cpid,SIGKILL);
1.14 noro 402: #endif
1.13 noro 403: exit(0);
1.1 noro 404: }
405:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>