Browse thread
Closing all open file descriptors
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: | 2007-09-15 (15:57) |
From: | Eric Cooper <ecc@c...> |
Subject: | Re: [Caml-list] Closing all open file descriptors |
I think the right interface for this should be a "fold" over open file descriptors: open Unix let descr_of_int n = (Obj.magic (n : int) : file_descr) let valid_descr fd = try ignore (fstat fd); true with Unix_error (EBADF, "fstat", _) -> false let fold_open_fds f init = let f' x s = let fd = descr_of_int (int_of_string s) in if valid_descr fd then f x fd else x in Array.fold_left f' init (Sys.readdir "/proc/self/fd") Note that the readdir itself opens a file descriptor, which is included in the result but then closed. So there's a little extra complexity to filter it out so that the caller's function doesn't get applied to a bad descriptor. -- Eric Cooper e c c @ c m u . e d u