fcntl.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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. #ifdef CONFIG_SEC_FILE_LEAK_DEBUG
  28. extern void sec_debug_EMFILE_error_proc(unsigned long files_addr);
  29. #endif
  30. void set_close_on_exec(unsigned int fd, int flag)
  31. {
  32. struct files_struct *files = current->files;
  33. struct fdtable *fdt;
  34. spin_lock(&files->file_lock);
  35. fdt = files_fdtable(files);
  36. if (flag)
  37. __set_close_on_exec(fd, fdt);
  38. else
  39. __clear_close_on_exec(fd, fdt);
  40. spin_unlock(&files->file_lock);
  41. }
  42. static bool get_close_on_exec(unsigned int fd)
  43. {
  44. struct files_struct *files = current->files;
  45. struct fdtable *fdt;
  46. bool res;
  47. rcu_read_lock();
  48. fdt = files_fdtable(files);
  49. res = close_on_exec(fd, fdt);
  50. rcu_read_unlock();
  51. return res;
  52. }
  53. SYSCALL_DEFINE3(dup3, unsigned int, oldfd, unsigned int, newfd, int, flags)
  54. {
  55. int err = -EBADF;
  56. struct file * file, *tofree;
  57. struct files_struct * files = current->files;
  58. struct fdtable *fdt;
  59. if ((flags & ~O_CLOEXEC) != 0)
  60. return -EINVAL;
  61. if (unlikely(oldfd == newfd))
  62. return -EINVAL;
  63. if (newfd >= rlimit(RLIMIT_NOFILE)) {
  64. #ifdef CONFIG_SEC_FILE_LEAK_DEBUG
  65. sec_debug_EMFILE_error_proc((unsigned long)files);
  66. #endif
  67. return -EMFILE;
  68. }
  69. spin_lock(&files->file_lock);
  70. err = expand_files(files, newfd);
  71. file = fcheck(oldfd);
  72. if (unlikely(!file))
  73. goto Ebadf;
  74. if (unlikely(err < 0)) {
  75. if (err == -EMFILE)
  76. goto Ebadf;
  77. goto out_unlock;
  78. }
  79. /*
  80. * We need to detect attempts to do dup2() over allocated but still
  81. * not finished descriptor. NB: OpenBSD avoids that at the price of
  82. * extra work in their equivalent of fget() - they insert struct
  83. * file immediately after grabbing descriptor, mark it larval if
  84. * more work (e.g. actual opening) is needed and make sure that
  85. * fget() treats larval files as absent. Potentially interesting,
  86. * but while extra work in fget() is trivial, locking implications
  87. * and amount of surgery on open()-related paths in VFS are not.
  88. * FreeBSD fails with -EBADF in the same situation, NetBSD "solution"
  89. * deadlocks in rather amusing ways, AFAICS. All of that is out of
  90. * scope of POSIX or SUS, since neither considers shared descriptor
  91. * tables and this condition does not arise without those.
  92. */
  93. err = -EBUSY;
  94. fdt = files_fdtable(files);
  95. tofree = fdt->fd[newfd];
  96. if (!tofree && fd_is_open(newfd, fdt))
  97. goto out_unlock;
  98. get_file(file);
  99. rcu_assign_pointer(fdt->fd[newfd], file);
  100. __set_open_fd(newfd, fdt);
  101. if (flags & O_CLOEXEC)
  102. __set_close_on_exec(newfd, fdt);
  103. else
  104. __clear_close_on_exec(newfd, fdt);
  105. spin_unlock(&files->file_lock);
  106. if (tofree)
  107. filp_close(tofree, files);
  108. return newfd;
  109. Ebadf:
  110. err = -EBADF;
  111. out_unlock:
  112. spin_unlock(&files->file_lock);
  113. return err;
  114. }
  115. SYSCALL_DEFINE2(dup2, unsigned int, oldfd, unsigned int, newfd)
  116. {
  117. if (unlikely(newfd == oldfd)) { /* corner case */
  118. struct files_struct *files = current->files;
  119. int retval = oldfd;
  120. rcu_read_lock();
  121. if (!fcheck_files(files, oldfd))
  122. retval = -EBADF;
  123. rcu_read_unlock();
  124. return retval;
  125. }
  126. return sys_dup3(oldfd, newfd, 0);
  127. }
  128. SYSCALL_DEFINE1(dup, unsigned int, fildes)
  129. {
  130. int ret = -EBADF;
  131. struct file *file = fget_raw(fildes);
  132. if (file) {
  133. ret = get_unused_fd();
  134. if (ret >= 0)
  135. fd_install(ret, file);
  136. else
  137. fput(file);
  138. }
  139. return ret;
  140. }
  141. #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
  142. static int setfl(int fd, struct file * filp, unsigned long arg)
  143. {
  144. struct inode * inode = filp->f_path.dentry->d_inode;
  145. int error = 0;
  146. /*
  147. * O_APPEND cannot be cleared if the file is marked as append-only
  148. * and the file is open for write.
  149. */
  150. if (((arg ^ filp->f_flags) & O_APPEND) && IS_APPEND(inode))
  151. return -EPERM;
  152. /* O_NOATIME can only be set by the owner or superuser */
  153. if ((arg & O_NOATIME) && !(filp->f_flags & O_NOATIME))
  154. if (!inode_owner_or_capable(inode))
  155. return -EPERM;
  156. /* required for strict SunOS emulation */
  157. if (O_NONBLOCK != O_NDELAY)
  158. if (arg & O_NDELAY)
  159. arg |= O_NONBLOCK;
  160. if (arg & O_DIRECT) {
  161. if (!filp->f_mapping || !filp->f_mapping->a_ops ||
  162. !filp->f_mapping->a_ops->direct_IO)
  163. return -EINVAL;
  164. }
  165. if (filp->f_op && filp->f_op->check_flags)
  166. error = filp->f_op->check_flags(arg);
  167. if (error)
  168. return error;
  169. /*
  170. * ->fasync() is responsible for setting the FASYNC bit.
  171. */
  172. if (((arg ^ filp->f_flags) & FASYNC) && filp->f_op &&
  173. filp->f_op->fasync) {
  174. error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0);
  175. if (error < 0)
  176. goto out;
  177. if (error > 0)
  178. error = 0;
  179. }
  180. spin_lock(&filp->f_lock);
  181. filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
  182. spin_unlock(&filp->f_lock);
  183. out:
  184. return error;
  185. }
  186. static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
  187. int force)
  188. {
  189. write_lock_irq(&filp->f_owner.lock);
  190. if (force || !filp->f_owner.pid) {
  191. put_pid(filp->f_owner.pid);
  192. filp->f_owner.pid = get_pid(pid);
  193. filp->f_owner.pid_type = type;
  194. if (pid) {
  195. const struct cred *cred = current_cred();
  196. filp->f_owner.uid = cred->uid;
  197. filp->f_owner.euid = cred->euid;
  198. }
  199. }
  200. write_unlock_irq(&filp->f_owner.lock);
  201. }
  202. int __f_setown(struct file *filp, struct pid *pid, enum pid_type type,
  203. int force)
  204. {
  205. int err;
  206. err = security_file_set_fowner(filp);
  207. if (err)
  208. return err;
  209. f_modown(filp, pid, type, force);
  210. return 0;
  211. }
  212. EXPORT_SYMBOL(__f_setown);
  213. int f_setown(struct file *filp, unsigned long arg, int force)
  214. {
  215. enum pid_type type;
  216. struct pid *pid;
  217. int who = arg;
  218. int result;
  219. type = PIDTYPE_PID;
  220. if (who < 0) {
  221. type = PIDTYPE_PGID;
  222. who = -who;
  223. }
  224. rcu_read_lock();
  225. pid = find_vpid(who);
  226. result = __f_setown(filp, pid, type, force);
  227. rcu_read_unlock();
  228. return result;
  229. }
  230. EXPORT_SYMBOL(f_setown);
  231. void f_delown(struct file *filp)
  232. {
  233. f_modown(filp, NULL, PIDTYPE_PID, 1);
  234. }
  235. pid_t f_getown(struct file *filp)
  236. {
  237. pid_t pid;
  238. read_lock(&filp->f_owner.lock);
  239. pid = pid_vnr(filp->f_owner.pid);
  240. if (filp->f_owner.pid_type == PIDTYPE_PGID)
  241. pid = -pid;
  242. read_unlock(&filp->f_owner.lock);
  243. return pid;
  244. }
  245. static int f_setown_ex(struct file *filp, unsigned long arg)
  246. {
  247. struct f_owner_ex * __user owner_p = (void * __user)arg;
  248. struct f_owner_ex owner;
  249. struct pid *pid;
  250. int type;
  251. int ret;
  252. ret = copy_from_user(&owner, owner_p, sizeof(owner));
  253. if (ret)
  254. return -EFAULT;
  255. switch (owner.type) {
  256. case F_OWNER_TID:
  257. type = PIDTYPE_MAX;
  258. break;
  259. case F_OWNER_PID:
  260. type = PIDTYPE_PID;
  261. break;
  262. case F_OWNER_PGRP:
  263. type = PIDTYPE_PGID;
  264. break;
  265. default:
  266. return -EINVAL;
  267. }
  268. rcu_read_lock();
  269. pid = find_vpid(owner.pid);
  270. if (owner.pid && !pid)
  271. ret = -ESRCH;
  272. else
  273. ret = __f_setown(filp, pid, type, 1);
  274. rcu_read_unlock();
  275. return ret;
  276. }
  277. static int f_getown_ex(struct file *filp, unsigned long arg)
  278. {
  279. struct f_owner_ex * __user owner_p = (void * __user)arg;
  280. struct f_owner_ex owner;
  281. int ret = 0;
  282. read_lock(&filp->f_owner.lock);
  283. owner.pid = pid_vnr(filp->f_owner.pid);
  284. switch (filp->f_owner.pid_type) {
  285. case PIDTYPE_MAX:
  286. owner.type = F_OWNER_TID;
  287. break;
  288. case PIDTYPE_PID:
  289. owner.type = F_OWNER_PID;
  290. break;
  291. case PIDTYPE_PGID:
  292. owner.type = F_OWNER_PGRP;
  293. break;
  294. default:
  295. WARN_ON(1);
  296. ret = -EINVAL;
  297. break;
  298. }
  299. read_unlock(&filp->f_owner.lock);
  300. if (!ret) {
  301. ret = copy_to_user(owner_p, &owner, sizeof(owner));
  302. if (ret)
  303. ret = -EFAULT;
  304. }
  305. return ret;
  306. }
  307. #ifdef CONFIG_CHECKPOINT_RESTORE
  308. static int f_getowner_uids(struct file *filp, unsigned long arg)
  309. {
  310. struct user_namespace *user_ns = current_user_ns();
  311. uid_t * __user dst = (void * __user)arg;
  312. uid_t src[2];
  313. int err;
  314. read_lock(&filp->f_owner.lock);
  315. src[0] = from_kuid(user_ns, filp->f_owner.uid);
  316. src[1] = from_kuid(user_ns, filp->f_owner.euid);
  317. read_unlock(&filp->f_owner.lock);
  318. err = put_user(src[0], &dst[0]);
  319. err |= put_user(src[1], &dst[1]);
  320. return err;
  321. }
  322. #else
  323. static int f_getowner_uids(struct file *filp, unsigned long arg)
  324. {
  325. return -EINVAL;
  326. }
  327. #endif
  328. static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
  329. struct file *filp)
  330. {
  331. long err = -EINVAL;
  332. switch (cmd) {
  333. case F_DUPFD:
  334. case F_DUPFD_CLOEXEC:
  335. if (arg >= rlimit(RLIMIT_NOFILE))
  336. break;
  337. err = alloc_fd(arg, cmd == F_DUPFD_CLOEXEC ? O_CLOEXEC : 0);
  338. if (err >= 0) {
  339. get_file(filp);
  340. fd_install(err, filp);
  341. }
  342. break;
  343. case F_GETFD:
  344. err = get_close_on_exec(fd) ? FD_CLOEXEC : 0;
  345. break;
  346. case F_SETFD:
  347. err = 0;
  348. set_close_on_exec(fd, arg & FD_CLOEXEC);
  349. break;
  350. case F_GETFL:
  351. err = filp->f_flags;
  352. break;
  353. case F_SETFL:
  354. err = setfl(fd, filp, arg);
  355. break;
  356. #if BITS_PER_LONG != 32
  357. /* 32-bit arches must use fcntl64() */
  358. case F_OFD_GETLK:
  359. #endif
  360. case F_GETLK:
  361. err = fcntl_getlk(filp, cmd, (struct flock __user *) arg);
  362. break;
  363. #if BITS_PER_LONG != 32
  364. /* 32-bit arches must use fcntl64() */
  365. case F_OFD_SETLK:
  366. case F_OFD_SETLKW:
  367. #endif
  368. /* Fallthrough */
  369. case F_SETLK:
  370. case F_SETLKW:
  371. err = fcntl_setlk(fd, filp, cmd, (struct flock __user *) arg);
  372. break;
  373. case F_GETOWN:
  374. /*
  375. * XXX If f_owner is a process group, the
  376. * negative return value will get converted
  377. * into an error. Oops. If we keep the
  378. * current syscall conventions, the only way
  379. * to fix this will be in libc.
  380. */
  381. err = f_getown(filp);
  382. force_successful_syscall_return();
  383. break;
  384. case F_SETOWN:
  385. err = f_setown(filp, arg, 1);
  386. break;
  387. case F_GETOWN_EX:
  388. err = f_getown_ex(filp, arg);
  389. break;
  390. case F_SETOWN_EX:
  391. err = f_setown_ex(filp, arg);
  392. break;
  393. case F_GETOWNER_UIDS:
  394. err = f_getowner_uids(filp, arg);
  395. break;
  396. case F_GETSIG:
  397. err = filp->f_owner.signum;
  398. break;
  399. case F_SETSIG:
  400. /* arg == 0 restores default behaviour. */
  401. if (!valid_signal(arg)) {
  402. break;
  403. }
  404. err = 0;
  405. filp->f_owner.signum = arg;
  406. break;
  407. case F_GETLEASE:
  408. err = fcntl_getlease(filp);
  409. break;
  410. case F_SETLEASE:
  411. err = fcntl_setlease(fd, filp, arg);
  412. break;
  413. case F_NOTIFY:
  414. err = fcntl_dirnotify(fd, filp, arg);
  415. break;
  416. case F_SETPIPE_SZ:
  417. case F_GETPIPE_SZ:
  418. err = pipe_fcntl(filp, cmd, arg);
  419. break;
  420. case F_ADD_SEALS:
  421. case F_GET_SEALS:
  422. err = shmem_fcntl(filp, cmd, arg);
  423. break;
  424. default:
  425. break;
  426. }
  427. return err;
  428. }
  429. static int check_fcntl_cmd(unsigned cmd)
  430. {
  431. switch (cmd) {
  432. case F_DUPFD:
  433. case F_DUPFD_CLOEXEC:
  434. case F_GETFD:
  435. case F_SETFD:
  436. case F_GETFL:
  437. return 1;
  438. }
  439. return 0;
  440. }
  441. SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
  442. {
  443. struct file *filp;
  444. int fput_needed;
  445. long err = -EBADF;
  446. filp = fget_raw_light(fd, &fput_needed);
  447. if (!filp)
  448. goto out;
  449. if (unlikely(filp->f_mode & FMODE_PATH)) {
  450. if (!check_fcntl_cmd(cmd))
  451. goto out1;
  452. }
  453. err = security_file_fcntl(filp, cmd, arg);
  454. if (!err)
  455. err = do_fcntl(fd, cmd, arg, filp);
  456. out1:
  457. fput_light(filp, fput_needed);
  458. out:
  459. return err;
  460. }
  461. #if BITS_PER_LONG == 32
  462. SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
  463. unsigned long, arg)
  464. {
  465. struct file * filp;
  466. long err = -EBADF;
  467. int fput_needed;
  468. filp = fget_raw_light(fd, &fput_needed);
  469. if (!filp)
  470. goto out;
  471. if (unlikely(filp->f_mode & FMODE_PATH)) {
  472. if (!check_fcntl_cmd(cmd))
  473. goto out1;
  474. }
  475. err = security_file_fcntl(filp, cmd, arg);
  476. if (err)
  477. goto out1;
  478. switch (cmd) {
  479. case F_GETLK64:
  480. case F_OFD_GETLK:
  481. err = fcntl_getlk64(filp, cmd, (struct flock64 __user *) arg);
  482. break;
  483. case F_SETLK64:
  484. case F_SETLKW64:
  485. case F_OFD_SETLK:
  486. case F_OFD_SETLKW:
  487. err = fcntl_setlk64(fd, filp, cmd,
  488. (struct flock64 __user *) arg);
  489. break;
  490. default:
  491. err = do_fcntl(fd, cmd, arg, filp);
  492. break;
  493. }
  494. out1:
  495. fput_light(filp, fput_needed);
  496. out:
  497. return err;
  498. }
  499. #endif
  500. /* Table to convert sigio signal codes into poll band bitmaps */
  501. static const long band_table[NSIGPOLL] = {
  502. POLLIN | POLLRDNORM, /* POLL_IN */
  503. POLLOUT | POLLWRNORM | POLLWRBAND, /* POLL_OUT */
  504. POLLIN | POLLRDNORM | POLLMSG, /* POLL_MSG */
  505. POLLERR, /* POLL_ERR */
  506. POLLPRI | POLLRDBAND, /* POLL_PRI */
  507. POLLHUP | POLLERR /* POLL_HUP */
  508. };
  509. static inline int sigio_perm(struct task_struct *p,
  510. struct fown_struct *fown, int sig)
  511. {
  512. const struct cred *cred;
  513. int ret;
  514. rcu_read_lock();
  515. cred = __task_cred(p);
  516. ret = ((uid_eq(fown->euid, GLOBAL_ROOT_UID) ||
  517. uid_eq(fown->euid, cred->suid) || uid_eq(fown->euid, cred->uid) ||
  518. uid_eq(fown->uid, cred->suid) || uid_eq(fown->uid, cred->uid)) &&
  519. !security_file_send_sigiotask(p, fown, sig));
  520. rcu_read_unlock();
  521. return ret;
  522. }
  523. static void send_sigio_to_task(struct task_struct *p,
  524. struct fown_struct *fown,
  525. int fd, int reason, int group)
  526. {
  527. /*
  528. * F_SETSIG can change ->signum lockless in parallel, make
  529. * sure we read it once and use the same value throughout.
  530. */
  531. int signum = ACCESS_ONCE(fown->signum);
  532. if (!sigio_perm(p, fown, signum))
  533. return;
  534. switch (signum) {
  535. siginfo_t si;
  536. default:
  537. /* Queue a rt signal with the appropriate fd as its
  538. value. We use SI_SIGIO as the source, not
  539. SI_KERNEL, since kernel signals always get
  540. delivered even if we can't queue. Failure to
  541. queue in this case _should_ be reported; we fall
  542. back to SIGIO in that case. --sct */
  543. si.si_signo = signum;
  544. si.si_errno = 0;
  545. si.si_code = reason;
  546. /* Make sure we are called with one of the POLL_*
  547. reasons, otherwise we could leak kernel stack into
  548. userspace. */
  549. BUG_ON((reason & __SI_MASK) != __SI_POLL);
  550. if (reason - POLL_IN >= NSIGPOLL)
  551. si.si_band = ~0L;
  552. else
  553. si.si_band = band_table[reason - POLL_IN];
  554. si.si_fd = fd;
  555. if (!do_send_sig_info(signum, &si, p, group))
  556. break;
  557. /* fall-through: fall back on the old plain SIGIO signal */
  558. case 0:
  559. do_send_sig_info(SIGIO, SEND_SIG_PRIV, p, group);
  560. }
  561. }
  562. void send_sigio(struct fown_struct *fown, int fd, int band)
  563. {
  564. struct task_struct *p;
  565. enum pid_type type;
  566. struct pid *pid;
  567. int group = 1;
  568. read_lock(&fown->lock);
  569. type = fown->pid_type;
  570. if (type == PIDTYPE_MAX) {
  571. group = 0;
  572. type = PIDTYPE_PID;
  573. }
  574. pid = fown->pid;
  575. if (!pid)
  576. goto out_unlock_fown;
  577. read_lock(&tasklist_lock);
  578. do_each_pid_task(pid, type, p) {
  579. send_sigio_to_task(p, fown, fd, band, group);
  580. } while_each_pid_task(pid, type, p);
  581. read_unlock(&tasklist_lock);
  582. out_unlock_fown:
  583. read_unlock(&fown->lock);
  584. }
  585. static void send_sigurg_to_task(struct task_struct *p,
  586. struct fown_struct *fown, int group)
  587. {
  588. if (sigio_perm(p, fown, SIGURG))
  589. do_send_sig_info(SIGURG, SEND_SIG_PRIV, p, group);
  590. }
  591. int send_sigurg(struct fown_struct *fown)
  592. {
  593. struct task_struct *p;
  594. enum pid_type type;
  595. struct pid *pid;
  596. int group = 1;
  597. int ret = 0;
  598. read_lock(&fown->lock);
  599. type = fown->pid_type;
  600. if (type == PIDTYPE_MAX) {
  601. group = 0;
  602. type = PIDTYPE_PID;
  603. }
  604. pid = fown->pid;
  605. if (!pid)
  606. goto out_unlock_fown;
  607. ret = 1;
  608. read_lock(&tasklist_lock);
  609. do_each_pid_task(pid, type, p) {
  610. send_sigurg_to_task(p, fown, group);
  611. } while_each_pid_task(pid, type, p);
  612. read_unlock(&tasklist_lock);
  613. out_unlock_fown:
  614. read_unlock(&fown->lock);
  615. return ret;
  616. }
  617. static DEFINE_SPINLOCK(fasync_lock);
  618. static struct kmem_cache *fasync_cache __read_mostly;
  619. static void fasync_free_rcu(struct rcu_head *head)
  620. {
  621. kmem_cache_free(fasync_cache,
  622. container_of(head, struct fasync_struct, fa_rcu));
  623. }
  624. /*
  625. * Remove a fasync entry. If successfully removed, return
  626. * positive and clear the FASYNC flag. If no entry exists,
  627. * do nothing and return 0.
  628. *
  629. * NOTE! It is very important that the FASYNC flag always
  630. * match the state "is the filp on a fasync list".
  631. *
  632. */
  633. int fasync_remove_entry(struct file *filp, struct fasync_struct **fapp)
  634. {
  635. struct fasync_struct *fa, **fp;
  636. int result = 0;
  637. spin_lock(&filp->f_lock);
  638. spin_lock(&fasync_lock);
  639. for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
  640. if (fa->fa_file != filp)
  641. continue;
  642. spin_lock_irq(&fa->fa_lock);
  643. fa->fa_file = NULL;
  644. spin_unlock_irq(&fa->fa_lock);
  645. *fp = fa->fa_next;
  646. call_rcu(&fa->fa_rcu, fasync_free_rcu);
  647. filp->f_flags &= ~FASYNC;
  648. result = 1;
  649. break;
  650. }
  651. spin_unlock(&fasync_lock);
  652. spin_unlock(&filp->f_lock);
  653. return result;
  654. }
  655. struct fasync_struct *fasync_alloc(void)
  656. {
  657. return kmem_cache_alloc(fasync_cache, GFP_KERNEL);
  658. }
  659. /*
  660. * NOTE! This can be used only for unused fasync entries:
  661. * entries that actually got inserted on the fasync list
  662. * need to be released by rcu - see fasync_remove_entry.
  663. */
  664. void fasync_free(struct fasync_struct *new)
  665. {
  666. kmem_cache_free(fasync_cache, new);
  667. }
  668. /*
  669. * Insert a new entry into the fasync list. Return the pointer to the
  670. * old one if we didn't use the new one.
  671. *
  672. * NOTE! It is very important that the FASYNC flag always
  673. * match the state "is the filp on a fasync list".
  674. */
  675. struct fasync_struct *fasync_insert_entry(int fd, struct file *filp, struct fasync_struct **fapp, struct fasync_struct *new)
  676. {
  677. struct fasync_struct *fa, **fp;
  678. spin_lock(&filp->f_lock);
  679. spin_lock(&fasync_lock);
  680. for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
  681. if (fa->fa_file != filp)
  682. continue;
  683. spin_lock_irq(&fa->fa_lock);
  684. fa->fa_fd = fd;
  685. spin_unlock_irq(&fa->fa_lock);
  686. goto out;
  687. }
  688. spin_lock_init(&new->fa_lock);
  689. new->magic = FASYNC_MAGIC;
  690. new->fa_file = filp;
  691. new->fa_fd = fd;
  692. new->fa_next = *fapp;
  693. rcu_assign_pointer(*fapp, new);
  694. filp->f_flags |= FASYNC;
  695. out:
  696. spin_unlock(&fasync_lock);
  697. spin_unlock(&filp->f_lock);
  698. return fa;
  699. }
  700. /*
  701. * Add a fasync entry. Return negative on error, positive if
  702. * added, and zero if did nothing but change an existing one.
  703. */
  704. static int fasync_add_entry(int fd, struct file *filp, struct fasync_struct **fapp)
  705. {
  706. struct fasync_struct *new;
  707. new = fasync_alloc();
  708. if (!new)
  709. return -ENOMEM;
  710. /*
  711. * fasync_insert_entry() returns the old (update) entry if
  712. * it existed.
  713. *
  714. * So free the (unused) new entry and return 0 to let the
  715. * caller know that we didn't add any new fasync entries.
  716. */
  717. if (fasync_insert_entry(fd, filp, fapp, new)) {
  718. fasync_free(new);
  719. return 0;
  720. }
  721. return 1;
  722. }
  723. /*
  724. * fasync_helper() is used by almost all character device drivers
  725. * to set up the fasync queue, and for regular files by the file
  726. * lease code. It returns negative on error, 0 if it did no changes
  727. * and positive if it added/deleted the entry.
  728. */
  729. int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
  730. {
  731. if (!on)
  732. return fasync_remove_entry(filp, fapp);
  733. return fasync_add_entry(fd, filp, fapp);
  734. }
  735. EXPORT_SYMBOL(fasync_helper);
  736. /*
  737. * rcu_read_lock() is held
  738. */
  739. static void kill_fasync_rcu(struct fasync_struct *fa, int sig, int band)
  740. {
  741. while (fa) {
  742. struct fown_struct *fown;
  743. unsigned long flags;
  744. if (fa->magic != FASYNC_MAGIC) {
  745. printk(KERN_ERR "kill_fasync: bad magic number in "
  746. "fasync_struct!\n");
  747. return;
  748. }
  749. spin_lock_irqsave(&fa->fa_lock, flags);
  750. if (fa->fa_file) {
  751. fown = &fa->fa_file->f_owner;
  752. /* Don't send SIGURG to processes which have not set a
  753. queued signum: SIGURG has its own default signalling
  754. mechanism. */
  755. if (!(sig == SIGURG && fown->signum == 0))
  756. send_sigio(fown, fa->fa_fd, band);
  757. }
  758. spin_unlock_irqrestore(&fa->fa_lock, flags);
  759. fa = rcu_dereference(fa->fa_next);
  760. }
  761. }
  762. void kill_fasync(struct fasync_struct **fp, int sig, int band)
  763. {
  764. /* First a quick test without locking: usually
  765. * the list is empty.
  766. */
  767. if (*fp) {
  768. rcu_read_lock();
  769. kill_fasync_rcu(rcu_dereference(*fp), sig, band);
  770. rcu_read_unlock();
  771. }
  772. }
  773. EXPORT_SYMBOL(kill_fasync);
  774. static int __init fcntl_init(void)
  775. {
  776. /*
  777. * Please add new bits here to ensure allocation uniqueness.
  778. * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY
  779. * is defined as O_NONBLOCK on some platforms and not on others.
  780. */
  781. BUILD_BUG_ON(20 - 1 /* for O_RDONLY being 0 */ != HWEIGHT32(
  782. O_RDONLY | O_WRONLY | O_RDWR |
  783. O_CREAT | O_EXCL | O_NOCTTY |
  784. O_TRUNC | O_APPEND | /* O_NONBLOCK | */
  785. __O_SYNC | O_DSYNC | FASYNC |
  786. O_DIRECT | O_LARGEFILE | O_DIRECTORY |
  787. O_NOFOLLOW | O_NOATIME | O_CLOEXEC |
  788. __FMODE_EXEC | O_PATH | __O_TMPFILE
  789. ));
  790. fasync_cache = kmem_cache_create("fasync_cache",
  791. sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL);
  792. return 0;
  793. }
  794. module_init(fcntl_init)