1
2
3
4
5
6
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
18#include <asm/poll.h>
19#include <asm/siginfo.h>
20#include <asm/uaccess.h>
21
22void set_close_on_exec(unsigned int fd, int flag)
23{
24 struct files_struct *files = current->files;
25 spin_lock(&files->file_lock);
26 if (flag)
27 FD_SET(fd, files->close_on_exec);
28 else
29 FD_CLR(fd, files->close_on_exec);
30 spin_unlock(&files->file_lock);
31}
32
33static inline int get_close_on_exec(unsigned int fd)
34{
35 struct files_struct *files = current->files;
36 int res;
37 spin_lock(&files->file_lock);
38 res = FD_ISSET(fd, files->close_on_exec);
39 spin_unlock(&files->file_lock);
40 return res;
41}
42
43
44
45
46
47
48
49static int expand_files(struct files_struct *files, int nr)
50{
51 int err, expand = 0;
52#ifdef FDSET_DEBUG
53 printk (KERN_ERR "%s %d: nr = %d\n", __FUNCTION__, current->pid, nr);
54#endif
55
56 if (nr >= files->max_fdset) {
57 expand = 1;
58 if ((err = expand_fdset(files, nr)))
59 goto out;
60 }
61 if (nr >= files->max_fds) {
62 expand = 1;
63 if ((err = expand_fd_array(files, nr)))
64 goto out;
65 }
66 err = expand;
67 out:
68#ifdef FDSET_DEBUG
69 if (err)
70 printk (KERN_ERR "%s %d: return %d\n", __FUNCTION__, current->pid, err);
71#endif
72 return err;
73}
74
75
76
77
78
79
80
81static int locate_fd(struct files_struct *files,
82 struct file *file, unsigned int orig_start)
83{
84 unsigned int newfd;
85 unsigned int start;
86 int error;
87
88 error = -EINVAL;
89 if (orig_start >= current->rlim[RLIMIT_NOFILE].rlim_cur)
90 goto out;
91
92repeat:
93
94
95
96
97 start = orig_start;
98 if (start < files->next_fd)
99 start = files->next_fd;
100
101 newfd = start;
102 if (start < files->max_fdset) {
103 newfd = find_next_zero_bit(files->open_fds->fds_bits,
104 files->max_fdset, start);
105 }
106
107 error = -EMFILE;
108 if (newfd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
109 goto out;
110
111 error = expand_files(files, newfd);
112 if (error < 0)
113 goto out;
114
115
116
117
118
119 if (error)
120 goto repeat;
121
122 if (start <= files->next_fd)
123 files->next_fd = newfd + 1;
124
125 error = newfd;
126
127out:
128 return error;
129}
130
131static int dupfd(struct file *file, unsigned int start)
132{
133 struct files_struct * files = current->files;
134 int fd;
135
136 spin_lock(&files->file_lock);
137 fd = locate_fd(files, file, start);
138 if (fd >= 0) {
139 FD_SET(fd, files->open_fds);
140 FD_CLR(fd, files->close_on_exec);
141 spin_unlock(&files->file_lock);
142 fd_install(fd, file);
143 } else {
144 spin_unlock(&files->file_lock);
145 fput(file);
146 }
147
148 return fd;
149}
150
151asmlinkage long sys_dup2(unsigned int oldfd, unsigned int newfd)
152{
153 int err = -EBADF;
154 struct file * file, *tofree;
155 struct files_struct * files = current->files;
156
157 spin_lock(&files->file_lock);
158 if (!(file = fcheck(oldfd)))
159 goto out_unlock;
160 err = newfd;
161 if (newfd == oldfd)
162 goto out_unlock;
163 err = -EBADF;
164 if (newfd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
165 goto out_unlock;
166 get_file(file);
167
168 err = expand_files(files, newfd);
169 if (err < 0)
170 goto out_fput;
171
172
173
174
175
176
177
178
179
180 err = -EBUSY;
181 tofree = files->fd[newfd];
182 if (!tofree && FD_ISSET(newfd, files->open_fds))
183 goto out_fput;
184
185 files->fd[newfd] = file;
186 FD_SET(newfd, files->open_fds);
187 FD_CLR(newfd, files->close_on_exec);
188 spin_unlock(&files->file_lock);
189
190 if (tofree)
191 filp_close(tofree, files);
192 err = newfd;
193out:
194 return err;
195out_unlock:
196 spin_unlock(&files->file_lock);
197 goto out;
198
199out_fput:
200 spin_unlock(&files->file_lock);
201 fput(file);
202 goto out;
203}
204
205asmlinkage long sys_dup(unsigned int fildes)
206{
207 int ret = -EBADF;
208 struct file * file = fget(fildes);
209
210 if (file)
211 ret = dupfd(file, 0);
212 return ret;
213}
214
215#define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | FASYNC | O_DIRECT)
216
217static int setfl(int fd, struct file * filp, unsigned long arg)
218{
219 struct inode * inode = filp->f_dentry->d_inode;
220 int error = 0;
221
222
223 if (!(arg & O_APPEND) && IS_APPEND(inode))
224 return -EPERM;
225
226
227 if (O_NONBLOCK != O_NDELAY)
228 if (arg & O_NDELAY)
229 arg |= O_NONBLOCK;
230
231 if (arg & O_DIRECT) {
232 if (!inode->i_mapping || !inode->i_mapping->a_ops ||
233 !inode->i_mapping->a_ops->direct_IO)
234 return -EINVAL;
235 }
236
237 lock_kernel();
238 if ((arg ^ filp->f_flags) & FASYNC) {
239 if (filp->f_op && filp->f_op->fasync) {
240 error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0);
241 if (error < 0)
242 goto out;
243 }
244 }
245
246 filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
247 out:
248 unlock_kernel();
249 return error;
250}
251
252static void f_modown(struct file *filp, unsigned long pid,
253 uid_t uid, uid_t euid, int force)
254{
255 write_lock_irq(&filp->f_owner.lock);
256 if (force || !filp->f_owner.pid) {
257 filp->f_owner.pid = pid;
258 filp->f_owner.uid = uid;
259 filp->f_owner.euid = euid;
260 }
261 write_unlock_irq(&filp->f_owner.lock);
262}
263
264int f_setown(struct file *filp, unsigned long arg, int force)
265{
266 int err;
267
268 err = security_file_set_fowner(filp);
269 if (err)
270 return err;
271
272 f_modown(filp, arg, current->uid, current->euid, force);
273 return 0;
274}
275
276EXPORT_SYMBOL(f_setown);
277
278void f_delown(struct file *filp)
279{
280 f_modown(filp, 0, 0, 0, 1);
281}
282
283EXPORT_SYMBOL(f_delown);
284
285static long do_fcntl(unsigned int fd, unsigned int cmd,
286 unsigned long arg, struct file * filp)
287{
288 long err = -EINVAL;
289
290 switch (cmd) {
291 case F_DUPFD:
292 get_file(filp);
293 err = dupfd(filp, arg);
294 break;
295 case F_GETFD:
296 err = get_close_on_exec(fd);
297 break;
298 case F_SETFD:
299 err = 0;
300 set_close_on_exec(fd, arg&1);
301 break;
302 case F_GETFL:
303 err = filp->f_flags;
304 break;
305 case F_SETFL:
306 err = setfl(fd, filp, arg);
307 break;
308 case F_GETLK:
309 err = fcntl_getlk(filp, (struct flock __user *) arg);
310 break;
311 case F_SETLK:
312 case F_SETLKW:
313 err = fcntl_setlk(filp, cmd, (struct flock __user *) arg);
314 break;
315 case F_GETOWN:
316
317
318
319
320
321
322
323 err = filp->f_owner.pid;
324 force_successful_syscall_return();
325 break;
326 case F_SETOWN:
327 err = f_setown(filp, arg, 1);
328 break;
329 case F_GETSIG:
330 err = filp->f_owner.signum;
331 break;
332 case F_SETSIG:
333
334 if (arg < 0 || arg > _NSIG) {
335 break;
336 }
337 err = 0;
338 filp->f_owner.signum = arg;
339 break;
340 case F_GETLEASE:
341 err = fcntl_getlease(filp);
342 break;
343 case F_SETLEASE:
344 err = fcntl_setlease(fd, filp, arg);
345 break;
346 case F_NOTIFY:
347 err = fcntl_dirnotify(fd, filp, arg);
348 break;
349 default:
350 break;
351 }
352
353 return err;
354}
355
356asmlinkage long sys_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg)
357{
358 struct file * filp;
359 long err = -EBADF;
360
361 filp = fget(fd);
362 if (!filp)
363 goto out;
364
365 err = security_file_fcntl(filp, cmd, arg);
366 if (err) {
367 fput(filp);
368 return err;
369 }
370
371 err = do_fcntl(fd, cmd, arg, filp);
372
373 fput(filp);
374out:
375 return err;
376}
377
378#if BITS_PER_LONG == 32
379asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
380{
381 struct file * filp;
382 long err;
383
384 err = -EBADF;
385 filp = fget(fd);
386 if (!filp)
387 goto out;
388
389 err = security_file_fcntl(filp, cmd, arg);
390 if (err) {
391 fput(filp);
392 return err;
393 }
394 err = -EBADF;
395
396 switch (cmd) {
397 case F_GETLK64:
398 err = fcntl_getlk64(filp, (struct flock64 __user *) arg);
399 break;
400 case F_SETLK64:
401 case F_SETLKW64:
402 err = fcntl_setlk64(filp, cmd, (struct flock64 __user *) arg);
403 break;
404 default:
405 err = do_fcntl(fd, cmd, arg, filp);
406 break;
407 }
408 fput(filp);
409out:
410 return err;
411}
412#endif
413
414
415
416static long band_table[NSIGPOLL] = {
417 POLLIN | POLLRDNORM,
418 POLLOUT | POLLWRNORM | POLLWRBAND,
419 POLLIN | POLLRDNORM | POLLMSG,
420 POLLERR,
421 POLLPRI | POLLRDBAND,
422 POLLHUP | POLLERR
423};
424
425static inline int sigio_perm(struct task_struct *p,
426 struct fown_struct *fown)
427{
428 return ((fown->euid == 0) ||
429 (fown->euid == p->suid) || (fown->euid == p->uid) ||
430 (fown->uid == p->suid) || (fown->uid == p->uid));
431}
432
433static void send_sigio_to_task(struct task_struct *p,
434 struct fown_struct *fown,
435 int fd,
436 int reason)
437{
438 if (!sigio_perm(p, fown))
439 return;
440
441 if (security_file_send_sigiotask(p, fown, fd, reason))
442 return;
443
444 switch (fown->signum) {
445 siginfo_t si;
446 default:
447
448
449
450
451
452
453 si.si_signo = fown->signum;
454 si.si_errno = 0;
455 si.si_code = reason;
456
457
458
459 if ((reason & __SI_MASK) != __SI_POLL)
460 BUG();
461 if (reason - POLL_IN >= NSIGPOLL)
462 si.si_band = ~0L;
463 else
464 si.si_band = band_table[reason - POLL_IN];
465 si.si_fd = fd;
466 if (!send_sig_info(fown->signum, &si, p))
467 break;
468
469 case 0:
470 send_group_sig_info(SIGIO, SEND_SIG_PRIV, p);
471 }
472}
473
474void send_sigio(struct fown_struct *fown, int fd, int band)
475{
476 struct task_struct *p;
477 int pid;
478
479 read_lock(&fown->lock);
480 pid = fown->pid;
481 if (!pid)
482 goto out_unlock_fown;
483
484 read_lock(&tasklist_lock);
485 if (pid > 0) {
486 p = find_task_by_pid(pid);
487 if (p) {
488 send_sigio_to_task(p, fown, fd, band);
489 }
490 } else {
491 struct list_head *l;
492 struct pid *pidptr;
493 for_each_task_pid(-pid, PIDTYPE_PGID, p, l, pidptr) {
494 send_sigio_to_task(p, fown, fd, band);
495 }
496 }
497 read_unlock(&tasklist_lock);
498 out_unlock_fown:
499 read_unlock(&fown->lock);
500}
501
502static void send_sigurg_to_task(struct task_struct *p,
503 struct fown_struct *fown)
504{
505 if (sigio_perm(p, fown))
506 send_group_sig_info(SIGURG, SEND_SIG_PRIV, p);
507}
508
509int send_sigurg(struct fown_struct *fown)
510{
511 struct task_struct *p;
512 int pid, ret = 0;
513
514 read_lock(&fown->lock);
515 pid = fown->pid;
516 if (!pid)
517 goto out_unlock_fown;
518
519 ret = 1;
520
521 read_lock(&tasklist_lock);
522 if (pid > 0) {
523 p = find_task_by_pid(pid);
524 if (p) {
525 send_sigurg_to_task(p, fown);
526 }
527 } else {
528 struct list_head *l;
529 struct pid *pidptr;
530 for_each_task_pid(-pid, PIDTYPE_PGID, p, l, pidptr) {
531 send_sigurg_to_task(p, fown);
532 }
533 }
534 read_unlock(&tasklist_lock);
535 out_unlock_fown:
536 read_unlock(&fown->lock);
537 return ret;
538}
539
540static rwlock_t fasync_lock = RW_LOCK_UNLOCKED;
541static kmem_cache_t *fasync_cache;
542
543
544
545
546
547
548int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
549{
550 struct fasync_struct *fa, **fp;
551 struct fasync_struct *new = NULL;
552 int result = 0;
553
554 if (on) {
555 new = kmem_cache_alloc(fasync_cache, SLAB_KERNEL);
556 if (!new)
557 return -ENOMEM;
558 }
559 write_lock_irq(&fasync_lock);
560 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
561 if (fa->fa_file == filp) {
562 if(on) {
563 fa->fa_fd = fd;
564 kmem_cache_free(fasync_cache, new);
565 } else {
566 *fp = fa->fa_next;
567 kmem_cache_free(fasync_cache, fa);
568 result = 1;
569 }
570 goto out;
571 }
572 }
573
574 if (on) {
575 new->magic = FASYNC_MAGIC;
576 new->fa_file = filp;
577 new->fa_fd = fd;
578 new->fa_next = *fapp;
579 *fapp = new;
580 result = 1;
581 }
582out:
583 write_unlock_irq(&fasync_lock);
584 return result;
585}
586
587EXPORT_SYMBOL(fasync_helper);
588
589void __kill_fasync(struct fasync_struct *fa, int sig, int band)
590{
591 while (fa) {
592 struct fown_struct * fown;
593 if (fa->magic != FASYNC_MAGIC) {
594 printk(KERN_ERR "kill_fasync: bad magic number in "
595 "fasync_struct!\n");
596 return;
597 }
598 fown = &fa->fa_file->f_owner;
599
600
601
602 if (!(sig == SIGURG && fown->signum == 0))
603 send_sigio(fown, fa->fa_fd, band);
604 fa = fa->fa_next;
605 }
606}
607
608EXPORT_SYMBOL(__kill_fasync);
609
610void kill_fasync(struct fasync_struct **fp, int sig, int band)
611{
612 read_lock(&fasync_lock);
613 __kill_fasync(*fp, sig, band);
614 read_unlock(&fasync_lock);
615}
616
617EXPORT_SYMBOL(kill_fasync);
618
619static int __init fasync_init(void)
620{
621 fasync_cache = kmem_cache_create("fasync_cache",
622 sizeof(struct fasync_struct), 0, 0, NULL, NULL);
623 if (!fasync_cache)
624 panic("cannot create fasync slab cache");
625 return 0;
626}
627
628module_init(fasync_init)
629