fcntl.c 19 KB

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