1.\" $NetBSD: unix.4,v 1.10 2002/02/13 08:17:50 ross Exp $ 2.\" 3.\" Copyright (c) 1991, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. All advertising materials mentioning features or use of this software 15.\" must display the following acknowledgement: 16.\" This product includes software developed by the University of 17.\" California, Berkeley and its contributors. 18.\" 4. Neither the name of the University nor the names of its contributors 19.\" may be used to endorse or promote products derived from this software 20.\" without specific prior written permission. 21.\" 22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32.\" SUCH DAMAGE. 33.\" 34.\" @(#)unix.4 8.1 (Berkeley) 6/9/93 35.\" 36.Dd January 7, 1998 37.Dt UNIX 4 38.Os 39.Sh NAME 40.Nm unix 41.Nd UNIX-domain protocol family 42.Sh SYNOPSIS 43.Fd #include \*[Lt]sys/types.h\*[Gt] 44.Fd #include \*[Lt]sys/un.h\*[Gt] 45.Sh DESCRIPTION 46The 47.Tn UNIX Ns -domain 48protocol family is a collection of protocols 49that provides local (on-machine) interprocess 50communication through the normal 51.Xr socket 2 52mechanisms. 53The 54.Tn UNIX Ns -domain 55family supports the 56.Dv SOCK_STREAM 57and 58.Dv SOCK_DGRAM 59socket types and uses 60filesystem pathnames for addressing. 61.Sh ADDRESSING 62.Tn UNIX Ns -domain 63addresses are variable-length filesystem pathnames of 64at most 104 characters. 65The include file 66.Aq Pa sys/un.h 67defines this address: 68.Bd -literal -offset indent 69struct sockaddr_un { 70 u_char sun_len; 71 u_char sun_family; 72 char sun_path[104]; 73}; 74.Ed 75.Pp 76Binding a name to a 77.Tn UNIX Ns -domain 78socket with 79.Xr bind 2 80causes a socket file to be created in the filesystem. 81This file is 82.Em not 83removed when the socket is closed\(em\c 84.Xr unlink 2 85must be used to remove the file. 86.Pp 87The length of 88.Tn UNIX Ns -domain 89address, required by 90.Xr bind 2 91and 92.Xr connect 2 , 93can be calculated by the macro 94.Fn SUN_LEN 95defined in 96.Aq Pa sys/un.h . 97The 98.Ar sun_path 99field must be terminated by a NUL character to be used with 100.Fn SUN_LEN , 101but the terminating NUL is 102.Em not 103part of the address. 104.Pp 105The 106.Tn UNIX Ns -domain 107protocol family does not support broadcast addressing or any form 108of 109.Dq wildcard 110matching on incoming messages. 111All addresses are absolute- or relative-pathnames 112of other 113.Tn UNIX Ns -domain 114sockets. 115Normal filesystem access-control mechanisms are also 116applied when referencing pathnames; e.g., the destination 117of a 118.Xr connect 2 119or 120.Xr sendto 2 121must be writable. 122.Sh PROTOCOLS 123The 124.Tn UNIX Ns -domain 125protocol family comprises simple 126transport protocols that support the 127.Dv SOCK_STREAM 128and 129.Dv SOCK_DGRAM 130abstractions. 131.Dv SOCK_STREAM 132sockets also support the communication of 133.Ux 134file descriptors through the use of the 135.Ar msg_control 136field in the 137.Ar msg 138argument to 139.Xr sendmsg 2 140and 141.Xr recvmsg 2 . 142.Pp 143Any valid descriptor may be sent in a message. 144The file descriptor(s) to be passed are described using a 145.Ar struct cmsghdr 146that is defined in the include file 147.Aq Pa sys/socket.h . 148The type of the message is 149.Dv SCM_RIGHTS , 150and the data portion of the messages is an array of integers 151representing the file descriptors to be passed. 152The number of descriptors being passed is defined 153by the length field of the message; 154the length field is the sum of the size of the header 155plus the size of the array of file descriptors. 156.Pp 157The received descriptor is a 158.Em duplicate 159of the sender's descriptor, as if it were created with a call to 160.Xr dup 2 . 161Per-process descriptor flags, set with 162.Xr fcntl 2 , 163are 164.Em not 165passed to a receiver. 166Descriptors that are awaiting delivery, or that are 167purposely not received, are automatically closed by the system 168when the destination socket is closed. 169.Pp 170There is one 171.Tn socket-level 172.Xr setsockopt 2 / Ns Xr getsockopt 2 173option available in the 174.Nm 175domain. 176The 177.Dv LOCAL_CREDS 178option may be enabled on a 179.Dv SOCK_DGRAM 180or a 181.Dv SOCK_STREAM 182socket. This option provides a mechanism for the receiver to 183receive the credentials of the process as a 184.Xr recvmsg 2 185control message. The msg_control field in the msghdr structure points 186to a buffer that contains a cmsghdr structure followed by a variable 187length sockcred structure, defined in 188.Pa \*[Lt]sys/socket.h\*[Gt] 189as follows: 190.Bd -literal 191struct sockcred { 192 uid_t sc_uid; /* real user id */ 193 uid_t sc_euid; /* effective user id */ 194 gid_t sc_gid; /* real group id */ 195 gid_t sc_egid; /* effective group id */ 196 int sc_ngroups; /* number of supplemental groups */ 197 gid_t sc_groups[1]; /* variable length */ 198}; 199.Ed 200.Pp 201The 202.Fn SOCKCREDSIZE 203macro computes the size of the sockcred structure for a specified number 204of groups. 205The cmsghdr fields have the following values: 206.Bd -literal 207cmsg_len = sizeof(struct cmsghdr) + SOCKCREDSIZE(ngroups) 208cmsg_level = SOL_SOCKET 209cmsg_type = SCM_CREDS 210.Ed 211.Sh SEE ALSO 212.Xr socket 2 , 213.Xr intro 4 214.Rs 215.%T "An Introductory 4.3 BSD Interprocess Communication Tutorial" 216.%B PS1 217.%N 7 218.Re 219.Rs 220.%T "An Advanced 4.3 BSD Interprocess Communication Tutorial" 221.%B PS1 222.%N 8 223.Re 224