DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Interprocess communication using UNIX domain sockets

Binding socket names (UNIX domain)

A socket is created without a name. To be used, it must be given a name so that processes can reference it and messages can be received on it. Names can be bound explicitly by a process; otherwise, the system assigns a name (with an ``implicit bind'') during certain calls (such as connect).

Communicating processes are bound by an association. Unlike the Internet domain, an association in the UNIX domain is composed of local and foreign pathnames. (A ``foreign pathname'' is a pathname created by a foreign process, not a pathname on a foreign system). UNIX domain sockets need not always be explicitly bound to a name, but when bound there may never be duplicate <local pathname, foreign pathname> pairs. Currently, the pathnames may not refer to files already existing on the system.

UNIX domain socket names, like file pathnames, may be either absolute (like /dev/socket) or relative (like ../socket). Because these names are used to allow processes to establish connections, relative pathnames can pose difficulties and should be used with care. When a name is bound into the name space, a file is created in the file system. If the file is not removed (unlinked), the name will continue to exist even after the bound socket is closed. This can cause subsequent runs of a program to find that a name is unavailable for use, and can cause directories to fill up with these objects. The names are removed by calling unlink or using the rm command. Names in the UNIX domain are only used for establishing connections. They are not used for message delivery once a connection is established. Therefore, in contrast with the Internet domain, unbound sockets need not be (and are not) automatically given addresses when they are connected.

The bind call is used to assign a name to a socket on the local side of a connection. For example:

bind(s, name, namelen);

The argument s in the line above is the socket descriptor returned from the socket() call. (See ``Creating a Socket,'' above.)

The argument name is a variable length byte string to be interpreted by the supporting protocol or protocols according to the domain type. The argument namelen is the length of name.

In the UNIX domain, names contain a pathname and a family, which is always AF_UNIX. To bind the name /dev/foo to a UNIX domain socket:

#include <sys/un.h>
 ...
struct sockaddr_un addr;
 ...
strcpy(addr.sun_path, "/dev/foo");
addr.sun_family = AF_UNIX;
bind(s, (struct sockaddr *) &addr, strlen(addr.sun_path) +
    sizeof (addr.sun_family));
Note that in determining the size of a UNIX domain address, null bytes are not counted, which is why strlen is used. In the current implementation of UNIX domain IPC, the filename referred to in addr.sun_path is created as a pipe in the system file space. The caller must, therefore, have write permission in the directory where addr.sun_path is to reside, and this file should be deleted by the caller when it is no longer needed.
Next topic: Making a connection (UNIX domain)
Previous topic: Socket creation errors (UNIX domain)

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003