fcntl.c 17 KB

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