mqueue.c 30 KB

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