Hello,

I'm working on a project at school to develop a multimedia system (a la
Windows Media Center) based on FreeBSD. I was looking at some code in
sys/kern/kern_descrip.c to figure out how the fcntl() with F_DUPFD and
dup() differ.

I discovered that kern_fcntl() contains some redundant code. Right
before calling do_dup(), it locks the process and checks that the
minimal file descriptor is lower than the limit for the process. The
do_dup() call does exactly the same check almost at the beginning. This
causes the fcntl() call to call PROC_LOCK() once too much.

The patch below prevents this by performing this check by do_dup(). It
will prevent fcntl() from PROC_LOCK()'ing twice. It also fixes the
return value of fcntl(). The manual page states that it should return
EMFILE when it exceeds its limit, though the actual code sets EINVAL.

%%%
--- kern_descrip.c      Thu Nov  9 13:23:40 2006
+++ kern_descrip.c      Thu Nov  9 13:32:28 2006
@@ -355,7 +355,6 @@
        struct proc *p;
        char *pop;
        struct vnode *vp;
-       u_int newmin;
        int error, flg, tmp;
        int giant_locked;
 
@@ -393,16 +392,7 @@
        case F_DUPFD:
                /* mtx_assert(&Giant, MA_NOTOWNED); */
                FILEDESC_UNLOCK(fdp);
-               newmin = arg;
-               PROC_LOCK(p);
-               if (newmin >= lim_cur(p, RLIMIT_NOFILE) ||
-                   newmin >= maxfilesperproc) {
-                       PROC_UNLOCK(p);
-                       error = EINVAL;
-                       break;
-               }
-               PROC_UNLOCK(p);
-               error = do_dup(td, DUP_VARIABLE, fd, newmin, td->td_retval);
+               error = do_dup(td, DUP_VARIABLE, fd, arg, td->td_retval);
                break;
 
        case F_GETFD:
%%%

-- 
 Ed Schouten <[EMAIL PROTECTED]>
 WWW: http://g-rave.nl/

Attachment: pgpyNumRS83bw.pgp
Description: PGP signature

Reply via email to