Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

on iOS/iPhone: "Too many open files": need to list open files (like lsof)

Tags:

c

ios

iphone

We've discovered our complex iPhone app (ObjC, C++, JavaScript/WebKit) is leaking file descriptors under unusual circumstances.

I need to know which files (by file path) we are leaving open.

I want something like the BSD command "lsof", which, of course, isn't available in iOS 4, at least not to me. Ideally a C or ObjC function. Or a tool, like shark or Instruments. Just need the files for our running app, not (as with lsof) for all apps/processes.

We do all sorts of things with files, and the code that is failing with "Too many open files" hasn't changed in ages, and since the circumstances are unusual, this could have crept in months ago. So there's no need to remind me to look at code that opens files and make sure I close them. I know that already. Would be nice to narrow it down with something lsof-esque. Thanks.

like image 808
Tim James Avatar asked Nov 03 '10 01:11

Tim James


People also ask

What causes too many open files?

"Too many open files " errors happen when a process needs to open more files than it is allowed by the operating system. This number is controlled by the maximum number of file descriptors the process has. 2. Explicitly set the number of file descriptors using the ulimit command.

How do I choose which app opens a file in IOS?

Drag the file onto the app icon in the Finder or the Dock. Select the file in the Finder, choose File > Open With, then choose an app. Control-click the file, choose Open With, then choose an app. Open the app, then choose File > Open.


1 Answers

#import <sys/types.h>  
#import <fcntl.h>
#import <errno.h>
#import <sys/param.h>

+(void) lsof
{
    int flags;
    int fd;
    char buf[MAXPATHLEN+1] ;
    int n = 1 ;

    for (fd = 0; fd < (int) FD_SETSIZE; fd++) {
        errno = 0;
        flags = fcntl(fd, F_GETFD, 0);
        if (flags == -1 && errno) {
            if (errno != EBADF) {
                return ;
            }
            else
                continue;
        }
        fcntl(fd , F_GETPATH, buf ) ; 
        NSLog( @"File Descriptor %d number %d in use for: %s",fd,n , buf ) ;
        ++n ; 
    }
}
like image 57
Rich Waters Avatar answered Sep 28 '22 18:09

Rich Waters