mqueue.c 35 KB

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