1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2015, Joyent Inc.
25  * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
26  */
27 
28 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29 /*	All Rights Reserved */
30 
31 /*
32  * The kTLI "shim" over in ./fake_ktli.c uses getf(), releasef() to
33  * represent an open socket FD in "fake" vnode_t and file_t objects.
34  * This implements minimal getf()/releasef() shims for that purpose.
35  */
36 
37 #include <sys/types.h>
38 #include <sys/sysmacros.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/errno.h>
42 #include <sys/cred.h>
43 #include <sys/user.h>
44 #include <sys/vfs.h>
45 #include <sys/vnode.h>
46 #include <sys/file.h>
47 #include <sys/debug.h>
48 #include <sys/kmem.h>
49 
50 #define	FAKEFDS	256
51 
52 kmutex_t ftlock;
53 file_t *ftab[FAKEFDS];
54 
55 file_t *
getf(int fd)56 getf(int fd)
57 {
58 	file_t *fp;
59 	vnode_t *vp;
60 
61 	if (fd >= FAKEFDS)
62 		return (NULL);
63 
64 	mutex_enter(&ftlock);
65 	if ((fp = ftab[fd]) != NULL) {
66 		fp->f_count++;
67 		mutex_exit(&ftlock);
68 		return (fp);
69 	}
70 
71 	fp = kmem_zalloc(sizeof (*fp), KM_SLEEP);
72 	vp = kmem_zalloc(sizeof (*vp), KM_SLEEP);
73 	vp->v_fd = fd;
74 	fp->f_vnode = vp;
75 	fp->f_count = 1;
76 	ftab[fd] = fp;
77 
78 	mutex_exit(&ftlock);
79 
80 	return (fp);
81 }
82 
83 void
releasef(int fd)84 releasef(int fd)
85 {
86 	file_t *fp;
87 	vnode_t *vp;
88 
89 	mutex_enter(&ftlock);
90 	if ((fp = ftab[fd]) == NULL) {
91 		mutex_exit(&ftlock);
92 		return;
93 	}
94 	fp->f_count--;
95 	if (fp->f_count > 0) {
96 		mutex_exit(&ftlock);
97 		return;
98 	}
99 	ftab[fd] = NULL;
100 	mutex_exit(&ftlock);
101 
102 	vp = fp->f_vnode;
103 	kmem_free(vp, sizeof (*vp));
104 	kmem_free(fp, sizeof (*fp));
105 }
106