Annotation of OpenXM_contrib2/asir2000/io/tcpf.c, Revision 1.68
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.68 ! fujimoto 47: * $OpenXM: OpenXM_contrib2/asir2000/io/tcpf.c,v 1.67 2015/08/05 16:01:20 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);
878: sleep(20);
879: /* system(cmd); */
1.15 noro 880: } else {
1.1 noro 881: if ( dname )
1.13 noro 882: if ( use_ssh )
1.25 noro 883: sprintf(cmd,
1.51 noro 884: "%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 885: rsh,host,OX_XTERM,host,launcher,localhost,conn_str,
886: control_port_str,server_port_str,server,"1");
887: else
1.25 noro 888: sprintf(cmd,
1.51 noro 889: "%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 890: rsh,host,OX_XTERM,host,dname,launcher,localhost,conn_str,
1.1 noro 891: control_port_str,server_port_str,server,dname);
892: else
1.25 noro 893: if ( use_ssh )
894: sprintf(cmd,
895: "%s -f -n %s \"%s %s %s %s %s %s %s %s>&/dev/null&\">/dev/null",
896: rsh,host,launcher,localhost,conn_str,
897: control_port_str,server_port_str,server,"1","-nolog");
898: else
899: sprintf(cmd,
1.17 noro 900: "%s -n %s \"%s %s %s %s %s %s %s %s>&/dev/null&\">/dev/null",
1.1 noro 901: rsh,host,launcher,localhost,conn_str,
1.17 noro 902: control_port_str,server_port_str,server,dname0,"-nolog");
1.1 noro 903: system(cmd);
904: }
1.15 noro 905: #endif /* VISUAL */
1.1 noro 906: }
907:
1.26 noro 908: void Pox_launch(NODE arg,Obj *rp)
1.1 noro 909: {
1.26 noro 910: ox_launch_main(1,arg,rp);
1.1 noro 911: }
912:
1.26 noro 913: void Pox_launch_nox(NODE arg,Obj *rp)
1.1 noro 914: {
1.26 noro 915: ox_launch_main(0,arg,rp);
1.1 noro 916: }
917:
918: /*
919: ox_launch() : invoke local ox_asir
920: ox_launch(0,ox_xxx) : invoke local ox_xxx with asir_libdir/ox_launch
921: ox_launch(remote,lib,ox_xxx) : invoke remote ox_xxx with lib/ox_launch
922: */
923:
1.26 noro 924: void ox_launch_main(int with_x,NODE arg,Obj *p)
1.1 noro 925: {
926: char *str;
1.26 noro 927: char *hostname,*servername;
1.1 noro 928: char *control;
929: int use_unix;
1.26 noro 930: Q ret;
1.1 noro 931: extern char *asir_libdir;
932:
933: if ( arg && ARG0(arg) && argc(arg) != 3 )
934: error("ox_launch : argument mismatch");
935: control = (char *)MALLOC(BUFSIZ);
936: if ( !arg || ( !ARG0(arg) && argc(arg) == 1 ) ) {
937: sprintf(control,"%s/ox_launch",asir_libdir);
1.28 noro 938: use_unix = IS_CYGWIN ? 0 : 1;
1.1 noro 939: servername = (char *)MALLOC(BUFSIZ);
940: sprintf(servername,"%s/ox_asir",asir_libdir);
941: } else if ( !ARG0(arg) && argc(arg) == 2 ) {
942: sprintf(control,"%s/ox_launch",asir_libdir);
1.28 noro 943: use_unix = IS_CYGWIN ? 0 : 1;
1.1 noro 944: str = BDY((STRING)ARG1(arg));
945: if ( str[0] == '/' )
946: servername = str;
947: else {
948: servername = (char *)MALLOC(BUFSIZ);
949: sprintf(servername,"%s/%s",asir_libdir,str);
950: }
951: } else {
952: sprintf(control,"%s/ox_launch",BDY((STRING)ARG1(arg)));
953: if ( !ARG0(arg) )
1.28 noro 954: use_unix = IS_CYGWIN ? 0 : 1;
1.1 noro 955: else
956: use_unix = 0;
957: str = BDY((STRING)ARG2(arg));
958: if ( str[0] == '/' )
959: servername = str;
960: else {
961: servername = (char *)MALLOC(BUFSIZ);
962: sprintf(servername,"%s/%s",BDY((STRING)ARG1(arg)),str);
963: }
964: }
965: if ( arg && ARG0(arg) )
966: hostname = BDY((STRING)ARG0(arg));
967: else
968: hostname = 0;
1.26 noro 969: ox_launch_generic(hostname,control,servername,use_unix,0,with_x,0,&ret);
970: *p = (Obj)ret;
1.1 noro 971: }
972:
1.59 ohara 973: void extend_mctab(int bound)
974: {
1.65 ohara 975: int i,n;
1.59 ohara 976: struct m_c *t;
977: if ( !m_c_tab ) {
978: n = (bound/INIT_TAB_SIZ + 1)*INIT_TAB_SIZ;
979: t = (struct m_c *)MALLOC_ATOMIC(n*sizeof(struct m_c));
980: for ( i = m_c_s; i < n; i++ ) {
981: t[i].af_unix = 0;
982: t[i].m = t[i].c = -1;
983: }
984: m_c_s = n; m_c_tab = t;
985: }else if (bound >= m_c_s) {
986: n = (bound/INIT_TAB_SIZ + 1)*INIT_TAB_SIZ;
987: t = (struct m_c *)MALLOC_ATOMIC(n*sizeof(struct m_c));
1.65 ohara 988: bzero((void *)t,n*sizeof(struct m_c));
1.59 ohara 989: bcopy((void *)m_c_tab,(void *)t,m_c_s*sizeof(struct m_c));
990: for ( i = m_c_s; i < n; i++ ) {
991: t[i].af_unix = 0;
992: t[i].m = t[i].c = -1;
993: }
994: m_c_s = n; m_c_tab = t;
995: }else {
996: return;
997: }
998: }
999:
1000: int available_mcindex(int ind)
1001: {
1002: if (ind < 0) return 1;
1003: extend_mctab(ind);
1004: return m_c_tab[ind].m<0 && m_c_tab[ind].c<0;
1005: }
1006:
1007: int register_server(int af_unix,int m,int c,int ind)
1.1 noro 1008: {
1.26 noro 1009: int s,i;
1.1 noro 1010: struct m_c *t;
1011: if ( c < 0 )
1012: return -1;
1.59 ohara 1013: extend_mctab( (ind<0)? 0: ind );
1014: if(ind >= 0) {
1015: if (m_c_tab[ind].m<0 && m_c_tab[ind].c<0) {
1016: m_c_tab[ind].m = m; m_c_tab[ind].c = c;
1017: m_c_tab[ind].af_unix = af_unix;
1018: if (ind>=m_c_i) m_c_i = ind+1;
1019: return ind;
1.8 noro 1020: }
1.59 ohara 1021: return -1;
1.1 noro 1022: }
1.39 noro 1023: #if !defined(MPI)
1.1 noro 1024: for ( i = 0; i < m_c_i; i++ )
1.8 noro 1025: if ( (m_c_tab[i].m<0) && (m_c_tab[i].c<0) )
1.1 noro 1026: break;
1027: if ( i < m_c_i ) {
1028: m_c_tab[i].m = m; m_c_tab[i].c = c;
1029: m_c_tab[i].af_unix = af_unix;
1030: return i;
1031: }
1032: #endif
1033: if ( m_c_i == m_c_s ) {
1.9 noro 1034: s = (m_c_s+INIT_TAB_SIZ)*sizeof(struct m_c);
1.66 noro 1035: t = (struct m_c *)MALLOC_ATOMIC(s);
1.27 noro 1036: bcopy((void *)m_c_tab,(void *)t,m_c_s*sizeof(struct m_c));
1.9 noro 1037: for ( i = 0; i < INIT_TAB_SIZ; i++ ) {
1.66 noro 1038: t[m_c_s+i].af_unix = 0;
1039: t[m_c_s+i].m = m_c_tab[m_c_s+i].c = -1;
1.8 noro 1040: }
1.9 noro 1041: m_c_s += INIT_TAB_SIZ; m_c_tab = t;
1.1 noro 1042: }
1043: m_c_tab[m_c_i].m = m; m_c_tab[m_c_i].c = c;
1044: m_c_tab[m_c_i].af_unix = af_unix;
1045: return m_c_i++;
1046: }
1047:
1048: /* iofp index => m_c_tab index */
1049:
1.26 noro 1050: int get_mcindex(int i)
1.1 noro 1051: {
1052: int j;
1053:
1054: for ( j = 0; j < m_c_i; j++ )
1055: if ( m_c_tab[j].c == i )
1056: return j;
1057: return -1;
1058: }
1059:
1060: /* arg = [ind0,ind1,...]; indk = index to m_c_tab */
1061:
1.26 noro 1062: void Pox_select(NODE arg,LIST *rp)
1.1 noro 1063: {
1.60 noro 1064: int fd,n,i,index,mcind,s,maxfd,minfd;
1.1 noro 1065: fd_set r,w,e;
1.57 noro 1066: NODE list,t,t1,t0;
1.1 noro 1067: Q q;
1068: double max;
1069: struct timeval interval;
1070: struct timeval *tvp;
1071:
1072: list = BDY((LIST)ARG0(arg)); arg = NEXT(arg);
1073: if ( arg ) {
1074: max = ToReal((Num)BDY(arg));
1075: interval.tv_sec = (int)max;
1076: interval.tv_usec = (int)((max-(int)max)*1000000);
1077: tvp = &interval;
1078: } else
1079: tvp = 0;
1080:
1081: FD_ZERO(&r); FD_ZERO(&w); FD_ZERO(&e);
1.60 noro 1082: maxfd = minfd = -1;
1.57 noro 1083: for ( t = list, t0 = 0; t; t = NEXT(t) ) {
1.1 noro 1084: index = QTOS((Q)BDY(t));
1085: valid_mctab_index(index);
1.57 noro 1086: s = m_c_tab[index].c;
1087: if ( ox_data_is_available(s) ) {
1088: MKNODE(t1,(Q)BDY(t),t0); t0 = t1;
1089: } else {
1090: fd = get_fd(s); FD_SET((unsigned int)fd,&r);
1.60 noro 1091: maxfd = maxfd<0 ? fd : MAX(fd,maxfd);
1092: minfd = minfd<0 ? fd : MIN(fd,minfd);
1.57 noro 1093: }
1094: }
1095: if ( t0 ) {
1096: MKLIST(*rp,t0); return;
1.1 noro 1097: }
1.57 noro 1098:
1.1 noro 1099: n = select(FD_SETSIZE,&r,&w,&e,tvp);
1.68 ! fujimoto 1100: #if defined(VISUAL) || defined(__MINGW32__) || defined(__MINGW64__)
1.60 noro 1101: for ( i = minfd, t = 0; n && i <= maxfd; i++ )
1102: #else
1.1 noro 1103: for ( i = 0, t = 0; n && i < FD_SETSIZE; i++ )
1.60 noro 1104: #endif
1.1 noro 1105: if ( FD_ISSET(i,&r) ) {
1106: /* index : index to iofp array */
1107: index = get_index(i);
1108: /* mcind : index to m_c_tab array */
1109: mcind = get_mcindex(index);
1110: n--; STOQ(mcind,q); MKNODE(t1,q,t); t = t1;
1111: }
1112: MKLIST(*rp,t);
1113: }
1114:
1.26 noro 1115: void Pox_flush(NODE arg,Q *rp)
1.1 noro 1116: {
1117: int index = QTOS((Q)ARG0(arg));
1118:
1119: valid_mctab_index(index);
1120: ox_flush_stream_force(m_c_tab[index].c);
1121: *rp = ONE;
1122: }
1123:
1.26 noro 1124: void Pox_send_raw_cmo(NODE arg,Obj *rp)
1.8 noro 1125: {
1126: int s;
1127: int index = QTOS((Q)ARG0(arg));
1128:
1129: valid_mctab_index(index);
1130: s = m_c_tab[index].c;
1131: ox_write_cmo(s,(Obj)ARG1(arg));
1132: /* flush always */
1133: ox_flush_stream(s);
1134: *rp = 0;
1135: }
1136:
1.26 noro 1137: void Pox_recv_raw_cmo(NODE arg,Obj *rp)
1.8 noro 1138: {
1139: int s;
1140: int index = QTOS((Q)ARG0(arg));
1141:
1142: valid_mctab_index(index);
1143: s = m_c_tab[index].c;
1144: ox_read_cmo(s,rp);
1145: }
1146:
1.44 noro 1147: void Pox_send_102(NODE arg,Obj *rp)
1.43 noro 1148: {
1149: int rank = QTOS((Q)ARG0(arg));
1150:
1.44 noro 1151: ox_send_data_102(rank,(Obj)ARG1(arg));
1.43 noro 1152: *rp = 0;
1153: }
1154:
1.44 noro 1155: void Pox_recv_102(NODE arg,Obj *rp)
1.43 noro 1156: {
1.44 noro 1157: int id;
1.43 noro 1158: int rank = QTOS((Q)ARG0(arg));
1159:
1.44 noro 1160: ox_recv_102(rank,&id,rp);
1.46 noro 1161: }
1162:
1163: void Pox_bcast_102(NODE arg,Obj *rp)
1164: {
1165: int rank = QTOS((Q)ARG0(arg));
1166: Obj data;
1167:
1.48 noro 1168: if ( argc(arg) > 1 )
1169: asir_push_one((Obj)ARG1(arg));
1170: ox_bcast_102(rank);
1171: *rp = (Obj)asir_pop_one();
1.47 noro 1172: }
1173:
1174: void Pox_reduce_102(NODE arg,Obj *rp)
1175: {
1.48 noro 1176: int root = QTOS((Q)ARG0(arg));
1.47 noro 1177: STRING op;
1178: char *opname;
1179: void (*func)();
1180:
1181: op = (STRING)ARG1(arg);
1182: asir_assert(op,O_STR,"ox_reduce_102");
1183: opname = BDY(op);
1184: if ( !strcmp(opname,"+") )
1185: func = arf_add;
1186: else if ( !strcmp(opname,"*") )
1187: func = arf_mul;
1188: else {
1189: error("ox_reduce_102 : operation not supported");
1190: }
1.48 noro 1191: if ( argc(arg) > 2 )
1192: asir_push_one((Obj)ARG2(arg));
1193: ox_reduce_102(root,func);
1194: *rp = (Obj)asir_pop_one();
1.43 noro 1195: }
1196:
1.26 noro 1197: void Pox_push_local(NODE arg,Obj *rp)
1.1 noro 1198: {
1199: int s;
1200: struct oLIST dummy;
1201: VL vl;
1.41 noro 1202: int index;
1.1 noro 1203:
1.41 noro 1204: if ( !arg )
1205: error("ox_push_local : too few arguments.");
1206: index = QTOS((Q)ARG0(arg));
1.1 noro 1207: valid_mctab_index(index);
1208: s = m_c_tab[index].c; arg = NEXT(arg);
1209:
1210: dummy.id = O_LIST; dummy.body = arg;
1211: get_vars_recursive((Obj)&dummy,&vl);
1212:
1213: ox_send_local_ring(s,vl);
1214: for ( ; arg; arg = NEXT(arg) )
1215: ox_send_local_data(s,BDY(arg));
1216: *rp = 0;
1217: }
1218:
1.26 noro 1219: void Pox_push_cmo(NODE arg,Obj *rp)
1.1 noro 1220: {
1221: int s;
1.41 noro 1222: int index;
1.1 noro 1223:
1.41 noro 1224: if ( !arg )
1225: error("ox_push_cmo : too few arguments.");
1226: index = QTOS((Q)ARG0(arg));
1.1 noro 1227: valid_mctab_index(index);
1228: s = m_c_tab[index].c; arg = NEXT(arg);
1229: for ( ; arg; arg = NEXT(arg) )
1230: ox_send_data(s,BDY(arg));
1231: *rp = 0;
1232: }
1233:
1.26 noro 1234: void Pox_push_vl(NODE arg,Obj *rp)
1.1 noro 1235: {
1236: int index = QTOS((Q)ARG0(arg));
1237:
1238: valid_mctab_index(index);
1239: ox_send_local_ring(m_c_tab[index].c,CO);
1240: *rp = 0;
1241: }
1242:
1.26 noro 1243: void Pox_pop_local(NODE arg,Obj *rp)
1.1 noro 1244: {
1245: int s;
1246: int index = QTOS((Q)ARG0(arg));
1247:
1248: valid_mctab_index(index);
1249: s = m_c_tab[index].c;
1250: ox_send_cmd(s,SM_popSerializedLocalObject);
1251: ox_flush_stream_force(s);
1252: ox_get_result(s,rp);
1253: }
1254:
1.26 noro 1255: void Pox_pop_cmo(NODE arg,Obj *rp)
1.1 noro 1256: {
1257: int s;
1258: int index = QTOS((Q)ARG0(arg));
1259:
1260: valid_mctab_index(index);
1261: s = m_c_tab[index].c;
1262: ox_send_cmd(s,SM_popCMO);
1263: ox_flush_stream_force(s);
1264: ox_get_result(s,rp);
1265: }
1266:
1.26 noro 1267: void Pox_pop0_local(NODE arg,Obj *rp)
1.1 noro 1268: {
1269: int index = QTOS((Q)ARG0(arg));
1270:
1271: valid_mctab_index(index);
1272: ox_send_cmd(m_c_tab[index].c,SM_popSerializedLocalObject);
1273: *rp = 0;
1274: }
1275:
1.26 noro 1276: void Pox_pop0_cmo(NODE arg,Obj *rp)
1.1 noro 1277: {
1278: int index = QTOS((Q)ARG0(arg));
1279:
1280: valid_mctab_index(index);
1281: ox_send_cmd(m_c_tab[index].c,SM_popCMO);
1282: *rp = 0;
1283: }
1284:
1.26 noro 1285: void Pox_pop0_string(NODE arg,STRING *rp)
1.1 noro 1286: {
1287: int index = QTOS((Q)ARG0(arg));
1288:
1289: valid_mctab_index(index);
1290: ox_send_cmd(m_c_tab[index].c,SM_popString);
1291: *rp = 0;
1292: }
1293:
1.26 noro 1294: void Pox_pop_string(NODE arg,Obj *rp)
1.1 noro 1295: {
1.26 noro 1296: int s;
1.1 noro 1297: int index = QTOS((Q)ARG0(arg));
1298:
1299: valid_mctab_index(index);
1300: s = m_c_tab[index].c;
1301: ox_send_cmd(s,SM_popString);
1302: ox_flush_stream_force(s);
1303: ox_get_result(s,rp);
1304: }
1305:
1.26 noro 1306: void Pox_get(NODE arg,Obj *rp)
1.1 noro 1307: {
1.3 noro 1308: int index;
1.1 noro 1309: int s;
1310:
1.3 noro 1311: if ( !arg ) {
1312: /* client->server */
1313: ox_get_result(0,rp);
1314: } else {
1315: /* server->client */
1316: index = QTOS((Q)ARG0(arg));
1317: valid_mctab_index(index);
1318: s = m_c_tab[index].c;
1319: ox_flush_stream_force(s);
1320: ox_get_result(s,rp);
1321: }
1.1 noro 1322: }
1323:
1.26 noro 1324: void Pox_pops(NODE arg,Obj *rp)
1.1 noro 1325: {
1326: int s;
1327: USINT n;
1328: int index = QTOS((Q)ARG0(arg));
1329:
1330: valid_mctab_index(index);
1331: s = m_c_tab[index].c;
1332: if ( NEXT(arg) )
1333: MKUSINT(n,QTOS((Q)ARG1(arg)));
1334: else
1335: MKUSINT(n,1);
1336: ox_send_data(s,n);
1337: ox_send_cmd(s,SM_pops);
1338: *rp = 0;
1339: }
1340:
1.26 noro 1341: void Pox_execute_function(NODE arg,Obj *rp)
1.1 noro 1342: {
1343: int s;
1344: USINT ui;
1345: int index = QTOS((Q)ARG0(arg));
1346:
1347: valid_mctab_index(index);
1348: s = m_c_tab[index].c;
1349: MKUSINT(ui,QTOS((Q)ARG2(arg)));
1350: ox_send_data(s,ui);
1351: ox_send_data(s,ARG1(arg));
1352: ox_send_cmd(s,SM_executeFunction);
1353: *rp = 0;
1354: }
1355:
1.26 noro 1356: void Pox_setname(NODE arg,Obj *rp)
1.1 noro 1357: {
1358: int s;
1359: int index = QTOS((Q)ARG0(arg));
1360:
1361: valid_mctab_index(index);
1362: s = m_c_tab[index].c;
1363: ox_send_data(s,ARG1(arg));
1364: ox_send_cmd(s,SM_setName);
1365: *rp = 0;
1366: }
1367:
1.26 noro 1368: void Pox_evalname(NODE arg,Obj *rp)
1.1 noro 1369: {
1370: int s;
1371: int index = QTOS((Q)ARG0(arg));
1372:
1373: valid_mctab_index(index);
1374: s = m_c_tab[index].c;
1375: ox_send_data(s,ARG1(arg));
1376: ox_send_cmd(s,SM_evalName);
1377: *rp = 0;
1378: }
1379:
1.26 noro 1380: void Pox_execute_string(NODE arg,Obj *rp)
1.1 noro 1381: {
1382: int s;
1383: int index = QTOS((Q)ARG0(arg));
1384:
1.52 noro 1385: asir_assert(ARG1(arg),O_STR,"ox_execute_string");
1.1 noro 1386: valid_mctab_index(index);
1387: s = m_c_tab[index].c;
1.53 noro 1388: ox_send_data(s,ARG1(arg));
1.1 noro 1389: ox_send_cmd(s,SM_executeStringByLocalParser);
1390: *rp = 0;
1391: }
1392:
1393: /* arg=[sid,fname,arg0,arg1,...,arg{n-1}] */
1394:
1.26 noro 1395: void Pox_rpc(NODE arg,Obj *rp)
1.1 noro 1396: {
1397: int s,i,n;
1398: STRING f;
1399: USINT ui;
1400: pointer *w;
1401: NODE t;
1402: int index = QTOS((Q)ARG0(arg));
1403:
1404: valid_mctab_index(index);
1405: s = m_c_tab[index].c; arg = NEXT(arg);
1406: f = (STRING)BDY(arg); arg = NEXT(arg);
1407: ox_send_local_ring(s,CO);
1408: for ( n = 0, t = arg; t; t = NEXT(t), n++ );
1409: w = (pointer *)ALLOCA(n*sizeof(pointer));
1410: for ( i = 0, t = arg; i < n; t = NEXT(t), i++ )
1411: w[i] = BDY(t);
1412: for ( i = n-1; i >= 0; i-- )
1413: ox_send_local_data(s,w[i]);
1414: MKUSINT(ui,n);
1415: ox_send_data(s,ui);
1416: ox_send_data(s,f);
1417: ox_send_cmd(s,SM_executeFunction);
1418: *rp = 0;
1419: }
1420:
1.26 noro 1421: void Pox_cmo_rpc(NODE arg,Obj *rp)
1.1 noro 1422: {
1423: int s,i,n;
1424: STRING f;
1425: USINT ui;
1426: NODE t;
1.57 noro 1427: Obj dmy;
1.1 noro 1428: pointer *w;
1429: int index = QTOS((Q)ARG0(arg));
1.57 noro 1430: int sync,find;
1.1 noro 1431:
1.57 noro 1432: find = get_opt("sync",&sync);
1.1 noro 1433: valid_mctab_index(index);
1434: s = m_c_tab[index].c; arg = NEXT(arg);
1435: f = (STRING)BDY(arg); arg = NEXT(arg);
1436: for ( n = 0, t = arg; t; t = NEXT(t), n++ );
1437: w = (pointer *)ALLOCA(n*sizeof(pointer));
1438: for ( i = 0, t = arg; i < n; t = NEXT(t), i++ )
1439: w[i] = BDY(t);
1440: for ( i = n-1; i >= 0; i-- )
1441: ox_send_data(s,w[i]);
1442: MKUSINT(ui,n);
1443: ox_send_data(s,ui);
1444: ox_send_data(s,f);
1.57 noro 1445: if ( find && sync ) {
1446: ox_send_cmd(s,SM_executeFunctionSync);
1447: ox_get_result(s,&dmy);
1448: } else
1449: ox_send_cmd(s,SM_executeFunction);
1.1 noro 1450: *rp = 0;
1451: }
1452:
1.26 noro 1453: void Pox_reset(NODE arg,Q *rp)
1.1 noro 1454: {
1455: USINT t;
1.26 noro 1456: int id,c,m;
1.1 noro 1457: Obj obj;
1458: int index = QTOS((Q)ARG0(arg));
1459:
1460: valid_mctab_index(index);
1461: m = m_c_tab[index].m;
1462: c = m_c_tab[index].c;
1463: if ( m >= 0 ) {
1464: if ( argc(arg) == 1 ) {
1465: ox_send_cmd(m,SM_control_reset_connection);
1466: ox_flush_stream_force(m);
1.26 noro 1467: ox_recv(m,&id,&obj); t = (USINT)obj;
1.1 noro 1468: }
1469: *rp = ONE;
1.68 ! fujimoto 1470: #if defined(VISUAL) || defined(__MINGW32__) || defined(__MINGW64__)
1.1 noro 1471: Sleep(100);
1472: ox_send_cmd(c,SM_nop);
1473: ox_flush_stream_force(c);
1474: #endif
1475: while ( 1 ) {
1476: ox_recv(c,&id,&obj);
1477: if ( id == OX_SYNC_BALL )
1478: break;
1479: }
1480: ox_send_sync(c);
1.5 noro 1481: } else
1482: *rp = 0;
1483: }
1484:
1.26 noro 1485: void Pox_intr(NODE arg,Q *rp)
1.5 noro 1486: {
1487: int m;
1488: int index = QTOS((Q)ARG0(arg));
1489:
1490: valid_mctab_index(index);
1491: m = m_c_tab[index].m;
1492: if ( m >= 0 ) {
1493: if ( argc(arg) == 1 ) {
1494: ox_send_cmd(m,SM_control_intr);
1495: ox_flush_stream_force(m);
1496: }
1497: *rp = ONE;
1.1 noro 1498: } else
1499: *rp = 0;
1500: }
1501:
1.26 noro 1502: void Pox_sync(NODE arg,Q *rp)
1.1 noro 1503: {
1504: int c;
1505: int index = QTOS((Q)ARG0(arg));
1506:
1507: valid_mctab_index(index);
1508: c = m_c_tab[index].c;
1509: ox_send_sync(c);
1510: *rp = 0;
1511: }
1512:
1.26 noro 1513: void Pox_shutdown(NODE arg,Q *rp)
1.1 noro 1514: {
1515: int s;
1516: int index = QTOS((Q)ARG0(arg));
1.68 ! fujimoto 1517: #if !defined(VISUAL) && !defined(__MINGW32__) && !defined(__MINGW64__)
1.1 noro 1518: int status;
1.26 noro 1519: #endif
1.1 noro 1520:
1521: valid_mctab_index(index);
1522: s = m_c_tab[index].m;
1523: ox_send_cmd(s,SM_shutdown);
1524: free_iofp(s);
1.18 noro 1525: s = m_c_tab[index].c;
1526: free_iofp(s);
1.68 ! fujimoto 1527: #if !defined(MPI) && !defined(VISUAL) && !defined(__MINGW32__) && !defined(__MINGW64__)
1.1 noro 1528: if ( m_c_tab[index].af_unix )
1529: wait(&status);
1530: #endif
1.8 noro 1531: m_c_tab[index].m = -1; m_c_tab[index].c = -1;
1.1 noro 1532: m_c_tab[index].af_unix = 0;
1533: *rp = 0;
1534: }
1535:
1.26 noro 1536: void Pox_push_cmd(NODE arg,Q *rp)
1.1 noro 1537: {
1.7 noro 1538: int ui;
1.1 noro 1539: int index = QTOS((Q)ARG0(arg));
1540:
1541: valid_mctab_index(index);
1.7 noro 1542: ui = QTOS((Q)ARG1(arg));
1.1 noro 1543: ox_send_cmd(m_c_tab[index].c,ui);
1544: *rp = 0;
1545: }
1.16 noro 1546:
1547: void shutdown_all() {
1548: int s;
1549: int i,index;
1.68 ! fujimoto 1550: #if !defined(VISUAL) && !defined(__MINGW32__) && !defined(__MINGW64__)
1.16 noro 1551: int status;
1.26 noro 1552: #endif
1.16 noro 1553:
1554: for ( i = I_am_server?1:0; i < m_c_i; i++ ) {
1555: index = i;
1556: check_valid_mctab_index(index);
1557: if ( index < 0 )
1558: continue;
1559: s = m_c_tab[index].m;
1560: ox_send_cmd(s,SM_shutdown);
1.68 ! fujimoto 1561: #if defined(VISUAL) || defined(__MINGW32__) || defined(__MINGW64__)
1.21 noro 1562: Sleep(1000);
1563: #endif
1.16 noro 1564: free_iofp(s);
1.18 noro 1565: s = m_c_tab[index].c;
1566: free_iofp(s);
1.68 ! fujimoto 1567: #if !defined(MPI) && !defined(VISUAL) && !defined(__MINGW32__) && !defined(__MINGW64__)
1.16 noro 1568: if ( m_c_tab[index].af_unix )
1569: wait(&status);
1570: #endif
1571: m_c_tab[index].m = 0; m_c_tab[index].c = 0;
1572: m_c_tab[index].af_unix = 0;
1573: }
1.33 noro 1574: }
1575:
1576: char *ox_get_servername(int);
1577:
1578: int is_ox_plot(int index)
1579: {
1580: char *name;
1581:
1582: check_valid_mctab_index(index);
1583: if ( index < 0 )
1584: return 0;
1585: /* m : client, c : server ??? */
1586: name = ox_get_servername(m_c_tab[index].c);
1587: return strcmp(name,"ox_plot") ? 0 : 1;
1588: }
1589:
1590: int validate_ox_plot_stream(int index)
1591: {
1592: int i;
1593: NODE arg;
1594: STRING name;
1595: Obj r;
1596:
1597: if ( is_ox_plot(index) )
1598: return index;
1599: for ( i = 0; i < m_c_i; i++ )
1600: if ( is_ox_plot(i) )
1601: return i;
1602:
1603: /* create an ox_plot server */
1604: MKSTR(name,"ox_plot");
1.61 noro 1605: arg = mknode(2,NULL,name);
1.33 noro 1606: Pox_launch_nox(arg,&r);
1.34 noro 1607: i = QTOS((Q)r);
1.68 ! fujimoto 1608: #if defined(VISUAL) || defined(__MINGW32__) || defined(__MINGW64__)
1.34 noro 1609: Sleep(100);
1610: ox_send_cmd(m_c_tab[i].c,SM_nop);
1611: ox_flush_stream_force(m_c_tab[i].c);
1612: #endif
1613: return i;
1.42 noro 1614: }
1615:
1616: int get_ox_server_id(int index)
1617: {
1618: valid_mctab_index(index);
1619: return m_c_tab[index].c;
1.43 noro 1620: }
1621:
1622: int register_102(int s1,int rank,int is_master)
1623: {
1624: unsigned char c,rc;
1625:
1626: if ( rank >= MAXIOFP ) return -1;
1627: iofp_102[rank].s = s1;
1.68 ! fujimoto 1628: #if defined(VISUAL) || defined(__MINGW32__) || defined(__MINGW64__)
1.43 noro 1629: iofp_102[rank].in = WSIO_open(s1,"r");
1630: iofp_102[rank].out = WSIO_open(s1,"w");
1631: #else
1632: iofp_102[rank].in = fdopen(s1,"r");
1633: iofp_102[rank].out = fdopen(s1,"w");
1634: #if !defined(__CYGWIN__)
1635: setbuffer(iofp_102[rank].in,iofp_102[rank].inbuf =
1.62 noro 1636: (char *)MALLOC_ATOMIC(LBUFSIZ),LBUFSIZ);
1.43 noro 1637: setbuffer(iofp_102[rank].out,iofp_102[rank].outbuf =
1.62 noro 1638: (char *)MALLOC_ATOMIC(LBUFSIZ),LBUFSIZ);
1.43 noro 1639: #endif
1640: #endif
1641: if ( little_endian )
1642: c = 1;
1643: else
1644: c = 0xff;
1645: if ( is_master ) {
1646: /* server : write -> read */
1647: write_char((FILE *)iofp_102[rank].out,&c);
1.44 noro 1648: ox_flush_stream_force_102(rank);
1.43 noro 1649: read_char((FILE *)iofp_102[rank].in,&rc);
1650: } else {
1651: /* client : read -> write */
1652: read_char((FILE *)iofp_102[rank].in,&rc);
1653: /* special care for a failure of spawing a server */
1654: if ( rc !=0 && rc != 1 && rc != 0xff )
1655: return -1;
1656: write_char((FILE *)iofp_102[rank].out,&c);
1.44 noro 1657: ox_flush_stream_force_102(rank);
1.43 noro 1658: }
1659: iofp_102[rank].conv = c == rc ? 0 : 1;
1660: iofp_102[rank].socket = 0;
1661: return 0;
1662: }
1663:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>