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