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