| 1 |
/* @(#)osx.c 19.1 (ES0-DMD) 02/25/03 13:56:14 */ |
| 2 |
/*=========================================================================== |
| 3 |
Copyright (C) 1995 European Southern Observatory (ESO) |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or |
| 6 |
modify it under the terms of the GNU General Public License as |
| 7 |
published by the Free Software Foundation; either version 2 of |
| 8 |
the License, or (at your option) any later version. |
| 9 |
|
| 10 |
This program is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public |
| 16 |
License along with this program; if not, write to the Free |
| 17 |
Software Foundation, Inc., 675 Massachusetss Ave, Cambridge, |
| 18 |
MA 02139, USA. |
| 19 |
|
| 20 |
Corresponding concerning ESO-MIDAS should be addressed as follows: |
| 21 |
Internet e-mail: [email protected] |
| 22 |
Postal address: European Southern Observatory |
| 23 |
Data Management Division |
| 24 |
Karl-Schwarzschild-Strasse 2 |
| 25 |
D 85748 Garching bei Muenchen |
| 26 |
GERMANY |
| 27 |
===========================================================================*/ |
| 28 |
|
| 29 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 30 |
.TYPE Module |
| 31 |
.NAME osx |
| 32 |
.LANGUAGE C |
| 33 |
.AUTHOR K. Banse ESO-IPG, Garching |
| 34 |
.CATEGORY Host operating system interfaces. Interprocess Communication |
| 35 |
.COMMENTS Interprocess communication routines for VMS. These routines |
| 36 |
allow the creation of asynchronous communication channels |
| 37 |
between processes. |
| 38 |
The communication channel is open by giving the channel name |
| 39 |
and the mode of opening. The function returns an integer number |
| 40 |
used to address the channel. |
| 41 |
The routines return always |
| 42 |
a non-negative integer number on successful return. |
| 43 |
Otherwise, a value of -1 is set to indicate an error |
| 44 |
condition and the variable ``oserror'' contains the |
| 45 |
symbolic error code. |
| 46 |
Symbolic error codes for each function to be defined. |
| 47 |
|
| 48 |
.HISTORY 1.00 890720 adapted from prototype version by M. Pucillo, Trieste |
| 49 |
.HISTORY 1.10 891102 fix related problem in osxopen (return kid!) KB |
| 50 |
.HISTORY 1.20 891211 fix 4th par. in SYS$ASSIGN (to NULL pointer) TM |
| 51 |
.HISTORY 1.30 900115 replace $DESCRIPTOR construct by exact commands KB |
| 52 |
.HISTORY 1.40 910909 redesign the osx !! KB |
| 53 |
------------------------------------------------------------*/ |
| 54 |
|
| 55 |
#include <stdio.h> |
| 56 |
#include <errno.h> |
| 57 |
#include <osparms.h> |
| 58 |
|
| 59 |
/* VMS dependent include section */ |
| 60 |
|
| 61 |
#include <ssdef.h> |
| 62 |
#include <descrip.h> |
| 63 |
#include <iodef.h> |
| 64 |
|
| 65 |
#define BIT_0 0x1 |
| 66 |
|
| 67 |
|
| 68 |
typedef struct |
| 69 |
{ |
| 70 |
short cond_value; |
| 71 |
short count; |
| 72 |
int info; |
| 73 |
} iostat_blk; |
| 74 |
|
| 75 |
struct ipccstat IPCC[MAX_IPCC]; |
| 76 |
|
| 77 |
static short channel[MAX_IPCC]; |
| 78 |
static iostat_blk iosb[MAX_IPCC]; |
| 79 |
|
| 80 |
static unsigned int retval; |
| 81 |
|
| 82 |
extern int oserror; |
| 83 |
|
| 84 |
extern int errno; |
| 85 |
|
| 86 |
|
| 87 |
int osxopen(channame, mode) |
| 88 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 89 |
.PURPOSE Opens communication channel for read or write. The variable mode |
| 90 |
defines the way of opening the file by constructing |
| 91 |
the OR of the following flags: |
| 92 |
- LOCAL (local communication channel) |
| 93 |
- CLIENT (client mode of opening : no priv. required) |
| 94 |
- IPC_READ (open the channel in READ mode) |
| 95 |
- IPC_WRITE (open the channel in WRITE mode) |
| 96 |
.RETURNS Upon successful completion a positive number with the |
| 97 |
channel identification is returned or -1 in case of error. |
| 98 |
.REMARKS System dependencies: |
| 99 |
-- VMS : SYS$CREMBX, $DESCRIPTOR, LIB$SIGNAL, open(2) |
| 100 |
------------------------------------------------------------*/ |
| 101 |
char *channame[2]; /* physical channel name */ |
| 102 |
int mode; /* open mode */ |
| 103 |
|
| 104 |
{ |
| 105 |
static int cid = 0; |
| 106 |
int kid; |
| 107 |
|
| 108 |
unsigned short chan; |
| 109 |
unsigned int maxmsg = 4096; |
| 110 |
unsigned int bufquo = 8192; |
| 111 |
char bzero = 0; |
| 112 |
char bone = 1; |
| 113 |
unsigned int izero = 0; |
| 114 |
|
| 115 |
struct dsc$descriptor_s lognam_desc; |
| 116 |
|
| 117 |
lognam_desc.dsc$w_length = strlen(channame[0]); |
| 118 |
lognam_desc.dsc$a_pointer = channame[0]; |
| 119 |
lognam_desc.dsc$b_class = DSC$K_CLASS_S; |
| 120 |
lognam_desc.dsc$b_dtype = DSC$K_DTYPE_T; |
| 121 |
|
| 122 |
if (cid >= MAX_IPCC) |
| 123 |
{ |
| 124 |
oserror = EINVAL; |
| 125 |
return (-1); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
for (kid=1; kid<=cid; kid++) /* look for free (closed) entry */ |
| 130 |
{ |
| 131 |
if (IPCC[kid].status == CLOSED) goto slot_found; |
| 132 |
} |
| 133 |
|
| 134 |
kid = ++ cid; |
| 135 |
|
| 136 |
|
| 137 |
slot_found: |
| 138 |
|
| 139 |
switch (mode) |
| 140 |
{ |
| 141 |
|
| 142 |
case 0: /* open channel in read mode, locally */ |
| 143 |
/* --------- local server ----------- */ |
| 144 |
|
| 145 |
|
| 146 |
retval = SYS$CREMBX(bone,&chan,maxmsg,bufquo,izero,izero,&lognam_desc); |
| 147 |
|
| 148 |
if (retval != SS$_NORMAL) LIB$SIGNAL (retval); |
| 149 |
|
| 150 |
IPCC[kid].status = OPENED_READ; |
| 151 |
IPCC[kid].omode = IPC_READ; |
| 152 |
IPCC[kid].type = LOCAL; |
| 153 |
channel[kid] = chan; |
| 154 |
break; |
| 155 |
|
| 156 |
case 1: /* open channel in write mode, locally */ |
| 157 |
/* --------- local server ------------ */ |
| 158 |
/* default IPCC number is set to 0 at opening */ |
| 159 |
|
| 160 |
|
| 161 |
retval = SYS$CREMBX(bone,&chan,maxmsg,bufquo,izero,izero,&lognam_desc); |
| 162 |
if (retval != SS$_NORMAL) LIB$SIGNAL (retval); |
| 163 |
|
| 164 |
IPCC[kid].status = OPENED_WRITE; |
| 165 |
IPCC[kid].omode = IPC_WRITE; |
| 166 |
IPCC[kid].type = LOCAL; |
| 167 |
channel[kid] = chan; |
| 168 |
break; |
| 169 |
|
| 170 |
case 4: /* open channel in read mode, locally */ |
| 171 |
/* --------- local client ----------- */ |
| 172 |
|
| 173 |
|
| 174 |
retval = SYS$ASSIGN(&lognam_desc,&chan,bzero,(struct dsc$descriptor *)NULL); |
| 175 |
if (retval != SS$_NORMAL) LIB$SIGNAL (retval); |
| 176 |
|
| 177 |
IPCC[kid].status = OPENED_READ; |
| 178 |
IPCC[kid].omode = IPC_READ; |
| 179 |
IPCC[kid].type = LOCAL | CLIENT; |
| 180 |
channel[kid] = chan; |
| 181 |
break; |
| 182 |
|
| 183 |
case 5: /* open channel in write mode, locally */ |
| 184 |
/* --------- local client ------------ */ |
| 185 |
/* default IPCC number is set to 0 at opening */ |
| 186 |
|
| 187 |
|
| 188 |
retval = SYS$ASSIGN(&lognam_desc,&chan,bzero,(struct dsc$descriptor *)NULL); |
| 189 |
if (retval != SS$_NORMAL) LIB$SIGNAL (retval); |
| 190 |
|
| 191 |
IPCC[kid].status = OPENED_WRITE; |
| 192 |
IPCC[kid].omode = IPC_WRITE; |
| 193 |
IPCC[kid].type = LOCAL | CLIENT; |
| 194 |
channel[kid] = chan; |
| 195 |
break; |
| 196 |
|
| 197 |
default: |
| 198 |
oserror = EINVAL; |
| 199 |
break; |
| 200 |
} |
| 201 |
|
| 202 |
return(oserror == 0 ? kid : -1); |
| 203 |
|
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
int osxclose(cid) |
| 209 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 210 |
.PURPOSE Closes interprocess communication channel. |
| 211 |
The argument cid is the channel identification as |
| 212 |
obtained from osxopen. |
| 213 |
.RETURNS Value 0 on normal return. |
| 214 |
.REMARKS System dependencies: |
| 215 |
-- VMS: SYS$DASSGN, SYS$DELMBX, LIB$SIGNAL |
| 216 |
------------------------------------------------------------*/ |
| 217 |
int cid; /* channel identification */ |
| 218 |
|
| 219 |
{ |
| 220 |
|
| 221 |
if (cid < 0 || cid > MAX_IPCC) |
| 222 |
{ |
| 223 |
oserror = EINVAL; |
| 224 |
return (-1); |
| 225 |
} |
| 226 |
|
| 227 |
IPCC[cid].status = CLOSED; |
| 228 |
|
| 229 |
if (IPCC[cid].type == LOCAL) |
| 230 |
{ |
| 231 |
retval = SYS$DELMBX (channel[cid]); |
| 232 |
if (retval != SS$_NORMAL) LIB$SIGNAL (retval); |
| 233 |
} |
| 234 |
|
| 235 |
retval = SYS$DASSGN (channel[cid]); |
| 236 |
if (retval != SS$_NORMAL) LIB$SIGNAL (retval); |
| 237 |
|
| 238 |
return(0); |
| 239 |
|
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
int osxread(cid, pbuf, nobytes) |
| 245 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 246 |
.PURPOSE Synchronous read from interprocess communication channel. |
| 247 |
The argument cid is the channel identification as obtained |
| 248 |
from osxopen. The function reads nobyt bytes from the |
| 249 |
channel and put the information into |
| 250 |
the buffer pointed by pbuf. |
| 251 |
.RETURNS Upon successful completion a value of 0 is returned. |
| 252 |
( The function osxwait must be called after the asynchronous read. ) |
| 253 |
.REMARKS System dependencies: |
| 254 |
-- VMS:SYS$QIOW, LIB$SIGNAL |
| 255 |
------------------------------------------------------------*/ |
| 256 |
int cid; /* channel identification */ |
| 257 |
char *pbuf; /* pointer to input buffer */ |
| 258 |
int nobytes; /* number of input bytes */ |
| 259 |
|
| 260 |
{ |
| 261 |
|
| 262 |
unsigned short chan; |
| 263 |
unsigned short func = IO$_READVBLK; |
| 264 |
unsigned int nobyt; |
| 265 |
char bzero = 0; |
| 266 |
unsigned int izero = 0, ifour = 4; |
| 267 |
|
| 268 |
chan = channel[cid]; |
| 269 |
nobyt = (unsigned int)nobytes; |
| 270 |
|
| 271 |
if (cid < 0 || cid > MAX_IPCC ) |
| 272 |
{ |
| 273 |
oserror = EINVAL; |
| 274 |
return (-1); |
| 275 |
} |
| 276 |
|
| 277 |
if (nobyt <= 0) return(0); |
| 278 |
|
| 279 |
|
| 280 |
/* the server does not wait on I/O - and uses osxinfo for synchronization */ |
| 281 |
|
| 282 |
if (IPCC[cid].type == LOCAL) |
| 283 |
retval = SYS$QIO (ifour, chan, func, &iosb[cid], izero, izero, |
| 284 |
pbuf, nobyt, izero, izero, izero, izero); |
| 285 |
|
| 286 |
/* the clients do an I/O with wait */ |
| 287 |
|
| 288 |
else |
| 289 |
retval = SYS$QIOW (izero, chan, func, &iosb[cid], izero, izero, |
| 290 |
pbuf, nobyt, izero, izero, izero, izero); |
| 291 |
|
| 292 |
|
| 293 |
if (retval != SS$_NORMAL) LIB$SIGNAL (retval); |
| 294 |
|
| 295 |
return(0); |
| 296 |
|
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
int osxwrite(cid, pbuf, nobytes) |
| 302 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 303 |
.PURPOSE Synchronous write into interprocess communication channel. |
| 304 |
The argument cid is the channel identification as obtained |
| 305 |
from osxopen. The function writes nobyt bytes from the |
| 306 |
the buffer pointed by pbuf into the channel. |
| 307 |
.RETURNS Upon successful completion a value of 0 is returned. |
| 308 |
( The function osxwait must be called after the asynchronous write.) |
| 309 |
.REMARKS System dependencies: |
| 310 |
-- VMS:SYS$QIOW, LIB$SIGNAL |
| 311 |
------------------------------------------------------------*/ |
| 312 |
int cid; /* channel identification */ |
| 313 |
char *pbuf; /* pointer to output buffer */ |
| 314 |
int nobytes; /* number of output bytes */ |
| 315 |
{ |
| 316 |
|
| 317 |
unsigned short chan; |
| 318 |
unsigned short func = IO$_WRITEVBLK; |
| 319 |
unsigned int nobyt; |
| 320 |
char bzero = 0; |
| 321 |
unsigned int izero = 0; |
| 322 |
|
| 323 |
chan = channel[cid]; |
| 324 |
nobyt = (unsigned int)nobytes; |
| 325 |
|
| 326 |
if (cid < 0 || cid > MAX_IPCC) |
| 327 |
{ |
| 328 |
oserror = EINVAL; |
| 329 |
return (-1); |
| 330 |
} |
| 331 |
|
| 332 |
if (nobyt > 0) |
| 333 |
{ |
| 334 |
retval = SYS$QIOW (izero, chan, func, &iosb[cid], izero, izero, |
| 335 |
pbuf, nobyt, izero, izero, izero, izero); |
| 336 |
if (retval != SS$_NORMAL) LIB$SIGNAL (retval); |
| 337 |
} |
| 338 |
|
| 339 |
return(0); |
| 340 |
|
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
int osxinfo(cid, sec, usec) |
| 346 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 347 |
.PURPOSE Gets status of the interprocess communication channel. |
| 348 |
.RETURNS Upon successful completion a value of 0 is returned. |
| 349 |
.REMARKS System dependencies: |
| 350 |
-- UNIX: none |
| 351 |
------------------------------------------------------------*/ |
| 352 |
int cid; /* channel identification */ |
| 353 |
int sec; /* seconds to wait in osxinfo */ |
| 354 |
int usec; /* millisecs to wait in osxinfo */ |
| 355 |
|
| 356 |
{ |
| 357 |
|
| 358 |
short int iostat; |
| 359 |
long int milsecs, time[2]; |
| 360 |
unsigned int efmask; |
| 361 |
unsigned int ithree = 3; |
| 362 |
|
| 363 |
if (cid < 0 || cid > MAX_IPCC || usec < 0) |
| 364 |
{ |
| 365 |
oserror = EINVAL; |
| 366 |
return (-1); |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
iostat = iosb[cid].cond_value; /* look at status of last read */ |
| 372 |
|
| 373 |
if (iostat == SS$_NORMAL) |
| 374 |
return (DATARDY); |
| 375 |
|
| 376 |
|
| 377 |
/* init mask to just wait for event flag #4 (from the QIO) */ |
| 378 |
|
| 379 |
efmask = 16; |
| 380 |
|
| 381 |
|
| 382 |
/* if sec or usec > 0, wait */ |
| 383 |
|
| 384 |
if (sec > 0) |
| 385 |
milsecs = (sec * 1000) + usec; |
| 386 |
else |
| 387 |
milsecs = usec; |
| 388 |
|
| 389 |
if (milsecs > 0) |
| 390 |
{ |
| 391 |
time[1] = -1; |
| 392 |
time[0] = - (10000 * milsecs); |
| 393 |
retval = SYS$SETIMR(ithree,time,0,0); /* use event flag 3 */ |
| 394 |
|
| 395 |
if (retval & BIT_0) efmask = 24; /* set also bit 3 to 1 */ |
| 396 |
} |
| 397 |
|
| 398 |
retval = SYS$WFLOR(ithree,efmask); |
| 399 |
if (retval != SS$_NORMAL) LIB$SIGNAL(retval); |
| 400 |
|
| 401 |
|
| 402 |
/* now look again */ |
| 403 |
|
| 404 |
iostat = iosb[cid].cond_value; /* look at status of last read */ |
| 405 |
|
| 406 |
if (iostat == SS$_NORMAL) |
| 407 |
return (DATARDY); |
| 408 |
else |
| 409 |
return (NOCONN); |
| 410 |
} |
| 411 |
|
| 412 |
int osxgetservbyname(service) |
| 413 |
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 414 |
.PURPOSE Interface to the getservbyname(). |
| 415 |
.RETURNS The port number, or -1 if no port. |
| 416 |
.REMARKS Not implemented yet |
| 417 |
------------------------------------------------------------*/ |
| 418 |
char *service; |
| 419 |
{ |
| 420 |
return(-1); |
| 421 |
/* |
| 422 |
struct servent *sp; |
| 423 |
|
| 424 |
if ((sp = getservbyname(service,"tcp")) == NULL) |
| 425 |
return(-1); |
| 426 |
else |
| 427 |
return(sp->s_port); |
| 428 |
*/ |
| 429 |
} |