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

Annotation of OpenXM_contrib2/asir2018/io/sio.c, Revision 1.1

1.1     ! 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
        !            26:  * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
        !            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.
        !            47:  * $OpenXM$
        !            48: */
        !            49: #include "ca.h"
        !            50: #include <setjmp.h>
        !            51: #include "ox.h"
        !            52: #if defined(VISUAL) || defined(__MINGW32__)
        !            53: #include <winsock2.h>
        !            54: #include <io.h>
        !            55: #else
        !            56: #include <sys/time.h>
        !            57: #include <sys/uio.h>
        !            58: #include <sys/ioctl.h>
        !            59: #include <sys/un.h>
        !            60: #include <sys/socket.h>
        !            61: #include <netinet/in.h>
        !            62: #include <arpa/inet.h>
        !            63: #endif
        !            64: #include<signal.h>
        !            65:
        !            66: #define SOCKQUEUELENGTH 5
        !            67: #define ISIZ sizeof(int)
        !            68:
        !            69: extern int little_endian;
        !            70:
        !            71: int I_am_server;
        !            72: struct IOFP iofp[MAXIOFP];
        !            73:
        !            74: #if !defined(_PA_RISC1_1)
        !            75: #define RSH "rsh"
        !            76: #else
        !            77: #define RSH "remsh"
        !            78: #endif
        !            79:
        !            80: void init_socket(void);
        !            81:
        !            82: #if !defined(VISUAL) && !defined(__MINGW32__)
        !            83: #define closesocket(s)   (close((s)))
        !            84: #endif
        !            85:
        !            86: int getremotesocket(int s)
        !            87: {
        !            88:   return iofp[s].s;
        !            89: }
        !            90:
        !            91: void getremotename(int s,char *name)
        !            92: {
        !            93:   struct sockaddr_in peer;
        !            94:   struct hostent *hp;
        !            95:   int peerlen;
        !            96:
        !            97:   peerlen = sizeof(peer);
        !            98:   getpeername(getremotesocket(s),(struct sockaddr *)&peer,&peerlen);
        !            99:   hp = gethostbyaddr((char *)&peer.sin_addr,sizeof(struct in_addr),AF_INET);
        !           100:   if ( hp )
        !           101:     strcpy(name,hp->h_name);
        !           102:   else
        !           103:     strcpy(name,(char *)inet_ntoa(peer.sin_addr));
        !           104: }
        !           105:
        !           106: void generate_port(int use_unix,char *port_str)
        !           107: {
        !           108:   double get_current_time();
        !           109:   unsigned long mt_genrand();
        !           110:   unsigned int port;
        !           111:   static int count=0;
        !           112:
        !           113: #if !defined(VISUAL) && !defined(__MINGW32__)
        !           114:   if ( use_unix ) {
        !           115:     sprintf(port_str,"/tmp/ox%02x.XXXXXX",count);
        !           116:     count++;
        !           117:     mktemp(port_str);
        !           118:   } else
        !           119: #endif
        !           120:   {
        !           121:     port = ((unsigned int)mt_genrand()+(unsigned int)get_current_time())
        !           122:       %(65536-1024)+1024;
        !           123:     sprintf(port_str,"%d",port);
        !           124:   }
        !           125: }
        !           126:
        !           127: int try_bind_listen(int use_unix,char *port_str)
        !           128: {
        !           129:   struct sockaddr_in sin;
        !           130:   struct sockaddr *saddr;
        !           131:   int len;
        !           132:   int service;
        !           133: #if !defined(VISUAL) && !defined(__MINGW32__)
        !           134:   struct sockaddr_un s_un;
        !           135:
        !           136:   if ( use_unix ) {
        !           137:     service = socket(AF_UNIX, SOCK_STREAM, 0);
        !           138:     if (service < 0) {
        !           139:       perror("in socket");
        !           140:       return -1;
        !           141:     }
        !           142:     s_un.sun_family = AF_UNIX;
        !           143:     strcpy(s_un.sun_path,port_str);
        !           144: #if defined(__FreeBSD__)
        !           145:     len = SUN_LEN(&s_un);
        !           146:     s_un.sun_len = len+1; /* XXX */
        !           147: #else
        !           148:     len = strlen(s_un.sun_path)+sizeof(s_un.sun_family);
        !           149: #endif
        !           150:     saddr = (struct sockaddr *)&s_un;
        !           151:   } else
        !           152: #endif
        !           153:   {
        !           154:     service = socket(AF_INET, SOCK_STREAM, 0);
        !           155:     if ( service < 0 ) {
        !           156:       perror("in socket");
        !           157:       return -1;
        !           158:     }
        !           159:     sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY;
        !           160:     sin.sin_port = htons((unsigned short)atoi(port_str));
        !           161:     len = sizeof(sin);
        !           162:     saddr = (struct sockaddr *)&sin;
        !           163:   }
        !           164:   if (bind(service, saddr, len) < 0) {
        !           165:     perror("in bind");
        !           166:     closesocket(service);
        !           167:     return -1;
        !           168:   }
        !           169:   if (getsockname(service,saddr, &len) < 0) {
        !           170:       perror("in getsockname");
        !           171:       closesocket(service);
        !           172:       return -1;
        !           173:   }
        !           174:   if (listen(service, SOCKQUEUELENGTH) < 0) {
        !           175:     perror("in listen");
        !           176:     closesocket(service);
        !           177:     return -1;
        !           178:   }
        !           179:   return service;
        !           180: }
        !           181:
        !           182: /*
        !           183:   try to accept a connection request
        !           184:
        !           185:   Input
        !           186:     af_unix: s is UNIX domain socket if af_unix is nonzero
        !           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:
        !           196: int try_accept(int af_unix,int s)
        !           197: {
        !           198:   int len,c,i;
        !           199:   struct sockaddr_in sin;
        !           200:
        !           201: #if !defined(VISUAL) && !defined(__MINGW32__)
        !           202:   struct sockaddr_un s_un;
        !           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);
        !           207:   } else
        !           208: #endif
        !           209:   {
        !           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:   closesocket(s);
        !           218:   return c;
        !           219: }
        !           220:
        !           221: int try_connect(int use_unix,char *host,char *port_str)
        !           222: {
        !           223:   struct sockaddr_in sin;
        !           224:   struct sockaddr *saddr;
        !           225:   struct hostent *hp;
        !           226:   int len,s,i;
        !           227: #if !defined(VISUAL) && !defined(__MINGW32__)
        !           228:   struct sockaddr_un s_un;
        !           229: #endif
        !           230:
        !           231:   for ( i = 0; i < 10; i++ ) {
        !           232: #if !defined(VISUAL) && !defined(__MINGW32__)
        !           233:     if ( use_unix ) {
        !           234:       if ( (s = socket(AF_UNIX,SOCK_STREAM,0)) < 0 ) {
        !           235:         perror("socket");
        !           236:         return -1;
        !           237:       }
        !           238:       bzero(&s_un,sizeof(s_un));
        !           239:       s_un.sun_family = AF_UNIX;
        !           240:       strcpy(s_un.sun_path,port_str);
        !           241: #if defined(__FreeBSD__)
        !           242:       len = SUN_LEN(&s_un);
        !           243:       s_un.sun_len = len+1; /* XXX */
        !           244: #else
        !           245:       len = strlen(s_un.sun_path)+sizeof(s_un.sun_family);
        !           246: #endif
        !           247:       saddr = (struct sockaddr *)&s_un;
        !           248:     } else
        !           249: #endif /* VISUAL */
        !           250:     {
        !           251:       if ( !host )
        !           252:         host = "127.0.0.1";
        !           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((unsigned short)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:       closesocket(s);
        !           274: #if defined(VISUAL) || defined(__MINGW32__)
        !           275:       Sleep(100);
        !           276: #else
        !           277:       usleep(100000);
        !           278: #endif
        !           279:     }
        !           280:   }
        !           281:   if ( i == 10 ) {
        !           282:     perror("connect");
        !           283:     return -1;
        !           284:   } else
        !           285:     return s;
        !           286: }
        !           287:
        !           288: #if 0
        !           289: void 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: void close_connection(int s)
        !           301: {
        !           302:   struct IOFP *r;
        !           303:
        !           304:   r = &iofp[s];
        !           305:   if ( r->in && r->out ) {
        !           306:     if ( check_sm_by_mc(s,SM_shutdown) )
        !           307:       ox_send_cmd(s,SM_shutdown);
        !           308:     free_iofp(s);
        !           309:   }
        !           310: }
        !           311: #else
        !           312: void close_allconnections()
        !           313: {
        !           314:   shutdown_all();
        !           315: }
        !           316: #endif
        !           317:
        !           318: void free_iofp(int s)
        !           319: {
        !           320:   struct IOFP *r;
        !           321:
        !           322:   r = &iofp[s];
        !           323: #if defined(VISUAL) || defined(__MINGW32__)
        !           324:   if ( r->s ) closesocket(r->s);
        !           325: #elif !defined(MPI)
        !           326:   if ( r->in ) fclose(r->in);
        !           327:   if ( r->out ) fclose(r->out);
        !           328:   if ( r->socket ) unlink(r->socket);
        !           329: #endif
        !           330:   r->inbuf = r->outbuf = 0;
        !           331:   r->in = r->out = 0; r->s = 0;
        !           332: }
        !           333:
        !           334: int get_iofp(int s1,char *af_sock,int is_server)
        !           335: {
        !           336:   int i;
        !           337:   unsigned char c,rc;
        !           338:   extern int mpi_myid;
        !           339:
        !           340: #if defined(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
        !           354:   for ( i = 0; i < MAXIOFP; i++ )
        !           355:     if ( !iofp[i].in )
        !           356:       break;
        !           357:   iofp[i].s = s1;
        !           358: #if defined(VISUAL) || defined(__MINGW32__)
        !           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: #if !defined(__CYGWIN__)
        !           365:   setbuffer(iofp[i].in,iofp[i].inbuf = (char *)MALLOC_ATOMIC(LBUFSIZ),LBUFSIZ);
        !           366:   setbuffer(iofp[i].out,iofp[i].outbuf = (char *)MALLOC_ATOMIC(LBUFSIZ),LBUFSIZ);
        !           367: #endif
        !           368: #endif
        !           369:   if ( little_endian )
        !           370:     c = 1;
        !           371:   else
        !           372:     c = 0xff;
        !           373:   if ( is_server ) {
        !           374:     /* server : write -> read */
        !           375:     write_char((FILE *)iofp[i].out,&c); ox_flush_stream_force(i);
        !           376:     read_char((FILE *)iofp[i].in,&rc);
        !           377:   } else {
        !           378:     /* client : read -> write */
        !           379:     read_char((FILE *)iofp[i].in,&rc);
        !           380:     /* special care for a failure of spawing a server */
        !           381:     if ( rc !=0 && rc != 1 && rc != 0xff )
        !           382:       return -1;
        !           383:     write_char((FILE *)iofp[i].out,&c); ox_flush_stream_force(i);
        !           384:   }
        !           385:   iofp[i].conv = c == rc ? 0 : 1;
        !           386:   if ( af_sock && af_sock[0] ) {
        !           387:     iofp[i].socket = (char *)malloc(strlen(af_sock)+1);
        !           388:     strcpy(iofp[i].socket,af_sock);
        !           389:   } else
        !           390:     iofp[i].socket = 0;
        !           391:   return i;
        !           392: #endif
        !           393: }
        !           394:
        !           395: #if defined(VISUAL) || defined(__MINGW32__)
        !           396: void init_socket()
        !           397: {
        !           398:   static int socket_is_initialized;
        !           399:   WORD wVersionRequested;
        !           400:   WSADATA wsaData;
        !           401:   int err;
        !           402:   wVersionRequested = MAKEWORD(2,0);
        !           403:
        !           404:   if ( socket_is_initialized )
        !           405:     return;
        !           406:   err = WSAStartup(wVersionRequested,&wsaData);
        !           407:   if ( err )
        !           408:     return;
        !           409: }
        !           410: #endif
        !           411:
        !           412: int get_fd(int index)
        !           413: {
        !           414:   return iofp[index].s;
        !           415: }
        !           416:
        !           417: int get_index(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: }
        !           426:

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