getrpcbyname_r - Linux


Overview

getrpcbyname_r is used to retrieve information about a RPC service based on its name, program number, protocol, or version number. It is a thread-safe version of getrpcbyname.

Syntax

int getrpcbyname_r(const char *name, struct rpc_service *result, char *buffer, size_t buflen, struct rpc_service **res);

Options/Flags

The following options are supported:

  • name: The name of the RPC service to be queried.
  • result: The RPC service structure to be filled with the retrieved information.
  • buffer: A buffer to store the RPC service name.
  • buflen: The size of the buffer provided.
  • res: A pointer to the RPC service structure.

Examples

To retrieve information about the RPC service named "rpcbind":

#include <rpc/rpc.h>

int main() {
  struct rpc_service service;
  char buffer[1024];
  struct rpc_service *result;

  if (getrpcbyname_r("rpcbind", &service, buffer, sizeof(buffer), &result) != 0) {
    perror("getrpcbyname_r");
    return 1;
  }

  printf("RPC service name: %s\n", service.rs_name);
  printf("RPC program number: %d\n", service.rs_number);
  printf("RPC protocol: %d\n", service.rs_proto);
  printf("RPC version number: %d\n", service.rs_vers);

  return 0;
}

Common Issues

  • The program doesn’t terminate: Ensure that the buffer provided is large enough to store the RPC service name.
  • The program returns an error: Check the error code returned by getrpcbyname_r to determine the cause of the error. Common error codes include:
    • RPC_SUCCESS: The function succeeded.
    • RPC_CANTENCODEARGS: The arguments could not be encoded.
    • RPC_CANTDECODEARGS: The arguments could not be decoded.
    • RPC_CANTRECV: The RPC server could not be reached.
    • RPC_CANTDECODERES: The RPC response could not be decoded.

Integration

getrpcbyname_r can be used with other RPC-related commands, such as rpcinfo and rpcgen. It can also be used with the getaddrinfo function to retrieve the network address of an RPC service.

Related Commands

  • getrpcbynumber_r
  • getrpcbyport_r
  • rpcinfo
  • rpcgen
  • getaddrinfo