Checking currently open files on my /Volumes/UNTITLED gives me 2 entries:

$ sudo lsof /Volumes/UNTITLED/
mds      62 root    8r   DIR    1,6    32768    2 /Volumes/UNTITLED
mds      62 root   19r   DIR    1,6    32768    2 /Volumes/UNTITLED

When I run diskutil unmountDisk disk2, all descriptors are successfully closed (e.g. I cannot do lsof /Volumes/UNTITLED/ anymore, since the folder is gone).

What exactly diskutil unmountDisk does to close file descriptors? Can I close file descriptors myself?

As far as I understand diskutil is closed source. Can the question be answered by taking umount?


Basically I'm trying to understand how to tell a process close a given file descriptor, e.g. emulate what unmount does.

I think telling the process to close a file descriptor should be illegal, as generally it can easily cause process to crash. However I think diskutil unmountDisk somehow manages to gently asks mds to no longer possess the file.

I'm curious if I can ask gently too. I know there's a endfsent OS function, but it's not a program, I cannot run it to manipulate file descriptors open by different processes.

  • Just because lsof can't list them anymore by path, doesn't mean they're closed. I'd guess they're still open, but all reads and writes to them return errors. mds may also close them on unmount... – derobert Mar 23 '16 at 17:06
  • Good point @derobert. Do you know if I could look anywhere for those errors, like some system log? – gmile Mar 23 '16 at 17:08
  • They'd be returned to each process directly, and only logged if that process decided to. Not sure if OS X has some method to observe them otherwise (I'm not really that familiar with OS X). – derobert Mar 23 '16 at 17:09
  • I don't know how OSX works, but based on my general understanding of how OSes work, the most plausible way this works is that diskutil talks to mds and either mds does the unmounting or mds closes the volumes and then signals that it's ok to unmount. The theory that the file descriptors in mds are somehow closed from the outside sounds extremely strange. You aren't looking for a way to get a program to close a file descriptor, you're looking for the way mds learns about unmount requests. – Gilles Mar 23 '16 at 22:40

Your Answer

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

Browse other questions tagged or ask your own question.