Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Section 17.7 Summary 669


/* parse the arguments, set options */
if (buf_args(buf, cli_args) < 0) {
send_err(clifd, -1, errmsg);
log_msg(errmsg);
return;
}

if ((newfd = open(pathname, oflag)) < 0) {
snprintf(errmsg, MAXLINE-1, "can’t open %s: %s\n",
pathname, strerror(errno));
send_err(clifd, -1, errmsg);
log_msg(errmsg);
return;
}

/* send the descriptor */
if (send_fd(clifd, newfd) < 0)
log_sys("send_fd error");
log_msg("sent fd %d over fd %d for %s", newfd, clifd, pathname);
close(newfd); /* we’re done with descriptor */
}

Figure 17.31 Therequestfunction, version 2

This completes the second version of the open server,which uses a single daemon to
handle all the client requests.

17.7 Summary


The key points in this chapter arethe ability to pass file descriptors between processes
and the ability of a server to accept unique connections from clients. Although all
platforms provide support for UNIX domain sockets (refer back to Figure15.1), we’ve
seen that thereare differences in each implementation, which makes it moredifficult for
us to develop portable applications.
We used UNIX domain sockets throughout this chapter.Wesaw how to use them
to implement a full-duplex pipe and how they can be used to adapt the I/O
multiplexing functions from Section 14.4 to work indirectly with XSI message queues.
We presented two versions of an open server.One version was invoked directly by
the client, usingforkandexec.The second was a daemon server that handled all
client requests. Both versions used the file descriptor passing and receiving functions.
We also saw how to use thegetoptfunction to enforce consistent command-line
processing for our programs. The final version of the open server used thegetopt
function, the client–server connection functions introduced in Section 17.3, and the I/O
multiplexing functions from Section 14.4.
Free download pdf