home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
WindowsWare 2 the Maxx
/
winmaxx.zip
/
winmaxx
/
WIN_NT
/
PSXRPC.ZIP
/
RPCCLT.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-11-20
|
4KB
|
183 lines
/*
POSIX RPC Client Library
(C) Copyright 1992 by
John Richardson
CompuServe 70541,672
Internet jr@sni-usa.com
This program may be used freely provided that this copyright is
included in the source listings.
This is the run time support for the POSIX side RPC client.
This part can be extended to include user written RPC requests for the
WIN32SVR process. Ideally, it should go into its own DLL.
*/
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include "win32psx.h"
extern int errno;
/*
Attach to the POSIX server process that acts as the gate way
to the WIN32 server process.
The POSIX server process is started by the WIN32 server with its
stdin+stdout redirected to pipes that are read and written by the WIN32
server in order to execute and reply to remote RPC requests.
We communicate with this server from other POSIX programs through POSIX
named PIPES.
*/
int ReadStream(char *buf, int size);
int WriteStream(char *buf, int size);
int ServerInputHandle;
int ServerOutputHandle;
void AttachToServer()
{
char InNameBuf[256], OutNameBuf[256];
InNameBuf[0] = 0;
OutNameBuf[0] = 0;
strcat(InNameBuf, NAMED_PIPE_PATH);
strcat(OutNameBuf, NAMED_PIPE_PATH);
strcat(InNameBuf, "RPC_REQ");
strcat(OutNameBuf, "RPC_REP");
ServerOutputHandle = open(InNameBuf, O_WRONLY, 0);
if(ServerOutputHandle == (-1))
{
fprintf(stderr, "Can't open Named pipe, errno %d\n",errno);
exit(1);
}
ServerInputHandle = open(OutNameBuf, O_RDONLY, 0);
if(ServerInputHandle == (-1))
{
fprintf(stderr, "Can't open Named pipe, errno %d\n",errno);
exit(1);
}
}
/* Send a NOOP RPC */
int NoopRPC()
{
struct Request_Header Hd;
int numxfer;
#ifdef DEBUG
fprintf(stderr, "Sending Noop RPC to Server\n");
#endif
Hd.rh_type = RPC_REQUEST;
Hd.rh_hdrsize = sizeof(struct Request_Header);
Hd.rh_size = 0;
Hd.rh_chan = 1;
Hd.rh_request = RPC_NOOP;
WriteStream((char *)&Hd, sizeof(struct Request_Header));
#ifdef DEBUG
fprintf(stderr, "Waiting for NOOP reply...\n");
#endif
/* Now wait for the reply */
numxfer = ReadStream((char *)&Hd, sizeof(struct Request_Header));
if(Hd.rh_type != RPC_REPLY)
{
fprintf(stderr, "POSIX: Reply is not an RPC REPLY\n");
return(0);
}
#ifdef DEBUG
else
fprintf(stderr, "Got NOOP Reply\n");
#endif
return(1);
}
/* Have WIN32 run a command line */
int SystemRPC(char *cmd)
{
struct Request_Header Hd;
int numxfer, exitstatus;
#ifdef DEBUG
fprintf(stderr, "Sending RUN_SHELL_CMD RPC to Server\n");
#endif
Hd.rh_type = RPC_REQUEST;
Hd.rh_hdrsize = sizeof(struct Request_Header);
Hd.rh_size = strlen(cmd) + 1;
Hd.rh_chan = 1;
Hd.rh_request = RPC_RUN_SHELL_CMD_SYNC;
WriteStream((char *)&Hd, sizeof(struct Request_Header));
WriteStream(cmd, strlen(cmd) + 1);
#ifdef DEBUG
fprintf(stderr, "Waiting for RUN_SHELL_CMD reply...\n");
#endif
/* Now wait for the reply */
numxfer = ReadStream((char *)&Hd, sizeof(struct Request_Header));
if(Hd.rh_type != RPC_REPLY)
{
fprintf(stderr, "POSIX: Reply is not an RPC REPLY\n");
exit(1);
}
else
{
if((Hd.rh_size == 0) && (Hd.rh_request == 0))
{
/* error running command or creating process */
return(1);
}
else if(Hd.rh_size != 4)
{
fprintf(stderr, "POSIX: Reply is not 4 bytes as expected\n");
exit(1);
}
ReadStream((char *)&exitstatus, 4);
#ifdef DEBUG
fprintf(stderr, "Got RUN_SHELL_CMD Reply\n");
#endif
}
return(exitstatus);
}
/*
read data from the communications stream
*/
int ReadStream(char *buf, int size)
{
int numxfer;
if((numxfer = read(ServerInputHandle, buf, size)) == (-1))
{
fprintf(stderr, "POSIX: Error reading Child Stdin, %d\n", errno);
exit(1);
}
return(numxfer);
}
/*
write data to the communications stream
*/
int WriteStream(char *buf, int size)
{
int numxfer;
if((numxfer = write(ServerOutputHandle, buf, size)) == (-1))
{
fprintf(stderr, "POSIX: Error writing Child Stdout, %d\n", errno);
exit(1);
}
return(numxfer);
}