fcntl.c 13 KB

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