Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

638 Advanced IPC Chapter 17


goto errout;
}

if (listen(fd, QLEN) < 0) { /* tell kernel we’re a server */
rval = -4;
goto errout;
}
return(fd);

errout:
err = errno;
close(fd);
errno = err;
return(rval);
}

Figure 17.8Theserv_listenfunction

First, we create a single UNIX domain socket by callingsocket.Wethen fill in a
sockaddr_unstructurewith the well-known pathname to be assigned to the socket.
This structure is the argument tobind.Note that we don’t need to set thesun_len
field present on some platforms, because the operating system sets this for us, deriving
it from the address length we pass to thebindfunction.
Finally, we calllisten(Section 16.4) to tell the kernel that the process will be
acting as a server awaiting connections from clients. When a connect request from a
client arrives, the server calls theserv_acceptfunction (Figure17.9).
#include "apue.h"
#include <sys/socket.h>
#include <sys/un.h>
#include <time.h>
#include <errno.h>
#define STALE 30 /* client’s name can’t be older than this (sec) */
/*
*Wait for a client connection to arrive, and accept it.
* We also obtain the client’s user ID from the pathname
*that it must bind before calling us.
*Returns new fd if all OK, <0 on error
*/
int
serv_accept(int listenfd, uid_t *uidptr)
{
int clifd, err, rval;
socklen_t len;
time_t staletime;
struct sockaddr_un un;
struct stat statbuf;
char *name;
/* allocate enough space for longest name plus terminating null */
Free download pdf