fcntl.c 19 KB

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