mqueue.c 30 KB

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