mqueue.c 30 KB

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