Pull button into test branch
[pandora-kernel.git] / arch / sparc64 / solaris / socksys.c
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>
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
38 static int af_inet_protocols[] = {
39 IPPROTO_ICMP, IPPROTO_ICMP, IPPROTO_IGMP, IPPROTO_IPIP, IPPROTO_TCP,
40 IPPROTO_EGP, IPPROTO_PUP, IPPROTO_UDP, IPPROTO_IDP, IPPROTO_RAW,
41 0, 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
51 extern void * mykmalloc(size_t s, gfp_t gfp);
52 extern void mykfree(void *);
53
54 #endif
55
56 static unsigned int (*sock_poll)(struct file *, poll_table *);
57
58 static struct file_operations socksys_file_ops = {
59         /* Currently empty */
60 };
61
62 static 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_path.dentry), then d_instantiate with sock inode
100          */
101         dentry = filp->f_path.dentry;
102         filp->f_path.dentry = dget(fcheck(fd)->f_path.dentry);
103         filp->f_path.dentry->d_inode->i_rdev = inode->i_rdev;
104         filp->f_path.dentry->d_inode->i_flock = inode->i_flock;
105         SOCKET_I(filp->f_path.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
124 static 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
146 static 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_path.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         
166 static struct file_operations socksys_fops = {
167         .open =         socksys_open,
168         .release =      socksys_release,
169 };
170
171 int __init init_socksys(void)
172 {
173         int ret;
174         struct file * file;
175         int (*sys_socket)(int,int,int) =
176                 (int (*)(int,int,int))SUNOS(97);
177         int (*sys_close)(unsigned int) = 
178                 (int (*)(unsigned int))SYS(close);
179         
180         ret = register_chrdev (30, "socksys", &socksys_fops);
181         if (ret < 0) {
182                 printk ("Couldn't register socksys character device\n");
183                 return ret;
184         }
185         ret = sys_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
186         if (ret < 0) {
187                 printk ("Couldn't create socket\n");
188                 return ret;
189         }
190
191         file = fcheck(ret);
192         /* N.B. Is this valid? Suppose the f_ops are in a module ... */
193         socksys_file_ops = *file->f_op;
194         sys_close(ret);
195         sock_poll = socksys_file_ops.poll;
196         socksys_file_ops.poll = socksys_poll;
197         socksys_file_ops.release = socksys_release;
198         return 0;
199 }
200
201 void __exit cleanup_socksys(void)
202 {
203         if (unregister_chrdev(30, "socksys"))
204                 printk ("Couldn't unregister socksys character device\n");
205 }