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

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

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