~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/fs/fcntl.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.