fcntl.c 14 KB

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