Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / arch / sparc64 / solaris / socksys.c
blob7736411f244ff06f760a233ebc94ab8a5bf36bba
1 /* $Id: socksys.c,v 1.21 2002/02/08 03:57:14 davem Exp $
2 * socksys.c: /dev/inet/ stuff for Solaris emulation.
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 */
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
16 #include <linux/types.h>
17 #include <linux/kernel.h>
18 #include <linux/sched.h>
19 #include <linux/smp.h>
20 #include <linux/ioctl.h>
21 #include <linux/fs.h>
22 #include <linux/file.h>
23 #include <linux/init.h>
24 #include <linux/poll.h>
25 #include <linux/slab.h>
26 #include <linux/syscalls.h>
27 #include <linux/in.h>
29 #include <net/sock.h>
31 #include <asm/uaccess.h>
32 #include <asm/termios.h>
34 #include "conv.h"
35 #include "socksys.h"
37 static int af_inet_protocols[] = {
38 IPPROTO_ICMP, IPPROTO_ICMP, IPPROTO_IGMP, IPPROTO_IPIP, IPPROTO_TCP,
39 IPPROTO_EGP, IPPROTO_PUP, IPPROTO_UDP, IPPROTO_IDP, IPPROTO_RAW,
40 0, 0, 0, 0, 0, 0,
43 #ifndef DEBUG_SOLARIS_KMALLOC
45 #define mykmalloc kmalloc
46 #define mykfree kfree
48 #else
50 extern void * mykmalloc(size_t s, gfp_t gfp);
51 extern void mykfree(void *);
53 #endif
55 static unsigned int (*sock_poll)(struct file *, poll_table *);
57 static struct file_operations socksys_file_ops = {
58 /* Currently empty */
61 static int socksys_open(struct inode * inode, struct file * filp)
63 int family, type, protocol, fd;
64 struct dentry *dentry;
65 int (*sys_socket)(int,int,int) =
66 (int (*)(int,int,int))SUNOS(97);
67 struct sol_socket_struct * sock;
69 family = ((iminor(inode) >> 4) & 0xf);
70 switch (family) {
71 case AF_UNIX:
72 type = SOCK_STREAM;
73 protocol = 0;
74 break;
75 case AF_INET:
76 protocol = af_inet_protocols[iminor(inode) & 0xf];
77 switch (protocol) {
78 case IPPROTO_TCP: type = SOCK_STREAM; break;
79 case IPPROTO_UDP: type = SOCK_DGRAM; break;
80 default: type = SOCK_RAW; break;
82 break;
83 default:
84 type = SOCK_RAW;
85 protocol = 0;
86 break;
89 fd = sys_socket(family, type, protocol);
90 if (fd < 0)
91 return fd;
93 * N.B. The following operations are not legal!
95 * No shit. WTF is it supposed to do, anyway?
97 * Try instead:
98 * d_delete(filp->f_path.dentry), then d_instantiate with sock inode
100 dentry = filp->f_path.dentry;
101 filp->f_path.dentry = dget(fcheck(fd)->f_path.dentry);
102 filp->f_path.dentry->d_inode->i_rdev = inode->i_rdev;
103 filp->f_path.dentry->d_inode->i_flock = inode->i_flock;
104 SOCKET_I(filp->f_path.dentry->d_inode)->file = filp;
105 filp->f_op = &socksys_file_ops;
106 sock = (struct sol_socket_struct*)
107 mykmalloc(sizeof(struct sol_socket_struct), GFP_KERNEL);
108 if (!sock) return -ENOMEM;
109 SOLDD(("sock=%016lx(%016lx)\n", sock, filp));
110 sock->magic = SOLARIS_SOCKET_MAGIC;
111 sock->modcount = 0;
112 sock->state = TS_UNBND;
113 sock->offset = 0;
114 sock->pfirst = sock->plast = NULL;
115 filp->private_data = sock;
116 SOLDD(("filp->private_data %016lx\n", filp->private_data));
118 sys_close(fd);
119 dput(dentry);
120 return 0;
123 static int socksys_release(struct inode * inode, struct file * filp)
125 struct sol_socket_struct * sock;
126 struct T_primsg *it;
128 /* XXX: check this */
129 sock = (struct sol_socket_struct *)filp->private_data;
130 SOLDD(("sock release %016lx(%016lx)\n", sock, filp));
131 it = sock->pfirst;
132 while (it) {
133 struct T_primsg *next = it->next;
135 SOLDD(("socksys_release %016lx->%016lx\n", it, next));
136 mykfree((char*)it);
137 it = next;
139 filp->private_data = NULL;
140 SOLDD(("socksys_release %016lx\n", sock));
141 mykfree((char*)sock);
142 return 0;
145 static unsigned int socksys_poll(struct file * filp, poll_table * wait)
147 struct inode *ino;
148 unsigned int mask = 0;
150 ino=filp->f_path.dentry->d_inode;
151 if (ino && S_ISSOCK(ino->i_mode)) {
152 struct sol_socket_struct *sock;
153 sock = (struct sol_socket_struct*)filp->private_data;
154 if (sock && sock->pfirst) {
155 mask |= POLLIN | POLLRDNORM;
156 if (sock->pfirst->pri == MSG_HIPRI)
157 mask |= POLLPRI;
160 if (sock_poll)
161 mask |= (*sock_poll)(filp, wait);
162 return mask;
165 static const struct file_operations socksys_fops = {
166 .open = socksys_open,
167 .release = socksys_release,
170 int __init init_socksys(void)
172 int ret;
173 struct file * file;
174 int (*sys_socket)(int,int,int) =
175 (int (*)(int,int,int))SUNOS(97);
176 int (*sys_close)(unsigned int) =
177 (int (*)(unsigned int))SYS(close);
179 ret = register_chrdev (30, "socksys", &socksys_fops);
180 if (ret < 0) {
181 printk ("Couldn't register socksys character device\n");
182 return ret;
184 ret = sys_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
185 if (ret < 0) {
186 printk ("Couldn't create socket\n");
187 return ret;
190 file = fcheck(ret);
191 /* N.B. Is this valid? Suppose the f_ops are in a module ... */
192 socksys_file_ops = *file->f_op;
193 sys_close(ret);
194 sock_poll = socksys_file_ops.poll;
195 socksys_file_ops.poll = socksys_poll;
196 socksys_file_ops.release = socksys_release;
197 return 0;
200 void __exit cleanup_socksys(void)
202 unregister_chrdev(30, "socksys");