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

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

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