The OpenNET Project / Index page

[ новости /+++ | форум | wiki | теги | ]

Интерактивная система просмотра системных руководств (man-ов)

 [Cписок руководств | Печать]

unix (4)
  • unix (4) ( FreeBSD man: Специальные файлы /dev/* )
  • >> unix (4) ( Linux man: Специальные файлы /dev/* )
  • unix (7) ( Русские man: Макропакеты и соглашения )
  • unix (7) ( Linux man: Макропакеты и соглашения )
  • Ключ unix обнаружен в базе ключевых слов.


    unix, PF_UNIX, AF_UNIX, PF_LOCAL, AF_LOCAL - Sockets for local interprocess communication.


    #include <sys/socket.h>
    #include <sys/un.h>
    unix_socket = socket(PF_UNIX, type, 0);
    error = socketpair(PF_UNIX, type, 0, int *sv);



    The PF_UNIX (also called PF_LOCAL) socket family is used to communicate between processes on the same machine efficiently. Unix sockets can be either anonymous (created by socketpair(2) ) or associated with an socket object in the filesystem namespace (and subject to the usual filesystem permission checks). Since Linux 2.2 an abstract name space independent from the file system is supported too.

    Valid types are SOCK_STREAM for a stream oriented socket type and SOCK_DGRAM for a datagram oriented socket type that preserves message boundaries. Unix sockets are always reliable.

    Unix sockets support passing file descriptors or process credentials to other processes using ancillary data.



    A unix address is defined as a unique string either in the filesystem or in the abstract namespace. Sockets create by socketpair(2) don't have an address. For other sockets the target address can be set using connect(2). The local address can be set using bind(2). When a socket is connected and it doesn't have a local address already a unique address in the abstract namespace will be generated automatically.

    #define UNIX_PATH_MAX   108
    struct sockaddr_un {
            sa_family_t sun_family; /* AF_UNIX */
            char sun_path[UNIX_PATH_MAX];   /* pathname */

    sun_family always contains AF_UNIX (or AF_LOCAL which is a synonym) sun_path contains the null terminated pathname of the filesystem socket object. If sun_path starts with a 0 byte it refers to the abstract namespace maintained by the Unix protocol module. After that a non-zero terminated byte sequence of the passed length number of bytes - 1 follows.



    For historical reasons these socket options are specified with a SOL_SOCKET type. They are PF_UNIX specific though. They can be set with setsockopt(2) and read with getsockopt(2) by specifying SOL_SOCKET as the socket family.

    SO_PASSCRED enables the receiving of the credentials of the sending process ancillary message. When this option is set and the socket is not connected yet an unique name in the abstract namespace will be generated automatically. Expects an integer boolean flag.



    For historical reasons these ancillary message type are specified with a SOL_SOCKET type. They are PF_UNIX specific though. To send them set the cmsg_level field of the struct cmsghdr to SOL_SOCKET and the cmsg_type field to the type. For more information see cmsg(3).

    SCM_RIGHTS Send or receive a file descriptor. The data portion contains a integer array of the file descriptors.

    SCM_CREDENTIALS Send or receive the credentials of the sending process. This can be used for authentication. The credentials are passed as a struct ucred ancillary message.

    struct ucred {
            pid_t   pid;    /* process id of the sending process */  
            uid_t   uid;    /* user id of the sending process */ 
            gid_t   gid;    /* group id of the sending process */ 

      During sending only root processes are allowed specify credentials they don't own. On receiving the current credentials of the sending process are passed, unless the user specified different credentials (and had the rights to do that). To receive the message the SO_PASSCRED option must be enabled.



    SCM_CREDENTIALS and the abstract namespace were introduced with Linux 2.2.



    In Linux PF_UNIX sockets visible in the filesystem honor the permissions of the the directory they are part of. It is also possible to change their owner, groups and permissions. To create a new socket (bind) write and executable permission to the directory containing the socket is needed, for connecting read/write permissions to the socket object in the filesystem. This behavior differs from many BSD derived systems which ignore permissions for Unix sockets. Portable programs should not rely on this feature.

    To pass file descriptors or credentials you need to send/read at least one byte.



    Out of memory.

    connect(2) called with a socket object that isn't listening. This can happen when the remote socket does not exist or the filename is not a socket.

    Invalid argument passed. A common cause is the missing setting of AF_UNIX in the sun_type field of passed addresses or the socket being in an invalid state for the applied operation.

    Stream operation called on non-stream oriented socket or tried to use the out-of-band data option.

    Passed protocol is not PF_UNIX.

    Unknown socket type.

    Remote socket does not match the local socket type (SOCK_DGRAM vs. SOCK_STREAM)

    Selected local address is already taken or filesystem socket object already exists.

    connect(2) called on an already connected socket or a target address was specified on a connected socket.

    Socket operation needs a target address, but the socket is not connected.

    Remote socket was unexpectedly closed.
    Remote socket was closed on a stream socket. If enabled, a SIGPIPE is sent as well. This can be avoided by passing the MSG_NOSIGNAL flag to sendmsg(2) or recvmsg(2).
    User memory address was not valid.

    Other errors can be generated by the generic socket layer or by the filesystem while generating a filesystem socket object. See the appropriate manual pages for more information.  


    socket(4), sendmsg(2), recvmsg(2), socketpair(2), socket(2)




    Поиск по тексту MAN-ов: 

    Inferno Solutions
    Hosting by

    Закладки на сайте
    Проследить за страницей
    Created 1996-2023 by Maxim Chirkov
    Добавить, Поддержать, Вебмастеру