Changeset 1233


Ignore:
Timestamp:
Feb 12, 2004, 9:12:42 PM (22 years ago)
Author:
bird
Message:

fixed libc_FH() and libc_FHClose potential trouble when someone increased the max fh number.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/emx/src/lib/sys/filehandles.c

    • Property cvs2svn:cvs-rev changed from 1.4 to 1.5
    r1232 r1233  
    6464static int __libc_fhMoreHandles(unsigned cMin);
    6565static int __libc_fhAllocate(int fh, unsigned fFlags, int cb, PLIBCFHOPS pOps, PLIBCFH *ppFH, int *pfh, int fOwnSem);
     66static PLIBCFH __libc_fh(int fh);
    6667
    6768
     
    630631     * Validate input.
    631632     */
    632     if (    (fh < 0 && fh >= gcFHs)
    633         ||  !gpapFHs[fh])
    634     {
    635         _fmutex_release(&gmtx);
     633    if (!__libc_fh(fh))
     634    {
    636635#ifdef DEBUG
    637636        __asm__("int $3");
    638637#endif
     638        _fmutex_release(&gmtx);
    639639        errno = EBADF;
    640640        return -1;
     
    657657    {
    658658        int rc2;
     659
    659660        /*
    660661         * Close the OS/2 handle and remove the handle from the array
     
    702703 * @returns NULL on failure.
    703704 * @param   fh  Handle to lookup.
     705 * @remark  Must own the mutex.
     706 * @internal
     707 */
     708static PLIBCFH __libc_fh(int fh)
     709{
     710    PLIBCFH pFH = NULL;
     711
     712    /*
     713     * Someone might have opened this externally after
     714     * growing the filehandle range.
     715     */
     716    if (fh >= gcFHs && fh < 0x10000)
     717    {
     718        LONG    lDelta = 0;
     719        ULONG   cCur = 0;
     720        if (!DosSetRelMaxFH(&lDelta, &cCur))
     721            cCur = gcFHs;
     722        if (gcFHs != cCur)
     723            __libc_fhMoreHandles(cCur);
     724    }
     725
     726    /*
     727     * Validate file handle range.
     728     */
     729    if (fh >= 0 && fh < gcFHs)
     730    {
     731        pFH = gpapFHs[fh];
     732        if (!pFH)
     733        {
     734            /*
     735             * Try import the handle.
     736             */
     737            ULONG       rc;
     738            ULONG       fulType;
     739            ULONG       fulDevFlags;
     740            ULONG       fulMode;
     741            unsigned    fLibc;
     742
     743            /*
     744             * Is it in use and if so what kind of handle?
     745             */
     746            if (    (rc = DosQueryHType((HFILE)fh, &fulType, &fulDevFlags)) != NO_ERROR
     747                ||  (rc = DosQueryFHState((HFILE)fh, &fulMode)) != NO_ERROR)
     748            {
     749                errno = EBADF;
     750                return NULL;
     751            }
     752
     753            /*
     754             * Determin initial flags.
     755             */
     756            switch (fulType & 0xff)
     757            {
     758                default: /* paranoia */
     759                case HANDTYPE_FILE:
     760                    fLibc = F_FILE;
     761                    break;
     762                case HANDTYPE_DEVICE:
     763                    fLibc = F_DEV;
     764                    /* @todo inherit O_NDELAY */
     765                    break;
     766                case HANDTYPE_PIPE:
     767                    fLibc = F_PIPE;
     768                    break;
     769            }
     770
     771            /*
     772             * Read write flags.
     773             */
     774            switch (fulMode & (OPEN_ACCESS_READONLY | OPEN_ACCESS_WRITEONLY | OPEN_ACCESS_READWRITE))
     775            {
     776                case OPEN_ACCESS_READONLY:      fLibc |= O_RDONLY; break;
     777                case OPEN_ACCESS_WRITEONLY:     fLibc |= O_WRONLY; break;
     778                default: /* paranoia */
     779                case OPEN_ACCESS_READWRITE:     fLibc |= O_RDWR; break;
     780            }
     781
     782            /*
     783             * Textflag.
     784             */
     785            if (!_fmode_bin)
     786                fLibc |= O_TEXT;
     787
     788
     789            /*
     790             * Allocate a new handle for this filehandle.
     791             */
     792            rc = __libc_fhAllocate(fh, fLibc, sizeof(LIBCFH), NULL, &pFH, NULL, 1);
     793            if (rc)
     794                pFH = NULL;
     795        }
     796    }
     797    else /* out of range: can't possibly be open. */
     798        errno = EBADF;
     799
     800    return pFH;
     801}
     802
     803/**
     804 * Get the LIBC handle structure corresponding to a filehandle.
     805 *
     806 * @returns Pointer to handle structure on success.
     807 * @returns NULL on failure.
     808 * @param   fh  Handle to lookup.
    704809 */
    705810PLIBCFH __libc_FH(int fh)
    706811{
    707     PLIBCFH pFH = NULL;
     812    PLIBCFH pFH;
    708813
    709814    /** @todo shared access */
    710     if (!_fmutex_request(&gmtx, 0))
    711     {
    712         if (fh >= 0 && fh < gcFHs)
    713         {
    714             pFH = gpapFHs[fh];
    715             if (!pFH)
    716             {
    717                 /*
    718                  * Try import the handle.
    719                  */
    720                 ULONG       rc;
    721                 ULONG       fulType;
    722                 ULONG       fulDevFlags;
    723                 ULONG       fulMode;
    724                 unsigned    fLibc;
    725 
    726                 /*
    727                  * Is it in use and if so what kind of handle?
    728                  */
    729                 if (    (rc = DosQueryHType((HFILE)fh, &fulType, &fulDevFlags)) != NO_ERROR
    730                     ||  (rc = DosQueryFHState((HFILE)fh, &fulMode)) != NO_ERROR)
    731                 {
    732                     errno = EBADF;
    733                     _fmutex_release(&gmtx);
    734                     return NULL;
    735                 }
    736 
    737                 /*
    738                  * Determin initial flags.
    739                  */
    740                 switch (fulType & 0xff)
    741                 {
    742                     default: /* paranoia */
    743                     case HANDTYPE_FILE:
    744                         fLibc = F_FILE;
    745                         break;
    746                     case HANDTYPE_DEVICE:
    747                         fLibc = F_DEV;
    748                         /* @todo inherit O_NDELAY */
    749                         break;
    750                     case HANDTYPE_PIPE:
    751                         fLibc = F_PIPE;
    752                         break;
    753                 }
    754 
    755                 /*
    756                  * Read write flags.
    757                  */
    758                 switch (fulMode & (OPEN_ACCESS_READONLY | OPEN_ACCESS_WRITEONLY | OPEN_ACCESS_READWRITE))
    759                 {
    760                     case OPEN_ACCESS_READONLY:      fLibc |= O_RDONLY; break;
    761                     case OPEN_ACCESS_WRITEONLY:     fLibc |= O_WRONLY; break;
    762                     default: /* paranoia */
    763                     case OPEN_ACCESS_READWRITE:     fLibc |= O_RDWR; break;
    764                 }
    765 
    766                 /*
    767                  * Textflag.
    768                  */
    769                 if (!_fmode_bin)
    770                     fLibc |= O_TEXT;
    771 
    772 
    773                 /*
    774                  * Allocate a new handle for this filehandle.
    775                  */
    776                 rc = __libc_fhAllocate(fh, fLibc, sizeof(LIBCFH), NULL, &pFH, NULL, 1);
    777                 if (rc)
    778                     pFH = NULL;
    779             }
    780         }
    781 
    782         _fmutex_release(&gmtx);
    783     }
    784 
     815    if (_fmutex_request(&gmtx, 0))
     816        return NULL;
     817
     818    pFH = __libc_fh(fh);
     819
     820    _fmutex_release(&gmtx);
    785821    return pFH;
    786822}
Note: See TracChangeset for help on using the changeset viewer.