| 1 |
/*=========================================================================== |
| 2 |
Copyright (C) 1995-2009 European Southern Observatory (ESO) |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or |
| 5 |
modify it under the terms of the GNU General Public License as |
| 6 |
published by the Free Software Foundation; either version 2 of |
| 7 |
the License, or (at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public |
| 15 |
License along with this program; if not, write to the Free |
| 16 |
Software Foundation, Inc., 675 Massachusetts Ave, Cambridge, |
| 17 |
MA 02139, USA. |
| 18 |
|
| 19 |
Correspondence concerning ESO-MIDAS should be addressed as follows: |
| 20 |
Internet e-mail: [email protected] |
| 21 |
Postal address: European Southern Observatory |
| 22 |
Data Management Division |
| 23 |
Karl-Schwarzschild-Strasse 2 |
| 24 |
D 85748 Garching bei Muenchen |
| 25 |
GERMANY |
| 26 |
===========================================================================*/ |
| 27 |
|
| 28 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 29 |
.TYPE Module |
| 30 |
.NAME osx.c |
| 31 |
.LANGUAGE C |
| 32 |
.AUTHOR IPG-ESO Garching |
| 33 |
.CATEGORY Host operating system interfaces. Interprocess Communication |
| 34 |
.COMMENTS Interprocess communication routines. The routines allow the |
| 35 |
creation of asynchronous communication channels between processes |
| 36 |
possibly located in different nodes. |
| 37 |
Three modes of operation are foreseen depending on the |
| 38 |
communication channel: |
| 39 |
local communication (fast channel for processes residing in |
| 40 |
the same node), |
| 41 |
network communication (medium channel for processes located |
| 42 |
in processors interconnected with a local |
| 43 |
area network), |
| 44 |
The communication channel is open by giving the channel name |
| 45 |
and the mode of opening. The function returns an integer number |
| 46 |
used to address the channel. Operations include asynchronous |
| 47 |
input/output, wait for completion and control operations. |
| 48 |
The routines return always |
| 49 |
a non-negative integer number on successful return. |
| 50 |
Otherwise, a value of -1 is set to indicate an error |
| 51 |
condition and the variable ``oserror'' contains the |
| 52 |
symbolic error code. |
| 53 |
Symbolic error codes for each function to be defined. |
| 54 |
|
| 55 |
.HISTORY [1.1] 910828 CG. New implementation. |
| 56 |
.HISTORY [2.1] 920527 If no S_IFSOCK no local unix sockets. CG. |
| 57 |
.HISTORY [3.1] 21-Apr-1993 Posix compliant |
| 58 |
|
| 59 |
.VERSION |
| 60 |
|
| 61 |
090324 last modif |
| 62 |
------------------------------------------------------------*/ |
| 63 |
|
| 64 |
/* |
| 65 |
* Define _POSIX_SOURCE to indicate |
| 66 |
* that this is a POSIX program |
| 67 |
* Except for Maciontosh, SUN & GNU-SUN & HP-UX & OSF & AIX & ULTRIX & SGI |
| 68 |
* because the POSIX definition |
| 69 |
* causes undefines (eg. u_long) in socket includes. |
| 70 |
*/ |
| 71 |
#if !defined(__APPLE__) && !defined(__hpux) && !defined(sun) && !defined(__sun) && !defined(__osf__) && !defined(_AIX) && !defined(ultrix) && !defined(__sgi) && !defined(__linux__) && !defined(__FreeBSD__) && !defined(Cygwin) |
| 72 |
# define _POSIX_SOURCE 1 |
| 73 |
#endif |
| 74 |
# define _XOPEN_SOURCE |
| 75 |
# define _XOPEN_SOURCE_EXTENDED |
| 76 |
|
| 77 |
|
| 78 |
#include <sys/types.h> /* generic definitions */ |
| 79 |
#include <sys/socket.h> /* sockets definitions */ |
| 80 |
#include <sys/stat.h> /* stat(2v); */ |
| 81 |
|
| 82 |
#ifdef S_IFSOCK /* defined in stat.h if Unix domain sockets */ |
| 83 |
# include <sys/un.h> /* struct sockaddr_un (Unix domain) */ |
| 84 |
#endif /* S_IFSOCK */ |
| 85 |
|
| 86 |
#include <netinet/in.h> /* struct sockaddr_in (Internet domain) */ |
| 87 |
#include <netdb.h> /* getservbyname(3n), gethostbyname(3n); */ |
| 88 |
#include <sys/time.h> /* struct timeval timeout */ |
| 89 |
#include <signal.h> /* signal(3v); */ |
| 90 |
#include <errno.h> /* error definitions */ |
| 91 |
#include <sys/errno.h> /* error definitions */ |
| 92 |
#include <stdio.h> /* generic definitions */ |
| 93 |
#include <stdlib.h> /* function definitions */ |
| 94 |
#include <string.h> /* string definitions */ |
| 95 |
#include <unistd.h> /* string definitions */ |
| 96 |
#include <proto_os.h> /* ANSI-C prototyping */ |
| 97 |
|
| 98 |
#include <osxdef.h> /* MIDAS osx definitions */ |
| 99 |
|
| 100 |
|
| 101 |
/* OSF1 and HP-UX don't know socklen_t ... */ |
| 102 |
|
| 103 |
#if defined(__osf__) || defined(__hpux) |
| 104 |
# define XSOCKLEN int |
| 105 |
#else |
| 106 |
# define XSOCKLEN socklen_t |
| 107 |
#endif |
| 108 |
|
| 109 |
|
| 110 |
/* |
| 111 |
static FILE *fid; |
| 112 |
static int first_time = 0; |
| 113 |
static char logname[24] = "osxlogA.dat"; |
| 114 |
|
| 115 |
FILE *fopen(); |
| 116 |
*/ |
| 117 |
|
| 118 |
|
| 119 |
#ifndef SUN_LEN |
| 120 |
# define SUN_LEN(su) (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path)) |
| 121 |
#endif |
| 122 |
|
| 123 |
static int indxIPCC[MAX_IPCC] = |
| 124 |
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 125 |
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}; |
| 126 |
static struct ipccstat IPCC[MAX_IPCC]; |
| 127 |
|
| 128 |
#define NULL_PTR(x) (x *)0 |
| 129 |
#define RET_ERROR(x) { oserror=(x); return(-1); } |
| 130 |
|
| 131 |
static char msg0[] = "OSX: Channel out of table"; |
| 132 |
static char messg[80]; |
| 133 |
|
| 134 |
#ifndef FD_SET /* If it is not the Berkeley select */ |
| 135 |
typedef int osxfds; |
| 136 |
# define FD_SET(n, p) (*(p) |= (1 << (n)) ) |
| 137 |
#else |
| 138 |
typedef fd_set osxfds; |
| 139 |
#endif |
| 140 |
|
| 141 |
/* |
| 142 |
int osxin(m) |
| 143 |
int m; |
| 144 |
|
| 145 |
{ |
| 146 |
if (m == 0) |
| 147 |
(void) strcpy(logname,"osxlogB.dat"); |
| 148 |
else if (m == 1) |
| 149 |
(void) strcpy(logname,"osxlogC.dat"); |
| 150 |
|
| 151 |
return 0; |
| 152 |
} |
| 153 |
*/ |
| 154 |
|
| 155 |
/* |
| 156 |
|
| 157 |
*/ |
| 158 |
|
| 159 |
static int writen (fd, ptr, nbytes) |
| 160 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 161 |
.PURPOSE Write "n" bytest into a descriptor. Use in place of |
| 162 |
write() when fd is a stream socket |
| 163 |
.RETURNS If succesful, the number of bytes actaully written is |
| 164 |
returned. Otherwise, a -1 is returned. |
| 165 |
.REMARKS System dependencies: |
| 166 |
-- UNIX: write(2) |
| 167 |
------------------------------------------------------------*/ |
| 168 |
int fd; |
| 169 |
char *ptr; |
| 170 |
int nbytes; |
| 171 |
|
| 172 |
{ |
| 173 |
int nleft, nwritten; |
| 174 |
|
| 175 |
|
| 176 |
nleft = nbytes; |
| 177 |
while (nleft > 0) |
| 178 |
{ |
| 179 |
nwritten = write (fd, ptr, nleft); |
| 180 |
if (nwritten <= 0) return (nwritten); /* error */ |
| 181 |
|
| 182 |
nleft -= nwritten; |
| 183 |
ptr += nwritten; |
| 184 |
} |
| 185 |
|
| 186 |
return (nbytes - nleft); |
| 187 |
} |
| 188 |
|
| 189 |
static int readn (fd, ptr, nbytes) |
| 190 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 191 |
.PURPOSE Read "n" bytest from a descriptor. Use in place of |
| 192 |
read() when fd is a stream socket |
| 193 |
.RETURNS If succesful, the number of bytes actaully read is |
| 194 |
returned. Otherwise, a -1 is returned. |
| 195 |
.REMARKS System dependencies: |
| 196 |
-- UNIX: read(2) |
| 197 |
------------------------------------------------------------*/ |
| 198 |
int fd; |
| 199 |
char *ptr; |
| 200 |
int nbytes; |
| 201 |
|
| 202 |
{ |
| 203 |
int nleft, nread; |
| 204 |
|
| 205 |
|
| 206 |
nleft = nbytes; |
| 207 |
while (nleft > 0) |
| 208 |
{ |
| 209 |
nread = read (fd, ptr, nleft); |
| 210 |
if (nread < 0) /* error, return < 0 */ |
| 211 |
return (nread); |
| 212 |
else if (nread == 0) /* EOF */ |
| 213 |
break; |
| 214 |
nleft -= nread; |
| 215 |
ptr += nread; |
| 216 |
} |
| 217 |
|
| 218 |
return (nbytes - nleft); /* return >= 0 */ |
| 219 |
} |
| 220 |
|
| 221 |
static int osxstat (fd, sec, usec) |
| 222 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 223 |
.PURPOSE Examines the read status of a file descriptor. |
| 224 |
The timeout (sec, usec) specifies a maximum interval to |
| 225 |
wait for data to be available in the descriptor. |
| 226 |
To effect a poll, the timeout (sec, usec) should be 0. |
| 227 |
.RETURNS a non-negative value on data available. |
| 228 |
0 indicates that the time limit referred by timeout expired. |
| 229 |
On failure, it returns -1 and errno is set to indicate the |
| 230 |
error. |
| 231 |
.REMARKS System dependencies: |
| 232 |
-- UNIX: select(2) |
| 233 |
------------------------------------------------------------*/ |
| 234 |
int fd, sec, usec; |
| 235 |
|
| 236 |
{ |
| 237 |
int ret; |
| 238 |
int width; |
| 239 |
struct timeval timeout; |
| 240 |
osxfds readfds; |
| 241 |
|
| 242 |
/* |
| 243 |
fprintf(fid,"osxstat: fd = %d\n",fd); |
| 244 |
printf("osxstat: fd = %d\n",fd); |
| 245 |
*/ |
| 246 |
|
| 247 |
memset ((char *) &readfds, 0, sizeof (readfds)); |
| 248 |
FD_SET (fd, &readfds); |
| 249 |
width = fd + 1; |
| 250 |
timeout.tv_sec = sec; |
| 251 |
timeout.tv_usec = usec; |
| 252 |
ret = |
| 253 |
select (width, &readfds, NULL_PTR (osxfds), NULL_PTR (osxfds), |
| 254 |
&timeout); |
| 255 |
|
| 256 |
return (ret); |
| 257 |
} |
| 258 |
|
| 259 |
int osxopen (channame, mode) |
| 260 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 261 |
.PURPOSE Opens communication channel for read or write. The variable mode |
| 262 |
defines the way of opening the file by constructing |
| 263 |
the OR of the following flags: |
| 264 |
- LOCAL (local communication channel) |
| 265 |
- NETW (communication channel using the network) |
| 266 |
- IPC_READ (open the channel in READ mode or SERVER) |
| 267 |
- IPC_WRITE (open the channel in WRITE mode or CLIENT) |
| 268 |
.RETURNS Upon successful completion a positive number with the |
| 269 |
channel identification is returned or -1 in case of error. |
| 270 |
.REMARKS System dependencies: |
| 271 |
-- UNIX: socket(2), bind(2), listen(2), gethostbyname(3n), |
| 272 |
getservbyname(3n), mknod(2), open(2) |
| 273 |
------------------------------------------------------------*/ |
| 274 |
char *channame[2]; /* physical channel name */ |
| 275 |
int mode; /* open mode */ |
| 276 |
|
| 277 |
{ |
| 278 |
int n; |
| 279 |
int xcid, cid; |
| 280 |
|
| 281 |
char *name1; |
| 282 |
|
| 283 |
|
| 284 |
static struct stat statbuf; |
| 285 |
static struct hostent *hp; |
| 286 |
static struct servent *sp; |
| 287 |
static struct sockaddr_in pin; |
| 288 |
|
| 289 |
struct sigaction act; |
| 290 |
int sockopt = 1; |
| 291 |
#ifdef S_IFSOCK /* defined in stat.h if Unix domain sockets */ |
| 292 |
static struct sockaddr_un pun; |
| 293 |
#endif |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
/* Ignore 'broken pipes' signal. */ |
| 298 |
|
| 299 |
act.sa_handler = SIG_IGN; |
| 300 |
sigemptyset (&act.sa_mask); |
| 301 |
act.sa_flags = 0; |
| 302 |
if (sigaction (SIGPIPE, &act, (struct sigaction *) NULL) != 0) |
| 303 |
{ |
| 304 |
oserror = errno; |
| 305 |
(void) strcpy(messg,"osxopen: sigaction failed"); |
| 306 |
goto error_ret3; |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
/* |
| 311 |
if (first_time == 0) |
| 312 |
{ |
| 313 |
first_time = 1; |
| 314 |
|
| 315 |
fid = fopen(logname,"w"); |
| 316 |
fprintf(fid,"writing into logfile %s; \n",logname); |
| 317 |
} |
| 318 |
|
| 319 |
fprintf(fid,"osxopen: %s mode: %d\n",channame[0],mode); |
| 320 |
printf("osxopen: %s mode: %d\n",channame[0],mode); |
| 321 |
*/ |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
/* obtain the file descriptor for the socket comm. */ |
| 326 |
|
| 327 |
|
| 328 |
switch (mode & (LOCAL | NETW)) |
| 329 |
{ |
| 330 |
case LOCAL: /* open socket in server mode, locally */ |
| 331 |
#ifdef S_IFSOCK /* defined in stat.h if Unix domain sockets */ |
| 332 |
if ((xcid = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) |
| 333 |
{ |
| 334 |
(void) sprintf(messg,"osxopen(LOCAL) failed: errno = %d",errno); |
| 335 |
oserror = errno; |
| 336 |
goto error_ret3; |
| 337 |
} |
| 338 |
|
| 339 |
/* |
| 340 |
fprintf(fid,"osxopen(LOCAL): xcid = %d\n",xcid); |
| 341 |
printf("osxopen(LOCAL): xcid = %d\n",xcid); |
| 342 |
*/ |
| 343 |
memset ((char *) &pun, 0, sizeof (pun)); |
| 344 |
pun.sun_family = AF_UNIX; |
| 345 |
(void) strcpy (pun.sun_path, channame[0]); |
| 346 |
|
| 347 |
#else |
| 348 |
(void) strcpy(messg,"osxopen: local sockets not available"); |
| 349 |
oserror = -1; |
| 350 |
goto error_ret3; |
| 351 |
#endif |
| 352 |
break; |
| 353 |
|
| 354 |
|
| 355 |
case NETW: /* open socket in server mode, locally */ |
| 356 |
if ((xcid = socket (AF_INET, SOCK_STREAM, 0)) < 0) |
| 357 |
{ |
| 358 |
(void) sprintf(messg,"osxopen(NETW) failed: errno = %d",errno); |
| 359 |
oserror = errno; |
| 360 |
goto error_ret3; |
| 361 |
} |
| 362 |
|
| 363 |
/* |
| 364 |
fprintf(fid,"osxopen(NETW): xcid = %d\n",xcid); |
| 365 |
printf("osxopen(NETW): xcid = %d\n",xcid); |
| 366 |
*/ |
| 367 |
|
| 368 |
memset ((char *) &pin, 0, sizeof (pin)); |
| 369 |
pin.sin_family = AF_INET; |
| 370 |
|
| 371 |
/* channame[0] could indicate a port number or a service */ |
| 372 |
|
| 373 |
if ((pin.sin_port = atoi (channame[0])) == 0) |
| 374 |
{ |
| 375 |
/* get service reference */ |
| 376 |
if ((sp = getservbyname (channame[0], "tcp")) == NULL) |
| 377 |
{ |
| 378 |
(void) strcpy(messg,"osxopen: unable to open service"); |
| 379 |
oserror = -1; |
| 380 |
goto error_ret2; |
| 381 |
} |
| 382 |
pin.sin_port = sp->s_port; |
| 383 |
} |
| 384 |
|
| 385 |
/* allow process to reuse both the IP address and port number */ |
| 386 |
|
| 387 |
if (setsockopt (xcid, SOL_SOCKET, SO_REUSEADDR, |
| 388 |
(char *) &sockopt, sizeof (sockopt)) != 0) |
| 389 |
{ |
| 390 |
(void) strcpy(messg,"osxopen: unable to reuse sockets"); |
| 391 |
oserror = -1; |
| 392 |
goto error_ret2; |
| 393 |
} |
| 394 |
break; |
| 395 |
|
| 396 |
default: |
| 397 |
(void) strcpy(messg,"osxopen: unknown open mode"); |
| 398 |
oserror = -1; |
| 399 |
goto error_ret3; |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
/* find empty slot in indxIPCC for cid */ |
| 404 |
|
| 405 |
for (n=0; n<MAX_IPCC; n++) |
| 406 |
{ |
| 407 |
if (indxIPCC[n] == -1) |
| 408 |
{ |
| 409 |
indxIPCC[n] = xcid; /* holds fd of socket */ |
| 410 |
cid = n; /* that's the index into IPCC */ |
| 411 |
goto next_step; |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
(void) strcpy(messg,"osxopen: channel out of table"); |
| 416 |
oserror = -2; |
| 417 |
goto error_ret2; |
| 418 |
|
| 419 |
|
| 420 |
next_step: |
| 421 |
n = strlen(channame[0]) + 1; |
| 422 |
name1 = (char *) malloc ((size_t) n); |
| 423 |
(void) strcpy(name1, channame[0]); |
| 424 |
|
| 425 |
IPCC[cid].chname = name1; |
| 426 |
IPCC[cid].phname = (char *) 0; |
| 427 |
IPCC[cid].omode = mode & (IPC_READ | IPC_WRITE); |
| 428 |
IPCC[cid].type = mode & (LOCAL | NETW); |
| 429 |
IPCC[cid].status = 0; |
| 430 |
IPCC[cid].accept = 0; |
| 431 |
|
| 432 |
|
| 433 |
/* handle READ or WRITE for local or network sockets */ |
| 434 |
|
| 435 |
switch (mode) |
| 436 |
{ |
| 437 |
#ifdef S_IFSOCK |
| 438 |
case LOCAL | IPC_READ: /* open socket in server mode, locally */ |
| 439 |
/* unlink the socket_file if existing */ |
| 440 |
/* |
| 441 |
fprintf(fid,"osxopen: case LOCAL | IPC_READ\n"); |
| 442 |
printf("osxopen: case LOCAL | IPC_READ\n"); |
| 443 |
*/ |
| 444 |
|
| 445 |
if (stat (channame[0], &statbuf) == 0) |
| 446 |
{ |
| 447 |
if ((statbuf.st_mode & S_IFSOCK) == S_IFSOCK || |
| 448 |
(statbuf.st_mode & S_IFIFO) == S_IFIFO) |
| 449 |
{ |
| 450 |
if (unlink (channame[0]) == -1) |
| 451 |
{ |
| 452 |
(void) sprintf(messg,"local unlink(%s) failed",channame[0]); |
| 453 |
goto error_ret0; |
| 454 |
} |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
if (bind (xcid, (struct sockaddr *) &pun, SUN_LEN (&pun)) < 0) |
| 459 |
{ |
| 460 |
(void) sprintf(messg,"local bind() failed: errno = %d",errno); |
| 461 |
goto error_ret0; |
| 462 |
} |
| 463 |
|
| 464 |
/* Start accepting connections */ |
| 465 |
|
| 466 |
if (listen (xcid, LOCAL_QUEUE_LENGTH) == -1) |
| 467 |
{ |
| 468 |
(void) sprintf(messg,"local listen() failed: errno = %d",errno); |
| 469 |
goto error_ret0; |
| 470 |
} |
| 471 |
break; |
| 472 |
|
| 473 |
case LOCAL | IPC_WRITE: /* open channel in client mode, locally */ |
| 474 |
|
| 475 |
/* |
| 476 |
fprintf(fid,"osxopen: case LOCAL | IPC_WRITE\n"); |
| 477 |
printf("osxopen: case LOCAL | IPC_WRITE\n"); |
| 478 |
*/ |
| 479 |
|
| 480 |
if (connect (xcid, (struct sockaddr *) &pun, sizeof (pun)) < 0) |
| 481 |
{ |
| 482 |
(void) sprintf(messg,"local write connect() failed: errno = %d",errno); |
| 483 |
goto error_ret0; |
| 484 |
} |
| 485 |
IPCC[cid].accept = xcid; |
| 486 |
break; |
| 487 |
#endif |
| 488 |
|
| 489 |
case NETW | IPC_READ: /* open channel in server mode, network */ |
| 490 |
|
| 491 |
pin.sin_addr.s_addr = htonl (INADDR_ANY); |
| 492 |
|
| 493 |
/* |
| 494 |
fprintf(fid,"osxopen: case NETW | IPC_READ\n"); |
| 495 |
printf("osxopen: case NETW | IPC_READ\n"); |
| 496 |
*/ |
| 497 |
|
| 498 |
if (bind (xcid, (struct sockaddr *) &pin, sizeof (pin)) < 0) |
| 499 |
{ |
| 500 |
(void) sprintf(messg,"NETW bind() failed: errno = %d",errno); |
| 501 |
goto error_ret0; |
| 502 |
} |
| 503 |
|
| 504 |
if (listen (xcid, NETW_QUEUE_LENGTH) == -1) |
| 505 |
{ |
| 506 |
(void) sprintf(messg,"NETW listen() failed: errno = %d",errno); |
| 507 |
goto error_ret0; |
| 508 |
} |
| 509 |
break; |
| 510 |
|
| 511 |
case NETW | IPC_WRITE: /* open channel in client mode, network */ |
| 512 |
|
| 513 |
/* |
| 514 |
fprintf(fid,"osxopen: case NETW | IPC_WRITE\n"); |
| 515 |
printf("osxopen: case NETW | IPC_WRITE\n"); |
| 516 |
*/ |
| 517 |
|
| 518 |
if ((hp = gethostbyname (channame[1])) == NULL) |
| 519 |
{ |
| 520 |
(void) strcpy(messg,"osxopen: unable to open host"); |
| 521 |
goto error_ret0; |
| 522 |
} |
| 523 |
memcpy ((char *) &pin.sin_addr, hp->h_addr_list[0], hp->h_length); |
| 524 |
|
| 525 |
if (connect (xcid, (struct sockaddr *) &pin, sizeof (pin)) < 0) |
| 526 |
{ |
| 527 |
(void) sprintf(messg,"NETW connect() failed: errno = %d",errno); |
| 528 |
goto error_ret0; |
| 529 |
} |
| 530 |
|
| 531 |
IPCC[cid].accept = xcid; |
| 532 |
break; |
| 533 |
|
| 534 |
default: |
| 535 |
(void) strcpy(messg,"osxopen: unknown open mode"); |
| 536 |
oserror = -1; |
| 537 |
goto error_ret1; |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
/* |
| 542 |
fprintf(fid,"osxopen: xcid = %d, cid = %d, IPCC[%d].accept = %d\n", xcid,cid,cid,IPCC[cid].accept); |
| 543 |
printf("osxopen: xcid = %d, cid = %d, IPCC[%d].accept = %d\n", xcid,cid,cid,IPCC[cid].accept); |
| 544 |
*/ |
| 545 |
|
| 546 |
return (xcid); |
| 547 |
|
| 548 |
|
| 549 |
/* problems, problems, ... */ |
| 550 |
|
| 551 |
error_ret0: |
| 552 |
oserror = errno; |
| 553 |
|
| 554 |
error_ret1: |
| 555 |
indxIPCC[cid] = -1; |
| 556 |
|
| 557 |
error_ret2: |
| 558 |
(void) close (xcid); |
| 559 |
|
| 560 |
error_ret3: |
| 561 |
oserrmsg = messg; |
| 562 |
return (-1); |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
/* |
| 567 |
int osxexit(m) |
| 568 |
int m; |
| 569 |
|
| 570 |
{ |
| 571 |
int ret; |
| 572 |
|
| 573 |
fprintf(fid,"osxexit .......\n"); |
| 574 |
ret = fclose(fid); |
| 575 |
|
| 576 |
return 0; |
| 577 |
} |
| 578 |
*/ |
| 579 |
|
| 580 |
|
| 581 |
int osxclose (xcid) |
| 582 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 583 |
.PURPOSE Closes interprocess communication channel. |
| 584 |
The argument cid is the channel identification as |
| 585 |
obtained from osxopen. |
| 586 |
.RETURNS Value 0 on normal return. |
| 587 |
.REMARKS System dependencies: |
| 588 |
-- UNIX: close(2), unlink(2) |
| 589 |
------------------------------------------------------------*/ |
| 590 |
int xcid; /* channel identification */ |
| 591 |
|
| 592 |
{ |
| 593 |
int n, cid; |
| 594 |
|
| 595 |
char *name1; |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
/* find slot in indxIPCC containing xcid */ |
| 601 |
|
| 602 |
for (n=0; n<MAX_IPCC; n++) |
| 603 |
{ |
| 604 |
if (indxIPCC[n] == xcid) |
| 605 |
{ |
| 606 |
cid = n; /* that's the index into IPCC */ |
| 607 |
goto next_step; |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
oserrmsg = msg0; |
| 612 |
RET_ERROR (-1); |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
next_step: |
| 617 |
indxIPCC[cid] = -1; /* mark index as free */ |
| 618 |
|
| 619 |
close (xcid); |
| 620 |
|
| 621 |
/* |
| 622 |
fprintf(fid,"osxclose: xcid = %d, cid = %d\n",xcid,cid); |
| 623 |
printf("osxclose: xcid = %d, cid = %d\n",xcid,cid); |
| 624 |
*/ |
| 625 |
|
| 626 |
|
| 627 |
/* close any connection accepted */ |
| 628 |
|
| 629 |
if ((IPCC[cid].accept != xcid) && (IPCC[cid].accept != 0)) |
| 630 |
close (IPCC[cid].accept); |
| 631 |
|
| 632 |
|
| 633 |
/* suppress the associated unix socket file */ |
| 634 |
|
| 635 |
if ((IPCC[cid].type == LOCAL) && (IPCC[cid].omode == IPC_READ)) |
| 636 |
{ |
| 637 |
if (unlink (IPCC[cid].chname) == -1) RET_ERROR (errno); |
| 638 |
} |
| 639 |
|
| 640 |
name1 = (IPCC[cid].chname); |
| 641 |
if (name1 != (char *) 0) free (name1); |
| 642 |
|
| 643 |
|
| 644 |
/* |
| 645 |
fprintf(fid,"osxclose: cchname = %s,\n",name1); |
| 646 |
printf("osxclose: chname = %s,\n",name1); |
| 647 |
*/ |
| 648 |
|
| 649 |
return (0); |
| 650 |
} |
| 651 |
|
| 652 |
|
| 653 |
int osxread (xcid, pbuf, nobyt) |
| 654 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 655 |
.PURPOSE Synchronous read from interprocess communication channel. |
| 656 |
The argument cid is the channel identification as obtained |
| 657 |
from osxopen. The function reads nobyt bytes from the |
| 658 |
channel and put the information into |
| 659 |
the buffer pointed by pbuf. |
| 660 |
.RETURNS Upon successful completion, osxread return the number of bytes |
| 661 |
actually read and placed in the buffer. If the returned values is |
| 662 |
0, then EOF has been reached. Otherwise, a -1 is returnde and the |
| 663 |
gloval variable oserror is set to indicate the error. |
| 664 |
(The function osxwait must be called after the asynchronous read. |
| 665 |
NOT IMPLEMENTED YET) |
| 666 |
.REMARKS System dependencies: |
| 667 |
-- UNIX: accept(2),close(2),read(2) |
| 668 |
------------------------------------------------------------*/ |
| 669 |
int xcid; /* channel identification */ |
| 670 |
char *pbuf; /* pointer to input buffer */ |
| 671 |
int nobyt; /* number of input bytes */ |
| 672 |
|
| 673 |
{ |
| 674 |
int n, cid, ns, nb, stat; |
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
/* find slot in indxIPCC containing xcid */ |
| 680 |
|
| 681 |
for (n=0; n<MAX_IPCC; n++) |
| 682 |
{ |
| 683 |
if (indxIPCC[n] == xcid) |
| 684 |
{ |
| 685 |
cid = n; /* that's the index into IPCC */ |
| 686 |
goto next_step; |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
oserrmsg = msg0; |
| 691 |
RET_ERROR (-1); |
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
next_step: |
| 696 |
if (nobyt <= 0) return (0); |
| 697 |
|
| 698 |
|
| 699 |
/* |
| 700 |
fprintf(fid,"osxread: xcid = %d, cid = %d\n",xcid,cid); |
| 701 |
printf("osxread: xcid = %d, cid = %d\n",xcid,cid); |
| 702 |
*/ |
| 703 |
|
| 704 |
if ((ns = IPCC[cid].accept) == 0) |
| 705 |
{ |
| 706 |
if ((stat = osxstat (xcid, 0, 0)) == -1) RET_ERROR (errno); |
| 707 |
|
| 708 |
if (stat == 0) RET_ERROR (ENOTCONN); |
| 709 |
|
| 710 |
if ((ns = accept (xcid, (struct sockaddr *) 0, (XSOCKLEN *) 0)) == -1) |
| 711 |
RET_ERROR (errno); |
| 712 |
|
| 713 |
IPCC[cid].accept = ns; |
| 714 |
} |
| 715 |
|
| 716 |
if ((nb = readn (ns, pbuf, nobyt)) == -1) RET_ERROR (errno); |
| 717 |
|
| 718 |
if (nb == 0) |
| 719 |
{ |
| 720 |
if (IPCC[cid].omode == IPC_READ) IPCC[cid].accept = 0; |
| 721 |
|
| 722 |
if (close (ns) == -1) RET_ERROR (errno); |
| 723 |
} |
| 724 |
|
| 725 |
return (nb); |
| 726 |
} |
| 727 |
|
| 728 |
|
| 729 |
int osxwrite (xcid, pbuf, nobyt) |
| 730 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 731 |
.PURPOSE Synchronous write into interprocess communication channel. |
| 732 |
The argument cid is the channel identification as obtained |
| 733 |
from osxopen. The function writes nobyt bytes from the |
| 734 |
the buffer pointed by pbuf into the channel. |
| 735 |
.RETURNS Upon successful completion returns the number of bytes actually |
| 736 |
written. Otherwise -1 is returned an the global variable oserror |
| 737 |
is set to indicate the error. |
| 738 |
.REMARKS System dependencies: |
| 739 |
-- UNIX: connect(2), write(2), ospwait(0) |
| 740 |
------------------------------------------------------------*/ |
| 741 |
int xcid; /* channel identification */ |
| 742 |
char *pbuf; /* pointer to output buffer */ |
| 743 |
int nobyt; /* number of output bytes */ |
| 744 |
|
| 745 |
{ |
| 746 |
int n, cid, stat, nb, ns; |
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
/* find slot in indxIPCC containing xcid */ |
| 751 |
|
| 752 |
for (n=0; n<MAX_IPCC; n++) |
| 753 |
{ |
| 754 |
if (indxIPCC[n] == xcid) |
| 755 |
{ |
| 756 |
cid = n; /* that's the index into IPCC */ |
| 757 |
goto next_step; |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
oserrmsg = msg0; |
| 762 |
RET_ERROR (-1); |
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
next_step: |
| 767 |
|
| 768 |
/* |
| 769 |
fprintf(fid,"osxwrite: xcid = %d, cid = %d\n",xcid,cid); |
| 770 |
printf("osxwrite: xcid = %d, cid = %d\n",xcid,cid); |
| 771 |
*/ |
| 772 |
|
| 773 |
if ((ns = IPCC[cid].accept) == 0) |
| 774 |
{ |
| 775 |
if ((stat = osxstat (xcid, 0, 0)) == -1) RET_ERROR (errno); |
| 776 |
|
| 777 |
if (stat == 0) RET_ERROR (ENOTCONN); |
| 778 |
|
| 779 |
if ((ns = accept (xcid, (struct sockaddr *) 0, (XSOCKLEN *) 0)) == -1) |
| 780 |
RET_ERROR (errno); |
| 781 |
|
| 782 |
IPCC[cid].accept = ns; |
| 783 |
} |
| 784 |
|
| 785 |
|
| 786 |
if ((nb = writen (ns, pbuf, nobyt)) < 0) |
| 787 |
{ |
| 788 |
if (IPCC[cid].omode == IPC_READ) |
| 789 |
{ |
| 790 |
IPCC[cid].accept = 0; |
| 791 |
|
| 792 |
if (close (ns) == -1) RET_ERROR (errno); |
| 793 |
RET_ERROR (ENOTCONN); |
| 794 |
} |
| 795 |
} |
| 796 |
|
| 797 |
|
| 798 |
return (nb); |
| 799 |
} |
| 800 |
|
| 801 |
int osxinfo (xcid, sec, usec) |
| 802 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 803 |
.PURPOSE Gets status of the interprocess communication channel. |
| 804 |
.RETURNS Upon successful the status of the channel is returned: |
| 805 |
DATARDY if there is data to be read. |
| 806 |
NODATA if there is no data available. |
| 807 |
NOCONN if there is no connection. |
| 808 |
On error -1 is returned , and the variable oserror set. |
| 809 |
.REMARKS System dependencies: |
| 810 |
-- UNIX: select |
| 811 |
------------------------------------------------------------*/ |
| 812 |
int xcid; /* channel identification */ |
| 813 |
int sec, usec; /* Timeout in seconds and microseconds */ |
| 814 |
|
| 815 |
{ |
| 816 |
int n, cid, ns, stat; |
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
/* find slot in indxIPCC containing xcid */ |
| 821 |
|
| 822 |
for (n=0; n<MAX_IPCC; n++) |
| 823 |
{ |
| 824 |
if (indxIPCC[n] == xcid) |
| 825 |
{ |
| 826 |
cid = n; /* that's the index into IPCC */ |
| 827 |
goto next_step; |
| 828 |
} |
| 829 |
} |
| 830 |
|
| 831 |
oserrmsg = msg0; |
| 832 |
RET_ERROR (-1); |
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
next_step: |
| 837 |
|
| 838 |
/* |
| 839 |
fprintf(fid,"osxinfo: xcid = %d, cid = %d, IPCC[cid].accept = %d\n", |
| 840 |
xcid,cid,IPCC[cid].accept); |
| 841 |
printf("osxinfo: xcid = %d, cid = %d, IPCC[cid].accept = %d\n", |
| 842 |
xcid,cid,IPCC[cid].accept); |
| 843 |
*/ |
| 844 |
|
| 845 |
if ((ns = IPCC[cid].accept) == 0) |
| 846 |
{ |
| 847 |
if ((stat = osxstat (xcid, sec, usec)) == -1) RET_ERROR (errno); |
| 848 |
|
| 849 |
if (stat == 0) return (NOCONN); |
| 850 |
|
| 851 |
if ((ns = accept (xcid, (struct sockaddr *) 0, (XSOCKLEN *) 0)) == -1) |
| 852 |
RET_ERROR (errno); |
| 853 |
|
| 854 |
IPCC[cid].accept = ns; |
| 855 |
} |
| 856 |
|
| 857 |
|
| 858 |
if ((stat = osxstat (ns, sec, usec)) < 0) return (NOCONN); |
| 859 |
|
| 860 |
return (stat ? DATARDY : NODATA); |
| 861 |
} |
| 862 |
|
| 863 |
|
| 864 |
int osxgetservbyname (service) |
| 865 |
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 866 |
.PURPOSE Interface to the getservbyname(). |
| 867 |
.RETURNS The port number, or -1 if no port. |
| 868 |
.REMARKS System dependencies: |
| 869 |
-- UNIX: getservbyname |
| 870 |
------------------------------------------------------------*/ |
| 871 |
char *service; |
| 872 |
|
| 873 |
{ |
| 874 |
struct servent *sp; |
| 875 |
|
| 876 |
if ((sp = getservbyname (service, "tcp")) == NULL) |
| 877 |
return (-1); |
| 878 |
else |
| 879 |
return (sp->s_port); |
| 880 |
} |