[BACK]Return to tcpf.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / io

Annotation of OpenXM_contrib2/asir2000/io/tcpf.c, Revision 1.55

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>