mqueue.c 30 KB

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