fcntl.c 14 KB

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