mqueue.c 36 KB

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