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

Annotation of OpenXM_contrib2/asir2000/io/sio.c, Revision 1.9.2.2

1.5       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.6       noro       26:  * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.5       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.9.2.2 ! maekawa    47:  * $OpenXM: OpenXM_contrib2/asir2000/io/sio.c,v 1.9.2.1 2000/11/08 08:18:14 maekawa Exp $
1.5       noro       48: */
1.1       noro       49: #if INET
                     50: #include "ca.h"
1.3       noro       51: #include "setjmp.h"
1.1       noro       52: #include "ox.h"
                     53: #if defined(VISUAL)
                     54: #include <winsock.h>
                     55: #else
                     56: #include <sys/time.h>
                     57: #include <sys/uio.h>
                     58: #include <sys/ioctl.h>
                     59: #include <sys/un.h>
                     60: #endif
                     61: #include<signal.h>
                     62:
                     63: #define SOCKQUEUELENGTH 5
                     64: #define ISIZ sizeof(int)
                     65:
                     66: extern int little_endian;
                     67:
1.9       noro       68: int I_am_server;
1.1       noro       69: struct IOFP iofp[MAXIOFP];
                     70:
                     71: #if !defined(_PA_RISC1_1)
                     72: #define RSH "rsh"
                     73: #else
                     74: #define RSH "remsh"
                     75: #endif
                     76:
                     77: void init_socket(void);
                     78:
                     79: int getremotesocket(s)
                     80: int s;
                     81: {
                     82:        return iofp[s].s;
                     83: }
                     84:
                     85: void getremotename(s,name)
                     86: int s;
                     87: char *name;
                     88: {
1.9.2.1   maekawa    89:        union {
                     90:                struct sockaddr sa;
                     91:                char data[SOCK_MAXADDRLEN];
                     92:        } dummy;
                     93:        struct sockaddr *sa;
                     94:        socklen_t len;
                     95:        char host[NI_MAXHOST];
                     96:        int rs;
                     97:
                     98:        rs = getremotesocket(s);
                     99:        len = SOCK_MAXADDRLEN;
                    100:        getpeername(rs, (struct sockaddr *)dummy.data, &len);
                    101:        sa = &(dummy.sa);
                    102:        getnameinfo(sa, sa->sa_len, host, sizeof(host), NULL, 0, 0);
                    103:        strcpy(name, host);
1.1       noro      104: }
                    105:
                    106: int generate_port(use_unix,port_str)
                    107: int use_unix;
                    108: char *port_str;
                    109: {
                    110:        double get_current_time();
                    111:        unsigned long mt_genrand();
                    112:        unsigned int port;
                    113:        static int count=0;
                    114:
1.8       noro      115: #if !defined(VISUAL)
1.1       noro      116:        if ( use_unix ) {
                    117:                sprintf(port_str,"/tmp/ox%02x.XXXXXX",count);
                    118:                count++;
                    119:                mktemp(port_str);
1.8       noro      120:        } else
                    121: #endif
                    122:        {
1.1       noro      123:                port = ((unsigned int)mt_genrand()+(unsigned int)get_current_time())
                    124:                        %(65536-1024)+1024;
                    125:                sprintf(port_str,"%d",port);
                    126:        }
                    127: }
                    128:
                    129: int try_bind_listen(use_unix,port_str)
                    130: int use_unix;
                    131: char *port_str;
                    132: {
1.9.2.1   maekawa   133:        struct addrinfo hints, *res, *ai;
                    134:        int s, error;
                    135:        char *errstr;
1.1       noro      136:
1.9.2.1   maekawa   137:        memset(&hints, 0, sizeof(hints));
                    138: #if defined(VISUAL)
                    139:        hints.ai_family = PF_UNSPEC;
1.1       noro      140: #else
1.9.2.1   maekawa   141:        if (use_unix)
                    142:                hints.ai_family = PF_UNIX;
                    143:        else
                    144:                hints.ai_family = PF_UNSPEC;
                    145: #endif /* VISUAL */
                    146:        hints.ai_socktype = SOCK_STREAM;
                    147:
                    148:        error = getaddrinfo(NULL, port_str, &hints, &res);
                    149:        if (error) {
                    150:                warnx("try_bind_listen(): %s", gai_strerror(error));
                    151:                return (-1);
                    152:        }
                    153:
                    154:        for (ai = res ; ai != NULL ; ai = ai->ai_next) {
                    155:                if ((s = socket(ai->ai_family, ai->ai_socktype,
                    156:                                ai->ai_protocol)) < 0 ) {
                    157:                        errstr = "in socket";
                    158:                        continue;
                    159:                }
                    160:
                    161:                if (bind(s, ai->ai_addr, ai->ai_addrlen) < 0) {
                    162:                        errstr = "in bind";
                    163:                        close(s);
                    164:                        s = -1;
                    165:                        continue;
                    166:                }
                    167:
                    168:                if (listen(s, SOCKQUEUELENGTH) < 0) {
                    169:                        errstr = "in listen";
                    170:                        close(s);
                    171:                        s = -1;
                    172:                        continue;
                    173:                }
                    174:
                    175:                /* established connection */
                    176:                break;
1.1       noro      177:        }
1.9.2.1   maekawa   178:        freeaddrinfo(res);
                    179:
                    180:        if (s < 0)
                    181:                perror(errstr);
                    182:        return (s);
1.1       noro      183: }
                    184:
                    185: /*
                    186:   try to accept a connection request
                    187:
                    188:   Input
                    189:     s: socket
                    190:
                    191:   Output
                    192:     c: an accepted socket which is newly created
                    193:     -1: if failed to accept
                    194:
                    195:   the original socket is always closed.
                    196: */
                    197:
