fcntl.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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/security.h>
  17. #include <linux/ptrace.h>
  18. #include <linux/signal.h>
  19. #include <linux/rcupdate.h>
  20. #include <linux/pid_namespace.h>
  21. #include <asm/poll.h>
  22. #include <asm/siginfo.h>
  23. #include <asm/uaccess.h>
  24. void set_close_on_exec(unsigned int fd, int flag)
  25. {
  26. struct files_struct *files = current->files;
  27. struct fdtable *fdt;
  28. spin_lock(&files->file_lock);
  29. fdt = files_fdtable(files);
  30. if (flag)
  31. FD_SET(fd, fdt->close_on_exec);
  32. else
  33. FD_CLR(fd, fdt->close_on_exec);
  34. spin_unlock(&files->file_lock);
  35. }
  36. static int get_close_on_exec(unsigned int fd)
  37. {
  38. struct files_struct *files = current->files;
  39. struct fdtable *fdt;
  40. int res;
  41. rcu_read_lock();
  42. fdt = files_fdtable(files);
  43. res = FD_ISSET(fd, fdt->close_on_exec);
  44. rcu_read_unlock();
  45. return res;
  46. }
  47. SYSCALL_DEFINE3(dup3, unsigned int, oldfd, unsigned int, newfd, int, flags)
  48. {
  49. int err = -EBADF;
  50. struct file * file, *tofree;
  51. struct files_struct * files = current->files;
  52. struct fdtable *fdt;
  53. if ((flags & ~O_CLOEXEC) != 0)
  54. return -EINVAL;
  55. if (unlikely(oldfd == newfd))
  56. return -EINVAL;
  57. spin_lock(&files->file_lock);
  58. err = expand_files(files, newfd);
  59. file = fcheck(oldfd);
  60. if (unlikely(!file))
  61. goto Ebadf;
  62. if (unlikely(err < 0)) {
  63. if (err == -EMFILE)
  64. goto Ebadf;
  65. goto out_unlock;
  66. }
  67. /*
  68. * We need to detect attempts to do dup2() over allocated but still
  69. * not finished descriptor. NB: OpenBSD avoids that at the price of
  70. * extra work in their equivalent of fget() - they insert struct
  71. * file immediately after grabbing descriptor, mark it larval if
  72. * more work (e.g. actual opening) is needed and make sure that
  73. * fget() treats larval files as absent. Potentially interesting,
  74. * but while extra work in fget() is trivial, locking implications
  75. * and amount of surgery on open()-related paths in VFS are not.
  76. * FreeBSD fails with -EBADF in the same situation, NetBSD "solution"
  77. * deadlocks in rather amusing ways, AFAICS. All of that is out of
  78. * scope of POSIX or SUS, since neither considers shared descriptor
  79. * tables and this condition does not arise without those.
  80. */
  81. err = -EBUSY;
  82. fdt = files_fdtable(files);
  83. tofree = fdt->fd[newfd];
  84. if (!tofree && FD_ISSET(newfd, fdt->open_fds))
  85. goto out_unlock;
  86. get_file(file);
  87. rcu_assign_pointer(fdt->fd[newfd], file);
  88. FD_SET(newfd, fdt->open_fds);
  89. if (flags & O_CLOEXEC)
  90. FD_SET(newfd, fdt->close_on_exec);
  91. else
  92. FD_CLR(newfd, fdt->close_on_exec);
  93. spin_unlock(&files->file_lock);
  94. if (tofree)
  95. filp_close(tofree, files);
  96. return newfd;
  97. Ebadf:
  98. err = -EBADF;
  99. out_unlock:
  100. spin_unlock(&files->file_lock);
  101. return err;
  102. }
  103. SYSCALL_DEFINE2(dup2, unsigned int, oldfd, unsigned int, newfd)
  104. {
  105. if (unlikely(newfd == oldfd)) { /* corner case */
  106. struct files_struct *files = current->files;
  107. int retval = oldfd;
  108. rcu_read_lock();
  109. if (!fcheck_files(files, oldfd))
  110. retval = -EBADF;
  111. rcu_read_unlock();
  112. return retval;
  113. }
  114. return sys_dup3(oldfd, newfd, 0);
  115. }
  116. SYSCALL_DEFINE1(dup, unsigned int, fildes)
  117. {
  118. int ret = -EBADF;
  119. struct file *file = fget(fildes);
  120. if (file) {
  121. ret = get_unused_fd();
  122. if (ret >= 0)
  123. fd_install(ret, file);
  124. else
  125. fput(file);
  126. }
  127. return ret;
  128. }
  129. #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
  130. static int setfl(int fd, struct file * filp, unsigned long arg)
  131. {
  132. struct inode * inode = filp->f_path.dentry->d_inode;
  133. int error = 0;
  134. /*
  135. * O_APPEND cannot be cleared if the file is marked as append-only
  136. * and the file is open for write.
  137. */
  138. if (((arg ^ filp->f_flags) & O_APPEND) && IS_APPEND(inode))
  139. return -EPERM;
  140. /* O_NOATIME can only be set by the owner or superuser */
  141. if ((arg & O_NOATIME) && !(filp->f_flags & O_NOATIME))
  142. if (!is_owner_or_cap(inode))
  143. return -EPERM;
  144. /* required for strict SunOS emulation */
  145. if (O_NONBLOCK != O_NDELAY)
  146. if (arg & O_NDELAY)
  147. arg |= O_NONBLOCK;
  148. if (arg & O_DIRECT) {
  149. if (!filp->f_mapping || !filp->f_mapping->a_ops ||
  150. !filp->f_mapping->a_ops->direct_IO)
  151. return -EINVAL;
  152. }
  153. if (filp->f_op && filp->f_op->check_flags)
  154. error = filp->f_op->check_flags(arg);
  155. if (error)
  156. return error;
  157. /*
  158. * ->fasync() is responsible for setting the FASYNC bit.
  159. */
  160. if (((arg ^ filp->f_flags) & FASYNC) && filp->f_op &&
  161. filp->f_op->fasync) {
  162. error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0);
  163. if (error < 0)
  164. goto out;
  165. if (error > 0)
  166. error = 0;
  167. }
  168. spin_lock(&filp->f_lock);
  169. filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
  170. spin_unlock(&filp->f_lock);
  171. out:
  172. return error;
  173. }
  174. static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
  175. int force)
  176. {
  177. unsigned long flags;
  178. write_lock_irqsave(&filp->f_owner.lock, flags);
  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_irqrestore(&filp->f_owner.lock, flags);
  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 ret;
  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. return ret;
  292. }
  293. static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
  294. struct file *filp)
  295. {
  296. long err = -EINVAL;
  297. switch (cmd) {
  298. case F_DUPFD:
  299. case F_DUPFD_CLOEXEC:
  300. if (arg >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
  301. break;
  302. err = alloc_fd(arg, cmd == F_DUPFD_CLOEXEC ? O_CLOEXEC : 0);
  303. if (err >= 0) {
  304. get_file(filp);
  305. fd_install(err, filp);
  306. }
  307. break;
  308. case F_GETFD:
  309. err = get_close_on_exec(fd) ? FD_CLOEXEC : 0;
  310. break;
  311. case F_SETFD:
  312. err = 0;
  313. set_close_on_exec(fd, arg & FD_CLOEXEC);
  314. break;
  315. case F_GETFL:
  316. err = filp->f_flags;
  317. break;
  318. case F_SETFL:
  319. err = setfl(fd, filp, arg);
  320. break;
  321. case F_GETLK:
  322. err = fcntl_getlk(filp, (struct flock __user *) arg);
  323. break;
  324. case F_SETLK:
  325. case F_SETLKW:
  326. err = fcntl_setlk(fd, filp, cmd, (struct flock __user *) arg);
  327. break;
  328. case F_GETOWN:
  329. /*
  330. * XXX If f_owner is a process group, the
  331. * negative return value will get converted
  332. * into an error. Oops. If we keep the
  333. * current syscall conventions, the only way
  334. * to fix this will be in libc.
  335. */
  336. err = f_getown(filp);
  337. force_successful_syscall_return();
  338. break;
  339. case F_SETOWN:
  340. err = f_setown(filp, arg, 1);
  341. break;
  342. case F_GETOWN_EX:
  343. err = f_getown_ex(filp, arg);
  344. break;
  345. case F_SETOWN_EX:
  346. err = f_setown_ex(filp, arg);
  347. break;
  348. case F_GETSIG:
  349. err = filp->f_owner.signum;
  350. break;
  351. case F_SETSIG:
  352. /* arg == 0 restores default behaviour. */
  353. if (!valid_signal(arg)) {
  354. break;
  355. }
  356. err = 0;
  357. filp->f_owner.signum = arg;
  358. break;
  359. case F_GETLEASE:
  360. err = fcntl_getlease(filp);
  361. break;
  362. case F_SETLEASE:
  363. err = fcntl_setlease(fd, filp, arg);
  364. break;
  365. case F_NOTIFY:
  366. err = fcntl_dirnotify(fd, filp, arg);
  367. break;
  368. default:
  369. break;
  370. }
  371. return err;
  372. }
  373. SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
  374. {
  375. struct file *filp;
  376. long err = -EBADF;
  377. filp = fget(fd);
  378. if (!filp)
  379. goto out;
  380. err = security_file_fcntl(filp, cmd, arg);
  381. if (err) {
  382. fput(filp);
  383. return err;
  384. }
  385. err = do_fcntl(fd, cmd, arg, filp);
  386. fput(filp);
  387. out:
  388. return err;
  389. }
  390. #if BITS_PER_LONG == 32
  391. SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
  392. unsigned long, arg)
  393. {
  394. struct file * filp;
  395. long err;
  396. err = -EBADF;
  397. filp = fget(fd);
  398. if (!filp)
  399. goto out;
  400. err = security_file_fcntl(filp, cmd, arg);
  401. if (err) {
  402. fput(filp);
  403. return err;
  404. }
  405. err = -EBADF;
  406. switch (cmd) {
  407. case F_GETLK64:
  408. err = fcntl_getlk64(filp, (struct flock64 __user *) arg);
  409. break;
  410. case F_SETLK64:
  411. case F_SETLKW64:
  412. err = fcntl_setlk64(fd, filp, cmd,
  413. (struct flock64 __user *) arg);
  414. break;
  415. default:
  416. err = do_fcntl(fd, cmd, arg, filp);
  417. break;
  418. }
  419. fput(filp);
  420. out:
  421. return err;
  422. }
  423. #endif
  424. /* Table to convert sigio signal codes into poll band bitmaps */
  425. static const long band_table[NSIGPOLL] = {
  426. POLLIN | POLLRDNORM, /* POLL_IN */
  427. POLLOUT | POLLWRNORM | POLLWRBAND, /* POLL_OUT */
  428. POLLIN | POLLRDNORM | POLLMSG, /* POLL_MSG */
  429. POLLERR, /* POLL_ERR */
  430. POLLPRI | POLLRDBAND, /* POLL_PRI */
  431. POLLHUP | POLLERR /* POLL_HUP */
  432. };
  433. static inline int sigio_perm(struct task_struct *p,
  434. struct fown_struct *fown, int sig)
  435. {
  436. const struct cred *cred;
  437. int ret;
  438. rcu_read_lock();
  439. cred = __task_cred(p);
  440. ret = ((fown->euid == 0 ||
  441. fown->euid == cred->suid || fown->euid == cred->uid ||
  442. fown->uid == cred->suid || fown->uid == cred->uid) &&
  443. !security_file_send_sigiotask(p, fown, sig));
  444. rcu_read_unlock();
  445. return ret;
  446. }
  447. static void send_sigio_to_task(struct task_struct *p,
  448. struct fown_struct *fown,
  449. int fd, int reason, int group)
  450. {
  451. /*
  452. * F_SETSIG can change ->signum lockless in parallel, make
  453. * sure we read it once and use the same value throughout.
  454. */
  455. int signum = ACCESS_ONCE(fown->signum);
  456. if (!sigio_perm(p, fown, signum))
  457. return;
  458. switch (signum) {
  459. siginfo_t si;
  460. default:
  461. /* Queue a rt signal with the appropriate fd as its
  462. value. We use SI_SIGIO as the source, not
  463. SI_KERNEL, since kernel signals always get
  464. delivered even if we can't queue. Failure to
  465. queue in this case _should_ be reported; we fall
  466. back to SIGIO in that case. --sct */
  467. si.si_signo = signum;
  468. si.si_errno = 0;
  469. si.si_code = reason;
  470. /* Make sure we are called with one of the POLL_*
  471. reasons, otherwise we could leak kernel stack into
  472. userspace. */
  473. BUG_ON((reason & __SI_MASK) != __SI_POLL);
  474. if (reason - POLL_IN >= NSIGPOLL)
  475. si.si_band = ~0L;
  476. else
  477. si.si_band = band_table[reason - POLL_IN];
  478. si.si_fd = fd;
  479. if (!do_send_sig_info(signum, &si, p, group))
  480. break;
  481. /* fall-through: fall back on the old plain SIGIO signal */
  482. case 0:
  483. do_send_sig_info(SIGIO, SEND_SIG_PRIV, p, group);
  484. }
  485. }
  486. void send_sigio(struct fown_struct *fown, int fd, int band)
  487. {
  488. struct task_struct *p;
  489. enum pid_type type;
  490. struct pid *pid;
  491. int group = 1;
  492. read_lock(&fown->lock);
  493. type = fown->pid_type;
  494. if (type == PIDTYPE_MAX) {
  495. group = 0;
  496. type = PIDTYPE_PID;
  497. }
  498. pid = fown->pid;
  499. if (!pid)
  500. goto out_unlock_fown;
  501. read_lock(&tasklist_lock);
  502. do_each_pid_task(pid, type, p) {
  503. send_sigio_to_task(p, fown, fd, band, group);
  504. } while_each_pid_task(pid, type, p);
  505. read_unlock(&tasklist_lock);
  506. out_unlock_fown:
  507. read_unlock(&fown->lock);
  508. }
  509. static void send_sigurg_to_task(struct task_struct *p,
  510. struct fown_struct *fown, int group)
  511. {
  512. if (sigio_perm(p, fown, SIGURG))
  513. do_send_sig_info(SIGURG, SEND_SIG_PRIV, p, group);
  514. }
  515. int send_sigurg(struct fown_struct *fown)
  516. {
  517. struct task_struct *p;
  518. enum pid_type type;
  519. struct pid *pid;
  520. int group = 1;
  521. int ret = 0;
  522. read_lock(&fown->lock);
  523. type = fown->pid_type;
  524. if (type == PIDTYPE_MAX) {
  525. group = 0;
  526. type = PIDTYPE_PID;
  527. }
  528. pid = fown->pid;
  529. if (!pid)
  530. goto out_unlock_fown;
  531. ret = 1;
  532. read_lock(&tasklist_lock);
  533. do_each_pid_task(pid, type, p) {
  534. send_sigurg_to_task(p, fown, group);
  535. } while_each_pid_task(pid, type, p);
  536. read_unlock(&tasklist_lock);
  537. out_unlock_fown:
  538. read_unlock(&fown->lock);
  539. return ret;
  540. }
  541. static DEFINE_RWLOCK(fasync_lock);
  542. static struct kmem_cache *fasync_cache __read_mostly;
  543. /*
  544. * Remove a fasync entry. If successfully removed, return
  545. * positive and clear the FASYNC flag. If no entry exists,
  546. * do nothing and return 0.
  547. *
  548. * NOTE! It is very important that the FASYNC flag always
  549. * match the state "is the filp on a fasync list".
  550. *
  551. * We always take the 'filp->f_lock', in since fasync_lock
  552. * needs to be irq-safe.
  553. */
  554. static int fasync_remove_entry(struct file *filp, struct fasync_struct **fapp)
  555. {
  556. struct fasync_struct *fa, **fp;
  557. int result = 0;
  558. spin_lock(&filp->f_lock);
  559. write_lock_irq(&fasync_lock);
  560. for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
  561. if (fa->fa_file != filp)
  562. continue;
  563. *fp = fa->fa_next;
  564. kmem_cache_free(fasync_cache, fa);
  565. filp->f_flags &= ~FASYNC;
  566. result = 1;
  567. break;
  568. }
  569. write_unlock_irq(&fasync_lock);
  570. spin_unlock(&filp->f_lock);
  571. return result;
  572. }
  573. /*
  574. * Add a fasync entry. Return negative on error, positive if
  575. * added, and zero if did nothing but change an existing one.
  576. *
  577. * NOTE! It is very important that the FASYNC flag always
  578. * match the state "is the filp on a fasync list".
  579. */
  580. static int fasync_add_entry(int fd, struct file *filp, struct fasync_struct **fapp)
  581. {
  582. struct fasync_struct *new, *fa, **fp;
  583. int result = 0;
  584. new = kmem_cache_alloc(fasync_cache, GFP_KERNEL);
  585. if (!new)
  586. return -ENOMEM;
  587. spin_lock(&filp->f_lock);
  588. write_lock_irq(&fasync_lock);
  589. for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
  590. if (fa->fa_file != filp)
  591. continue;
  592. fa->fa_fd = fd;
  593. kmem_cache_free(fasync_cache, new);
  594. goto out;
  595. }
  596. new->magic = FASYNC_MAGIC;
  597. new->fa_file = filp;
  598. new->fa_fd = fd;
  599. new->fa_next = *fapp;
  600. *fapp = new;
  601. result = 1;
  602. filp->f_flags |= FASYNC;
  603. out:
  604. write_unlock_irq(&fasync_lock);
  605. spin_unlock(&filp->f_lock);
  606. return result;
  607. }
  608. /*
  609. * fasync_helper() is used by almost all character device drivers
  610. * to set up the fasync queue, and for regular files by the file
  611. * lease code. It returns negative on error, 0 if it did no changes
  612. * and positive if it added/deleted the entry.
  613. */
  614. int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
  615. {
  616. if (!on)
  617. return fasync_remove_entry(filp, fapp);
  618. return fasync_add_entry(fd, filp, fapp);
  619. }
  620. EXPORT_SYMBOL(fasync_helper);
  621. void __kill_fasync(struct fasync_struct *fa, int sig, int band)
  622. {
  623. while (fa) {
  624. struct fown_struct * fown;
  625. if (fa->magic != FASYNC_MAGIC) {
  626. printk(KERN_ERR "kill_fasync: bad magic number in "
  627. "fasync_struct!\n");
  628. return;
  629. }
  630. fown = &fa->fa_file->f_owner;
  631. /* Don't send SIGURG to processes which have not set a
  632. queued signum: SIGURG has its own default signalling
  633. mechanism. */
  634. if (!(sig == SIGURG && fown->signum == 0))
  635. send_sigio(fown, fa->fa_fd, band);
  636. fa = fa->fa_next;
  637. }
  638. }
  639. EXPORT_SYMBOL(__kill_fasync);
  640. void kill_fasync(struct fasync_struct **fp, int sig, int band)
  641. {
  642. /* First a quick test without locking: usually
  643. * the list is empty.
  644. */
  645. if (*fp) {
  646. read_lock(&fasync_lock);
  647. /* reread *fp after obtaining the lock */
  648. __kill_fasync(*fp, sig, band);
  649. read_unlock(&fasync_lock);
  650. }
  651. }
  652. EXPORT_SYMBOL(kill_fasync);
  653. static int __init fasync_init(void)
  654. {
  655. fasync_cache = kmem_cache_create("fasync_cache",
  656. sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL);
  657. return 0;
  658. }
  659. module_init(fasync_init)