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