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