mqueue.c 34 KB

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