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