1 .\" $NetBSD: unix.4,v 1.19 2007/08/09 15:23:03 he Exp $
3 .\" Copyright (c) 1991, 1993
4 .\" The Regents of the University of California. All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
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.
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
30 .\" @(#)unix.4 8.1 (Berkeley) 6/9/93
37 .Nd UNIX-domain protocol family
44 protocol family is a collection of protocols
45 that provides local (on-machine) interprocess
46 communication through the normal
56 filesystem pathnames for addressing.
59 addresses are variable-length filesystem pathnames of
60 at most 104 characters.
64 .Bd -literal -offset indent
76 causes a socket file to be created in the filesystem.
79 removed when the socket is closed\(em\c
81 must be used to remove the file.
89 can be calculated by the macro
95 field must be terminated by a NUL character to be used with
97 but the terminating NUL is
102 kernel ignores any user-set value in the
104 member of the structure.
108 protocol family does not support broadcast addressing or any form
111 matching on incoming messages.
112 All addresses are absolute- or relative-pathnames
116 Normal filesystem access-control mechanisms are also
117 applied when referencing pathnames; e.g., the destination
126 protocol family comprises simple
127 transport protocols that support the
133 sockets also support the communication of
135 file descriptors through the use of the
144 Any valid descriptor may be sent in a message.
145 The file descriptor(s) to be passed are described using a
147 that is defined in the include file
148 .Aq Pa sys/socket.h .
149 The type of the message is
151 and the data portion of the messages is an array of integers
152 representing the file descriptors to be passed.
153 The number of descriptors being passed is defined
154 by the length field of the message;
155 the length field is the sum of the size of the header
156 plus the size of the array of file descriptors.
158 The received descriptor is a
160 of the sender's descriptor, as if it were created with a call to
162 Per-process descriptor flags, set with
166 passed to a receiver.
167 Descriptors that are awaiting delivery, or that are
168 purposely not received, are automatically closed by the system
169 when the destination socket is closed.
171 A UNIX-domain socket supports two
180 option may be enabled on a
185 This option provides a mechanism for the receiver to
186 receive the credentials of the process as a
189 The msg_control field in the msghdr structure points
190 to a buffer that contains a cmsghdr structure followed by a variable
191 length sockcred structure, defined in
196 uid_t sc_uid; /* real user id */
197 uid_t sc_euid; /* effective user id */
198 gid_t sc_gid; /* real group id */
199 gid_t sc_egid; /* effective group id */
200 int sc_ngroups; /* number of supplemental groups */
201 gid_t sc_groups[1]; /* variable length */
207 option may be used with
209 to get the PID and effective user and group IDs of a
215 The returned structure is
218 pid_t unp_pid; /* process id */
219 uid_t unp_euid; /* effective user id */
220 gid_t unp_egid; /* effective group id */
228 macro computes the size of the sockcred structure for a specified number
230 The cmsghdr fields have the following values:
232 cmsg_len = sizeof(struct cmsghdr) + SOCKCREDSIZE(ngroups)
233 cmsg_level = SOL_SOCKET
234 cmsg_type = SCM_CREDS
237 The following code fragment shows how to bind a socket to pathname:
238 .Bd -literal -offset indent
239 const char *pathname = "/path/to/socket";
240 struct sockaddr_un addr;
243 memset(\*[Am]addr, 0, sizeof(addr));
244 addr.sun_family = AF_LOCAL;
245 if (strlen(pathname) \*[Ge] sizeof(addr.sun_path))
247 strncpy(addr.sun_path, pathname, sizeof(addr.sun_path));
248 ret = bind(s, (const struct sockaddr *)\*[Am]addr, SUN_LEN(\*[Am]addr));
257 field exists only in system derived from 4.4BSD.
258 On systems which don't have the
260 macro, the following definition is recommended:
261 .Bd -literal -offset indent
263 #define SUN_LEN(su) sizeof(struct(sockaddr_un))
270 .%T "An Introductory 4.4BSD Interprocess Communication Tutorial"
273 .Pq see Pa /usr/share/doc/psd/20.ipctut
275 .%T "Advanced 4.4BSD IPC Tutorial"
276 .%A Samuel J. Leffler
283 .Pq see Pa /usr/share/doc/psd/21.ipc