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