Changeset 1903


Ignore:
Timestamp:
03/11/2007 18:29:37 (5 years ago)
Author:
chris
Message:

Log exactly which file descriptors have been left open and what they are.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • box/trunk/infrastructure/buildenv-testmain-template.cpp

    r1699 r1903  
    6565 
    6666// any way to check for open file descriptors on Win32? 
    67 inline int  count_filedes()      { return 0;     } 
    68 inline bool checkfilesleftopen() { return false; } 
     67inline bool check_filedes(bool x) { return 0;     } 
     68inline bool checkfilesleftopen()  { return false; } 
    6969 
    7070#else // !WIN32 
    7171 
    72 int count_filedes() 
     72#define FILEDES_MAX 256 
     73 
     74bool filedes_open[FILEDES_MAX]; 
     75 
     76bool check_filedes(bool report) 
    7377{ 
    74         int c = 0; 
     78        bool allOk = true; 
    7579 
    7680        // See how many file descriptors there are with values < 256 
    77         for(int d = 0; d < 256; ++d) 
     81        for(int d = 0; d < FILEDES_MAX; ++d) 
    7882        { 
    7983                if(::fcntl(d, F_GETFD) != -1) 
    8084                { 
    8185                        // File descriptor obviously exists 
    82                         ++c; 
    83                 } 
    84         } 
     86                        if (report && !filedes_open[d]) 
     87                        { 
     88                                struct stat st; 
     89                                if (fstat(d, &st) == 0) 
     90                                { 
     91                                        int m = st.st_mode; 
     92                                        #define flag(x) ((m & x) ? #x " " : "") 
     93                                        BOX_FATAL("File descriptor " << d <<  
     94                                                " left open (type == " << 
     95                                                flag(S_IFIFO) << 
     96                                                flag(S_IFCHR) << 
     97                                                flag(S_IFDIR) << 
     98                                                flag(S_IFBLK) << 
     99                                                flag(S_IFREG) << 
     100                                                flag(S_IFLNK) << 
     101                                                flag(S_IFSOCK) << 
     102                                                flag(S_IFWHT) << " plus " <<                                                    m << ")"); 
     103                                } 
     104                                else 
     105                                { 
     106                                        BOX_FATAL("File descriptor " << d <<  
     107                                                " left open (and stat failed)"); 
     108                                } 
    85109         
    86         return c; 
     110                                allOk = false; 
     111                                 
     112                        } 
     113                        else if (!report) 
     114                        { 
     115                                filedes_open[d] = true; 
     116                        } 
     117                } 
     118                else  
     119                { 
     120                        if (report && filedes_open[d]) 
     121                        { 
     122                                BOX_FATAL("File descriptor " << d <<  
     123                                        " was open, now closed"); 
     124                                allOk = false; 
     125                        } 
     126                        else 
     127                        { 
     128                                filedes_open[d] = false; 
     129                        } 
     130                } 
     131        } 
     132 
     133        if (!report && allOk) 
     134        { 
     135                filedes_open_at_beginning = 0; 
     136        } 
     137         
     138        return !allOk; 
    87139} 
    88140 
     
    93145                // Not used correctly, pretend that there were things  
    94146                // left open so this gets investigated 
     147                BOX_FATAL("File descriptor test was not initialised"); 
    95148                return true; 
    96149        } 
     
    100153 
    101154        // Count the file descriptors open 
    102         return filedes_open_at_beginning != count_filedes(); 
     155        return check_filedes(true); 
    103156} 
    104157 
     
    177230        { 
    178231                // Count open file descriptors for a very crude "files left open" test 
    179                 filedes_open_at_beginning = count_filedes(); 
     232                check_filedes(false); 
    180233 
    181234                // banner 
Note: See TracChangeset for help on using the changeset viewer.