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

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

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