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