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