mqueue.c 36 KB

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