Annotation of OpenXM_contrib2/asir2000/io/tcpf.c, Revision 1.69
1.10 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.11 noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.10 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.69 ! fujimoto 47: * $OpenXM: OpenXM_contrib2/asir2000/io/tcpf.c,v 1.68 2015/08/06 10:01:52 fujimoto Exp $
1.10 noro 48: */
1.1 noro 49: #include "ca.h"
50: #include "parse.h"
51: #include "com.h"
52: #include <signal.h>
1.27 noro 53: #include <string.h>
1.68 fujimoto 54: #if !defined(VISUAL) && !defined(__MINGW32__) && !defined(__MINGW64__)
1.28 noro 55: #include <unistd.h>
1.1 noro 56: #include <sys/types.h>
57: #include <pwd.h>
58: #endif
59: #include "ox.h"
60:
1.63 ohara 61: #include <stdlib.h>
1.68 fujimoto 62: #if defined(VISUAL) || defined(__MINGW32__) || defined(__MINGW64__)
1.19 noro 63: #include <winsock2.h>
1.15 noro 64: #include <process.h>
1.1 noro 65: #endif
66:
1.59 ohara 67: #define INIT_TAB_SIZ 64
1.1 noro 68: #define OX_XTERM "ox_xterm"
69:
70: #if !defined(_PA_RISC1_1)
71: #define RSH "rsh"
72: #else
73: #define RSH "remsh"
74: #endif
75:
76: static struct m_c {
77: int m,c,af_unix;
78: } *m_c_tab;
79:
80: static int m_c_i,m_c_s;
1.16 noro 81: int I_am_server;
1.1 noro 82:
1.43 noro 83: extern int little_endian;
84:
1.39 noro 85: #if defined(MPI)
1.1 noro 86: extern int mpi_nprocs;
87: #define valid_mctab_index(ind)\
1.8 noro 88: if((ind)<0||(ind)>=mpi_nprocs){error("invalid server id");}
1.16 noro 89: #define check_valid_mctab_index(ind)\
90: if((ind)<0||(ind)>=mpi_nprocs){(ind)=-1;}
1.1 noro 91: #else
92: #define valid_mctab_index(ind)\
1.54 noro 93: if((ind)<I_am_server||(ind)>=m_c_i||\
1.8 noro 94: ((m_c_tab[ind].m<0)&&(m_c_tab[ind].c<0))){error("invalid server id");}
1.16 noro 95: #define check_valid_mctab_index(ind)\
1.54 noro 96: if((ind)<I_am_server||(ind)>=m_c_i||\
1.16 noro 97: ((m_c_tab[ind].m<0)&&(m_c_tab[ind].c<0))){(ind)=-1;}
1.1 noro 98: #endif
99:
1.44 noro 100: struct IOFP iofp_102[MAXIOFP];
101: int nserver_102;
102: int myrank_102;
1.43 noro 103:
104: int register_102(int s,int rank, int is_master);
105:
1.1 noro 106: int register_server();
107: int get_mcindex(int);
108:
1.8 noro 109: void Pox_send_raw_cmo(), Pox_recv_raw_cmo();
110:
1.26 noro 111: void Pox_launch(),Pox_launch_nox();
1.1 noro 112: void Pox_launch_generic();
113: void Pox_shutdown();
114:
1.5 noro 115: void Pox_rpc(),Pox_cmo_rpc(),Pox_reset(),Pox_intr(),Pox_sync(),Pox_select();
1.1 noro 116:
117: void Pox_push_local(),Pox_push_cmo(),Pox_push_vl(),Pox_push_cmd();
118:
119: void Pox_pop_local(),Pox_pop_cmo();
120: void Pox_pop0_local(),Pox_pop0_cmo();
121: void Pox_pop_string(),Pox_pop0_string();
122: void Pox_get(),Pox_pops();
123:
124: void Pox_execute_function(),Pox_execute_string();
125: void Pox_setname(), Pox_evalname();
126: void Pox_flush();
127: void Pgenerate_port(),Ptry_bind_listen(),Ptry_connect(),Ptry_accept();
128: void Pregister_server();
129: void Pox_get_serverinfo();
1.8 noro 130: void Pox_mpi_myid(), Pox_mpi_nprocs();
1.44 noro 131: void Pox_tcp_accept_102(),Pox_tcp_connect_102();
132: void Pox_send_102(),Pox_recv_102();
133: void Pox_set_rank_102();
1.49 noro 134: void Pox_get_rank_102();
1.44 noro 135: void Pox_reset_102();
1.46 noro 136: void Pox_bcast_102();
1.47 noro 137: void Pox_reduce_102();
1.1 noro 138:
139: void ox_launch_generic();
140:
141: pointer bevalf();
142:
143: struct ftab tcp_tab[] = {
1.8 noro 144: {"ox_send_raw_cmo",Pox_send_raw_cmo,2},
145: {"ox_recv_raw_cmo",Pox_recv_raw_cmo,1},
1.1 noro 146: {"ox_get_serverinfo",Pox_get_serverinfo,-1},
147: {"generate_port",Pgenerate_port,-1},
1.44 noro 148:
1.45 noro 149: /* from master to client */
1.44 noro 150: {"ox_set_rank_102",Pox_set_rank_102,3},
1.45 noro 151: {"ox_tcp_accept_102",Pox_tcp_accept_102,3},
152: {"ox_tcp_connect_102",Pox_tcp_connect_102,4},
153: {"ox_reset_102",Pox_reset_102,1},
1.44 noro 154:
1.49 noro 155: {"ox_get_rank_102",Pox_get_rank_102,0},
1.44 noro 156: {"ox_send_102",Pox_send_102,2},
157: {"ox_recv_102",Pox_recv_102,1},
1.46 noro 158: {"ox_bcast_102",Pox_bcast_102,-2},
1.48 noro 159: {"ox_reduce_102",Pox_reduce_102,-3},
1.44 noro 160:
1.1 noro 161: {"try_bind_listen",Ptry_bind_listen,1},
162: {"try_connect",Ptry_connect,2},
163: {"try_accept",Ptry_accept,2},
164: {"register_server",Pregister_server,4},
165: {"ox_flush",Pox_flush,1},
166: {"ox_launch",Pox_launch,-3},
167: {"ox_launch_nox",Pox_launch_nox,-3},
168: {"ox_launch_generic",Pox_launch_generic,7},
169: {"ox_shutdown",Pox_shutdown,1},
170:
171: {"ox_rpc",Pox_rpc,-99999999},
172: {"ox_cmo_rpc",Pox_cmo_rpc,-99999999},
173:
174: {"ox_sync",Pox_sync,1},
1.39 noro 175: #if defined(MPI)
1.8 noro 176: {"ox_mpi_myid",Pox_mpi_myid,0},
177: {"ox_mpi_nprocs",Pox_mpi_nprocs,0},
178: #endif
1.39 noro 179: #if !defined(MPI)
1.1 noro 180: {"ox_reset",Pox_reset,-2},
1.5 noro 181: {"ox_intr",Pox_intr,1},
1.1 noro 182: {"ox_select",Pox_select,-2},
183: #endif
184:
185: {"ox_pop_string",Pox_pop_string,1},
186: {"ox_pop0_string",Pox_pop0_string,1},
187: {"ox_pop_local",Pox_pop_local,1},
188: {"ox_pop0_local",Pox_pop0_local,1},
189: {"ox_pop_cmo",Pox_pop_cmo,1},
190: {"ox_pop0_cmo",Pox_pop0_cmo,1},
1.3 noro 191: {"ox_get",Pox_get,-1},
1.1 noro 192: {"ox_pops",Pox_pops,-2},
193:
194: {"ox_push_vl",Pox_push_vl,1},
195: {"ox_push_local",Pox_push_local,-99999999},
196: {"ox_push_cmo",Pox_push_cmo,-99999999},
197: {"ox_push_cmd",Pox_push_cmd,2},
198:
199: {"ox_setname",Pox_setname,2},
200: {"ox_evalname",Pox_evalname,2},
201:
202: {"ox_execute_string",Pox_execute_string,2},
203: {"ox_execute_function",Pox_execute_function,3},
204:
205: {0,0,0},
206: };
207:
208: extern struct IOFP iofp[];
209: extern MATHCAP my_mathcap;
1.6 noro 210: extern int ox_exchange_mathcap;
1.1 noro 211:
1.63 ohara 212: Obj asir_pop_one();
213: void asir_push_one(Obj);
1.1 noro 214:
1.39 noro 215: #if defined(MPI)
1.8 noro 216: extern int mpi_myid, mpi_nprocs;
217:
1.26 noro 218: void Pox_mpi_myid(Q *rp)
1.8 noro 219: {
220: STOQ(mpi_myid,*rp);
221: }
222:
1.26 noro 223: void Pox_mpi_nprocs(Q *rp)
1.8 noro 224: {
225: STOQ(mpi_nprocs,*rp);
226: }
227: #endif
228:
1.26 noro 229: void Pox_get_serverinfo(NODE arg,LIST *rp)
1.1 noro 230: {
231: int i,c;
1.24 noro 232: Q s_id;
1.1 noro 233: NODE t,n0,n;
234: LIST list,l;
235:
236: if ( !arg ) {
1.35 noro 237: for ( i = I_am_server?1:0, n0 = 0; i < m_c_i; i++ )
1.8 noro 238: if ( (m_c_tab[i].m>=0) || (m_c_tab[i].c>=0) ) {
1.1 noro 239: c = m_c_tab[i].c;
240: ox_get_serverinfo(c,&list);
1.24 noro 241: STOQ(i,s_id);
242: t = mknode(2,s_id,list);
1.1 noro 243: MKLIST(l,t);
244: NEXTNODE(n0,n);
245: BDY(n) = (pointer)l;
246: }
247: if ( n0 )
248: NEXT(n) = 0;
249: MKLIST(*rp,n0);
250: } else {
251: i = QTOS((Q)ARG0(arg));
1.8 noro 252: if ( i >= 0 && i < m_c_i && ((m_c_tab[i].m>=0) || (m_c_tab[i].c>=0)) )
1.1 noro 253: ox_get_serverinfo(m_c_tab[i].c,rp);
254: else {
255: MKLIST(*rp,0);
256: }
257: }
258: }
259:
260: /*
261: if noarg or arg[0]==0 then use IP socket and return a port number
262: else use UNIX socket and return a string which represents a path name
263: */
264:
1.26 noro 265: void Pgenerate_port(NODE arg,Obj *rp)
1.1 noro 266: {
267: char port_str[BUFSIZ];
268: int port;
269: char *s;
270: STRING str;
271: Q q;
272:
273: if ( !arg || !ARG0(arg) ) {
274: generate_port(0,port_str);
275: port = atoi(port_str);
276: STOQ(port,q);
277: *rp = (Obj)q;
278: } else {
279: generate_port(1,port_str);
1.26 noro 280: s = (char *)MALLOC(strlen((char *)port_str)+1);
1.1 noro 281: strcpy(s,port_str);
282: MKSTR(str,s);
283: *rp = (Obj)str;
284: }
285: }
286:
1.44 noro 287: void Pox_reset_102(NODE arg,Q *rp)
288: {
289: int s;
290: int index = QTOS((Q)ARG0(arg));
291:
292: valid_mctab_index(index);
293: s = m_c_tab[index].c;
294: ox_send_cmd(s,SM_reset_102);
295: ox_flush_stream_force(s);
1.50 noro 296: *rp = 0;
1.49 noro 297: }
298:
299: void Pox_get_rank_102(LIST *rp)
300: {
301: Q n,r;
302: NODE node;
303:
304: STOQ(nserver_102,n);
305: STOQ(myrank_102,r);
306: node = mknode(2,n,r);
307: MKLIST(*rp,node);
1.44 noro 308: }
309:
310: void Pox_set_rank_102(NODE arg,Q *rp)
311: {
312: Q nserver,rank;
313: int s;
314: int index = QTOS((Q)ARG0(arg));
315:
316: valid_mctab_index(index);
317: s = m_c_tab[index].c;
318: nserver = (Q)ARG1(arg);
319: rank = (Q)ARG2(arg);
320: ox_send_data(s,nserver);
321: ox_send_data(s,rank);
322: ox_send_cmd(s,SM_set_rank_102);
323: ox_flush_stream_force(s);
324: *rp = 0;
325: }
326:
1.45 noro 327: /* ox_tcp_accept_102(server,port,rank) */
1.43 noro 328:
1.44 noro 329: void Pox_tcp_accept_102(NODE arg,Q *rp)
1.43 noro 330: {
1.45 noro 331: int s;
332: int index = QTOS((Q)ARG0(arg));
333:
334: valid_mctab_index(index);
335: s = m_c_tab[index].c;
1.43 noro 336:
1.45 noro 337: ox_send_data(s,ARG1(arg));
338: ox_send_data(s,ARG2(arg));
339: ox_send_cmd(s,SM_tcp_accept_102);
340: ox_flush_stream_force(s);
341: *rp = 0;
1.43 noro 342: }
343:
344: /*
1.45 noro 345: ox_tcp_connect_102(server,host,port,rank)
1.43 noro 346: */
347:
1.44 noro 348: void Pox_tcp_connect_102(NODE arg,Q *rp)
1.43 noro 349: {
1.45 noro 350: int s;
351: int index = QTOS((Q)ARG0(arg));
352:
353: valid_mctab_index(index);
354: s = m_c_tab[index].c;
1.43 noro 355:
1.45 noro 356: ox_send_data(s,ARG1(arg));
357: ox_send_data(s,ARG2(arg));
358: ox_send_data(s,ARG3(arg));
359: ox_send_cmd(s,SM_tcp_connect_102);
360: ox_flush_stream_force(s);
361: *rp = 0;
1.43 noro 362: }
363:
1.1 noro 364: /*
365: try_bind_listen(port)
366: */
367:
1.26 noro 368: void Ptry_bind_listen(NODE arg,Q *rp)
1.1 noro 369: {
370: char port_str[BUFSIZ];
371: int port,s,use_unix;
372:
1.28 noro 373: if ( IS_CYGWIN || !ARG0(arg) || NUM(ARG0(arg)) ) {
1.1 noro 374: port = QTOS((Q)ARG0(arg));
375: sprintf(port_str,"%d",port);
376: use_unix = 0;
377: } else {
378: strcpy(port_str,BDY((STRING)ARG0(arg)));
379: use_unix = 1;
380: }
381: s = try_bind_listen(use_unix,port_str);
382: STOQ(s,*rp);
383: }
384:
385: /*
386: try_connect(host,port)
387: */
388:
1.26 noro 389: void Ptry_connect(NODE arg,Q *rp)
1.1 noro 390: {
391: char port_str[BUFSIZ];
392: char *host;
393: int port,s,use_unix;
394:
1.28 noro 395: if ( IS_CYGWIN || !ARG1(arg) || NUM(ARG1(arg)) ) {
1.1 noro 396: port = QTOS((Q)ARG1(arg));
397: sprintf(port_str,"%d",port);
398: use_unix = 0;
399: } else {
400: strcpy(port_str,BDY((STRING)ARG1(arg)));
401: use_unix = 1;
402: }
403: host = BDY((STRING)ARG0(arg));
404: s = try_connect(use_unix,host,port_str);
405: STOQ(s,*rp);
406: }
407:
408: /*
409: try_accept(sock,port)
410: */
411:
1.26 noro 412: void Ptry_accept(NODE arg,Q *rp)
1.1 noro 413: {
414: int use_unix,s;
415:
1.28 noro 416: if ( IS_CYGWIN || !ARG1(arg) || NUM(ARG1(arg)) )
1.1 noro 417: use_unix = 0;
418: else
419: use_unix = 1;
420: s = try_accept(use_unix,QTOS((Q)ARG0(arg)));
421: STOQ(s,*rp);
422: }
423:
424: /*
425: register_server(cs,cport,ss,sport)
426: */
427:
1.26 noro 428: void Pregister_server(NODE arg,Q *rp)
1.1 noro 429: {
430: int cs,ss,cn,sn,ind,use_unix,id;
431: char cport_str[BUFSIZ],sport_str[BUFSIZ];
1.26 noro 432: Obj obj;
1.1 noro 433: MATHCAP server_mathcap;
434:
435: cs = QTOS((Q)ARG0(arg));
436: ss = QTOS((Q)ARG2(arg));
1.28 noro 437: if ( IS_CYGWIN || !ARG1(arg) || NUM(ARG1(arg)) ) {
1.1 noro 438: sprintf(cport_str,"%d",QTOS((Q)ARG1(arg)));
439: use_unix = 0;
440: } else {
441: strcpy(cport_str,BDY((STRING)ARG1(arg)));
442: use_unix = 1;
443: }
444: if ( !ARG3(arg) || NUM(ARG3(arg)) ) {
445: if ( use_unix )
446: error("register_server : the protocol should conincide for two sockets");
447: sprintf(sport_str,"%d",QTOS((Q)ARG3(arg)));
448: } else {
449: if ( !use_unix )
450: error("register_server : the protocol should conincide for two sockets");
451: strcpy(sport_str,BDY((STRING)ARG3(arg)));
452: }
453:
454: /* client mode */
455: cn = get_iofp(cs,cport_str,0);
456: sn = get_iofp(ss,sport_str,0);
1.12 noro 457: /* get_iofp returns -1 if the laucher could not spawn the server */
458: if ( sn < 0 ) {
459: /* we should terminate the launcher */
460: ox_send_cmd(cn,SM_shutdown); ox_flush_stream_force(cn);
461: STOQ(-1,*rp);
462: return;
463: }
1.1 noro 464:
465: /* register server to the server list */
1.59 ohara 466: ind = register_server(use_unix,cn,sn,-1);
1.1 noro 467:
1.6 noro 468: if ( ox_exchange_mathcap ) {
469: /* request remote mathcap */
470: ox_send_cmd(sn,SM_mathcap);
471: ox_send_cmd(sn,SM_popCMO);
472: ox_flush_stream_force(sn);
1.26 noro 473: ox_recv(sn,&id,&obj); server_mathcap = (MATHCAP)obj;
1.6 noro 474: store_remote_mathcap(sn,server_mathcap);
475:
476: /* send my mathcap */
477: create_my_mathcap("asir");
478: ox_send_data(sn,my_mathcap);
479: ox_send_cmd(sn,SM_setMathcap);
480: }
1.1 noro 481: /* return the server id */
482: STOQ(ind,*rp);
483: }
484:
1.68 fujimoto 485: #if !defined(VISUAL) && !defined(__MINGW32__) && !defined(__MINGW64__)
1.55 noro 486: #include <sys/file.h>
487: #include <sys/types.h>
488: #include <sys/stat.h>
489: #include <pwd.h>
490:
491: static int find_executable(char *);
492: static int find_executable_main(char *);
493:
494: static int find_executable(char *com)
495: {
496: char *c,*s;
497: int len;
498: char dir[BUFSIZ],path[BUFSIZ];
499:
500: for ( s = (char *)getenv("PATH"); s; ) {
501: c = (char *)index(s,':');
502: if ( c ) {
503: len = c-s;
504: strncpy(dir,s,len); s = c+1; dir[len] = 0;
505: } else {
506: strcpy(dir,s); s = 0;
507: }
508: sprintf(path,"%s/%s",dir,com);
509: if ( find_executable_main(path) )
510: return 1;
511: }
512: return 0;
513: }
514:
515: static int find_executable_main(char *file)
516: {
517: struct stat buf;
518:
519: if ( stat(file,&buf) || (buf.st_mode & S_IFDIR) )
520: return 0;
521: if ( access(file,X_OK) )
522: return 0;
523: else
524: return 1;
525: }
526:
527: #endif
1.1 noro 528: /*
529: ox_launch_generic(host,launcher,server,use_unix,use_ssh,use_x,conn_to_serv)
530:
531: Input
532: host: hostname on which servers run
533: launcher: path name of the launcher
534: server: path name of the server
535: use_unix: use UNIX domain socket if 1
536: use_ssh: use ssh if 1
537: use_x: use X11 facilities if 1
538: conn_to_serv: connect to server if 1
539: */
540:
1.26 noro 541: void Pox_launch_generic(NODE arg,Q *rp)
1.1 noro 542: {
543: int use_unix,use_ssh,use_x,conn_to_serv;
544: char *host,*launcher,*server;
1.26 noro 545: Q ret;
1.1 noro 546:
547: host = (arg&&ARG0(arg))?BDY((STRING)ARG0(arg)):0;
548: launcher = BDY((STRING)ARG1(arg));
549: server = BDY((STRING)ARG2(arg));
1.28 noro 550: use_unix = !IS_CYGWIN && ARG3(arg) ? 1 : 0;
1.1 noro 551: use_ssh = ARG4(arg) ? 1 : 0;
552: use_x = ARG5(arg) ? 1 : 0;
553: conn_to_serv = QTOS((Q)ARG6(arg));
1.28 noro 554: if ( !IS_CYGWIN && !host )
1.1 noro 555: use_unix = 1;
556: ox_launch_generic(host,launcher,server,
1.26 noro 557: use_unix,use_ssh,use_x,conn_to_serv,&ret);
558: *rp = ret;
1.1 noro 559: }
1.56 noro 560:
561: #if 0
562: void ox_launcher_101_generic(char *host,char *launcher,
563: int use_unix,int use_ssh,int use_x,int conn_to_serv,Q *rp)
564: {
565: int cs,cn,ind,id;
566: char control_port_str[BUFSIZ];
567: Obj obj;
568:
1.68 fujimoto 569: #if !defined(VISUAL) && !defined(__MINGW32__) && !defined(__MINGW64__)
1.56 noro 570: if ( use_unix && !find_executable("xterm") ) use_x = 0;
571: #endif
572: control_port_str[0] = 0;
573: do {
574: generate_port(use_unix,control_port_str);
575: if ( conn_to_serv ) {
576: spawn_server_101(host,launcher,
577: use_unix,use_ssh,use_x,conn_to_serv,
578: control_port_str);
579: cs = try_connect(use_unix,host,control_port_str);
580: } else {
581: cs = try_bind_listen(use_unix,control_port_str);
582: if ( cs < 0 ) continue;
583: spawn_laucher_101(host,launcher,
584: use_unix,use_ssh,use_x,conn_to_serv,
585: control_port_str);
586: cs = try_accept(use_unix,cs);
587: }
588: } while ( cs < 0 );
589:
590: /* client mode */
591: cn = get_iofp(cs,control_port_str,0);
592:
593: /* register server to the server list */
594: ind = register_server_101(use_unix,cn);
595:
596: STOQ(ind,*rp);
597: }
598: #endif
1.1 noro 599:
1.26 noro 600: void ox_launch_generic(char *host,char *launcher,char *server,
601: int use_unix,int use_ssh,int use_x,int conn_to_serv,Q *rp)
1.1 noro 602: {
603: int cs,ss,cn,sn,ind,id;
604: char control_port_str[BUFSIZ];
605: char server_port_str[BUFSIZ];
1.26 noro 606: Obj obj;
1.1 noro 607: MATHCAP server_mathcap;
1.59 ohara 608: Q value;
609: char *key;
610: int fd=-1;
611: NODE opt,n0;
612:
613: if ( current_option ) {
614: for ( opt = current_option; opt; opt = NEXT(opt) ) {
615: n0 = BDY((LIST)BDY(opt));
616: key = BDY((STRING)BDY(n0));
617: value = (Q)BDY(NEXT(n0));
618: if ( !strcmp(key,"fd") && value ) {
619: fd = QTOS(value);
620: break;
621: }
622: }
623: }
624: if (!available_mcindex(fd)) {
625: STOQ(-1,*rp);
626: return;
627: }
1.68 fujimoto 628: #if !defined(VISUAL) && !defined(__MINGW32__) && !defined(__MINGW64__)
1.55 noro 629: if ( use_unix && !find_executable("xterm") ) use_x = 0;
630: #endif
1.1 noro 631: control_port_str[0] = 0;
632: server_port_str[0] = 0;
633: do {
634: generate_port(use_unix,control_port_str);
635: generate_port(use_unix,server_port_str);
636: if ( !conn_to_serv ) {
637: cs = try_bind_listen(use_unix,control_port_str);
1.40 noro 638: if ( cs < 0 ) continue;
1.1 noro 639: ss = try_bind_listen(use_unix,server_port_str);
1.40 noro 640: if ( ss < 0 ) continue;
1.1 noro 641: }
642: spawn_server(host,launcher,server,
643: use_unix,use_ssh,use_x,conn_to_serv,
644: control_port_str,server_port_str);
645: if ( conn_to_serv ) {
646: cs = try_connect(use_unix,host,control_port_str);
1.40 noro 647: if ( cs < 0 ) continue;
1.1 noro 648: ss = try_connect(use_unix,host,server_port_str);
1.40 noro 649: if ( ss < 0 ) continue;
1.1 noro 650: } else {
651: cs = try_accept(use_unix,cs);
1.40 noro 652: if ( cs < 0 ) continue;
1.1 noro 653: ss = try_accept(use_unix,ss);
1.40 noro 654: if ( ss < 0 ) continue;
1.1 noro 655: }
656: } while ( cs < 0 || ss < 0 );
657:
658: /* client mode */
659: cn = get_iofp(cs,control_port_str,0);
660: sn = get_iofp(ss,server_port_str,0);
1.12 noro 661: /* get_iofp returns -1 if the laucher could not spawn the server */
662: if ( sn < 0 ) {
663: /* we should terminate the launcher */
664: ox_send_cmd(cn,SM_shutdown); ox_flush_stream_force(cn);
665: STOQ(-1,*rp);
666: return;
667: }
1.1 noro 668:
669: /* register server to the server list */
1.59 ohara 670: ind = register_server(use_unix,cn,sn,fd);
1.1 noro 671:
1.6 noro 672: if ( ox_exchange_mathcap ) {
673: /* request remote mathcap */
674: ox_send_cmd(sn,SM_mathcap);
675: ox_send_cmd(sn,SM_popCMO);
676: ox_flush_stream_force(sn);
1.26 noro 677: ox_recv(sn,&id,&obj); server_mathcap = (MATHCAP)obj;
1.6 noro 678: store_remote_mathcap(sn,server_mathcap);
679:
680: /* send my mathcap */
681: create_my_mathcap("asir");
682: ox_send_data(sn,my_mathcap);
683: ox_send_cmd(sn,SM_setMathcap);
684: }
1.1 noro 685: /* return the server id */
686: STOQ(ind,*rp);
687: }
688:
1.67 fujimoto 689: #if defined(__CYGWIN32__)
1.29 noro 690: static void bslash2slash(char *buf)
691: {
692: char *p;
693:
694: for ( p = buf; *p; p++ )
695: if ( *p == '\\' )
696: *p = '/';
697: }
698:
1.28 noro 699: static int get_start_path(char *buf)
700: {
701: static char start_path[BUFSIZ];
702: static int start_initialized = 0;
703: char name[BUFSIZ];
704:
705: if ( start_initialized ) {
706: strcpy(buf,start_path);
707: return 1;
708: }
709:
1.32 noro 710: /* Windows98 */
711: strcpy(buf,"c:\\windows\\command\\start.exe");
712: cygwin_conv_to_full_posix_path(buf,name);
713: if ( !access(name,X_OK) ) {
714: bslash2slash(buf);
715: strcpy(start_path,buf);
716: start_initialized = 1;
717: return 1;
718: }
719:
1.28 noro 720: /* Windows2000 */
721: strcpy(buf,"c:\\winnt\\system32\\start.exe");
722: cygwin_conv_to_full_posix_path(buf,name);
723: if ( !access(name,X_OK) ) {
1.29 noro 724: bslash2slash(buf);
1.28 noro 725: strcpy(start_path,buf);
1.32 noro 726: start_initialized = 1;
727: return 1;
728: }
729:
730: strcpy(buf,"c:\\winnt\\system32\\cmd.exe");
731: cygwin_conv_to_full_posix_path(buf,name);
732: if ( !access(name,X_OK) ) {
733: bslash2slash(buf);
734: sprintf(start_path,"%s /c start",buf);
735: strcpy(buf,start_path);
736: start_initialized = 1;
1.28 noro 737: return 1;
738: }
739:
1.32 noro 740: strcpy(buf,"c:\\windows\\system32\\cmd.exe");
1.28 noro 741: cygwin_conv_to_full_posix_path(buf,name);
742: if ( !access(name,X_OK) ) {
1.29 noro 743: bslash2slash(buf);
1.32 noro 744: sprintf(start_path,"%s /c start",buf);
745: strcpy(buf,start_path);
746: start_initialized = 1;
1.28 noro 747: return 1;
748: }
749:
750: return 0;
751: }
752:
753: static void get_launcher_path(char *buf)
754: {
755: static char rootname[BUFSIZ];
756: static char launcher_path[BUFSIZ];
757: static int launcher_initialized = 0;
758: char name[BUFSIZ];
759:
760: if ( launcher_initialized ) {
761: strcpy(buf,launcher_path);
762: return;
763: }
764:
765: get_rootdir(rootname,sizeof(rootname));
766: sprintf(name,"%s/ox_launch.exe",rootname);
767: cygwin_conv_to_full_win32_path(name,launcher_path);
1.29 noro 768: bslash2slash(launcher_path);
1.28 noro 769: launcher_initialized = 1;
770: strcpy(buf,launcher_path);
771: }
772: #endif
773:
1.26 noro 774: void spawn_server(char *host,char *launcher,char *server,
775: int use_unix,int use_ssh,int use_x,int conn_to_serv,
776: char *control_port_str,char *server_port_str)
1.1 noro 777: {
778: char localhost[BUFSIZ];
1.31 noro 779: char *dname,*conn_str,*rsh,*dname0,*asirhost;
1.15 noro 780: char AsirExe[BUFSIZ];
781: STRING rootdir;
782: char prog[BUFSIZ];
783: char *av[BUFSIZ];
1.68 fujimoto 784: #if !defined(VISUAL) && !defined(__MINGW32__) && !defined(__MINGW64__)
1.26 noro 785: char cmd[BUFSIZ];
786: #endif
1.28 noro 787: #if defined(__CYGWIN__)
788: char win_start[BUFSIZ],win_launcher[BUFSIZ];
789: #endif
1.26 noro 790: void Pget_rootdir();
1.1 noro 791:
1.17 noro 792: dname0 = (char *)getenv("DISPLAY");
1.23 noro 793: if ( !dname0 )
794: dname0 = "0";
1.17 noro 795: dname = use_x ? dname0 : 0;
1.1 noro 796: conn_str = conn_to_serv ? "1" : "0";
1.13 noro 797: rsh = getenv("ASIR_RSH");
798: if ( !rsh )
799: rsh = use_ssh ? "ssh" : RSH;
1.25 noro 800: if ( !use_unix && strstr(rsh,"ssh") ) {
1.13 noro 801: /*
802: * if "ssh" is used to invoke a remote server,
803: * we should not specify "-display".
804: */
805: use_ssh = 1;
806: }
1.31 noro 807: asirhost = (char *)getenv("ASIRHOSTNAME");
808: if ( asirhost )
809: strcpy(localhost,asirhost);
810: else
811: gethostname(localhost,BUFSIZ);
1.68 fujimoto 812: #if defined(VISUAL) || defined(__MINGW32__) || defined(__MINGW64__)
1.15 noro 813: if ( !use_unix )
814: error("spawn_server : not implemented on Windows");
815: Pget_rootdir(&rootdir);
816: sprintf(AsirExe,"%s\\bin\\engine.exe",BDY(rootdir));
817: strcpy(prog,server);
818: server = strrchr(prog,'/')+1;
819: av[0] = "ox_launch";
820: av[1] = "127.0.0.1";
821: av[2] = conn_str;
822: av[3] = control_port_str;
823: av[4] = server_port_str;
824: av[5] = server;
825: av[6] = use_x ? "1" : "0";
826: av[7] = 0;
827:
828: _spawnv(_P_NOWAIT,AsirExe,av);
829: // _spawnv(_P_NOWAIT,"d:\\home\\noro\\engine2000\\debug\\engine.exe",av);
830: // printf("ox_launch 127.0.0.1 %s %s %s %s 0\n",conn_str,control_port_str,server_port_str,server);
831: #else
1.28 noro 832: if ( use_unix || !host ) {
1.67 fujimoto 833: #if defined(__CYGWIN32__)
1.29 noro 834: get_launcher_path(win_launcher);
1.30 noro 835: if ( dname && strchr(dname,':') ) {
836: if ( !fork() ) {
837: setpgid(0,getpid());
838: execlp("xterm","xterm","-name",OX_XTERM,"-T","ox_launch:local","-display",dname,
1.51 noro 839: "-geometry","60x10","-xrm","XTerm*locale:false","-e",launcher,use_unix?".":"127.1",conn_str,
1.58 noro 840: control_port_str,server_port_str,server,dname,(char *)0);
1.30 noro 841: }
842: } else if ( dname && get_start_path(win_start) ) {
843: sprintf(cmd,"%s %s %s %s %s %s %s 1",
1.29 noro 844: win_start,win_launcher,use_unix?".":"127.1",conn_str,
845: control_port_str,server_port_str,server);
846: system(cmd);
847: } else {
848: if ( !fork() ) {
849: setpgid(0,getpid());
850: execlp(launcher,launcher,use_unix?".":"127.1",conn_str,
1.58 noro 851: control_port_str,server_port_str,server,dname0,"-nolog",(char *)0);
1.27 noro 852: }
1.29 noro 853: }
1.27 noro 854: #else
1.29 noro 855: if ( !fork() ) {
856: setpgid(0,getpid());
1.28 noro 857: if ( dname )
1.13 noro 858: execlp("xterm","xterm","-name",OX_XTERM,"-T","ox_launch:local","-display",dname,
1.51 noro 859: "-geometry","60x10","-xrm","XTerm*locale:false","-e",launcher,use_unix?".":"127.1",conn_str,
1.58 noro 860: control_port_str,server_port_str,server,dname,(char *)0);
1.27 noro 861: else
1.28 noro 862: execlp(launcher,launcher,use_unix?".":"127.1",conn_str,
1.58 noro 863: control_port_str,server_port_str,server,dname0,"-nolog",(char *)0);
1.1 noro 864: }
1.29 noro 865: #endif
1.1 noro 866: } else if ( conn_to_serv == 2 ) {
867: /* special support for java */
868: if ( dname )
869: sprintf(cmd,
1.13 noro 870: "%s -n %s \"(cd %s; xterm -name %s %s -geometry 60x10 -e java %s -host %s -control %s -data %s)>&/dev/null&\">/dev/null",
1.1 noro 871: rsh,host,launcher,OX_XTERM,dname,server,localhost,control_port_str,server_port_str);
872: else
873: sprintf(cmd,
874: "%s -n %s \"(cd %s; java %s -host %s -control %s -data %s)>&/dev/null&\">/dev/null",
875: rsh,host,launcher,server,localhost,
876: control_port_str,server_port_str,server);
877: fprintf(stderr,"%s\n",cmd);
1.69 ! fujimoto 878: #if defined(__MINGW32__) || defined(__MINGW64__)
! 879: fflush(stderr);
! 880: #endif
1.1 noro 881: sleep(20);
882: /* system(cmd); */
1.15 noro 883: } else {
1.1 noro 884: if ( dname )
1.13 noro 885: if ( use_ssh )
1.25 noro 886: sprintf(cmd,
1.51 noro 887: "%s -f -n %s \"xterm -name %s -title ox_launch:%s -geometry 60x10 -xrm 'XTerm*locale:false' -e %s %s %s %s %s %s %s >&/dev/null\">/dev/null",
1.13 noro 888: rsh,host,OX_XTERM,host,launcher,localhost,conn_str,
889: control_port_str,server_port_str,server,"1");
890: else
1.25 noro 891: sprintf(cmd,
1.51 noro 892: "%s -n %s \"xterm -name %s -title ox_launch:%s -display %s -geometry 60x10 -xrm 'XTerm*locale:false' -e %s %s %s %s %s %s %s >&/dev/null&\">/dev/null",
1.13 noro 893: rsh,host,OX_XTERM,host,dname,launcher,localhost,conn_str,
1.1 noro 894: control_port_str,server_port_str,server,dname);
895: else
1.25 noro 896: if ( use_ssh )
897: sprintf(cmd,
898: "%s -f -n %s \"%s %s %s %s %s %s %s %s>&/dev/null&\">/dev/null",
899: rsh,host,launcher,localhost,conn_str,
900: control_port_str,server_port_str,server,"1","-nolog");
901: else
902: sprintf(cmd,
1.17 noro 903: "%s -n %s \"%s %s %s %s %s %s %s %s>&/dev/null&\">/dev/null",
1.1 noro 904: rsh,host,launcher,localhost,conn_str,
1.17 noro 905: control_port_str,server_port_str,server,dname0,"-nolog");
1.1 noro 906: system(cmd);
907: }
1.15 noro 908: #endif /* VISUAL */
1.1 noro 909: }
910:
1.26 noro 911: void Pox_launch(NODE arg,Obj *rp)
1.1 noro 912: {
1.26 noro 913: ox_launch_main(1,arg,rp);
1.1 noro 914: }
915:
1.26 noro 916: void Pox_launch_nox(NODE arg,Obj *rp)
1.1 noro 917: {
1.26 noro 918: ox_launch_main(0,arg,rp);
1.1 noro 919: }
920:
921: /*
922: ox_launch() : invoke local ox_asir
923: ox_launch(0,ox_xxx) : invoke local ox_xxx with asir_libdir/ox_launch
924: ox_launch(remote,lib,ox_xxx) : invoke remote ox_xxx with lib/ox_launch
925: */
926:
1.26 noro 927: void ox_launch_main(int with_x,NODE arg,Obj *p)
1.1 noro 928: {
929: char *str;
1.26 noro 930: char *hostname,*servername;
1.1 noro 931: char *control;
932: int use_unix;
1.26 noro 933: Q ret;
1.1 noro 934: extern char *asir_libdir;
935:
936: if ( arg && ARG0(arg) && argc(arg) != 3 )
937: error("ox_launch : argument mismatch");
938: control = (char *)MALLOC(BUFSIZ);
939: if ( !arg || ( !ARG0(arg) && argc(arg) == 1 ) ) {
940: sprintf(control,"%s/ox_launch",asir_libdir);
1.28 noro 941: use_unix = IS_CYGWIN ? 0 : 1;
1.1 noro 942: servername = (char *)MALLOC(BUFSIZ);
943: sprintf(servername,"%s/ox_asir",asir_libdir);
944: } else if ( !ARG0(arg) && argc(arg) == 2 ) {
945: sprintf(control,"%s/ox_launch",asir_libdir);
1.28 noro 946: use_unix = IS_CYGWIN ? 0 : 1;
1.1 noro 947: str = BDY((STRING)ARG1(arg));
948: if ( str[0] == '/' )
949: servername = str;
950: else {
951: servername = (char *)MALLOC(BUFSIZ);
952: sprintf(servername,"%s/%s",asir_libdir,str);
953: }
954: } else {
955: sprintf(control,"%s/ox_launch",BDY((STRING)ARG1(arg)));
956: if ( !ARG0(arg) )
1.28 noro 957: use_unix = IS_CYGWIN ? 0 : 1;
1.1 noro 958: else
959: use_unix = 0;
960: str = BDY((STRING)ARG2(arg));
961: if ( str[0] == '/' )
962: servername = str;
963: else {
964: servername = (char *)MALLOC(BUFSIZ);
965: sprintf(servername,"%s/%s",BDY((STRING)ARG1(arg)),str);
966: }
967: }
968: if ( arg && ARG0(arg) )
969: hostname = BDY((STRING)ARG0(arg));
970: else
971: hostname = 0;
1.26 noro 972: ox_launch_generic(hostname,control,servername,use_unix,0,with_x,0,&ret);
973: *p = (Obj)ret;
1.1 noro 974: }
975:
1.59 ohara 976: void extend_mctab(int bound)
977: {
1.65 ohara 978: int i,n;
1.59 ohara 979: struct m_c *t;
980: if ( !m_c_tab ) {
981: n = (bound/INIT_TAB_SIZ + 1)*INIT_TAB_SIZ;
982: t = (struct m_c *)MALLOC_ATOMIC(n*sizeof(struct m_c));
983: for ( i = m_c_s; i < n; i++ ) {
984: t[i].af_unix = 0;
985: t[i].m = t[i].c = -1;
986: }
987: m_c_s = n; m_c_tab = t;
988: }else if (bound >= m_c_s) {
989: n = (bound/INIT_TAB_SIZ + 1)*INIT_TAB_SIZ;
990: t = (struct m_c *)MALLOC_ATOMIC(n*sizeof(struct m_c));
1.65 ohara 991: bzero((void *)t,n*sizeof(struct m_c));
1.59 ohara 992: bcopy((void *)m_c_tab,(void *)t,m_c_s*sizeof(struct m_c));
993: for ( i = m_c_s; i < n; i++ ) {
994: t[i].af_unix = 0;
995: t[i].m = t[i].c = -1;
996: }
997: m_c_s = n; m_c_tab = t;
998: }else {
999: return;
1000: }
1001: }
1002:
1003: int available_mcindex(int ind)
1004: {
1005: if (ind < 0) return 1;
1006: extend_mctab(ind);
1007: return m_c_tab[ind].m<0 && m_c_tab[ind].c<0;
1008: }
1009:
1010: int register_server(int af_unix,int m,int c,int ind)
1.1 noro 1011: {
1.26 noro 1012: int s,i;
1.1 noro 1013: struct m_c *t;
1014: if ( c < 0 )
1015: return -1;
1.59 ohara 1016: extend_mctab( (ind<0)? 0: ind );
1017: if(ind >= 0) {
1018: if (m_c_tab[ind].m<0 && m_c_tab[ind].c<0) {
1019: m_c_tab[ind].m = m; m_c_tab[ind].c = c;
1020: m_c_tab[ind].af_unix = af_unix;
1021: if (ind>=m_c_i) m_c_i = ind+1;
1022: return ind;
1.8 noro 1023: }
1.59 ohara 1024: return -1;
1.1 noro 1025: }
1.39 noro 1026: #if !defined(MPI)
1.1 noro 1027: for ( i = 0; i < m_c_i; i++ )
1.8 noro 1028: if ( (m_c_tab[i].m<0) && (m_c_tab[i].c<0) )
1.1 noro 1029: break;
1030: if ( i < m_c_i ) {
1031: m_c_tab[i].m = m; m_c_tab[i].c = c;
1032: m_c_tab[i].af_unix = af_unix;
1033: return i;
1034: }
1035: #endif
1036: if ( m_c_i == m_c_s ) {
1.9 noro 1037: s = (m_c_s+INIT_TAB_SIZ)*sizeof(struct m_c);
1.66 noro 1038: t = (struct m_c *)MALLOC_ATOMIC(s);
1.27 noro 1039: bcopy((void *)m_c_tab,(void *)t,m_c_s*sizeof(struct m_c));
1.9 noro 1040: for ( i = 0; i < INIT_TAB_SIZ; i++ ) {
1.66 noro 1041: t[m_c_s+i].af_unix = 0;
1042: t[m_c_s+i].m = m_c_tab[m_c_s+i].c = -1;
1.8 noro 1043: }
1.9 noro 1044: m_c_s += INIT_TAB_SIZ; m_c_tab = t;
1.1 noro 1045: }
1046: m_c_tab[m_c_i].m = m; m_c_tab[m_c_i].c = c;
1047: m_c_tab[m_c_i].af_unix = af_unix;
1048: return m_c_i++;
1049: }
1050:
1051: /* iofp index => m_c_tab index */
1052:
1.26 noro 1053: int get_mcindex(int i)
1.1 noro 1054: {
1055: int j;
1056:
1057: for ( j = 0; j < m_c_i; j++ )
1058: if ( m_c_tab[j].c == i )
1059: return j;
1060: return -1;
1061: }
1062:
1063: /* arg = [ind0,ind1,...]; indk = index to m_c_tab */
1064:
1.26 noro 1065: void Pox_select(NODE arg,LIST *rp)
1.1 noro 1066: {
1.60 noro 1067: int fd,n,i,index,mcind,s,maxfd,minfd;
1.1 noro 1068: fd_set r,w,e;
1.57 noro 1069: NODE list,t,t1,t0;
1.1 noro 1070: Q q;
1071: double max;
1072: struct timeval interval;
1073: struct timeval *tvp;
1074:
1075: list = BDY((LIST)ARG0(arg)); arg = NEXT(arg);
1076: if ( arg ) {
1077: max = ToReal((Num)BDY(arg));
1078: interval.tv_sec = (int)max;
1079: interval.tv_usec = (int)((max-(int)max)*1000000);
1080: tvp = &interval;
1081: } else
1082: tvp = 0;
1083:
1084: FD_ZERO(&r); FD_ZERO(&w); FD_ZERO(&e);
1.60 noro 1085: maxfd = minfd = -1;
1.57 noro 1086: for ( t = list, t0 = 0; t; t = NEXT(t) ) {
1.1 noro 1087: index = QTOS((Q)BDY(t));
1088: valid_mctab_index(index);
1.57 noro 1089: s = m_c_tab[index].c;
1090: if ( ox_data_is_available(s) ) {
1091: MKNODE(t1,(Q)BDY(t),t0); t0 = t1;
1092: } else {
1093: fd = get_fd(s); FD_SET((unsigned int)fd,&r);
1.60 noro 1094: maxfd = maxfd<0 ? fd : MAX(fd,maxfd);
1095: minfd = minfd<0 ? fd : MIN(fd,minfd);
1.57 noro 1096: }
1097: }
1098: if ( t0 ) {
1099: MKLIST(*rp,t0); return;
1.1 noro 1100: }
1.57 noro 1101:
1.1 noro 1102: n = select(FD_SETSIZE,&r,&w,&e,tvp);
1.68 fujimoto 1103: #if defined(VISUAL) || defined(__MINGW32__) || defined(__MINGW64__)
1.60 noro 1104: for ( i = minfd, t = 0; n && i <= maxfd; i++ )
1105: #else
1.1 noro 1106: for ( i = 0, t = 0; n && i < FD_SETSIZE; i++ )
1.60 noro 1107: #endif
1.1 noro 1108: if ( FD_ISSET(i,&r) ) {
1109: /* index : index to iofp array */
1110: index = get_index(i);
1111: /* mcind : index to m_c_tab array */
1112: mcind = get_mcindex(index);
1113: n--; STOQ(mcind,q); MKNODE(t1,q,t); t = t1;
1114: }
1115: MKLIST(*rp,t);
1116: }
1117:
1.26 noro 1118: void Pox_flush(NODE arg,Q *rp)
1.1 noro 1119: {
1120: int index = QTOS((Q)ARG0(arg));
1121:
1122: valid_mctab_index(index);
1123: ox_flush_stream_force(m_c_tab[index].c);
1124: *rp = ONE;
1125: }
1126:
1.26 noro 1127: void Pox_send_raw_cmo(NODE arg,Obj *rp)
1.8 noro 1128: {
1129: int s;
1130: int index = QTOS((Q)ARG0(arg));
1131:
1132: valid_mctab_index(index);
1133: s = m_c_tab[index].c;
1134: ox_write_cmo(s,(Obj)ARG1(arg));
1135: /* flush always */
1136: ox_flush_stream(s);
1137: *rp = 0;
1138: }
1139:
1.26 noro 1140: void Pox_recv_raw_cmo(NODE arg,Obj *rp)
1.8 noro 1141: {
1142: int s;
1143: int index = QTOS((Q)ARG0(arg));
1144:
1145: valid_mctab_index(index);
1146: s = m_c_tab[index].c;
1147: ox_read_cmo(s,rp);
1148: }
1149:
1.44 noro 1150: void Pox_send_102(NODE arg,Obj *rp)
1.43 noro 1151: {
1152: int rank = QTOS((Q)ARG0(arg));
1153:
1.44 noro 1154: ox_send_data_102(rank,(Obj)ARG1(arg));
1.43 noro 1155: *rp = 0;
1156: }
1157:
1.44 noro 1158: void Pox_recv_102(NODE arg,Obj *rp)
1.43 noro 1159: {
1.44 noro 1160: int id;
1.43 noro 1161: int rank = QTOS((Q)ARG0(arg));
1162:
1.44 noro 1163: ox_recv_102(rank,&id,rp);
1.46 noro 1164: }
1165:
1166: void Pox_bcast_102(NODE arg,Obj *rp)
1167: {
1168: int rank = QTOS((Q)ARG0(arg));
1169: Obj data;
1170:
1.48 noro 1171: if ( argc(arg) > 1 )
1172: asir_push_one((Obj)ARG1(arg));
1173: ox_bcast_102(rank);
1174: *rp = (Obj)asir_pop_one();
1.47 noro 1175: }
1176:
1177: void Pox_reduce_102(NODE arg,Obj *rp)
1178: {
1.48 noro 1179: int root = QTOS((Q)ARG0(arg));
1.47 noro 1180: STRING op;
1181: char *opname;
1182: void (*func)();
1183:
1184: op = (STRING)ARG1(arg);
1185: asir_assert(op,O_STR,"ox_reduce_102");
1186: opname = BDY(op);
1187: if ( !strcmp(opname,"+") )
1188: func = arf_add;
1189: else if ( !strcmp(opname,"*") )
1190: func = arf_mul;
1191: else {
1192: error("ox_reduce_102 : operation not supported");
1193: }
1.48 noro 1194: if ( argc(arg) > 2 )
1195: asir_push_one((Obj)ARG2(arg));
1196: ox_reduce_102(root,func);
1197: *rp = (Obj)asir_pop_one();
1.43 noro 1198: }
1199:
1.26 noro 1200: void Pox_push_local(NODE arg,Obj *rp)
1.1 noro 1201: {
1202: int s;
1203: struct oLIST dummy;
1204: VL vl;
1.41 noro 1205: int index;
1.1 noro 1206:
1.41 noro 1207: if ( !arg )
1208: error("ox_push_local : too few arguments.");
1209: index = QTOS((Q)ARG0(arg));
1.1 noro 1210: valid_mctab_index(index);
1211: s = m_c_tab[index].c; arg = NEXT(arg);
1212:
1213: dummy.id = O_LIST; dummy.body = arg;
1214: get_vars_recursive((Obj)&dummy,&vl);
1215:
1216: ox_send_local_ring(s,vl);
1217: for ( ; arg; arg = NEXT(arg) )
1218: ox_send_local_data(s,BDY(arg));
1219: *rp = 0;
1220: }
1221:
1.26 noro 1222: void Pox_push_cmo(NODE arg,Obj *rp)
1.1 noro 1223: {
1224: int s;
1.41 noro 1225: int index;
1.1 noro 1226:
1.41 noro 1227: if ( !arg )
1228: error("ox_push_cmo : too few arguments.");
1229: index = QTOS((Q)ARG0(arg));
1.1 noro 1230: valid_mctab_index(index);
1231: s = m_c_tab[index].c; arg = NEXT(arg);
1232: for ( ; arg; arg = NEXT(arg) )
1233: ox_send_data(s,BDY(arg));
1234: *rp = 0;
1235: }
1236:
1.26 noro 1237: void Pox_push_vl(NODE arg,Obj *rp)
1.1 noro 1238: {
1239: int index = QTOS((Q)ARG0(arg));
1240:
1241: valid_mctab_index(index);
1242: ox_send_local_ring(m_c_tab[index].c,CO);
1243: *rp = 0;
1244: }
1245:
1.26 noro 1246: void Pox_pop_local(NODE arg,Obj *rp)
1.1 noro 1247: {
1248: int s;
1249: int index = QTOS((Q)ARG0(arg));
1250:
1251: valid_mctab_index(index);
1252: s = m_c_tab[index].c;
1253: ox_send_cmd(s,SM_popSerializedLocalObject);
1254: ox_flush_stream_force(s);
1255: ox_get_result(s,rp);
1256: }
1257:
1.26 noro 1258: void Pox_pop_cmo(NODE arg,Obj *rp)
1.1 noro 1259: {
1260: int s;
1261: int index = QTOS((Q)ARG0(arg));
1262:
1263: valid_mctab_index(index);
1264: s = m_c_tab[index].c;
1265: ox_send_cmd(s,SM_popCMO);
1266: ox_flush_stream_force(s);
1267: ox_get_result(s,rp);
1268: }
1269:
1.26 noro 1270: void Pox_pop0_local(NODE arg,Obj *rp)
1.1 noro 1271: {
1272: int index = QTOS((Q)ARG0(arg));
1273:
1274: valid_mctab_index(index);
1275: ox_send_cmd(m_c_tab[index].c,SM_popSerializedLocalObject);
1276: *rp = 0;
1277: }
1278:
1.26 noro 1279: void Pox_pop0_cmo(NODE arg,Obj *rp)
1.1 noro 1280: {
1281: int index = QTOS((Q)ARG0(arg));
1282:
1283: valid_mctab_index(index);
1284: ox_send_cmd(m_c_tab[index].c,SM_popCMO);
1285: *rp = 0;
1286: }
1287:
1.26 noro 1288: void Pox_pop0_string(NODE arg,STRING *rp)
1.1 noro 1289: {
1290: int index = QTOS((Q)ARG0(arg));
1291:
1292: valid_mctab_index(index);
1293: ox_send_cmd(m_c_tab[index].c,SM_popString);
1294: *rp = 0;
1295: }
1296:
1.26 noro 1297: void Pox_pop_string(NODE arg,Obj *rp)
1.1 noro 1298: {
1.26 noro 1299: int s;
1.1 noro 1300: int index = QTOS((Q)ARG0(arg));
1301:
1302: valid_mctab_index(index);
1303: s = m_c_tab[index].c;
1304: ox_send_cmd(s,SM_popString);
1305: ox_flush_stream_force(s);
1306: ox_get_result(s,rp);
1307: }
1308:
1.26 noro 1309: void Pox_get(NODE arg,Obj *rp)
1.1 noro 1310: {
1.3 noro 1311: int index;
1.1 noro 1312: int s;
1313:
1.3 noro 1314: if ( !arg ) {
1315: /* client->server */
1316: ox_get_result(0,rp);
1317: } else {
1318: /* server->client */
1319: index = QTOS((Q)ARG0(arg));
1320: valid_mctab_index(index);
1321: s = m_c_tab[index].c;
1322: ox_flush_stream_force(s);
1323: ox_get_result(s,rp);
1324: }
1.1 noro 1325: }
1326:
1.26 noro 1327: void Pox_pops(NODE arg,Obj *rp)
1.1 noro 1328: {
1329: int s;
1330: USINT n;
1331: int index = QTOS((Q)ARG0(arg));
1332:
1333: valid_mctab_index(index);
1334: s = m_c_tab[index].c;
1335: if ( NEXT(arg) )
1336: MKUSINT(n,QTOS((Q)ARG1(arg)));
1337: else
1338: MKUSINT(n,1);
1339: ox_send_data(s,n);
1340: ox_send_cmd(s,SM_pops);
1341: *rp = 0;
1342: }
1343:
1.26 noro 1344: void Pox_execute_function(NODE arg,Obj *rp)
1.1 noro 1345: {
1346: int s;
1347: USINT ui;
1348: int index = QTOS((Q)ARG0(arg));
1349:
1350: valid_mctab_index(index);
1351: s = m_c_tab[index].c;
1352: MKUSINT(ui,QTOS((Q)ARG2(arg)));
1353: ox_send_data(s,ui);
1354: ox_send_data(s,ARG1(arg));
1355: ox_send_cmd(s,SM_executeFunction);
1356: *rp = 0;
1357: }
1358:
1.26 noro 1359: void Pox_setname(NODE arg,Obj *rp)
1.1 noro 1360: {
1361: int s;
1362: int index = QTOS((Q)ARG0(arg));
1363:
1364: valid_mctab_index(index);
1365: s = m_c_tab[index].c;
1366: ox_send_data(s,ARG1(arg));
1367: ox_send_cmd(s,SM_setName);
1368: *rp = 0;
1369: }
1370:
1.26 noro 1371: void Pox_evalname(NODE arg,Obj *rp)
1.1 noro 1372: {
1373: int s;
1374: int index = QTOS((Q)ARG0(arg));
1375:
1376: valid_mctab_index(index);
1377: s = m_c_tab[index].c;
1378: ox_send_data(s,ARG1(arg));
1379: ox_send_cmd(s,SM_evalName);
1380: *rp = 0;
1381: }
1382:
1.26 noro 1383: void Pox_execute_string(NODE arg,Obj *rp)
1.1 noro 1384: {
1385: int s;
1386: int index = QTOS((Q)ARG0(arg));
1387:
1.52 noro 1388: asir_assert(ARG1(arg),O_STR,"ox_execute_string");
1.1 noro 1389: valid_mctab_index(index);
1390: s = m_c_tab[index].c;
1.53 noro 1391: ox_send_data(s,ARG1(arg));
1.1 noro 1392: ox_send_cmd(s,SM_executeStringByLocalParser);
1393: *rp = 0;
1394: }
1395:
1396: /* arg=[sid,fname,arg0,arg1,...,arg{n-1}] */
1397:
1.26 noro 1398: void Pox_rpc(NODE arg,Obj *rp)
1.1 noro 1399: {
1400: int s,i,n;
1401: STRING f;
1402: USINT ui;
1403: pointer *w;
1404: NODE t;
1405: int index = QTOS((Q)ARG0(arg));
1406:
1407: valid_mctab_index(index);
1408: s = m_c_tab[index].c; arg = NEXT(arg);
1409: f = (STRING)BDY(arg); arg = NEXT(arg);
1410: ox_send_local_ring(s,CO);
1411: for ( n = 0, t = arg; t; t = NEXT(t), n++ );
1412: w = (pointer *)ALLOCA(n*sizeof(pointer));
1413: for ( i = 0, t = arg; i < n; t = NEXT(t), i++ )
1414: w[i] = BDY(t);
1415: for ( i = n-1; i >= 0; i-- )
1416: ox_send_local_data(s,w[i]);
1417: MKUSINT(ui,n);
1418: ox_send_data(s,ui);
1419: ox_send_data(s,f);
1420: ox_send_cmd(s,SM_executeFunction);
1421: *rp = 0;
1422: }
1423:
1.26 noro 1424: void Pox_cmo_rpc(NODE arg,Obj *rp)
1.1 noro 1425: {
1426: int s,i,n;
1427: STRING f;
1428: USINT ui;
1429: NODE t;
1.57 noro 1430: Obj dmy;
1.1 noro 1431: pointer *w;
1432: int index = QTOS((Q)ARG0(arg));
1.57 noro 1433: int sync,find;
1.1 noro 1434:
1.57 noro 1435: find = get_opt("sync",&sync);
1.1 noro 1436: valid_mctab_index(index);
1437: s = m_c_tab[index].c; arg = NEXT(arg);
1438: f = (STRING)BDY(arg); arg = NEXT(arg);
1439: for ( n = 0, t = arg; t; t = NEXT(t), n++ );
1440: w = (pointer *)ALLOCA(n*sizeof(pointer));
1441: for ( i = 0, t = arg; i < n; t = NEXT(t), i++ )
1442: w[i] = BDY(t);
1443: for ( i = n-1; i >= 0; i-- )
1444: ox_send_data(s,w[i]);
1445: MKUSINT(ui,n);
1446: ox_send_data(s,ui);
1447: ox_send_data(s,f);
1.57 noro 1448: if ( find && sync ) {
1449: ox_send_cmd(s,SM_executeFunctionSync);
1450: ox_get_result(s,&dmy);
1451: } else
1452: ox_send_cmd(s,SM_executeFunction);
1.1 noro 1453: *rp = 0;
1454: }
1455:
1.26 noro 1456: void Pox_reset(NODE arg,Q *rp)
1.1 noro 1457: {
1458: USINT t;
1.26 noro 1459: int id,c,m;
1.1 noro 1460: Obj obj;
1461: int index = QTOS((Q)ARG0(arg));
1462:
1463: valid_mctab_index(index);
1464: m = m_c_tab[index].m;
1465: c = m_c_tab[index].c;
1466: if ( m >= 0 ) {
1467: if ( argc(arg) == 1 ) {
1468: ox_send_cmd(m,SM_control_reset_connection);
1469: ox_flush_stream_force(m);
1.26 noro 1470: ox_recv(m,&id,&obj); t = (USINT)obj;
1.1 noro 1471: }
1472: *rp = ONE;
1.68 fujimoto 1473: #if defined(VISUAL) || defined(__MINGW32__) || defined(__MINGW64__)
1.1 noro 1474: Sleep(100);
1475: ox_send_cmd(c,SM_nop);
1476: ox_flush_stream_force(c);
1477: #endif
1478: while ( 1 ) {
1479: ox_recv(c,&id,&obj);
1480: if ( id == OX_SYNC_BALL )
1481: break;
1482: }
1483: ox_send_sync(c);
1.5 noro 1484: } else
1485: *rp = 0;
1486: }
1487:
1.26 noro 1488: void Pox_intr(NODE arg,Q *rp)
1.5 noro 1489: {
1490: int m;
1491: int index = QTOS((Q)ARG0(arg));
1492:
1493: valid_mctab_index(index);
1494: m = m_c_tab[index].m;
1495: if ( m >= 0 ) {
1496: if ( argc(arg) == 1 ) {
1497: ox_send_cmd(m,SM_control_intr);
1498: ox_flush_stream_force(m);
1499: }
1500: *rp = ONE;
1.1 noro 1501: } else
1502: *rp = 0;
1503: }
1504:
1.26 noro 1505: void Pox_sync(NODE arg,Q *rp)
1.1 noro 1506: {
1507: int c;
1508: int index = QTOS((Q)ARG0(arg));
1509:
1510: valid_mctab_index(index);
1511: c = m_c_tab[index].c;
1512: ox_send_sync(c);
1513: *rp = 0;
1514: }
1515:
1.26 noro 1516: void Pox_shutdown(NODE arg,Q *rp)
1.1 noro 1517: {
1518: int s;
1519: int index = QTOS((Q)ARG0(arg));
1.68 fujimoto 1520: #if !defined(VISUAL) && !defined(__MINGW32__) && !defined(__MINGW64__)
1.1 noro 1521: int status;
1.26 noro 1522: #endif
1.1 noro 1523:
1524: valid_mctab_index(index);
1525: s = m_c_tab[index].m;
1526: ox_send_cmd(s,SM_shutdown);
1527: free_iofp(s);
1.18 noro 1528: s = m_c_tab[index].c;
1529: free_iofp(s);
1.68 fujimoto 1530: #if !defined(MPI) && !defined(VISUAL) && !defined(__MINGW32__) && !defined(__MINGW64__)
1.1 noro 1531: if ( m_c_tab[index].af_unix )
1532: wait(&status);
1533: #endif
1.8 noro 1534: m_c_tab[index].m = -1; m_c_tab[index].c = -1;
1.1 noro 1535: m_c_tab[index].af_unix = 0;
1536: *rp = 0;
1537: }
1538:
1.26 noro 1539: void Pox_push_cmd(NODE arg,Q *rp)
1.1 noro 1540: {
1.7 noro 1541: int ui;
1.1 noro 1542: int index = QTOS((Q)ARG0(arg));
1543:
1544: valid_mctab_index(index);
1.7 noro 1545: ui = QTOS((Q)ARG1(arg));
1.1 noro 1546: ox_send_cmd(m_c_tab[index].c,ui);
1547: *rp = 0;
1548: }
1.16 noro 1549:
1550: void shutdown_all() {
1551: int s;
1552: int i,index;
1.68 fujimoto 1553: #if !defined(VISUAL) && !defined(__MINGW32__) && !defined(__MINGW64__)
1.16 noro 1554: int status;
1.26 noro 1555: #endif
1.16 noro 1556:
1557: for ( i = I_am_server?1:0; i < m_c_i; i++ ) {
1558: index = i;
1559: check_valid_mctab_index(index);
1560: if ( index < 0 )
1561: continue;
1562: s = m_c_tab[index].m;
1563: ox_send_cmd(s,SM_shutdown);
1.68 fujimoto 1564: #if defined(VISUAL) || defined(__MINGW32__) || defined(__MINGW64__)
1.21 noro 1565: Sleep(1000);
1566: #endif
1.16 noro 1567: free_iofp(s);
1.18 noro 1568: s = m_c_tab[index].c;
1569: free_iofp(s);
1.68 fujimoto 1570: #if !defined(MPI) && !defined(VISUAL) && !defined(__MINGW32__) && !defined(__MINGW64__)
1.16 noro 1571: if ( m_c_tab[index].af_unix )
1572: wait(&status);
1573: #endif
1574: m_c_tab[index].m = 0; m_c_tab[index].c = 0;
1575: m_c_tab[index].af_unix = 0;
1576: }
1.33 noro 1577: }
1578:
1579: char *ox_get_servername(int);
1580:
1581: int is_ox_plot(int index)
1582: {
1583: char *name;
1584:
1585: check_valid_mctab_index(index);
1586: if ( index < 0 )
1587: return 0;
1588: /* m : client, c : server ??? */
1589: name = ox_get_servername(m_c_tab[index].c);
1590: return strcmp(name,"ox_plot") ? 0 : 1;
1591: }
1592:
1593: int validate_ox_plot_stream(int index)
1594: {
1595: int i;
1596: NODE arg;
1597: STRING name;
1598: Obj r;
1599:
1600: if ( is_ox_plot(index) )
1601: return index;
1602: for ( i = 0; i < m_c_i; i++ )
1603: if ( is_ox_plot(i) )
1604: return i;
1605:
1606: /* create an ox_plot server */
1607: MKSTR(name,"ox_plot");
1.61 noro 1608: arg = mknode(2,NULL,name);
1.33 noro 1609: Pox_launch_nox(arg,&r);
1.34 noro 1610: i = QTOS((Q)r);
1.68 fujimoto 1611: #if defined(VISUAL) || defined(__MINGW32__) || defined(__MINGW64__)
1.34 noro 1612: Sleep(100);
1613: ox_send_cmd(m_c_tab[i].c,SM_nop);
1614: ox_flush_stream_force(m_c_tab[i].c);
1615: #endif
1616: return i;
1.42 noro 1617: }
1618:
1619: int get_ox_server_id(int index)
1620: {
1621: valid_mctab_index(index);
1622: return m_c_tab[index].c;
1.43 noro 1623: }
1624:
1625: int register_102(int s1,int rank,int is_master)
1626: {
1627: unsigned char c,rc;
1628:
1629: if ( rank >= MAXIOFP ) return -1;
1630: iofp_102[rank].s = s1;
1.68 fujimoto 1631: #if defined(VISUAL) || defined(__MINGW32__) || defined(__MINGW64__)
1.43 noro 1632: iofp_102[rank].in = WSIO_open(s1,"r");
1633: iofp_102[rank].out = WSIO_open(s1,"w");
1634: #else
1635: iofp_102[rank].in = fdopen(s1,"r");
1636: iofp_102[rank].out = fdopen(s1,"w");
1637: #if !defined(__CYGWIN__)
1638: setbuffer(iofp_102[rank].in,iofp_102[rank].inbuf =
1.62 noro 1639: (char *)MALLOC_ATOMIC(LBUFSIZ),LBUFSIZ);
1.43 noro 1640: setbuffer(iofp_102[rank].out,iofp_102[rank].outbuf =
1.62 noro 1641: (char *)MALLOC_ATOMIC(LBUFSIZ),LBUFSIZ);
1.43 noro 1642: #endif
1643: #endif
1644: if ( little_endian )
1645: c = 1;
1646: else
1647: c = 0xff;
1648: if ( is_master ) {
1649: /* server : write -> read */
1650: write_char((FILE *)iofp_102[rank].out,&c);
1.44 noro 1651: ox_flush_stream_force_102(rank);
1.43 noro 1652: read_char((FILE *)iofp_102[rank].in,&rc);
1653: } else {
1654: /* client : read -> write */
1655: read_char((FILE *)iofp_102[rank].in,&rc);
1656: /* special care for a failure of spawing a server */
1657: if ( rc !=0 && rc != 1 && rc != 0xff )
1658: return -1;
1659: write_char((FILE *)iofp_102[rank].out,&c);
1.44 noro 1660: ox_flush_stream_force_102(rank);
1.43 noro 1661: }
1662: iofp_102[rank].conv = c == rc ? 0 : 1;
1663: iofp_102[rank].socket = 0;
1664: return 0;
1665: }
1666:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>