mqueue.c 30 KB

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