svr4_socket.c revision 52333
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: head/sys/compat/svr4/svr4_socket.c 52333 1999-10-17 14:44:48Z newton $ 32 */ 33 34/* 35 * In SVR4 unix domain sockets are referenced sometimes 36 * (in putmsg(2) for example) as a [device, inode] pair instead of a pathname. 37 * Since there is no iname() routine in the kernel, and we need access to 38 * a mapping from inode to pathname, we keep our own table. This is a simple 39 * linked list that contains the pathname, the [device, inode] pair, the 40 * file corresponding to that socket and the process. When the 41 * socket gets closed we remove the item from the list. The list gets loaded 42 * every time a stat(2) call finds a socket. 43 */ 44 45#include <sys/param.h> 46#include <sys/kernel.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 <svr4/svr4_types.h> 59#include <svr4/svr4_util.h> 60#include <svr4/svr4_socket.h> 61#include <svr4/svr4_signal.h> 62#include <svr4/svr4_sockmod.h> 63#include <svr4/svr4_proto.h> 64 65struct svr4_sockcache_entry { 66 struct proc *p; /* Process for the socket */ 67 void *cookie; /* Internal cookie used for matching */ 68 struct sockaddr_un sock;/* Pathname for the socket */ 69 udev_t dev; /* Device where the socket lives on */ 70 ino_t ino; /* Inode where the socket lives on */ 71 TAILQ_ENTRY(svr4_sockcache_entry) entries; 72}; 73 74extern TAILQ_HEAD(svr4_sockcache_head, svr4_sockcache_entry) svr4_head; 75extern int svr4_str_initialized; 76 77struct sockaddr_un * 78svr4_find_socket(p, fp, dev, ino) 79 struct proc *p; 80 struct file *fp; 81 udev_t dev; 82 ino_t ino; 83{ 84 struct svr4_sockcache_entry *e; 85 void *cookie = ((struct socket *) fp->f_data)->so_emuldata; 86 87 if (!svr4_str_initialized) { 88 DPRINTF(("svr4_find_socket: uninitialized [%p,%d,%d]\n", 89 p, dev, ino)); 90 TAILQ_INIT(&svr4_head); 91 svr4_str_initialized = 1; 92 return NULL; 93 } 94 95 96 DPRINTF(("svr4_find_socket: [%p,%d,%d]: ", p, dev, ino)); 97 for (e = svr4_head.tqh_first; e != NULL; e = e->entries.tqe_next) 98 if (e->p == p && e->dev == dev && e->ino == ino) { 99#ifdef DIAGNOSTIC 100 if (e->cookie != NULL && e->cookie != cookie) 101 panic("svr4 socket cookie mismatch"); 102#endif 103 e->cookie = cookie; 104 DPRINTF(("%s\n", e->sock.sun_path)); 105 return &e->sock; 106 } 107 108 DPRINTF(("not found\n")); 109 return NULL; 110} 111 112 113void 114svr4_delete_socket(p, fp) 115 struct proc *p; 116 struct file *fp; 117{ 118 struct svr4_sockcache_entry *e; 119 void *cookie = ((struct socket *) fp->f_data)->so_emuldata; 120 121 if (!svr4_str_initialized) { 122 TAILQ_INIT(&svr4_head); 123 svr4_str_initialized = 1; 124 return; 125 } 126 127 for (e = svr4_head.tqh_first; e != NULL; e = e->entries.tqe_next) 128 if (e->p == p && e->cookie == cookie) { 129 TAILQ_REMOVE(&svr4_head, e, entries); 130 DPRINTF(("svr4_delete_socket: %s [%p,%d,%d]\n", 131 e->sock.sun_path, p, e->dev, e->ino)); 132 free(e, M_TEMP); 133 return; 134 } 135} 136 137 138int 139svr4_add_socket(p, path, st) 140 struct proc *p; 141 const char *path; 142 struct stat *st; 143{ 144 struct svr4_sockcache_entry *e; 145 int len, error; 146 147 if (!svr4_str_initialized) { 148 TAILQ_INIT(&svr4_head); 149 svr4_str_initialized = 1; 150 } 151 152 e = malloc(sizeof(*e), M_TEMP, M_WAITOK); 153 e->cookie = NULL; 154 e->dev = st->st_dev; 155 e->ino = st->st_ino; 156 e->p = p; 157 158 if ((error = copyinstr(path, e->sock.sun_path, 159 sizeof(e->sock.sun_path), &len)) != 0) { 160 DPRINTF(("svr4_add_socket: copyinstr failed %d\n", error)); 161 free(e, M_TEMP); 162 return error; 163 } 164 165 e->sock.sun_family = AF_LOCAL; 166 e->sock.sun_len = len; 167 168 TAILQ_INSERT_HEAD(&svr4_head, e, entries); 169 DPRINTF(("svr4_add_socket: %s [%p,%d,%d]\n", e->sock.sun_path, 170 p, e->dev, e->ino)); 171 return 0; 172} 173 174 175int 176svr4_sys_socket(p, uap) 177 struct proc *p; 178 struct svr4_sys_socket_args *uap; 179{ 180 switch (SCARG(uap, type)) { 181 case SVR4_SOCK_DGRAM: 182 SCARG(uap, type) = SOCK_DGRAM; 183 break; 184 185 case SVR4_SOCK_STREAM: 186 SCARG(uap, type) = SOCK_STREAM; 187 break; 188 189 case SVR4_SOCK_RAW: 190 SCARG(uap, type) = SOCK_RAW; 191 break; 192 193 case SVR4_SOCK_RDM: 194 SCARG(uap, type) = SOCK_RDM; 195 break; 196 197 case SVR4_SOCK_SEQPACKET: 198 SCARG(uap, type) = SOCK_SEQPACKET; 199 break; 200 default: 201 return EINVAL; 202 } 203 return socket(p, (struct socket_args *)uap); 204} 205