mqueue.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  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_CONSTRUCTOR)
  186. inode_init_once(&p->vfs_inode);
  187. }
  188. static struct inode *mqueue_alloc_inode(struct super_block *sb)
  189. {
  190. struct mqueue_inode_info *ei;
  191. ei = kmem_cache_alloc(mqueue_inode_cachep, GFP_KERNEL);
  192. if (!ei)
  193. return NULL;
  194. return &ei->vfs_inode;
  195. }
  196. static void mqueue_destroy_inode(struct inode *inode)
  197. {
  198. kmem_cache_free(mqueue_inode_cachep, MQUEUE_I(inode));
  199. }
  200. static void mqueue_delete_inode(struct inode *inode)
  201. {
  202. struct mqueue_inode_info *info;
  203. struct user_struct *user;
  204. unsigned long mq_bytes;
  205. int i;
  206. if (S_ISDIR(inode->i_mode)) {
  207. clear_inode(inode);
  208. return;
  209. }
  210. info = MQUEUE_I(inode);
  211. spin_lock(&info->lock);
  212. for (i = 0; i < info->attr.mq_curmsgs; i++)
  213. free_msg(info->messages[i]);
  214. kfree(info->messages);
  215. spin_unlock(&info->lock);
  216. clear_inode(inode);
  217. mq_bytes = (info->attr.mq_maxmsg * sizeof(struct msg_msg *) +
  218. (info->attr.mq_maxmsg * info->attr.mq_msgsize));
  219. user = info->user;
  220. if (user) {
  221. spin_lock(&mq_lock);
  222. user->mq_bytes -= mq_bytes;
  223. queues_count--;
  224. spin_unlock(&mq_lock);
  225. free_uid(user);
  226. }
  227. }
  228. static int mqueue_create(struct inode *dir, struct dentry *dentry,
  229. int mode, struct nameidata *nd)
  230. {
  231. struct inode *inode;
  232. struct mq_attr *attr = dentry->d_fsdata;
  233. int error;
  234. spin_lock(&mq_lock);
  235. if (queues_count >= queues_max && !capable(CAP_SYS_RESOURCE)) {
  236. error = -ENOSPC;
  237. goto out_lock;
  238. }
  239. queues_count++;
  240. spin_unlock(&mq_lock);
  241. inode = mqueue_get_inode(dir->i_sb, mode, attr);
  242. if (!inode) {
  243. error = -ENOMEM;
  244. spin_lock(&mq_lock);
  245. queues_count--;
  246. goto out_lock;
  247. }
  248. dir->i_size += DIRENT_SIZE;
  249. dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
  250. d_instantiate(dentry, inode);
  251. dget(dentry);
  252. return 0;
  253. out_lock:
  254. spin_unlock(&mq_lock);
  255. return error;
  256. }
  257. static int mqueue_unlink(struct inode *dir, struct dentry *dentry)
  258. {
  259. struct inode *inode = dentry->d_inode;
  260. dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
  261. dir->i_size -= DIRENT_SIZE;
  262. drop_nlink(inode);
  263. dput(dentry);
  264. return 0;
  265. }
  266. /*
  267. * This is routine for system read from queue file.
  268. * To avoid mess with doing here some sort of mq_receive we allow
  269. * to read only queue size & notification info (the only values
  270. * that are interesting from user point of view and aren't accessible
  271. * through std routines)
  272. */
  273. static ssize_t mqueue_read_file(struct file *filp, char __user *u_data,
  274. size_t count, loff_t * off)
  275. {
  276. struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode);
  277. char buffer[FILENT_SIZE];
  278. size_t slen;
  279. loff_t o;
  280. if (!count)
  281. return 0;
  282. spin_lock(&info->lock);
  283. snprintf(buffer, sizeof(buffer),
  284. "QSIZE:%-10lu NOTIFY:%-5d SIGNO:%-5d NOTIFY_PID:%-6d\n",
  285. info->qsize,
  286. info->notify_owner ? info->notify.sigev_notify : 0,
  287. (info->notify_owner &&
  288. info->notify.sigev_notify == SIGEV_SIGNAL) ?
  289. info->notify.sigev_signo : 0,
  290. pid_nr(info->notify_owner));
  291. spin_unlock(&info->lock);
  292. buffer[sizeof(buffer)-1] = '\0';
  293. slen = strlen(buffer)+1;
  294. o = *off;
  295. if (o > slen)
  296. return 0;
  297. if (o + count > slen)
  298. count = slen - o;
  299. if (copy_to_user(u_data, buffer + o, count))
  300. return -EFAULT;
  301. *off = o + count;
  302. filp->f_path.dentry->d_inode->i_atime = filp->f_path.dentry->d_inode->i_ctime = CURRENT_TIME;
  303. return count;
  304. }
  305. static int mqueue_flush_file(struct file *filp, fl_owner_t id)
  306. {
  307. struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode);
  308. spin_lock(&info->lock);
  309. if (task_tgid(current) == info->notify_owner)
  310. remove_notification(info);
  311. spin_unlock(&info->lock);
  312. return 0;
  313. }
  314. static unsigned int mqueue_poll_file(struct file *filp, struct poll_table_struct *poll_tab)
  315. {
  316. struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode);
  317. int retval = 0;
  318. poll_wait(filp, &info->wait_q, poll_tab);
  319. spin_lock(&info->lock);
  320. if (info->attr.mq_curmsgs)
  321. retval = POLLIN | POLLRDNORM;
  322. if (info->attr.mq_curmsgs < info->attr.mq_maxmsg)
  323. retval |= POLLOUT | POLLWRNORM;
  324. spin_unlock(&info->lock);
  325. return retval;
  326. }
  327. /* Adds current to info->e_wait_q[sr] before element with smaller prio */
  328. static void wq_add(struct mqueue_inode_info *info, int sr,
  329. struct ext_wait_queue *ewp)
  330. {
  331. struct ext_wait_queue *walk;
  332. ewp->task = current;
  333. list_for_each_entry(walk, &info->e_wait_q[sr].list, list) {
  334. if (walk->task->static_prio <= current->static_prio) {
  335. list_add_tail(&ewp->list, &walk->list);
  336. return;
  337. }
  338. }
  339. list_add_tail(&ewp->list, &info->e_wait_q[sr].list);
  340. }
  341. /*
  342. * Puts current task to sleep. Caller must hold queue lock. After return
  343. * lock isn't held.
  344. * sr: SEND or RECV
  345. */
  346. static int wq_sleep(struct mqueue_inode_info *info, int sr,
  347. long timeout, struct ext_wait_queue *ewp)
  348. {
  349. int retval;
  350. signed long time;
  351. wq_add(info, sr, ewp);
  352. for (;;) {
  353. set_current_state(TASK_INTERRUPTIBLE);
  354. spin_unlock(&info->lock);
  355. time = schedule_timeout(timeout);
  356. while (ewp->state == STATE_PENDING)
  357. cpu_relax();
  358. if (ewp->state == STATE_READY) {
  359. retval = 0;
  360. goto out;
  361. }
  362. spin_lock(&info->lock);
  363. if (ewp->state == STATE_READY) {
  364. retval = 0;
  365. goto out_unlock;
  366. }
  367. if (signal_pending(current)) {
  368. retval = -ERESTARTSYS;
  369. break;
  370. }
  371. if (time == 0) {
  372. retval = -ETIMEDOUT;
  373. break;
  374. }
  375. }
  376. list_del(&ewp->list);
  377. out_unlock:
  378. spin_unlock(&info->lock);
  379. out:
  380. return retval;
  381. }
  382. /*
  383. * Returns waiting task that should be serviced first or NULL if none exists
  384. */
  385. static struct ext_wait_queue *wq_get_first_waiter(
  386. struct mqueue_inode_info *info, int sr)
  387. {
  388. struct list_head *ptr;
  389. ptr = info->e_wait_q[sr].list.prev;
  390. if (ptr == &info->e_wait_q[sr].list)
  391. return NULL;
  392. return list_entry(ptr, struct ext_wait_queue, list);
  393. }
  394. /* Auxiliary functions to manipulate messages' list */
  395. static void msg_insert(struct msg_msg *ptr, struct mqueue_inode_info *info)
  396. {
  397. int k;
  398. k = info->attr.mq_curmsgs - 1;
  399. while (k >= 0 && info->messages[k]->m_type >= ptr->m_type) {
  400. info->messages[k + 1] = info->messages[k];
  401. k--;
  402. }
  403. info->attr.mq_curmsgs++;
  404. info->qsize += ptr->m_ts;
  405. info->messages[k + 1] = ptr;
  406. }
  407. static inline struct msg_msg *msg_get(struct mqueue_inode_info *info)
  408. {
  409. info->qsize -= info->messages[--info->attr.mq_curmsgs]->m_ts;
  410. return info->messages[info->attr.mq_curmsgs];
  411. }
  412. static inline void set_cookie(struct sk_buff *skb, char code)
  413. {
  414. ((char*)skb->data)[NOTIFY_COOKIE_LEN-1] = code;
  415. }
  416. /*
  417. * The next function is only to split too long sys_mq_timedsend
  418. */
  419. static void __do_notify(struct mqueue_inode_info *info)
  420. {
  421. /* notification
  422. * invoked when there is registered process and there isn't process
  423. * waiting synchronously for message AND state of queue changed from
  424. * empty to not empty. Here we are sure that no one is waiting
  425. * synchronously. */
  426. if (info->notify_owner &&
  427. info->attr.mq_curmsgs == 1) {
  428. struct siginfo sig_i;
  429. switch (info->notify.sigev_notify) {
  430. case SIGEV_NONE:
  431. break;
  432. case SIGEV_SIGNAL:
  433. /* sends signal */
  434. sig_i.si_signo = info->notify.sigev_signo;
  435. sig_i.si_errno = 0;
  436. sig_i.si_code = SI_MESGQ;
  437. sig_i.si_value = info->notify.sigev_value;
  438. sig_i.si_pid = current->tgid;
  439. sig_i.si_uid = current->uid;
  440. kill_pid_info(info->notify.sigev_signo,
  441. &sig_i, info->notify_owner);
  442. break;
  443. case SIGEV_THREAD:
  444. set_cookie(info->notify_cookie, NOTIFY_WOKENUP);
  445. netlink_sendskb(info->notify_sock,
  446. info->notify_cookie, 0);
  447. break;
  448. }
  449. /* after notification unregisters process */
  450. put_pid(info->notify_owner);
  451. info->notify_owner = NULL;
  452. }
  453. wake_up(&info->wait_q);
  454. }
  455. static long prepare_timeout(const struct timespec __user *u_arg)
  456. {
  457. struct timespec ts, nowts;
  458. long timeout;
  459. if (u_arg) {
  460. if (unlikely(copy_from_user(&ts, u_arg,
  461. sizeof(struct timespec))))
  462. return -EFAULT;
  463. if (unlikely(ts.tv_nsec < 0 || ts.tv_sec < 0
  464. || ts.tv_nsec >= NSEC_PER_SEC))
  465. return -EINVAL;
  466. nowts = CURRENT_TIME;
  467. /* first subtract as jiffies can't be too big */
  468. ts.tv_sec -= nowts.tv_sec;
  469. if (ts.tv_nsec < nowts.tv_nsec) {
  470. ts.tv_nsec += NSEC_PER_SEC;
  471. ts.tv_sec--;
  472. }
  473. ts.tv_nsec -= nowts.tv_nsec;
  474. if (ts.tv_sec < 0)
  475. return 0;
  476. timeout = timespec_to_jiffies(&ts) + 1;
  477. } else
  478. return MAX_SCHEDULE_TIMEOUT;
  479. return timeout;
  480. }
  481. static void remove_notification(struct mqueue_inode_info *info)
  482. {
  483. if (info->notify_owner != NULL &&
  484. info->notify.sigev_notify == SIGEV_THREAD) {
  485. set_cookie(info->notify_cookie, NOTIFY_REMOVED);
  486. netlink_sendskb(info->notify_sock, info->notify_cookie, 0);
  487. }
  488. put_pid(info->notify_owner);
  489. info->notify_owner = NULL;
  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. audit_inode(name, dentry->d_inode);
  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. audit_inode(name, dentry->d_inode);
  596. filp = do_open(dentry, oflag);
  597. }
  598. if (IS_ERR(filp)) {
  599. error = PTR_ERR(filp);
  600. goto out_putfd;
  601. }
  602. set_close_on_exec(fd, 1);
  603. fd_install(fd, filp);
  604. goto out_upsem;
  605. out:
  606. dput(dentry);
  607. mntput(mqueue_mnt);
  608. out_putfd:
  609. put_unused_fd(fd);
  610. out_err:
  611. fd = error;
  612. out_upsem:
  613. mutex_unlock(&mqueue_mnt->mnt_root->d_inode->i_mutex);
  614. out_putname:
  615. putname(name);
  616. return fd;
  617. }
  618. asmlinkage long sys_mq_unlink(const char __user *u_name)
  619. {
  620. int err;
  621. char *name;
  622. struct dentry *dentry;
  623. struct inode *inode = NULL;
  624. name = getname(u_name);
  625. if (IS_ERR(name))
  626. return PTR_ERR(name);
  627. mutex_lock_nested(&mqueue_mnt->mnt_root->d_inode->i_mutex,
  628. I_MUTEX_PARENT);
  629. dentry = lookup_one_len(name, mqueue_mnt->mnt_root, strlen(name));
  630. if (IS_ERR(dentry)) {
  631. err = PTR_ERR(dentry);
  632. goto out_unlock;
  633. }
  634. if (!dentry->d_inode) {
  635. err = -ENOENT;
  636. goto out_err;
  637. }
  638. inode = dentry->d_inode;
  639. if (inode)
  640. atomic_inc(&inode->i_count);
  641. err = vfs_unlink(dentry->d_parent->d_inode, dentry);
  642. out_err:
  643. dput(dentry);
  644. out_unlock:
  645. mutex_unlock(&mqueue_mnt->mnt_root->d_inode->i_mutex);
  646. putname(name);
  647. if (inode)
  648. iput(inode);
  649. return err;
  650. }
  651. /* Pipelined send and receive functions.
  652. *
  653. * If a receiver finds no waiting message, then it registers itself in the
  654. * list of waiting receivers. A sender checks that list before adding the new
  655. * message into the message array. If there is a waiting receiver, then it
  656. * bypasses the message array and directly hands the message over to the
  657. * receiver.
  658. * The receiver accepts the message and returns without grabbing the queue
  659. * spinlock. Therefore an intermediate STATE_PENDING state and memory barriers
  660. * are necessary. The same algorithm is used for sysv semaphores, see
  661. * ipc/sem.c for more details.
  662. *
  663. * The same algorithm is used for senders.
  664. */
  665. /* pipelined_send() - send a message directly to the task waiting in
  666. * sys_mq_timedreceive() (without inserting message into a queue).
  667. */
  668. static inline void pipelined_send(struct mqueue_inode_info *info,
  669. struct msg_msg *message,
  670. struct ext_wait_queue *receiver)
  671. {
  672. receiver->msg = message;
  673. list_del(&receiver->list);
  674. receiver->state = STATE_PENDING;
  675. wake_up_process(receiver->task);
  676. smp_wmb();
  677. receiver->state = STATE_READY;
  678. }
  679. /* pipelined_receive() - if there is task waiting in sys_mq_timedsend()
  680. * gets its message and put to the queue (we have one free place for sure). */
  681. static inline void pipelined_receive(struct mqueue_inode_info *info)
  682. {
  683. struct ext_wait_queue *sender = wq_get_first_waiter(info, SEND);
  684. if (!sender) {
  685. /* for poll */
  686. wake_up_interruptible(&info->wait_q);
  687. return;
  688. }
  689. msg_insert(sender->msg, info);
  690. list_del(&sender->list);
  691. sender->state = STATE_PENDING;
  692. wake_up_process(sender->task);
  693. smp_wmb();
  694. sender->state = STATE_READY;
  695. }
  696. asmlinkage long sys_mq_timedsend(mqd_t mqdes, const char __user *u_msg_ptr,
  697. size_t msg_len, unsigned int msg_prio,
  698. const struct timespec __user *u_abs_timeout)
  699. {
  700. struct file *filp;
  701. struct inode *inode;
  702. struct ext_wait_queue wait;
  703. struct ext_wait_queue *receiver;
  704. struct msg_msg *msg_ptr;
  705. struct mqueue_inode_info *info;
  706. long timeout;
  707. int ret;
  708. ret = audit_mq_timedsend(mqdes, msg_len, msg_prio, u_abs_timeout);
  709. if (ret != 0)
  710. return ret;
  711. if (unlikely(msg_prio >= (unsigned long) MQ_PRIO_MAX))
  712. return -EINVAL;
  713. timeout = prepare_timeout(u_abs_timeout);
  714. ret = -EBADF;
  715. filp = fget(mqdes);
  716. if (unlikely(!filp))
  717. goto out;
  718. inode = filp->f_path.dentry->d_inode;
  719. if (unlikely(filp->f_op != &mqueue_file_operations))
  720. goto out_fput;
  721. info = MQUEUE_I(inode);
  722. audit_inode(NULL, inode);
  723. if (unlikely(!(filp->f_mode & FMODE_WRITE)))
  724. goto out_fput;
  725. if (unlikely(msg_len > info->attr.mq_msgsize)) {
  726. ret = -EMSGSIZE;
  727. goto out_fput;
  728. }
  729. /* First try to allocate memory, before doing anything with
  730. * existing queues. */
  731. msg_ptr = load_msg(u_msg_ptr, msg_len);
  732. if (IS_ERR(msg_ptr)) {
  733. ret = PTR_ERR(msg_ptr);
  734. goto out_fput;
  735. }
  736. msg_ptr->m_ts = msg_len;
  737. msg_ptr->m_type = msg_prio;
  738. spin_lock(&info->lock);
  739. if (info->attr.mq_curmsgs == info->attr.mq_maxmsg) {
  740. if (filp->f_flags & O_NONBLOCK) {
  741. spin_unlock(&info->lock);
  742. ret = -EAGAIN;
  743. } else if (unlikely(timeout < 0)) {
  744. spin_unlock(&info->lock);
  745. ret = timeout;
  746. } else {
  747. wait.task = current;
  748. wait.msg = (void *) msg_ptr;
  749. wait.state = STATE_NONE;
  750. ret = wq_sleep(info, SEND, timeout, &wait);
  751. }
  752. if (ret < 0)
  753. free_msg(msg_ptr);
  754. } else {
  755. receiver = wq_get_first_waiter(info, RECV);
  756. if (receiver) {
  757. pipelined_send(info, msg_ptr, receiver);
  758. } else {
  759. /* adds message to the queue */
  760. msg_insert(msg_ptr, info);
  761. __do_notify(info);
  762. }
  763. inode->i_atime = inode->i_mtime = inode->i_ctime =
  764. CURRENT_TIME;
  765. spin_unlock(&info->lock);
  766. ret = 0;
  767. }
  768. out_fput:
  769. fput(filp);
  770. out:
  771. return ret;
  772. }
  773. asmlinkage ssize_t sys_mq_timedreceive(mqd_t mqdes, char __user *u_msg_ptr,
  774. size_t msg_len, unsigned int __user *u_msg_prio,
  775. const struct timespec __user *u_abs_timeout)
  776. {
  777. long timeout;
  778. ssize_t ret;
  779. struct msg_msg *msg_ptr;
  780. struct file *filp;
  781. struct inode *inode;
  782. struct mqueue_inode_info *info;
  783. struct ext_wait_queue wait;
  784. ret = audit_mq_timedreceive(mqdes, msg_len, u_msg_prio, u_abs_timeout);
  785. if (ret != 0)
  786. return ret;
  787. timeout = prepare_timeout(u_abs_timeout);
  788. ret = -EBADF;
  789. filp = fget(mqdes);
  790. if (unlikely(!filp))
  791. goto out;
  792. inode = filp->f_path.dentry->d_inode;
  793. if (unlikely(filp->f_op != &mqueue_file_operations))
  794. goto out_fput;
  795. info = MQUEUE_I(inode);
  796. audit_inode(NULL, inode);
  797. if (unlikely(!(filp->f_mode & FMODE_READ)))
  798. goto out_fput;
  799. /* checks if buffer is big enough */
  800. if (unlikely(msg_len < info->attr.mq_msgsize)) {
  801. ret = -EMSGSIZE;
  802. goto out_fput;
  803. }
  804. spin_lock(&info->lock);
  805. if (info->attr.mq_curmsgs == 0) {
  806. if (filp->f_flags & O_NONBLOCK) {
  807. spin_unlock(&info->lock);
  808. ret = -EAGAIN;
  809. msg_ptr = NULL;
  810. } else if (unlikely(timeout < 0)) {
  811. spin_unlock(&info->lock);
  812. ret = timeout;
  813. msg_ptr = NULL;
  814. } else {
  815. wait.task = current;
  816. wait.state = STATE_NONE;
  817. ret = wq_sleep(info, RECV, timeout, &wait);
  818. msg_ptr = wait.msg;
  819. }
  820. } else {
  821. msg_ptr = msg_get(info);
  822. inode->i_atime = inode->i_mtime = inode->i_ctime =
  823. CURRENT_TIME;
  824. /* There is now free space in queue. */
  825. pipelined_receive(info);
  826. spin_unlock(&info->lock);
  827. ret = 0;
  828. }
  829. if (ret == 0) {
  830. ret = msg_ptr->m_ts;
  831. if ((u_msg_prio && put_user(msg_ptr->m_type, u_msg_prio)) ||
  832. store_msg(u_msg_ptr, msg_ptr, msg_ptr->m_ts)) {
  833. ret = -EFAULT;
  834. }
  835. free_msg(msg_ptr);
  836. }
  837. out_fput:
  838. fput(filp);
  839. out:
  840. return ret;
  841. }
  842. /*
  843. * Notes: the case when user wants us to deregister (with NULL as pointer)
  844. * and he isn't currently owner of notification, will be silently discarded.
  845. * It isn't explicitly defined in the POSIX.
  846. */
  847. asmlinkage long sys_mq_notify(mqd_t mqdes,
  848. const struct sigevent __user *u_notification)
  849. {
  850. int ret;
  851. struct file *filp;
  852. struct sock *sock;
  853. struct inode *inode;
  854. struct sigevent notification;
  855. struct mqueue_inode_info *info;
  856. struct sk_buff *nc;
  857. ret = audit_mq_notify(mqdes, u_notification);
  858. if (ret != 0)
  859. return ret;
  860. nc = NULL;
  861. sock = NULL;
  862. if (u_notification != NULL) {
  863. if (copy_from_user(&notification, u_notification,
  864. sizeof(struct sigevent)))
  865. return -EFAULT;
  866. if (unlikely(notification.sigev_notify != SIGEV_NONE &&
  867. notification.sigev_notify != SIGEV_SIGNAL &&
  868. notification.sigev_notify != SIGEV_THREAD))
  869. return -EINVAL;
  870. if (notification.sigev_notify == SIGEV_SIGNAL &&
  871. !valid_signal(notification.sigev_signo)) {
  872. return -EINVAL;
  873. }
  874. if (notification.sigev_notify == SIGEV_THREAD) {
  875. /* create the notify skb */
  876. nc = alloc_skb(NOTIFY_COOKIE_LEN, GFP_KERNEL);
  877. ret = -ENOMEM;
  878. if (!nc)
  879. goto out;
  880. ret = -EFAULT;
  881. if (copy_from_user(nc->data,
  882. notification.sigev_value.sival_ptr,
  883. NOTIFY_COOKIE_LEN)) {
  884. goto out;
  885. }
  886. /* TODO: add a header? */
  887. skb_put(nc, NOTIFY_COOKIE_LEN);
  888. /* and attach it to the socket */
  889. retry:
  890. filp = fget(notification.sigev_signo);
  891. ret = -EBADF;
  892. if (!filp)
  893. goto out;
  894. sock = netlink_getsockbyfilp(filp);
  895. fput(filp);
  896. if (IS_ERR(sock)) {
  897. ret = PTR_ERR(sock);
  898. sock = NULL;
  899. goto out;
  900. }
  901. ret = netlink_attachskb(sock, nc, 0,
  902. MAX_SCHEDULE_TIMEOUT, NULL);
  903. if (ret == 1)
  904. goto retry;
  905. if (ret) {
  906. sock = NULL;
  907. nc = NULL;
  908. goto out;
  909. }
  910. }
  911. }
  912. ret = -EBADF;
  913. filp = fget(mqdes);
  914. if (!filp)
  915. goto out;
  916. inode = filp->f_path.dentry->d_inode;
  917. if (unlikely(filp->f_op != &mqueue_file_operations))
  918. goto out_fput;
  919. info = MQUEUE_I(inode);
  920. ret = 0;
  921. spin_lock(&info->lock);
  922. if (u_notification == NULL) {
  923. if (info->notify_owner == task_tgid(current)) {
  924. remove_notification(info);
  925. inode->i_atime = inode->i_ctime = CURRENT_TIME;
  926. }
  927. } else if (info->notify_owner != NULL) {
  928. ret = -EBUSY;
  929. } else {
  930. switch (notification.sigev_notify) {
  931. case SIGEV_NONE:
  932. info->notify.sigev_notify = SIGEV_NONE;
  933. break;
  934. case SIGEV_THREAD:
  935. info->notify_sock = sock;
  936. info->notify_cookie = nc;
  937. sock = NULL;
  938. nc = NULL;
  939. info->notify.sigev_notify = SIGEV_THREAD;
  940. break;
  941. case SIGEV_SIGNAL:
  942. info->notify.sigev_signo = notification.sigev_signo;
  943. info->notify.sigev_value = notification.sigev_value;
  944. info->notify.sigev_notify = SIGEV_SIGNAL;
  945. break;
  946. }
  947. info->notify_owner = get_pid(task_tgid(current));
  948. inode->i_atime = inode->i_ctime = CURRENT_TIME;
  949. }
  950. spin_unlock(&info->lock);
  951. out_fput:
  952. fput(filp);
  953. out:
  954. if (sock) {
  955. netlink_detachskb(sock, nc);
  956. } else if (nc) {
  957. dev_kfree_skb(nc);
  958. }
  959. return ret;
  960. }
  961. asmlinkage long sys_mq_getsetattr(mqd_t mqdes,
  962. const struct mq_attr __user *u_mqstat,
  963. struct mq_attr __user *u_omqstat)
  964. {
  965. int ret;
  966. struct mq_attr mqstat, omqstat;
  967. struct file *filp;
  968. struct inode *inode;
  969. struct mqueue_inode_info *info;
  970. if (u_mqstat != NULL) {
  971. if (copy_from_user(&mqstat, u_mqstat, sizeof(struct mq_attr)))
  972. return -EFAULT;
  973. if (mqstat.mq_flags & (~O_NONBLOCK))
  974. return -EINVAL;
  975. }
  976. ret = -EBADF;
  977. filp = fget(mqdes);
  978. if (!filp)
  979. goto out;
  980. inode = filp->f_path.dentry->d_inode;
  981. if (unlikely(filp->f_op != &mqueue_file_operations))
  982. goto out_fput;
  983. info = MQUEUE_I(inode);
  984. spin_lock(&info->lock);
  985. omqstat = info->attr;
  986. omqstat.mq_flags = filp->f_flags & O_NONBLOCK;
  987. if (u_mqstat) {
  988. ret = audit_mq_getsetattr(mqdes, &mqstat);
  989. if (ret != 0)
  990. goto out;
  991. if (mqstat.mq_flags & O_NONBLOCK)
  992. filp->f_flags |= O_NONBLOCK;
  993. else
  994. filp->f_flags &= ~O_NONBLOCK;
  995. inode->i_atime = inode->i_ctime = CURRENT_TIME;
  996. }
  997. spin_unlock(&info->lock);
  998. ret = 0;
  999. if (u_omqstat != NULL && copy_to_user(u_omqstat, &omqstat,
  1000. sizeof(struct mq_attr)))
  1001. ret = -EFAULT;
  1002. out_fput:
  1003. fput(filp);
  1004. out:
  1005. return ret;
  1006. }
  1007. static const struct inode_operations mqueue_dir_inode_operations = {
  1008. .lookup = simple_lookup,
  1009. .create = mqueue_create,
  1010. .unlink = mqueue_unlink,
  1011. };
  1012. static const struct file_operations mqueue_file_operations = {
  1013. .flush = mqueue_flush_file,
  1014. .poll = mqueue_poll_file,
  1015. .read = mqueue_read_file,
  1016. };
  1017. static struct super_operations mqueue_super_ops = {
  1018. .alloc_inode = mqueue_alloc_inode,
  1019. .destroy_inode = mqueue_destroy_inode,
  1020. .statfs = simple_statfs,
  1021. .delete_inode = mqueue_delete_inode,
  1022. .drop_inode = generic_delete_inode,
  1023. };
  1024. static struct file_system_type mqueue_fs_type = {
  1025. .name = "mqueue",
  1026. .get_sb = mqueue_get_sb,
  1027. .kill_sb = kill_litter_super,
  1028. };
  1029. static int msg_max_limit_min = DFLT_MSGMAX;
  1030. static int msg_max_limit_max = HARD_MSGMAX;
  1031. static int msg_maxsize_limit_min = DFLT_MSGSIZEMAX;
  1032. static int msg_maxsize_limit_max = INT_MAX;
  1033. static ctl_table mq_sysctls[] = {
  1034. {
  1035. .ctl_name = CTL_QUEUESMAX,
  1036. .procname = "queues_max",
  1037. .data = &queues_max,
  1038. .maxlen = sizeof(int),
  1039. .mode = 0644,
  1040. .proc_handler = &proc_dointvec,
  1041. },
  1042. {
  1043. .ctl_name = CTL_MSGMAX,
  1044. .procname = "msg_max",
  1045. .data = &msg_max,
  1046. .maxlen = sizeof(int),
  1047. .mode = 0644,
  1048. .proc_handler = &proc_dointvec_minmax,
  1049. .extra1 = &msg_max_limit_min,
  1050. .extra2 = &msg_max_limit_max,
  1051. },
  1052. {
  1053. .ctl_name = CTL_MSGSIZEMAX,
  1054. .procname = "msgsize_max",
  1055. .data = &msgsize_max,
  1056. .maxlen = sizeof(int),
  1057. .mode = 0644,
  1058. .proc_handler = &proc_dointvec_minmax,
  1059. .extra1 = &msg_maxsize_limit_min,
  1060. .extra2 = &msg_maxsize_limit_max,
  1061. },
  1062. { .ctl_name = 0 }
  1063. };
  1064. static ctl_table mq_sysctl_dir[] = {
  1065. {
  1066. .ctl_name = FS_MQUEUE,
  1067. .procname = "mqueue",
  1068. .mode = 0555,
  1069. .child = mq_sysctls,
  1070. },
  1071. { .ctl_name = 0 }
  1072. };
  1073. static ctl_table mq_sysctl_root[] = {
  1074. {
  1075. .ctl_name = CTL_FS,
  1076. .procname = "fs",
  1077. .mode = 0555,
  1078. .child = mq_sysctl_dir,
  1079. },
  1080. { .ctl_name = 0 }
  1081. };
  1082. static int __init init_mqueue_fs(void)
  1083. {
  1084. int error;
  1085. mqueue_inode_cachep = kmem_cache_create("mqueue_inode_cache",
  1086. sizeof(struct mqueue_inode_info), 0,
  1087. SLAB_HWCACHE_ALIGN, init_once, NULL);
  1088. if (mqueue_inode_cachep == NULL)
  1089. return -ENOMEM;
  1090. /* ignore failues - they are not fatal */
  1091. mq_sysctl_table = register_sysctl_table(mq_sysctl_root);
  1092. error = register_filesystem(&mqueue_fs_type);
  1093. if (error)
  1094. goto out_sysctl;
  1095. if (IS_ERR(mqueue_mnt = kern_mount(&mqueue_fs_type))) {
  1096. error = PTR_ERR(mqueue_mnt);
  1097. goto out_filesystem;
  1098. }
  1099. /* internal initialization - not common for vfs */
  1100. queues_count = 0;
  1101. spin_lock_init(&mq_lock);
  1102. return 0;
  1103. out_filesystem:
  1104. unregister_filesystem(&mqueue_fs_type);
  1105. out_sysctl:
  1106. if (mq_sysctl_table)
  1107. unregister_sysctl_table(mq_sysctl_table);
  1108. kmem_cache_destroy(mqueue_inode_cachep);
  1109. return error;
  1110. }
  1111. __initcall(init_mqueue_fs);