RPC protocol compiler
rpcgen is a tool that generates C code to
implement an RPC protocol. The input is a language similar to C known as RPC
Language (Remote Procedure Call Language).
normally used as in the first synopsis where it takes an input file and
generates up to four output files. If the infile is
named proto.x, then
will generate a header file in proto.h, XDR routines
in proto_xdr.c, server-side stubs in
proto_svc.c, and client-side stubs in
proto_clnt.c. With the
option, it will also generate the RPC dispatch table in
proto_tbl.i. With the
option, it will also generate sample code which would illustrate how to use
the remote procedures on the client side. This code would be created in
proto_client.c. With the
option, it will also generate a sample server code which would illustrate
how to write the remote procedures. This code would be created in
The server created can be started both by the port monitors (for
example, inetd(8)) or by itself. When it is started by a port monitor, it
creates servers only for the transport for which the file descriptor 0 was
passed. The transports are chosen at run time and not at compile time. When
the server is self-started, it backgrounds itself by default. A special
RPC_SVC_FG can be used to run the
server process in the foreground.
The second synopsis provides special features which allow for the
creation of more sophisticated RPC servers. These features include support
for user provided
#defines and RPC dispatch tables.
The entries in the RPC dispatch table contain:
- pointers to the service routine corresponding to that procedure,
- a pointer to the input and output arguments,
- the size of these routines
A server can use the dispatch table to check authorization and then to execute the service routine; a client library may use it to deal with the details of storage management and XDR data conversion.
The other three synopses shown above are used when one does not
want to generate all the output files, but only a particular one. Some
examples of their usage is described in the
EXAMPLES section below. When
rpcgen is executed with the
-s option, it creates servers for that particular
class of transports. When executed with the
option, it creates a server for the transport specified by
netid. If infile is not specified,
rpcgen accepts the standard input.
The C preprocessor, cpp(1) is run on the input file before it is actually
rpcgen. For each type of output file,
rpcgen defines a special preprocessor symbol for use
- Defined when compiling into header files.
- Defined when compiling into XDR routines.
- Defined when compiling into server-side stubs.
- Defined when compiling into client-side stubs.
- Defined when compiling into RPC dispatch tables.
Any line beginning with ‘%’ is passed directly into
the output file, uninterpreted by
For every data type referred to in infile
rpcgen assumes that there exists a routine with the
string “xdr_” prepended to the name of the data type. If this
routine does not exist in the RPC/XDR library, it must be provided.
Providing an undefined data type allows customization of XDR routines.
The options are as follows:
- Generate all the files including sample code for client and server side.
- This generates code for the SunOS4.1 style of RPC. This is the default.
- Compile into XDR routines.
- Generate code in ANSI C. This option also generates code that could be compiled with the C++ compiler.
- Define a symbol
name. Equivalent to the
#definedirective in the source. If no value is given, value is defined as 1. This option may be specified more than once.
- Compile into C data-definitions (a header file). The
-Toption can be used in conjunction to produce a header file which supports RPC dispatch tables.
- By default, services created using
rpcgenwait 120 seconds after servicing a request before exiting. That interval can be changed using the
-Kflag. To create a server that exits immediately upon servicing a request, “
-K0” can be used. To create a server that never exits, the appropriate argument is “
When monitoring for a server, some port monitors, like the AT&T System V UNIX listen utility, always spawn a new process in response to a service request. If it is known that a server will be used with such a monitor, the server should exit immediately on completion. For such servers,
rpcgenshould be used with “
- Compile into client-side stubs.
- Compile into server-side stubs, but do not generate a
main() routine. This option is useful for doing callback-routines and for users who need to write their own
main() routine to do initialization.
- Compile into server-side stubs for the transport specified by netid. There should be an entry for netid in the netconfig database. This option may be specified more than once, so as to compile a server that serves multiple transports.
- Use the newstyle of
rpcgen. This allows procedures to have multiple arguments. It also uses the style of parameter passing that closely resembles C. So, when passing an argument to a remote procedure you do not have to pass a pointer to the argument but the argument itself. This behaviour is different from the oldstyle of
rpcgengenerated code. The newstyle is not the default case because of backward compatibility.
- Specify the name of the output file. If none is specified, standard output
is used (
- Compile into server-side stubs for all the transports belonging to the class nettype. The supported classes are netpath, visible, circuit_n, circuit_v, datagram_n, datagram_v, tcp, and udp [see rpc(3) for the meanings associated with these classes. Note: BSD currently supports only the tcp and udp classes]. This option may be specified more than once. Note: the transports are chosen at run time and not at compile time.
- Generate sample code to show the use of remote procedure and how to bind
to the server before calling the client side stubs generated by
- Generate skeleton code for the remote procedures on the server side. You would need to fill in the actual code for the remote procedures.
- Compile into RPC dispatch table.
- Generate the code to support RPC dispatch tables.
-t are used exclusively to generate a particular
type of file, while the options
-T are global and can be used with the other
$ rpcgen -T prot.x
generates the five files: prot.h, prot_clnt.c, prot_svc.c, prot_xdr.c and prot_tbl.i.
The following example sends the C data-definitions (header file) to standard output:
$ rpcgen -h prot.x
To send the test version of the
server side stubs for all the transport belonging to the class
datagram_n to standard output, use:
$ rpcgen -s datagram_n -DTEST prot.x
To create the server side stubs for the transport indicated by netid tcp, use:
$ rpcgen -n tcp -o prot_svc.c prot.x
The RPC Language does not support nesting of structures. As a workaround, structures can be declared at the top-level, and their name used inside other structures in order to achieve the same effect.
Name clashes can occur when using program definitions, since the apparent scoping does not really apply. Most of these can be avoided by giving unique names for programs, versions, procedures, and types.
The server code generated with
refers to the transport indicated by netid and hence is
very site specific.