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