Annotation of OpenXM_contrib2/asir2000/io/sio.c, Revision 1.8
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.8 ! noro 47: * $OpenXM: OpenXM_contrib2/asir2000/io/sio.c,v 1.7 2000/09/23 00:57:43 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:
68: struct IOFP iofp[MAXIOFP];
69:
70: #if !defined(_PA_RISC1_1)
71: #define RSH "rsh"
72: #else
73: #define RSH "remsh"
74: #endif
75:
76: void init_socket(void);
77:
78: int getremotesocket(s)
79: int s;
80: {
81: return iofp[s].s;
82: }
83:
84: void getremotename(s,name)
85: int s;
86: char *name;
87: {
88: struct sockaddr_in peer;
89: struct hostent *hp;
90: int peerlen;
91:
92: peerlen = sizeof(peer);
93: getpeername(getremotesocket(s),(struct sockaddr *)&peer,&peerlen);
94: hp = gethostbyaddr((char *)&peer.sin_addr,sizeof(struct in_addr),AF_INET);
95: if ( hp )
96: strcpy(name,hp->h_name);
97: else
98: strcpy(name,(char *)inet_ntoa(peer.sin_addr));
99: }
100:
101: int generate_port(use_unix,port_str)
102: int use_unix;
103: char *port_str;
104: {
105: double get_current_time();
106: unsigned long mt_genrand();
107: unsigned int port;
108: static int count=0;
109:
1.8 ! noro 110: #if !defined(VISUAL)
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:
124: int try_bind_listen(use_unix,port_str)
125: int use_unix;
126: char *port_str;
127: {
128: struct sockaddr_in sin;
129: struct sockaddr *saddr;
130: int len;
131: int service;
1.2 noro 132: #if !defined(VISUAL)
133: struct sockaddr_un s_un;
1.1 noro 134:
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);
143: #if defined(__FreeBSD__)
144: len = SUN_LEN(&s_un);
145: s_un.sun_len = len+1; /* XXX */
146: #else
147: len = strlen(s_un.sun_path)+sizeof(s_un.sun_family);
148: #endif
149: saddr = (struct sockaddr *)&s_un;
1.2 noro 150: } else
151: #endif
152: {
1.1 noro 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(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: close(service);
166: return -1;
167: }
168: if (getsockname(service,saddr, &len) < 0) {
169: perror("in getsockname");
170: close(service);
171: return -1;
172: }
173: if (listen(service, SOCKQUEUELENGTH) < 0) {
174: perror("in listen");
175: close(service);
176: return -1;
177: }
178: return service;
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:
195: int try_accept(af_unix,s)
196: int af_unix,s;
197: {
198: int len,c,i;
199: struct sockaddr_in sin;
200:
1.2 noro 201: #if !defined(VISUAL)
202: struct sockaddr_un s_un;
1.1 noro 203: if ( af_unix ) {
204: len = sizeof(s_un);
205: for ( c = -1, i = 0; (c < 0)&&(i = 10) ; i++ )
206: c = accept(s, (struct sockaddr *) &s_un, &len);
1.2 noro 207: } else
208: #endif
209: {
1.1 noro 210:
211: len = sizeof(sin);
212: for ( c = -1, i = 0; (c < 0)&&(i = 10) ; i++ )
213: c = accept(s, (struct sockaddr *) &sin, &len);
214: }
215: if ( i == 10 )
216: c = -1;
217: close(s);
218: return c;
219: }
220:
221: int try_connect(use_unix,host,port_str)
222: int use_unix;
223: char *host,*port_str;
224: {
225: struct sockaddr_in sin;
226: struct sockaddr *saddr;
227: struct hostent *hp;
228: int len,s,i;
1.2 noro 229: #if !defined(VISUAL)
230: struct sockaddr_un s_un;
231: #endif
1.1 noro 232:
233: for ( i = 0; i < 10; i++ ) {
1.2 noro 234: #if !defined(VISUAL)
1.1 noro 235: if ( use_unix ) {
236: if ( (s = socket(AF_UNIX,SOCK_STREAM,0)) < 0 ) {
237: perror("socket");
238: return -1;
239: }
240: bzero(&s_un,sizeof(s_un));
241: s_un.sun_family = AF_UNIX;
242: strcpy(s_un.sun_path,port_str);
243: #if defined(__FreeBSD__)
244: len = SUN_LEN(&s_un);
245: s_un.sun_len = len+1; /* XXX */
246: #else
247: len = strlen(s_un.sun_path)+sizeof(s_un.sun_family);
248: #endif
249: saddr = (struct sockaddr *)&s_un;
1.2 noro 250: } else
251: #endif /* VISUAL */
252: {
1.1 noro 253: if ( (s = socket(AF_INET,SOCK_STREAM,0)) < 0 ) {
254: perror("socket");
255: return -1;
256: }
257: bzero(&sin,sizeof(sin));
258: sin.sin_port = htons(atoi(port_str));
259: sin.sin_addr.s_addr = inet_addr(host);
260: if ( sin.sin_addr.s_addr != -1 ) {
261: sin.sin_family = AF_INET;
262: } else {
263: hp = gethostbyname(host);
264: bcopy(hp->h_addr,&sin.sin_addr,hp->h_length);
265: sin.sin_family = hp->h_addrtype;
266: }
267: len = sizeof(sin);
268: saddr = (struct sockaddr *)&sin;
269: }
270: if ( connect(s,saddr,len) >= 0 )
271: break;
272: else {
273: close(s);
1.2 noro 274: #if defined(VISUAL)
275: Sleep(100);
276: #else
1.1 noro 277: usleep(100000);
1.2 noro 278: #endif
1.1 noro 279: }
280: }
281: if ( i == 10 ) {
282: perror("connect");
283: return -1;
284: } else
285: return s;
286: }
287:
288: close_allconnections()
289: {
290: int s;
291:
292: #if defined(SIGPIPE)
293: signal(SIGPIPE,SIG_IGN);
294: #endif
295: for ( s = 0; s < MAXIOFP; s++ )
296: close_connection(s);
297: }
298:
299: close_connection(s)
300: int s;
301: {
302: struct IOFP *r;
303:
304: r = &iofp[s];
305: if ( r->in && r->out ) {
306: if ( check_sm_by_mc(s,SM_shutdown) )
307: ox_send_cmd(s,SM_shutdown);
308: free_iofp(s);
309: }
310: }
311:
312: free_iofp(s)
313: int s;
314: {
315: struct IOFP *r;
316:
317: r = &iofp[s];
318: r->in = r->out = 0; r->s = 0;
319: #if !defined(VISUAL)
320: if ( r->socket )
321: unlink(r->socket);
322: #endif
323: }
324:
325: #define LBUFSIZ BUFSIZ*10
326:
327: int get_iofp(s1,af_sock,is_server)
328: int s1;
329: char *af_sock;
330: int is_server;
331: {
332: int i;
333: unsigned char c,rc;
1.4 noro 334: extern int mpi_myid;
1.1 noro 335:
1.4 noro 336: #if MPI
337: iofp[s1].s = s1;
338: if ( mpi_myid == s1 ) {
339: iofp[s1].in = 0;
340: iofp[s1].out = 0;
341: } else {
342: iofp[s1].in = WSIO_open(s1,"r");
343: iofp[s1].out = WSIO_open(s1,"w");
344: }
345: iofp[s1].conv = 0;
346: iofp[s1].socket = 0;
347:
348: return s1;
349: #else
1.1 noro 350: for ( i = 0; i < MAXIOFP; i++ )
351: if ( !iofp[i].in )
352: break;
353: iofp[i].s = s1;
1.4 noro 354: #if defined(VISUAL)
1.1 noro 355: iofp[i].in = WSIO_open(s1,"r");
356: iofp[i].out = WSIO_open(s1,"w");
357: #else
358: iofp[i].in = fdopen(s1,"r");
359: iofp[i].out = fdopen(s1,"w");
360: setbuffer(iofp[i].in,(char *)malloc(LBUFSIZ),LBUFSIZ);
361: setbuffer(iofp[i].out,(char *)malloc(LBUFSIZ),LBUFSIZ);
362: #endif
363: if ( little_endian )
364: c = 1;
365: else
366: c = 0xff;
367: if ( is_server ) {
368: /* server : write -> read */
369: write_char(iofp[i].out,&c); ox_flush_stream_force(i);
370: read_char(iofp[i].in,&rc);
371: } else {
372: /* client : read -> write */
373: read_char(iofp[i].in,&rc);
1.7 noro 374: /* special care for a failure of spawing a server */
375: if ( rc !=0 && rc != 1 && rc != 0xff )
376: return -1;
1.1 noro 377: write_char(iofp[i].out,&c); ox_flush_stream_force(i);
378: }
379: iofp[i].conv = c == rc ? 0 : 1;
380: if ( af_sock && af_sock[0] ) {
381: iofp[i].socket = (char *)malloc(strlen(af_sock)+1);
382: strcpy(iofp[i].socket,af_sock);
383: } else
384: iofp[i].socket = 0;
1.4 noro 385: return i;
1.1 noro 386: #endif
387: }
388:
389: #if defined(VISUAL)
390: void init_socket()
391: {
392: static int socket_is_initialized;
393: WORD wVersionRequested;
394: WSADATA wsaData;
395: int err;
396: wVersionRequested = MAKEWORD(2,0);
397:
398: if ( socket_is_initialized )
399: return;
400: err = WSAStartup(wVersionRequested,&wsaData);
401: if ( err )
402: return;
403: }
404: #endif
405:
406: get_fd(index)
407: int index;
408: {
409: return iofp[index].s;
410: }
411:
412: get_index(fd)
413: int fd;
414: {
415: int i;
416:
417: for ( i = 0; i < MAXIOFP; i++ )
418: if ( iofp[i].s == fd )
419: return i;
420: return -1;
421: }
422: #endif /* INET */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>