fcntl.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  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. long err = -EBADF;
  445. filp = fget_raw(fd);
  446. if (!filp)
  447. goto out;
  448. if (unlikely(filp->f_mode & FMODE_PATH)) {
  449. if (!check_fcntl_cmd(cmd)) {
  450. fput(filp);
  451. goto out;
  452. }
  453. }
  454. err = security_file_fcntl(filp, cmd, arg);
  455. if (err) {
  456. fput(filp);
  457. return err;
  458. }
  459. err = do_fcntl(fd, cmd, arg, filp);
  460. fput(filp);
  461. out:
  462. return err;
  463. }
  464. #if BITS_PER_LONG == 32
  465. SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
  466. unsigned long, arg)
  467. {
  468. struct file * filp;
  469. long err;
  470. err = -EBADF;
  471. filp = fget_raw(fd);
  472. if (!filp)
  473. goto out;
  474. if (unlikely(filp->f_mode & FMODE_PATH)) {
  475. if (!check_fcntl_cmd(cmd)) {
  476. fput(filp);
  477. goto out;
  478. }
  479. }
  480. err = security_file_fcntl(filp, cmd, arg);
  481. if (err) {
  482. fput(filp);
  483. return err;
  484. }
  485. err = -EBADF;
  486. switch (cmd) {
  487. case F_GETLK64:
  488. case F_OFD_GETLK:
  489. err = fcntl_getlk64(filp, cmd, (struct flock64 __user *) arg);
  490. break;
  491. case F_SETLK64:
  492. case F_SETLKW64:
  493. case F_OFD_SETLK:
  494. case F_OFD_SETLKW:
  495. err = fcntl_setlk64(fd, filp, cmd,
  496. (struct flock64 __user *) arg);
  497. break;
  498. default:
  499. err = do_fcntl(fd, cmd, arg, filp);
  500. break;
  501. }
  502. fput(filp);
  503. out:
  504. return err;
  505. }
  506. #endif
  507. /* Table to convert sigio signal codes into poll band bitmaps */
  508. static const long band_table[NSIGPOLL] = {
  509. POLLIN | POLLRDNORM, /* POLL_IN */
  510. POLLOUT | POLLWRNORM | POLLWRBAND, /* POLL_OUT */
  511. POLLIN | POLLRDNORM | POLLMSG, /* POLL_MSG */
  512. POLLERR, /* POLL_ERR */
  513. POLLPRI | POLLRDBAND, /* POLL_PRI */
  514. POLLHUP | POLLERR /* POLL_HUP */
  515. };
  516. static inline int sigio_perm(struct task_struct *p,
  517. struct fown_struct *fown, int sig)
  518. {
  519. const struct cred *cred;
  520. int ret;
  521. rcu_read_lock();
  522. cred = __task_cred(p);
  523. ret = ((uid_eq(fown->euid, GLOBAL_ROOT_UID) ||
  524. uid_eq(fown->euid, cred->suid) || uid_eq(fown->euid, cred->uid) ||
  525. uid_eq(fown->uid, cred->suid) || uid_eq(fown->uid, cred->uid)) &&
  526. !security_file_send_sigiotask(p, fown, sig));
  527. rcu_read_unlock();
  528. return ret;
  529. }
  530. static void send_sigio_to_task(struct task_struct *p,
  531. struct fown_struct *fown,
  532. int fd, int reason, int group)
  533. {
  534. /*
  535. * F_SETSIG can change ->signum lockless in parallel, make
  536. * sure we read it once and use the same value throughout.
  537. */
  538. int signum = ACCESS_ONCE(fown->signum);
  539. if (!sigio_perm(p, fown, signum))
  540. return;
  541. switch (signum) {
  542. siginfo_t si;
  543. default:
  544. /* Queue a rt signal with the appropriate fd as its
  545. value. We use SI_SIGIO as the source, not
  546. SI_KERNEL, since kernel signals always get
  547. delivered even if we can't queue. Failure to
  548. queue in this case _should_ be reported; we fall
  549. back to SIGIO in that case. --sct */
  550. si.si_signo = signum;
  551. si.si_errno = 0;
  552. si.si_code = reason;
  553. /* Make sure we are called with one of the POLL_*
  554. reasons, otherwise we could leak kernel stack into
  555. userspace. */
  556. BUG_ON((reason & __SI_MASK) != __SI_POLL);
  557. if (reason - POLL_IN >= NSIGPOLL)
  558. si.si_band = ~0L;
  559. else
  560. si.si_band = band_table[reason - POLL_IN];
  561. si.si_fd = fd;
  562. if (!do_send_sig_info(signum, &si, p, group))
  563. break;
  564. /* fall-through: fall back on the old plain SIGIO signal */
  565. case 0:
  566. do_send_sig_info(SIGIO, SEND_SIG_PRIV, p, group);
  567. }
  568. }
  569. void send_sigio(struct fown_struct *fown, int fd, int band)
  570. {
  571. struct task_struct *p;
  572. enum pid_type type;
  573. struct pid *pid;
  574. int group = 1;
  575. read_lock(&fown->lock);
  576. type = fown->pid_type;
  577. if (type == PIDTYPE_MAX) {
  578. group = 0;
  579. type = PIDTYPE_PID;
  580. }
  581. pid = fown->pid;
  582. if (!pid)
  583. goto out_unlock_fown;
  584. read_lock(&tasklist_lock);
  585. do_each_pid_task(pid, type, p) {
  586. send_sigio_to_task(p, fown, fd, band, group);
  587. } while_each_pid_task(pid, type, p);
  588. read_unlock(&tasklist_lock);
  589. out_unlock_fown:
  590. read_unlock(&fown->lock);
  591. }
  592. static void send_sigurg_to_task(struct task_struct *p,
  593. struct fown_struct *fown, int group)
  594. {
  595. if (sigio_perm(p, fown, SIGURG))
  596. do_send_sig_info(SIGURG, SEND_SIG_PRIV, p, group);
  597. }
  598. int send_sigurg(struct fown_struct *fown)
  599. {
  600. struct task_struct *p;
  601. enum pid_type type;
  602. struct pid *pid;
  603. int group = 1;
  604. int ret = 0;
  605. read_lock(&fown->lock);
  606. type = fown->pid_type;
  607. if (type == PIDTYPE_MAX) {
  608. group = 0;
  609. type = PIDTYPE_PID;
  610. }
  611. pid = fown->pid;
  612. if (!pid)
  613. goto out_unlock_fown;
  614. ret = 1;
  615. read_lock(&tasklist_lock);
  616. do_each_pid_task(pid, type, p) {
  617. send_sigurg_to_task(p, fown, group);
  618. } while_each_pid_task(pid, type, p);
  619. read_unlock(&tasklist_lock);
  620. out_unlock_fown:
  621. read_unlock(&fown->lock);
  622. return ret;
  623. }
  624. static DEFINE_SPINLOCK(fasync_lock);
  625. static struct kmem_cache *fasync_cache __read_mostly;
  626. static void fasync_free_rcu(struct rcu_head *head)
  627. {
  628. kmem_cache_free(fasync_cache,
  629. container_of(head, struct fasync_struct, fa_rcu));
  630. }
  631. /*
  632. * Remove a fasync entry. If successfully removed, return
  633. * positive and clear the FASYNC flag. If no entry exists,
  634. * do nothing and return 0.
  635. *
  636. * NOTE! It is very important that the FASYNC flag always
  637. * match the state "is the filp on a fasync list".
  638. *
  639. */
  640. int fasync_remove_entry(struct file *filp, struct fasync_struct **fapp)
  641. {
  642. struct fasync_struct *fa, **fp;
  643. int result = 0;
  644. spin_lock(&filp->f_lock);
  645. spin_lock(&fasync_lock);
  646. for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
  647. if (fa->fa_file != filp)
  648. continue;
  649. spin_lock_irq(&fa->fa_lock);
  650. fa->fa_file = NULL;
  651. spin_unlock_irq(&fa->fa_lock);
  652. *fp = fa->fa_next;
  653. call_rcu(&fa->fa_rcu, fasync_free_rcu);
  654. filp->f_flags &= ~FASYNC;
  655. result = 1;
  656. break;
  657. }
  658. spin_unlock(&fasync_lock);
  659. spin_unlock(&filp->f_lock);
  660. return result;
  661. }
  662. struct fasync_struct *fasync_alloc(void)
  663. {
  664. return kmem_cache_alloc(fasync_cache, GFP_KERNEL);
  665. }
  666. /*
  667. * NOTE! This can be used only for unused fasync entries:
  668. * entries that actually got inserted on the fasync list
  669. * need to be released by rcu - see fasync_remove_entry.
  670. */
  671. void fasync_free(struct fasync_struct *new)
  672. {
  673. kmem_cache_free(fasync_cache, new);
  674. }
  675. /*
  676. * Insert a new entry into the fasync list. Return the pointer to the
  677. * old one if we didn't use the new one.
  678. *
  679. * NOTE! It is very important that the FASYNC flag always
  680. * match the state "is the filp on a fasync list".
  681. */
  682. struct fasync_struct *fasync_insert_entry(int fd, struct file *filp, struct fasync_struct **fapp, struct fasync_struct *new)
  683. {
  684. struct fasync_struct *fa, **fp;
  685. spin_lock(&filp->f_lock);
  686. spin_lock(&fasync_lock);
  687. for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
  688. if (fa->fa_file != filp)
  689. continue;
  690. spin_lock_irq(&fa->fa_lock);
  691. fa->fa_fd = fd;
  692. spin_unlock_irq(&fa->fa_lock);
  693. goto out;
  694. }
  695. spin_lock_init(&new->fa_lock);
  696. new->magic = FASYNC_MAGIC;
  697. new->fa_file = filp;
  698. new->fa_fd = fd;
  699. new->fa_next = *fapp;
  700. rcu_assign_pointer(*fapp, new);
  701. filp->f_flags |= FASYNC;
  702. out:
  703. spin_unlock(&fasync_lock);
  704. spin_unlock(&filp->f_lock);
  705. return fa;
  706. }
  707. /*
  708. * Add a fasync entry. Return negative on error, positive if
  709. * added, and zero if did nothing but change an existing one.
  710. */
  711. static int fasync_add_entry(int fd, struct file *filp, struct fasync_struct **fapp)
  712. {
  713. struct fasync_struct *new;
  714. new = fasync_alloc();
  715. if (!new)
  716. return -ENOMEM;
  717. /*
  718. * fasync_insert_entry() returns the old (update) entry if
  719. * it existed.
  720. *
  721. * So free the (unused) new entry and return 0 to let the
  722. * caller know that we didn't add any new fasync entries.
  723. */
  724. if (fasync_insert_entry(fd, filp, fapp, new)) {
  725. fasync_free(new);
  726. return 0;
  727. }
  728. return 1;
  729. }
  730. /*
  731. * fasync_helper() is used by almost all character device drivers
  732. * to set up the fasync queue, and for regular files by the file
  733. * lease code. It returns negative on error, 0 if it did no changes
  734. * and positive if it added/deleted the entry.
  735. */
  736. int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
  737. {
  738. if (!on)
  739. return fasync_remove_entry(filp, fapp);
  740. return fasync_add_entry(fd, filp, fapp);
  741. }
  742. EXPORT_SYMBOL(fasync_helper);
  743. /*
  744. * rcu_read_lock() is held
  745. */
  746. static void kill_fasync_rcu(struct fasync_struct *fa, int sig, int band)
  747. {
  748. while (fa) {
  749. struct fown_struct *fown;
  750. unsigned long flags;
  751. if (fa->magic != FASYNC_MAGIC) {
  752. printk(KERN_ERR "kill_fasync: bad magic number in "
  753. "fasync_struct!\n");
  754. return;
  755. }
  756. spin_lock_irqsave(&fa->fa_lock, flags);
  757. if (fa->fa_file) {
  758. fown = &fa->fa_file->f_owner;
  759. /* Don't send SIGURG to processes which have not set a
  760. queued signum: SIGURG has its own default signalling
  761. mechanism. */
  762. if (!(sig == SIGURG && fown->signum == 0))
  763. send_sigio(fown, fa->fa_fd, band);
  764. }
  765. spin_unlock_irqrestore(&fa->fa_lock, flags);
  766. fa = rcu_dereference(fa->fa_next);
  767. }
  768. }
  769. void kill_fasync(struct fasync_struct **fp, int sig, int band)
  770. {
  771. /* First a quick test without locking: usually
  772. * the list is empty.
  773. */
  774. if (*fp) {
  775. rcu_read_lock();
  776. kill_fasync_rcu(rcu_dereference(*fp), sig, band);
  777. rcu_read_unlock();
  778. }
  779. }
  780. EXPORT_SYMBOL(kill_fasync);
  781. static int __init fcntl_init(void)
  782. {
  783. /*
  784. * Please add new bits here to ensure allocation uniqueness.
  785. * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY
  786. * is defined as O_NONBLOCK on some platforms and not on others.
  787. */
  788. BUILD_BUG_ON(20 - 1 /* for O_RDONLY being 0 */ != HWEIGHT32(
  789. O_RDONLY | O_WRONLY | O_RDWR |
  790. O_CREAT | O_EXCL | O_NOCTTY |
  791. O_TRUNC | O_APPEND | /* O_NONBLOCK | */
  792. __O_SYNC | O_DSYNC | FASYNC |
  793. O_DIRECT | O_LARGEFILE | O_DIRECTORY |
  794. O_NOFOLLOW | O_NOATIME | O_CLOEXEC |
  795. __FMODE_EXEC | O_PATH | __O_TMPFILE
  796. ));
  797. fasync_cache = kmem_cache_create("fasync_cache",
  798. sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL);
  799. return 0;
  800. }
  801. module_init(fcntl_init)