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