fcntl.c 16 KB

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