svr4_socket.c revision 50477
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 50477 1999-08-28 01:08:13Z peter $ 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/mbuf.h> 50#include <sys/file.h> 51#include <sys/mount.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 <svr4/svr4_types.h> 61#include <svr4/svr4_util.h> 62#include <svr4/svr4_socket.h> 63#include <svr4/svr4_signal.h> 64#include <svr4/svr4_sockmod.h> 65#include <svr4/svr4_proto.h> 66 67struct svr4_sockcache_entry { 68 struct proc *p; /* Process for the socket */ 69 void *cookie; /* Internal cookie used for matching */ 70 struct sockaddr_un sock;/* Pathname for the socket */ 71 udev_t dev; /* Device where the socket lives on */ 72 ino_t ino; /* Inode where the socket lives on */ 73 TAILQ_ENTRY(svr4_sockcache_entry) entries; 74}; 75 76extern TAILQ_HEAD(svr4_sockcache_head, svr4_sockcache_entry) svr4_head; 77extern int svr4_str_initialized; 78 79struct sockaddr_un * 80svr4_find_socket(p, fp, dev, ino) 81 struct proc *p; 82 struct file *fp; 83 udev_t dev; 84 ino_t ino; 85{ 86 struct svr4_sockcache_entry *e; 87 void *cookie = ((struct socket *) fp->f_data)->so_emuldata; 88 89 if (!svr4_str_initialized) { 90 DPRINTF(("svr4_find_socket: uninitialized [%p,%d,%d]\n", 91 p, dev, ino)); 92 TAILQ_INIT(&svr4_head); 93 svr4_str_initialized = 1; 94 return NULL; 95 } 96 97 98 DPRINTF(("svr4_find_socket: [%p,%d,%d]: ", p, dev, ino)); 99 for (e = svr4_head.tqh_first; e != NULL; e = e->entries.tqe_next) 100 if (e->p == p && e->dev == dev && e->ino == ino) { 101#ifdef DIAGNOSTIC 102 if (e->cookie != NULL && e->cookie != cookie) 103 panic("svr4 socket cookie mismatch"); 104#endif 105 e->cookie = cookie; 106 DPRINTF(("%s\n", e->sock.sun_path)); 107 return &e->sock; 108 } 109 110 DPRINTF(("not found\n")); 111 return NULL; 112} 113 114 115void 116svr4_delete_socket(p, fp) 117 struct proc *p; 118 struct file *fp; 119{ 120 struct svr4_sockcache_entry *e; 121 void *cookie = ((struct socket *) fp->f_data)->so_emuldata; 122 123 if (!svr4_str_initialized) { 124 TAILQ_INIT(&svr4_head); 125 svr4_str_initialized = 1; 126 return; 127 } 128 129 for (e = svr4_head.tqh_first; e != NULL; e = e->entries.tqe_next) 130 if (e->p == p && e->cookie == cookie) { 131 TAILQ_REMOVE(&svr4_head, e, entries); 132 DPRINTF(("svr4_delete_socket: %s [%p,%d,%d]\n", 133 e->sock.sun_path, p, e->dev, e->ino)); 134 free(e, M_TEMP); 135 return; 136 } 137} 138 139 140int 141svr4_add_socket(p, path, st) 142 struct proc *p; 143 const char *path; 144 struct stat *st; 145{ 146 struct svr4_sockcache_entry *e; 147 int len, error; 148 149 if (!svr4_str_initialized) { 150 TAILQ_INIT(&svr4_head); 151 svr4_str_initialized = 1; 152 } 153 154 e = malloc(sizeof(*e), M_TEMP, M_WAITOK); 155 e->cookie = NULL; 156 e->dev = st->st_dev; 157 e->ino = st->st_ino; 158 e->p = p; 159 160 if ((error = copyinstr(path, e->sock.sun_path, 161 sizeof(e->sock.sun_path), &len)) != 0) { 162 DPRINTF(("svr4_add_socket: copyinstr failed %d\n", error)); 163 free(e, M_TEMP); 164 return error; 165 } 166 167 e->sock.sun_family = AF_LOCAL; 168 e->sock.sun_len = len; 169 170 TAILQ_INSERT_HEAD(&svr4_head, e, entries); 171 DPRINTF(("svr4_add_socket: %s [%p,%d,%d]\n", e->sock.sun_path, 172 p, e->dev, e->ino)); 173 return 0; 174} 175 176 177int 178svr4_sys_socket(p, uap) 179 struct proc *p; 180 struct svr4_sys_socket_args *uap; 181{ 182 switch (SCARG(uap, type)) { 183 case SVR4_SOCK_DGRAM: 184 SCARG(uap, type) = SOCK_DGRAM; 185 break; 186 187 case SVR4_SOCK_STREAM: 188 SCARG(uap, type) = SOCK_STREAM; 189 break; 190 191 case SVR4_SOCK_RAW: 192 SCARG(uap, type) = SOCK_RAW; 193 break; 194 195 case SVR4_SOCK_RDM: 196 SCARG(uap, type) = SOCK_RDM; 197 break; 198 199 case SVR4_SOCK_SEQPACKET: 200 SCARG(uap, type) = SOCK_SEQPACKET; 201 break; 202 default: 203 return EINVAL; 204 } 205 return socket(p, (struct socket_args *)uap); 206} 207