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