1

This code from Stevens et al., Advanced Programming in the Unix Environment, Figure 16.17 is a server program to provide system uptime:

#include "apue.h"
#include <netdb.h>
#include <errno.h>
#include <syslog.h>
#include <sys/socket.h>

#define BUFLEN  128
#define QLEN 10

#ifndef HOST_NAME_MAX
#define HOST_NAME_MAX 256
#endif

extern int initserver(int, const struct sockaddr *, socklen_t, int);
void
serve(int socked);

int
main(int argc, char *argv[])
{
    struct addrinfo *ailist, *aip;
    struct addrinfo hint;
    int             sockfd, err, n;
    char            *host;

    if (argc != 1)
        err_quit("usage: ruptimed");
    if ((n = sysconf(_SC_HOST_NAME_MAX)) < 0)
        n = HOST_NAME_MAX;  /* best guess */
    if ((host = malloc(n)) == NULL)
        err_sys("malloc error");
    if (gethostname(host, n) < 0)
        err_sys("gethostname error");
    daemonize("ruptimed");
    memset(&hint, 0, sizeof(hint));
    hint.ai_flags = AI_CANONNAME;
    hint.ai_socktype = SOCK_STREAM;
    hint.ai_canonname = NULL;
    hint.ai_addr = NULL;
    hint.ai_next = NULL;
    if ((err = getaddrinfo(host, "ruptime", &hint, &ailist)) != 0) {
        syslog(LOG_ERR, "ruptimed: getaddrinfo error: %s",
          gai_strerror(err));
        exit(1);
    }
    for (aip = ailist; aip != NULL; aip = aip->ai_next) {
        if ((sockfd = initserver(SOCK_STREAM, aip->ai_addr,
          aip->ai_addrlen, QLEN)) >= 0) {
            serve(sockfd);
            exit(0);
        }
    }
    exit(1);
}

What confused me is the function call getaddrinfo, it just tells me the service name is ruptime, and I have no idea where this name comes from. Did the service-name get named after the name of this program? How can I determine the service name? Can I designate the service name by myself?

I didn't duplicate the code of initserver and serve, because I think it doesn't concern the question.

roschach
  • 8,390
  • 14
  • 74
  • 124
Sherwin
  • 847
  • 1
  • 6
  • 16
  • I am doing the same exercise from the same book and I get an error from `getaddrinfo` because, I think, the service `ruptime` does not exist. How did you define and create this service? I cannot find any reference in the book about the creation of this service: did you? – roschach Dec 10 '18 at 13:35
  • @FrancescoBoi It seems that you need to add that service to `/etc/service` by yourself. I didn't do that. Instead I delete some code and initialize the server directly. – Rick Oct 25 '19 at 02:21

1 Answers1

2

The service name is simply a key to look up in /etc/services; i.e. it's a symbolic reference to a port number.

Gil Hamilton
  • 11,973
  • 28
  • 51
  • thanks for your answering, but what confuses me is how to determine the service name? can i designate the service name by myself? – Sherwin May 17 '16 at 00:08
  • 1
    Like Gil said, the service names are stored in the [`services`](http://man7.org/linux/man-pages/man5/services.5.html) file. You can add whatever custom entries you want to that file. `getaddrinfo()` will check if the `service` parameter is numeric, and if not then lookup the corresponding entry in the file. – Remy Lebeau May 17 '16 at 02:07
  • Stevens' client code also uses "ruptime" instead of a numeral. If I add an entry to /etc/services on the server's host, I would also need to add the same entry to the /etc/services files on the hosts of all clients. Would I not? – Theodore Norvell May 25 '16 at 20:45
  • Correct. If you use the name, it must appear in the local /etc/services file for every host executing the code. – Gil Hamilton May 26 '16 at 14:19