home *** CD-ROM | disk | FTP | other *** search
/ WindowsWare 2 the Maxx / winmaxx.zip / winmaxx / WIN_NT / PSXRPC.ZIP / RPCCLT.C < prev    next >
C/C++ Source or Header  |  1992-11-20  |  4KB  |  183 lines

  1. /*
  2.  
  3.   POSIX RPC Client Library
  4.  
  5.   (C) Copyright 1992 by
  6.  
  7.       John Richardson
  8.       CompuServe 70541,672
  9.       Internet jr@sni-usa.com
  10.  
  11.       This program may be used freely provided that this copyright is
  12.       included in the source listings.
  13.  
  14.  
  15.   This is the run time support for the POSIX side RPC client.
  16.   This part can be extended to include user written RPC requests for the
  17.   WIN32SVR process. Ideally, it should go into its own DLL.
  18. */
  19.  
  20. #include <unistd.h>
  21. #include <stdio.h>
  22. #include <fcntl.h>
  23. #include <string.h>
  24. #include "win32psx.h"
  25.  
  26. extern int errno;
  27.  
  28. /*
  29.    Attach to the POSIX server process that acts as the gate way
  30.    to the WIN32 server process.
  31.  
  32.    The POSIX server process is started by the WIN32 server with its
  33.    stdin+stdout redirected to pipes that are read and written by the WIN32
  34.    server in order to execute and reply to remote RPC requests.
  35.  
  36.    We communicate with this server from other POSIX programs through POSIX
  37.    named PIPES.
  38. */
  39.  
  40. int  ReadStream(char *buf, int size);
  41. int  WriteStream(char *buf, int size);
  42.  
  43. int ServerInputHandle;
  44. int ServerOutputHandle;
  45.  
  46. void AttachToServer()
  47. {
  48.   char InNameBuf[256], OutNameBuf[256];
  49.   
  50.   InNameBuf[0] = 0;
  51.   OutNameBuf[0] = 0;
  52.   strcat(InNameBuf, NAMED_PIPE_PATH);
  53.   strcat(OutNameBuf, NAMED_PIPE_PATH);
  54.   strcat(InNameBuf, "RPC_REQ");
  55.   strcat(OutNameBuf, "RPC_REP");
  56.  
  57.   ServerOutputHandle = open(InNameBuf, O_WRONLY, 0);
  58.   if(ServerOutputHandle == (-1))
  59.   {
  60.     fprintf(stderr, "Can't open Named pipe, errno %d\n",errno);
  61.     exit(1);
  62.   }
  63.   ServerInputHandle = open(OutNameBuf, O_RDONLY, 0);
  64.   if(ServerInputHandle == (-1))
  65.   {
  66.     fprintf(stderr, "Can't open Named pipe, errno %d\n",errno);
  67.     exit(1);
  68.   }
  69. }
  70.  
  71. /* Send a NOOP RPC */
  72. int NoopRPC()
  73. {
  74.   struct Request_Header Hd;
  75.   int numxfer;
  76.  
  77. #ifdef DEBUG
  78.   fprintf(stderr, "Sending Noop RPC to Server\n");
  79. #endif
  80.  
  81.   Hd.rh_type = RPC_REQUEST;
  82.   Hd.rh_hdrsize = sizeof(struct Request_Header);
  83.   Hd.rh_size = 0;
  84.   Hd.rh_chan = 1;
  85.   Hd.rh_request = RPC_NOOP;
  86.   WriteStream((char *)&Hd, sizeof(struct Request_Header));
  87.  
  88. #ifdef DEBUG
  89.   fprintf(stderr, "Waiting for NOOP reply...\n");
  90. #endif
  91.   /* Now wait for the reply */
  92.   numxfer = ReadStream((char *)&Hd, sizeof(struct Request_Header));
  93.   if(Hd.rh_type != RPC_REPLY)
  94.   {
  95.      fprintf(stderr, "POSIX: Reply is not an RPC REPLY\n");
  96.      return(0);
  97.   }
  98. #ifdef DEBUG
  99.   else
  100.      fprintf(stderr, "Got NOOP Reply\n");
  101. #endif
  102.  return(1);
  103. }
  104.  
  105. /* Have WIN32 run a command line */
  106. int SystemRPC(char *cmd)
  107. {
  108.   struct Request_Header Hd;
  109.   int numxfer, exitstatus;
  110.  
  111. #ifdef DEBUG
  112.   fprintf(stderr, "Sending RUN_SHELL_CMD RPC to Server\n");
  113. #endif
  114.  
  115.   Hd.rh_type = RPC_REQUEST;
  116.   Hd.rh_hdrsize = sizeof(struct Request_Header);
  117.   Hd.rh_size = strlen(cmd) + 1;
  118.   Hd.rh_chan = 1;
  119.   Hd.rh_request = RPC_RUN_SHELL_CMD_SYNC;
  120.   WriteStream((char *)&Hd, sizeof(struct Request_Header));
  121.   WriteStream(cmd, strlen(cmd) + 1);
  122.  
  123. #ifdef DEBUG
  124.   fprintf(stderr, "Waiting for RUN_SHELL_CMD reply...\n");
  125. #endif
  126.   /* Now wait for the reply */
  127.   numxfer = ReadStream((char *)&Hd, sizeof(struct Request_Header));
  128.   if(Hd.rh_type != RPC_REPLY)
  129.   {
  130.      fprintf(stderr, "POSIX: Reply is not an RPC REPLY\n");
  131.      exit(1);
  132.   }
  133.   else
  134.   {
  135.     if((Hd.rh_size == 0) && (Hd.rh_request == 0))
  136.     {
  137.       /* error running command or creating process */
  138.       return(1);
  139.     }
  140.     else if(Hd.rh_size != 4)
  141.     {
  142.       fprintf(stderr, "POSIX: Reply is not 4 bytes as expected\n");
  143.       exit(1);
  144.     }
  145.     ReadStream((char *)&exitstatus, 4);
  146. #ifdef DEBUG
  147.      fprintf(stderr, "Got RUN_SHELL_CMD Reply\n");
  148. #endif
  149.   }
  150.   return(exitstatus);
  151. }
  152.  
  153. /*
  154.   read data from the communications stream
  155. */
  156. int ReadStream(char *buf, int size)
  157. {
  158.   int numxfer;
  159.  
  160.   if((numxfer = read(ServerInputHandle, buf, size)) == (-1))
  161.   {
  162.     fprintf(stderr, "POSIX: Error reading Child Stdin, %d\n", errno);
  163.     exit(1);
  164.   }
  165.   return(numxfer);
  166. }
  167.  
  168. /*
  169.   write data to the communications stream
  170. */
  171. int WriteStream(char *buf, int size)
  172. {
  173.   int numxfer;
  174.  
  175.   if((numxfer = write(ServerOutputHandle, buf, size)) == (-1))
  176.   {
  177.     fprintf(stderr, "POSIX: Error writing Child Stdout, %d\n", errno);
  178.     exit(1);
  179.   }
  180.   return(numxfer);
  181. }
  182.  
  183.