1.9.2.2 ! maekawa   198: int try_accept(s)
        !           199: int s;
1.1       noro      200: {
1.9.2.1   maekawa   201:        union {
                    202:                struct sockaddr sa;
                    203:                char data[SOCK_MAXADDRLEN];
                    204:        } dummy;
                    205:        socklen_t len;
                    206:        int c, i;
                    207:
                    208:        len = SOCK_MAXADDRLEN;
                    209:        if (getsockname(s, (struct sockaddr *)dummy.data, &len) < 0) {
                    210:                close(s);
                    211:                return (-1)
                    212:        }
                    213:
                    214:        for (i = 0 ; i < 10 ; i++) {
                    215:                c = accept(s, &(dummy.sa), &len);
                    216:                if (c >= 0) {
                    217:                        close(s);
                    218:                        return (c);
                    219:                }
1.1       noro      220:        }
1.9.2.1   maekawa   221:
1.1       noro      222:        close(s);
1.9.2.1   maekawa   223:        return (-1);
1.1       noro      224: }
                    225:
                    226: int try_connect(use_unix,host,port_str)
                    227: int use_unix;
                    228: char *host,*port_str;
                    229: {
1.9.2.1   maekawa   230:        struct addrinfo hints, *res, *ai;
                    231:        int s, error, i;
                    232:        char *errstr;
1.1       noro      233:
1.9.2.1   maekawa   234:        memset(&hints, 0, sizeof(hints));
                    235: #if defined(VISUAL)
                    236:        hints.ai_family = PF_UNSPEC;
1.1       noro      237: #else
1.9.2.1   maekawa   238:        if (use_unix)
                    239:                hints.ai_family = PF_UNIX;
                    240:        else
                    241:                hints.ai_family = PF_UNSPEC;
1.2       noro      242: #endif /* VISUAL */
1.9.2.1   maekawa   243:        hints.ai_socktype = SOCK_STREAM;
                    244:
                    245:        error = getaddrinfo(host, port_str, &hints, &res);
                    246:        if (error) {
                    247:                warnx("try_connect: %s", gai_strerror(error));
                    248:                return (-1);
                    249:        }
                    250:        for (i = 0 ; i < 10 ; i++) {
                    251:                for (ai = res ; ai != NULL ; ai = ai->ai_next) {
                    252:                        if ((s = socket(ai->ai_family, ai->ai_socktype,
                    253:                                        ai->ai_protocol)) < 0 ) {
                    254:                                errstr = "socket";
                    255:                                continue;
1.1       noro      256:                        }
1.9.2.1   maekawa   257:                        if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0) {
                    258:                                errstr = "connect";
                    259:                                close(s);
                    260:                                s = -1;
                    261:                                continue;
1.1       noro      262:                        }
1.9.2.1   maekawa   263:
                    264:                        /* established a connection */
1.1       noro      265:                        break;
1.9.2.1   maekawa   266:                }
                    267:
                    268:                if (s >= 0) {
                    269:                        freeaddrinfo(res);
                    270:                        return (s);
                    271:                }
                    272:
1.2       noro      273: #if defined(VISUAL)
1.9.2.1   maekawa   274:                Sleep(100);
1.2       noro      275: #else
1.9.2.1   maekawa   276:                usleep(100000);
1.2       noro      277: #endif
1.1       noro      278:        }
1.9.2.1   maekawa   279:        freeaddrinfo(res);
                    280:
                    281:        perror(errstr);
                    282:        return (-1);
1.1       noro      283: }
                    284:
1.9       noro      285: #if 0
1.1       noro      286: close_allconnections()
                    287: {
                    288:        int s;
                    289:
                    290: #if defined(SIGPIPE)
                    291:        signal(SIGPIPE,SIG_IGN);
                    292: #endif
                    293:        for ( s = 0; s < MAXIOFP; s++ )
                    294:                close_connection(s);
                    295: }
                    296:
                    297: close_connection(s)
                    298: int s;
                    299: {
                    300:        struct IOFP *r;
                    301:
                    302:        r = &iofp[s];
                    303:        if ( r->in && r->out ) {
                    304:                if ( check_sm_by_mc(s,SM_shutdown) )
                    305:                        ox_send_cmd(s,SM_shutdown);
                    306:                free_iofp(s);
                    307:        }
                    308: }
