4

I know about lsof and ls /proc/*/fd but none of them are atomic AFAIK. Because in the latter case I would need to get all pids for user and then filter by them and by that time some of the file descriptors could be closed.

Maybe there is some system call for that or something, because obviously OS tracks that number as it would refuse to create FD if max limit for user is exhausted.

9
  • 2
    Are you sure that it would refuse to create FD if max limit for user is exhausted? The setrlimit/getrlimit system calls work a per-process base. diskquota works on a per filesystem base. AFAIK, there is no API that works on a per user base. – andcoz Apr 25 '16 at 14:46
  • Well, there is a limit for every user. If it wouldn't refuse to create file when it's exhausted then this limit is useless, right? – user1685095 Apr 25 '16 at 14:53
  • either lsof(with non atomic constraint) or read kernel sources and build your own utility. A good question anyway. – Archemar Apr 25 '16 at 15:03
  • 1
    @user1685095 There is a limit for each process of a specified user not for each user. If a user has an hard limit of RLIMIT_NOFILE set to 100, she'll can have two processes with 99 open files (198 in total). – andcoz Apr 25 '16 at 15:20
  • @andcoz And how much precesses are available for user? – user1685095 Apr 25 '16 at 15:22
1

I haven't made an intensive search, but I don't think what you're looking for exists on Linux. Opening a file descriptor doesn't take any global lock, only a per-process lock, so on a multicore machine whatever you'd be using to count the number of open file descriptors could be running literally at the same time that other threads is opening or closing files on other cores.

Linux doesn't have a global limit on the total number of open files. There's no explicit per-user limit either. There's a per-user limit on processes, and a per-process limit on file descriptor numbers, which indirectly imposes a limit on open files per user, but that isn't explicitly tracked.

Exploring /proc (which is what lsof does under the hood) is as good as it gets. /proc is the Linux API to get information about processes.

5
  • What does /etc/security/limits.conf is doing if there's no limit on open files for user – user1685095 Apr 28 '16 at 11:33
  • @user1685095 It sets limits that are not limits on open files per user. – Gilles 'SO- stop being evil' Apr 28 '16 at 11:43
  • You answering on what it isn't. And I'm asking what it is. – user1685095 Apr 28 '16 at 11:44
  • gerardnico.com/wiki/linux/limits.conf you can see there that there is a limit for open file descriptors. Which is exactly what I'm talking about. – user1685095 Apr 28 '16 at 11:47
  • @user1685095 As several of us have told you already, this is a per process limit, not per user. limits.conf defines different values for each user, but what that means is that each process run by the user has this limit, not that the limit is for the total number of files open by a process run by the user. – Gilles 'SO- stop being evil' Apr 28 '16 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.