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

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

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