mqueue.c 30 KB

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