Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | #include <linux/devfs_fs_kernel.h> |
| 30 | |
| 31 | #include <net/sock.h> |
| 32 | |
| 33 | #include <asm/uaccess.h> |
| 34 | #include <asm/termios.h> |
| 35 | |
| 36 | #include "conv.h" |
| 37 | #include "socksys.h" |
| 38 | |
| 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, |
| 42 | 0, 0, 0, 0, 0, 0, |
| 43 | }; |
| 44 | |
| 45 | #ifndef DEBUG_SOLARIS_KMALLOC |
| 46 | |
| 47 | #define mykmalloc kmalloc |
| 48 | #define mykfree kfree |
| 49 | |
| 50 | #else |
| 51 | |
Al Viro | 53f9fc9 | 2005-10-21 03:22:24 -0400 | [diff] [blame] | 52 | extern void * mykmalloc(size_t s, gfp_t gfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | extern void mykfree(void *); |
| 54 | |
| 55 | #endif |
| 56 | |
| 57 | static unsigned int (*sock_poll)(struct file *, poll_table *); |
| 58 | |
| 59 | static struct file_operations socksys_file_ops = { |
| 60 | /* Currently empty */ |
| 61 | }; |
| 62 | |
| 63 | static int socksys_open(struct inode * inode, struct file * filp) |
| 64 | { |
| 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; |
| 70 | |
| 71 | family = ((iminor(inode) >> 4) & 0xf); |
| 72 | switch (family) { |
| 73 | case AF_UNIX: |
| 74 | type = SOCK_STREAM; |
| 75 | protocol = 0; |
| 76 | break; |
| 77 | case AF_INET: |
| 78 | protocol = af_inet_protocols[iminor(inode) & 0xf]; |
| 79 | switch (protocol) { |
| 80 | case IPPROTO_TCP: type = SOCK_STREAM; break; |
| 81 | case IPPROTO_UDP: type = SOCK_DGRAM; break; |
| 82 | default: type = SOCK_RAW; break; |
| 83 | } |
| 84 | break; |
| 85 | default: |
| 86 | type = SOCK_RAW; |
| 87 | protocol = 0; |
| 88 | break; |
| 89 | } |
| 90 | |
| 91 | fd = sys_socket(family, type, protocol); |
| 92 | if (fd < 0) |
| 93 | return fd; |
| 94 | /* |
| 95 | * N.B. The following operations are not legal! |
| 96 | * |
| 97 | * No shit. WTF is it supposed to do, anyway? |
| 98 | * |
| 99 | * Try instead: |
| 100 | * d_delete(filp->f_dentry), then d_instantiate with sock inode |
| 101 | */ |
| 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; |
| 113 | sock->modcount = 0; |
| 114 | sock->state = TS_UNBND; |
| 115 | sock->offset = 0; |
| 116 | sock->pfirst = sock->plast = NULL; |
| 117 | filp->private_data = sock; |
| 118 | SOLDD(("filp->private_data %016lx\n", filp->private_data)); |
| 119 | |
| 120 | sys_close(fd); |
| 121 | dput(dentry); |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static int socksys_release(struct inode * inode, struct file * filp) |
| 126 | { |
| 127 | struct sol_socket_struct * sock; |
| 128 | struct T_primsg *it; |
| 129 | |
| 130 | /* XXX: check this */ |
| 131 | sock = (struct sol_socket_struct *)filp->private_data; |
| 132 | SOLDD(("sock release %016lx(%016lx)\n", sock, filp)); |
| 133 | it = sock->pfirst; |
| 134 | while (it) { |
| 135 | struct T_primsg *next = it->next; |
| 136 | |
| 137 | SOLDD(("socksys_release %016lx->%016lx\n", it, next)); |
| 138 | mykfree((char*)it); |
| 139 | it = next; |
| 140 | } |
| 141 | filp->private_data = NULL; |
| 142 | SOLDD(("socksys_release %016lx\n", sock)); |
| 143 | mykfree((char*)sock); |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | static unsigned int socksys_poll(struct file * filp, poll_table * wait) |
| 148 | { |
| 149 | struct inode *ino; |
| 150 | unsigned int mask = 0; |
| 151 | |
| 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) |
| 159 | mask |= POLLPRI; |
| 160 | } |
| 161 | } |
| 162 | if (sock_poll) |
| 163 | mask |= (*sock_poll)(filp, wait); |
| 164 | return mask; |
| 165 | } |
| 166 | |
| 167 | static struct file_operations socksys_fops = { |
| 168 | .open = socksys_open, |
| 169 | .release = socksys_release, |
| 170 | }; |
| 171 | |
| 172 | int __init |
| 173 | init_socksys(void) |
| 174 | { |
| 175 | int ret; |
| 176 | struct file * file; |
| 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); |
| 181 | |
| 182 | ret = register_chrdev (30, "socksys", &socksys_fops); |
| 183 | if (ret < 0) { |
| 184 | printk ("Couldn't register socksys character device\n"); |
| 185 | return ret; |
| 186 | } |
| 187 | ret = sys_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 188 | if (ret < 0) { |
| 189 | printk ("Couldn't create socket\n"); |
| 190 | return ret; |
| 191 | } |
| 192 | |
| 193 | devfs_mk_cdev(MKDEV(30, 0), S_IFCHR|S_IRUSR|S_IWUSR, "socksys"); |
| 194 | |
| 195 | file = fcheck(ret); |
| 196 | /* N.B. Is this valid? Suppose the f_ops are in a module ... */ |
| 197 | socksys_file_ops = *file->f_op; |
| 198 | sys_close(ret); |
| 199 | sock_poll = socksys_file_ops.poll; |
| 200 | socksys_file_ops.poll = socksys_poll; |
| 201 | socksys_file_ops.release = socksys_release; |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | void |
| 206 | cleanup_socksys(void) |
| 207 | { |
| 208 | if (unregister_chrdev(30, "socksys")) |
| 209 | printk ("Couldn't unregister socksys character device\n"); |
| 210 | devfs_remove ("socksys"); |
| 211 | } |