fcntl.c 14 KB

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