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