Sun RPC demo programs from 4.4-Lite

This commit is contained in:
Garrett Wollman 1994-08-07 18:50:51 +00:00
parent 9e4ded5d0e
commit 944fcc15f8
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/cvs2svn/branches/SUN/; revision=1905
14 changed files with 539 additions and 0 deletions

View File

@ -0,0 +1,25 @@
#
# @(#)Makefile 2.1 88/08/02 4.0 RPCSRC
#
#
# Build all demo services
#
MAKE = make
LIB=-lrpclib
SUBDIR= dir msg sort
all: ${SUBDIR}
clean cleanup:
cd dir; $(MAKE) ${MFLAGS} cleanup
cd msg; $(MAKE) ${MFLAGS} cleanup
cd sort; $(MAKE) ${MFLAGS} cleanup
install:
@echo "No installations done."
${SUBDIR}: FRC
cd $@; $(MAKE) ${MFLAGS} LIB=$(LIB)
FRC:

View File

@ -0,0 +1,26 @@
#
# @(#)Makefile 2.1 88/08/02 4.0 RPCSRC
#
BIN = dir_svc rls
GEN = dir_clnt.c dir_svc.c dir_xdr.c dir.h
LIB = -lrpclib
RPCCOM = rpcgen
all: $(BIN)
$(GEN): dir.x
$(RPCCOM) dir.x
dir_svc: dir_proc.o dir_svc.o dir_xdr.o
$(CC) -o $@ dir_proc.o dir_svc.o dir_xdr.o $(LIB)
rls: rls.o dir_clnt.o dir_xdr.o
$(CC) -o $@ rls.o dir_clnt.o dir_xdr.o $(LIB)
rls.o: rls.c dir.h
dir_proc.o: dir_proc.c dir.h
clean cleanup:
rm -f $(GEN) *.o $(BIN)

View File

@ -0,0 +1,37 @@
/* @(#)dir.x 2.1 88/08/02 4.0 RPCSRC */
/*
* dir.x: Remote directory listing protocol
*/
const MAXNAMELEN = 255; /* maximum length of a directory entry */
typedef string nametype<MAXNAMELEN>; /* a directory entry */
typedef struct namenode *namelist; /* a link in the listing */
/*
* A node in the directory listing
*/
struct namenode {
nametype name; /* name of directory entry */
namelist next; /* next entry */
};
/*
* The result of a READDIR operation.
*/
union readdir_res switch (int errno) {
case 0:
namelist list; /* no error: return directory listing */
default:
void; /* error occurred: nothing else to return */
};
/*
* The directory program definition
*/
program DIRPROG {
version DIRVERS {
readdir_res
READDIR(nametype) = 1;
} = 1;
} = 76;

View File

@ -0,0 +1,55 @@
/* @(#)dir_proc.c 2.1 88/08/02 4.0 RPCSRC */
/*
* dir_proc.c: remote readdir implementation
*/
#include <rpc/rpc.h>
#include <sys/dir.h>
#include "dir.h"
extern int errno;
extern char *malloc();
extern char *strcpy();
readdir_res *
readdir_1(dirname)
nametype *dirname;
{
DIR *dirp;
struct direct *d;
namelist nl;
namelist *nlp;
static readdir_res res; /* must be static! */
/*
* Open directory
*/
dirp = opendir(*dirname);
if (dirp == NULL) {
res.errno = errno;
return (&res);
}
/*
* Free previous result
*/
xdr_free(xdr_readdir_res, &res);
/*
* Collect directory entries
*/
nlp = &res.readdir_res_u.list;
while (d = readdir(dirp)) {
nl = *nlp = (namenode *) malloc(sizeof(namenode));
nl->name = malloc(strlen(d->d_name)+1);
strcpy(nl->name, d->d_name);
nlp = &nl->next;
}
*nlp = NULL;
/*
* Return the result
*/
res.errno = 0;
closedir(dirp);
return (&res);
}

View File

