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