fcntl.c 14 KB

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