[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.63

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

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