| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
open Xenbus |
| 18 |
|
| 19 |
exception Partial_not_empty |
| 20 |
exception Unexpected_packet of string |
| 21 |
|
| 22 |
|
| 23 |
exception Invalid_path of string |
| 24 |
|
| 25 |
let unexpected_packet expected received = |
| 26 |
let s = Printf.sprintf "expecting %s received %s" |
| 27 |
(Xb.Op.to_string expected) |
| 28 |
(Xb.Op.to_string received) in |
| 29 |
raise (Unexpected_packet s) |
| 30 |
|
| 31 |
type con = { |
| 32 |
xb: Xenbus.Xb.t; |
| 33 |
watchevents: (string * string) Queue.t; |
| 34 |
} |
| 35 |
|
| 36 |
let close con = |
| 37 |
Xb.close con.xb |
| 38 |
|
| 39 |
let capacity = { Xb.maxoutstanding = 1; maxwatchevents = 0; } |
| 40 |
|
| 41 |
let open_fd fd = { |
| 42 |
xb = Xb.open_fd ~capacity fd; |
| 43 |
watchevents = Queue.create (); |
| 44 |
} |
| 45 |
|
| 46 |
let rec split_string ?limit:(limit=(-1)) c s = |
| 47 |
let i = try String.index s c with Not_found -> -1 in |
| 48 |
let nlimit = if limit = -1 || limit = 0 then limit else limit - 1 in |
| 49 |
if i = -1 || nlimit = 0 then |
| 50 |
[ s ] |
| 51 |
else |
| 52 |
let a = String.sub s 0 i |
| 53 |
and b = String.sub s (i + 1) (String.length s - i - 1) in |
| 54 |
a :: (split_string ~limit: nlimit c b) |
| 55 |
|
| 56 |
type perm = PERM_NONE | PERM_READ | PERM_WRITE | PERM_RDWR |
| 57 |
|
| 58 |
type perms = int * perm * (int * perm) list |
| 59 |
|
| 60 |
let string_of_perms perms = |
| 61 |
let owner, other, acl = perms in |
| 62 |
let char_of_perm perm = |
| 63 |
match perm with PERM_NONE -> 'n' | PERM_READ -> 'r' |
| 64 |
| PERM_WRITE -> 'w' | PERM_RDWR -> 'b' in |
| 65 |
let string_of_perm (id, perm) = Printf.sprintf "%c%u" (char_of_perm perm) id in |
| 66 |
String.concat "\000" (List.map string_of_perm ((owner,other) :: acl)) |
| 67 |
|
| 68 |
let perms_of_string s = |
| 69 |
let perm_of_char c = |
| 70 |
match c with 'n' -> PERM_NONE | 'r' -> PERM_READ |
| 71 |
| 'w' -> PERM_WRITE | 'b' -> PERM_RDWR |
| 72 |
| c -> invalid_arg (Printf.sprintf "unknown permission type: %c" c) in |
| 73 |
let perm_of_string s = |
| 74 |
if String.length s < 2 |
| 75 |
then invalid_arg (Printf.sprintf "perm of string: length = %d; contents=\"%s\"" (String.length s) s) |
| 76 |
else |
| 77 |
begin |
| 78 |
int_of_string (String.sub s 1 (String.length s - 1)), |
| 79 |
perm_of_char s.[0] |
| 80 |
end in |
| 81 |
let rec split s = |
| 82 |
try let i = String.index s '\000' in |
| 83 |
String.sub s 0 i :: split (String.sub s (i + 1) (String.length s - 1 - i)) |
| 84 |
with Not_found -> if s = "" then [] else [ s ] in |
| 85 |
let l = List.map perm_of_string (split s) in |
| 86 |
match l with h :: l -> (fst h, snd h, l) | [] -> (0, PERM_NONE, []) |
| 87 |
|
| 88 |
|
| 89 |
let pkt_send con = |
| 90 |
if Xb.has_old_output con.xb then |
| 91 |
raise Partial_not_empty; |
| 92 |
let workdone = ref false in |
| 93 |
while not !workdone |
| 94 |
do |
| 95 |
workdone := Xb.output con.xb |
| 96 |
done |
| 97 |
|
| 98 |
|
| 99 |
let rec pkt_recv con = |
| 100 |
match Xb.input con.xb with |
| 101 |
| Some packet -> packet |
| 102 |
| None -> pkt_recv con |
| 103 |
|
| 104 |
let pkt_recv_timeout con timeout = |
| 105 |
let fd = Xb.get_fd con.xb in |
| 106 |
let r, _, _ = Unix.select [ fd ] [] [] timeout in |
| 107 |
if r = [] then |
| 108 |
true, None |
| 109 |
else |
| 110 |
false, Xb.input con.xb |
| 111 |
|
| 112 |
let queue_watchevent con data = |
| 113 |
let ls = split_string ~limit:2 '\000' data in |
| 114 |
if List.length ls != 2 then |
| 115 |
raise (Xb.Packet.DataError "arguments number mismatch"); |
| 116 |
let event = List.nth ls 0 |
| 117 |
and event_data = List.nth ls 1 in |
| 118 |
Queue.push (event, event_data) con.watchevents |
| 119 |
|
| 120 |
let has_watchevents con = Queue.length con.watchevents > 0 |
| 121 |
let get_watchevent con = Queue.pop con.watchevents |
| 122 |
|
| 123 |
let read_watchevent con = |
| 124 |
let pkt = pkt_recv con in |
| 125 |
match Xb.Packet.get_ty pkt with |
| 126 |
| Xb.Op.Watchevent -> |
| 127 |
queue_watchevent con (Xb.Packet.get_data pkt); |
| 128 |
Queue.pop con.watchevents |
| 129 |
| ty -> unexpected_packet Xb.Op.Watchevent ty |
| 130 |
|
| 131 |
|
| 132 |
let rec sync_recv ty con = |
| 133 |
let pkt = pkt_recv con in |
| 134 |
match Xb.Packet.get_ty pkt with |
| 135 |
| Xb.Op.Error -> ( |
| 136 |
match Xb.Packet.get_data pkt with |
| 137 |
| "ENOENT" -> raise Xb.Noent |
| 138 |
| "EAGAIN" -> raise Xb.Eagain |
| 139 |
| "EINVAL" -> raise Xb.Invalid |
| 140 |
| s -> raise (Xb.Packet.Error s)) |
| 141 |
| Xb.Op.Watchevent -> |
| 142 |
queue_watchevent con (Xb.Packet.get_data pkt); |
| 143 |
sync_recv ty con |
| 144 |
| rty when rty = ty -> Xb.Packet.get_data pkt |
| 145 |
| rty -> unexpected_packet ty rty |
| 146 |
|
| 147 |
let sync f con = |
| 148 |
|
| 149 |
f con.xb; |
| 150 |
if Xb.output_len con.xb = 0 then |
| 151 |
Printf.printf "output len = 0\n%!"; |
| 152 |
let ty = Xb.Packet.get_ty (Xb.peek_output con.xb) in |
| 153 |
pkt_send con; |
| 154 |
sync_recv ty con |
| 155 |
|
| 156 |
let ack s = |
| 157 |
if s = "OK" then () else raise (Xb.Packet.DataError s) |
| 158 |
|
| 159 |
|
| 160 |
let validate_path path = |
| 161 |
|
| 162 |
let bad = "//" in |
| 163 |
for offset = 0 to String.length path - (String.length bad) do |
| 164 |
if String.sub path offset (String.length bad) = bad then |
| 165 |
raise (Invalid_path path) |
| 166 |
done; |
| 167 |
|
| 168 |
if path <> "/" && path <> "" && path.[String.length path - 1] = '/' then |
| 169 |
raise (Invalid_path path) |
| 170 |
|
| 171 |
|
| 172 |
let validate_watch_path path = |
| 173 |
|
| 174 |
if path <> "" && path.[0] = '@' then () |
| 175 |
else validate_path path |
| 176 |
|
| 177 |
let debug command con = |
| 178 |
sync (Queueop.debug command) con |
| 179 |
|
| 180 |
let directory tid path con = |
| 181 |
validate_path path; |
| 182 |
let data = sync (Queueop.directory tid path) con in |
| 183 |
split_string '\000' data |
| 184 |
|
| 185 |
let read tid path con = |
| 186 |
validate_path path; |
| 187 |
sync (Queueop.read tid path) con |
| 188 |
|
| 189 |
let readv tid dir vec con = |
| 190 |
List.map (fun path -> validate_path path; read tid path con) |
| 191 |
(if dir <> "" then |
| 192 |
(List.map (fun v -> dir ^ "/" ^ v) vec) else vec) |
| 193 |
|
| 194 |
let getperms tid path con = |
| 195 |
validate_path path; |
| 196 |
perms_of_string (sync (Queueop.getperms tid path) con) |
| 197 |
|
| 198 |
let watch path data con = |
| 199 |
validate_watch_path path; |
| 200 |
ack (sync (Queueop.watch path data) con) |
| 201 |
|
| 202 |
let unwatch path data con = |
| 203 |
validate_watch_path path; |
| 204 |
ack (sync (Queueop.unwatch path data) con) |
| 205 |
|
| 206 |
let transaction_start con = |
| 207 |
let data = sync (Queueop.transaction_start) con in |
| 208 |
try |
| 209 |
int_of_string data |
| 210 |
with |
| 211 |
_ -> raise (Packet.DataError (Printf.sprintf "int expected; got '%s'" data)) |
| 212 |
|
| 213 |
let transaction_end tid commit con = |
| 214 |
try |
| 215 |
ack (sync (Queueop.transaction_end tid commit) con); |
| 216 |
true |
| 217 |
with |
| 218 |
Xb.Eagain -> false |
| 219 |
|
| 220 |
let introduce domid mfn port con = |
| 221 |
ack (sync (Queueop.introduce domid mfn port) con) |
| 222 |
|
| 223 |
let release domid con = |
| 224 |
ack (sync (Queueop.release domid) con) |
| 225 |
|
| 226 |
let resume domid con = |
| 227 |
ack (sync (Queueop.resume domid) con) |
| 228 |
|
| 229 |
let getdomainpath domid con = |
| 230 |
sync (Queueop.getdomainpath domid) con |
| 231 |
|
| 232 |
let write tid path value con = |
| 233 |
validate_path path; |
| 234 |
ack (sync (Queueop.write tid path value) con) |
| 235 |
|
| 236 |
let writev tid dir vec con = |
| 237 |
List.iter (fun (entry, value) -> |
| 238 |
let path = (if dir <> "" then dir ^ "/" ^ entry else entry) in |
| 239 |
validate_path path; |
| 240 |
write tid path value con) vec |
| 241 |
|
| 242 |
let mkdir tid path con = |
| 243 |
validate_path path; |
| 244 |
ack (sync (Queueop.mkdir tid path) con) |
| 245 |
|
| 246 |
let rm tid path con = |
| 247 |
validate_path path; |
| 248 |
try |
| 249 |
ack (sync (Queueop.rm tid path) con) |
| 250 |
with |
| 251 |
Xb.Noent -> () |
| 252 |
|
| 253 |
let setperms tid path perms con = |
| 254 |
validate_path path; |
| 255 |
ack (sync (Queueop.setperms tid path (string_of_perms perms)) con) |
| 256 |
|
| 257 |
let setpermsv tid dir vec perms con = |
| 258 |
List.iter (fun entry -> |
| 259 |
let path = (if dir <> "" then dir ^ "/" ^ entry else entry) in |
| 260 |
validate_path path; |
| 261 |
setperms tid path perms con) vec |