summaryrefslogtreecommitdiffstats
path: root/sys/compat/svr4/svr4_socket.c
blob: bd14556314e69c7fcf1b8840f4650d3439498da6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * Copyright (c) 1998 Mark Newton
 * Copyright (c) 1996 Christos Zoulas. 
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Christos Zoulas.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * $FreeBSD$
 */

/*
 * In SVR4 unix domain sockets are referenced sometimes
 * (in putmsg(2) for example) as a [device, inode] pair instead of a pathname.
 * Since there is no iname() routine in the kernel, and we need access to
 * a mapping from inode to pathname, we keep our own table. This is a simple
 * linked list that contains the pathname, the [device, inode] pair, the
 * file corresponding to that socket and the process. When the
 * socket gets closed we remove the item from the list. The list gets loaded
 * every time a stat(2) call finds a socket.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/queue.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/sysproto.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <sys/proc.h>
#include <sys/malloc.h>

#include <compat/svr4/svr4.h>
#include <compat/svr4/svr4_types.h>
#include <compat/svr4/svr4_util.h>
#include <compat/svr4/svr4_socket.h>
#include <compat/svr4/svr4_signal.h>
#include <compat/svr4/svr4_sockmod.h>
#include <compat/svr4/svr4_proto.h>

struct svr4_sockcache_entry {
	struct proc *p;		/* Process for the socket		*/
	void *cookie;		/* Internal cookie used for matching	*/
	struct sockaddr_un sock;/* Pathname for the socket		*/
	udev_t dev;		/* Device where the socket lives on	*/
	ino_t ino;		/* Inode where the socket lives on	*/
	TAILQ_ENTRY(svr4_sockcache_entry) entries;
};

extern TAILQ_HEAD(svr4_sockcache_head, svr4_sockcache_entry) svr4_head;
extern int svr4_str_initialized;

struct sockaddr_un *
svr4_find_socket(p, fp, dev, ino)
	struct proc *p;
	struct file *fp;
	udev_t dev;
	ino_t ino;
{
	struct svr4_sockcache_entry *e;
	void *cookie = ((struct socket *) fp->f_data)->so_emuldata;

	if (svr4_str_initialized != 2) {
		if (atomic_cmpset_acq_int(&svr4_str_initialized, 0, 1)) {
			DPRINTF(("svr4_find_socket: uninitialized [%p,%d,%d]\n",
			    p, dev, ino));
			TAILQ_INIT(&svr4_head);
			atomic_store_rel_int(&svr4_str_initialized, 2);
		}
		return NULL;
	}


	DPRINTF(("svr4_find_socket: [%p,%d,%d]: ", p, dev, ino));
	TAILQ_FOREACH(e, &svr4_head, entries)
		if (e->p == p && e->dev == dev && e->ino == ino) {
#ifdef DIAGNOSTIC
			if (e->cookie != NULL && e->cookie != cookie)
				panic("svr4 socket cookie mismatch");
#endif
			e->cookie = cookie;
			DPRINTF(("%s\n", e->sock.sun_path));
			return &e->sock;
		}

	DPRINTF(("not found\n"));
	return NULL;
}


/*
 * svr4_delete_socket() is in sys/dev/streams.c (because it's called by
 * the streams "soo_close()" routine).
 */
int
svr4_add_socket(p, path, st)
	struct proc *p;
	const char *path;
	struct stat *st;
{
	struct svr4_sockcache_entry *e;
	int len, error;

	/*
	 * Wait for the TAILQ to be initialized.  Only the very first CPU
	 * will succeed on the atomic_cmpset().  The other CPU's will spin
	 * until the first one finishes the initialization.  Once the
	 * initialization is complete, the condition will always fail
	 * avoiding expensive atomic operations in the common case.
	 */
	while (svr4_str_initialized != 2)
		if (atomic_cmpset_acq_int(&svr4_str_initialized, 0, 1)) {
			TAILQ_INIT(&svr4_head);
			atomic_store_rel_int(&svr4_str_initialized, 2);
		}

	e = malloc(sizeof(*e), M_TEMP, M_WAITOK);
	e->cookie = NULL;
	e->dev = st->st_dev;
	e->ino = st->st_ino;
	e->p = p;

	if ((error = copyinstr(path, e->sock.sun_path,
	    sizeof(e->sock.sun_path), &len)) != 0) {
		DPRINTF(("svr4_add_socket: copyinstr failed %d\n", error));
		free(e, M_TEMP);
		return error;
	}

	e->sock.sun_family = AF_LOCAL;
	e->sock.sun_len = len;

	TAILQ_INSERT_HEAD(&svr4_head, e, entries);
	DPRINTF(("svr4_add_socket: %s [%p,%d,%d]\n", e->sock.sun_path,
		 p, e->dev, e->ino));
	return 0;
}


int
svr4_sys_socket(p, uap)
	struct proc *p;
	struct svr4_sys_socket_args *uap;
{
	switch (SCARG(uap, type)) {
	case SVR4_SOCK_DGRAM:
		SCARG(uap, type) = SOCK_DGRAM;
		break;

	case SVR4_SOCK_STREAM:
		SCARG(uap, type) = SOCK_STREAM;
		break;

	case SVR4_SOCK_RAW:
		SCARG(uap, type) = SOCK_RAW;
		break;

	case SVR4_SOCK_RDM:
		SCARG(uap, type) = SOCK_RDM;
		break;

	case SVR4_SOCK_SEQPACKET:
		SCARG(uap, type) = SOCK_SEQPACKET;
		break;
	default:
		return EINVAL;
	}
	return socket(p, (struct socket_args *)uap);
}
OpenPOWER on IntegriCloud