Merge from vendor branch GROFF:
[dragonfly.git] / sys / emulation / 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  * $FreeBSD: src/sys/svr4/svr4_socket.c,v 1.7 1999/12/08 12:00:48 newton Exp $
32  * $DragonFly: src/sys/emulation/svr4/Attic/svr4_socket.c,v 1.7 2005/08/02 13:03:54 joerg Exp $
33  */
34
35 /*
36  * In SVR4 unix domain sockets are referenced sometimes
37  * (in putmsg(2) for example) as a [device, inode] pair instead of a pathname.
38  * Since there is no iname() routine in the kernel, and we need access to
39  * a mapping from inode to pathname, we keep our own table. This is a simple
40  * linked list that contains the pathname, the [device, inode] pair, the
41  * file corresponding to that socket and the process. When the
42  * socket gets closed we remove the item from the list. The list gets loaded
43  * every time a stat(2) call finds a socket.
44  */
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/queue.h>
49 #include <sys/file.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/sysproto.h>
53 #include <sys/un.h>
54 #include <sys/stat.h>
55 #include <sys/proc.h>
56 #include <sys/malloc.h>
57
58 #include <machine/inttypes.h>
59
60 #include "svr4.h"
61 #include "svr4_types.h"
62 #include "svr4_util.h"
63 #include "svr4_socket.h"
64 #include "svr4_signal.h"
65 #include "svr4_sockmod.h"
66 #include "svr4_proto.h"
67
68 struct svr4_sockcache_entry {
69         struct thread *td;      /* Process for the socket               */
70         void *cookie;           /* Internal cookie used for matching    */
71         struct sockaddr_un sock;/* Pathname for the socket              */
72         udev_t dev;             /* Device where the socket lives on     */
73         ino_t ino;              /* Inode where the socket lives on      */
74         TAILQ_ENTRY(svr4_sockcache_entry) entries;
75 };
76
77 extern TAILQ_HEAD(svr4_sockcache_head, svr4_sockcache_entry) svr4_head;
78 extern int svr4_str_initialized;
79
80 struct sockaddr_un *
81 svr4_find_socket(td, fp, dev, ino)
82         struct thread *td;
83         struct file *fp;
84         udev_t dev;
85         ino_t ino;
86 {
87         struct svr4_sockcache_entry *e;
88         void *cookie = ((struct socket *) fp->f_data)->so_emuldata;
89
90         if (!svr4_str_initialized) {
91                 DPRINTF(("svr4_find_socket: uninitialized [%p,%d,%"PRId64"]\n",
92                     td, dev, ino));
93                 TAILQ_INIT(&svr4_head);
94                 svr4_str_initialized = 1;
95                 return NULL;
96         }
97
98
99         DPRINTF(("svr4_find_socket: [%p,%d,%"PRId64"]: ", td, dev, ino));
100         TAILQ_FOREACH(e, &svr4_head, entries)
101                 if (e->td == td && e->dev == dev && e->ino == ino) {
102 #ifdef DIAGNOSTIC
103                         if (e->cookie != NULL && e->cookie != cookie)
104                                 panic("svr4 socket cookie mismatch");
105 #endif
106                         e->cookie = cookie;
107                         DPRINTF(("%s\n", e->sock.sun_path));
108                         return &e->sock;
109                 }
110
111         DPRINTF(("not found\n"));
112         return NULL;
113 }
114
115
116 /*
117  * svr4_delete_socket() is in sys/dev/streams.c (because it's called by
118  * the streams "soo_close()" routine).
119  */
120 int
121 svr4_add_socket(td, path, st)
122         struct thread *td;
123         const char *path;
124         struct stat *st;
125 {
126         struct svr4_sockcache_entry *e;
127         int len, error;
128
129         if (!svr4_str_initialized) {
130                 TAILQ_INIT(&svr4_head);
131                 svr4_str_initialized = 1;
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->td = td;
139
140         if ((error = copyinstr(path, e->sock.sun_path,
141             sizeof(e->sock.sun_path), &len)) != 0) {
142                 DPRINTF(("svr4_add_socket: copyinstr failed %d\n", error));
143                 free(e, M_TEMP);
144                 return error;
145         }
146
147         e->sock.sun_family = AF_LOCAL;
148         e->sock.sun_len = len;
149
150         TAILQ_INSERT_HEAD(&svr4_head, e, entries);
151         DPRINTF(("svr4_add_socket: %s [%p,%d,%"PRId64"]\n", e->sock.sun_path,
152                  td, e->dev, e->ino));
153         return 0;
154 }
155
156
157 int
158 svr4_sys_socket(struct svr4_sys_socket_args *uap)
159 {
160         switch (SCARG(uap, type)) {
161         case SVR4_SOCK_DGRAM:
162                 SCARG(uap, type) = SOCK_DGRAM;
163                 break;
164
165         case SVR4_SOCK_STREAM:
166                 SCARG(uap, type) = SOCK_STREAM;
167                 break;
168
169         case SVR4_SOCK_RAW:
170                 SCARG(uap, type) = SOCK_RAW;
171                 break;
172
173         case SVR4_SOCK_RDM:
174                 SCARG(uap, type) = SOCK_RDM;
175                 break;
176
177         case SVR4_SOCK_SEQPACKET:
178                 SCARG(uap, type) = SOCK_SEQPACKET;
179                 break;
180         default:
181                 return EINVAL;
182         }
183         return socket((struct socket_args *)uap);
184 }