Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * POSIX message queues filesystem for Linux. |
| 3 | * |
| 4 | * Copyright (C) 2003,2004 Krzysztof Benedyczak (golbi@mat.uni.torun.pl) |
Michal Wronski | f66e928 | 2006-10-03 23:23:27 +0200 | [diff] [blame] | 5 | * Michal Wronski (michal.wronski@gmail.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * Spinlocks: Mohamed Abbas (abbas.mohamed@intel.com) |
| 8 | * Lockless receive & send, fd based notify: |
| 9 | * Manfred Spraul (manfred@colorfullife.com) |
| 10 | * |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 11 | * Audit: George Wilson (ltcgcw@us.ibm.com) |
| 12 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * This file is released under the GPL. |
| 14 | */ |
| 15 | |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 16 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/init.h> |
| 18 | #include <linux/pagemap.h> |
| 19 | #include <linux/file.h> |
| 20 | #include <linux/mount.h> |
| 21 | #include <linux/namei.h> |
| 22 | #include <linux/sysctl.h> |
| 23 | #include <linux/poll.h> |
| 24 | #include <linux/mqueue.h> |
| 25 | #include <linux/msg.h> |
| 26 | #include <linux/skbuff.h> |
| 27 | #include <linux/netlink.h> |
| 28 | #include <linux/syscalls.h> |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 29 | #include <linux/audit.h> |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 30 | #include <linux/signal.h> |
Ingo Molnar | 5f921ae | 2006-03-26 01:37:17 -0800 | [diff] [blame] | 31 | #include <linux/mutex.h> |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 32 | #include <linux/nsproxy.h> |
| 33 | #include <linux/pid.h> |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 34 | #include <linux/ipc_namespace.h> |
Ingo Molnar | 5f921ae | 2006-03-26 01:37:17 -0800 | [diff] [blame] | 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <net/sock.h> |
| 37 | #include "util.h" |
| 38 | |
| 39 | #define MQUEUE_MAGIC 0x19800202 |
| 40 | #define DIRENT_SIZE 20 |
| 41 | #define FILENT_SIZE 80 |
| 42 | |
| 43 | #define SEND 0 |
| 44 | #define RECV 1 |
| 45 | |
| 46 | #define STATE_NONE 0 |
| 47 | #define STATE_PENDING 1 |
| 48 | #define STATE_READY 2 |
| 49 | |
Joe Korty | b231cca4 | 2008-10-18 20:28:32 -0700 | [diff] [blame] | 50 | /* |
| 51 | * Define the ranges various user-specified maximum values can |
| 52 | * be set to. |
| 53 | */ |
| 54 | #define MIN_MSGMAX 1 /* min value for msg_max */ |
| 55 | #define MAX_MSGMAX HARD_MSGMAX /* max value for msg_max */ |
| 56 | #define MIN_MSGSIZEMAX 128 /* min value for msgsize_max */ |
| 57 | #define MAX_MSGSIZEMAX (8192*128) /* max value for msgsize_max */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | struct ext_wait_queue { /* queue of sleeping tasks */ |
| 60 | struct task_struct *task; |
| 61 | struct list_head list; |
| 62 | struct msg_msg *msg; /* ptr of loaded message */ |
| 63 | int state; /* one of STATE_* values */ |
| 64 | }; |
| 65 | |
| 66 | struct mqueue_inode_info { |
| 67 | spinlock_t lock; |
| 68 | struct inode vfs_inode; |
| 69 | wait_queue_head_t wait_q; |
| 70 | |
| 71 | struct msg_msg **messages; |
| 72 | struct mq_attr attr; |
| 73 | |
| 74 | struct sigevent notify; |
Cedric Le Goater | a03fcb7 | 2006-10-02 02:17:26 -0700 | [diff] [blame] | 75 | struct pid* notify_owner; |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 76 | struct user_struct *user; /* user who created, for accounting */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | struct sock *notify_sock; |
| 78 | struct sk_buff *notify_cookie; |
| 79 | |
| 80 | /* for tasks waiting for free space and messages, respectively */ |
| 81 | struct ext_wait_queue e_wait_q[2]; |
| 82 | |
| 83 | unsigned long qsize; /* size of queue in memory (sum of all msgs) */ |
| 84 | }; |
| 85 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 86 | static const struct inode_operations mqueue_dir_inode_operations; |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 87 | static const struct file_operations mqueue_file_operations; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | static struct super_operations mqueue_super_ops; |
| 89 | static void remove_notification(struct mqueue_inode_info *info); |
| 90 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 91 | static struct kmem_cache *mqueue_inode_cachep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
| 93 | static struct ctl_table_header * mq_sysctl_table; |
| 94 | |
| 95 | static inline struct mqueue_inode_info *MQUEUE_I(struct inode *inode) |
| 96 | { |
| 97 | return container_of(inode, struct mqueue_inode_info, vfs_inode); |
| 98 | } |
| 99 | |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame^] | 100 | /* |
| 101 | * This routine should be called with the mq_lock held. |
| 102 | */ |
| 103 | static inline struct ipc_namespace *__get_ns_from_inode(struct inode *inode) |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 104 | { |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame^] | 105 | return get_ipc_ns(inode->i_sb->s_fs_info); |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame^] | 108 | static struct ipc_namespace *get_ns_from_inode(struct inode *inode) |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 109 | { |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame^] | 110 | struct ipc_namespace *ns; |
| 111 | |
| 112 | spin_lock(&mq_lock); |
| 113 | ns = __get_ns_from_inode(inode); |
| 114 | spin_unlock(&mq_lock); |
| 115 | return ns; |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame^] | 118 | static struct inode *mqueue_get_inode(struct super_block *sb, |
| 119 | struct ipc_namespace *ipc_ns, int mode, |
| 120 | struct mq_attr *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | { |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 122 | struct user_struct *u = current_user(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | struct inode *inode; |
| 124 | |
| 125 | inode = new_inode(sb); |
| 126 | if (inode) { |
| 127 | inode->i_mode = mode; |
David Howells | 414c070 | 2008-11-14 10:39:06 +1100 | [diff] [blame] | 128 | inode->i_uid = current_fsuid(); |
| 129 | inode->i_gid = current_fsgid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | inode->i_mtime = inode->i_ctime = inode->i_atime = |
| 131 | CURRENT_TIME; |
| 132 | |
| 133 | if (S_ISREG(mode)) { |
| 134 | struct mqueue_inode_info *info; |
| 135 | struct task_struct *p = current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | unsigned long mq_bytes, mq_msg_tblsz; |
| 137 | |
| 138 | inode->i_fop = &mqueue_file_operations; |
| 139 | inode->i_size = FILENT_SIZE; |
| 140 | /* mqueue specific info */ |
| 141 | info = MQUEUE_I(inode); |
| 142 | spin_lock_init(&info->lock); |
| 143 | init_waitqueue_head(&info->wait_q); |
| 144 | INIT_LIST_HEAD(&info->e_wait_q[0].list); |
| 145 | INIT_LIST_HEAD(&info->e_wait_q[1].list); |
| 146 | info->messages = NULL; |
Cedric Le Goater | a03fcb7 | 2006-10-02 02:17:26 -0700 | [diff] [blame] | 147 | info->notify_owner = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | info->qsize = 0; |
| 149 | info->user = NULL; /* set when all is ok */ |
| 150 | memset(&info->attr, 0, sizeof(info->attr)); |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 151 | info->attr.mq_maxmsg = ipc_ns->mq_msg_max; |
| 152 | info->attr.mq_msgsize = ipc_ns->mq_msgsize_max; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | if (attr) { |
| 154 | info->attr.mq_maxmsg = attr->mq_maxmsg; |
| 155 | info->attr.mq_msgsize = attr->mq_msgsize; |
| 156 | } |
| 157 | mq_msg_tblsz = info->attr.mq_maxmsg * sizeof(struct msg_msg *); |
| 158 | mq_bytes = (mq_msg_tblsz + |
| 159 | (info->attr.mq_maxmsg * info->attr.mq_msgsize)); |
| 160 | |
| 161 | spin_lock(&mq_lock); |
| 162 | if (u->mq_bytes + mq_bytes < u->mq_bytes || |
| 163 | u->mq_bytes + mq_bytes > |
| 164 | p->signal->rlim[RLIMIT_MSGQUEUE].rlim_cur) { |
| 165 | spin_unlock(&mq_lock); |
| 166 | goto out_inode; |
| 167 | } |
| 168 | u->mq_bytes += mq_bytes; |
| 169 | spin_unlock(&mq_lock); |
| 170 | |
| 171 | info->messages = kmalloc(mq_msg_tblsz, GFP_KERNEL); |
| 172 | if (!info->messages) { |
| 173 | spin_lock(&mq_lock); |
| 174 | u->mq_bytes -= mq_bytes; |
| 175 | spin_unlock(&mq_lock); |
| 176 | goto out_inode; |
| 177 | } |
| 178 | /* all is ok */ |
| 179 | info->user = get_uid(u); |
| 180 | } else if (S_ISDIR(mode)) { |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 181 | inc_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | /* Some things misbehave if size == 0 on a directory */ |
| 183 | inode->i_size = 2 * DIRENT_SIZE; |
| 184 | inode->i_op = &mqueue_dir_inode_operations; |
| 185 | inode->i_fop = &simple_dir_operations; |
| 186 | } |
| 187 | } |
| 188 | return inode; |
| 189 | out_inode: |
| 190 | make_bad_inode(inode); |
| 191 | iput(inode); |
| 192 | return NULL; |
| 193 | } |
| 194 | |
| 195 | static int mqueue_fill_super(struct super_block *sb, void *data, int silent) |
| 196 | { |
| 197 | struct inode *inode; |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame^] | 198 | struct ipc_namespace *ns = data; |
| 199 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
| 201 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 202 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 203 | sb->s_magic = MQUEUE_MAGIC; |
| 204 | sb->s_op = &mqueue_super_ops; |
| 205 | |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame^] | 206 | inode = mqueue_get_inode(sb, ns, S_IFDIR | S_ISVTX | S_IRWXUGO, |
| 207 | NULL); |
| 208 | if (!inode) { |
| 209 | error = -ENOMEM; |
| 210 | goto out; |
| 211 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
| 213 | sb->s_root = d_alloc_root(inode); |
| 214 | if (!sb->s_root) { |
| 215 | iput(inode); |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame^] | 216 | error = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame^] | 219 | out: |
| 220 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |
| 222 | |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 223 | static int mqueue_get_sb(struct file_system_type *fs_type, |
| 224 | int flags, const char *dev_name, |
| 225 | void *data, struct vfsmount *mnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | { |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame^] | 227 | if (!(flags & MS_KERNMOUNT)) |
| 228 | data = current->nsproxy->ipc_ns; |
| 229 | return get_sb_ns(fs_type, flags, data, mqueue_fill_super, mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Alexey Dobriyan | 51cc506 | 2008-07-25 19:45:34 -0700 | [diff] [blame] | 232 | static void init_once(void *foo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | { |
| 234 | struct mqueue_inode_info *p = (struct mqueue_inode_info *) foo; |
| 235 | |
Christoph Lameter | a35afb8 | 2007-05-16 22:10:57 -0700 | [diff] [blame] | 236 | inode_init_once(&p->vfs_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static struct inode *mqueue_alloc_inode(struct super_block *sb) |
| 240 | { |
| 241 | struct mqueue_inode_info *ei; |
| 242 | |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 243 | ei = kmem_cache_alloc(mqueue_inode_cachep, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | if (!ei) |
| 245 | return NULL; |
| 246 | return &ei->vfs_inode; |
| 247 | } |
| 248 | |
| 249 | static void mqueue_destroy_inode(struct inode *inode) |
| 250 | { |
| 251 | kmem_cache_free(mqueue_inode_cachep, MQUEUE_I(inode)); |
| 252 | } |
| 253 | |
| 254 | static void mqueue_delete_inode(struct inode *inode) |
| 255 | { |
| 256 | struct mqueue_inode_info *info; |
| 257 | struct user_struct *user; |
| 258 | unsigned long mq_bytes; |
| 259 | int i; |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame^] | 260 | struct ipc_namespace *ipc_ns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
| 262 | if (S_ISDIR(inode->i_mode)) { |
| 263 | clear_inode(inode); |
| 264 | return; |
| 265 | } |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame^] | 266 | ipc_ns = get_ns_from_inode(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | info = MQUEUE_I(inode); |
| 268 | spin_lock(&info->lock); |
| 269 | for (i = 0; i < info->attr.mq_curmsgs; i++) |
| 270 | free_msg(info->messages[i]); |
| 271 | kfree(info->messages); |
| 272 | spin_unlock(&info->lock); |
| 273 | |
| 274 | clear_inode(inode); |
| 275 | |
| 276 | mq_bytes = (info->attr.mq_maxmsg * sizeof(struct msg_msg *) + |
| 277 | (info->attr.mq_maxmsg * info->attr.mq_msgsize)); |
| 278 | user = info->user; |
| 279 | if (user) { |
| 280 | spin_lock(&mq_lock); |
| 281 | user->mq_bytes -= mq_bytes; |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame^] | 282 | /* |
| 283 | * get_ns_from_inode() ensures that the |
| 284 | * (ipc_ns = sb->s_fs_info) is either a valid ipc_ns |
| 285 | * to which we now hold a reference, or it is NULL. |
| 286 | * We can't put it here under mq_lock, though. |
| 287 | */ |
| 288 | if (ipc_ns) |
| 289 | ipc_ns->mq_queues_count--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | spin_unlock(&mq_lock); |
| 291 | free_uid(user); |
| 292 | } |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame^] | 293 | if (ipc_ns) |
| 294 | put_ipc_ns(ipc_ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | static int mqueue_create(struct inode *dir, struct dentry *dentry, |
| 298 | int mode, struct nameidata *nd) |
| 299 | { |
| 300 | struct inode *inode; |
| 301 | struct mq_attr *attr = dentry->d_fsdata; |
| 302 | int error; |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame^] | 303 | struct ipc_namespace *ipc_ns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
| 305 | spin_lock(&mq_lock); |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame^] | 306 | ipc_ns = __get_ns_from_inode(dir); |
| 307 | if (!ipc_ns) { |
| 308 | error = -EACCES; |
| 309 | goto out_unlock; |
| 310 | } |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 311 | if (ipc_ns->mq_queues_count >= ipc_ns->mq_queues_max && |
| 312 | !capable(CAP_SYS_RESOURCE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | error = -ENOSPC; |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 314 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | } |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 316 | ipc_ns->mq_queues_count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | spin_unlock(&mq_lock); |
| 318 | |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame^] | 319 | inode = mqueue_get_inode(dir->i_sb, ipc_ns, mode, attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | if (!inode) { |
| 321 | error = -ENOMEM; |
| 322 | spin_lock(&mq_lock); |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 323 | ipc_ns->mq_queues_count--; |
| 324 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame^] | 327 | put_ipc_ns(ipc_ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | dir->i_size += DIRENT_SIZE; |
| 329 | dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME; |
| 330 | |
| 331 | d_instantiate(dentry, inode); |
| 332 | dget(dentry); |
| 333 | return 0; |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 334 | out_unlock: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | spin_unlock(&mq_lock); |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame^] | 336 | if (ipc_ns) |
| 337 | put_ipc_ns(ipc_ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | return error; |
| 339 | } |
| 340 | |
| 341 | static int mqueue_unlink(struct inode *dir, struct dentry *dentry) |
| 342 | { |
| 343 | struct inode *inode = dentry->d_inode; |
| 344 | |
| 345 | dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME; |
| 346 | dir->i_size -= DIRENT_SIZE; |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 347 | drop_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | dput(dentry); |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | /* |
| 353 | * This is routine for system read from queue file. |
| 354 | * To avoid mess with doing here some sort of mq_receive we allow |
| 355 | * to read only queue size & notification info (the only values |
| 356 | * that are interesting from user point of view and aren't accessible |
| 357 | * through std routines) |
| 358 | */ |
| 359 | static ssize_t mqueue_read_file(struct file *filp, char __user *u_data, |
Akinobu Mita | f1a43f9 | 2008-07-25 01:48:07 -0700 | [diff] [blame] | 360 | size_t count, loff_t *off) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | { |
Josef Sipek | 6d63079 | 2006-12-08 02:37:11 -0800 | [diff] [blame] | 362 | struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | char buffer[FILENT_SIZE]; |
Akinobu Mita | f1a43f9 | 2008-07-25 01:48:07 -0700 | [diff] [blame] | 364 | ssize_t ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | |
| 366 | spin_lock(&info->lock); |
| 367 | snprintf(buffer, sizeof(buffer), |
| 368 | "QSIZE:%-10lu NOTIFY:%-5d SIGNO:%-5d NOTIFY_PID:%-6d\n", |
| 369 | info->qsize, |
| 370 | info->notify_owner ? info->notify.sigev_notify : 0, |
| 371 | (info->notify_owner && |
| 372 | info->notify.sigev_notify == SIGEV_SIGNAL) ? |
| 373 | info->notify.sigev_signo : 0, |
Pavel Emelyanov | 6c5f3e7 | 2008-02-08 04:19:20 -0800 | [diff] [blame] | 374 | pid_vnr(info->notify_owner)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | spin_unlock(&info->lock); |
| 376 | buffer[sizeof(buffer)-1] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | |
Akinobu Mita | f1a43f9 | 2008-07-25 01:48:07 -0700 | [diff] [blame] | 378 | ret = simple_read_from_buffer(u_data, count, off, buffer, |
| 379 | strlen(buffer)); |
| 380 | if (ret <= 0) |
| 381 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | |
Josef Sipek | 6d63079 | 2006-12-08 02:37:11 -0800 | [diff] [blame] | 383 | filp->f_path.dentry->d_inode->i_atime = filp->f_path.dentry->d_inode->i_ctime = CURRENT_TIME; |
Akinobu Mita | f1a43f9 | 2008-07-25 01:48:07 -0700 | [diff] [blame] | 384 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Miklos Szeredi | 75e1fcc | 2006-06-23 02:05:12 -0700 | [diff] [blame] | 387 | static int mqueue_flush_file(struct file *filp, fl_owner_t id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | { |
Josef Sipek | 6d63079 | 2006-12-08 02:37:11 -0800 | [diff] [blame] | 389 | struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | |
| 391 | spin_lock(&info->lock); |
Cedric Le Goater | a03fcb7 | 2006-10-02 02:17:26 -0700 | [diff] [blame] | 392 | if (task_tgid(current) == info->notify_owner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | remove_notification(info); |
| 394 | |
| 395 | spin_unlock(&info->lock); |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | static unsigned int mqueue_poll_file(struct file *filp, struct poll_table_struct *poll_tab) |
| 400 | { |
Josef Sipek | 6d63079 | 2006-12-08 02:37:11 -0800 | [diff] [blame] | 401 | struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | int retval = 0; |
| 403 | |
| 404 | poll_wait(filp, &info->wait_q, poll_tab); |
| 405 | |
| 406 | spin_lock(&info->lock); |
| 407 | if (info->attr.mq_curmsgs) |
| 408 | retval = POLLIN | POLLRDNORM; |
| 409 | |
| 410 | if (info->attr.mq_curmsgs < info->attr.mq_maxmsg) |
| 411 | retval |= POLLOUT | POLLWRNORM; |
| 412 | spin_unlock(&info->lock); |
| 413 | |
| 414 | return retval; |
| 415 | } |
| 416 | |
| 417 | /* Adds current to info->e_wait_q[sr] before element with smaller prio */ |
| 418 | static void wq_add(struct mqueue_inode_info *info, int sr, |
| 419 | struct ext_wait_queue *ewp) |
| 420 | { |
| 421 | struct ext_wait_queue *walk; |
| 422 | |
| 423 | ewp->task = current; |
| 424 | |
| 425 | list_for_each_entry(walk, &info->e_wait_q[sr].list, list) { |
| 426 | if (walk->task->static_prio <= current->static_prio) { |
| 427 | list_add_tail(&ewp->list, &walk->list); |
| 428 | return; |
| 429 | } |
| 430 | } |
| 431 | list_add_tail(&ewp->list, &info->e_wait_q[sr].list); |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * Puts current task to sleep. Caller must hold queue lock. After return |
| 436 | * lock isn't held. |
| 437 | * sr: SEND or RECV |
| 438 | */ |
| 439 | static int wq_sleep(struct mqueue_inode_info *info, int sr, |
| 440 | long timeout, struct ext_wait_queue *ewp) |
| 441 | { |
| 442 | int retval; |
| 443 | signed long time; |
| 444 | |
| 445 | wq_add(info, sr, ewp); |
| 446 | |
| 447 | for (;;) { |
| 448 | set_current_state(TASK_INTERRUPTIBLE); |
| 449 | |
| 450 | spin_unlock(&info->lock); |
| 451 | time = schedule_timeout(timeout); |
| 452 | |
| 453 | while (ewp->state == STATE_PENDING) |
| 454 | cpu_relax(); |
| 455 | |
| 456 | if (ewp->state == STATE_READY) { |
| 457 | retval = 0; |
| 458 | goto out; |
| 459 | } |
| 460 | spin_lock(&info->lock); |
| 461 | if (ewp->state == STATE_READY) { |
| 462 | retval = 0; |
| 463 | goto out_unlock; |
| 464 | } |
| 465 | if (signal_pending(current)) { |
| 466 | retval = -ERESTARTSYS; |
| 467 | break; |
| 468 | } |
| 469 | if (time == 0) { |
| 470 | retval = -ETIMEDOUT; |
| 471 | break; |
| 472 | } |
| 473 | } |
| 474 | list_del(&ewp->list); |
| 475 | out_unlock: |
| 476 | spin_unlock(&info->lock); |
| 477 | out: |
| 478 | return retval; |
| 479 | } |
| 480 | |
| 481 | /* |
| 482 | * Returns waiting task that should be serviced first or NULL if none exists |
| 483 | */ |
| 484 | static struct ext_wait_queue *wq_get_first_waiter( |
| 485 | struct mqueue_inode_info *info, int sr) |
| 486 | { |
| 487 | struct list_head *ptr; |
| 488 | |
| 489 | ptr = info->e_wait_q[sr].list.prev; |
| 490 | if (ptr == &info->e_wait_q[sr].list) |
| 491 | return NULL; |
| 492 | return list_entry(ptr, struct ext_wait_queue, list); |
| 493 | } |
| 494 | |
| 495 | /* Auxiliary functions to manipulate messages' list */ |
| 496 | static void msg_insert(struct msg_msg *ptr, struct mqueue_inode_info *info) |
| 497 | { |
| 498 | int k; |
| 499 | |
| 500 | k = info->attr.mq_curmsgs - 1; |
| 501 | while (k >= 0 && info->messages[k]->m_type >= ptr->m_type) { |
| 502 | info->messages[k + 1] = info->messages[k]; |
| 503 | k--; |
| 504 | } |
| 505 | info->attr.mq_curmsgs++; |
| 506 | info->qsize += ptr->m_ts; |
| 507 | info->messages[k + 1] = ptr; |
| 508 | } |
| 509 | |
| 510 | static inline struct msg_msg *msg_get(struct mqueue_inode_info *info) |
| 511 | { |
| 512 | info->qsize -= info->messages[--info->attr.mq_curmsgs]->m_ts; |
| 513 | return info->messages[info->attr.mq_curmsgs]; |
| 514 | } |
| 515 | |
| 516 | static inline void set_cookie(struct sk_buff *skb, char code) |
| 517 | { |
| 518 | ((char*)skb->data)[NOTIFY_COOKIE_LEN-1] = code; |
| 519 | } |
| 520 | |
| 521 | /* |
| 522 | * The next function is only to split too long sys_mq_timedsend |
| 523 | */ |
| 524 | static void __do_notify(struct mqueue_inode_info *info) |
| 525 | { |
| 526 | /* notification |
| 527 | * invoked when there is registered process and there isn't process |
| 528 | * waiting synchronously for message AND state of queue changed from |
| 529 | * empty to not empty. Here we are sure that no one is waiting |
| 530 | * synchronously. */ |
| 531 | if (info->notify_owner && |
| 532 | info->attr.mq_curmsgs == 1) { |
| 533 | struct siginfo sig_i; |
| 534 | switch (info->notify.sigev_notify) { |
| 535 | case SIGEV_NONE: |
| 536 | break; |
| 537 | case SIGEV_SIGNAL: |
| 538 | /* sends signal */ |
| 539 | |
| 540 | sig_i.si_signo = info->notify.sigev_signo; |
| 541 | sig_i.si_errno = 0; |
| 542 | sig_i.si_code = SI_MESGQ; |
| 543 | sig_i.si_value = info->notify.sigev_value; |
Sukadev Bhattiprolu | a668499 | 2009-01-07 18:08:50 -0800 | [diff] [blame] | 544 | sig_i.si_pid = task_tgid_nr_ns(current, |
| 545 | ns_of_pid(info->notify_owner)); |
David Howells | 414c070 | 2008-11-14 10:39:06 +1100 | [diff] [blame] | 546 | sig_i.si_uid = current_uid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | |
Cedric Le Goater | a03fcb7 | 2006-10-02 02:17:26 -0700 | [diff] [blame] | 548 | kill_pid_info(info->notify.sigev_signo, |
| 549 | &sig_i, info->notify_owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | break; |
| 551 | case SIGEV_THREAD: |
| 552 | set_cookie(info->notify_cookie, NOTIFY_WOKENUP); |
Denis V. Lunev | 7ee015e | 2007-10-10 21:14:03 -0700 | [diff] [blame] | 553 | netlink_sendskb(info->notify_sock, info->notify_cookie); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | break; |
| 555 | } |
| 556 | /* after notification unregisters process */ |
Cedric Le Goater | a03fcb7 | 2006-10-02 02:17:26 -0700 | [diff] [blame] | 557 | put_pid(info->notify_owner); |
| 558 | info->notify_owner = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | } |
| 560 | wake_up(&info->wait_q); |
| 561 | } |
| 562 | |
Al Viro | c32c8af | 2008-12-14 03:46:48 -0500 | [diff] [blame] | 563 | static long prepare_timeout(struct timespec *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | { |
Al Viro | c32c8af | 2008-12-14 03:46:48 -0500 | [diff] [blame] | 565 | struct timespec nowts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | long timeout; |
| 567 | |
Al Viro | c32c8af | 2008-12-14 03:46:48 -0500 | [diff] [blame] | 568 | if (p) { |
| 569 | if (unlikely(p->tv_nsec < 0 || p->tv_sec < 0 |
| 570 | || p->tv_nsec >= NSEC_PER_SEC)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | return -EINVAL; |
| 572 | nowts = CURRENT_TIME; |
| 573 | /* first subtract as jiffies can't be too big */ |
Al Viro | c32c8af | 2008-12-14 03:46:48 -0500 | [diff] [blame] | 574 | p->tv_sec -= nowts.tv_sec; |
| 575 | if (p->tv_nsec < nowts.tv_nsec) { |
| 576 | p->tv_nsec += NSEC_PER_SEC; |
| 577 | p->tv_sec--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | } |
Al Viro | c32c8af | 2008-12-14 03:46:48 -0500 | [diff] [blame] | 579 | p->tv_nsec -= nowts.tv_nsec; |
| 580 | if (p->tv_sec < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | return 0; |
| 582 | |
Al Viro | c32c8af | 2008-12-14 03:46:48 -0500 | [diff] [blame] | 583 | timeout = timespec_to_jiffies(p) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | } else |
| 585 | return MAX_SCHEDULE_TIMEOUT; |
| 586 | |
| 587 | return timeout; |
| 588 | } |
| 589 | |
| 590 | static void remove_notification(struct mqueue_inode_info *info) |
| 591 | { |
Cedric Le Goater | a03fcb7 | 2006-10-02 02:17:26 -0700 | [diff] [blame] | 592 | if (info->notify_owner != NULL && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | info->notify.sigev_notify == SIGEV_THREAD) { |
| 594 | set_cookie(info->notify_cookie, NOTIFY_REMOVED); |
Denis V. Lunev | 7ee015e | 2007-10-10 21:14:03 -0700 | [diff] [blame] | 595 | netlink_sendskb(info->notify_sock, info->notify_cookie); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | } |
Cedric Le Goater | a03fcb7 | 2006-10-02 02:17:26 -0700 | [diff] [blame] | 597 | put_pid(info->notify_owner); |
| 598 | info->notify_owner = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | } |
| 600 | |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 601 | static int mq_attr_ok(struct ipc_namespace *ipc_ns, struct mq_attr *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | { |
| 603 | if (attr->mq_maxmsg <= 0 || attr->mq_msgsize <= 0) |
| 604 | return 0; |
| 605 | if (capable(CAP_SYS_RESOURCE)) { |
| 606 | if (attr->mq_maxmsg > HARD_MSGMAX) |
| 607 | return 0; |
| 608 | } else { |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 609 | if (attr->mq_maxmsg > ipc_ns->mq_msg_max || |
| 610 | attr->mq_msgsize > ipc_ns->mq_msgsize_max) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | return 0; |
| 612 | } |
| 613 | /* check for overflow */ |
| 614 | if (attr->mq_msgsize > ULONG_MAX/attr->mq_maxmsg) |
| 615 | return 0; |
| 616 | if ((unsigned long)(attr->mq_maxmsg * attr->mq_msgsize) + |
| 617 | (attr->mq_maxmsg * sizeof (struct msg_msg *)) < |
| 618 | (unsigned long)(attr->mq_maxmsg * attr->mq_msgsize)) |
| 619 | return 0; |
| 620 | return 1; |
| 621 | } |
| 622 | |
| 623 | /* |
| 624 | * Invoked when creating a new queue via sys_mq_open |
| 625 | */ |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 626 | static struct file *do_create(struct ipc_namespace *ipc_ns, struct dentry *dir, |
| 627 | struct dentry *dentry, int oflag, mode_t mode, |
| 628 | struct mq_attr *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | { |
David Howells | 745ca24 | 2008-11-14 10:39:22 +1100 | [diff] [blame] | 630 | const struct cred *cred = current_cred(); |
Dave Hansen | 4a3fd21 | 2008-02-15 14:37:48 -0800 | [diff] [blame] | 631 | struct file *result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | int ret; |
| 633 | |
Al Viro | 564f699 | 2008-12-14 04:02:26 -0500 | [diff] [blame] | 634 | if (attr) { |
Alexander Viro | 7c7dce9 | 2006-01-14 15:29:55 -0500 | [diff] [blame] | 635 | ret = -EINVAL; |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 636 | if (!mq_attr_ok(ipc_ns, attr)) |
Alexander Viro | 7c7dce9 | 2006-01-14 15:29:55 -0500 | [diff] [blame] | 637 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | /* store for use during create */ |
Al Viro | 564f699 | 2008-12-14 04:02:26 -0500 | [diff] [blame] | 639 | dentry->d_fsdata = attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 642 | mode &= ~current_umask(); |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 643 | ret = mnt_want_write(ipc_ns->mq_mnt); |
Dave Hansen | 4a3fd21 | 2008-02-15 14:37:48 -0800 | [diff] [blame] | 644 | if (ret) |
| 645 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | ret = vfs_create(dir->d_inode, dentry, mode, NULL); |
| 647 | dentry->d_fsdata = NULL; |
| 648 | if (ret) |
Dave Hansen | 4a3fd21 | 2008-02-15 14:37:48 -0800 | [diff] [blame] | 649 | goto out_drop_write; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 651 | result = dentry_open(dentry, ipc_ns->mq_mnt, oflag, cred); |
Dave Hansen | 4a3fd21 | 2008-02-15 14:37:48 -0800 | [diff] [blame] | 652 | /* |
| 653 | * dentry_open() took a persistent mnt_want_write(), |
| 654 | * so we can now drop this one. |
| 655 | */ |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 656 | mnt_drop_write(ipc_ns->mq_mnt); |
Dave Hansen | 4a3fd21 | 2008-02-15 14:37:48 -0800 | [diff] [blame] | 657 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | |
Dave Hansen | 4a3fd21 | 2008-02-15 14:37:48 -0800 | [diff] [blame] | 659 | out_drop_write: |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 660 | mnt_drop_write(ipc_ns->mq_mnt); |
Alexander Viro | 7c7dce9 | 2006-01-14 15:29:55 -0500 | [diff] [blame] | 661 | out: |
| 662 | dput(dentry); |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 663 | mntput(ipc_ns->mq_mnt); |
Alexander Viro | 7c7dce9 | 2006-01-14 15:29:55 -0500 | [diff] [blame] | 664 | return ERR_PTR(ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | /* Opens existing queue */ |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 668 | static struct file *do_open(struct ipc_namespace *ipc_ns, |
| 669 | struct dentry *dentry, int oflag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | { |
David Howells | 745ca24 | 2008-11-14 10:39:22 +1100 | [diff] [blame] | 671 | const struct cred *cred = current_cred(); |
| 672 | |
| 673 | static const int oflag2acc[O_ACCMODE] = { MAY_READ, MAY_WRITE, |
| 674 | MAY_READ | MAY_WRITE }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | |
Alexander Viro | 7c7dce9 | 2006-01-14 15:29:55 -0500 | [diff] [blame] | 676 | if ((oflag & O_ACCMODE) == (O_RDWR | O_WRONLY)) { |
| 677 | dput(dentry); |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 678 | mntput(ipc_ns->mq_mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | return ERR_PTR(-EINVAL); |
Alexander Viro | 7c7dce9 | 2006-01-14 15:29:55 -0500 | [diff] [blame] | 680 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | |
Al Viro | f419a2e | 2008-07-22 00:07:17 -0400 | [diff] [blame] | 682 | if (inode_permission(dentry->d_inode, oflag2acc[oflag & O_ACCMODE])) { |
Alexander Viro | 7c7dce9 | 2006-01-14 15:29:55 -0500 | [diff] [blame] | 683 | dput(dentry); |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 684 | mntput(ipc_ns->mq_mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | return ERR_PTR(-EACCES); |
Alexander Viro | 7c7dce9 | 2006-01-14 15:29:55 -0500 | [diff] [blame] | 686 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 688 | return dentry_open(dentry, ipc_ns->mq_mnt, oflag, cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Heiko Carstens | d5460c9 | 2009-01-14 14:14:27 +0100 | [diff] [blame] | 691 | SYSCALL_DEFINE4(mq_open, const char __user *, u_name, int, oflag, mode_t, mode, |
| 692 | struct mq_attr __user *, u_attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | { |
| 694 | struct dentry *dentry; |
| 695 | struct file *filp; |
| 696 | char *name; |
Al Viro | 564f699 | 2008-12-14 04:02:26 -0500 | [diff] [blame] | 697 | struct mq_attr attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | int fd, error; |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame^] | 699 | struct ipc_namespace *ipc_ns = current->nsproxy->ipc_ns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | |
Al Viro | 564f699 | 2008-12-14 04:02:26 -0500 | [diff] [blame] | 701 | if (u_attr && copy_from_user(&attr, u_attr, sizeof(struct mq_attr))) |
| 702 | return -EFAULT; |
| 703 | |
| 704 | audit_mq_open(oflag, mode, u_attr ? &attr : NULL); |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 705 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | if (IS_ERR(name = getname(u_name))) |
| 707 | return PTR_ERR(name); |
| 708 | |
Ulrich Drepper | 269f213 | 2008-05-03 15:28:45 -0400 | [diff] [blame] | 709 | fd = get_unused_fd_flags(O_CLOEXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | if (fd < 0) |
| 711 | goto out_putname; |
| 712 | |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 713 | mutex_lock(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex); |
| 714 | dentry = lookup_one_len(name, ipc_ns->mq_mnt->mnt_root, strlen(name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | if (IS_ERR(dentry)) { |
| 716 | error = PTR_ERR(dentry); |
| 717 | goto out_err; |
| 718 | } |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 719 | mntget(ipc_ns->mq_mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | |
| 721 | if (oflag & O_CREAT) { |
| 722 | if (dentry->d_inode) { /* entry already exists */ |
Al Viro | 5a190ae | 2007-06-07 12:19:32 -0400 | [diff] [blame] | 723 | audit_inode(name, dentry); |
Alexander Viro | 7c7dce9 | 2006-01-14 15:29:55 -0500 | [diff] [blame] | 724 | error = -EEXIST; |
| 725 | if (oflag & O_EXCL) |
| 726 | goto out; |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 727 | filp = do_open(ipc_ns, dentry, oflag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | } else { |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 729 | filp = do_create(ipc_ns, ipc_ns->mq_mnt->mnt_root, |
| 730 | dentry, oflag, mode, |
Al Viro | 564f699 | 2008-12-14 04:02:26 -0500 | [diff] [blame] | 731 | u_attr ? &attr : NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | } |
Alexander Viro | 7c7dce9 | 2006-01-14 15:29:55 -0500 | [diff] [blame] | 733 | } else { |
| 734 | error = -ENOENT; |
| 735 | if (!dentry->d_inode) |
| 736 | goto out; |
Al Viro | 5a190ae | 2007-06-07 12:19:32 -0400 | [diff] [blame] | 737 | audit_inode(name, dentry); |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 738 | filp = do_open(ipc_ns, dentry, oflag); |
Alexander Viro | 7c7dce9 | 2006-01-14 15:29:55 -0500 | [diff] [blame] | 739 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | |
| 741 | if (IS_ERR(filp)) { |
| 742 | error = PTR_ERR(filp); |
| 743 | goto out_putfd; |
| 744 | } |
| 745 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | fd_install(fd, filp); |
| 747 | goto out_upsem; |
| 748 | |
Alexander Viro | 7c7dce9 | 2006-01-14 15:29:55 -0500 | [diff] [blame] | 749 | out: |
| 750 | dput(dentry); |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 751 | mntput(ipc_ns->mq_mnt); |
Alexander Viro | 7c7dce9 | 2006-01-14 15:29:55 -0500 | [diff] [blame] | 752 | out_putfd: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | put_unused_fd(fd); |
| 754 | out_err: |
| 755 | fd = error; |
| 756 | out_upsem: |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 757 | mutex_unlock(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | out_putname: |
| 759 | putname(name); |
| 760 | return fd; |
| 761 | } |
| 762 | |
Heiko Carstens | d5460c9 | 2009-01-14 14:14:27 +0100 | [diff] [blame] | 763 | SYSCALL_DEFINE1(mq_unlink, const char __user *, u_name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | { |
| 765 | int err; |
| 766 | char *name; |
| 767 | struct dentry *dentry; |
| 768 | struct inode *inode = NULL; |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame^] | 769 | struct ipc_namespace *ipc_ns = current->nsproxy->ipc_ns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | |
| 771 | name = getname(u_name); |
| 772 | if (IS_ERR(name)) |
| 773 | return PTR_ERR(name); |
| 774 | |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 775 | mutex_lock_nested(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex, |
Peter Zijlstra | 7a43481 | 2007-03-06 01:42:09 -0800 | [diff] [blame] | 776 | I_MUTEX_PARENT); |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 777 | dentry = lookup_one_len(name, ipc_ns->mq_mnt->mnt_root, strlen(name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | if (IS_ERR(dentry)) { |
| 779 | err = PTR_ERR(dentry); |
| 780 | goto out_unlock; |
| 781 | } |
| 782 | |
| 783 | if (!dentry->d_inode) { |
| 784 | err = -ENOENT; |
| 785 | goto out_err; |
| 786 | } |
| 787 | |
| 788 | inode = dentry->d_inode; |
| 789 | if (inode) |
| 790 | atomic_inc(&inode->i_count); |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 791 | err = mnt_want_write(ipc_ns->mq_mnt); |
Dave Hansen | 0622753 | 2008-02-15 14:37:34 -0800 | [diff] [blame] | 792 | if (err) |
| 793 | goto out_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | err = vfs_unlink(dentry->d_parent->d_inode, dentry); |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 795 | mnt_drop_write(ipc_ns->mq_mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | out_err: |
| 797 | dput(dentry); |
| 798 | |
| 799 | out_unlock: |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 800 | mutex_unlock(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | putname(name); |
| 802 | if (inode) |
| 803 | iput(inode); |
| 804 | |
| 805 | return err; |
| 806 | } |
| 807 | |
| 808 | /* Pipelined send and receive functions. |
| 809 | * |
| 810 | * If a receiver finds no waiting message, then it registers itself in the |
| 811 | * list of waiting receivers. A sender checks that list before adding the new |
| 812 | * message into the message array. If there is a waiting receiver, then it |
| 813 | * bypasses the message array and directly hands the message over to the |
| 814 | * receiver. |
| 815 | * The receiver accepts the message and returns without grabbing the queue |
| 816 | * spinlock. Therefore an intermediate STATE_PENDING state and memory barriers |
| 817 | * are necessary. The same algorithm is used for sysv semaphores, see |
Serge E. Hallyn | 7b7a317 | 2006-03-28 01:56:23 -0800 | [diff] [blame] | 818 | * ipc/sem.c for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | * |
| 820 | * The same algorithm is used for senders. |
| 821 | */ |
| 822 | |
| 823 | /* pipelined_send() - send a message directly to the task waiting in |
| 824 | * sys_mq_timedreceive() (without inserting message into a queue). |
| 825 | */ |
| 826 | static inline void pipelined_send(struct mqueue_inode_info *info, |
| 827 | struct msg_msg *message, |
| 828 | struct ext_wait_queue *receiver) |
| 829 | { |
| 830 | receiver->msg = message; |
| 831 | list_del(&receiver->list); |
| 832 | receiver->state = STATE_PENDING; |
| 833 | wake_up_process(receiver->task); |
akpm@osdl.org | d59dd46 | 2005-05-01 08:58:47 -0700 | [diff] [blame] | 834 | smp_wmb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | receiver->state = STATE_READY; |
| 836 | } |
| 837 | |
| 838 | /* pipelined_receive() - if there is task waiting in sys_mq_timedsend() |
| 839 | * gets its message and put to the queue (we have one free place for sure). */ |
| 840 | static inline void pipelined_receive(struct mqueue_inode_info *info) |
| 841 | { |
| 842 | struct ext_wait_queue *sender = wq_get_first_waiter(info, SEND); |
| 843 | |
| 844 | if (!sender) { |
| 845 | /* for poll */ |
| 846 | wake_up_interruptible(&info->wait_q); |
| 847 | return; |
| 848 | } |
| 849 | msg_insert(sender->msg, info); |
| 850 | list_del(&sender->list); |
| 851 | sender->state = STATE_PENDING; |
| 852 | wake_up_process(sender->task); |
akpm@osdl.org | d59dd46 | 2005-05-01 08:58:47 -0700 | [diff] [blame] | 853 | smp_wmb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | sender->state = STATE_READY; |
| 855 | } |
| 856 | |
Heiko Carstens | c4ea37c | 2009-01-14 14:14:28 +0100 | [diff] [blame] | 857 | SYSCALL_DEFINE5(mq_timedsend, mqd_t, mqdes, const char __user *, u_msg_ptr, |
| 858 | size_t, msg_len, unsigned int, msg_prio, |
| 859 | const struct timespec __user *, u_abs_timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | { |
| 861 | struct file *filp; |
| 862 | struct inode *inode; |
| 863 | struct ext_wait_queue wait; |
| 864 | struct ext_wait_queue *receiver; |
| 865 | struct msg_msg *msg_ptr; |
| 866 | struct mqueue_inode_info *info; |
Al Viro | c32c8af | 2008-12-14 03:46:48 -0500 | [diff] [blame] | 867 | struct timespec ts, *p = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | long timeout; |
| 869 | int ret; |
| 870 | |
Al Viro | c32c8af | 2008-12-14 03:46:48 -0500 | [diff] [blame] | 871 | if (u_abs_timeout) { |
| 872 | if (copy_from_user(&ts, u_abs_timeout, |
| 873 | sizeof(struct timespec))) |
| 874 | return -EFAULT; |
| 875 | p = &ts; |
| 876 | } |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 877 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | if (unlikely(msg_prio >= (unsigned long) MQ_PRIO_MAX)) |
| 879 | return -EINVAL; |
| 880 | |
Al Viro | c32c8af | 2008-12-14 03:46:48 -0500 | [diff] [blame] | 881 | audit_mq_sendrecv(mqdes, msg_len, msg_prio, p); |
| 882 | timeout = prepare_timeout(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | |
| 884 | ret = -EBADF; |
| 885 | filp = fget(mqdes); |
| 886 | if (unlikely(!filp)) |
| 887 | goto out; |
| 888 | |
Josef Sipek | 6d63079 | 2006-12-08 02:37:11 -0800 | [diff] [blame] | 889 | inode = filp->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | if (unlikely(filp->f_op != &mqueue_file_operations)) |
| 891 | goto out_fput; |
| 892 | info = MQUEUE_I(inode); |
Al Viro | 5a190ae | 2007-06-07 12:19:32 -0400 | [diff] [blame] | 893 | audit_inode(NULL, filp->f_path.dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | |
| 895 | if (unlikely(!(filp->f_mode & FMODE_WRITE))) |
| 896 | goto out_fput; |
| 897 | |
| 898 | if (unlikely(msg_len > info->attr.mq_msgsize)) { |
| 899 | ret = -EMSGSIZE; |
| 900 | goto out_fput; |
| 901 | } |
| 902 | |
| 903 | /* First try to allocate memory, before doing anything with |
| 904 | * existing queues. */ |
| 905 | msg_ptr = load_msg(u_msg_ptr, msg_len); |
| 906 | if (IS_ERR(msg_ptr)) { |
| 907 | ret = PTR_ERR(msg_ptr); |
| 908 | goto out_fput; |
| 909 | } |
| 910 | msg_ptr->m_ts = msg_len; |
| 911 | msg_ptr->m_type = msg_prio; |
| 912 | |
| 913 | spin_lock(&info->lock); |
| 914 | |
| 915 | if (info->attr.mq_curmsgs == info->attr.mq_maxmsg) { |
| 916 | if (filp->f_flags & O_NONBLOCK) { |
| 917 | spin_unlock(&info->lock); |
| 918 | ret = -EAGAIN; |
| 919 | } else if (unlikely(timeout < 0)) { |
| 920 | spin_unlock(&info->lock); |
| 921 | ret = timeout; |
| 922 | } else { |
| 923 | wait.task = current; |
| 924 | wait.msg = (void *) msg_ptr; |
| 925 | wait.state = STATE_NONE; |
| 926 | ret = wq_sleep(info, SEND, timeout, &wait); |
| 927 | } |
| 928 | if (ret < 0) |
| 929 | free_msg(msg_ptr); |
| 930 | } else { |
| 931 | receiver = wq_get_first_waiter(info, RECV); |
| 932 | if (receiver) { |
| 933 | pipelined_send(info, msg_ptr, receiver); |
| 934 | } else { |
| 935 | /* adds message to the queue */ |
| 936 | msg_insert(msg_ptr, info); |
| 937 | __do_notify(info); |
| 938 | } |
| 939 | inode->i_atime = inode->i_mtime = inode->i_ctime = |
| 940 | CURRENT_TIME; |
| 941 | spin_unlock(&info->lock); |
| 942 | ret = 0; |
| 943 | } |
| 944 | out_fput: |
| 945 | fput(filp); |
| 946 | out: |
| 947 | return ret; |
| 948 | } |
| 949 | |
Heiko Carstens | c4ea37c | 2009-01-14 14:14:28 +0100 | [diff] [blame] | 950 | SYSCALL_DEFINE5(mq_timedreceive, mqd_t, mqdes, char __user *, u_msg_ptr, |
| 951 | size_t, msg_len, unsigned int __user *, u_msg_prio, |
| 952 | const struct timespec __user *, u_abs_timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | { |
| 954 | long timeout; |
| 955 | ssize_t ret; |
| 956 | struct msg_msg *msg_ptr; |
| 957 | struct file *filp; |
| 958 | struct inode *inode; |
| 959 | struct mqueue_inode_info *info; |
| 960 | struct ext_wait_queue wait; |
Al Viro | c32c8af | 2008-12-14 03:46:48 -0500 | [diff] [blame] | 961 | struct timespec ts, *p = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | |
Al Viro | c32c8af | 2008-12-14 03:46:48 -0500 | [diff] [blame] | 963 | if (u_abs_timeout) { |
| 964 | if (copy_from_user(&ts, u_abs_timeout, |
| 965 | sizeof(struct timespec))) |
| 966 | return -EFAULT; |
| 967 | p = &ts; |
| 968 | } |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 969 | |
Al Viro | c32c8af | 2008-12-14 03:46:48 -0500 | [diff] [blame] | 970 | audit_mq_sendrecv(mqdes, msg_len, 0, p); |
| 971 | timeout = prepare_timeout(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | |
| 973 | ret = -EBADF; |
| 974 | filp = fget(mqdes); |
| 975 | if (unlikely(!filp)) |
| 976 | goto out; |
| 977 | |
Josef Sipek | 6d63079 | 2006-12-08 02:37:11 -0800 | [diff] [blame] | 978 | inode = filp->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | if (unlikely(filp->f_op != &mqueue_file_operations)) |
| 980 | goto out_fput; |
| 981 | info = MQUEUE_I(inode); |
Al Viro | 5a190ae | 2007-06-07 12:19:32 -0400 | [diff] [blame] | 982 | audit_inode(NULL, filp->f_path.dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | |
| 984 | if (unlikely(!(filp->f_mode & FMODE_READ))) |
| 985 | goto out_fput; |
| 986 | |
| 987 | /* checks if buffer is big enough */ |
| 988 | if (unlikely(msg_len < info->attr.mq_msgsize)) { |
| 989 | ret = -EMSGSIZE; |
| 990 | goto out_fput; |
| 991 | } |
| 992 | |
| 993 | spin_lock(&info->lock); |
| 994 | if (info->attr.mq_curmsgs == 0) { |
| 995 | if (filp->f_flags & O_NONBLOCK) { |
| 996 | spin_unlock(&info->lock); |
| 997 | ret = -EAGAIN; |
| 998 | msg_ptr = NULL; |
| 999 | } else if (unlikely(timeout < 0)) { |
| 1000 | spin_unlock(&info->lock); |
| 1001 | ret = timeout; |
| 1002 | msg_ptr = NULL; |
| 1003 | } else { |
| 1004 | wait.task = current; |
| 1005 | wait.state = STATE_NONE; |
| 1006 | ret = wq_sleep(info, RECV, timeout, &wait); |
| 1007 | msg_ptr = wait.msg; |
| 1008 | } |
| 1009 | } else { |
| 1010 | msg_ptr = msg_get(info); |
| 1011 | |
| 1012 | inode->i_atime = inode->i_mtime = inode->i_ctime = |
| 1013 | CURRENT_TIME; |
| 1014 | |
| 1015 | /* There is now free space in queue. */ |
| 1016 | pipelined_receive(info); |
| 1017 | spin_unlock(&info->lock); |
| 1018 | ret = 0; |
| 1019 | } |
| 1020 | if (ret == 0) { |
| 1021 | ret = msg_ptr->m_ts; |
| 1022 | |
| 1023 | if ((u_msg_prio && put_user(msg_ptr->m_type, u_msg_prio)) || |
| 1024 | store_msg(u_msg_ptr, msg_ptr, msg_ptr->m_ts)) { |
| 1025 | ret = -EFAULT; |
| 1026 | } |
| 1027 | free_msg(msg_ptr); |
| 1028 | } |
| 1029 | out_fput: |
| 1030 | fput(filp); |
| 1031 | out: |
| 1032 | return ret; |
| 1033 | } |
| 1034 | |
| 1035 | /* |
| 1036 | * Notes: the case when user wants us to deregister (with NULL as pointer) |
| 1037 | * and he isn't currently owner of notification, will be silently discarded. |
| 1038 | * It isn't explicitly defined in the POSIX. |
| 1039 | */ |
Heiko Carstens | c4ea37c | 2009-01-14 14:14:28 +0100 | [diff] [blame] | 1040 | SYSCALL_DEFINE2(mq_notify, mqd_t, mqdes, |
| 1041 | const struct sigevent __user *, u_notification) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | { |
| 1043 | int ret; |
| 1044 | struct file *filp; |
| 1045 | struct sock *sock; |
| 1046 | struct inode *inode; |
| 1047 | struct sigevent notification; |
| 1048 | struct mqueue_inode_info *info; |
| 1049 | struct sk_buff *nc; |
| 1050 | |
Al Viro | 20114f7 | 2008-12-10 07:16:12 -0500 | [diff] [blame] | 1051 | if (u_notification) { |
| 1052 | if (copy_from_user(¬ification, u_notification, |
| 1053 | sizeof(struct sigevent))) |
| 1054 | return -EFAULT; |
| 1055 | } |
| 1056 | |
| 1057 | audit_mq_notify(mqdes, u_notification ? ¬ification : NULL); |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 1058 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | nc = NULL; |
| 1060 | sock = NULL; |
| 1061 | if (u_notification != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | if (unlikely(notification.sigev_notify != SIGEV_NONE && |
| 1063 | notification.sigev_notify != SIGEV_SIGNAL && |
| 1064 | notification.sigev_notify != SIGEV_THREAD)) |
| 1065 | return -EINVAL; |
| 1066 | if (notification.sigev_notify == SIGEV_SIGNAL && |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 1067 | !valid_signal(notification.sigev_signo)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | return -EINVAL; |
| 1069 | } |
| 1070 | if (notification.sigev_notify == SIGEV_THREAD) { |
Patrick McHardy | c3d8d1e | 2007-11-07 02:42:09 -0800 | [diff] [blame] | 1071 | long timeo; |
| 1072 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | /* create the notify skb */ |
| 1074 | nc = alloc_skb(NOTIFY_COOKIE_LEN, GFP_KERNEL); |
| 1075 | ret = -ENOMEM; |
| 1076 | if (!nc) |
| 1077 | goto out; |
| 1078 | ret = -EFAULT; |
| 1079 | if (copy_from_user(nc->data, |
| 1080 | notification.sigev_value.sival_ptr, |
| 1081 | NOTIFY_COOKIE_LEN)) { |
| 1082 | goto out; |
| 1083 | } |
| 1084 | |
| 1085 | /* TODO: add a header? */ |
| 1086 | skb_put(nc, NOTIFY_COOKIE_LEN); |
| 1087 | /* and attach it to the socket */ |
| 1088 | retry: |
| 1089 | filp = fget(notification.sigev_signo); |
| 1090 | ret = -EBADF; |
| 1091 | if (!filp) |
| 1092 | goto out; |
| 1093 | sock = netlink_getsockbyfilp(filp); |
| 1094 | fput(filp); |
| 1095 | if (IS_ERR(sock)) { |
| 1096 | ret = PTR_ERR(sock); |
| 1097 | sock = NULL; |
| 1098 | goto out; |
| 1099 | } |
| 1100 | |
Patrick McHardy | c3d8d1e | 2007-11-07 02:42:09 -0800 | [diff] [blame] | 1101 | timeo = MAX_SCHEDULE_TIMEOUT; |
Denis V. Lunev | 9457afe | 2008-06-05 11:23:39 -0700 | [diff] [blame] | 1102 | ret = netlink_attachskb(sock, nc, &timeo, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | if (ret == 1) |
| 1104 | goto retry; |
| 1105 | if (ret) { |
| 1106 | sock = NULL; |
| 1107 | nc = NULL; |
| 1108 | goto out; |
| 1109 | } |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | ret = -EBADF; |
| 1114 | filp = fget(mqdes); |
| 1115 | if (!filp) |
| 1116 | goto out; |
| 1117 | |
Josef Sipek | 6d63079 | 2006-12-08 02:37:11 -0800 | [diff] [blame] | 1118 | inode = filp->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | if (unlikely(filp->f_op != &mqueue_file_operations)) |
| 1120 | goto out_fput; |
| 1121 | info = MQUEUE_I(inode); |
| 1122 | |
| 1123 | ret = 0; |
| 1124 | spin_lock(&info->lock); |
| 1125 | if (u_notification == NULL) { |
Cedric Le Goater | a03fcb7 | 2006-10-02 02:17:26 -0700 | [diff] [blame] | 1126 | if (info->notify_owner == task_tgid(current)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | remove_notification(info); |
| 1128 | inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 1129 | } |
Cedric Le Goater | a03fcb7 | 2006-10-02 02:17:26 -0700 | [diff] [blame] | 1130 | } else if (info->notify_owner != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | ret = -EBUSY; |
| 1132 | } else { |
| 1133 | switch (notification.sigev_notify) { |
| 1134 | case SIGEV_NONE: |
| 1135 | info->notify.sigev_notify = SIGEV_NONE; |
| 1136 | break; |
| 1137 | case SIGEV_THREAD: |
| 1138 | info->notify_sock = sock; |
| 1139 | info->notify_cookie = nc; |
| 1140 | sock = NULL; |
| 1141 | nc = NULL; |
| 1142 | info->notify.sigev_notify = SIGEV_THREAD; |
| 1143 | break; |
| 1144 | case SIGEV_SIGNAL: |
| 1145 | info->notify.sigev_signo = notification.sigev_signo; |
| 1146 | info->notify.sigev_value = notification.sigev_value; |
| 1147 | info->notify.sigev_notify = SIGEV_SIGNAL; |
| 1148 | break; |
| 1149 | } |
Cedric Le Goater | a03fcb7 | 2006-10-02 02:17:26 -0700 | [diff] [blame] | 1150 | |
| 1151 | info->notify_owner = get_pid(task_tgid(current)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 1153 | } |
| 1154 | spin_unlock(&info->lock); |
| 1155 | out_fput: |
| 1156 | fput(filp); |
| 1157 | out: |
| 1158 | if (sock) { |
| 1159 | netlink_detachskb(sock, nc); |
| 1160 | } else if (nc) { |
| 1161 | dev_kfree_skb(nc); |
| 1162 | } |
| 1163 | return ret; |
| 1164 | } |
| 1165 | |
Heiko Carstens | c4ea37c | 2009-01-14 14:14:28 +0100 | [diff] [blame] | 1166 | SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes, |
| 1167 | const struct mq_attr __user *, u_mqstat, |
| 1168 | struct mq_attr __user *, u_omqstat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | { |
| 1170 | int ret; |
| 1171 | struct mq_attr mqstat, omqstat; |
| 1172 | struct file *filp; |
| 1173 | struct inode *inode; |
| 1174 | struct mqueue_inode_info *info; |
| 1175 | |
| 1176 | if (u_mqstat != NULL) { |
| 1177 | if (copy_from_user(&mqstat, u_mqstat, sizeof(struct mq_attr))) |
| 1178 | return -EFAULT; |
| 1179 | if (mqstat.mq_flags & (~O_NONBLOCK)) |
| 1180 | return -EINVAL; |
| 1181 | } |
| 1182 | |
| 1183 | ret = -EBADF; |
| 1184 | filp = fget(mqdes); |
| 1185 | if (!filp) |
| 1186 | goto out; |
| 1187 | |
Josef Sipek | 6d63079 | 2006-12-08 02:37:11 -0800 | [diff] [blame] | 1188 | inode = filp->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | if (unlikely(filp->f_op != &mqueue_file_operations)) |
| 1190 | goto out_fput; |
| 1191 | info = MQUEUE_I(inode); |
| 1192 | |
| 1193 | spin_lock(&info->lock); |
| 1194 | |
| 1195 | omqstat = info->attr; |
| 1196 | omqstat.mq_flags = filp->f_flags & O_NONBLOCK; |
| 1197 | if (u_mqstat) { |
Al Viro | 7392906 | 2008-12-10 06:58:59 -0500 | [diff] [blame] | 1198 | audit_mq_getsetattr(mqdes, &mqstat); |
Jonathan Corbet | db1dd4d | 2009-02-06 15:25:24 -0700 | [diff] [blame] | 1199 | spin_lock(&filp->f_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | if (mqstat.mq_flags & O_NONBLOCK) |
| 1201 | filp->f_flags |= O_NONBLOCK; |
| 1202 | else |
| 1203 | filp->f_flags &= ~O_NONBLOCK; |
Jonathan Corbet | db1dd4d | 2009-02-06 15:25:24 -0700 | [diff] [blame] | 1204 | spin_unlock(&filp->f_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | |
| 1206 | inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 1207 | } |
| 1208 | |
| 1209 | spin_unlock(&info->lock); |
| 1210 | |
| 1211 | ret = 0; |
| 1212 | if (u_omqstat != NULL && copy_to_user(u_omqstat, &omqstat, |
| 1213 | sizeof(struct mq_attr))) |
| 1214 | ret = -EFAULT; |
| 1215 | |
| 1216 | out_fput: |
| 1217 | fput(filp); |
| 1218 | out: |
| 1219 | return ret; |
| 1220 | } |
| 1221 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 1222 | static const struct inode_operations mqueue_dir_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | .lookup = simple_lookup, |
| 1224 | .create = mqueue_create, |
| 1225 | .unlink = mqueue_unlink, |
| 1226 | }; |
| 1227 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 1228 | static const struct file_operations mqueue_file_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | .flush = mqueue_flush_file, |
| 1230 | .poll = mqueue_poll_file, |
| 1231 | .read = mqueue_read_file, |
| 1232 | }; |
| 1233 | |
| 1234 | static struct super_operations mqueue_super_ops = { |
| 1235 | .alloc_inode = mqueue_alloc_inode, |
| 1236 | .destroy_inode = mqueue_destroy_inode, |
| 1237 | .statfs = simple_statfs, |
| 1238 | .delete_inode = mqueue_delete_inode, |
| 1239 | .drop_inode = generic_delete_inode, |
| 1240 | }; |
| 1241 | |
| 1242 | static struct file_system_type mqueue_fs_type = { |
| 1243 | .name = "mqueue", |
| 1244 | .get_sb = mqueue_get_sb, |
| 1245 | .kill_sb = kill_litter_super, |
| 1246 | }; |
| 1247 | |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame^] | 1248 | int mq_init_ns(struct ipc_namespace *ns) |
| 1249 | { |
| 1250 | ns->mq_queues_count = 0; |
| 1251 | ns->mq_queues_max = DFLT_QUEUESMAX; |
| 1252 | ns->mq_msg_max = DFLT_MSGMAX; |
| 1253 | ns->mq_msgsize_max = DFLT_MSGSIZEMAX; |
| 1254 | |
| 1255 | ns->mq_mnt = kern_mount_data(&mqueue_fs_type, ns); |
| 1256 | if (IS_ERR(ns->mq_mnt)) { |
| 1257 | int err = PTR_ERR(ns->mq_mnt); |
| 1258 | ns->mq_mnt = NULL; |
| 1259 | return err; |
| 1260 | } |
| 1261 | return 0; |
| 1262 | } |
| 1263 | |
| 1264 | void mq_clear_sbinfo(struct ipc_namespace *ns) |
| 1265 | { |
| 1266 | ns->mq_mnt->mnt_sb->s_fs_info = NULL; |
| 1267 | } |
| 1268 | |
| 1269 | void mq_put_mnt(struct ipc_namespace *ns) |
| 1270 | { |
| 1271 | mntput(ns->mq_mnt); |
| 1272 | } |
| 1273 | |
Joe Korty | b231cca4 | 2008-10-18 20:28:32 -0700 | [diff] [blame] | 1274 | static int msg_max_limit_min = MIN_MSGMAX; |
| 1275 | static int msg_max_limit_max = MAX_MSGMAX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | |
Joe Korty | b231cca4 | 2008-10-18 20:28:32 -0700 | [diff] [blame] | 1277 | static int msg_maxsize_limit_min = MIN_MSGSIZEMAX; |
| 1278 | static int msg_maxsize_limit_max = MAX_MSGSIZEMAX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | |
| 1280 | static ctl_table mq_sysctls[] = { |
| 1281 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | .procname = "queues_max", |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 1283 | .data = &init_ipc_ns.mq_queues_max, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | .maxlen = sizeof(int), |
| 1285 | .mode = 0644, |
| 1286 | .proc_handler = &proc_dointvec, |
| 1287 | }, |
| 1288 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | .procname = "msg_max", |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 1290 | .data = &init_ipc_ns.mq_msg_max, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | .maxlen = sizeof(int), |
| 1292 | .mode = 0644, |
| 1293 | .proc_handler = &proc_dointvec_minmax, |
| 1294 | .extra1 = &msg_max_limit_min, |
| 1295 | .extra2 = &msg_max_limit_max, |
| 1296 | }, |
| 1297 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | .procname = "msgsize_max", |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 1299 | .data = &init_ipc_ns.mq_msgsize_max, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | .maxlen = sizeof(int), |
| 1301 | .mode = 0644, |
| 1302 | .proc_handler = &proc_dointvec_minmax, |
| 1303 | .extra1 = &msg_maxsize_limit_min, |
| 1304 | .extra2 = &msg_maxsize_limit_max, |
| 1305 | }, |
| 1306 | { .ctl_name = 0 } |
| 1307 | }; |
| 1308 | |
| 1309 | static ctl_table mq_sysctl_dir[] = { |
| 1310 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | .procname = "mqueue", |
| 1312 | .mode = 0555, |
| 1313 | .child = mq_sysctls, |
| 1314 | }, |
| 1315 | { .ctl_name = 0 } |
| 1316 | }; |
| 1317 | |
| 1318 | static ctl_table mq_sysctl_root[] = { |
| 1319 | { |
| 1320 | .ctl_name = CTL_FS, |
| 1321 | .procname = "fs", |
| 1322 | .mode = 0555, |
| 1323 | .child = mq_sysctl_dir, |
| 1324 | }, |
| 1325 | { .ctl_name = 0 } |
| 1326 | }; |
| 1327 | |
| 1328 | static int __init init_mqueue_fs(void) |
| 1329 | { |
| 1330 | int error; |
| 1331 | |
| 1332 | mqueue_inode_cachep = kmem_cache_create("mqueue_inode_cache", |
| 1333 | sizeof(struct mqueue_inode_info), 0, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1334 | SLAB_HWCACHE_ALIGN, init_once); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | if (mqueue_inode_cachep == NULL) |
| 1336 | return -ENOMEM; |
| 1337 | |
| 1338 | /* ignore failues - they are not fatal */ |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 1339 | mq_sysctl_table = register_sysctl_table(mq_sysctl_root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | |
| 1341 | error = register_filesystem(&mqueue_fs_type); |
| 1342 | if (error) |
| 1343 | goto out_sysctl; |
| 1344 | |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame^] | 1345 | spin_lock_init(&mq_lock); |
| 1346 | |
| 1347 | init_ipc_ns.mq_mnt = kern_mount_data(&mqueue_fs_type, &init_ipc_ns); |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 1348 | if (IS_ERR(init_ipc_ns.mq_mnt)) { |
| 1349 | error = PTR_ERR(init_ipc_ns.mq_mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | goto out_filesystem; |
| 1351 | } |
| 1352 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | return 0; |
| 1354 | |
| 1355 | out_filesystem: |
| 1356 | unregister_filesystem(&mqueue_fs_type); |
| 1357 | out_sysctl: |
| 1358 | if (mq_sysctl_table) |
| 1359 | unregister_sysctl_table(mq_sysctl_table); |
Alexey Dobriyan | 1a1d92c | 2006-09-27 01:49:40 -0700 | [diff] [blame] | 1360 | kmem_cache_destroy(mqueue_inode_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | return error; |
| 1362 | } |
| 1363 | |
| 1364 | __initcall(init_mqueue_fs); |