What’s the correct answer about getservbyname?

All of the possibilities are wrong to first order. This is a bad exam question and you may tell your instructor I said so.

(a) is the least wrong answer. getservbyname could be used by a generic TCP client to determine what port of a server to connect to, given a textual service name. However, it has several other uses, and it’s also obsolete: new programs should be using getaddrinfo (which does both getservbyname‘s job and gethostbyname‘s job, simultaneously, and more flexibly).

(b) is completely wrong. getservbyname may require read access to /etc/services, depending on how Name Service Switch is configured; under no circumstances will it require write access to that file. (The contents of /etc/services are defined by the IANA service name and port number registry, and nowadays the only programs that should ever modify that file are the sysadmin’s text editor and the OS’s package manager. I don’t remember off the top of my head whether there was ever a C library interface to update that file, but if it exists it would be even more obsolete than getservbyname.)

(c) is partially wrong; a server could reasonably use getservbyname to look up the port that it was supposed to bind to, but, as explained above, it would at most need read access to /etc/services to do that. (Saying “permission to modify” in option (b) and then just “access” in (c) is sloppy question-writing technique.)

(d) is wrong since (b) is wrong.

Leave a Comment