mqueue.c 30 KB

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