2 * Copyright (c) 1998 Mark Newton
3 * Copyright (c) 1996 Christos Zoulas.
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. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Christos Zoulas.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * In SVR4 unix domain sockets are referenced sometimes
34 * (in putmsg(2) for example) as a [device, inode] pair instead of a pathname.
35 * Since there is no iname() routine in the kernel, and we need access to
36 * a mapping from inode to pathname, we keep our own table. This is a simple
37 * linked list that contains the pathname, the [device, inode] pair, the
38 * file corresponding to that socket and the process. When the
39 * socket gets closed we remove the item from the list. The list gets loaded
40 * every time a stat(2) call finds a socket.
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/queue.h>
49 #include <sys/eventhandler.h>
51 #include <sys/kernel.h>
53 #include <sys/mutex.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/sysproto.h>
60 #include <sys/malloc.h>
62 #include <compat/svr4/svr4.h>
63 #include <compat/svr4/svr4_types.h>
64 #include <compat/svr4/svr4_util.h>
65 #include <compat/svr4/svr4_socket.h>
66 #include <compat/svr4/svr4_signal.h>
67 #include <compat/svr4/svr4_sockmod.h>
68 #include <compat/svr4/svr4_proto.h>
70 struct svr4_sockcache_entry {
71 struct proc *p; /* Process for the socket */
72 void *cookie; /* Internal cookie used for matching */
73 struct sockaddr_un sock;/* Pathname for the socket */
74 dev_t dev; /* Device where the socket lives on */
75 ino_t ino; /* Inode where the socket lives on */
76 TAILQ_ENTRY(svr4_sockcache_entry) entries;
79 static TAILQ_HEAD(, svr4_sockcache_entry) svr4_head;
80 static struct mtx svr4_sockcache_lock;
81 static eventhandler_tag svr4_sockcache_exit_tag, svr4_sockcache_exec_tag;
83 static void svr4_purge_sockcache(void *arg, struct proc *p);
86 svr4_find_socket(td, fp, dev, ino, saun)
91 struct sockaddr_un *saun;
93 struct svr4_sockcache_entry *e;
94 void *cookie = ((struct socket *)fp->f_data)->so_emuldata;
96 DPRINTF(("svr4_find_socket: [%p,%ju,%ju]: ", td, (uintmax_t)dev,
98 mtx_lock(&svr4_sockcache_lock);
99 TAILQ_FOREACH(e, &svr4_head, entries)
100 if (e->p == td->td_proc && e->dev == dev && e->ino == ino) {
102 if (e->cookie != NULL && e->cookie != cookie)
103 panic("svr4 socket cookie mismatch");
106 DPRINTF(("%s\n", e->sock.sun_path));
108 mtx_unlock(&svr4_sockcache_lock);
112 mtx_unlock(&svr4_sockcache_lock);
113 DPRINTF(("not found\n"));
118 svr4_add_socket(td, path, st)
123 struct svr4_sockcache_entry *e;
127 e = malloc(sizeof(*e), M_TEMP, M_WAITOK);
133 if ((error = copyinstr(path, e->sock.sun_path,
134 sizeof(e->sock.sun_path), &len)) != 0) {
135 DPRINTF(("svr4_add_socket: copyinstr failed %d\n", error));
140 e->sock.sun_family = AF_LOCAL;
141 e->sock.sun_len = len;
143 mtx_lock(&svr4_sockcache_lock);
144 TAILQ_INSERT_HEAD(&svr4_head, e, entries);
145 mtx_unlock(&svr4_sockcache_lock);
146 DPRINTF(("svr4_add_socket: %s [%p,%ju,%ju]\n", e->sock.sun_path,
147 td->td_proc, (uintmax_t)e->dev, (uintmax_t)e->ino));
152 svr4_delete_socket(p, fp)
156 struct svr4_sockcache_entry *e;
157 void *cookie = ((struct socket *)fp->f_data)->so_emuldata;
159 mtx_lock(&svr4_sockcache_lock);
160 TAILQ_FOREACH(e, &svr4_head, entries)
161 if (e->p == p && e->cookie == cookie) {
162 TAILQ_REMOVE(&svr4_head, e, entries);
163 mtx_unlock(&svr4_sockcache_lock);
164 DPRINTF(("svr4_delete_socket: %s [%p,%ju,%ju]\n",
165 e->sock.sun_path, p, (uintmax_t)e->dev,
170 mtx_unlock(&svr4_sockcache_lock);
174 svr4_purge_sockcache(arg, p)
178 struct svr4_sockcache_entry *e, *ne;
180 mtx_lock(&svr4_sockcache_lock);
181 TAILQ_FOREACH_SAFE(e, &svr4_head, entries, ne) {
183 TAILQ_REMOVE(&svr4_head, e, entries);
184 DPRINTF(("svr4_purge_sockcache: %s [%p,%ju,%ju]\n",
185 e->sock.sun_path, p, (uintmax_t)e->dev,
190 mtx_unlock(&svr4_sockcache_lock);
194 svr4_sockcache_init(void)
197 TAILQ_INIT(&svr4_head);
198 mtx_init(&svr4_sockcache_lock, "svr4 socket cache", NULL, MTX_DEF);
199 svr4_sockcache_exit_tag = EVENTHANDLER_REGISTER(process_exit,
200 svr4_purge_sockcache, NULL, EVENTHANDLER_PRI_ANY);
201 svr4_sockcache_exec_tag = EVENTHANDLER_REGISTER(process_exec,
202 svr4_purge_sockcache, NULL, EVENTHANDLER_PRI_ANY);
206 svr4_sockcache_destroy(void)
209 KASSERT(TAILQ_EMPTY(&svr4_head),
210 ("%s: sockcache entries still around", __func__));
211 EVENTHANDLER_DEREGISTER(process_exec, svr4_sockcache_exec_tag);
212 EVENTHANDLER_DEREGISTER(process_exit, svr4_sockcache_exit_tag);
213 mtx_destroy(&svr4_sockcache_lock);
217 svr4_sys_socket(td, uap)
219 struct svr4_sys_socket_args *uap;
222 case SVR4_SOCK_DGRAM:
223 uap->type = SOCK_DGRAM;
226 case SVR4_SOCK_STREAM:
227 uap->type = SOCK_STREAM;
231 uap->type = SOCK_RAW;
235 uap->type = SOCK_RDM;
238 case SVR4_SOCK_SEQPACKET:
239 uap->type = SOCK_SEQPACKET;
244 return sys_socket(td, (struct socket_args *)uap);