fcntl.c 17 KB

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