@ -0,0 +1,81 @@
/* @(#)rls.c 2.2 88/08/12 4.0 RPCSRC */
/*
* rls.c: Remote directory listing client
*/
#include <stdio.h>
#include <rpc/rpc.h> /* always need this */
#include "dir.h" /* need this too: will be generated by rpcgen*/
extern int errno;
main(argc, argv)
int argc;
char *argv[];
{
CLIENT *cl;
char *server;
char *dir;
readdir_res *result;
namelist nl;
if (argc != 3) {
fprintf(stderr, "usage: %s host directory\n", argv[0]);
exit(1);
}
/*
* Remember what our command line arguments refer to
*/
server = argv[1];
dir = argv[2];
/*
* Create client "handle" used for calling DIRPROG on the
* server designated on the command line. We tell the rpc package
* to use the "tcp" protocol when contacting the server.
*/
cl = clnt_create(server, DIRPROG, DIRVERS, "tcp");
if (cl == NULL) {
/*
* Couldn't establish connection with server.
* Print error message and die.
*/
clnt_pcreateerror(server);
exit(1);
}
/*
* Call the remote procedure "readdir" on the server
*/
result = readdir_1(&dir, cl);
if (result == NULL) {
/*
* An error occurred while calling the server.
* Print error message and die.
*/
clnt_perror(cl, server);
exit(1);
}
/*
* Okay, we successfully called the remote procedure.
*/
if (result->errno != 0) {
/*
* A remote system error occurred.
* Print error message and die.
*/
errno = result->errno;
perror(dir);
exit(1);
}
/*
* Successfuly got a directory listing.
* Print it out.
*/
for (nl = result->readdir_res_u.list; nl != NULL; nl = nl->next) {
printf("%s\n", nl->name);
}
}

View File

@ -0,0 +1,36 @@
#
# @(#)Makefile 2.1 88/08/11 4.0 RPCSRC
#
BIN = printmsg msg_svc rprintmsg
GEN = msg_clnt.c msg_svc.c msg.h
LIB = -lrpclib
RPCCOM = rpcgen
all: $(BIN)
#
# This is the non-networked version of the program
#
printmsg: printmsg.o
$(CC) -o $@ printmsg.o
#
# note: no xdr routines are generated here, due this service's
# use of basic data types.
#
$(GEN): msg.x
$(RPCCOM) msg.x
msg_svc: msg_proc.o msg_svc.o
$(CC) -o $@ msg_proc.o msg_svc.o $(LIB)
rprintmsg: rprintmsg.o msg_clnt.o
$(CC) -o $@ rprintmsg.o msg_clnt.o $(LIB)
rprintmsg.o: rprintmsg.c msg.h
msg_proc.o: msg_proc.c msg.h
clean cleanup:
rm -f $(GEN) *.o $(BIN)

View File

@ -0,0 +1,9 @@
/* @(#)msg.x 2.1 88/08/11 4.0 RPCSRC */
/*
* msg.x: Remote message printing protocol
*/
program MESSAGEPROG {
version MESSAGEVERS {
int PRINTMESSAGE(string) = 1;
} = 1;
} = 99;

View File

@ -0,0 +1,28 @@
/* @(#)msg_proc.c 2.1 88/08/11 4.0 RPCSRC */
/*
* msg_proc.c: implementation of the remote procedure "printmessage"
*/
#include <stdio.h>
#include <rpc/rpc.h> /* always need this here */
#include "msg.h" /* need this too: msg.h will be generated by rpcgen */
/*
* Remote verson of "printmessage"
*/
int *
printmessage_1(msg)
char **msg;
{
static int result; /* must be static! */
FILE *f;
f = fopen("/dev/console", "w");
if (f == NULL) {
result = 0;
return (&result);
}
fprintf(f, "%s\n", *msg);
fclose(f);
result = 1;
return (&result);
}

View File

@ -0,0 +1,43 @@
/* @(#)printmsg.c 2.1 88/08/11 4.0 RPCSRC */
/*
* printmsg.c: print a message on the console
*/
#include <stdio.h>
main(argc, argv)
int argc;
char *argv[];
{
char *message;
if (argc < 2) {
fprintf(stderr, "usage: %s <message>\n", argv[0]);
exit(1);
}
message = argv[1];
if (!printmessage(message)) {
fprintf(stderr, "%s: sorry, couldn't print your message\n",
argv[0]);
exit(1);
}
printf("Message delivered!\n");
}
/*
* Print a message to the console.
* Return a boolean indicating whether the message was actually printed.
*/
printmessage(msg)
char *msg;
{
FILE *f;
f = fopen("/dev/console", "w");
if (f == NULL) {
return (0);
}
fprintf(f, "%s\n", msg);
fclose(f);
return(1);
}

View File

@ -0,0 +1,74 @@
/* @(#)rprintmsg.c 2.1 88/08/11 4.0 RPCSRC */
/*
* rprintmsg.c: remote version of "printmsg.c"
*/
#include <stdio.h>
#include <rpc/rpc.h> /* always need this */
#include "msg.h" /* need this too: will be generated by rpcgen*/
main(argc, argv)
int argc;
char *argv[];
{
CLIENT *cl;
int *result;
char *server;
char *message;
if (argc < 3) {
fprintf(stderr, "usage: %s host message\n", argv[0]);
exit(1);
}
/*
* Remember what our command line arguments refer to
*/
server = argv[1];
message = argv[2];
/*
* Create client "handle" used for calling MESSAGEPROG on the
* server designated on the command line. We tell the rpc package
* to use the "tcp" protocol when contacting the server.
*/
cl = clnt_create(server, MESSAGEPROG, MESSAGEVERS, "tcp");
if (cl == NULL) {
/*
* Couldn't establish connection with server.
* Print error message and die.
*/
clnt_pcreateerror(server);
exit(1);
}
/*
* Call the remote procedure "printmessage" on the server
*/
result = printmessage_1(&message, cl);
if (result == NULL) {
/*
* An error occurred while calling the server.
* Print error message and die.
*/
clnt_perror(cl, server);
exit(1);
}
/*
* Okay, we successfully called the remote procedure.
*/
if (*result == 0) {
/*
* Server was unable to print our message.
* Print error message and die.
*/
fprintf(stderr, "%s: sorry, %s couldn't print your message\n",
argv[0], server);
exit(1);
}
/*
* The message got printed on the server's console
*/
printf("Message delivered to %s!\n", server);
}

