mqueue.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  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 0;
  593. if (capable(CAP_SYS_RESOURCE)) {
  594. if (attr->mq_maxmsg > HARD_MSGMAX ||
  595. attr->mq_msgsize > HARD_MSGSIZEMAX)
  596. return 0;
  597. } else {
  598. if (attr->mq_maxmsg > ipc_ns->mq_msg_max ||
  599. attr->mq_msgsize > ipc_ns->mq_msgsize_max)
  600. return 0;
  601. }
  602. /* check for overflow */
  603. if (attr->mq_msgsize > ULONG_MAX/attr->mq_maxmsg)
  604. return 0;
  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 0;
  611. return 1;
  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. if (!mq_attr_ok(ipc_ns, attr)) {
  625. ret = -EINVAL;
  626. goto out;
  627. }
  628. /* store for use during create */
  629. dentry->d_fsdata = attr;
  630. }
  631. mode &= ~current_umask();
  632. ret = mnt_want_write(ipc_ns->mq_mnt);
  633. if (ret)
  634. goto out;
  635. ret = vfs_create(dir->d_inode, dentry, mode, NULL);
  636. dentry->d_fsdata = NULL;
  637. if (ret)
  638. goto out_drop_write;
  639. result = dentry_open(dentry, ipc_ns->mq_mnt, oflag, cred);
  640. /*
  641. * dentry_open() took a persistent mnt_want_write(),
  642. * so we can now drop this one.
  643. */
  644. mnt_drop_write(ipc_ns->mq_mnt);
  645. return result;
  646. out_drop_write:
  647. mnt_drop_write(ipc_ns->mq_mnt);
  648. out:
  649. dput(dentry);
  650. mntput(ipc_ns->mq_mnt);
  651. return ERR_PTR(ret);
  652. }
  653. /* Opens existing queue */
  654. static struct file *do_open(struct ipc_namespace *ipc_ns,
  655. struct dentry *dentry, int oflag)
  656. {
  657. int ret;
  658. const struct cred *cred = current_cred();
  659. static const int oflag2acc[O_ACCMODE] = { MAY_READ, MAY_WRITE,
  660. MAY_READ | MAY_WRITE };
  661. if ((oflag & O_ACCMODE) == (O_RDWR | O_WRONLY)) {
  662. ret = -EINVAL;
  663. goto err;
  664. }
  665. if (inode_permission(dentry->d_inode, oflag2acc[oflag & O_ACCMODE])) {
  666. ret = -EACCES;
  667. goto err;
  668. }
  669. return dentry_open(dentry, ipc_ns->mq_mnt, oflag, cred);
  670. err:
  671. dput(dentry);
  672. mntput(ipc_ns->mq_mnt);
  673. return ERR_PTR(ret);
  674. }
  675. SYSCALL_DEFINE4(mq_open, const char __user *, u_name, int, oflag, umode_t, mode,
  676. struct mq_attr __user *, u_attr)
  677. {
  678. struct dentry *dentry;
  679. struct file *filp;
  680. char *name;
  681. struct mq_attr attr;
  682. int fd, error;
  683. struct ipc_namespace *ipc_ns = current->nsproxy->ipc_ns;
  684. if (u_attr && copy_from_user(&attr, u_attr, sizeof(struct mq_attr)))
  685. return -EFAULT;
  686. audit_mq_open(oflag, mode, u_attr ? &attr : NULL);
  687. if (IS_ERR(name = getname(u_name)))
  688. return PTR_ERR(name);
  689. fd = get_unused_fd_flags(O_CLOEXEC);
  690. if (fd < 0)
  691. goto out_putname;
  692. mutex_lock(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex);
  693. dentry = lookup_one_len(name, ipc_ns->mq_mnt->mnt_root, strlen(name));
  694. if (IS_ERR(dentry)) {
  695. error = PTR_ERR(dentry);
  696. goto out_putfd;
  697. }
  698. mntget(ipc_ns->mq_mnt);
  699. if (oflag & O_CREAT) {
  700. if (dentry->d_inode) { /* entry already exists */
  701. audit_inode(name, dentry);
  702. if (oflag & O_EXCL) {
  703. error = -EEXIST;
  704. goto out;
  705. }
  706. filp = do_open(ipc_ns, dentry, oflag);
  707. } else {
  708. filp = do_create(ipc_ns, ipc_ns->mq_mnt->mnt_root,
  709. dentry, oflag, mode,
  710. u_attr ? &attr : NULL);
  711. }
  712. } else {
  713. if (!dentry->d_inode) {
  714. error = -ENOENT;
  715. goto out;
  716. }
  717. audit_inode(name, dentry);
  718. filp = do_open(ipc_ns, dentry, oflag);
  719. }
  720. if (IS_ERR(filp)) {
  721. error = PTR_ERR(filp);
  722. goto out_putfd;
  723. }
  724. fd_install(fd, filp);
  725. goto out_upsem;
  726. out:
  727. dput(dentry);
  728. mntput(ipc_ns->mq_mnt);
  729. out_putfd:
  730. put_unused_fd(fd);
  731. fd = error;
  732. out_upsem:
  733. mutex_unlock(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex);
  734. out_putname:
  735. putname(name);
  736. return fd;
  737. }
  738. SYSCALL_DEFINE1(mq_unlink, const char __user *, u_name)
  739. {
  740. int err;
  741. char *name;
  742. struct dentry *dentry;
  743. struct inode *inode = NULL;
  744. struct ipc_namespace *ipc_ns = current->nsproxy->ipc_ns;
  745. name = getname(u_name);
  746. if (IS_ERR(name))
  747. return PTR_ERR(name);
  748. mutex_lock_nested(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex,
  749. I_MUTEX_PARENT);
  750. dentry = lookup_one_len(name, ipc_ns->mq_mnt->mnt_root, strlen(name));
  751. if (IS_ERR(dentry)) {
  752. err = PTR_ERR(dentry);
  753. goto out_unlock;
  754. }
  755. if (!dentry->d_inode) {
  756. err = -ENOENT;
  757. goto out_err;
  758. }
  759. inode = dentry->d_inode;
  760. if (inode)
  761. ihold(inode);
  762. err = mnt_want_write(ipc_ns->mq_mnt);
  763. if (err)
  764. goto out_err;
  765. err = vfs_unlink(dentry->d_parent->d_inode, dentry);
  766. mnt_drop_write(ipc_ns->mq_mnt);
  767. out_err:
  768. dput(dentry);
  769. out_unlock:
  770. mutex_unlock(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex);
  771. putname(name);
  772. if (inode)
  773. iput(inode);
  774. return err;
  775. }
  776. /* Pipelined send and receive functions.
  777. *
  778. * If a receiver finds no waiting message, then it registers itself in the
  779. * list of waiting receivers. A sender checks that list before adding the new
  780. * message into the message array. If there is a waiting receiver, then it
  781. * bypasses the message array and directly hands the message over to the
  782. * receiver.
  783. * The receiver accepts the message and returns without grabbing the queue
  784. * spinlock. Therefore an intermediate STATE_PENDING state and memory barriers
  785. * are necessary. The same algorithm is used for sysv semaphores, see
  786. * ipc/sem.c for more details.
  787. *
  788. * The same algorithm is used for senders.
  789. */
  790. /* pipelined_send() - send a message directly to the task waiting in
  791. * sys_mq_timedreceive() (without inserting message into a queue).
  792. */
  793. static inline void pipelined_send(struct mqueue_inode_info *info,
  794. struct msg_msg *message,
  795. struct ext_wait_queue *receiver)
  796. {
  797. receiver->msg = message;
  798. list_del(&receiver->list);
  799. receiver->state = STATE_PENDING;
  800. wake_up_process(receiver->task);
  801. smp_wmb();
  802. receiver->state = STATE_READY;
  803. }
  804. /* pipelined_receive() - if there is task waiting in sys_mq_timedsend()
  805. * gets its message and put to the queue (we have one free place for sure). */
  806. static inline void pipelined_receive(struct mqueue_inode_info *info)
  807. {
  808. struct ext_wait_queue *sender = wq_get_first_waiter(info, SEND);
  809. if (!sender) {
  810. /* for poll */
  811. wake_up_interruptible(&info->wait_q);
  812. return;
  813. }
  814. if (msg_insert(sender->msg, info))
  815. return;
  816. list_del(&sender->list);
  817. sender->state = STATE_PENDING;
  818. wake_up_process(sender->task);
  819. smp_wmb();
  820. sender->state = STATE_READY;
  821. }
  822. SYSCALL_DEFINE5(mq_timedsend, mqd_t, mqdes, const char __user *, u_msg_ptr,
  823. size_t, msg_len, unsigned int, msg_prio,
  824. const struct timespec __user *, u_abs_timeout)
  825. {
  826. struct file *filp;
  827. struct inode *inode;
  828. struct ext_wait_queue wait;
  829. struct ext_wait_queue *receiver;
  830. struct msg_msg *msg_ptr;
  831. struct mqueue_inode_info *info;
  832. ktime_t expires, *timeout = NULL;
  833. struct timespec ts;
  834. int ret;
  835. if (u_abs_timeout) {
  836. int res = prepare_timeout(u_abs_timeout, &expires, &ts);
  837. if (res)
  838. return res;
  839. timeout = &expires;
  840. }
  841. if (unlikely(msg_prio >= (unsigned long) MQ_PRIO_MAX))
  842. return -EINVAL;
  843. audit_mq_sendrecv(mqdes, msg_len, msg_prio, timeout ? &ts : NULL);
  844. filp = fget(mqdes);
  845. if (unlikely(!filp)) {
  846. ret = -EBADF;
  847. goto out;
  848. }
  849. inode = filp->f_path.dentry->d_inode;
  850. if (unlikely(filp->f_op != &mqueue_file_operations)) {
  851. ret = -EBADF;
  852. goto out_fput;
  853. }
  854. info = MQUEUE_I(inode);
  855. audit_inode(NULL, filp->f_path.dentry);
  856. if (unlikely(!(filp->f_mode & FMODE_WRITE))) {
  857. ret = -EBADF;
  858. goto out_fput;
  859. }
  860. if (unlikely(msg_len > info->attr.mq_msgsize)) {
  861. ret = -EMSGSIZE;
  862. goto out_fput;
  863. }
  864. /* First try to allocate memory, before doing anything with
  865. * existing queues. */
  866. msg_ptr = load_msg(u_msg_ptr, msg_len);
  867. if (IS_ERR(msg_ptr)) {
  868. ret = PTR_ERR(msg_ptr);
  869. goto out_fput;
  870. }
  871. msg_ptr->m_ts = msg_len;
  872. msg_ptr->m_type = msg_prio;
  873. spin_lock(&info->lock);
  874. if (info->attr.mq_curmsgs == info->attr.mq_maxmsg) {
  875. if (filp->f_flags & O_NONBLOCK) {
  876. spin_unlock(&info->lock);
  877. ret = -EAGAIN;
  878. } else {
  879. wait.task = current;
  880. wait.msg = (void *) msg_ptr;
  881. wait.state = STATE_NONE;
  882. ret = wq_sleep(info, SEND, timeout, &wait);
  883. }
  884. if (ret < 0)
  885. free_msg(msg_ptr);
  886. } else {
  887. receiver = wq_get_first_waiter(info, RECV);
  888. if (receiver) {
  889. pipelined_send(info, msg_ptr, receiver);
  890. } else {
  891. /* adds message to the queue */
  892. if (msg_insert(msg_ptr, info)) {
  893. free_msg(msg_ptr);
  894. ret = -ENOMEM;
  895. spin_unlock(&info->lock);
  896. goto out_fput;
  897. }
  898. __do_notify(info);
  899. }
  900. inode->i_atime = inode->i_mtime = inode->i_ctime =
  901. CURRENT_TIME;
  902. spin_unlock(&info->lock);
  903. ret = 0;
  904. }
  905. out_fput:
  906. fput(filp);
  907. out:
  908. return ret;
  909. }
  910. SYSCALL_DEFINE5(mq_timedreceive, mqd_t, mqdes, char __user *, u_msg_ptr,
  911. size_t, msg_len, unsigned int __user *, u_msg_prio,
  912. const struct timespec __user *, u_abs_timeout)
  913. {
  914. ssize_t ret;
  915. struct msg_msg *msg_ptr;
  916. struct file *filp;
  917. struct inode *inode;
  918. struct mqueue_inode_info *info;
  919. struct ext_wait_queue wait;
  920. ktime_t expires, *timeout = NULL;
  921. struct timespec ts;
  922. if (u_abs_timeout) {
  923. int res = prepare_timeout(u_abs_timeout, &expires, &ts);
  924. if (res)
  925. return res;
  926. timeout = &expires;
  927. }
  928. audit_mq_sendrecv(mqdes, msg_len, 0, timeout ? &ts : NULL);
  929. filp = fget(mqdes);
  930. if (unlikely(!filp)) {
  931. ret = -EBADF;
  932. goto out;
  933. }
  934. inode = filp->f_path.dentry->d_inode;
  935. if (unlikely(filp->f_op != &mqueue_file_operations)) {
  936. ret = -EBADF;
  937. goto out_fput;
  938. }
  939. info = MQUEUE_I(inode);
  940. audit_inode(NULL, filp->f_path.dentry);
  941. if (unlikely(!(filp->f_mode & FMODE_READ))) {
  942. ret = -EBADF;
  943. goto out_fput;
  944. }
  945. /* checks if buffer is big enough */
  946. if (unlikely(msg_len < info->attr.mq_msgsize)) {
  947. ret = -EMSGSIZE;
  948. goto out_fput;
  949. }
  950. spin_lock(&info->lock);
  951. if (info->attr.mq_curmsgs == 0) {
  952. if (filp->f_flags & O_NONBLOCK) {
  953. spin_unlock(&info->lock);
  954. ret = -EAGAIN;
  955. } else {
  956. wait.task = current;
  957. wait.state = STATE_NONE;
  958. ret = wq_sleep(info, RECV, timeout, &wait);
  959. msg_ptr = wait.msg;
  960. }
  961. } else {
  962. msg_ptr = msg_get(info);
  963. inode->i_atime = inode->i_mtime = inode->i_ctime =
  964. CURRENT_TIME;
  965. /* There is now free space in queue. */
  966. pipelined_receive(info);
  967. spin_unlock(&info->lock);
  968. ret = 0;
  969. }
  970. if (ret == 0) {
  971. ret = msg_ptr->m_ts;
  972. if ((u_msg_prio && put_user(msg_ptr->m_type, u_msg_prio)) ||
  973. store_msg(u_msg_ptr, msg_ptr, msg_ptr->m_ts)) {
  974. ret = -EFAULT;
  975. }
  976. free_msg(msg_ptr);
  977. }
  978. out_fput:
  979. fput(filp);
  980. out:
  981. return ret;
  982. }
  983. /*
  984. * Notes: the case when user wants us to deregister (with NULL as pointer)
  985. * and he isn't currently owner of notification, will be silently discarded.
  986. * It isn't explicitly defined in the POSIX.
  987. */
  988. SYSCALL_DEFINE2(mq_notify, mqd_t, mqdes,
  989. const struct sigevent __user *, u_notification)
  990. {
  991. int ret;
  992. struct file *filp;
  993. struct sock *sock;
  994. struct inode *inode;
  995. struct sigevent notification;
  996. struct mqueue_inode_info *info;
  997. struct sk_buff *nc;
  998. if (u_notification) {
  999. if (copy_from_user(&notification, u_notification,
  1000. sizeof(struct sigevent)))
  1001. return -EFAULT;
  1002. }
  1003. audit_mq_notify(mqdes, u_notification ? &notification : NULL);
  1004. nc = NULL;
  1005. sock = NULL;
  1006. if (u_notification != NULL) {
  1007. if (unlikely(notification.sigev_notify != SIGEV_NONE &&
  1008. notification.sigev_notify != SIGEV_SIGNAL &&
  1009. notification.sigev_notify != SIGEV_THREAD))
  1010. return -EINVAL;
  1011. if (notification.sigev_notify == SIGEV_SIGNAL &&
  1012. !valid_signal(notification.sigev_signo)) {
  1013. return -EINVAL;
  1014. }
  1015. if (notification.sigev_notify == SIGEV_THREAD) {
  1016. long timeo;
  1017. /* create the notify skb */
  1018. nc = alloc_skb(NOTIFY_COOKIE_LEN, GFP_KERNEL);
  1019. if (!nc) {
  1020. ret = -ENOMEM;
  1021. goto out;
  1022. }
  1023. if (copy_from_user(nc->data,
  1024. notification.sigev_value.sival_ptr,
  1025. NOTIFY_COOKIE_LEN)) {
  1026. ret = -EFAULT;
  1027. goto out;
  1028. }
  1029. /* TODO: add a header? */
  1030. skb_put(nc, NOTIFY_COOKIE_LEN);
  1031. /* and attach it to the socket */
  1032. retry:
  1033. filp = fget(notification.sigev_signo);
  1034. if (!filp) {
  1035. ret = -EBADF;
  1036. goto out;
  1037. }
  1038. sock = netlink_getsockbyfilp(filp);
  1039. fput(filp);
  1040. if (IS_ERR(sock)) {
  1041. ret = PTR_ERR(sock);
  1042. sock = NULL;
  1043. goto out;
  1044. }
  1045. timeo = MAX_SCHEDULE_TIMEOUT;
  1046. ret = netlink_attachskb(sock, nc, &timeo, NULL);
  1047. if (ret == 1)
  1048. goto retry;
  1049. if (ret) {
  1050. sock = NULL;
  1051. nc = NULL;
  1052. goto out;
  1053. }
  1054. }
  1055. }
  1056. filp = fget(mqdes);
  1057. if (!filp) {
  1058. ret = -EBADF;
  1059. goto out;
  1060. }
  1061. inode = filp->f_path.dentry->d_inode;
  1062. if (unlikely(filp->f_op != &mqueue_file_operations)) {
  1063. ret = -EBADF;
  1064. goto out_fput;
  1065. }
  1066. info = MQUEUE_I(inode);
  1067. ret = 0;
  1068. spin_lock(&info->lock);
  1069. if (u_notification == NULL) {
  1070. if (info->notify_owner == task_tgid(current)) {
  1071. remove_notification(info);
  1072. inode->i_atime = inode->i_ctime = CURRENT_TIME;
  1073. }
  1074. } else if (info->notify_owner != NULL) {
  1075. ret = -EBUSY;
  1076. } else {
  1077. switch (notification.sigev_notify) {
  1078. case SIGEV_NONE:
  1079. info->notify.sigev_notify = SIGEV_NONE;
  1080. break;
  1081. case SIGEV_THREAD:
  1082. info->notify_sock = sock;
  1083. info->notify_cookie = nc;
  1084. sock = NULL;
  1085. nc = NULL;
  1086. info->notify.sigev_notify = SIGEV_THREAD;
  1087. break;
  1088. case SIGEV_SIGNAL:
  1089. info->notify.sigev_signo = notification.sigev_signo;
  1090. info->notify.sigev_value = notification.sigev_value;
  1091. info->notify.sigev_notify = SIGEV_SIGNAL;
  1092. break;
  1093. }
  1094. info->notify_owner = get_pid(task_tgid(current));
  1095. info->notify_user_ns = get_user_ns(current_user_ns());
  1096. inode->i_atime = inode->i_ctime = CURRENT_TIME;
  1097. }
  1098. spin_unlock(&info->lock);
  1099. out_fput:
  1100. fput(filp);
  1101. out:
  1102. if (sock) {
  1103. netlink_detachskb(sock, nc);
  1104. } else if (nc) {
  1105. dev_kfree_skb(nc);
  1106. }
  1107. return ret;
  1108. }
  1109. SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes,
  1110. const struct mq_attr __user *, u_mqstat,
  1111. struct mq_attr __user *, u_omqstat)
  1112. {
  1113. int ret;
  1114. struct mq_attr mqstat, omqstat;
  1115. struct file *filp;
  1116. struct inode *inode;
  1117. struct mqueue_inode_info *info;
  1118. if (u_mqstat != NULL) {
  1119. if (copy_from_user(&mqstat, u_mqstat, sizeof(struct mq_attr)))
  1120. return -EFAULT;
  1121. if (mqstat.mq_flags & (~O_NONBLOCK))
  1122. return -EINVAL;
  1123. }
  1124. filp = fget(mqdes);
  1125. if (!filp) {
  1126. ret = -EBADF;
  1127. goto out;
  1128. }
  1129. inode = filp->f_path.dentry->d_inode;
  1130. if (unlikely(filp->f_op != &mqueue_file_operations)) {
  1131. ret = -EBADF;
  1132. goto out_fput;
  1133. }
  1134. info = MQUEUE_I(inode);
  1135. spin_lock(&info->lock);
  1136. omqstat = info->attr;
  1137. omqstat.mq_flags = filp->f_flags & O_NONBLOCK;
  1138. if (u_mqstat) {
  1139. audit_mq_getsetattr(mqdes, &mqstat);
  1140. spin_lock(&filp->f_lock);
  1141. if (mqstat.mq_flags & O_NONBLOCK)
  1142. filp->f_flags |= O_NONBLOCK;
  1143. else
  1144. filp->f_flags &= ~O_NONBLOCK;
  1145. spin_unlock(&filp->f_lock);
  1146. inode->i_atime = inode->i_ctime = CURRENT_TIME;
  1147. }
  1148. spin_unlock(&info->lock);
  1149. ret = 0;
  1150. if (u_omqstat != NULL && copy_to_user(u_omqstat, &omqstat,
  1151. sizeof(struct mq_attr)))
  1152. ret = -EFAULT;
  1153. out_fput:
  1154. fput(filp);
  1155. out:
  1156. return ret;
  1157. }
  1158. static const struct inode_operations mqueue_dir_inode_operations = {
  1159. .lookup = simple_lookup,
  1160. .create = mqueue_create,
  1161. .unlink = mqueue_unlink,
  1162. };
  1163. static const struct file_operations mqueue_file_operations = {
  1164. .flush = mqueue_flush_file,
  1165. .poll = mqueue_poll_file,
  1166. .read = mqueue_read_file,
  1167. .llseek = default_llseek,
  1168. };
  1169. static const struct super_operations mqueue_super_ops = {
  1170. .alloc_inode = mqueue_alloc_inode,
  1171. .destroy_inode = mqueue_destroy_inode,
  1172. .evict_inode = mqueue_evict_inode,
  1173. .statfs = simple_statfs,
  1174. };
  1175. static struct file_system_type mqueue_fs_type = {
  1176. .name = "mqueue",
  1177. .mount = mqueue_mount,
  1178. .kill_sb = kill_litter_super,
  1179. };
  1180. int mq_init_ns(struct ipc_namespace *ns)
  1181. {
  1182. ns->mq_queues_count = 0;
  1183. ns->mq_queues_max = DFLT_QUEUESMAX;
  1184. ns->mq_msg_max = DFLT_MSGMAX;
  1185. ns->mq_msgsize_max = DFLT_MSGSIZEMAX;
  1186. ns->mq_msg_default = DFLT_MSG;
  1187. ns->mq_msgsize_default = DFLT_MSGSIZE;
  1188. ns->mq_mnt = kern_mount_data(&mqueue_fs_type, ns);
  1189. if (IS_ERR(ns->mq_mnt)) {
  1190. int err = PTR_ERR(ns->mq_mnt);
  1191. ns->mq_mnt = NULL;
  1192. return err;
  1193. }
  1194. return 0;
  1195. }
  1196. void mq_clear_sbinfo(struct ipc_namespace *ns)
  1197. {
  1198. ns->mq_mnt->mnt_sb->s_fs_info = NULL;
  1199. }
  1200. void mq_put_mnt(struct ipc_namespace *ns)
  1201. {
  1202. kern_unmount(ns->mq_mnt);
  1203. }
  1204. static int __init init_mqueue_fs(void)
  1205. {
  1206. int error;
  1207. mqueue_inode_cachep = kmem_cache_create("mqueue_inode_cache",
  1208. sizeof(struct mqueue_inode_info), 0,
  1209. SLAB_HWCACHE_ALIGN, init_once);
  1210. if (mqueue_inode_cachep == NULL)
  1211. return -ENOMEM;
  1212. /* ignore failures - they are not fatal */
  1213. mq_sysctl_table = mq_register_sysctl_table();
  1214. error = register_filesystem(&mqueue_fs_type);
  1215. if (error)
  1216. goto out_sysctl;
  1217. spin_lock_init(&mq_lock);
  1218. error = mq_init_ns(&init_ipc_ns);
  1219. if (error)
  1220. goto out_filesystem;
  1221. return 0;
  1222. out_filesystem:
  1223. unregister_filesystem(&mqueue_fs_type);
  1224. out_sysctl:
  1225. if (mq_sysctl_table)
  1226. unregister_sysctl_table(mq_sysctl_table);
  1227. kmem_cache_destroy(mqueue_inode_cachep);
  1228. return error;
  1229. }
  1230. __initcall(init_mqueue_fs);