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