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

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

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