fcntl.c 17 KB

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