UNIX

UNIX-domain protocol family

Synopsis:

#include <sys/socket.h>
#include <sys/un.h>

socket( AF_LOCAL, 
        SOCK_STREAM, 
        0 );

socket( AF_LOCAL, 
        SOCK_DGRAM, 
        0 );

Description:

The UNIX-domain protocol family provides local (on-machine) interprocess communication through the normal socket() mechanisms. The UNIX-domain family supports the SOCK_STREAM and SOCK_DGRAM socket types and uses filesystem pathnames for addressing.

Addressing

UNIX-domain addresses are variable-length filesystem pathnames of at most 104 characters. The <sys/un.h> include file defines this address:

struct sockaddr_un {
    u_char  sun_len;
    u_char  sun_family;
    char    sun_path[104];
};

Binding a name to a UNIX-domain socket with bind() causes a socket file to be created in the filesystem. This file isn't removed when the socket is closed; you must use unlink() to remove the file.

If the leading byte of the address is '\0', the socket is an abstract Unix-domain socket. Abstract sockets don't create nodes in the path space, which in turn means they don't need to be unlinked.

You can use the macro SUN_LEN() (defined in <sys/un.h>) to calculate the length of UNIX-domain address, required by bind() and connect() . The sun_path field must be terminated by a NUL character to be used with SUN_LEN(), but the terminating NUL isn't part of the address.

The UNIX-domain protocol family doesn't support broadcast addressing or any form of "wildcard" matching on incoming messages. All addresses are absolute- or relative-pathnames of other UNIX-domain sockets. Normal filesystem access-control mechanisms are also applied when referencing pathnames (e.g. the destination of a connect() or sendto() must be writable).

Protocols

The UNIX-domain protocol family consists of simple transport protocols that support the SOCK_STREAM and SOCK_DGRAM abstractions. UNIX-domain sockets also support the communication of BlackBerry 10 OS file descriptors through the use of the msg_control field in the msg argument to sendmsg() and recvmsg() .

Any valid descriptor may be sent in a message. The file descriptor to be passed is described using a struct cmsghdr defined in the include file <sys/socket.h>. The type of the message is SCM_RIGHTS, and the data portion of the messages is an array of integers representing the file descriptors to be passed. The number of descriptors being passed is defined by the length field of the message; the length field is the sum of the size of the header plus the size of the array of file descriptors.

The received descriptor is a duplicate of the sender's descriptor, as if it were created with a call to dup() . Descriptors awaiting delivery or purposely not received are automatically closed by the system when the destination socket is closed.

LOCAL_CREDS

There is one socket-level option for setsockopt() and getsockopt() available in the UNIX-domain. The LOCAL_CREDS option may be enabled on a SOCK_DGRAM or a SOCK_STREAM socket. This option provides a mechanism for the receiver to receive the credentials of the process as a recvmsg() message. The msg_control field in the msghdr structure points to a buffer that contains a cmsghdr structure followed by a variable length sockcred structure defined in <sys/socket.h> as follows:

struct sockcred {
    uid_t   sc_uid;         /* real user id */
    uid_t   sc_euid;        /* effective user id */
    gid_t   sc_gid;         /* real group id */
    gid_t   sc_egid;        /* effective group id */
    int     sc_ngroups;     /* number of supplemental groups */
    gid_t   sc_groups[1];   /* variable length */
};

The SOCKCREDSIZE() macro computes the size of the sockcred structure for a specified number of groups. The cmsghdr fields have the following values:

cmsg_len = sizeof(struct cmsghdr) + SOCKCREDSIZE(ngroups)
cmsg_level = SOL_SOCKET
cmsg_type = SCM_CREDS

Last modified: 2013-12-21

comments powered by Disqus