NAME(名称)
unix, PF_UNIX, AF_UNIX, PF_LOCAL, AF_LOCAL - 用于本地内部进程通讯的套接字。
SYNOPSIS(总览)
#include <sys/socket.h>
#include <sys/un.h>
unix_socket = socket(PF_UNIX, type, 0);
error = socketpair(PF_UNIX, type, 0, int *sv);
DESCRIPTION(描述)
PF_UNIX (也称作 PF_LOCAL ) 套接字族用来在同一机器上的提供有效的进程间通讯.Unix 套接字可以是匿名的(由 socketpair(2) 创建), 也可以与套接字类型文件相关联. Linux 还支持一种抽象名字空间, 它是独立于文件系统的.
有效的类型有: SOCK_STREAM 用于面向流的套接字, SOCK_DGRAM 用于面向数据报的套接字,其可以保存消息界限. Unix 套接字总是可靠的,而且不会重组数据报.
Unix 套接字支持把文件描述符或者进程的信用证明作为数据报的辅助数据传递给其它进程.
ADDRESS FORMAT(地址格式)
unix 地址定义为文件系统中的一个文件名或者抽象名字空间中的一个单独的字符串. 由 socketpair(2) 创建的套接字是匿名的.对于非匿名的套接字,目标地址可使用 connect(2) 设置. 本地地址可使用 bind(2) 设置. 当套接字连接上而且它没有一个本地地址时, 会自动在抽象名字空间中生成一个唯一的地址.
-
#define UNIX_PATH_MAX 108 struct sockaddr_un { sa_family_t sun_family; /* AF_UNIX */ char sun_path[UNIX_PATH_MAX]; /* 路径名 */ };
sun_family 总是包含 AF_UNIX. sun_path 包含空零结尾的套接字在文件系统中的路径名. 如果 sun_path 以空零字节开头,它指向由 Unix 协议模块维护的抽象名字空间. 该套接字在此名字空间中的地址由 sun_path 中的剩余字节给定. 注意抽象名字空间的名字都不是空零终止的.
SOCKET OPTIONS(套接字选项)
由于历史原因, 这些套接字选项通过 SOL_SOCKET 类型确定, 即使它们是 PF_UNIX 指定的. 它们可以由 setsockopt(2) 设置. 通过指定 SOL_SOCKET 作为套接字族用 getsockopt(2) 来读取.
SO_PASSCRED 允许接收进程辅助信息发送的信用证明. 当设置了该选项且套接字尚未连接时, 则会自动生成一个抽象名字空间的唯一名字. 值为一个整数布尔标识.
ANCILLARY MESSAGES(辅助信息)
由于历史原因,这些辅助信息类型通过 SOL_SOCKET 类型确定, 即使它们是 PF_UNIX 指定的. 要发送它们, 可设置结构 cmsghdr 的 cmsg_level 字段为 SOL_SOCKET, 并设置 cmsg_type 字段为其类型. 要获得更多信息, 请参看 cmsg(3).
- SCM_RIGHTS
- 为其他进程发送或接收一套打开文件描述符. 其数据部分包含一个文件描述符的整型数组. 已传文件描述符的效果就如它们已由 dup(2) 创建过一样.
- SCM_CREDENTIALS
- 发送或者接收 unix 信用证明. 可用作认证.信用证明传送以 struct ucred 辅助信息的形式传送.
-
struct ucred { pid_t pid; /* 发送进程的进程标识 */ uid_t uid; /* 发送进程的用户标识 */ gid_t gid; /* 发送进程的组标识 */ };
发送者确定的信用证明由内核检查. 一个带有有效用户标识 0 的进程允许指定不与其自身值相匹配的值.发送者必须确定其自身的进程标识(除非它带有 CAP_SYS_ADMIN), 其用户标识,有效用户标识或者设置用户标识(除非它带有 CAP_SETUID), 以及其组标识,有效组标识或者设置组标识(除非它带有 CAP_SETGID). 为了接收一条 struct ucred 消息,必须在套接字上激活 SO_PASSCRED 选项.
-
VERSIONS(版本)
SCM_CREDENTIALS 和抽象名字空间是在 Linux 2.2 中引入的,不应该在要求可移植的程序中使用.
NOTES(注意)
在 Linux 实现中, 在文件系统中可见的套接字拥有它们所在目录的权限许可. 它们的所有者,组和权限可以作修改. 如果进程不拥有对创建的套接字所在目录的写和搜索(执行)权限, 则创建一个新的套接字会失败. 这种执行方式与许多由 BSD 发展而来的系统不同, 那些系统会忽略 Unix 套接字所需的权限. 可移植的程序不应把这项功能用于安全方面.
绑定文件名到套接字会在文件系统中创建一个套接字, 这个套接字在它不再需要时必须由调用者删除(使用 unlink(2)). 通用的 Unix 相关语义可适用; 套接字可在任何时候删除, 而且当***一个引用关闭时, 最终会从文件系统中删除.
要传递文件描述符或者信用证明, 你需要发送/读取至少一个字节.
ERRORS(错误)
- ENOMEM
- 内存溢出.
- ECONNREFUSED
- connect(2) 调用了一个未在监听的套接字对象. 这可能发生在远程套接字不存在或者文件名不是套接字的时候.
- EINVAL
- 传递了无效参数. 通常的产生原因是已传地址的 sun_type 字段的 AF_UNIX 设置丢失, 或者套接字对应用的操作处于无效状态.
- EOPNOTSUPP
- 在非面向流的套接字上调用了流操作,或者试图使用出界的数据选项.
- EPROTONOSUPPORT
- 传递的协议是非 PF_UNIX 的.
- ESOCKTNOSUPPORT
- 未知的套接字类型.
- EPROTOTYPE
- 远程套接字与本地套接字类型不匹配 (SOCK_DGRAM 对 SOCK_STREAM).
- EADDRINUSE
- 选择的本地地址已经占用,或者文件系统套接字对象已经存在.
- EISCONN
- 在一个已经连接的套接字上调用 connect(2) 或者指定的目标地址在一个已连接的套接字上.
- ENOTCONN
- 套接字操作需要一个目的地址,但是套接字尚未连接.
- ECONNRESET
- 远程套接字意外关闭.
- EPIPE
- 远程套接字在一个流套接字上关闭了.如果激活,会同时发送一个 SIGPIPE 标识.这可以通过传递 MSG_NOSIGNAL 标识给 sendmsg(2) 或者 recvmsg(2) 来避免.
- EFAULT
- 用户内存地址无效.
- EPERM
- 发送者在 struct ucred 中传递无效的信用证明.
当生成一个文件系统套接字对象时, 可能会由通用套接层或者文件系统产生其它错误. 要获得更多信息,可参见合适的手册页.
SEE ALSO(另见)
recvmsg(2), sendmsg(2), socket(2), socket(2), socketpair(2), cmsg(3), socket(7)
#p#
NAME
unix, PF_UNIX, AF_UNIX, PF_LOCAL, AF_LOCAL - Sockets for local interprocess communication
SYNOPSIS
#include <sys/socket.h>
#include <sys/un.h>
unix_socket = socket(PF_UNIX, type, 0);
error = socketpair(PF_UNIX, type, 0, int *sv);
DESCRIPTION
The PF_UNIX (also known as 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 a file of type socket. Linux also supports an abstract namespace which is independent of the file system.
Valid types are SOCK_STREAM for a stream oriented socket and SOCK_DGRAM for a datagram oriented socket that preserves message boundaries. Unix sockets are always reliable and don't reorder datagrams.
Unix sockets support passing file descriptors or process credentials to other processes using ancillary data.
ADDRESS FORMAT
A unix address is defined as a filename in the filesystem or as a unique string in the abstract namespace. Sockets created by socketpair(2) are anonymous. For non-anonymous 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 already have a local address 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. sun_path contains the zero-terminated pathname of the socket in the file system. If sun_path starts with a zero byte it refers to the abstract namespace maintained by the Unix protocol module. The socket's address in this namespace is given by the rest of the bytes in sun_path. Note that names in the abstract namespace are not zero-terminated.
SOCKET OPTIONS
For historical reasons these socket options are specified with a SOL_SOCKET type even though they are PF_UNIX specific. 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 yet connected a unique name in the abstract namespace will be generated automatically. Expects an integer boolean flag.
ANCILLARY MESSAGES
Ancillary data is sent and received using sendmsg(2) and recvmsg(2). For historical reasons the ancillary message types listed below are specified with a SOL_SOCKET type even though they are PF_UNIX specific. 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 set of open file descriptors from another process. The data portion contains an integer array of the file descriptors. The passed file descriptors behave as though they have been created with dup(2).
- SCM_CREDENTIALS
- Send or receive unix credentials. 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 */ };
The credentials which the sender specifies are checked by the kernel. A process with effective user ID 0 is allowed to specify values that do not match his own. The sender must specify its own process ID (unless it has the capability CAP_SYS_ADMIN), its user ID, effective user ID or set user ID (unless it has CAP_SETUID), and its group id, effective group ID or set group ID (unless it has CAP_SETGID). To receive a struct ucred message the SO_PASSCRED option must be enabled on the socket. -
VERSIONS
SCM_CREDENTIALS and the abstract namespace were introduced with Linux 2.2 and should not be used in portable programs. (Some BSD-derived systems also support credential passing, but the implementation details differ.)
NOTES
In the Linux implementation, sockets which are visible in the filesystem honour the permissions of the directory they are in. Their owner, group and their permissions can be changed. Creation of a new socket will fail if the process does not have write and search (execute) permission on the directory the socket is created in. Connecting to the socket object requires read/write permission. This behavior differs from many BSD-derived systems which ignore permissions for Unix sockets. Portable programs should not rely on this feature for security.
Binding to a socket with a filename creates a socket in the file system that must be deleted by the caller when it is no longer needed (using unlink(2)). The usual Unix close-behind semantics apply; the socket can be unlinked at any time and will be finally removed from the file system when the last reference to it is closed.
To pass file descriptors or credentials over a SOCK_STREAM, you need to send/recv at least one byte of non-ancillary data in the same send/recv_msg call.
Unix domain stream sockets do not support the notion of out-of-band data.
ERRORS
- ENOMEM
- Out of memory.
- ECONNREFUSED
- 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.
- EINVAL
- 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.
- EOPNOTSUPP
- Stream operation called on non-stream oriented socket or tried to use the out-of-band data option.
- EPROTONOSUPPORT
- Passed protocol is not PF_UNIX.
- ESOCKTNOSUPPORT
- Unknown socket type.
- EPROTOTYPE
- Remote socket does not match the local socket type (SOCK_DGRAM vs. SOCK_STREAM)
- EADDRINUSE
- Selected local address is already taken or filesystem socket object already exists.
- EISCONN
- connect(2) called on an already connected socket or a target address was specified on a connected socket.
- ENOTCONN
- Socket operation needs a target address, but the socket is not connected.
- ECONNRESET
- Remote socket was unexpectedly closed.
- EPIPE
- 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).
- EFAULT
- User memory address was not valid.
- EPERM
- The sender passed invalid credentials in the struct ucred.
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.
SEE ALSO
recvmsg(2), sendmsg(2), socket(2), socketpair(2), cmsg(3), capabilities(7), socket(7)