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