/* Taken from http://www2.cs.uregina.ca/~hamilton/courses/430/notes/rpcserver.c Modified to check error codes. BUG: This periodically aborting with "free(): invalid next size (fast)" message in xdr_wrapstring()! */ /*** Program Name: rpcserver.c Purpose: Acts like a feeble rsh server. Remotely executes a command. Passes result back! How does it do this? A remote proceedure call has been defined caller server, which returns a string (array of chars). Simply put the function server accepts a command, redirects all output to a dummy pipe, writes output of this command from the client to the pipe, closes the pipe, reads the output back from the pipe the origional charater pointer (passed by ref), then returns this value to the client. i guess not so simple. Notes: This program will run on hercules and the Dec stations. The C compiler on the Dec stations seem to be a tad less forgiving with pointers, so there will be a few warnings. This could easily be fixed by casting! By: Chad Holliday ***/ #include #include #include #include #include #include /* Added by MAS */ #include "hcrpc.h" XDR *server(char **text) { char *buffer; int tmp_fd[2], hmany = 0 ; if (pipe(tmp_fd) == -1) /* create temp pipe to store output */ abort(); /* Redirect all output to the write end of the pipe */ if ( (dup2(tmp_fd[1], STDOUT_FILENO) == -1) || (dup2(tmp_fd[1], STDERR_FILENO) == -1) ) abort(); system(*text) ; /* system so sh or csh runs so we get the advantage of env. vars etc ... */ write(tmp_fd[1], "\0", 2) ; hmany = read(tmp_fd[0], *text, 100000) ; /* take the command results from the pipe */ close(tmp_fd[0]) ; /* close off the read and write file descriptors associated with pipes */ close(tmp_fd[1]) ; buffer= *text; return((XDR*)text); /* return network string back to client */ } int main(void) { if(fork() != 0) exit(0); /* run this program in the background */ svc_unregister(SERVICENUM, VERSION); /* unregister previous running versions of RPC */ /* register with port mapper so as the client knows where to contact for service */ registerrpc(SERVICENUM, VERSION, PROCNUM, server, xdr_wrapstring, xdr_wrapstring); svc_run(); /* run the server RPC */ fprintf(stderr,"Never should have gotten here svc_run()\n"); return 1; }