ptg10805159
Section 17.5 An Open Server,Version 1 653
17.5 An Open Server,Version 1
Using file descriptor passing, we now develop an open server—a program that is
executed by a process to open one or morefiles. Instead of sending the contents of the
file back to the calling process, however,this server sends back an open file descriptor.
As a result, the open server can work with any type of file (such as a device or a socket)
and not simply regular files. The client and server exchange a minimum amount of
information using IPC: the filename and open mode sent by the client, and the
descriptor returned by the server.The contents of the file arenot exchanged using IPC.
Thereare several advantages in designing the server to be a separate executable
program (either one that is executed by the client, as we develop in this section, or a
daemon server,which we develop in the next section).
•The server can easily be contacted by any client, similar to the client calling a
library function. We are not hard-coding a particular service into the
application, but designing a general facility that others can reuse.
•If we need to change the server,only a single program is affected. Conversely,
updating a library function can requirethat all programs that call the function be
updated (i.e., relinked with the link editor). Shared libraries can simplify this
updating (Section 7.7).
•The server can be a set-user-ID program, providing it with additional
permissions that the client does not have. Note that a library function (or shared
library function) can’t provide this capability.
The client process creates an fd-pipe and then callsforkandexecto invoke the
server.The client sends requests across the fd-pipe using one end, and the server sends
back responses over the fd-pipe using the other end.
We define the following application protocol between the client and the server.
- The client sends a request of the form ‘‘open
\0’’
across the fd-pipe to the server.Theis the numeric value, in ASCII
decimal, of the second argument to theopenfunction. Thisrequest string is
terminated by a null byte. - The server sends back an open descriptor or an error by calling eithersend_fd
orsend_err.
This is an example of a process sending an open descriptor to its parent. In Section 17.6,
we’ll modify this example to use a single daemon server,wherethe server sends a
descriptor to a completely unrelated process.
We first have the header,open.h (Figure17.17), which includes the standard
headers and defines the function prototypes.
#include "apue.h"
#include <errno.h>
#define CL_OPEN "open" / client’s request for server /
int csopen(char *, int);
Figure 17.17 Theopen.hheader