1 /* $NetBSD: svr4_socket.c,v 1.15 2006/08/17 17:11:27 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * In SVR4 unix domain sockets are referenced sometimes
41 * (in putmsg(2) for example) as a [device, inode] pair instead of a pathname.
42 * Since there is no iname() routine in the kernel, and we need access to
43 * a mapping from inode to pathname, we keep our own table. This is a simple
44 * linked list that contains the pathname, the [device, inode] pair, the
45 * file corresponding to that socket and the process. When the
46 * socket gets closed we remove the item from the list. The list gets loaded
47 * every time a stat(2) call finds a socket.
48 */
49
50 #include <sys/cdefs.h>
51 __KERNEL_RCSID(0, "$NetBSD: svr4_socket.c,v 1.15 2006/08/17 17:11:27 christos Exp $");
52
53 #include <sys/param.h>
54 #include <sys/kernel.h>
55 #include <sys/systm.h>
56 #include <sys/queue.h>
57 #include <sys/mbuf.h>
58 #include <sys/file.h>
59 #include <sys/mount.h>
60 #include <sys/socket.h>
61 #include <sys/socketvar.h>
62 #include <sys/sa.h>
63 #include <sys/syscallargs.h>
64 #include <sys/un.h>
65 #include <sys/stat.h>
66
67 #include <compat/svr4/svr4_types.h>
68 #include <compat/svr4/svr4_util.h>
69 #include <compat/svr4/svr4_socket.h>
70 #include <compat/svr4/svr4_signal.h>
71 #include <compat/svr4/svr4_sockmod.h>
72 #include <compat/svr4/svr4_lwp.h>
73 #include <compat/svr4/svr4_ucontext.h>
74 #include <compat/svr4/svr4_syscallargs.h>
75
76 struct svr4_sockcache_entry {
77 struct proc *p; /* Process for the socket */
78 void *cookie; /* Internal cookie used for matching */
79 struct sockaddr_un sock;/* Pathname for the socket */
80 dev_t dev; /* Device where the socket lives on */
81 svr4_ino_t ino; /* Inode where the socket lives on */
82 TAILQ_ENTRY(svr4_sockcache_entry) entries;
83 };
84
85 static TAILQ_HEAD(svr4_sockcache_head, svr4_sockcache_entry) svr4_head;
86 static int initialized = 0;
87
88 struct sockaddr_un *
89 svr4_find_socket(p, fp, dev, ino)
90 struct proc *p;
91 struct file *fp;
92 dev_t dev;
93 svr4_ino_t ino;
94 {
95 struct svr4_sockcache_entry *e;
96 void *cookie = ((struct socket *) fp->f_data)->so_internal;
97
98 if (!initialized) {
99 DPRINTF(("svr4_find_socket: uninitialized [%p,%d,%lu]\n",
100 p, dev, ino));
101 TAILQ_INIT(&svr4_head);
102 initialized = 1;
103 return NULL;
104 }
105
106
107 DPRINTF(("svr4_find_socket: [%p,%d,%lu]: ", p, dev, ino));
108 for (e = svr4_head.tqh_first; e != NULL; e = e->entries.tqe_next)
109 if (e->p == p && e->dev == dev && e->ino == ino) {
110 #ifdef DIAGNOSTIC
111 if (e->cookie != NULL && e->cookie != cookie)
112 panic("svr4 socket cookie mismatch");
113 #endif
114 e->cookie = cookie;
115 DPRINTF(("%s\n", e->sock.sun_path));
116 return &e->sock;
117 }
118
119 DPRINTF(("not found\n"));
120 return NULL;
121 }
122
123
124 void
125 svr4_delete_socket(p, fp)
126 struct proc *p;
127 struct file *fp;
128 {
129 struct svr4_sockcache_entry *e;
130 void *cookie = ((struct socket *) fp->f_data)->so_internal;
131
132 if (!initialized) {
133 TAILQ_INIT(&svr4_head);
134 initialized = 1;
135 return;
136 }
137
138 for (e = svr4_head.tqh_first; e != NULL; e = e->entries.tqe_next)
139 if (e->p == p && e->cookie == cookie) {
140 TAILQ_REMOVE(&svr4_head, e, entries);
141 DPRINTF(("svr4_delete_socket: %s [%p,%d,%lu]\n",
142 e->sock.sun_path, p, e->dev, e->ino));
143 free(e, M_TEMP);
144 return;
145 }
146 }
147
148
149 int
150 svr4_add_socket(p, path, st)
151 struct proc *p;
152 const char *path;
153 struct stat *st;
154 {
155 struct svr4_sockcache_entry *e;
156 size_t len;
157 int error;
158
159 if (!initialized) {
160 TAILQ_INIT(&svr4_head);
161 initialized = 1;
162 }
163
164 e = malloc(sizeof(*e), M_TEMP, M_WAITOK);
165 e->cookie = NULL;
166 e->dev = st->st_dev;
167 e->ino = st->st_ino;
168 e->p = p;
169
170 if ((error = copyinstr(path, e->sock.sun_path,
171 sizeof(e->sock.sun_path), &len)) != 0) {
172 DPRINTF(("svr4_add_socket: copyinstr failed %d\n", error));
173 free(e, M_TEMP);
174 return error;
175 }
176
177 e->sock.sun_family = AF_LOCAL;
178 e->sock.sun_len = len;
179
180 TAILQ_INSERT_HEAD(&svr4_head, e, entries);
181 DPRINTF(("svr4_add_socket: %s [%p,%d,%lu]\n", e->sock.sun_path,
182 p, e->dev, e->ino));
183 return 0;
184 }
185
186
187 int
188 svr4_sys_socket(l, v, retval)
189 struct lwp *l;
190 void *v;
191 register_t *retval;
192 {
193 struct svr4_sys_socket_args *uap = v;
194
195 switch (SCARG(uap, type)) {
196 case SVR4_SOCK_DGRAM:
197 SCARG(uap, type) = SOCK_DGRAM;
198 break;
199
200 case SVR4_SOCK_STREAM:
201 SCARG(uap, type) = SOCK_STREAM;
202 break;
203
204 case SVR4_SOCK_RAW:
205 SCARG(uap, type) = SOCK_RAW;
206 break;
207
208 case SVR4_SOCK_RDM:
209 SCARG(uap, type) = SOCK_RDM;
210 break;
211
212 case SVR4_SOCK_SEQPACKET:
213 SCARG(uap, type) = SOCK_SEQPACKET;
214 break;
215 default:
216 return EINVAL;
217 }
218 return sys___socket30(l, uap, retval);
219 }
Cache object: d6f157885bb1376d50dc2597fae7efb4
|