-
Notifications
You must be signed in to change notification settings - Fork 1
/
fdinfo_example.ml
59 lines (47 loc) · 1.28 KB
/
fdinfo_example.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
(* A simple example to show how it works. *)
open Fdinfo;;
let print_fds info_list pid =
List.iter (fun (fd', fullpath) ->
try
let infos = Fdinfo.get_infos pid fd' in
Printf.printf "File descriptor number: %d\n" (int_of_fd fd') ;
Printf.printf "Name: %s\n" fullpath ;
Printf.printf "Current offset: %s\n" (Int64.to_string infos.offset) ;
Printf.printf "Flags: %s\n\n" infos.flags
with
| Fdinfo_parse_error ->
prerr_endline "An error occurred while parsing data from /proc"
| Fdinfo_sys_error errmsg ->
prerr_endline ("Sys error: "^errmsg)
)
info_list
;;
let _ =
let usage = "usage: fdinfo_example pid" in
if (Array.length Sys.argv) <> 2 then
begin
print_endline usage ;
exit 1
end ;
let ipid =
try
int_of_string (Sys.argv.(1))
with Failure "int_of_string" ->
print_endline usage ;
exit 2
in
let p = pid_of_int ipid in
let info_list =
try
Fdinfo.get_fds p
with
| Fdinfo_unix_error (error, funct, fdsret) ->
prerr_endline ((Unix.error_message error)^" in funct: "^funct);
match fdsret with
| Ppid _ -> assert false
| Ffd fds -> fds
in
Printf.printf "Length: %d\n" (List.length info_list);
print_fds info_list p;
flush stdout
;;