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/smp_lock.h>
21 #include <linux/ioctl.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>
32 #include <asm/uaccess.h>
33 #include <asm/termios.h>
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,
44 #ifndef DEBUG_SOLARIS_KMALLOC
46 #define mykmalloc kmalloc
51 extern void * mykmalloc(size_t s, gfp_t gfp);
52 extern void mykfree(void *);
56 static unsigned int (*sock_poll)(struct file *, poll_table *);
58 static struct file_operations socksys_file_ops = {
62 static int socksys_open(struct inode * inode, struct file * filp)
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;
70 family = ((iminor(inode) >> 4) & 0xf);
77 protocol = af_inet_protocols[iminor(inode) & 0xf];
79 case IPPROTO_TCP: type = SOCK_STREAM; break;
80 case IPPROTO_UDP: type = SOCK_DGRAM; break;
81 default: type = SOCK_RAW; break;
90 fd = sys_socket(family, type, protocol);
94 * N.B. The following operations are not legal!
96 * No shit. WTF is it supposed to do, anyway?
99 * d_delete(filp->f_dentry), then d_instantiate with sock inode
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;
113 sock->state = TS_UNBND;
115 sock->pfirst = sock->plast = NULL;
116 filp->private_data = sock;
117 SOLDD(("filp->private_data %016lx\n", filp->private_data));
124 static int socksys_release(struct inode * inode, struct file * filp)
126 struct sol_socket_struct * sock;
129 /* XXX: check this */
130 sock = (struct sol_socket_struct *)filp->private_data;
131 SOLDD(("sock release %016lx(%016lx)\n", sock, filp));
134 struct T_primsg *next = it->next;
136 SOLDD(("socksys_release %016lx->%016lx\n", it, next));
140 filp->private_data = NULL;
141 SOLDD(("socksys_release %016lx\n", sock));
142 mykfree((char*)sock);
146 static unsigned int socksys_poll(struct file * filp, poll_table * wait)
149 unsigned int mask = 0;
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)
162 mask |= (*sock_poll)(filp, wait);
166 static struct file_operations socksys_fops = {
167 .open = socksys_open,
168 .release = socksys_release,
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);
181 ret = register_chrdev (30, "socksys", &socksys_fops);
183 printk ("Couldn't register socksys character device\n");
186 ret = sys_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
188 printk ("Couldn't create socket\n");
193 /* N.B. Is this valid? Suppose the f_ops are in a module ... */
194 socksys_file_ops = *file->f_op;
196 sock_poll = socksys_file_ops.poll;
197 socksys_file_ops.poll = socksys_poll;
198 socksys_file_ops.release = socksys_release;
203 cleanup_socksys(void)
205 if (unregister_chrdev(30, "socksys"))
206 printk ("Couldn't unregister socksys character device\n");