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)
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>
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>
31 #include <asm/uaccess.h>
32 #include <asm/termios.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,
43 #ifndef DEBUG_SOLARIS_KMALLOC
45 #define mykmalloc kmalloc
50 extern void * mykmalloc(size_t s, gfp_t gfp);
51 extern void mykfree(void *);
55 static unsigned int (*sock_poll)(struct file *, poll_table *);
57 static struct file_operations socksys_file_ops = {
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);
76 protocol = af_inet_protocols[iminor(inode) & 0xf];
78 case IPPROTO_TCP: type = SOCK_STREAM; break;
79 case IPPROTO_UDP: type = SOCK_DGRAM; break;
80 default: type = SOCK_RAW; break;
89 fd = sys_socket(family, type, protocol);
93 * N.B. The following operations are not legal!
95 * No shit. WTF is it supposed to do, anyway?
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;
112 sock->state = TS_UNBND;
114 sock->pfirst = sock->plast = NULL;
115 filp->private_data = sock;
116 SOLDD(("filp->private_data %016lx\n", filp->private_data));
123 static int socksys_release(struct inode * inode, struct file * filp)
125 struct sol_socket_struct * sock;
128 /* XXX: check this */
129 sock = (struct sol_socket_struct *)filp->private_data;
130 SOLDD(("sock release %016lx(%016lx)\n", sock, filp));
133 struct T_primsg *next = it->next;
135 SOLDD(("socksys_release %016lx->%016lx\n", it, next));
139 filp->private_data = NULL;
140 SOLDD(("socksys_release %016lx\n", sock));
141 mykfree((char*)sock);
145 static unsigned int socksys_poll(struct file * filp, poll_table * wait)
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)
161 mask |= (*sock_poll)(filp, wait);
165 static const struct file_operations socksys_fops = {
166 .open = socksys_open,
167 .release = socksys_release,
170 int __init init_socksys(void)
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);
181 printk ("Couldn't register socksys character device\n");
184 ret = sys_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
186 printk ("Couldn't create socket\n");
191 /* N.B. Is this valid? Suppose the f_ops are in a module ... */
192 socksys_file_ops = *file->f_op;
194 sock_poll = socksys_file_ops.poll;
195 socksys_file_ops.poll = socksys_poll;
196 socksys_file_ops.release = socksys_release;
200 void __exit cleanup_socksys(void)
202 if (unregister_chrdev(30, "socksys"))
203 printk ("Couldn't unregister socksys character device\n");