]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/svr4/svr4_socket.c
This commit was generated by cvs2svn to compensate for changes in r157184,
[FreeBSD/FreeBSD.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/file.h>
50 #include <sys/lock.h>
51 #include <sys/mutex.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/sysproto.h>
55 #include <sys/un.h>
56 #include <sys/stat.h>
57 #include <sys/proc.h>
58 #include <sys/malloc.h>
59
60 #include <compat/svr4/svr4.h>
61 #include <compat/svr4/svr4_types.h>
62 #include <compat/svr4/svr4_util.h>
63 #include <compat/svr4/svr4_socket.h>
64 #include <compat/svr4/svr4_signal.h>
65 #include <compat/svr4/svr4_sockmod.h>
66 #include <compat/svr4/svr4_proto.h>
67
68 struct sockaddr_un *
69 svr4_find_socket(td, fp, dev, ino)
70         struct thread *td;
71         struct file *fp;
72         dev_t dev;
73         ino_t ino;
74 {
75         struct svr4_sockcache_entry *e;
76         void *cookie = ((struct socket *)fp->f_data)->so_emuldata;
77
78         if (svr4_str_initialized != 2) {
79                 if (atomic_cmpset_acq_int(&svr4_str_initialized, 0, 1)) {
80                         DPRINTF(("svr4_find_socket: uninitialized [%p,%d,%d]\n",
81                             td, dev, ino));
82                         TAILQ_INIT(&svr4_head);
83                         atomic_store_rel_int(&svr4_str_initialized, 2);
84                 }
85                 return NULL;
86         }
87
88
89         DPRINTF(("svr4_find_socket: [%p,%d,%d]: ", td, dev, ino));
90         TAILQ_FOREACH(e, &svr4_head, entries)
91                 if (e->p == td->td_proc && e->dev == dev && e->ino == ino) {
92 #ifdef DIAGNOSTIC
93                         if (e->cookie != NULL && e->cookie != cookie)
94                                 panic("svr4 socket cookie mismatch");
95 #endif
96                         e->cookie = cookie;
97                         DPRINTF(("%s\n", e->sock.sun_path));
98                         return &e->sock;
99                 }
100
101         DPRINTF(("not found\n"));
102         return NULL;
103 }
104
105
106 /*
107  * svr4_delete_socket() is in sys/dev/streams.c (because it's called by
108  * the streams "soo_close()" routine).
109  */
110 int
111 svr4_add_socket(td, path, st)
112         struct thread *td;
113         const char *path;
114         struct stat *st;
115 {
116         struct svr4_sockcache_entry *e;
117         int len, error;
118
119         mtx_lock(&Giant);
120
121         /*
122          * Wait for the TAILQ to be initialized.  Only the very first CPU
123          * will succeed on the atomic_cmpset().  The other CPU's will spin
124          * until the first one finishes the initialization.  Once the
125          * initialization is complete, the condition will always fail
126          * avoiding expensive atomic operations in the common case.
127          */
128         while (svr4_str_initialized != 2)
129                 if (atomic_cmpset_acq_int(&svr4_str_initialized, 0, 1)) {
130                         TAILQ_INIT(&svr4_head);
131                         atomic_store_rel_int(&svr4_str_initialized, 2);
132                 }
133
134         e = malloc(sizeof(*e), M_TEMP, M_WAITOK);
135         e->cookie = NULL;
136         e->dev = st->st_dev;
137         e->ino = st->st_ino;
138         e->p = td->td_proc;
139
140         if ((error = copyinstr(path, e->sock.sun_path,
141             sizeof(e->sock.sun_path), &len)) != 0) {
142                 mtx_unlock(&Giant);
143                 DPRINTF(("svr4_add_socket: copyinstr failed %d\n", error));
144                 free(e, M_TEMP);
145                 return error;
146         }
147
148         e->sock.sun_family = AF_LOCAL;
149         e->sock.sun_len = len;
150
151         TAILQ_INSERT_HEAD(&svr4_head, e, entries);
152         mtx_unlock(&Giant);
153         DPRINTF(("svr4_add_socket: %s [%p,%d,%d]\n", e->sock.sun_path,
154                  td->td_proc, e->dev, e->ino));
155         return 0;
156 }
157
158
159 int
160 svr4_sys_socket(td, uap)
161         struct thread *td;
162         struct svr4_sys_socket_args *uap;
163 {
164         switch (uap->type) {
165         case SVR4_SOCK_DGRAM:
166                 uap->type = SOCK_DGRAM;
167                 break;
168
169         case SVR4_SOCK_STREAM:
170                 uap->type = SOCK_STREAM;
171                 break;
172
173         case SVR4_SOCK_RAW:
174                 uap->type = SOCK_RAW;
175                 break;
176
177         case SVR4_SOCK_RDM:
178                 uap->type = SOCK_RDM;
179                 break;
180
181         case SVR4_SOCK_SEQPACKET:
182                 uap->type = SOCK_SEQPACKET;
183                 break;
184         default:
185                 return EINVAL;
186         }
187         return socket(td, (struct socket_args *)uap);
188 }