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>
29 #include <linux/devfs_fs_kernel.h>
33 #include <asm/uaccess.h>
34 #include <asm/termios.h>
39 static int af_inet_protocols
[] = {
40 IPPROTO_ICMP
, IPPROTO_ICMP
, IPPROTO_IGMP
, IPPROTO_IPIP
, IPPROTO_TCP
,
41 IPPROTO_EGP
, IPPROTO_PUP
, IPPROTO_UDP
, IPPROTO_IDP
, IPPROTO_RAW
,
45 #ifndef DEBUG_SOLARIS_KMALLOC
47 #define mykmalloc kmalloc
52 extern void * mykmalloc(size_t s
, int gfp
);
53 extern void mykfree(void *);
57 static unsigned int (*sock_poll
)(struct file
*, poll_table
*);
59 static struct file_operations socksys_file_ops
= {
63 static int socksys_open(struct inode
* inode
, struct file
* filp
)
65 int family
, type
, protocol
, fd
;
66 struct dentry
*dentry
;
67 int (*sys_socket
)(int,int,int) =
68 (int (*)(int,int,int))SUNOS(97);
69 struct sol_socket_struct
* sock
;
71 family
= ((iminor(inode
) >> 4) & 0xf);
78 protocol
= af_inet_protocols
[iminor(inode
) & 0xf];
80 case IPPROTO_TCP
: type
= SOCK_STREAM
; break;
81 case IPPROTO_UDP
: type
= SOCK_DGRAM
; break;
82 default: type
= SOCK_RAW
; break;
91 fd
= sys_socket(family
, type
, protocol
);
95 * N.B. The following operations are not legal!
97 * No shit. WTF is it supposed to do, anyway?
100 * d_delete(filp->f_dentry), then d_instantiate with sock inode
102 dentry
= filp
->f_dentry
;
103 filp
->f_dentry
= dget(fcheck(fd
)->f_dentry
);
104 filp
->f_dentry
->d_inode
->i_rdev
= inode
->i_rdev
;
105 filp
->f_dentry
->d_inode
->i_flock
= inode
->i_flock
;
106 SOCKET_I(filp
->f_dentry
->d_inode
)->file
= filp
;
107 filp
->f_op
= &socksys_file_ops
;
108 sock
= (struct sol_socket_struct
*)
109 mykmalloc(sizeof(struct sol_socket_struct
), GFP_KERNEL
);
110 if (!sock
) return -ENOMEM
;
111 SOLDD(("sock=%016lx(%016lx)\n", sock
, filp
));
112 sock
->magic
= SOLARIS_SOCKET_MAGIC
;
114 sock
->state
= TS_UNBND
;
116 sock
->pfirst
= sock
->plast
= NULL
;
117 filp
->private_data
= sock
;
118 SOLDD(("filp->private_data %016lx\n", filp
->private_data
));
125 static int socksys_release(struct inode
* inode
, struct file
* filp
)
127 struct sol_socket_struct
* sock
;
130 /* XXX: check this */
131 sock
= (struct sol_socket_struct
*)filp
->private_data
;
132 SOLDD(("sock release %016lx(%016lx)\n", sock
, filp
));
135 struct T_primsg
*next
= it
->next
;
137 SOLDD(("socksys_release %016lx->%016lx\n", it
, next
));
141 filp
->private_data
= NULL
;
142 SOLDD(("socksys_release %016lx\n", sock
));
143 mykfree((char*)sock
);
147 static unsigned int socksys_poll(struct file
* filp
, poll_table
* wait
)
150 unsigned int mask
= 0;
152 ino
=filp
->f_dentry
->d_inode
;
153 if (ino
&& S_ISSOCK(ino
->i_mode
)) {
154 struct sol_socket_struct
*sock
;
155 sock
= (struct sol_socket_struct
*)filp
->private_data
;
156 if (sock
&& sock
->pfirst
) {
157 mask
|= POLLIN
| POLLRDNORM
;
158 if (sock
->pfirst
->pri
== MSG_HIPRI
)
163 mask
|= (*sock_poll
)(filp
, wait
);
167 static struct file_operations socksys_fops
= {
168 .open
= socksys_open
,
169 .release
= socksys_release
,
177 int (*sys_socket
)(int,int,int) =
178 (int (*)(int,int,int))SUNOS(97);
179 int (*sys_close
)(unsigned int) =
180 (int (*)(unsigned int))SYS(close
);
182 ret
= register_chrdev (30, "socksys", &socksys_fops
);
184 printk ("Couldn't register socksys character device\n");
187 ret
= sys_socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
189 printk ("Couldn't create socket\n");
193 devfs_mk_cdev(MKDEV(30, 0), S_IFCHR
|S_IRUSR
|S_IWUSR
, "socksys");
196 /* N.B. Is this valid? Suppose the f_ops are in a module ... */
197 socksys_file_ops
= *file
->f_op
;
199 sock_poll
= socksys_file_ops
.poll
;
200 socksys_file_ops
.poll
= socksys_poll
;
201 socksys_file_ops
.release
= socksys_release
;
206 cleanup_socksys(void)
208 if (unregister_chrdev(30, "socksys"))
209 printk ("Couldn't unregister socksys character device\n");
210 devfs_remove ("socksys");