]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/compat/svr4/svr4_socket.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / compat / svr4 / svr4_socket.c
1 /*-
2  * Copyright (c) 1998 Mark Newton
3  * Copyright (c) 1996 Christos Zoulas. 
4  * 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 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.
19  *
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.
30  */
31
32 /*
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.
41  */
42
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/queue.h>
49 #include <sys/eventhandler.h>
50 #include <sys/file.h>
51 #include <sys/kernel.h>
52 #include <sys/lock.h>
53 #include <sys/mutex.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/sysproto.h>
57 #include <sys/un.h>
58 #include <sys/stat.h>
59 #include <sys/proc.h>
60 #include <sys/malloc.h>
61
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>
69
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;
77 };
78
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;
82
83 static void     svr4_purge_sockcache(void *arg, struct proc *p);
84
85 int
86 svr4_find_socket(td, fp, dev, ino, saun)
87         struct thread *td;
88         struct file *fp;
89         dev_t dev;
90         ino_t ino;
91         struct sockaddr_un *saun;
92 {
93         struct svr4_sockcache_entry *e;
94         void *cookie = ((struct socket *)fp->f_data)->so_emuldata;
95
96         DPRINTF(("svr4_find_socket: [%p,%d,%d]: ", td, dev, ino));
97         mtx_lock(&svr4_sockcache_lock);
98         TAILQ_FOREACH(e, &svr4_head, entries)
99                 if (e->p == td->td_proc && e->dev == dev && e->ino == ino) {
100 #ifdef DIAGNOSTIC
101                         if (e->cookie != NULL && e->cookie != cookie)
102                                 panic("svr4 socket cookie mismatch");
103 #endif
104                         e->cookie = cookie;
105                         DPRINTF(("%s\n", e->sock.sun_path));
106                         *saun = e->sock;
107                         mtx_unlock(&svr4_sockcache_lock);
108                         return (0);
109                 }
110
111         mtx_unlock(&svr4_sockcache_lock);
112         DPRINTF(("not found\n"));
113         return (ENOENT);
114 }
115
116 int
117 svr4_add_socket(td, path, st)
118         struct thread *td;
119         const char *path;
120         struct stat *st;
121 {
122         struct svr4_sockcache_entry *e;
123         int len, error;
124
125         e = malloc(sizeof(*e), M_TEMP, M_WAITOK);
126         e->cookie = NULL;
127         e->dev = st->st_dev;
128         e->ino = st->st_ino;
129         e->p = td->td_proc;
130
131         if ((error = copyinstr(path, e->sock.sun_path,
132             sizeof(e->sock.sun_path), &len)) != 0) {
133                 DPRINTF(("svr4_add_socket: copyinstr failed %d\n", error));
134                 free(e, M_TEMP);
135                 return error;
136         }
137
138         e->sock.sun_family = AF_LOCAL;
139         e->sock.sun_len = len;
140
141         mtx_lock(&svr4_sockcache_lock);
142         TAILQ_INSERT_HEAD(&svr4_head, e, entries);
143         mtx_unlock(&svr4_sockcache_lock);
144         DPRINTF(("svr4_add_socket: %s [%p,%d,%d]\n", e->sock.sun_path,
145                  td->td_proc, e->dev, e->ino));
146         return 0;
147 }
148
149 void
150 svr4_delete_socket(p, fp)
151         struct proc *p;
152         struct file *fp;
153 {
154         struct svr4_sockcache_entry *e;
155         void *cookie = ((struct socket *)fp->f_data)->so_emuldata;
156
157         mtx_lock(&svr4_sockcache_lock);
158         TAILQ_FOREACH(e, &svr4_head, entries)
159                 if (e->p == p && e->cookie == cookie) {
160                         TAILQ_REMOVE(&svr4_head, e, entries);
161                         mtx_unlock(&svr4_sockcache_lock);
162                         DPRINTF(("svr4_delete_socket: %s [%p,%d,%d]\n",
163                                  e->sock.sun_path, p, (int)e->dev, e->ino));
164                         free(e, M_TEMP);
165                         return;
166                 }
167         mtx_unlock(&svr4_sockcache_lock);
168 }
169
170 void
171 svr4_purge_sockcache(arg, p)
172         void *arg;
173         struct proc *p;
174 {
175         struct svr4_sockcache_entry *e, *ne;
176
177         mtx_lock(&svr4_sockcache_lock);
178         TAILQ_FOREACH_SAFE(e, &svr4_head, entries, ne) {
179                 if (e->p == p) {
180                         TAILQ_REMOVE(&svr4_head, e, entries);
181                         DPRINTF(("svr4_purge_sockcache: %s [%p,%d,%d]\n",
182                                  e->sock.sun_path, p, (int)e->dev, e->ino));
183                         free(e, M_TEMP);
184                 }
185         }
186         mtx_unlock(&svr4_sockcache_lock);
187 }
188
189 void
190 svr4_sockcache_init(void)
191 {
192
193         TAILQ_INIT(&svr4_head);
194         mtx_init(&svr4_sockcache_lock, "svr4 socket cache", NULL, MTX_DEF);
195         svr4_sockcache_exit_tag = EVENTHANDLER_REGISTER(process_exit,
196             svr4_purge_sockcache, NULL, EVENTHANDLER_PRI_ANY);
197         svr4_sockcache_exec_tag = EVENTHANDLER_REGISTER(process_exec,
198             svr4_purge_sockcache, NULL, EVENTHANDLER_PRI_ANY);
199 }
200
201 void
202 svr4_sockcache_destroy(void)
203 {
204
205         KASSERT(TAILQ_EMPTY(&svr4_head),
206             ("%s: sockcache entries still around", __func__));
207         EVENTHANDLER_DEREGISTER(process_exec, svr4_sockcache_exec_tag);
208         EVENTHANDLER_DEREGISTER(process_exit, svr4_sockcache_exit_tag);
209         mtx_destroy(&svr4_sockcache_lock);
210 }
211
212 int
213 svr4_sys_socket(td, uap)
214         struct thread *td;
215         struct svr4_sys_socket_args *uap;
216 {
217         switch (uap->type) {
218         case SVR4_SOCK_DGRAM:
219                 uap->type = SOCK_DGRAM;
220                 break;
221
222         case SVR4_SOCK_STREAM:
223                 uap->type = SOCK_STREAM;
224                 break;
225
226         case SVR4_SOCK_RAW:
227                 uap->type = SOCK_RAW;
228                 break;
229
230         case SVR4_SOCK_RDM:
231                 uap->type = SOCK_RDM;
232                 break;
233
234         case SVR4_SOCK_SEQPACKET:
235                 uap->type = SOCK_SEQPACKET;
236                 break;
237         default:
238                 return EINVAL;
239         }
240         return socket(td, (struct socket_args *)uap);
241 }