fcntl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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/security.h>
  17. #include <linux/ptrace.h>
  18. #include <linux/signal.h>
  19. #include <linux/rcupdate.h>
  20. #include <linux/pid_namespace.h>
  21. #include <asm/poll.h>
  22. #include <asm/siginfo.h>
  23. #include <asm/uaccess.h>
  24. void set_close_on_exec(unsigned int fd, int flag)
  25. {
  26. struct files_struct *files = current->files;
  27. struct fdtable *fdt;
  28. spin_lock(&files->file_lock);
  29. fdt = files_fdtable(files);
  30. if (flag)
  31. FD_SET(fd, fdt->close_on_exec);
  32. else
  33. FD_CLR(fd, fdt->close_on_exec);
  34. spin_unlock(&files->file_lock);
  35. }
  36. static int get_close_on_exec(unsigned int fd)
  37. {
  38. struct files_struct *files = current->files;
  39. struct fdtable *fdt;
  40. int res;
  41. rcu_read_lock();
  42. fdt = files_fdtable(files);
  43. res = FD_ISSET(fd, fdt->close_on_exec);
  44. rcu_read_unlock();
  45. return res;
  46. }
  47. /*
  48. * locate_fd finds a free file descriptor in the open_fds fdset,
  49. * expanding the fd arrays if necessary. Must be called with the
  50. * file_lock held for write.
  51. */
  52. static int locate_fd(unsigned int orig_start, int cloexec)
  53. {
  54. struct files_struct *files = current->files;
  55. unsigned int newfd;
  56. unsigned int start;
  57. int error;
  58. struct fdtable *fdt;
  59. spin_lock(&files->file_lock);
  60. error = -EINVAL;
  61. if (orig_start >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
  62. goto out;
  63. repeat:
  64. fdt = files_fdtable(files);
  65. /*
  66. * Someone might have closed fd's in the range
  67. * orig_start..fdt->next_fd
  68. */
  69. start = orig_start;
  70. if (start < files->next_fd)
  71. start = files->next_fd;
  72. newfd = start;
  73. if (start < fdt->max_fds)
  74. newfd = find_next_zero_bit(fdt->open_fds->fds_bits,
  75. fdt->max_fds, start);
  76. error = -EMFILE;
  77. if (newfd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
  78. goto out;
  79. error = expand_files(files, newfd);
  80. if (error < 0)
  81. goto out;
  82. /*
  83. * If we needed to expand the fs array we
  84. * might have blocked - try again.
  85. */
  86. if (error)
  87. goto repeat;
  88. if (start <= files->next_fd)
  89. files->next_fd = newfd + 1;
  90. FD_SET(newfd, fdt->open_fds);
  91. if (cloexec)
  92. FD_SET(newfd, fdt->close_on_exec);
  93. else
  94. FD_CLR(newfd, fdt->close_on_exec);
  95. error = newfd;
  96. out:
  97. spin_unlock(&files->file_lock);
  98. return error;
  99. }
  100. static int dupfd(struct file *file, unsigned int start, int cloexec)
  101. {
  102. int fd = locate_fd(start, cloexec);
  103. if (fd >= 0)
  104. fd_install(fd, file);
  105. else
  106. fput(file);
  107. return fd;
  108. }
  109. asmlinkage long sys_dup3(unsigned int oldfd, unsigned int newfd, int flags)
  110. {
  111. int err = -EBADF;
  112. struct file * file, *tofree;
  113. struct files_struct * files = current->files;
  114. struct fdtable *fdt;
  115. if ((flags & ~O_CLOEXEC) != 0)
  116. return -EINVAL;
  117. if (unlikely(oldfd == newfd))
  118. return -EINVAL;
  119. spin_lock(&files->file_lock);
  120. if (!(file = fcheck(oldfd)))
  121. goto out_unlock;
  122. if (newfd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
  123. goto out_unlock;
  124. get_file(file); /* We are now finished with oldfd */
  125. err = expand_files(files, newfd);
  126. if (err < 0)
  127. goto out_fput;
  128. /* To avoid races with open() and dup(), we will mark the fd as
  129. * in-use in the open-file bitmap throughout the entire dup2()
  130. * process. This is quite safe: do_close() uses the fd array
  131. * entry, not the bitmap, to decide what work needs to be
  132. * done. --sct */
  133. /* Doesn't work. open() might be there first. --AV */
  134. /* Yes. It's a race. In user space. Nothing sane to do */
  135. err = -EBUSY;
  136. fdt = files_fdtable(files);
  137. tofree = fdt->fd[newfd];
  138. if (!tofree && FD_ISSET(newfd, fdt->open_fds))
  139. goto out_fput;
  140. rcu_assign_pointer(fdt->fd[newfd], file);
  141. FD_SET(newfd, fdt->open_fds);
  142. if (flags & O_CLOEXEC)
  143. FD_SET(newfd, fdt->close_on_exec);
  144. else
  145. FD_CLR(newfd, fdt->close_on_exec);
  146. spin_unlock(&files->file_lock);
  147. if (tofree)
  148. filp_close(tofree, files);
  149. err = newfd;
  150. out:
  151. return err;
  152. out_unlock:
  153. spin_unlock(&files->file_lock);
  154. goto out;
  155. out_fput:
  156. spin_unlock(&files->file_lock);
  157. fput(file);
  158. goto out;
  159. }
  160. asmlinkage long sys_dup2(unsigned int oldfd, unsigned int newfd)
  161. {
  162. if (unlikely(newfd == oldfd)) { /* corner case */
  163. struct files_struct *files = current->files;
  164. rcu_read_lock();
  165. if (!fcheck_files(files, oldfd))
  166. oldfd = -EBADF;
  167. rcu_read_unlock();
  168. return oldfd;
  169. }
  170. return sys_dup3(oldfd, newfd, 0);
  171. }
  172. asmlinkage long sys_dup(unsigned int fildes)
  173. {
  174. int ret = -EBADF;
  175. struct file * file = fget(fildes);
  176. if (file)
  177. ret = dupfd(file, 0, 0);
  178. return ret;
  179. }
  180. #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | FASYNC | O_DIRECT | O_NOATIME)
  181. static int setfl(int fd, struct file * filp, unsigned long arg)
  182. {
  183. struct inode * inode = filp->f_path.dentry->d_inode;
  184. int error = 0;
  185. /*
  186. * O_APPEND cannot be cleared if the file is marked as append-only
  187. * and the file is open for write.
  188. */
  189. if (((arg ^ filp->f_flags) & O_APPEND) && IS_APPEND(inode))
  190. return -EPERM;
  191. /* O_NOATIME can only be set by the owner or superuser */
  192. if ((arg & O_NOATIME) && !(filp->f_flags & O_NOATIME))
  193. if (!is_owner_or_cap(inode))
  194. return -EPERM;
  195. /* required for strict SunOS emulation */
  196. if (O_NONBLOCK != O_NDELAY)
  197. if (arg & O_NDELAY)
  198. arg |= O_NONBLOCK;
  199. if (arg & O_DIRECT) {
  200. if (!filp->f_mapping || !filp->f_mapping->a_ops ||
  201. !filp->f_mapping->a_ops->direct_IO)
  202. return -EINVAL;
  203. }
  204. if (filp->f_op && filp->f_op->check_flags)
  205. error = filp->f_op->check_flags(arg);
  206. if (error)
  207. return error;
  208. if ((arg ^ filp->f_flags) & FASYNC) {
  209. if (filp->f_op && filp->f_op->fasync) {
  210. error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0);
  211. if (error < 0)
  212. goto out;
  213. }
  214. }
  215. filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
  216. out:
  217. return error;
  218. }
  219. static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
  220. uid_t uid, uid_t euid, int force)
  221. {
  222. write_lock_irq(&filp->f_owner.lock);
  223. if (force || !filp->f_owner.pid) {
  224. put_pid(filp->f_owner.pid);
  225. filp->f_owner.pid = get_pid(pid);
  226. filp->f_owner.pid_type = type;
  227. filp->f_owner.uid = uid;
  228. filp->f_owner.euid = euid;
  229. }
  230. write_unlock_irq(&filp->f_owner.lock);
  231. }
  232. int __f_setown(struct file *filp, struct pid *pid, enum pid_type type,
  233. int force)
  234. {
  235. int err;
  236. err = security_file_set_fowner(filp);
  237. if (err)
  238. return err;
  239. f_modown(filp, pid, type, current->uid, current->euid, force);
  240. return 0;
  241. }
  242. EXPORT_SYMBOL(__f_setown);
  243. int f_setown(struct file *filp, unsigned long arg, int force)
  244. {
  245. enum pid_type type;
  246. struct pid *pid;
  247. int who = arg;
  248. int result;
  249. type = PIDTYPE_PID;
  250. if (who < 0) {
  251. type = PIDTYPE_PGID;
  252. who = -who;
  253. }
  254. rcu_read_lock();
  255. pid = find_vpid(who);
  256. result = __f_setown(filp, pid, type, force);
  257. rcu_read_unlock();
  258. return result;
  259. }
  260. EXPORT_SYMBOL(f_setown);
  261. void f_delown(struct file *filp)
  262. {
  263. f_modown(filp, NULL, PIDTYPE_PID, 0, 0, 1);
  264. }
  265. pid_t f_getown(struct file *filp)
  266. {
  267. pid_t pid;
  268. read_lock(&filp->f_owner.lock);
  269. pid = pid_vnr(filp->f_owner.pid);
  270. if (filp->f_owner.pid_type == PIDTYPE_PGID)
  271. pid = -pid;
  272. read_unlock(&filp->f_owner.lock);
  273. return pid;
  274. }
  275. static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
  276. struct file *filp)
  277. {
  278. long err = -EINVAL;
  279. switch (cmd) {
  280. case F_DUPFD:
  281. case F_DUPFD_CLOEXEC:
  282. get_file(filp);
  283. err = dupfd(filp, arg, cmd == F_DUPFD_CLOEXEC);
  284. break;
  285. case F_GETFD:
  286. err = get_close_on_exec(fd) ? FD_CLOEXEC : 0;
  287. break;
  288. case F_SETFD:
  289. err = 0;
  290. set_close_on_exec(fd, arg & FD_CLOEXEC);
  291. break;
  292. case F_GETFL:
  293. err = filp->f_flags;
  294. break;
  295. case F_SETFL:
  296. err = setfl(fd, filp, arg);
  297. break;
  298. case F_GETLK:
  299. err = fcntl_getlk(filp, (struct flock __user *) arg);
  300. break;
  301. case F_SETLK:
  302. case F_SETLKW:
  303. err = fcntl_setlk(fd, filp, cmd, (struct flock __user *) arg);
  304. break;
  305. case F_GETOWN:
  306. /*
  307. * XXX If f_owner is a process group, the
  308. * negative return value will get converted
  309. * into an error. Oops. If we keep the
  310. * current syscall conventions, the only way
  311. * to fix this will be in libc.
  312. */
  313. err = f_getown(filp);
  314. force_successful_syscall_return();
  315. break;
  316. case F_SETOWN:
  317. err = f_setown(filp, arg, 1);
  318. break;
  319. case F_GETSIG:
  320. err = filp->f_owner.signum;
  321. break;
  322. case F_SETSIG:
  323. /* arg == 0 restores default behaviour. */
  324. if (!valid_signal(arg)) {
  325. break;
  326. }
  327. err = 0;
  328. filp->f_owner.signum = arg;
  329. break;
  330. case F_GETLEASE:
  331. err = fcntl_getlease(filp);
  332. break;
  333. case F_SETLEASE:
  334. err = fcntl_setlease(fd, filp, arg);
  335. break;
  336. case F_NOTIFY:
  337. err = fcntl_dirnotify(fd, filp, arg);
  338. break;
  339. default:
  340. break;
  341. }
  342. return err;
  343. }
  344. asmlinkage long sys_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg)
  345. {
  346. struct file *filp;
  347. long err = -EBADF;
  348. filp = fget(fd);
  349. if (!filp)
  350. goto out;
  351. err = security_file_fcntl(filp, cmd, arg);
  352. if (err) {
  353. fput(filp);
  354. return err;
  355. }
  356. err = do_fcntl(fd, cmd, arg, filp);
  357. fput(filp);
  358. out:
  359. return err;
  360. }
  361. #if BITS_PER_LONG == 32
  362. asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
  363. {
  364. struct file * filp;
  365. long err;
  366. err = -EBADF;
  367. filp = fget(fd);
  368. if (!filp)
  369. goto out;
  370. err = security_file_fcntl(filp, cmd, arg);
  371. if (err) {
  372. fput(filp);
  373. return err;
  374. }
  375. err = -EBADF;
  376. switch (cmd) {
  377. case F_GETLK64:
  378. err = fcntl_getlk64(filp, (struct flock64 __user *) arg);
  379. break;
  380. case F_SETLK64:
  381. case F_SETLKW64:
  382. err = fcntl_setlk64(fd, filp, cmd,
  383. (struct flock64 __user *) arg);
  384. break;
  385. default:
  386. err = do_fcntl(fd, cmd, arg, filp);
  387. break;
  388. }
  389. fput(filp);
  390. out:
  391. return err;
  392. }
  393. #endif
  394. /* Table to convert sigio signal codes into poll band bitmaps */
  395. static const long band_table[NSIGPOLL] = {
  396. POLLIN | POLLRDNORM, /* POLL_IN */
  397. POLLOUT | POLLWRNORM | POLLWRBAND, /* POLL_OUT */
  398. POLLIN | POLLRDNORM | POLLMSG, /* POLL_MSG */
  399. POLLERR, /* POLL_ERR */
  400. POLLPRI | POLLRDBAND, /* POLL_PRI */
  401. POLLHUP | POLLERR /* POLL_HUP */
  402. };
  403. static inline int sigio_perm(struct task_struct *p,
  404. struct fown_struct *fown, int sig)
  405. {
  406. return (((fown->euid == 0) ||
  407. (fown->euid == p->suid) || (fown->euid == p->uid) ||
  408. (fown->uid == p->suid) || (fown->uid == p->uid)) &&
  409. !security_file_send_sigiotask(p, fown, sig));
  410. }
  411. static void send_sigio_to_task(struct task_struct *p,
  412. struct fown_struct *fown,
  413. int fd,
  414. int reason)
  415. {
  416. if (!sigio_perm(p, fown, fown->signum))
  417. return;
  418. switch (fown->signum) {
  419. siginfo_t si;
  420. default:
  421. /* Queue a rt signal with the appropriate fd as its
  422. value. We use SI_SIGIO as the source, not
  423. SI_KERNEL, since kernel signals always get
  424. delivered even if we can't queue. Failure to
  425. queue in this case _should_ be reported; we fall
  426. back to SIGIO in that case. --sct */
  427. si.si_signo = fown->signum;
  428. si.si_errno = 0;
  429. si.si_code = reason;
  430. /* Make sure we are called with one of the POLL_*
  431. reasons, otherwise we could leak kernel stack into
  432. userspace. */
  433. BUG_ON((reason & __SI_MASK) != __SI_POLL);
  434. if (reason - POLL_IN >= NSIGPOLL)
  435. si.si_band = ~0L;
  436. else
  437. si.si_band = band_table[reason - POLL_IN];
  438. si.si_fd = fd;
  439. if (!group_send_sig_info(fown->signum, &si, p))
  440. break;
  441. /* fall-through: fall back on the old plain SIGIO signal */
  442. case 0:
  443. group_send_sig_info(SIGIO, SEND_SIG_PRIV, p);
  444. }
  445. }
  446. void send_sigio(struct fown_struct *fown, int fd, int band)
  447. {
  448. struct task_struct *p;
  449. enum pid_type type;
  450. struct pid *pid;
  451. read_lock(&fown->lock);
  452. type = fown->pid_type;
  453. pid = fown->pid;
  454. if (!pid)
  455. goto out_unlock_fown;
  456. read_lock(&tasklist_lock);
  457. do_each_pid_task(pid, type, p) {
  458. send_sigio_to_task(p, fown, fd, band);
  459. } while_each_pid_task(pid, type, p);
  460. read_unlock(&tasklist_lock);
  461. out_unlock_fown:
  462. read_unlock(&fown->lock);
  463. }
  464. static void send_sigurg_to_task(struct task_struct *p,
  465. struct fown_struct *fown)
  466. {
  467. if (sigio_perm(p, fown, SIGURG))
  468. group_send_sig_info(SIGURG, SEND_SIG_PRIV, p);
  469. }
  470. int send_sigurg(struct fown_struct *fown)
  471. {
  472. struct task_struct *p;
  473. enum pid_type type;
  474. struct pid *pid;
  475. int ret = 0;
  476. read_lock(&fown->lock);
  477. type = fown->pid_type;
  478. pid = fown->pid;
  479. if (!pid)
  480. goto out_unlock_fown;
  481. ret = 1;
  482. read_lock(&tasklist_lock);
  483. do_each_pid_task(pid, type, p) {
  484. send_sigurg_to_task(p, fown);
  485. } while_each_pid_task(pid, type, p);
  486. read_unlock(&tasklist_lock);
  487. out_unlock_fown:
  488. read_unlock(&fown->lock);
  489. return ret;
  490. }
  491. static DEFINE_RWLOCK(fasync_lock);
  492. static struct kmem_cache *fasync_cache __read_mostly;
  493. /*
  494. * fasync_helper() is used by some character device drivers (mainly mice)
  495. * to set up the fasync queue. It returns negative on error, 0 if it did
  496. * no changes and positive if it added/deleted the entry.
  497. */
  498. int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
  499. {
  500. struct fasync_struct *fa, **fp;
  501. struct fasync_struct *new = NULL;
  502. int result = 0;
  503. if (on) {
  504. new = kmem_cache_alloc(fasync_cache, GFP_KERNEL);
  505. if (!new)
  506. return -ENOMEM;
  507. }
  508. write_lock_irq(&fasync_lock);
  509. for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
  510. if (fa->fa_file == filp) {
  511. if(on) {
  512. fa->fa_fd = fd;
  513. kmem_cache_free(fasync_cache, new);
  514. } else {
  515. *fp = fa->fa_next;
  516. kmem_cache_free(fasync_cache, fa);
  517. result = 1;
  518. }
  519. goto out;
  520. }
  521. }
  522. if (on) {
  523. new->magic = FASYNC_MAGIC;
  524. new->fa_file = filp;
  525. new->fa_fd = fd;
  526. new->fa_next = *fapp;
  527. *fapp = new;
  528. result = 1;
  529. }
  530. out:
  531. write_unlock_irq(&fasync_lock);
  532. return result;
  533. }
  534. EXPORT_SYMBOL(fasync_helper);
  535. void __kill_fasync(struct fasync_struct *fa, int sig, int band)
  536. {
  537. while (fa) {
  538. struct fown_struct * fown;
  539. if (fa->magic != FASYNC_MAGIC) {
  540. printk(KERN_ERR "kill_fasync: bad magic number in "
  541. "fasync_struct!\n");
  542. return;
  543. }
  544. fown = &fa->fa_file->f_owner;
  545. /* Don't send SIGURG to processes which have not set a
  546. queued signum: SIGURG has its own default signalling
  547. mechanism. */
  548. if (!(sig == SIGURG && fown->signum == 0))
  549. send_sigio(fown, fa->fa_fd, band);
  550. fa = fa->fa_next;
  551. }
  552. }
  553. EXPORT_SYMBOL(__kill_fasync);
  554. void kill_fasync(struct fasync_struct **fp, int sig, int band)
  555. {
  556. /* First a quick test without locking: usually
  557. * the list is empty.
  558. */
  559. if (*fp) {
  560. read_lock(&fasync_lock);
  561. /* reread *fp after obtaining the lock */
  562. __kill_fasync(*fp, sig, band);
  563. read_unlock(&fasync_lock);
  564. }
  565. }
  566. EXPORT_SYMBOL(kill_fasync);
  567. static int __init fasync_init(void)
  568. {
  569. fasync_cache = kmem_cache_create("fasync_cache",
  570. sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL);
  571. return 0;
  572. }
  573. module_init(fasync_init)