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@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. if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
  186. SLAB_CTOR_CONSTRUCTOR)
  187. inode_init_once(&p->vfs_inode);
  188. }
  189. static struct inode *mqueue_alloc_inode(struct super_block *sb)
  190. {
  191. struct mqueue_inode_info *ei;
  192. ei = kmem_cache_alloc(mqueue_inode_cachep, GFP_KERNEL);
  193. if (!ei)
  194. return NULL;
  195. return &ei->vfs_inode;
  196. }
  197. static void mqueue_destroy_inode(struct inode *inode)
  198. {
  199. kmem_cache_free(mqueue_inode_cachep, MQUEUE_I(inode));
  200. }
  201. static void mqueue_delete_inode(struct inode *inode)
  202. {
  203. struct mqueue_inode_info *info;
  204. struct user_struct *user;
  205. unsigned long mq_bytes;
  206. int i;
  207. if (S_ISDIR(inode->i_mode)) {
  208. clear_inode(inode);
  209. return;
  210. }
  211. info = MQUEUE_I(inode);
  212. spin_lock(&info->lock);
  213. for (i = 0; i < info->attr.mq_curmsgs; i++)
  214. free_msg(info->messages[i]);
  215. kfree(info->messages);
  216. spin_unlock(&info->lock);
  217. clear_inode(inode);
  218. mq_bytes = (info->attr.mq_maxmsg * sizeof(struct msg_msg *) +
  219. (info->attr.mq_maxmsg * info->attr.mq_msgsize));
  220. user = info->user;
  221. if (user) {
  222. spin_lock(&mq_lock);
  223. user->mq_bytes -= mq_bytes;
  224. queues_count--;
  225. spin_unlock(&mq_lock);
  226. free_uid(user);
  227. }
  228. }
  229. static int mqueue_create(struct inode *dir, struct dentry *dentry,
  230. int mode, struct nameidata *nd)
  231. {
  232. struct inode *inode;
  233. struct mq_attr *attr = dentry->d_fsdata;
  234. int error;
  235. spin_lock(&mq_lock);
  236. if (queues_count >= queues_max && !capable(CAP_SYS_RESOURCE)) {
  237. error = -ENOSPC;
  238. goto out_lock;
  239. }
  240. queues_count++;
  241. spin_unlock(&mq_lock);
  242. inode = mqueue_get_inode(dir->i_sb, mode, attr);
  243. if (!inode) {
  244. error = -ENOMEM;
  245. spin_lock(&mq_lock);
  246. queues_count--;
  247. goto out_lock;
  248. }
  249. dir->i_size += DIRENT_SIZE;
  250. dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
  251. d_instantiate(dentry, inode);
  252. dget(dentry);
  253. return 0;
  254. out_lock:
  255. spin_unlock(&mq_lock);
  256. return error;
  257. }
  258. static int mqueue_unlink(struct inode *dir, struct dentry *dentry)
  259. {
  260. struct inode *inode = dentry->d_inode;
  261. dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
  262. dir->i_size -= DIRENT_SIZE;
  263. drop_nlink(inode);
  264. dput(dentry);
  265. return 0;
  266. }
  267. /*
  268. * This is routine for system read from queue file.
  269. * To avoid mess with doing here some sort of mq_receive we allow
  270. * to read only queue size & notification info (the only values
  271. * that are interesting from user point of view and aren't accessible
  272. * through std routines)
  273. */
  274. static ssize_t mqueue_read_file(struct file *filp, char __user *u_data,
  275. size_t count, loff_t * off)
  276. {
  277. struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode);
  278. char buffer[FILENT_SIZE];
  279. size_t slen;
  280. loff_t o;
  281. if (!count)
  282. return 0;
  283. spin_lock(&info->lock);
  284. snprintf(buffer, sizeof(buffer),
  285. "QSIZE:%-10lu NOTIFY:%-5d SIGNO:%-5d NOTIFY_PID:%-6d\n",
  286. info->qsize,
  287. info->notify_owner ? info->notify.sigev_notify : 0,
  288. (info->notify_owner &&
  289. info->notify.sigev_notify == SIGEV_SIGNAL) ?
  290. info->notify.sigev_signo : 0,
  291. pid_nr(info->notify_owner));
  292. spin_unlock(&info->lock);
  293. buffer[sizeof(buffer)-1] = '\0';
  294. slen = strlen(buffer)+1;
  295. o = *off;
  296. if (o > slen)
  297. return 0;
  298. if (o + count > slen)
  299. count = slen - o;
  300. if (copy_to_user(u_data, buffer + o, count))
  301. return -EFAULT;
  302. *off = o + count;
  303. filp->f_path.dentry->d_inode->i_atime = filp->f_path.dentry->d_inode->i_ctime = CURRENT_TIME;
  304. return count;
  305. }
  306. static int mqueue_flush_file(struct file *filp, fl_owner_t id)
  307. {
  308. struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode);
  309. spin_lock(&info->lock);
  310. if (task_tgid(current) == info->notify_owner)
  311. remove_notification(info);
  312. spin_unlock(&info->lock);
  313. return 0;
  314. }
  315. static unsigned int mqueue_poll_file(struct file *filp, struct poll_table_struct *poll_tab)
  316. {
  317. struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode);
  318. int retval = 0;
  319. poll_wait(filp, &info->wait_q, poll_tab);
  320. spin_lock(&info->lock);
  321. if (info->attr.mq_curmsgs)
  322. retval = POLLIN | POLLRDNORM;
  323. if (info->attr.mq_curmsgs < info->attr.mq_maxmsg)
  324. retval |= POLLOUT | POLLWRNORM;
  325. spin_unlock(&info->lock);
  326. return retval;
  327. }
  328. /* Adds current to info->e_wait_q[sr] before element with smaller prio */
  329. static void wq_add(struct mqueue_inode_info *info, int sr,
  330. struct ext_wait_queue *ewp)
  331. {
  332. struct ext_wait_queue *walk;
  333. ewp->task = current;
  334. list_for_each_entry(walk, &info->e_wait_q[sr].list, list) {
  335. if (walk->task->static_prio <= current->static_prio) {
  336. list_add_tail(&ewp->list, &walk->list);
  337. return;
  338. }
  339. }
  340. list_add_tail(&ewp->list, &info->e_wait_q[sr].list);
  341. }
  342. /*
  343. * Puts current task to sleep. Caller must hold queue lock. After return
  344. * lock isn't held.
  345. * sr: SEND or RECV
  346. */
  347. static int wq_sleep(struct mqueue_inode_info *info, int sr,
  348. long timeout, struct ext_wait_queue *ewp)
  349. {
  350. int retval;
  351. signed long time;
  352. wq_add(info, sr, ewp);
  353. for (;;) {
  354. set_current_state(TASK_INTERRUPTIBLE);
  355. spin_unlock(&info->lock);
  356. time = schedule_timeout(timeout);
  357. while (ewp->state == STATE_PENDING)
  358. cpu_relax();
  359. if (ewp->state == STATE_READY) {
  360. retval = 0;
  361. goto out;
  362. }
  363. spin_lock(&info->lock);
  364. if (ewp->state == STATE_READY) {
  365. retval = 0;
  366. goto out_unlock;
  367. }
  368. if (signal_pending(current)) {
  369. retval = -ERESTARTSYS;
  370. break;
  371. }
  372. if (time == 0) {
  373. retval = -ETIMEDOUT;
  374. break;
  375. }
  376. }
  377. list_del(&ewp->list);
  378. out_unlock:
  379. spin_unlock(&info->lock);
  380. out:
  381. return retval;
  382. }
  383. /*
  384. * Returns waiting task that should be serviced first or NULL if none exists
  385. */
  386. static struct ext_wait_queue *wq_get_first_waiter(
  387. struct mqueue_inode_info *info, int sr)
  388. {
  389. struct list_head *ptr;
  390. ptr = info->e_wait_q[sr].list.prev;
  391. if (ptr == &info->e_wait_q[sr].list)
  392. return NULL;
  393. return list_entry(ptr, struct ext_wait_queue, list);
  394. }
  395. /* Auxiliary functions to manipulate messages' list */
  396. static void msg_insert(struct msg_msg *ptr, struct mqueue_inode_info *info)
  397. {
  398. int k;
  399. k = info->attr.mq_curmsgs - 1;
  400. while (k >= 0 && info->messages[k]->m_type >= ptr->m_type) {
  401. info->messages[k + 1] = info->messages[k];
  402. k--;
  403. }
  404. info->attr.mq_curmsgs++;
  405. info->qsize += ptr->m_ts;
  406. info->messages[k + 1] = ptr;
  407. }
  408. static inline struct msg_msg *msg_get(struct mqueue_inode_info *info)
  409. {
  410. info->qsize -= info->messages[--info->attr.mq_curmsgs]->m_ts;
  411. return info->messages[info->attr.mq_curmsgs];
  412. }
  413. static inline void set_cookie(struct sk_buff *skb, char code)
  414. {
  415. ((char*)skb->data)[NOTIFY_COOKIE_LEN-1] = code;
  416. }
  417. /*
  418. * The next function is only to split too long sys_mq_timedsend
  419. */
  420. static void __do_notify(struct mqueue_inode_info *info)
  421. {
  422. /* notification
  423. * invoked when there is registered process and there isn't process
  424. * waiting synchronously for message AND state of queue changed from
  425. * empty to not empty. Here we are sure that no one is waiting
  426. * synchronously. */
  427. if (info->notify_owner &&
  428. info->attr.mq_curmsgs == 1) {
  429. struct siginfo sig_i;
  430. switch (info->notify.sigev_notify) {
  431. case SIGEV_NONE:
  432. break;
  433. case SIGEV_SIGNAL:
  434. /* sends signal */
  435. sig_i.si_signo = info->notify.sigev_signo;
  436. sig_i.si_errno = 0;
  437. sig_i.si_code = SI_MESGQ;
  438. sig_i.si_value = info->notify.sigev_value;
  439. sig_i.si_pid = current->tgid;
  440. sig_i.si_uid = current->uid;
  441. kill_pid_info(info->notify.sigev_signo,
  442. &sig_i, info->notify_owner);
  443. break;
  444. case SIGEV_THREAD:
  445. set_cookie(info->notify_cookie, NOTIFY_WOKENUP);
  446. netlink_sendskb(info->notify_sock,
  447. info->notify_cookie, 0);
  448. break;
  449. }
  450. /* after notification unregisters process */
  451. put_pid(info->notify_owner);
  452. info->notify_owner = NULL;
  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 != NULL &&
  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. put_pid(info->notify_owner);
  490. info->notify_owner = NULL;
  491. }
  492. static int mq_attr_ok(struct mq_attr *attr)
  493. {
  494. if (attr->mq_maxmsg <= 0 || attr->mq_msgsize <= 0)
  495. return 0;
  496. if (capable(CAP_SYS_RESOURCE)) {
  497. if (attr->mq_maxmsg > HARD_MSGMAX)
  498. return 0;
  499. } else {
  500. if (attr->mq_maxmsg > msg_max ||
  501. attr->mq_msgsize > msgsize_max)
  502. return 0;
  503. }
  504. /* check for overflow */
  505. if (attr->mq_msgsize > ULONG_MAX/attr->mq_maxmsg)
  506. return 0;
  507. if ((unsigned long)(attr->mq_maxmsg * attr->mq_msgsize) +
  508. (attr->mq_maxmsg * sizeof (struct msg_msg *)) <
  509. (unsigned long)(attr->mq_maxmsg * attr->mq_msgsize))
  510. return 0;
  511. return 1;
  512. }
  513. /*
  514. * Invoked when creating a new queue via sys_mq_open
  515. */
  516. static struct file *do_create(struct dentry *dir, struct dentry *dentry,
  517. int oflag, mode_t mode, struct mq_attr __user *u_attr)
  518. {
  519. struct mq_attr attr;
  520. int ret;
  521. if (u_attr) {
  522. ret = -EFAULT;
  523. if (copy_from_user(&attr, u_attr, sizeof(attr)))
  524. goto out;
  525. ret = -EINVAL;
  526. if (!mq_attr_ok(&attr))
  527. goto out;
  528. /* store for use during create */
  529. dentry->d_fsdata = &attr;
  530. }
  531. mode &= ~current->fs->umask;
  532. ret = vfs_create(dir->d_inode, dentry, mode, NULL);
  533. dentry->d_fsdata = NULL;
  534. if (ret)
  535. goto out;
  536. return dentry_open(dentry, mqueue_mnt, oflag);
  537. out:
  538. dput(dentry);
  539. mntput(mqueue_mnt);
  540. return ERR_PTR(ret);
  541. }
  542. /* Opens existing queue */
  543. static struct file *do_open(struct dentry *dentry, int oflag)
  544. {
  545. static int oflag2acc[O_ACCMODE] = { MAY_READ, MAY_WRITE,
  546. MAY_READ | MAY_WRITE };
  547. if ((oflag & O_ACCMODE) == (O_RDWR | O_WRONLY)) {
  548. dput(dentry);
  549. mntput(mqueue_mnt);
  550. return ERR_PTR(-EINVAL);
  551. }
  552. if (permission(dentry->d_inode, oflag2acc[oflag & O_ACCMODE], NULL)) {
  553. dput(dentry);
  554. mntput(mqueue_mnt);
  555. return ERR_PTR(-EACCES);
  556. }
  557. return dentry_open(dentry, mqueue_mnt, oflag);
  558. }
  559. asmlinkage long sys_mq_open(const char __user *u_name, int oflag, mode_t mode,
  560. struct mq_attr __user *u_attr)
  561. {
  562. struct dentry *dentry;
  563. struct file *filp;
  564. char *name;
  565. int fd, error;
  566. error = audit_mq_open(oflag, mode, u_attr);
  567. if (error != 0)
  568. return error;
  569. if (IS_ERR(name = getname(u_name)))
  570. return PTR_ERR(name);
  571. fd = get_unused_fd();
  572. if (fd < 0)
  573. goto out_putname;
  574. mutex_lock(&mqueue_mnt->mnt_root->d_inode->i_mutex);
  575. dentry = lookup_one_len(name, mqueue_mnt->mnt_root, strlen(name));
  576. if (IS_ERR(dentry)) {
  577. error = PTR_ERR(dentry);
  578. goto out_err;
  579. }
  580. mntget(mqueue_mnt);
  581. if (oflag & O_CREAT) {
  582. if (dentry->d_inode) { /* entry already exists */
  583. error = -EEXIST;
  584. if (oflag & O_EXCL)
  585. goto out;
  586. filp = do_open(dentry, oflag);
  587. } else {
  588. filp = do_create(mqueue_mnt->mnt_root, dentry,
  589. oflag, mode, u_attr);
  590. }
  591. } else {
  592. error = -ENOENT;
  593. if (!dentry->d_inode)
  594. goto out;
  595. filp = do_open(dentry, oflag);
  596. }
  597. if (IS_ERR(filp)) {
  598. error = PTR_ERR(filp);
  599. goto out_putfd;
  600. }
  601. set_close_on_exec(fd, 1);
  602. fd_install(fd, filp);
  603. goto out_upsem;
  604. out:
  605. dput(dentry);
  606. mntput(mqueue_mnt);
  607. out_putfd:
  608. put_unused_fd(fd);
  609. out_err:
  610. fd = error;
  611. out_upsem:
  612. mutex_unlock(&mqueue_mnt->mnt_root->d_inode->i_mutex);
  613. out_putname:
  614. putname(name);
  615. return fd;
  616. }
  617. asmlinkage long sys_mq_unlink(const char __user *u_name)
  618. {
  619. int err;
  620. char *name;
  621. struct dentry *dentry;
  622. struct inode *inode = NULL;
  623. name = getname(u_name);
  624. if (IS_ERR(name))
  625. return PTR_ERR(name);
  626. mutex_lock_nested(&mqueue_mnt->mnt_root->d_inode->i_mutex,
  627. I_MUTEX_PARENT);
  628. dentry = lookup_one_len(name, mqueue_mnt->mnt_root, strlen(name));
  629. if (IS_ERR(dentry)) {
  630. err = PTR_ERR(dentry);
  631. goto out_unlock;
  632. }
  633. if (!dentry->d_inode) {
  634. err = -ENOENT;
  635. goto out_err;
  636. }
  637. inode = dentry->d_inode;
  638. if (inode)
  639. atomic_inc(&inode->i_count);
  640. err = vfs_unlink(dentry->d_parent->d_inode, dentry);
  641. out_err:
  642. dput(dentry);
  643. out_unlock:
  644. mutex_unlock(&mqueue_mnt->mnt_root->d_inode->i_mutex);
  645. putname(name);
  646. if (inode)
  647. iput(inode);
  648. return err;
  649. }
  650. /* Pipelined send and receive functions.
  651. *
  652. * If a receiver finds no waiting message, then it registers itself in the
  653. * list of waiting receivers. A sender checks that list before adding the new
  654. * message into the message array. If there is a waiting receiver, then it
  655. * bypasses the message array and directly hands the message over to the
  656. * receiver.
  657. * The receiver accepts the message and returns without grabbing the queue
  658. * spinlock. Therefore an intermediate STATE_PENDING state and memory barriers
  659. * are necessary. The same algorithm is used for sysv semaphores, see
  660. * ipc/sem.c for more details.
  661. *
  662. * The same algorithm is used for senders.
  663. */
  664. /* pipelined_send() - send a message directly to the task waiting in
  665. * sys_mq_timedreceive() (without inserting message into a queue).
  666. */
  667. static inline void pipelined_send(struct mqueue_inode_info *info,
  668. struct msg_msg *message,
  669. struct ext_wait_queue *receiver)
  670. {
  671. receiver->msg = message;
  672. list_del(&receiver->list);
  673. receiver->state = STATE_PENDING;
  674. wake_up_process(receiver->task);
  675. smp_wmb();
  676. receiver->state = STATE_READY;
  677. }
  678. /* pipelined_receive() - if there is task waiting in sys_mq_timedsend()
  679. * gets its message and put to the queue (we have one free place for sure). */
  680. static inline void pipelined_receive(struct mqueue_inode_info *info)
  681. {
  682. struct ext_wait_queue *sender = wq_get_first_waiter(info, SEND);
  683. if (!sender) {
  684. /* for poll */
  685. wake_up_interruptible(&info->wait_q);
  686. return;
  687. }
  688. msg_insert(sender->msg, info);
  689. list_del(&sender->list);
  690. sender->state = STATE_PENDING;
  691. wake_up_process(sender->task);
  692. smp_wmb();
  693. sender->state = STATE_READY;
  694. }
  695. asmlinkage long sys_mq_timedsend(mqd_t mqdes, const char __user *u_msg_ptr,
  696. size_t msg_len, unsigned int msg_prio,
  697. const struct timespec __user *u_abs_timeout)
  698. {
  699. struct file *filp;
  700. struct inode *inode;
  701. struct ext_wait_queue wait;
  702. struct ext_wait_queue *receiver;
  703. struct msg_msg *msg_ptr;
  704. struct mqueue_inode_info *info;
  705. long timeout;
  706. int ret;
  707. ret = audit_mq_timedsend(mqdes, msg_len, msg_prio, u_abs_timeout);
  708. if (ret != 0)
  709. return ret;
  710. if (unlikely(msg_prio >= (unsigned long) MQ_PRIO_MAX))
  711. return -EINVAL;
  712. timeout = prepare_timeout(u_abs_timeout);
  713. ret = -EBADF;
  714. filp = fget(mqdes);
  715. if (unlikely(!filp))
  716. goto out;
  717. inode = filp->f_path.dentry->d_inode;
  718. if (unlikely(filp->f_op != &mqueue_file_operations))
  719. goto out_fput;
  720. info = MQUEUE_I(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. if (unlikely(!(filp->f_mode & FMODE_READ)))
  795. goto out_fput;
  796. /* checks if buffer is big enough */
  797. if (unlikely(msg_len < info->attr.mq_msgsize)) {
  798. ret = -EMSGSIZE;
  799. goto out_fput;
  800. }
  801. spin_lock(&info->lock);
  802. if (info->attr.mq_curmsgs == 0) {
  803. if (filp->f_flags & O_NONBLOCK) {
  804. spin_unlock(&info->lock);
  805. ret = -EAGAIN;
  806. msg_ptr = NULL;
  807. } else if (unlikely(timeout < 0)) {
  808. spin_unlock(&info->lock);
  809. ret = timeout;
  810. msg_ptr = NULL;
  811. } else {
  812. wait.task = current;
  813. wait.state = STATE_NONE;
  814. ret = wq_sleep(info, RECV, timeout, &wait);
  815. msg_ptr = wait.msg;
  816. }
  817. } else {
  818. msg_ptr = msg_get(info);
  819. inode->i_atime = inode->i_mtime = inode->i_ctime =
  820. CURRENT_TIME;
  821. /* There is now free space in queue. */
  822. pipelined_receive(info);
  823. spin_unlock(&info->lock);
  824. ret = 0;
  825. }
  826. if (ret == 0) {
  827. ret = msg_ptr->m_ts;
  828. if ((u_msg_prio && put_user(msg_ptr->m_type, u_msg_prio)) ||
  829. store_msg(u_msg_ptr, msg_ptr, msg_ptr->m_ts)) {
  830. ret = -EFAULT;
  831. }
  832. free_msg(msg_ptr);
  833. }
  834. out_fput:
  835. fput(filp);
  836. out:
  837. return ret;
  838. }
  839. /*
  840. * Notes: the case when user wants us to deregister (with NULL as pointer)
  841. * and he isn't currently owner of notification, will be silently discarded.
  842. * It isn't explicitly defined in the POSIX.
  843. */
  844. asmlinkage long sys_mq_notify(mqd_t mqdes,
  845. const struct sigevent __user *u_notification)
  846. {
  847. int ret;
  848. struct file *filp;
  849. struct sock *sock;
  850. struct inode *inode;
  851. struct sigevent notification;
  852. struct mqueue_inode_info *info;
  853. struct sk_buff *nc;
  854. ret = audit_mq_notify(mqdes, u_notification);
  855. if (ret != 0)
  856. return ret;
  857. nc = NULL;
  858. sock = NULL;
  859. if (u_notification != NULL) {
  860. if (copy_from_user(&notification, u_notification,
  861. sizeof(struct sigevent)))
  862. return -EFAULT;
  863. if (unlikely(notification.sigev_notify != SIGEV_NONE &&
  864. notification.sigev_notify != SIGEV_SIGNAL &&
  865. notification.sigev_notify != SIGEV_THREAD))
  866. return -EINVAL;
  867. if (notification.sigev_notify == SIGEV_SIGNAL &&
  868. !valid_signal(notification.sigev_signo)) {
  869. return -EINVAL;
  870. }
  871. if (notification.sigev_notify == SIGEV_THREAD) {
  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. ret = netlink_attachskb(sock, nc, 0,
  899. MAX_SCHEDULE_TIMEOUT, 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. goto out;
  988. if (mqstat.mq_flags & O_NONBLOCK)
  989. filp->f_flags |= O_NONBLOCK;
  990. else
  991. filp->f_flags &= ~O_NONBLOCK;
  992. inode->i_atime = inode->i_ctime = CURRENT_TIME;
  993. }
  994. spin_unlock(&info->lock);
  995. ret = 0;
  996. if (u_omqstat != NULL && copy_to_user(u_omqstat, &omqstat,
  997. sizeof(struct mq_attr)))
  998. ret = -EFAULT;
  999. out_fput:
  1000. fput(filp);
  1001. out:
  1002. return ret;
  1003. }
  1004. static const struct inode_operations mqueue_dir_inode_operations = {
  1005. .lookup = simple_lookup,
  1006. .create = mqueue_create,
  1007. .unlink = mqueue_unlink,
  1008. };
  1009. static const struct file_operations mqueue_file_operations = {
  1010. .flush = mqueue_flush_file,
  1011. .poll = mqueue_poll_file,
  1012. .read = mqueue_read_file,
  1013. };
  1014. static struct super_operations mqueue_super_ops = {
  1015. .alloc_inode = mqueue_alloc_inode,
  1016. .destroy_inode = mqueue_destroy_inode,
  1017. .statfs = simple_statfs,
  1018. .delete_inode = mqueue_delete_inode,
  1019. .drop_inode = generic_delete_inode,
  1020. };
  1021. static struct file_system_type mqueue_fs_type = {
  1022. .name = "mqueue",
  1023. .get_sb = mqueue_get_sb,
  1024. .kill_sb = kill_litter_super,
  1025. };
  1026. static int msg_max_limit_min = DFLT_MSGMAX;
  1027. static int msg_max_limit_max = HARD_MSGMAX;
  1028. static int msg_maxsize_limit_min = DFLT_MSGSIZEMAX;
  1029. static int msg_maxsize_limit_max = INT_MAX;
  1030. static ctl_table mq_sysctls[] = {
  1031. {
  1032. .ctl_name = CTL_QUEUESMAX,
  1033. .procname = "queues_max",
  1034. .data = &queues_max,
  1035. .maxlen = sizeof(int),
  1036. .mode = 0644,
  1037. .proc_handler = &proc_dointvec,
  1038. },
  1039. {
  1040. .ctl_name = CTL_MSGMAX,
  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. .ctl_name = CTL_MSGSIZEMAX,
  1051. .procname = "msgsize_max",
  1052. .data = &msgsize_max,
  1053. .maxlen = sizeof(int),
  1054. .mode = 0644,
  1055. .proc_handler = &proc_dointvec_minmax,
  1056. .extra1 = &msg_maxsize_limit_min,
  1057. .extra2 = &msg_maxsize_limit_max,
  1058. },
  1059. { .ctl_name = 0 }
  1060. };
  1061. static ctl_table mq_sysctl_dir[] = {
  1062. {
  1063. .ctl_name = FS_MQUEUE,
  1064. .procname = "mqueue",
  1065. .mode = 0555,
  1066. .child = mq_sysctls,
  1067. },
  1068. { .ctl_name = 0 }
  1069. };
  1070. static ctl_table mq_sysctl_root[] = {
  1071. {
  1072. .ctl_name = CTL_FS,
  1073. .procname = "fs",
  1074. .mode = 0555,
  1075. .child = mq_sysctl_dir,
  1076. },
  1077. { .ctl_name = 0 }
  1078. };
  1079. static int __init init_mqueue_fs(void)
  1080. {
  1081. int error;
  1082. mqueue_inode_cachep = kmem_cache_create("mqueue_inode_cache",
  1083. sizeof(struct mqueue_inode_info), 0,
  1084. SLAB_HWCACHE_ALIGN, init_once, NULL);
  1085. if (mqueue_inode_cachep == NULL)
  1086. return -ENOMEM;
  1087. /* ignore failues - they are not fatal */
  1088. mq_sysctl_table = register_sysctl_table(mq_sysctl_root);
  1089. error = register_filesystem(&mqueue_fs_type);
  1090. if (error)
  1091. goto out_sysctl;
  1092. if (IS_ERR(mqueue_mnt = kern_mount(&mqueue_fs_type))) {
  1093. error = PTR_ERR(mqueue_mnt);
  1094. goto out_filesystem;
  1095. }
  1096. /* internal initialization - not common for vfs */
  1097. queues_count = 0;
  1098. spin_lock_init(&mq_lock);
  1099. return 0;
  1100. out_filesystem:
  1101. unregister_filesystem(&mqueue_fs_type);
  1102. out_sysctl:
  1103. if (mq_sysctl_table)
  1104. unregister_sysctl_table(mq_sysctl_table);
  1105. kmem_cache_destroy(mqueue_inode_cachep);
  1106. return error;
  1107. }
  1108. __initcall(init_mqueue_fs);