mqueue.c 30 KB

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