Merge git://git.kernel.org/pub/scm/linux/kernel/git/sfrench/cifs-2.6
[linux-block.git] / arch / sparc64 / solaris / socksys.c
CommitLineData
1da177e4
LT
1/* $Id: socksys.c,v 1.21 2002/02/08 03:57:14 davem Exp $
2 * socksys.c: /dev/inet/ stuff for Solaris emulation.
3 *
4 * Copyright (C) 1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
5 * Copyright (C) 1997, 1998 Patrik Rak (prak3264@ss1000.ms.mff.cuni.cz)
6 * Copyright (C) 1995, 1996 Mike Jagdis (jaggy@purplet.demon.co.uk)
7 */
8
9/*
10 * Dave, _please_ give me specifications on this fscking mess so that I
11 * could at least get it into the state when it wouldn't screw the rest of
12 * the kernel over. socksys.c and timod.c _stink_ and we are not talking
13 * H2S here, it's isopropilmercaptan in concentrations way over LD50. -- AV
14 */
15
16#include <linux/types.h>
17#include <linux/kernel.h>
18#include <linux/sched.h>
19#include <linux/smp.h>
20#include <linux/smp_lock.h>
21#include <linux/ioctl.h>
22#include <linux/fs.h>
23#include <linux/file.h>
24#include <linux/init.h>
25#include <linux/poll.h>
26#include <linux/slab.h>
27#include <linux/syscalls.h>
28#include <linux/in.h>
1da177e4
LT
29
30#include <net/sock.h>
31
32#include <asm/uaccess.h>
33#include <asm/termios.h>
34
35#include "conv.h"
36#include "socksys.h"
37
38static int af_inet_protocols[] = {
39IPPROTO_ICMP, IPPROTO_ICMP, IPPROTO_IGMP, IPPROTO_IPIP, IPPROTO_TCP,
40IPPROTO_EGP, IPPROTO_PUP, IPPROTO_UDP, IPPROTO_IDP, IPPROTO_RAW,
410, 0, 0, 0, 0, 0,
42};
43
44#ifndef DEBUG_SOLARIS_KMALLOC
45
46#define mykmalloc kmalloc
47#define mykfree kfree
48
49#else
50
53f9fc93 51extern void * mykmalloc(size_t s, gfp_t gfp);
1da177e4
LT
52extern void mykfree(void *);
53
54#endif
55
56static unsigned int (*sock_poll)(struct file *, poll_table *);
57
58static struct file_operations socksys_file_ops = {
59 /* Currently empty */
60};
61
62static int socksys_open(struct inode * inode, struct file * filp)
63{
64 int family, type, protocol, fd;
65 struct dentry *dentry;
66 int (*sys_socket)(int,int,int) =
67 (int (*)(int,int,int))SUNOS(97);
68 struct sol_socket_struct * sock;
69
70 family = ((iminor(inode) >> 4) & 0xf);
71 switch (family) {
72 case AF_UNIX:
73 type = SOCK_STREAM;
74 protocol = 0;
75 break;
76 case AF_INET:
77 protocol = af_inet_protocols[iminor(inode) & 0xf];
78 switch (protocol) {
79 case IPPROTO_TCP: type = SOCK_STREAM; break;
80 case IPPROTO_UDP: type = SOCK_DGRAM; break;
81 default: type = SOCK_RAW; break;
82 }
83 break;
84 default:
85 type = SOCK_RAW;
86 protocol = 0;
87 break;
88 }
89
90 fd = sys_socket(family, type, protocol);
91 if (fd < 0)
92 return fd;
93 /*
94 * N.B. The following operations are not legal!
95 *
96 * No shit. WTF is it supposed to do, anyway?
97 *
98 * Try instead:
99 * d_delete(filp->f_dentry), then d_instantiate with sock inode
100 */
101 dentry = filp->f_dentry;
102 filp->f_dentry = dget(fcheck(fd)->f_dentry);
103 filp->f_dentry->d_inode->i_rdev = inode->i_rdev;
104 filp->f_dentry->d_inode->i_flock = inode->i_flock;
105 SOCKET_I(filp->f_dentry->d_inode)->file = filp;
106 filp->f_op = &socksys_file_ops;
107 sock = (struct sol_socket_struct*)
108 mykmalloc(sizeof(struct sol_socket_struct), GFP_KERNEL);
109 if (!sock) return -ENOMEM;
110 SOLDD(("sock=%016lx(%016lx)\n", sock, filp));
111 sock->magic = SOLARIS_SOCKET_MAGIC;
112 sock->modcount = 0;
113 sock->state = TS_UNBND;
114 sock->offset = 0;
115 sock->pfirst = sock->plast = NULL;
116 filp->private_data = sock;
117 SOLDD(("filp->private_data %016lx\n", filp->private_data));
118
119 sys_close(fd);
120 dput(dentry);
121 return 0;
122}
123
124static int socksys_release(struct inode * inode, struct file * filp)
125{
126 struct sol_socket_struct * sock;
127 struct T_primsg *it;
128
129 /* XXX: check this */
130 sock = (struct sol_socket_struct *)filp->private_data;
131 SOLDD(("sock release %016lx(%016lx)\n", sock, filp));
132 it = sock->pfirst;
133 while (it) {
134 struct T_primsg *next = it->next;
135
136 SOLDD(("socksys_release %016lx->%016lx\n", it, next));
137 mykfree((char*)it);
138 it = next;
139 }
140 filp->private_data = NULL;
141 SOLDD(("socksys_release %016lx\n", sock));
142 mykfree((char*)sock);
143 return 0;
144}
145
146static unsigned int socksys_poll(struct file * filp, poll_table * wait)
147{
148 struct inode *ino;
149 unsigned int mask = 0;
150
151 ino=filp->f_dentry->d_inode;
152 if (ino && S_ISSOCK(ino->i_mode)) {
153 struct sol_socket_struct *sock;
154 sock = (struct sol_socket_struct*)filp->private_data;
155 if (sock && sock->pfirst) {
156 mask |= POLLIN | POLLRDNORM;
157 if (sock->pfirst->pri == MSG_HIPRI)
158 mask |= POLLPRI;
159 }
160 }
161 if (sock_poll)
162 mask |= (*sock_poll)(filp, wait);
163 return mask;
164}
165
166static struct file_operations socksys_fops = {
167 .open = socksys_open,
168 .release = socksys_release,
169};
170
171int __init
172init_socksys(void)
173{
174 int ret;
175 struct file * file;
176 int (*sys_socket)(int,int,int) =
177 (int (*)(int,int,int))SUNOS(97);
178 int (*sys_close)(unsigned int) =
179 (int (*)(unsigned int))SYS(close);
180
181 ret = register_chrdev (30, "socksys", &socksys_fops);
182 if (ret < 0) {
183 printk ("Couldn't register socksys character device\n");
184 return ret;
185 }
186 ret = sys_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
187 if (ret < 0) {
188 printk ("Couldn't create socket\n");
189 return ret;
190 }
191
1da177e4
LT
192 file = fcheck(ret);
193 /* N.B. Is this valid? Suppose the f_ops are in a module ... */
194 socksys_file_ops = *file->f_op;
195 sys_close(ret);
196 sock_poll = socksys_file_ops.poll;
197 socksys_file_ops.poll = socksys_poll;
198 socksys_file_ops.release = socksys_release;
199 return 0;
200}
201
202void
203cleanup_socksys(void)
204{
205 if (unregister_chrdev(30, "socksys"))
206 printk ("Couldn't unregister socksys character device\n");
1da177e4 207}