View File

@ -0,0 +1,36 @@
#
# @(#)Makefile 2.1 88/08/11 4.0 RPCSRC
#
BIN = rsort sort_svc
GEN = sort_clnt.c sort_svc.c sort_xdr.c sort.h
LIB = -lrpclib
RPCCOM = rpcgen
all: $(BIN)
rsort: rsort.o sort_clnt.o sort_xdr.o
$(CC) $(LDFLAGS) -o $@ rsort.o sort_clnt.o sort_xdr.o $(LIB)
rsort.o: rsort.c sort.h
sort_clnt.c:
$(RPCCOM) -l sort.x >$@
sort_svc: sort_proc.o sort_svc.o sort_xdr.o
$(CC) $(LDFLAGS) -o $@ sort_proc.o sort_svc.o sort_xdr.o $(LIB)
sort_proc.o: sort_proc.c sort.h
sort_svc.c:
$(RPCCOM) -s udp sort.x >$@
sort_xdr.c:
$(RPCCOM) -c sort.x >$@
sort.h:
$(RPCCOM) -h sort.x >$@
clean cleanup:
rm -f $(GEN) *.o $(BIN)

View File

@ -0,0 +1,43 @@
/* @(#)rsort.c 2.1 88/08/11 4.0 RPCSRC */
/*
* rsort.c
* Client side application which sorts argc, argv.
*/
#include <stdio.h>
#include <rpc/rpc.h>
#include "sort.h"
main(argc, argv)
int argc;
char **argv;
{
char *machinename;
struct sortstrings args, res;
int i;
if (argc < 3) {
fprintf(stderr, "usage: %s machinename [s1 ...]\n", argv[0]);
exit(1);
}
machinename = argv[1];
args.ss.ss_len = argc - 2; /* substract off progname, machinename */
args.ss.ss_val = &argv[2];
res.ss.ss_val = (char **)NULL;
if ((i = callrpc(machinename, SORTPROG, SORTVERS, SORT,
xdr_sortstrings, &args, xdr_sortstrings, &res)))
{
fprintf(stderr, "%s: call to sort service failed. ", argv[0]);
clnt_perrno(i);
fprintf(stderr, "\n");
exit(1);
}
for (i = 0; i < res.ss.ss_len; i++) {
printf("%s\n", res.ss.ss_val[i]);
}
/* should free res here */
exit(0);
}

View File

@ -0,0 +1,19 @@
/* @(#)sort.x 2.1 88/08/11 4.0 RPCSRC */
/*
* The sort procedure receives an array of strings and returns an array
* of strings. This toy service handles a maximum of 64 strings.
*/
const MAXSORTSIZE = 64;
const MAXSTRINGLEN = 64;
typedef string str<MAXSTRINGLEN>; /* the string itself */
struct sortstrings {
str ss<MAXSORTSIZE>;
};
program SORTPROG {
version SORTVERS {
sortstrings SORT(sortstrings) = 1;
} = 1;
} = 22855;

View File

@ -0,0 +1,27 @@
/* @(#)sort_proc.c 2.1 88/08/11 4.0 RPCSRC */
#include <rpc/rpc.h>
#include "sort.h"
static int
comparestrings(sp1, sp2)
char **sp1, **sp2;
{
return (strcmp(*sp1, *sp2));
}
struct sortstrings *
sort_1(ssp)
struct sortstrings *ssp;
{
static struct sortstrings ss_res;
if (ss_res.ss.ss_val != (str *)NULL)
free(ss_res.ss.ss_val);
qsort(ssp->ss.ss_val, ssp->ss.ss_len, sizeof (char *), comparestrings);
ss_res.ss.ss_len = ssp->ss.ss_len;
ss_res.ss.ss_val = (str *)malloc(ssp->ss.ss_len * sizeof(str *));
bcopy(ssp->ss.ss_val, ss_res.ss.ss_val,
ssp->ss.ss_len * sizeof(str *));
return(&ss_res);
}