forked from xapi-project/xen-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdrommon.ml
66 lines (59 loc) · 1.88 KB
/
cdrommon.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
60
61
62
63
64
65
66
(*
* Copyright (C) 2006-2009 Citrix Systems Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; version 2.1 only. with the special
* exception on linking described in file LICENSE.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*)
let oldnotify = ref false
let disc_inserted name =
let args = [| Xapi_globs.xe_path; "host-notify"; "type=cdrom"; "params=inserted:" ^ name |] in
let ret = Unixext.spawnvp args.(0) args in
(* check if we got an error, and record the fact *)
begin match ret with
| Unix.WEXITED 0 -> oldnotify := false
| Unix.WEXITED n -> oldnotify := true
| _ -> oldnotify := true
end
let disc_removed name =
(* we don't need to do anything *)
oldnotify := false
let check interval name =
let has_disc = ref false in
let check_has_disc status =
if !has_disc then (
begin match status with
| Cdrom.NO_INFO | Cdrom.NO_DISC | Cdrom.TRAY_OPEN ->
has_disc := false; disc_removed name
| _ -> ()
end;
if !oldnotify then
disc_inserted name
) else (
match status with
| Cdrom.DISC_OK -> has_disc := true; disc_inserted name
| _ -> ()
)
in
let status = Cdrom.query_cdrom_drive_status name in
has_disc := status = Cdrom.DISC_OK;
while Sys.file_exists name
do
let drive_status = Cdrom.query_cdrom_drive_status name in
check_has_disc drive_status;
Unix.sleep interval
done
let () =
if Array.length Sys.argv <> 2 then (
Printf.eprintf "usage: %s <cdrompath>\n" Sys.argv.(0);
exit 1
);
Unixext.daemonize ();
(* check every 2 seconds *)
check 2 Sys.argv.(1)