1.\"	$NetBSD: unix.4,v 1.35 2025/03/30 00:14:12 riastradh 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. Neither the name of the University nor the names of its contributors
15.\"    may be used to endorse or promote products derived from this software
16.\"    without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\"     @(#)unix.4	8.1 (Berkeley) 6/9/93
31.\"
32.Dd June 28, 2022
33.Dt UNIX 4
34.Os
35.Sh NAME
36.Nm unix
37.Nd UNIX-domain protocol family
38.Sh SYNOPSIS
39.In sys/types.h
40.In sys/un.h
41.Sh DESCRIPTION
42The
43.Tn UNIX Ns -domain
44protocol family is a collection of protocols
45that provides local (on-machine) interprocess
46communication through the normal
47.Xr socket 2
48mechanisms.
49The
50.Tn UNIX Ns -domain
51family supports the
52.Dv SOCK_STREAM ,
53.Dv SOCK_SEQPACKET ,
54and
55.Dv SOCK_DGRAM
56socket types and uses
57filesystem pathnames for addressing.
58.Sh ADDRESSING
59.Tn UNIX Ns -domain
60addresses are variable-length filesystem pathnames of
61at most 104 characters.
62The include file
63.In sys/un.h
64defines this address:
65.Bd -literal -offset indent
66struct sockaddr_un {
67        u_char  sun_len;
68        u_char  sun_family;
69        char    sun_path[104];
70};
71.Ed
72.Pp
73Binding a name to a
74.Tn UNIX Ns -domain
75socket with
76.Xr bind 2
77causes a socket file to be created in the filesystem.
78This file is
79.Em not
80removed when the socket is closed;
81.Xr unlink 2
82must be used to remove the file.
83.Pp
84The length of
85.Tn UNIX Ns -domain
86address, required by
87.Xr bind 2
88and
89.Xr connect 2 ,
90can be calculated by the macro
91.Fn SUN_LEN
92defined in
93.In sys/un.h .
94The
95.Fa sun_path
96field must be terminated by a NUL character to be used with
97.Fn SUN_LEN ,
98but the terminating NUL is
99.Em not
100part of the address.
101The
102.Nx
103kernel ignores any user-set value in the
104.Fa sun_len
105member of the structure.
106.Pp
107The
108.Tn UNIX Ns -domain
109protocol family does not support broadcast addressing or any form
110of
111.Dq wildcard
112matching on incoming messages.
113All addresses are absolute- or relative-pathnames
114of other
115.Tn UNIX Ns -domain
116sockets.
117Normal filesystem access-control mechanisms are also
118applied when referencing pathnames; e.g., the destination
119of a
120.Xr connect 2
121or
122.Xr sendto 2
123must be writable.
124.Sh PROTOCOLS
125The
126.Tn UNIX Ns -domain
127protocol family comprises simple
128transport protocols that support the
129.Dv SOCK_STREAM ,
130.Dv SOCK_SEQPACKET ,
131and
132.Dv SOCK_DGRAM
133abstractions.
134.Pp
135.Dv SOCK_STREAM
136and
137.Dv SOCK_SEQPACKET
138sockets also support the communication of
139.Ux
140file descriptors through the use of the
141.Fa msg_control
142field in the
143.Fa msg
144argument to
145.Xr sendmsg 2
146and
147.Xr recvmsg 2 .
148Supported file descriptors may be sent in control messages of type
149.Dv SCM_RIGHTS
150holding an array of
151.Vt int
152file descriptor objects; see
153.Xr cmsg 3
154for details on assembling and examining control messages.
155.Pp
156A file descriptor received this way is a
157.Em duplicate
158of the sender's descriptor, as if it were created with a call to
159.Xr dup 2 .
160Per-process descriptor flags, set with
161.Xr fcntl 2 ,
162are
163.Em not
164passed to a receiver.
165Descriptors that are awaiting delivery, or that are
166purposely not received, are automatically closed by the system
167when the destination socket is closed.
168.Pp
169A
170.Ux Ns -domain
171socket supports the following socket options for use with
172.Xr setsockopt 2
173and
174.Xr getsockopt 2
175at the level
176.Dv SOL_LOCAL :
177.Bl -tag -width 6n
178.It Dv LOCAL_CONNWAIT Pq Vt int
179May be enabled with
180.Xr setsockopt 2
181on a
182.Dv SOCK_SEQPACKET
183or
184.Dv SOCK_STREAM
185socket.
186If enabled,
187.Xr connect 2
188will block until a peer is waiting in
189.Xr accept 2 .
190.It Dv LOCAL_CREDS Pq Vt int
191May be enabled with
192.Xr setsockopt 2
193on a
194.Dv SOCK_DGRAM ,
195.Dv SOCK_SEQPACKET ,
196or a
197.Dv SOCK_STREAM
198socket.
199This option provides a mechanism for the receiver to
200receive the credentials of the process as a
201.Xr recvmsg 2
202control message.
203The
204.Fa msg_control
205field in the
206.Vt msghdr
207structure points
208to a buffer that contains a
209.Vt cmsghdr
210structure followed by a variable
211length
212.Vt sockcred
213structure, defined in
214.In sys/socket.h
215as follows:
216.Bd -literal
217struct sockcred {
218        pid_t   sc_pid;       /* process id */
219        uid_t   sc_uid;       /* real user id */
220        uid_t   sc_euid;      /* effective user id */
221        gid_t   sc_gid;       /* real group id */
222        gid_t   sc_egid;      /* effective group id */
223        int     sc_ngroups;   /* number of supplemental groups */
224        gid_t   sc_groups[1]; /* variable length */
225};
226.Ed
227.Pp
228The
229.Fn SOCKCREDSIZE
230macro computes the size of the
231.Vt sockcred
232structure for a specified number of groups.
233The
234.Vt cmsghdr
235fields have the following values:
236.Bd -literal -offset indent
237cmsg_len = CMSG_LEN(SOCKCREDSIZE(ngroups))
238cmsg_level = SOL_SOCKET
239cmsg_type = SCM_CREDS
240.Ed
241.It Dv LOCAL_PEEREID Pq Vt "struct unpcbid"
242May be queried with
243.Xr getsockopt 2
244to get the PID and effective user and group IDs of a
245.Dv SOCK_STREAM
246or
247.Dv SOCK_SEQPACKET
248peer when it did
249.Xr connect 2
250or
251.Xr bind 2 .
252The returned structure is
253.Bd -literal
254struct unpcbid {
255        pid_t unp_pid;        /* process id */
256        uid_t unp_euid;       /* effective user id */
257        gid_t unp_egid;       /* effective group id */
258};
259.Ed
260.Pp
261as defined in
262.In sys/un.h .
263.El
264.Sh EXAMPLES
265The following code fragment shows how to bind a socket to pathname:
266.Bd -literal -offset indent
267const char *pathname = "/path/to/socket";
268struct sockaddr_un addr;
269int ret;
270
271memset(&addr, 0, sizeof(addr));
272addr.sun_family = AF_LOCAL;
273if (strlen(pathname) >= sizeof(addr.sun_path))
274        goto too_long;
275strncpy(addr.sun_path, pathname, sizeof(addr.sun_path));
276ret = bind(s, (const struct sockaddr *)&addr, SUN_LEN(&addr));
277if (ret != 0)
278        goto bind_failed;
279\&...
280.Ed
281.Sh COMPATIBILITY
282The
283.Fa sun_len
284field exists only in system derived from
285.Bx 4.4 .
286On systems which don't have the
287.Fn SUN_LEN
288macro, the following definition is recommended:
289.Bd -literal -offset indent
290#ifndef SUN_LEN
291#define SUN_LEN(su)	sizeof(struct(sockaddr_un))
292#endif
293.Ed
294.Sh SEE ALSO
295.Xr socket 2 ,
296.Xr CMSG_DATA 3 ,
297.Xr intro 4
298.Rs
299.%T "An Introductory 4.4BSD Interprocess Communication Tutorial"
300.%A Stuart Sechrest
301.Re
302.Pq see Pa /usr/share/doc/reference/ref3/sockets
303.Rs
304.%T "Advanced 4.4BSD IPC Tutorial"
305.%A Samuel J. Leffler
306.%A Robert S. Fabry
307.%A William N. Joy
308.%A Phil Lapsley
309.%A Steve Miller
310.%A Chris Torek
311.Re
312.Pq see Pa /usr/share/doc/reference/ref3/sockets-advanced
313.Sh HISTORY
314The
315.Fa sc_pid
316field was introduced in
317.Nx 8.0 .
318