mqueue.c 36 KB

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