Annotation of OpenXM_contrib2/asir2000/io/sio.c, Revision 1.26
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.26 ! fujimoto 47: * $OpenXM: OpenXM_contrib2/asir2000/io/sio.c,v 1.25 2012/12/17 07:20:45 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.26 ! fujimoto 52: #if defined(VISUAL) || defined(__MINGW32__) || defined(__MINGW64__)
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.26 ! fujimoto 79: #if !defined(VISUAL) && !defined(__MINGW32__) && !defined(__MINGW64__)
1.24 ohara 80: #define closesocket(s) (close((s)))
81: #endif
82:
1.12 noro 83: int getremotesocket(int s)
1.1 noro 84: {
85: return iofp[s].s;
86: }
87:
1.12 noro 88: void getremotename(int s,char *name)
1.1 noro 89: {
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));
101: }
102:
1.12 noro 103: void generate_port(int use_unix,char *port_str)
1.1 noro 104: {
105: double get_current_time();
106: unsigned long mt_genrand();
107: unsigned int port;
108: static int count=0;
109:
1.26 ! fujimoto 110: #if !defined(VISUAL) && !defined(__MINGW32__) && !defined(__MINGW64__)
1.1 noro 111: if ( use_unix ) {
112: sprintf(port_str,"/tmp/ox%02x.XXXXXX",count);
113: count++;
114: mktemp(port_str);
1.8 noro 115: } else
116: #endif
117: {
1.1 noro 118: port = ((unsigned int)mt_genrand()+(unsigned int)get_current_time())
119: %(65536-1024)+1024;
120: sprintf(port_str,"%d",port);
121: }
122: }
123:
1.12 noro 124: int try_bind_listen(int use_unix,char *port_str)
1.1 noro 125: {
126: struct sockaddr_in sin;
127: struct sockaddr *saddr;
128: int len;
129: int service;
1.26 ! fujimoto 130: #if !defined(VISUAL) && !defined(__MINGW32__) && !defined(__MINGW64__)
1.2 noro 131: struct sockaddr_un s_un;
1.1 noro 132:
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);
141: #if defined(__FreeBSD__)
142: len = SUN_LEN(&s_un);
143: s_un.sun_len = len+1; /* XXX */
144: #else
145: len = strlen(s_un.sun_path)+sizeof(s_un.sun_family);
146: #endif
147: saddr = (struct sockaddr *)&s_un;
1.2 noro 148: } else
149: #endif
150: {
1.1 noro 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;
1.12 noro 157: sin.sin_port = htons((unsigned short)atoi(port_str));
1.1 noro 158: len = sizeof(sin);
159: saddr = (struct sockaddr *)&sin;
160: }
161: if (bind(service, saddr, len) < 0) {
162: perror("in bind");
1.24 ohara 163: closesocket(service);
1.1 noro 164: return -1;
165: }
166: if (getsockname(service,saddr, &len) < 0) {
167: perror("in getsockname");
1.24 ohara 168: closesocket(service);
1.1 noro 169: return -1;
170: }
171: if (listen(service, SOCKQUEUELENGTH) < 0) {
172: perror("in listen");
1.24 ohara 173: closesocket(service);
1.1 noro 174: return -1;
175: }
176: return service;
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: {
195: int len,c,i;
196: struct sockaddr_in sin;
197:
1.26 ! fujimoto 198: #if !defined(VISUAL) && !defined(__MINGW32__) && !defined(__MINGW64__)
1.2 noro 199: struct sockaddr_un s_un;
1.1 noro 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);
1.2 noro 204: } else
205: #endif
206: {
1.1 noro 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;
1.24 ohara 214: closesocket(s);
1.1 noro 215: return c;
216: }
217:
1.12 noro 218: int try_connect(int use_unix,char *host,char *port_str)
1.1 noro 219: {
220: struct sockaddr_in sin;
221: struct sockaddr *saddr;
222: struct hostent *hp;
223: int len,s,i;
1.26 ! fujimoto 224: #if !defined(VISUAL) && !defined(__MINGW32__) && !defined(__MINGW64__)
1.2 noro 225: struct sockaddr_un s_un;
226: #endif
1.1 noro 227:
228: for ( i = 0; i < 10; i++ ) {
1.26 ! fujimoto 229: #if !defined(VISUAL) && !defined(__MINGW32__) && !defined(__MINGW64__)
1.1 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);
238: #if defined(__FreeBSD__)
239: len = SUN_LEN(&s_un);
240: s_un.sun_len = len+1; /* XXX */
241: #else
242: len = strlen(s_un.sun_path)+sizeof(s_un.sun_family);
243: #endif
244: saddr = (struct sockaddr *)&s_un;
1.2 noro 245: } else
246: #endif /* VISUAL */
247: {
1.15 noro 248: if ( !host )
249: host = "127.0.0.1";
1.1 noro 250: if ( (s = socket(AF_INET,SOCK_STREAM,0)) < 0 ) {
251: perror("socket");
252: return -1;
253: }
254: bzero(&sin,sizeof(sin));
1.12 noro 255: sin.sin_port = htons((unsigned short)atoi(port_str));
1.1 noro 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 {
1.24 ohara 270: closesocket(s);
1.26 ! fujimoto 271: #if defined(VISUAL) || defined(__MINGW32__) || defined(__MINGW64__)
1.2 noro 272: Sleep(100);
273: #else
1.1 noro 274: usleep(100000);
1.2 noro 275: #endif
1.1 noro 276: }
277: }
278: if ( i == 10 ) {
279: perror("connect");
280: return -1;
281: } else
282: return s;
283: }
284:
1.9 noro 285: #if 0
1.12 noro 286: void close_allconnections()
1.1 noro 287: {
288: int s;
289:
290: #if defined(SIGPIPE)
291: signal(SIGPIPE,SIG_IGN);
292: #endif
293: for ( s = 0; s < MAXIOFP; s++ )
294: close_connection(s);
295: }
296:
1.12 noro 297: void close_connection(int s)
1.1 noro 298: {
299: struct IOFP *r;
300:
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: }
307: }
1.9 noro 308: #else
1.12 noro 309: void close_allconnections()
1.9 noro 310: {
311: shutdown_all();
312: }
313: #endif
1.1 noro 314:
1.12 noro 315: void free_iofp(int s)
1.1 noro 316: {
317: struct IOFP *r;
318:
319: r = &iofp[s];
1.26 ! fujimoto 320: #if defined(VISUAL) || defined(__MINGW32__) || defined(__MINGW64__)
1.24 ohara 321: if ( r->s ) closesocket(r->s);
1.23 noro 322: #elif !defined(MPI)
1.17 noro 323: if ( r->in ) fclose(r->in);
324: if ( r->out ) fclose(r->out);
325: if ( r->socket ) unlink(r->socket);
326: #endif
1.20 noro 327: r->inbuf = r->outbuf = 0;
1.16 noro 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: {
333: int i;
334: unsigned char c,rc;
1.4 noro 335: extern int mpi_myid;
1.1 noro 336:
1.23 noro 337: #if defined(MPI)
1.4 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;
348:
349: return s1;
350: #else
1.1 noro 351: for ( i = 0; i < MAXIOFP; i++ )
352: if ( !iofp[i].in )
353: break;
354: iofp[i].s = s1;
1.26 ! fujimoto 355: #if defined(VISUAL) || defined(__MINGW32__) || defined(__MINGW64__)
1.1 noro 356: iofp[i].in = WSIO_open(s1,"r");
357: iofp[i].out = WSIO_open(s1,"w");
358: #else
359: iofp[i].in = fdopen(s1,"r");
360: iofp[i].out = fdopen(s1,"w");
1.13 noro 361: #if !defined(__CYGWIN__)
1.25 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
366: if ( little_endian )
367: c = 1;
368: else
369: c = 0xff;
370: if ( is_server ) {
371: /* server : write -> read */
1.12 noro 372: write_char((FILE *)iofp[i].out,&c); ox_flush_stream_force(i);
373: read_char((FILE *)iofp[i].in,&rc);
1.1 noro 374: } else {
375: /* client : read -> write */
1.12 noro 376: read_char((FILE *)iofp[i].in,&rc);
1.7 noro 377: /* special care for a failure of spawing a server */
378: if ( rc !=0 && rc != 1 && rc != 0xff )
379: return -1;
1.12 noro 380: write_char((FILE *)iofp[i].out,&c); ox_flush_stream_force(i);
1.1 noro 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;
1.4 noro 388: return i;
1.1 noro 389: #endif
390: }
391:
1.26 ! fujimoto 392: #if defined(VISUAL) || defined(__MINGW32__) || defined(__MINGW64__)
1.1 noro 393: void init_socket()
394: {
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;
406: }
407: #endif
408:
1.12 noro 409: int get_fd(int index)
1.1 noro 410: {
411: return iofp[index].s;
412: }
413:
1.12 noro 414: int get_index(int fd)
1.1 noro 415: {
416: int i;
417:
418: for ( i = 0; i < MAXIOFP; i++ )
419: if ( iofp[i].s == fd )
420: return i;
421: return -1;
422: }
1.9 noro 423:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>