1.9       noro      309: #else
                    310: close_allconnections()
                    311: {
                    312:        shutdown_all();
                    313: }
                    314: #endif
1.1       noro      315:
                    316: free_iofp(s)
                    317: int s;
                    318: {
                    319:        struct IOFP *r;
                    320:
                    321:        r = &iofp[s];
                    322:        r->in = r->out = 0; r->s = 0;
                    323: #if !defined(VISUAL)
                    324:        if ( r->socket )
                    325:                unlink(r->socket);
                    326: #endif
                    327: }
                    328:
                    329: #define LBUFSIZ BUFSIZ*10
                    330:
                    331: int get_iofp(s1,af_sock,is_server)
                    332: int s1;
                    333: char *af_sock;
                    334: int is_server;
                    335: {
                    336:        int i;
                    337:        unsigned char c,rc;
1.4       noro      338:        extern int mpi_myid;
1.1       noro      339:
1.4       noro      340: #if MPI
                    341:        iofp[s1].s = s1;
                    342:        if ( mpi_myid == s1 ) {
                    343:                iofp[s1].in = 0;
                    344:                iofp[s1].out = 0;
                    345:        } else {
                    346:                iofp[s1].in = WSIO_open(s1,"r");
                    347:                iofp[s1].out = WSIO_open(s1,"w");
                    348:        }
                    349:        iofp[s1].conv = 0;
                    350:        iofp[s1].socket = 0;
                    351:
                    352:        return s1;
                    353: #else
1.1       noro      354:        for ( i = 0; i < MAXIOFP; i++ )
                    355:                if ( !iofp[i].in )
                    356:                        break;
                    357:        iofp[i].s = s1;
1.4       noro      358: #if defined(VISUAL)
1.1       noro      359:        iofp[i].in = WSIO_open(s1,"r");
                    360:        iofp[i].out = WSIO_open(s1,"w");
                    361: #else
                    362:        iofp[i].in = fdopen(s1,"r");
                    363:        iofp[i].out = fdopen(s1,"w");
                    364:        setbuffer(iofp[i].in,(char *)malloc(LBUFSIZ),LBUFSIZ);
                    365:        setbuffer(iofp[i].out,(char *)malloc(LBUFSIZ),LBUFSIZ);
                    366: #endif
                    367:        if ( little_endian )
                    368:                c = 1;
                    369:        else
                    370:                c = 0xff;
                    371:        if ( is_server ) {
                    372:                /* server : write -> read */
                    373:                write_char(iofp[i].out,&c); ox_flush_stream_force(i);
                    374:                read_char(iofp[i].in,&rc);
                    375:        } else {
                    376:                /* client : read -> write */
                    377:                read_char(iofp[i].in,&rc);
1.7       noro      378:                /* special care for a failure of spawing a server */
                    379:                if ( rc !=0 && rc != 1 && rc != 0xff )
                    380:                        return -1;
1.1       noro      381:                write_char(iofp[i].out,&c); ox_flush_stream_force(i);
                    382:        }
                    383:        iofp[i].conv = c == rc ? 0 : 1;
                    384:        if ( af_sock && af_sock[0] ) {
                    385:                iofp[i].socket = (char *)malloc(strlen(af_sock)+1);
                    386:                strcpy(iofp[i].socket,af_sock);
                    387:        } else
                    388:                iofp[i].socket = 0;
1.4       noro      389:        return i;
1.1       noro      390: #endif
                    391: }
                    392:
                    393: #if defined(VISUAL)
                    394: void init_socket()
                    395: {
                    396:        static int socket_is_initialized;
                    397:        WORD wVersionRequested;
                    398:        WSADATA wsaData;
                    399:        int err;
                    400:        wVersionRequested = MAKEWORD(2,0);
                    401:
                    402:        if ( socket_is_initialized )
                    403:                return;
                    404:        err = WSAStartup(wVersionRequested,&wsaData);
                    405:        if ( err )
                    406:                return;
                    407: }
                    408: #endif
                    409:
                    410: get_fd(index)
                    411: int index;
                    412: {
                    413:        return iofp[index].s;
                    414: }
                    415:
                    416: get_index(fd)
                    417: int fd;
                    418: {
                    419:        int i;
                    420:
                    421:        for ( i = 0; i < MAXIOFP; i++ )
                    422:                if ( iofp[i].s == fd )
                    423:                        return i;
                    424:        return -1;
                    425: }
1.9       noro      426:
1.1       noro      427: #endif /* INET */
1.9       noro      428:

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