Man page - unix(7)
Packages contains this manual
- shm_overview(7)
- nss(5)
- proc_mtrr(5)
- intro(7)
- tcp(7)
- iso_8859-9(7)
- armscii-8(7)
- proc_kpagecount(5)
- initrd(4)
- mouse(4)
- proc_stat(5)
- x25(7)
- proc_interrupts(5)
- fifo(7)
- repertoiremap(5)
- icmp(7)
- futex(7)
- feature_test_macros(7)
- lp(4)
- bpf-helpers(7)
- epoll(7)
- proc_sys_dev(5)
- namespaces(7)
- proc_sysrq-trigger(5)
- proc_bus(5)
- cp1251(7)
- proc_pid_maps(5)
- proc_sys_vm(5)
- proc_pid_projid_map(5)
- st(4)
- proc_pid(5)
- issue(5)
- pid_namespaces(7)
- unicode(7)
- inode(7)
- hosts.equiv(5)
- iso-8859-13(7)
- proc_fb(5)
- proc_modules(5)
- proc_pid_autogroup(5)
- keyrings(7)
- sysvipc(7)
- proc_kmsg(5)
- cgroups(7)
- latin6(7)
- proc_pid_uid_map(5)
- unix(7)
- proc_pid_io(5)
- pts(4)
- packet(7)
- ld-linux.so(8)
- tzselect(8)
- iconv(1)
- proc_pid_syscall(5)
- proc_pid_net(5)
- proc_pid_pagemap(5)
- tty(4)
- proc_profile(5)
- standards(7)
- proc_pid_mounts(5)
- filesystems(5)
- iso-8859-15(7)
- locale(5)
- iso_8859_3(7)
- xattr(7)
- iso-8859-2(7)
- proc_uptime(5)
- persistent-keyring(7)
- credentials(7)
- proc_pid_timers(5)
- utmpx(5)
- vcsa(4)
- proc_pid_exe(5)
- proc_net(5)
- proc_timer_stats(5)
- ip(7)
- proc_pid_fd(5)
- ptmx(4)
- user_namespaces(7)
- resolv.conf(5)
- url(7)
- iso_8859_5(7)
- iso_8859-8(7)
- urn(7)
- process-keyring(7)
- proc_pid_auxv(5)
- proc_ksyms(5)
- proc_ide(5)
- veth(4)
- ldd(1)
- proc_swaps(5)
- landlock(7)
- proc_vmstat(5)
- system_data_types(7)
- cp1252(7)
- lirc(4)
- proc_kpageflags(5)
- random(7)
- precedence(7)
- cpuset(7)
- proc_pid_ns(5)
- acct(5)
- latin4(7)
- proc_pid_cgroup(5)
- proc_cpuinfo(5)
- iso_8859-2(7)
- proc_keys(5)
- charsets(7)
- pldd(1)
- proc_pid_stat(5)
- rtnetlink(7)
- netlink(7)
- ram(4)
- mem(4)
- iso-8859-6(7)
- proc_key-users(5)
- iso_8859_15(7)
- fanotify(7)
- proc_sys_net(5)
- sysfs(5)
- math_error(7)
- latin1(7)
- proc_pid_root(5)
- nptl(7)
- proc_cgroups(5)
- proc_iomem(5)
- proc_pid_statm(5)
- sem_overview(7)
- hier(7)
- full(4)
- proc_pid_status(5)
- proc_pid_cwd(5)
- proc_pid_cpuset(5)
- proc_scsi(5)
- uri(7)
- proc_diskstats(5)
- iso_8859_6(7)
- latin2(7)
- latin5(7)
- man-pages(7)
- ld.so(8)
- uts_namespaces(7)
- proc_pid_mountstats(5)
- intro(3)
- proc_pid_seccomp(5)
- proc_pid_wchan(5)
- attributes(7)
- symlink(7)
- mount_namespaces(7)
- charmap(5)
- tis-620(7)
- iso-8859-10(7)
- getent(1)
- proc_buddyinfo(5)
- ttytype(5)
- rtc(4)
- proc_malloc(5)
- suffixes(7)
- sln(8)
- signal(7)
- proc_sys_abi(5)
- signal-safety(7)
- time_namespaces(7)
- proc_pid_comm(5)
- raw(7)
- gai.conf(5)
- proc_crypto(5)
- locale(1)
- iso-8859-3(7)
- motd(5)
- proc_meminfo(5)
- iso-8859-8(7)
- protocols(5)
- proc_pid_map_files(5)
- pthreads(7)
- null(4)
- proc(5)
- zdump(8)
- socket(7)
- proc_sys_kernel(5)
- ddp(7)
- memusagestat(1)
- hd(4)
- iso-8859-14(7)
- shells(5)
- pipe(7)
- glob(7)
- proc_self(5)
- network_namespaces(7)
- utmp(5)
- proc_kcore(5)
- nsswitch.conf(5)
- sd(4)
- iso-8859-5(7)
- iso_8859_16(7)
- man(7)
- iso_8859-6(7)
- dir_colors(5)
- mq_overview(7)
- vsock(7)
- ascii(7)
- thread-keyring(7)
- fs(5)
- proc_pid_attr(5)
- proc_sys_debug(5)
- proc_sys(5)
- proc_pid_cmdline(5)
- pty(7)
- services(5)
- cgroup_namespaces(7)
- securetty(5)
- netdevice(7)
- iso_8859_13(7)
- host.conf(5)
- proc_pid_setgroups(5)
- proc_slabinfo(5)
- sock_diag(7)
- iso_8859-14(7)
- iso-8859-11(7)
- iso_8859_11(7)
- operator(7)
- regex(7)
- wavelan(4)
- proc_sys_fs(5)
- nologin(5)
- proc_pci(5)
- koi8-r(7)
- erofs(5)
- intro(2)
- utf8(7)
- proc_kallsyms(5)
- proc_sysvipc(5)
- queue(7)
- proc_sys_sunrpc(5)
- intro(5)
- latin8(7)
- mtrace(1)
- ipc_namespaces(7)
- dsp56k(4)
- iso_8859_4(7)
- proc_pid_smaps(5)
- proc_cmdline(5)
- rpc(5)
- proc_tty(5)
- proc_version(5)
- smartpqi(4)
- proc_pid_timerslack_ns(5)
- aio(7)
- session-keyring(7)
- resolver(5)
- slabinfo(5)
- wtmp(5)
- iso_8859_9(7)
- proc_locks(5)
- mailaddr(7)
- proc_pid_oom_score(5)
- kmem(4)
- iconvconfig(8)
- iso_8859-7(7)
- glibc(7)
- hostname(7)
- proc_thread-self(5)
- ipv6(7)
- iso_8859_7(7)
- proc_kpagecgroup(5)
- core(5)
- time(7)
- units(7)
- proc_dma(5)
- loop(4)
- address_families(7)
- zero(4)
- intro(4)
- procfs(5)
- iso_8859-4(7)
- vdso(7)
- tmpfs(5)
- iso-8859-16(7)
- iso_8859_10(7)
- user-session-keyring(7)
- libc(7)
- proc_fs(5)
- koi8-u(7)
- latin3(7)
- proc_tid_children(5)
- proc_pid_limits(5)
- proc_pid_coredump_filter(5)
- iso_8859-15(7)
- arp(7)
- urandom(4)
- iso_8859-10(7)
- hpsa(4)
- proc_pid_environ(5)
- boot(7)
- ftm(7)
- ld-linux(8)
- proc_driver(5)
- loop-control(4)
- iso_8859-16(7)
- proc_filesystems(5)
- tzfile(5)
- sprof(1)
- proc_pid_task(5)
- proc_pid_oom_score_adj(5)
- proc_mounts(5)
- iso-8859-4(7)
- iso_8859-1(7)
- utf-8(7)
- iso_8859-13(7)
- intro(6)
- proc_timer_list(5)
- rtld-audit(7)
- iso_8859-3(7)
- group(5)
- sched(7)
- proc_pid_clear_refs(5)
- hosts(5)
- iso_8859-11(7)
- numa(7)
- iso_8859_2(7)
- locale(7)
- iso-8859-1(7)
- fuse(4)
- proc_tid(5)
- proc_execdomains(5)
- proc_pid_mountinfo(5)
- intro(8)
- iso_8859_8(7)
- proc_loadavg(5)
- proc_pid_oom_adj(5)
- re_format(7)
- iso_8859_14(7)
- zic(8)
- bootparam(7)
- inotify(7)
- posixoptions(7)
- proc_partitions(5)
- iso-8859-9(7)
- proc_pid_mem(5)
- networks(5)
- proc_sys_user(5)
- udp(7)
- proc_zoneinfo(5)
- latin10(7)
- proc_pid_fdinfo(5)
- proc_pid_stack(5)
- memusage(1)
- spufs(7)
- pkeys(7)
- path_resolution(7)
- proc_ioports(5)
- intro(1)
- ldconfig(8)
- msr(4)
- svipc(7)
- port(4)
- proc_pid_personality(5)
- cciss(4)
- latin9(7)
- capabilities(7)
- localedef(1)
- vcs(4)
- iso_8859-5(7)
- elf(5)
- proc_sys_proc(5)
- console_codes(4)
- random(4)
- iso-8859-7(7)
- termcap(5)
- cpuid(4)
- environ(7)
- string_copying(7)
- proc_pid_gid_map(5)
- queue(3)
- termio(7)
- user-keyring(7)
- complex(7)
- latin7(7)
- proc_config.gz(5)
- udplite(7)
- kernel_lockdown(7)
- proc_devices(5)
- proc_apm(5)
- iso_8859_1(7)
- proc_pid_numa_maps(5)
apt-get install manpages
Available languages:
en fr pl ja ru zh_TW zh_CNManual
UNIX
名 前書 式
説 明
ア ド レ ス の フ ォ ー マ ッ ト
パ ス 名 ソ ケ ッ ト
Pathname socket ownership and permissions
抽 象 ソ ケ ッ ト
ソ ケ ッ ト オ プ シ ョ ン
自 動 バ イ ン ド (autobind) 機 能
ソ ケ ッ ト API
補 助 メ ッ セ ー ジ
ioctl
エ ラ ー
バ ー ジ ョ ン
注 意
バ グ
例
出 力 例
プ ロ グ ラ ム の ソ ー ス
関 連 項 目
こ の 文 書 に つ い て
名 前
unix - ロ ー カ ル な プ ロ セ ス 間 通 信 用 の ソ ケ ッ ト
書 式
#include
<sys/socket.h>
#include <sys/un.h>
unix_socket
= socket(AF_UNIX, type, 0);
error
= socketpair(AF_UNIX, type, 0, int
*
sv
);
説 明
AF_UNIX ( AF_LOCAL と も 言 わ れ る ) ソ ケ ッ ト フ ァ ミ リ ー は 、 同 じ マ シ ン 上 で プ ロ セ ス 同 士 が 効 率 的 に 通 信 す る た め に 用 い ら れ る 。 伝 統 的 に 、 UNIX ド メ イ ン ソ ケ ッ ト は 、 名 前 な し に も で き る し 、 (ソ ケ ッ ト 型 で あ る と 印 の つ い た ) フ ァ イ ル シ ス テ ム の パ ス 名 に 結 び 付 け る こ と も で き る 。 さ ら に Linux で は 、 フ ァ イ ル シ ス テ ム に 依 存 し な い 抽 象 名 前 空 間 (abstract namespace) も サ ポ ー ト し て い る 。
Valid socket types in the UNIX domain are: SOCK_STREAM , for a stream-oriented socket; SOCK_DGRAM , for a datagram-oriented socket that preserves message boundaries (as on most UNIX implementations, UNIX domain datagram sockets are always reliable and don’t reorder datagrams); and (since Linux 2.6.4) SOCK_SEQPACKET , for a sequenced-packet socket that is connection-oriented, preserves message boundaries, and delivers messages in the order that they were sent.
UNIX ド メ イ ン ソ ケ ッ ト で は 、 補 助 デ ー タ を 使 っ て フ ァ イ ル デ ィ ス ク リ プ タ ー や プ ロ セ ス の 信 任 状 (credential) を 送 受 信 す る こ と も で き る 。
ア ド レ ス の フ ォ ー マ ッ ト
UNIX ド メ イ ン ソ ケ ッ ト の ア ド レ ス は 以 下 の 構 造 体 で 表 現 さ れ る 。
struct
sockaddr_un {
sa_family_t sun_family; /* AF_UNIX */
char sun_path[108]; /* Pathname */
};
The sun_family field always contains AF_UNIX . On Linux, sun_path is 108 bytes in size; see also NOTES, below.
様 々 な シ ス テ ム コ ー ル (例 え ば bind (2), connect (2), sendto (2)) は 入 力 と し て sockaddr_un 引 数 を 取 る 。 他 の い く つ か の シ ス テ ム コ ー ル (例 え ば getsockname (2), getpeername (2), recvfrom (2), accept (2)) は こ の 型 の 引 数 を 返 す 。
sockaddr_un 構 造 体 で は 3 種 類 の ア ド レ ス が 区 別 さ れ る 。
|
* |
pathname (パ ス 名 ) : bind (2) を 使 っ て 、 UNIX ド メ イ ン ソ ケ ッ ト を 、 ヌ ル 終 端 さ れ た フ ァ イ ル シ ス テ ム 上 の パ ス 名 に 結 び 付 け る こ と が で き る 。 (上 述 の い ず れ か の シ ス テ ム コ ー ル に よ り ) ソ ケ ッ ト の ア ド レ ス が 返 さ れ る 際 、 そ の 長 さ は |
offsetof(struct sockaddr_un, sun_path) + strlen(sun_path) + 1
で あ り 、 sun_path に は ヌ ル 終 端 さ れ た パ ス 名 が 格 納 さ れ る 。 (Linux で は 、 上 記 の offsetof () 式 は sizeof(sa_family_t) の 値 と 同 じ だ が 、 他 の 実 装 で は sun_path の 前 に 他 の フ ィ ー ル ド が 含 ま れ る 場 合 も あ る 。 そ の た め 、 offsetof () 式 を 使 う 方 が よ り 移 植 性 の あ る 方 法 で ア ド レ ス 構 造 体 の サ イ ズ を 知 る こ と が で き る 。 )
パ ス 名 ソ ケ ッ ト の 詳 細 に つ い て は 、 後 で 説 明 す る 。
|
* |
unnamed (名 前 な し ) : bind (2) を 使 っ て パ ス 名 に 結 び 付 け る こ と が で き な い ス ト リ ー ム 型 の ソ ケ ッ ト は 名 前 を 持 た な い 。 同 様 に 、 socketpair (2) で 作 成 さ れ る 2 つ の ソ ケ ッ ト も 名 前 を 持 た な い 。 名 前 な し の ソ ケ ッ ト の ア ド レ ス を 返 す 際 に は 、 そ の 長 さ は sizeof(sa_family_t) で あ り 、 sun_path は 検 査 す べ き で は な い 。 |
||
|
* |
abstract (抽 象 ) : 抽 象 ソ ケ ッ ト ア ド レ ス は 、 sun_path[0] が ヌ ル バ イ ト ('\0') で あ る こ と か ら (パ ス 名 ソ ケ ッ ト か ら ) 区 別 で き る 。 こ の 名 前 空 間 に お け る ソ ケ ッ ト の ア ド レ ス は 、 sun_path の 残 り の バ イ ト の 、 ア ド レ ス 構 造 体 の 指 定 さ れ た 長 さ の 範 囲 で 表 さ れ る (名 前 中 の ヌ ル バ イ ト に は 特 別 な 意 味 は な い )。 こ の 名 前 は フ ァ イ ル シ ス テ ム の パ ス 名 と は 何 の 関 係 も な い 。 抽 象 ソ ケ ッ ト の ア ド レ ス を 返 さ れ る 際 に は 、 返 さ れ る addrlen は sizeof(sa_family_t) よ り 大 き く (つ ま り 2 よ り 大 き く )、 ソ ケ ッ ト の 名 前 は sun_path の 最 初 の (addrlen - sizeof(sa_family_t)) バ イ ト に 格 納 さ れ る 。 |
パ ス 名 ソ ケ ッ ト
ソ ケ ッ ト に パ ス 名 を 結 び つ け る 際 に 、 最 大 限 の 移 植 性 を 持 た せ 、 コ ー デ ィ ン グ を 簡 単 に す る た め の ル ー ル が い く つ か あ る 。
|
* |
sun_path の パ ス 名 は ヌ ル 終 端 す べ き で あ る 。 |
||
|
* |
終 端 の ヌ ル バ イ ト を 含 め た パ ス 名 の 長 さ は sun_path の 大 き さ を 超 え な い よ う に す べ き で あ る 。 |
||
|
* |
sockaddr_un 構 造 体 の 終 わ り を 示 す addrlen 引 数 は 最 低 で も 以 下 の 値 を 持 つ べ き で あ る 。 |
offsetof(struct sockaddr_un, sun_path)+strlen(addr.sun_path)+1
も し く は 、 も っ と 簡 単 に は 、 addrlen に sizeof(struct sockaddr_un) を 指 定 す る こ と も で き る 。
UNIX ド メ イ ン ソ ケ ッ ト ア ド レ ス の 扱 い が 上 記 の ル ー ル に 従 っ て い な い 実 装 も い く つ か あ る 。 (全 部 で は な い が ) い く つ か の 実 装 で は 、 sun_path に 文 字 列 終 端 の NULL が な か っ た 場 合 に 終 端 の NULL が 追 加 さ れ る 。
移 植 性 が あ る ア プ リ ケ ー シ ョ ン を 作 成 す る 際 に は 、 い く つ か の 実 装 で は sun_path は 92 バ イ ト し か な い と い う 点 に も 留 意 し て お く と よ い 。
様 々 な シ ス テ ム コ ー ル ( accept (2), recvfrom (2), getsockname (2), getpeername (2)) が ソ ケ ッ ト ア ド レ ス 構 造 体 を 返 す 。 こ れ ら の シ ス テ ム コ ー ル が UNIX ド メ イ ン ソ ケ ッ ト に 対 し て 呼 ば れ た 際 に は 、 こ れ ら の 呼 び 出 し に 渡 す addrlen 引 数 は 上 記 の 説 明 の よ う に 初 期 化 す べ き で あ る 。 リ タ ー ン 時 に は 、 こ の 引 数 に は ア ド レ ス 構 造 体 の 「 実 際 の 」 サ イ ズ が 設 定 さ れ る 。 呼 び 出 し 側 で は こ の 引 数 で 返 さ れ た 値 を 確 認 す べ き で あ る 。 返 さ れ た 値 が 入 力 値 よ り も 大 き い 場 合 、 sun_path に 終 端 の NULL バ イ ト が 存 在 す る 保 証 は な い (「 バ グ 」 を 参 照 )。
Pathname socket ownership and permissions
In the Linux implementation, pathname sockets honor the permissions of the directory they are in. Creation of a new socket fails if the process does not have write and search (execute) permission on the directory in which the socket is created.
On Linux, connecting to a stream socket object requires write permission on that socket; sending a datagram to a datagram socket likewise requires write permission on that socket. POSIX does not make any statement about the effect of the permissions on a socket file, and on some systems (e.g., older BSDs), the socket permissions are ignored. Portable programs should not rely on this feature for security.
When creating a new socket, the owner and group of the socket file are set according to the usual rules. The socket file has all permissions enabled, other than those that are turned off by the process umask (2).
The owner, group, and permissions of a pathname socket can be changed (using chown (2) and chmod (2)).
抽 象 ソ ケ ッ ト
Socket permissions have no meaning for abstract sockets: the process umask (2) has no effect when binding an abstract socket, and changing the ownership and permissions of the object (via fchown (2) and fchmod (2)) has no effect on the accessibility of the socket.
Abstract sockets automatically disappear when all open references to the socket are closed.
The abstract socket namespace is a nonportable Linux extension.
ソ ケ ッ ト オ プ シ ョ ン
歴
史 的 な 理 由 に
よ り 、 こ れ ら
の オ プ シ ョ ン
は た と え
AF_UNIX
固
有 の オ プ シ ョ
ン で あ っ て も
SOL_SOCKET
型 で 指 定
す る 。 ソ ケ ッ
ト フ ァ ミ リ ー
と し て
SOL_SOCKET
を
指 定 す る と 、
setsockopt
(2) で オ プ シ
ョ ン が 設 定 で
き 、
getsockopt
(2) で 取
得 が で き る 。
SO_PASSCRED
Enabling this socket option causes receipt of the credentials of the sending process in an SCM_CREDENTIALS ancillary message in each subsequently received message. The returned credentials are those specified by the sender using SCM_CREDENTIALS , or a default that includes the sender’s PID, real user ID, and real group ID, if the sender did not specify SCM_CREDENTIALS ancillary data.
こ の オ プ シ ョ ン が セ ッ ト さ れ て い て 、 ま だ ソ ケ ッ ト が 接 続 さ れ て い な い と 、 抽 象 名 前 空 間 に 他 と 重 な ら な い 名 前 が 自 動 的 に 生 成 さ れ る 。
The value given as an argument to setsockopt (2) and returned as the result of getsockopt (2) is an integer boolean flag.
SO_PASSSEC
Enables receiving of the SELinux security label of the peer socket in an ancillary message of type SCM_SECURITY (see below).
The value given as an argument to setsockopt (2) and returned as the result of getsockopt (2) is an integer boolean flag.
The SO_PASSSEC option is supported for UNIX domain datagram sockets since Linux 2.6.18; support for UNIX domain stream sockets was added in Linux 4.2.
SO_PEEK_OFF
socket (7) を 参 照 。
SO_PEERCRED
This read-only socket option returns the credentials of the peer process connected to this socket. The returned credentials are those that were in effect at the time of the call to connect (2) or socketpair (2).
The argument to getsockopt (2) is a pointer to a ucred structure; define the _GNU_SOURCE feature test macro to obtain the definition of that structure from <sys/socket.h> .
The use of this option is possible only for connected AF_UNIX stream sockets and for AF_UNIX stream and datagram socket pairs created using socketpair (2).
SO_PEERSEC
This read-only socket option returns the security context of the peer socket connected to this socket. By default, this will be the same as the security context of the process that created the peer socket unless overridden by the policy or by a process with the required permissions.
The argument to getsockopt (2) is a pointer to a buffer of the specified length in bytes into which the security context string will be copied. If the buffer length is less than the length of the security context string, then getsockopt (2) returns -1, sets errno to ERANGE , and returns the required length via optlen . The caller should allocate at least NAME_MAX bytes for the buffer initially, although this is not guaranteed to be sufficient. Resizing the buffer to the returned length and retrying may be necessary.
The security context string may include a terminating null character in the returned length, but is not guaranteed to do so: a security context "foo" might be represented as either {’f’,’o’,’o’} of length 3 or {’f’,’o’,’o’,’\0’} of length 4, which are considered to be interchangeable. The string is printable, does not contain non-terminating null characters, and is in an unspecified encoding (in particular, it is not guaranteed to be ASCII or UTF-8).
The use of this option for sockets in the AF_UNIX address family is supported since Linux 2.6.2 for connected stream sockets, and since Linux 4.18 also for stream and datagram socket pairs created using socketpair (2).
自 動 バ イ ン ド (autobind) 機 能
bind (2) 呼 び 出 し で sizeof(sa_family_t) と し て addrlen を 指 定 す る か 、 ア ド レ ス に 明 示 的 に バ イ ン ド さ れ て い な い ソ ケ ッ ト に 対 し て SO_PASSCRED ソ ケ ッ ト オ プ シ ョ ン が 指 定 さ れ て い た 場 合 、 そ の ソ ケ ッ ト は 抽 象 ア ド レ ス に 自 動 的 に バ イ ン ド さ れ る 。 こ の ア ド レ ス は 、 1 個 の ヌ ル バ イ ト の 後 に 、 文 字 集 合 [0-9a-f] の バ イ ト が 5 個 続 く 形 式 で あ る 。 し た が っ て 、 自 動 的 に バ イ ン ド さ れ る ア ド レ ス 数 に は 2ˆ20 個 と い う 上 限 が 存 在 す る 。 (Linux 2.1.15 以 降 で 、 自 動 バ イ ン ド 機 能 が 追 加 さ れ た と き に は 、 8 バ イ ト が 使 わ れ て お り 、 自 動 バ イ ン ド ア ド レ ス 数 の 上 限 は 2ˆ32 で あ っ た 。 Linux 2.3.15 で 5 バ イ ト に 変 更 さ れ た 。 )
ソ ケ ッ ト API
こ の 節 で は 、 Linux の UNIX ド メ イ ン ソ ケ ッ ト で の 、 ド メ イ ン 固 有 の 詳 細 仕 様 と ソ ケ ッ ト API で サ ポ ー ト さ れ て い な い 機 能 に つ い て 説 明 す る 。
UNIX ド メ イ ン ソ ケ ッ ト で は 、 帯 域 外 デ ー タ (out-of-band data) の 送 信 ( send (2) と recv (2) の MSG_OOB フ ラ グ ) は サ ポ ー ト さ れ て い な い 。
send (2) MSG_MORE フ ラ グ は UNIX ド メ イ ン ソ ケ ッ ト で は サ ポ ー ト さ れ て い な い 。
Linux 3.4 よ り 前 で は 、 recv (2) の flags 引 数 で の MSG_TRUNC の 使 用 は UNIX ド メ イ ン ソ ケ ッ ト で は サ ポ ー ト さ れ て い な か っ た 。
SO_SNDBUF ソ ケ ッ ト オ プ シ ョ ン は UNIX ド メ イ ン ソ ケ ッ ト で 効 果 を 持 つ が 、 SO_RCVBUF は 効 果 が な い 。 デ ー タ グ ラ ム ソ ケ ッ ト で は 、 SO_SNDBUF の 値 が 出 力 デ ー タ グ ラ ム の 上 限 サ イ ズ と な る 。 実 際 の 上 限 値 は 、 SO_SNDBUF オ プ シ ョ ン と し て 設 定 さ れ た 値 の 2倍 ( socket (7) 参 照 ) か ら オ ー バ ヘ ッ ド と し て 使 用 さ れ る 32 バ イ ト を 引 い た 値 と な る 。
補 助 メ ッ セ ー ジ
補
助 デ ー タ を 送
受 す る に は 、
sendmsg
(2) や
recvmsg
(2) を
使 用 す る 。 歴
史 的 な 理 由 に
よ り 、 以 下 に
示 す 補 助 メ ッ
セ ー ジ の 型 は
た と え
AF_UNIX
固 有
の も の で あ っ
て も
SOL_SOCKET
型 で
指 定 す る 。 こ
れ ら を 送 る に
は 、 構 造 体
cmsghdr
の
cmsg_level
フ ィ ー
ル ド に
SOL_SOCKET
を
セ ッ ト し 、
cmsg_type
フ ィ ー ル
ド に タ イ プ を
セ ッ ト す る 。
詳 細 は
cmsg
(3) を 見
よ 。
SCM_RIGHTS
他 の プ ロ セ ス で オ ー プ ン さ れ た フ ァ イ ル デ ィ ス ク リ プ タ ー の セ ッ ト を 送 受 信 す る 。 デ ー タ 部 分 に フ ァ イ ル デ ィ ス ク リ プ タ ー の 整 数 配 列 が 入 っ て い る 。
Commonly, this operation is referred to as "passing a file descriptor" to another process. However, more accurately, what is being passed is a reference to an open file description (see open (2)), and in the receiving process it is likely that a different file descriptor number will be used. Semantically, this operation is equivalent to duplicating ( dup (2)) a file descriptor into the file descriptor table of another process.
If the buffer used to receive the ancillary data containing file descriptors is too small (or is absent), then the ancillary data is truncated (or discarded) and the excess file descriptors are automatically closed in the receiving process.
If the number of file descriptors received in the ancillary data would cause the process to exceed its RLIMIT_NOFILE resource limit (see getrlimit (2)), the excess file descriptors are automatically closed in the receiving process.
The kernel constant SCM_MAX_FD defines a limit on the number of file descriptors in the array. Attempting to send an array larger than this limit causes sendmsg (2) to fail with the error EINVAL . SCM_MAX_FD has the value 253 (or 255 in kernels before 2.6.38).
SCM_CREDENTIALS
UNIX 信 任 状 を 送 受 信 す る 。 こ れ は 認 証 に 用 い る こ と が で き る 。 信 任 状 は struct ucred の 補 助 メ ッ セ ー ジ と し て 渡 さ れ る 。 こ の 構 造 体 は <sys/socket.h> で 以 下 の よ う に 定 義 さ れ て い る 。
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 */
};
glibc 2.8 以 降 で は 、 こ の 構 造 体 の 定 義 を 得 る た め に は ( ど の ヘ ッ ダ ー フ ァ イ ル を イ ン ク ル ー ド す る よ り も 前 に ) 機 能 検 査 マ ク ロ _GNU_SOURCE を 定 義 し な け れ ば な ら な い 。
The credentials which the sender specifies are checked by the kernel. A privileged process is allowed to specify values that do not match its own. The sender must specify its own process ID (unless it has the capability CAP_SYS_ADMIN , in which case the PID of any existing process may be specified), its real user ID, effective user ID, or saved set-user-ID (unless it has CAP_SETUID ), and its real group ID, effective group ID, or saved set-group-ID (unless it has CAP_SETGID ).
To receive a struct ucred message, the SO_PASSCRED option must be enabled on the socket.
SCM_SECURITY
Receive the SELinux security context (the security label) of the peer socket. The received ancillary data is a null-terminated string containing the security context. The receiver should allocate at least NAME_MAX bytes in the data portion of the ancillary message for this data.
To receive the security context, the SO_PASSSEC option must be enabled on the socket (see above).
When sending ancillary data with sendmsg (2), only one item of each of the above types may be included in the sent message.
At least one byte of real data should be sent when sending ancillary data. On Linux, this is required to successfully send ancillary data over a UNIX domain stream socket. When sending ancillary data over a UNIX domain datagram socket, it is not necessary on Linux to send any accompanying real data. However, portable applications should also include at least one byte of real data when sending ancillary data over a datagram socket.
When receiving from a stream socket, ancillary data forms a kind of barrier for the received data. For example, suppose that the sender transmits as follows:
|
1. |
sendmsg (2) of four bytes, with no ancillary data. |
|||
|
2. |
sendmsg (2) of one byte, with ancillary data. |
|||
|
3. |
sendmsg (2) of four bytes, with no ancillary data. |
Suppose that the receiver now performs recvmsg (2) calls each with a buffer size of 20 bytes. The first call will receive five bytes of data, along with the ancillary data sent by the second sendmsg (2) call. The next call will receive the remaining four bytes of data.
If the space allocated for receiving incoming ancillary data is too small then the ancillary data is truncated to the number of headers that will fit in the supplied buffer (or, in the case of an SCM_RIGHTS file descriptor list, the list of file descriptors may be truncated). If no buffer is provided for incoming ancillary data (i.e., the msg_control field of the msghdr structure supplied to recvmsg (2) is NULL), then the incoming ancillary data is discarded. In both of these cases, the MSG_CTRUNC flag will be set in the msg.msg_flags value returned by recvmsg (2).
ioctl
以 下 の ioctl (2) 呼 び 出 し は value に 情 報 を 入 れ て 返 す 。 正 し い 書 式 は 以 下 の 通 り 。
int
value
;
error
= ioctl(
unix_socket
,
ioctl_type
, &
value
);
ioctl_type
に は 以 下 を 指
定 で き る :
SIOCINQ
For SOCK_STREAM sockets, this call returns the number of unread bytes in the receive buffer. The socket must not be in LISTEN state, otherwise an error ( EINVAL ) is returned. SIOCINQ is defined in <linux/sockios.h> . Alternatively, you can use the synonymous FIONREAD , defined in <sys/ioctl.h> . For SOCK_DGRAM sockets, the returned value is the same as for Internet domain datagram sockets; see udp (7).
エ ラ ー
EADDRINUSE
指 定 し た ロ ー カ ル ア ド レ ス が 既 に 使 用 さ れ て い る か 、 フ ァ イ ル シ ス テ ム の ソ ケ ッ ト オ ブ ジ ェ ク ト が 既 に 存 在 し て い る 。
|
EBADF |
This error can occur for sendmsg (2) when sending a file descriptor as ancillary data over a UNIX domain socket (see the description of SCM_RIGHTS , above), and indicates that the file descriptor number that is being sent is not valid (e.g., it is not an open file descriptor). |
ECONNREFUSED
connect (2) に よ り 指 定 さ れ た リ モ ー ト ア ド レ ス が 接 続 待 ち ソ ケ ッ ト で は な か っ た 。 こ の エ ラ ー は タ ー ゲ ッ ト の パ ス 名 が ソ ケ ッ ト で な か っ た 場 合 に も 発 生 す る 。
ECONNRESET
リ モ ー ト ソ ケ ッ ト が 予 期 し な い か た ち で ク ロ ー ズ さ れ た 。
|
EFAULT |
ユ ー ザ ー メ モ リ ー ア ド レ ス が 不 正 。 |
||
|
EINVAL |
渡 し た 引 数 が 不 正 。 よ く あ る 原 因 と し て は 、 渡 し た ア ド レ ス の sun_type フ ィ ー ル ド に AF_UNIX が 指 定 さ れ て い な か っ た 、 行 お う と し た 操 作 に 対 し て ソ ケ ッ ト が 有 効 な 状 態 で は な か っ た 、 な ど 。 |
EISCONN
既 に 接 続 さ れ て い る ソ ケ ッ ト に 対 し て connect (2) が 呼 ば れ た 。 ま た は 、 指 定 し た タ ー ゲ ッ ト ア ド レ ス が 既 に 接 続 済 み の ソ ケ ッ ト だ っ た 。
|
ENOENT |
connect (2) に 指 定 さ れ た リ モ ー ト ア ド レ ス の パ ス 名 が 存 在 し な か っ た 。 |
|||
|
ENOMEM |
メ モ リ ー が 足 り な い 。 |
ENOTCONN
ソ ケ ッ ト 操 作 に タ ー ゲ ッ ト ア ド レ ス が 必 要 だ が 、 こ の ソ ケ ッ ト は 接 続 さ れ て い な い 。
EOPNOTSUPP
ス ト リ ー ム 指 向 で な い ソ ケ ッ ト に 対 し て ス ト リ ー ム 操 作 が 呼 び 出 さ れ た 。 ま た は 帯 域 外 デ ー タ オ プ シ ョ ン を 用 い よ う と し た 。
|
EPERM |
送 信 者 が struct ucred に 不 正 な 信 任 状 を 渡 し た 。 |
|||
|
EPIPE |
リ モ ー ト ソ ケ ッ ト が ス ト リ ー ム ソ ケ ッ ト 上 で ク ロ ー ズ さ れ た 。 可 能 な 場 合 は SIGPIPE も 同 時 に 送 ら れ る 。 こ れ を 避 け る に は MSG_NOSIGNAL フ ラ グ を send (2) や sendmsg (2) に 渡 す 。 |
EPROTONOSUPPORT
渡 さ れ た プ ロ ト コ ル が AF_UNIX で な い 。
EPROTOTYPE
リ モ ー ト ソ ケ ッ ト と ロ ー カ ル ソ ケ ッ ト の タ イ プ が 一 致 し て い な か っ た ( SOCK_DGRAM と SOCK_STREAM )。
ESOCKTNOSUPPORT
未 知 の ソ ケ ッ ト タ イ プ 。
|
ESRCH |
While sending an ancillary message containing credentials ( SCM_CREDENTIALS ), the caller specified a PID that does not match any existing process. |
ETOOMANYREFS
This error can occur for sendmsg (2) when sending a file descriptor as ancillary data over a UNIX domain socket (see the description of SCM_RIGHTS , above). It occurs if the number of "in-flight" file descriptors exceeds the RLIMIT_NOFILE resource limit and the caller does not have the CAP_SYS_RESOURCE capability. An in-flight file descriptor is one that has been sent using sendmsg (2) but has not yet been accepted in the recipient process using recvmsg (2).
This error is diagnosed since mainline Linux 4.5 (and in some earlier kernel versions where the fix has been backported). In earlier kernel versions, it was possible to place an unlimited number of file descriptors in flight, by sending each file descriptor with sendmsg (2) and then closing the file descriptor so that it was not accounted against the RLIMIT_NOFILE resource limit.
他 に も 汎 用 の ソ ケ ッ ト 層 で エ ラ ー が 起 こ っ た り 、 フ ァ イ ル シ ス テ ム 上 に ソ ケ ッ ト オ ブ ジ ェ ク ト を 作 ろ う と し た 場 合 に フ ァ イ ル シ ス テ ム の エ ラ ー が 起 こ る こ と が あ る 。 そ れ ぞ れ の 詳 細 は 適 切 な man ペ ー ジ を 参 照 す る こ と 。
バ ー ジ ョ ン
SCM_CREDENTIALS と 抽 象 名 前 空 間 は 、 Linux 2.2 で 導 入 さ れ た 。 移 植 性 が 必 要 な プ ロ グ ラ ム で は 使 う べ き で は な い 。 (BSD 由 来 の シ ス テ ム の 中 に も 信 任 状 の 送 受 信 を サ ポ ー ト し て い る も の が あ る が 、 そ の 実 装 の 詳 細 は シ ス テ ム に よ っ て 異 な る )
注 意
フ ァ イ ル 名 を 指 定 し て ソ ケ ッ ト に バ イ ン ド す る と 、 フ ァ イ ル シ ス テ ム に ソ ケ ッ ト が 生 成 さ れ る 。 こ れ は 必 要 な く な っ た と き に 呼 び だ し た ユ ー ザ ー が 削 除 し な け れ ば な ら な い ( unlink (2) を 用 い る )。 UNIX で 通 常 使 わ れ る 「 背 後 で 閉 じ る 方 式 」 が 適 用 さ れ る 。 ソ ケ ッ ト は い つ で も unlink す る こ と が で き 、 最 後 の 参 照 が ク ロ ー ズ さ れ た と き に フ ァ イ ル シ ス テ ム か ら 削 除 さ れ る 。
SOCK_STREAM ソ ケ ッ ト 上 で フ ァ イ ル デ ィ ス ク リ プ タ ー や 信 任 状 を 渡 す た め に は 、 同 じ sendmsg (2) や recvmsg (2) コ ー ル で 補 助 デ ー タ 以 外 の デ ー タ を 少 な く と も 1 バ イ ト 送 信 /受 信 し な け れ ば な ら な い 。
UNIX ド メ イ ン の ス ト リ ー ム ソ ケ ッ ト で は 、 帯 域 外 デ ー タ の 概 念 は サ ポ ー ト さ れ な い 。
バ グ
ソ ケ ッ ト を ア ド レ ス に 結 び つ け る 際 、 Linux は 終 端 の NULL が sun_path に な か っ た 場 合 に 追 加 す る 実 装 の 一 つ で あ る 。 ほ と ん ど の 場 合 、 こ れ は 問 題 に な ら な い 。 ソ ケ ッ ト ア ド レ ス が 取 得 さ れ た 際 、 ソ ケ ッ ト を バ イ ン ド し た と き に 指 定 し た も の よ り 1 バ イ ト 長 く な る だ け で あ る 。 し か し な が ら 、 紛 ら わ し い 動 作 が 起 こ る 場 合 が 一 つ あ る 。 ソ ケ ッ ト を バ イ ン ド し た 際 に 108 個 の NULL で な い バ イ ト を 指 定 し た 場 合 、 終 端 の NULL が 追 加 さ れ る と パ ス 名 の 長 さ が sizeof(sun_path) を 超 え て し ま う 。 結 果 と し て 、 (例 え ば accept (2) で ) ソ ケ ッ ト ア ド レ ス を 取 得 し た 際 に 、 値 を 取 得 す る 呼 び 出 し の 入 力 の address 引 数 に sizeof(struct sockaddr_un) を 指 定 し た と す る と 、 返 さ れ る ア ド レ ス 構 造 体 は sun_path に 終 端 の NULL を 「 含 ま な い 」 こ と に な る 。
さ ら に 、 い く つ か の 実 装 で は 、 ソ ケ ッ ト を バ イ ン ド す る 際 に 終 端 の NULL が 必 要 で は な く ( addrlen 引 数 を 使 っ て sun_path の 長 さ が 判 定 さ れ る )、 こ の よ う な 実 装 で ソ ケ ッ ト ア ド レ ス を 取 得 す る 際 に は 、 sun_path に 終 端 の NULL は 存 在 し な い 。
ソ ケ ッ ト ア ド レ ス を 取 得 す る ア プ リ ケ ー シ ョ ン で は 、 sun_path に 終 端 の NULL が 存 在 し な い と い う 移 植 性 の 問 題 を 、 パ ス 名 の 有 効 な バ イ ト 数 が 以 下 の よ う に な る と 事 実 を 考 慮 す る こ と で 取 り 扱 う こ と が で き る 。
strnlen(addr.sun_path, addrlen - offsetof(sockaddr_un, sun_path))
他 の 方 法 と し て は 、 ア プ リ ケ ー シ ョ ン が ソ ケ ッ ト ア ド レ ス を 取 得 す る 際 、 取 得 の 呼 び 出 し を 行 う 前 に 、 大 き さ が sizeof(struct sockaddr_un)+1 の バ ッ フ ァ ー を 割 り 当 て る こ と も で き る 。 取 得 の 呼 び 出 し で は addrlen に sizeof(struct sockaddr_un) を 指 定 す る と 、 余 分 な 一 つ の 0 バ イ ト に よ り sun_path で 返 さ れ る 文 字 列 に 終 端 の NULL が 含 ま れ る こ と が 保 証 さ れ る 。
void *addrp;
addrlen =
sizeof(struct sockaddr_un);
addrp = malloc(addrlen + 1);
if (addrp == NULL)
/* Handle error */ ;
memset(addrp, 0, addrlen + 1);
if
(getsockname(sfd, (struct sockaddr *) addrp, &addrlen))
== -1)
/* handle error */ ;
printf("sun_path = %s\n", ((struct sockaddr_un *) addrp)->sun_path);
ア プ リ ケ ー シ ョ ン が 「 パ ス 名 ソ ケ ッ ト 」 の 節 で 説 明 し た ル ー ル に し た が っ て パ ス 名 を 「 作 成 」 し て い れ ば 、 こ の よ う な 分 か り に く さ は 避 け る こ と が で き る 。
例
The following code demonstrates the use of sequenced-packet sockets for local interprocess communication. It consists of two programs. The server program waits for a connection from the client program. The client sends each of its command-line arguments in separate messages. The server treats the incoming messages as integers and adds them up. The client sends the command string "END". The server sends back a message containing the sum of the client’s integers. The client prints the sum and exits. The server waits for the next client to connect. To stop the server, the client is called with the command-line argument "DOWN".
The following output was recorded while running the server in the background and repeatedly executing the client. Execution of the server program ends when it receives the "DOWN" command.
出 力 例
$
./server
&
[1] 25887
$
./client 3 4
Result = 7
$
./client 11 -5
Result = 6
$
./client DOWN
Result = 0
[1]+ Done ./server
$
プ ロ グ ラ ム の ソ ー ス
/*
* File connection.h
*/
#define
SOCKET_NAME "/tmp/9Lq7BNBnBycd6nxy.socket"
#define BUFFER_SIZE 12
/*
* File server.c
*/
#include
<stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include "connection.h"
int
main(int argc, char *argv[])
{
struct sockaddr_un name;
int down_flag = 0;
int ret;
int connection_socket;
int data_socket;
int result;
char buffer[BUFFER_SIZE];
/* Create local socket. */
connection_socket
= socket(AF_UNIX, SOCK_SEQPACKET, 0);
if (connection_socket == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
/*
* For portability clear the whole structure, since some
* implementations have additional (nonstandard) fields in
* the structure.
*/
memset(&name, 0, sizeof(name));
/* Bind socket to socket name. */
name.sun_family
= AF_UNIX;
strncpy(name.sun_path, SOCKET_NAME, sizeof(name.sun_path) -
1);
ret =
bind(connection_socket, (const struct sockaddr *) &name,
sizeof(name));
if (ret == -1) {
perror("bind");
exit(EXIT_FAILURE);
}
/*
* Prepare for accepting connections. The backlog size is set
* to 20. So while one request is being processed other
requests
* can be waiting.
*/
ret =
listen(connection_socket, 20);
if (ret == -1) {
perror("listen");
exit(EXIT_FAILURE);
}
/* This is the main loop for handling connections. */
for (;;) {
/* Wait for incoming connection. */
data_socket =
accept(connection_socket, NULL, NULL);
if (data_socket == -1) {
perror("accept");
exit(EXIT_FAILURE);
}
result = 0;
for (;;) {
/* Wait for next data packet. */
ret =
read(data_socket, buffer, sizeof(buffer));
if (ret == -1) {
perror("read");
exit(EXIT_FAILURE);
}
/* Ensure buffer is 0-terminated. */
buffer[sizeof(buffer) - 1] = 0;
/* Handle commands. */
if
(!strncmp(buffer, "DOWN", sizeof(buffer))) {
down_flag = 1;
break;
}
if
(!strncmp(buffer, "END", sizeof(buffer))) {
break;
}
/* Add received summand. */
result +=
atoi(buffer);
}
/* Send result. */
sprintf(buffer,
"%d", result);
ret = write(data_socket, buffer, sizeof(buffer));
if (ret == -1) {
perror("write");
exit(EXIT_FAILURE);
}
/* Close socket. */
close(data_socket);
/* Quit on DOWN command. */
if (down_flag) {
break;
}
}
close(connection_socket);
/* Unlink the socket. */
unlink(SOCKET_NAME);
exit(EXIT_SUCCESS);
}
/*
* File client.c
*/
#include
<errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include "connection.h"
int
main(int argc, char *argv[])
{
struct sockaddr_un addr;
int ret;
int data_socket;
char buffer[BUFFER_SIZE];
/* Create local socket. */
data_socket =
socket(AF_UNIX, SOCK_SEQPACKET, 0);
if (data_socket == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
/*
* For portability clear the whole structure, since some
* implementations have additional (nonstandard) fields in
* the structure.
*/
memset(&addr, 0, sizeof(addr));
/* Connect socket to socket address */
addr.sun_family
= AF_UNIX;
strncpy(addr.sun_path, SOCKET_NAME, sizeof(addr.sun_path) -
1);
ret =
connect(data_socket, (const struct sockaddr *) &addr,
sizeof(addr));
if (ret == -1) {
fprintf(stderr, "The server is down.\n");
exit(EXIT_FAILURE);
}
/* Send arguments. */
for (int i = 1;
i < argc; ++i) {
ret = write(data_socket, argv[i], strlen(argv[i]) + 1);
if (ret == -1) {
perror("write");
break;
}
}
/* Request result. */
strcpy(buffer,
"END");
ret = write(data_socket, buffer, strlen(buffer) + 1);
if (ret == -1) {
perror("write");
exit(EXIT_FAILURE);
}
/* Receive result. */
ret =
read(data_socket, buffer, sizeof(buffer));
if (ret == -1) {
perror("read");
exit(EXIT_FAILURE);
}
/* Ensure buffer is 0-terminated. */
buffer[sizeof(buffer) - 1] = 0;
printf("Result = %s\n", buffer);
/* Close socket. */
close(data_socket);
exit(EXIT_SUCCESS);
}
SCM_RIGHTS の 使 用 例 に つ い て は cmsg (3) を 参 照 。
関 連 項 目
recvmsg (2), sendmsg (2), socket (2), socketpair (2), cmsg (3), capabilities (7), credentials (7), socket (7), udp (7)
こ の 文 書 に つ い て
こ の man ペ ー ジ は Linux man-pages プ ロ ジ ェ ク ト の リ リ ー ス 5.10 の 一 部 で あ る 。 プ ロ ジ ェ ク ト の 説 明 と バ グ 報 告 に 関 す る 情 報 は https://www.kernel.org/doc/man-pages/ に 書 か れ て い る 。