Annotation of OpenXM_contrib2/asir2018/io/ox_launch.c, Revision 1.2
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.2 ! noro 47: * $OpenXM: OpenXM_contrib2/asir2018/io/ox_launch.c,v 1.1 2018/09/19 05:45:08 noro 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 )
102: logfile = fopen("/tmp/ox_log","w");
103: fprintf(logfile,"%s\n",str);
104: fflush(logfile);
105: }
106:
107: /*
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.
117: */
118:
119: void launch_main(argc,argv)
120: int argc;
121: char **argv;
122: {
123: #if !defined(VISUAL) && !defined(__MINGW32__)
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;
147:
148: #if defined(VISUAL) || defined(__MINGW32__)
149: init_socket();
150: #endif
151:
152: set_signal(SIGINT,SIG_IGN);
153: #if defined(SIGHUP)
154: set_signal(SIGHUP,SIG_IGN);
155: #endif
156: #if defined(SIGUSR1)
157: set_signal(SIGUSR1,SIG_IGN);
158: #endif
159: #if defined(SIGTERM)
160: set_signal(SIGTERM,terminate_server);
161: #endif
162:
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: }
202: }
203:
204: #if defined(VISUAL) || defined(__MINGW32__)
205: static void do_cmd(cmd)
206: int cmd;
207: {
208: USINT t;
209:
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:
222: #if 0
223: /* XXX obsolete */
224: MKUSINT(t,0);
225: ox_send_data(sindex,t);
226: #endif
227: SetEvent(hReset);
228: break;
229: default:
230: break;
231: }
232: }
233: #else
234: static void do_cmd(cmd)
235: int cmd;
236: {
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:
255: #if 0
256: /* XXX obsolete */
257: MKUSINT(t,0);
258: ox_send_data(sindex,t);
259: #endif
260: kill(cpid,SIGUSR1);
261: break;
262: default:
263: break;
264: }
265: }
266: #endif
267:
268: static int ox_spawn(prog,bs,dname,nolog)
269: char *prog;
270: int bs;
271: char *dname;
272: char *nolog;
273: {
274: #if defined(VISUAL) || defined(__MINGW32__)
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: }
311: #if _MSC_VER < 1400
312: _fileinfo = 1;
313: #endif
314: hProc = _spawnv(_P_NOWAIT,AsirExe,av);
315: return (int)hProc;
316: #else /* VISUAL */
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: {
334: #if defined(linux) || defined(__NeXT__) || defined(ultrix) || defined(__CYGWIN__)
335: #include <sys/param.h>
336: close(0);
337: for ( i = 5; i < NOFILE; i++ )
338: close(i);
339: #else
340: #include <sys/resource.h>
341: struct rlimit rl;
342:
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: }
374: #endif
375: }
376:
377: static void launch_error(s)
378: char *s;
379: {
380: exit(0);
381: }
382:
383: static void ox_io_init(sock)
384: int sock;
385: {
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;
391: }
392:
393: static void push_one(obj)
394: Obj obj;
395: {
396: if ( !obj || OID(obj) != O_VOID )
397: asir_OperandStack[++asir_OperandStackPtr] = obj;
398: }
399:
400: static Obj pop_one() {
401: if ( asir_OperandStackPtr >= 0 ) {
402: return asir_OperandStack[asir_OperandStackPtr--];
403: }
404: }
405:
406: static void terminate_server(int sig)
407: {
408: #if defined(SIGKILL)
409: kill(cpid,SIGKILL);
410: #endif
411: exit(0);
412: }
413:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>