fcntl.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. /*
  2. * linux/fs/fcntl.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/syscalls.h>
  7. #include <linux/init.h>
  8. #include <linux/mm.h>
  9. #include <linux/fs.h>
  10. #include <linux/file.h>
  11. #include <linux/fdtable.h>
  12. #include <linux/capability.h>
  13. #include <linux/dnotify.h>
  14. #include <linux/slab.h>
  15. #include <linux/module.h>
  16. #include <linux/pipe_fs_i.h>
  17. #include <linux/security.h>
  18. #include <linux/ptrace.h>
  19. #include <linux/signal.h>
  20. #include <linux/rcupdate.h>
  21. #include <linux/pid_namespace.h>
  22. #include <asm/poll.h>
  23. #include <asm/siginfo.h>
  24. #include <asm/uaccess.h>
  25. void set_close_on_exec(unsigned int fd, int flag)
  26. {
  27. struct files_struct *files = current->files;
  28. struct fdtable *fdt;
  29. spin_lock(&files->file_lock);
  30. fdt = files_fdtable(files);
  31. if (flag)
  32. FD_SET(fd, fdt->close_on_exec);
  33. else
  34. FD_CLR(fd, fdt->close_on_exec);
  35. spin_unlock(&files->file_lock);
  36. }
  37. static int get_close_on_exec(unsigned int fd)
  38. {
  39. struct files_struct *files = current->files;
  40. struct fdtable *fdt;
  41. int res;
  42. rcu_read_lock();
  43. fdt = files_fdtable(files);
  44. res = FD_ISSET(fd, fdt->close_on_exec);
  45. rcu_read_unlock();
  46. return res;
  47. }
  48. SYSCALL_DEFINE3(dup3, unsigned int, oldfd, unsigned int, newfd, int, flags)
  49. {
  50. int err = -EBADF;
  51. struct file * file, *tofree;
  52. struct files_struct * files = current->files;
  53. struct fdtable *fdt;
  54. if ((flags & ~O_CLOEXEC) != 0)
  55. return -EINVAL;
  56. if (unlikely(oldfd == newfd))
  57. return -EINVAL;
  58. spin_lock(&files->file_lock);
  59. err = expand_files(files, newfd);
  60. file = fcheck(oldfd);
  61. if (unlikely(!file))
  62. goto Ebadf;
  63. if (unlikely(err < 0)) {
  64. if (err == -EMFILE)
  65. goto Ebadf;
  66. goto out_unlock;
  67. }
  68. /*
  69. * We need to detect attempts to do dup2() over allocated but still
  70. * not finished descriptor. NB: OpenBSD avoids that at the price of
  71. * extra work in their equivalent of fget() - they insert struct
  72. * file immediately after grabbing descriptor, mark it larval if
  73. * more work (e.g. actual opening) is needed and make sure that
  74. * fget() treats larval files as absent. Potentially interesting,
  75. * but while extra work in fget() is trivial, locking implications
  76. * and amount of surgery on open()-related paths in VFS are not.
  77. * FreeBSD fails with -EBADF in the same situation, NetBSD "solution"
  78. * deadlocks in rather amusing ways, AFAICS. All of that is out of
  79. * scope of POSIX or SUS, since neither considers shared descriptor
  80. * tables and this condition does not arise without those.
  81. */
  82. err = -EBUSY;
  83. fdt = files_fdtable(files);
  84. tofree = fdt->fd[newfd];
  85. if (!tofree && FD_ISSET(newfd, fdt->open_fds))
  86. goto out_unlock;
  87. get_file(file);
  88. rcu_assign_pointer(fdt->fd[newfd], file);
  89. FD_SET(newfd, fdt->open_fds);
  90. if (flags & O_CLOEXEC)
  91. FD_SET(newfd, fdt->close_on_exec);
  92. else
  93. FD_CLR(newfd, fdt->close_on_exec);
  94. spin_unlock(&files->file_lock);
  95. if (tofree)
  96. filp_close(tofree, files);
  97. return newfd;
  98. Ebadf:
  99. err = -EBADF;
  100. out_unlock:
  101. spin_unlock(&files->file_lock);
  102. return err;
  103. }
  104. SYSCALL_DEFINE2(dup2, unsigned int, oldfd, unsigned int, newfd)
  105. {
  106. if (unlikely(newfd == oldfd)) { /* corner case */
  107. struct files_struct *files = current->files;
  108. int retval = oldfd;
  109. rcu_read_lock();
  110. if (!fcheck_files(files, oldfd))
  111. retval = -EBADF;
  112. rcu_read_unlock();
  113. return retval;
  114. }
  115. return sys_dup3(oldfd, newfd, 0);
  116. }
  117. SYSCALL_DEFINE1(dup, unsigned int, fildes)
  118. {
  119. int ret = -EBADF;
  120. struct file *file = fget_raw(fildes);
  121. if (file) {
  122. ret = get_unused_fd();
  123. if (ret >= 0)
  124. fd_install(ret, file);
  125. else
  126. fput(file);
  127. }
  128. return ret;
  129. }
  130. #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
  131. static int setfl(int fd, struct file * filp, unsigned long arg)
  132. {
  133. struct inode * inode = filp->f_path.dentry->d_inode;
  134. int error = 0;
  135. /*
  136. * O_APPEND cannot be cleared if the file is marked as append-only
  137. * and the file is open for write.
  138. */
  139. if (((arg ^ filp->f_flags) & O_APPEND) && IS_APPEND(inode))
  140. return -EPERM;
  141. /* O_NOATIME can only be set by the owner or superuser */
  142. if ((arg & O_NOATIME) && !(filp->f_flags & O_NOATIME))
  143. if (!inode_owner_or_capable(inode))
  144. return -EPERM;
  145. /* required for strict SunOS emulation */
  146. if (O_NONBLOCK != O_NDELAY)
  147. if (arg & O_NDELAY)
  148. arg |= O_NONBLOCK;
  149. if (arg & O_DIRECT) {
  150. if (!filp->f_mapping || !filp->f_mapping->a_ops ||
  151. !filp->f_mapping->a_ops->direct_IO)
  152. return -EINVAL;
  153. }
  154. if (filp->f_op && filp->f_op->check_flags)
  155. error = filp->f_op->check_flags(arg);
  156. if (error)
  157. return error;
  158. /*
  159. * ->fasync() is responsible for setting the FASYNC bit.
  160. */
  161. if (((arg ^ filp->f_flags) & FASYNC) && filp->f_op &&
  162. filp->f_op->fasync) {
  163. error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0);
  164. if (error < 0)
  165. goto out;
  166. if (error > 0)
  167. error = 0;
  168. }
  169. spin_lock(&filp->f_lock);
  170. filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
  171. spin_unlock(&filp->f_lock);
  172. out:
  173. return error;
  174. }
  175. static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
  176. int force)
  177. {
  178. write_lock_irq(&filp->f_owner.lock);
  179. if (force || !filp->f_owner.pid) {
  180. put_pid(filp->f_owner.pid);
  181. filp->f_owner.pid = get_pid(pid);
  182. filp->f_owner.pid_type = type;
  183. if (pid) {
  184. const struct cred *cred = current_cred();
  185. filp->f_owner.uid = cred->uid;
  186. filp->f_owner.euid = cred->euid;
  187. }
  188. }
  189. write_unlock_irq(&filp->f_owner.lock);
  190. }
  191. int __f_setown(struct file *filp, struct pid *pid, enum pid_type type,
  192. int force)
  193. {
  194. int err;
  195. err = security_file_set_fowner(filp);
  196. if (err)
  197. return err;
  198. f_modown(filp, pid, type, force);
  199. return 0;
  200. }
  201. EXPORT_SYMBOL(__f_setown);
  202. int f_setown(struct file *filp, unsigned long arg, int force)
  203. {
  204. enum pid_type type;
  205. struct pid *pid;
  206. int who = arg;
  207. int result;
  208. type = PIDTYPE_PID;
  209. if (who < 0) {
  210. type = PIDTYPE_PGID;
  211. who = -who;
  212. }
  213. rcu_read_lock();
  214. pid = find_vpid(who);
  215. result = __f_setown(filp, pid, type, force);
  216. rcu_read_unlock();
  217. return result;
  218. }
  219. EXPORT_SYMBOL(f_setown);
  220. void f_delown(struct file *filp)
  221. {
  222. f_modown(filp, NULL, PIDTYPE_PID, 1);
  223. }
  224. pid_t f_getown(struct file *filp)
  225. {
  226. pid_t pid;
  227. read_lock(&filp->f_owner.lock);
  228. pid = pid_vnr(filp->f_owner.pid);
  229. if (filp->f_owner.pid_type == PIDTYPE_PGID)
  230. pid = -pid;
  231. read_unlock(&filp->f_owner.lock);
  232. return pid;
  233. }
  234. static int f_setown_ex(struct file *filp, unsigned long arg)
  235. {
  236. struct f_owner_ex * __user owner_p = (void * __user)arg;
  237. struct f_owner_ex owner;
  238. struct pid *pid;
  239. int type;
  240. int ret;
  241. ret = copy_from_user(&owner, owner_p, sizeof(owner));
  242. if (ret)
  243. return -EFAULT;
  244. switch (owner.type) {
  245. case F_OWNER_TID:
  246. type = PIDTYPE_MAX;
  247. break;
  248. case F_OWNER_PID:
  249. type = PIDTYPE_PID;
  250. break;
  251. case F_OWNER_PGRP:
  252. type = PIDTYPE_PGID;
  253. break;
  254. default:
  255. return -EINVAL;
  256. }
  257. rcu_read_lock();
  258. pid = find_vpid(owner.pid);
  259. if (owner.pid && !pid)
  260. ret = -ESRCH;
  261. else
  262. ret = __f_setown(filp, pid, type, 1);
  263. rcu_read_unlock();
  264. return ret;
  265. }
  266. static int f_getown_ex(struct file *filp, unsigned long arg)
  267. {
  268. struct f_owner_ex * __user owner_p = (void * __user)arg;
  269. struct f_owner_ex owner;
  270. int ret = 0;
  271. read_lock(&filp->f_owner.lock);
  272. owner.pid = pid_vnr(filp->f_owner.pid);
  273. switch (filp->f_owner.pid_type) {
  274. case PIDTYPE_MAX:
  275. owner.type = F_OWNER_TID;
  276. break;
  277. case PIDTYPE_PID:
  278. owner.type = F_OWNER_PID;
  279. break;
  280. case PIDTYPE_PGID:
  281. owner.type = F_OWNER_PGRP;
  282. break;
  283. default:
  284. WARN_ON(1);
  285. ret = -EINVAL;
  286. break;
  287. }
  288. read_unlock(&filp->f_owner.lock);
  289. if (!ret) {
  290. ret = copy_to_user(owner_p, &owner, sizeof(owner));
  291. if (ret)
  292. ret = -EFAULT;
  293. }
  294. return ret;
  295. }
  296. static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
  297. struct file *filp)
  298. {
  299. long err = -EINVAL;
  300. switch (cmd) {
  301. case F_DUPFD:
  302. case F_DUPFD_CLOEXEC:
  303. if (arg >= rlimit(RLIMIT_NOFILE))
  304. break;
  305. err = alloc_fd(arg, cmd == F_DUPFD_CLOEXEC ? O_CLOEXEC : 0);
  306. if (err >= 0) {
  307. get_file(filp);
  308. fd_install(err, filp);
  309. }
  310. break;
  311. case F_GETFD:
  312. err = get_close_on_exec(fd) ? FD_CLOEXEC : 0;
  313. break;
  314. case F_SETFD:
  315. err = 0;
  316. set_close_on_exec(fd, arg & FD_CLOEXEC);
  317. break;
  318. case F_GETFL:
  319. err = filp->f_flags;
  320. break;
  321. case F_SETFL:
  322. err = setfl(fd, filp, arg);
  323. break;
  324. case F_GETLK:
  325. err = fcntl_getlk(filp, (struct flock __user *) arg);
  326. break;
  327. case F_SETLK:
  328. case F_SETLKW:
  329. err = fcntl_setlk(fd, filp, cmd, (struct flock __user *) arg);
  330. break;
  331. case F_GETOWN:
  332. /*
  333. * XXX If f_owner is a process group, the
  334. * negative return value will get converted
  335. * into an error. Oops. If we keep the
  336. * current syscall conventions, the only way
  337. * to fix this will be in libc.
  338. */
  339. err = f_getown(filp);
  340. force_successful_syscall_return();
  341. break;
  342. case F_SETOWN:
  343. err = f_setown(filp, arg, 1);
  344. break;
  345. case F_GETOWN_EX:
  346. err = f_getown_ex(filp, arg);
  347. break;
  348. case F_SETOWN_EX:
  349. err = f_setown_ex(filp, arg);
  350. break;
  351. case F_GETSIG:
  352. err = filp->f_owner.signum;
  353. break;
  354. case F_SETSIG:
  355. /* arg == 0 restores default behaviour. */
  356. if (!valid_signal(arg)) {
  357. break;
  358. }
  359. err = 0;
  360. filp->f_owner.signum = arg;
  361. break;
  362. case F_GETLEASE:
  363. err = fcntl_getlease(filp);
  364. break;
  365. case F_SETLEASE:
  366. err = fcntl_setlease(fd, filp, arg);
  367. break;
  368. case F_NOTIFY:
  369. err = fcntl_dirnotify(fd, filp, arg);
  370. break;
  371. case F_SETPIPE_SZ:
  372. case F_GETPIPE_SZ:
  373. err = pipe_fcntl(filp, cmd, arg);
  374. break;
  375. default:
  376. break;
  377. }
  378. return err;
  379. }
  380. static int check_fcntl_cmd(unsigned cmd)
  381. {
  382. switch (cmd) {
  383. case F_DUPFD:
  384. case F_DUPFD_CLOEXEC:
  385. case F_GETFD:
  386. case F_SETFD:
  387. case F_GETFL:
  388. return 1;
  389. }
  390. return 0;
  391. }
  392. SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
  393. {
  394. struct file *filp;
  395. long err = -EBADF;
  396. filp = fget_raw(fd);
  397. if (!filp)
  398. goto out;
  399. if (unlikely(filp->f_mode & FMODE_PATH)) {
  400. if (!check_fcntl_cmd(cmd)) {
  401. fput(filp);
  402. goto out;
  403. }
  404. }
  405. err = security_file_fcntl(filp, cmd, arg);
  406. if (err) {
  407. fput(filp);
  408. return err;
  409. }
  410. err = do_fcntl(fd, cmd, arg, filp);
  411. fput(filp);
  412. out:
  413. return err;
  414. }
  415. #if BITS_PER_LONG == 32
  416. SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
  417. unsigned long, arg)
  418. {
  419. struct file * filp;
  420. long err;
  421. err = -EBADF;
  422. filp = fget_raw(fd);
  423. if (!filp)
  424. goto out;
  425. if (unlikely(filp->f_mode & FMODE_PATH)) {
  426. if (!check_fcntl_cmd(cmd)) {
  427. fput(filp);
  428. goto out;
  429. }
  430. }
  431. err = security_file_fcntl(filp, cmd, arg);
  432. if (err) {
  433. fput(filp);
  434. return err;
  435. }
  436. err = -EBADF;
  437. switch (cmd) {
  438. case F_GETLK64:
  439. err = fcntl_getlk64(filp, (struct flock64 __user *) arg);
  440. break;
  441. case F_SETLK64:
  442. case F_SETLKW64:
  443. err = fcntl_setlk64(fd, filp, cmd,
  444. (struct flock64 __user *) arg);
  445. break;
  446. default:
  447. err = do_fcntl(fd, cmd, arg, filp);
  448. break;
  449. }
  450. fput(filp);
  451. out:
  452. return err;
  453. }
  454. #endif
  455. /* Table to convert sigio signal codes into poll band bitmaps */
  456. static const long band_table[NSIGPOLL] = {
  457. POLLIN | POLLRDNORM, /* POLL_IN */
  458. POLLOUT | POLLWRNORM | POLLWRBAND, /* POLL_OUT */
  459. POLLIN | POLLRDNORM | POLLMSG, /* POLL_MSG */
  460. POLLERR, /* POLL_ERR */
  461. POLLPRI | POLLRDBAND, /* POLL_PRI */
  462. POLLHUP | POLLERR /* POLL_HUP */
  463. };
  464. static inline int sigio_perm(struct task_struct *p,
  465. struct fown_struct *fown, int sig)
  466. {
  467. const struct cred *cred;
  468. int ret;
  469. rcu_read_lock();
  470. cred = __task_cred(p);
  471. ret = ((fown->euid == 0 ||
  472. fown->euid == cred->suid || fown->euid == cred->uid ||
  473. fown->uid == cred->suid || fown->uid == cred->uid) &&
  474. !security_file_send_sigiotask(p, fown, sig));
  475. rcu_read_unlock();
  476. return ret;
  477. }
  478. static void send_sigio_to_task(struct task_struct *p,
  479. struct fown_struct *fown,
  480. int fd, int reason, int group)
  481. {
  482. /*
  483. * F_SETSIG can change ->signum lockless in parallel, make
  484. * sure we read it once and use the same value throughout.
  485. */
  486. int signum = ACCESS_ONCE(fown->signum);
  487. if (!sigio_perm(p, fown, signum))
  488. return;
  489. switch (signum) {
  490. siginfo_t si;
  491. default:
  492. /* Queue a rt signal with the appropriate fd as its
  493. value. We use SI_SIGIO as the source, not
  494. SI_KERNEL, since kernel signals always get
  495. delivered even if we can't queue. Failure to
  496. queue in this case _should_ be reported; we fall
  497. back to SIGIO in that case. --sct */
  498. si.si_signo = signum;
  499. si.si_errno = 0;
  500. si.si_code = reason;
  501. /* Make sure we are called with one of the POLL_*
  502. reasons, otherwise we could leak kernel stack into
  503. userspace. */
  504. BUG_ON((reason & __SI_MASK) != __SI_POLL);
  505. if (reason - POLL_IN >= NSIGPOLL)
  506. si.si_band = ~0L;
  507. else
  508. si.si_band = band_table[reason - POLL_IN];
  509. si.si_fd = fd;
  510. if (!do_send_sig_info(signum, &si, p, group))
  511. break;
  512. /* fall-through: fall back on the old plain SIGIO signal */
  513. case 0:
  514. do_send_sig_info(SIGIO, SEND_SIG_PRIV, p, group);
  515. }
  516. }
  517. void send_sigio(struct fown_struct *fown, int fd, int band)
  518. {
  519. struct task_struct *p;
  520. enum pid_type type;
  521. struct pid *pid;
  522. int group = 1;
  523. read_lock(&fown->lock);
  524. type = fown->pid_type;
  525. if (type == PIDTYPE_MAX) {
  526. group = 0;
  527. type = PIDTYPE_PID;
  528. }
  529. pid = fown->pid;
  530. if (!pid)
  531. goto out_unlock_fown;
  532. read_lock(&tasklist_lock);
  533. do_each_pid_task(pid, type, p) {
  534. send_sigio_to_task(p, fown, fd, band, group);
  535. } while_each_pid_task(pid, type, p);
  536. read_unlock(&tasklist_lock);
  537. out_unlock_fown:
  538. read_unlock(&fown->lock);
  539. }
  540. static void send_sigurg_to_task(struct task_struct *p,
  541. struct fown_struct *fown, int group)
  542. {
  543. if (sigio_perm(p, fown, SIGURG))
  544. do_send_sig_info(SIGURG, SEND_SIG_PRIV, p, group);
  545. }
  546. int send_sigurg(struct fown_struct *fown)
  547. {
  548. struct task_struct *p;
  549. enum pid_type type;
  550. struct pid *pid;
  551. int group = 1;
  552. int ret = 0;
  553. read_lock(&fown->lock);
  554. type = fown->pid_type;
  555. if (type == PIDTYPE_MAX) {
  556. group = 0;
  557. type = PIDTYPE_PID;
  558. }
  559. pid = fown->pid;
  560. if (!pid)
  561. goto out_unlock_fown;
  562. ret = 1;
  563. read_lock(&tasklist_lock);
  564. do_each_pid_task(pid, type, p) {
  565. send_sigurg_to_task(p, fown, group);
  566. } while_each_pid_task(pid, type, p);
  567. read_unlock(&tasklist_lock);
  568. out_unlock_fown:
  569. read_unlock(&fown->lock);
  570. return ret;
  571. }
  572. static DEFINE_SPINLOCK(fasync_lock);
  573. static struct kmem_cache *fasync_cache __read_mostly;
  574. static void fasync_free_rcu(struct rcu_head *head)
  575. {
  576. kmem_cache_free(fasync_cache,
  577. container_of(head, struct fasync_struct, fa_rcu));
  578. }
  579. /*
  580. * Remove a fasync entry. If successfully removed, return
  581. * positive and clear the FASYNC flag. If no entry exists,
  582. * do nothing and return 0.
  583. *
  584. * NOTE! It is very important that the FASYNC flag always
  585. * match the state "is the filp on a fasync list".
  586. *
  587. */
  588. int fasync_remove_entry(struct file *filp, struct fasync_struct **fapp)
  589. {
  590. struct fasync_struct *fa, **fp;
  591. int result = 0;
  592. spin_lock(&filp->f_lock);
  593. spin_lock(&fasync_lock);
  594. for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
  595. if (fa->fa_file != filp)
  596. continue;
  597. spin_lock_irq(&fa->fa_lock);
  598. fa->fa_file = NULL;
  599. spin_unlock_irq(&fa->fa_lock);
  600. *fp = fa->fa_next;
  601. call_rcu(&fa->fa_rcu, fasync_free_rcu);
  602. filp->f_flags &= ~FASYNC;
  603. result = 1;
  604. break;
  605. }
  606. spin_unlock(&fasync_lock);
  607. spin_unlock(&filp->f_lock);
  608. return result;
  609. }
  610. struct fasync_struct *fasync_alloc(void)
  611. {
  612. return kmem_cache_alloc(fasync_cache, GFP_KERNEL);
  613. }
  614. /*
  615. * NOTE! This can be used only for unused fasync entries:
  616. * entries that actually got inserted on the fasync list
  617. * need to be released by rcu - see fasync_remove_entry.
  618. */
  619. void fasync_free(struct fasync_struct *new)
  620. {
  621. kmem_cache_free(fasync_cache, new);
  622. }
  623. /*
  624. * Insert a new entry into the fasync list. Return the pointer to the
  625. * old one if we didn't use the new one.
  626. *
  627. * NOTE! It is very important that the FASYNC flag always
  628. * match the state "is the filp on a fasync list".
  629. */
  630. struct fasync_struct *fasync_insert_entry(int fd, struct file *filp, struct fasync_struct **fapp, struct fasync_struct *new)
  631. {
  632. struct fasync_struct *fa, **fp;
  633. spin_lock(&filp->f_lock);
  634. spin_lock(&fasync_lock);
  635. for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
  636. if (fa->fa_file != filp)
  637. continue;
  638. spin_lock_irq(&fa->fa_lock);
  639. fa->fa_fd = fd;
  640. spin_unlock_irq(&fa->fa_lock);
  641. goto out;
  642. }
  643. spin_lock_init(&new->fa_lock);
  644. new->magic = FASYNC_MAGIC;
  645. new->fa_file = filp;
  646. new->fa_fd = fd;
  647. new->fa_next = *fapp;
  648. rcu_assign_pointer(*fapp, new);
  649. filp->f_flags |= FASYNC;
  650. out:
  651. spin_unlock(&fasync_lock);
  652. spin_unlock(&filp->f_lock);
  653. return fa;
  654. }
  655. /*
  656. * Add a fasync entry. Return negative on error, positive if
  657. * added, and zero if did nothing but change an existing one.
  658. */
  659. static int fasync_add_entry(int fd, struct file *filp, struct fasync_struct **fapp)
  660. {
  661. struct fasync_struct *new;
  662. new = fasync_alloc();
  663. if (!new)
  664. return -ENOMEM;
  665. /*
  666. * fasync_insert_entry() returns the old (update) entry if
  667. * it existed.
  668. *
  669. * So free the (unused) new entry and return 0 to let the
  670. * caller know that we didn't add any new fasync entries.
  671. */
  672. if (fasync_insert_entry(fd, filp, fapp, new)) {
  673. fasync_free(new);
  674. return 0;
  675. }
  676. return 1;
  677. }
  678. /*
  679. * fasync_helper() is used by almost all character device drivers
  680. * to set up the fasync queue, and for regular files by the file
  681. * lease code. It returns negative on error, 0 if it did no changes
  682. * and positive if it added/deleted the entry.
  683. */
  684. int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
  685. {
  686. if (!on)
  687. return fasync_remove_entry(filp, fapp);
  688. return fasync_add_entry(fd, filp, fapp);
  689. }
  690. EXPORT_SYMBOL(fasync_helper);
  691. /*
  692. * rcu_read_lock() is held
  693. */
  694. static void kill_fasync_rcu(struct fasync_struct *fa, int sig, int band)
  695. {
  696. while (fa) {
  697. struct fown_struct *fown;
  698. unsigned long flags;
  699. if (fa->magic != FASYNC_MAGIC) {
  700. printk(KERN_ERR "kill_fasync: bad magic number in "
  701. "fasync_struct!\n");
  702. return;
  703. }
  704. spin_lock_irqsave(&fa->fa_lock, flags);
  705. if (fa->fa_file) {
  706. fown = &fa->fa_file->f_owner;
  707. /* Don't send SIGURG to processes which have not set a
  708. queued signum: SIGURG has its own default signalling
  709. mechanism. */
  710. if (!(sig == SIGURG && fown->signum == 0))
  711. send_sigio(fown, fa->fa_fd, band);
  712. }
  713. spin_unlock_irqrestore(&fa->fa_lock, flags);
  714. fa = rcu_dereference(fa->fa_next);
  715. }
  716. }
  717. void kill_fasync(struct fasync_struct **fp, int sig, int band)
  718. {
  719. /* First a quick test without locking: usually
  720. * the list is empty.
  721. */
  722. if (*fp) {
  723. rcu_read_lock();
  724. kill_fasync_rcu(rcu_dereference(*fp), sig, band);
  725. rcu_read_unlock();
  726. }
  727. }
  728. EXPORT_SYMBOL(kill_fasync);
  729. static int __init fcntl_init(void)
  730. {
  731. /*
  732. * Please add new bits here to ensure allocation uniqueness.
  733. * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY
  734. * is defined as O_NONBLOCK on some platforms and not on others.
  735. */
  736. BUILD_BUG_ON(19 - 1 /* for O_RDONLY being 0 */ != HWEIGHT32(
  737. O_RDONLY | O_WRONLY | O_RDWR |
  738. O_CREAT | O_EXCL | O_NOCTTY |
  739. O_TRUNC | O_APPEND | /* O_NONBLOCK | */
  740. __O_SYNC | O_DSYNC | FASYNC |
  741. O_DIRECT | O_LARGEFILE | O_DIRECTORY |
  742. O_NOFOLLOW | O_NOATIME | O_CLOEXEC |
  743. __FMODE_EXEC | O_PATH
  744. ));
  745. fasync_cache = kmem_cache_create("fasync_cache",
  746. sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL);
  747. return 0;
  748. }
  749. module_init(fcntl_init)