fcntl.c 13 KB

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