mqueue.c 30 KB

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