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