fcntl.c 14 KB

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