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

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

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