text
stringlengths 0
601k
|
---|
let find_opt h x = if Hashtbl . mem h x then Some ( Hashtbl . find h x ) else None |
module type IO = sig type ' a t = ' a val return : ' a -> ' a t val ( >>= ) : ' a t -> ( ' a -> ' b t ) -> ' b t type channel val create : unit -> channel t val destroy : channel -> unit t val read : channel -> string -> int -> int -> int t val write : channel -> string -> int -> int -> unit t end |
let ( |> ) a b = b a |
let ( ++ ) f g x = f ( g x ) |
module Watcher = struct type t = { mutable paths : StringSet . t ; mutable cancelling : bool ; c : Condition . t ; m : Mutex . t ; } let make ( ) = { paths = StringSet . empty ; cancelling = false ; c = Condition . create ( ) ; m = Mutex . create ( ) ; } let put ( x : t ) path = with_mutex x . m ( fun ( ) -> x . paths <- StringSet . add path x . paths ; Condition . signal x . c ) let get ( x : t ) = with_mutex x . m ( fun ( ) -> while x . paths = StringSet . empty && not x . cancelling do Condition . wait x . c x . m done ; let results = x . paths in x . paths <- StringSet . empty ; results ) let cancel ( x : t ) = with_mutex x . m ( fun ( ) -> x . cancelling <- true ; Condition . signal x . c ) end |
module Task = struct type ' a u = { mutable thing : ' a option ; mutable cancelling : bool ; mutable on_cancel : unit -> unit ; m : Mutex . t ; c : Condition . t } let make ( ) = { thing = None ; cancelling = false ; on_cancel = ( fun ( ) -> ( ) ) ; m = Mutex . create ( ) ; c = Condition . create ( ) ; } let wakeup u thing = with_mutex u . m ( fun ( ) -> u . thing <- Some thing ; Condition . signal u . c ) let on_cancel u on_cancel = u . on_cancel <- on_cancel let cancel u = with_mutex u . m ( fun ( ) -> u . cancelling <- true ; Condition . signal u . c ) ; u . on_cancel ( ) let wait u = with_mutex u . m ( fun ( ) -> let rec loop ( ) = if u . cancelling then raise Cancelled else match u . thing with | None -> Condition . wait u . c u . m ; loop ( ) | Some thing -> thing in loop ( ) ) end |
type watch_callback = string * string -> unit |
let startswith prefix x = let prefix ' = String . length prefix and x ' = String . length x in x ' >= prefix ' && ( String . sub x 0 prefix ' ) = prefix |
module Client = functor ( IO : IO with type ' a t = ' a ) -> struct module PS = PacketStream ( IO ) let logger = ref ( fun s -> let _ : string = s in ( ) ) let error fmt = Printf . kprintf ! logger fmt let set_logger f = logger := f type client = { transport : IO . channel ; ps : PS . stream ; rid_to_wakeup : ( int32 , Xs_protocol . t Task . u ) Hashtbl . t ; mutable dispatcher_thread : Thread . t option ; mutable dispatcher_shutting_down : bool ; mutable watch_callback_thread : Thread . t option ; watchevents : ( string , Watcher . t ) Hashtbl . t ; incoming_watches : ( string * string ) Queue . t ; queue_overflowed : bool ref ; incoming_watches_m : Mutex . t ; incoming_watches_c : Condition . t ; mutable extra_watch_callback : ( ( string * string ) -> unit ) ; m : Mutex . t ; } type handle = client Xs_handle . t let recv_one t = match ( PS . recv t . ps ) with | Ok x -> x | Exception e -> raise e let send_one t = PS . send t . ps let handle_exn t e = error " Caught : % s \ n " %! ( Printexc . to_string e ) ; begin match e with | Xs_protocol . Response_parser_failed x -> ( ) | _ -> ( ) end ; t . dispatcher_shutting_down <- true ; raise e let enqueue_watch t event = with_mutex t . incoming_watches_m ( fun ( ) -> ) let rec dispatcher t = let pkt = try recv_one t with e -> handle_exn t e in match get_ty pkt with | Op . Watchevent -> begin match Unmarshal . list pkt with | Some [ path ; token ] -> let w = with_mutex t . m ( fun ( ) -> find_opt t . watchevents token ) in begin match w with | Some w -> Watcher . put w path | None -> if not ( startswith auto_watch_prefix token ) then enqueue_watch t ( path , token ) end ; | _ -> handle_exn t Malformed_watch_event end ; dispatcher t | _ -> let rid = get_rid pkt in let u = with_mutex t . m ( fun ( ) -> find_opt t . rid_to_wakeup rid ) in begin match u with | Some u -> Task . wakeup u pkt | None -> error " Unexpected rid : % ld \ n " %! rid end ; dispatcher t let dequeue_watches t = while true do try ( ) with done let make ( ) = let transport = IO . create ( ) in let t = { transport = transport ; ps = PS . make transport ; rid_to_wakeup = Hashtbl . create 10 ; dispatcher_thread = None ; dispatcher_shutting_down = false ; watch_callback_thread = None ; watchevents = Hashtbl . create 10 ; incoming_watches = Queue . create ( ) ; queue_overflowed = ref false ; incoming_watches_m = Mutex . create ( ) ; incoming_watches_c = Condition . create ( ) ; extra_watch_callback = ( fun _ -> ( ) ) ; m = Mutex . create ( ) ; } in t . dispatcher_thread <- Some ( Thread . create dispatcher t ) ; t . watch_callback_thread <- Some ( Thread . create dequeue_watches t ) ; t let set_watch_callback client cb = client . extra_watch_callback <- cb let make_rid = let rpc hint h payload unmarshal = let open Xs_handle in let rid = make_rid ( ) in let request = Request . print payload ( get_tid h ) rid in let t = Task . make ( ) in let c = get_client h in if c . dispatcher_shutting_down then raise Dispatcher_failed else begin with_mutex c . m ( fun ( ) -> Hashtbl . add c . rid_to_wakeup rid t ) ; send_one c request ; let res = Task . wait t in with_mutex c . m ( fun ( ) -> Hashtbl . remove c . rid_to_wakeup rid ) ; response hint request res unmarshal end let directory h path = rpc " directory " ( Xs_handle . accessed_path h path ) Request . ( PathOp ( path , Directory ) ) Unmarshal . list let read h path = rpc " read " ( Xs_handle . accessed_path h path ) Request . ( PathOp ( path , Read ) ) Unmarshal . string let write h path data = rpc " write " ( Xs_handle . accessed_path h path ) Request . ( PathOp ( path , Write data ) ) Unmarshal . ok let rm h path = rpc " rm " ( Xs_handle . accessed_path h path ) Request . ( PathOp ( path , Rm ) ) Unmarshal . ok let mkdir h path = rpc " mkdir " ( Xs_handle . accessed_path h path ) Request . ( PathOp ( path , Mkdir ) ) Unmarshal . ok let setperms h path acl = rpc " setperms " ( Xs_handle . accessed_path h path ) Request . ( PathOp ( path , Setperms acl ) ) Unmarshal . ok let debug h cmd_args = rpc " debug " h ( Request . Debug cmd_args ) Unmarshal . list let restrict h domid = rpc " restrict " h ( Request . Restrict domid ) Unmarshal . ok let getdomainpath h domid = rpc " getdomainpath " h ( Request . Getdomainpath domid ) Unmarshal . string let watch h path token = rpc " watch " ( Xs_handle . watch h path ) ( Request . Watch ( path , token ) ) Unmarshal . ok let unwatch h path token = rpc " unwatch " ( Xs_handle . watch h path ) ( Request . Unwatch ( path , token ) ) Unmarshal . ok let introduce h domid store_mfn store_port = rpc " introduce " h ( Request . Introduce ( domid , store_mfn , store_port ) ) Unmarshal . ok let set_target h stubdom_domid domid = rpc " set_target " h ( Request . Set_target ( stubdom_domid , domid ) ) Unmarshal . ok let immediate client f = f ( Xs_handle . no_transaction client ) let counter = ref 0l let wait client f = let open StringSet in counter := Int32 . succ ! counter ; let token = Printf . sprintf " % s % ld " auto_watch_prefix ! counter in let watcher = Watcher . make ( ) in with_mutex client . m ( fun ( ) -> Hashtbl . add client . watchevents token watcher ) ; let t = Task . make ( ) in Task . on_cancel t ( fun ( ) -> Watcher . cancel watcher ) ; let h = Xs_handle . watching client in let adjust_paths ( ) = let current_paths = Xs_handle . get_watched_paths h in let old_paths = diff current_paths ( Xs_handle . get_accessed_paths h ) in List . iter ( fun p -> unwatch h p token ) ( elements old_paths ) ; let new_paths = diff ( Xs_handle . get_accessed_paths h ) current_paths in List . iter ( fun p -> watch h p token ) ( elements new_paths ) ; if old_paths = empty && ( new_paths = empty ) then begin let results = Watcher . get watcher in if results = empty then raise ( Failure " goodnight " ) end in let rec loop ( ) = let finished = try let result = f h in Task . wakeup t result ; true with Eagain -> false in if not finished then begin adjust_paths ( ) ; loop ( ) end in let ( _ : Thread . t ) = Thread . create ( fun ( ) -> finally loop ( fun ( ) -> let current_paths = Xs_handle . get_watched_paths h in List . iter ( fun p -> unwatch h p token ) ( elements current_paths ) ; with_mutex client . m ( fun ( ) -> Hashtbl . remove client . watchevents token ) ; ) ) ( ) in t let rec transaction client f = let tid = rpc " transaction_start " ( Xs_handle . no_transaction client ) Request . Transaction_start Unmarshal . int32 in let h = Xs_handle . transaction client tid in let result = f h in try let res ' = rpc " transaction_end " h ( Request . Transaction_end true ) Unmarshal . string in if res ' = " OK " then result else raise ( Error ( Printf . sprintf " Unexpected transaction result : % s " res ' ) ) with Eagain -> transaction client f end |
module StringSet = Set . Make ( struct type t = string let compare = compare end ) |
type ' a t = { client : ' a ; tid : int32 ; mutable accessed_paths : StringSet . t option ; mutable watched_paths : StringSet . t ; } |
let make client = { client = client ; tid = 0l ; accessed_paths = None ; watched_paths = StringSet . empty } |
let get_tid h = h . tid |
let get_client h = h . client |
let no_transaction client = make client |
let transaction client tid = { ( make client ) with tid = tid } |
let watching client = { ( make client ) with accessed_paths = Some StringSet . empty } |
let accessed_path h path = match h . accessed_paths with | None -> h | Some ps -> h . accessed_paths <- Some ( StringSet . add path ps ) ; h |
let get_accessed_paths h = match h . accessed_paths with | None -> StringSet . empty | Some xs -> xs |
let watch h path = h . watched_paths <- StringSet . add path h . watched_paths ; h |
let unwatch h path = h . watched_paths <- StringSet . remove path h . watched_paths ; h |
let get_watched_paths h = h . watched_paths |
let ( |> ) f g = g f |
let ( ++ ) f g x = f ( g x ) |
module Op = struct type t = | Debug | Directory | Read | Getperms | Watch | Unwatch | Transaction_start | Transaction_end | Introduce | Release | Getdomainpath | Write | Mkdir | Rm | Setperms | Watchevent | Error | Isintroduced | Resume | Set_target | Restrict |
let on_the_wire = [ | Debug ; Directory ; Read ; Getperms ; Watch ; Unwatch ; Transaction_start ; Transaction_end ; Introduce ; Release ; Getdomainpath ; Write ; Mkdir ; Rm ; Setperms ; Watchevent ; Error ; Isintroduced ; Resume ; Set_target ; Restrict ] | |
let of_int32 i = let i = Int32 . to_int i in if i >= 0 && i < Array . length on_the_wire then Some ( on_the_wire . ( i ) ) else None |
let to_int32 x = match snd ( Array . fold_left ( fun ( idx , result ) v -> if x = v then ( idx + 1 , Some idx ) else ( idx + 1 , result ) ) ( 0 , None ) on_the_wire ) with | None -> assert false | Some i -> Int32 . of_int i |
let to_string = function | Debug -> " debug " | Directory -> " directory " | Read -> " read " | Getperms -> " getperms " | Watch -> " watch " | Unwatch -> " unwatch " | Transaction_start -> " transaction_start " | Transaction_end -> " transaction_end " | Introduce -> " introduce " | Release -> " release " | Getdomainpath -> " getdomainpath " | Write -> " write " | Mkdir -> " mkdir " | Rm -> " rm " | Setperms -> " setperms " | Watchevent -> " watchevent " | Error -> " error " | Isintroduced -> " isintroduced " | Resume -> " resume " | Set_target -> " set_target " | Restrict -> " restrict " end |
let rec split_string ? limit ( : limit ( =- 1 ) ) c s = let i = try String . index s c with Not_found -> - 1 in let nlimit = if limit = - 1 || limit = 0 then limit else limit - 1 in if i = - 1 || nlimit = 0 then [ s ] else let a = String . sub s 0 i and b = String . sub s ( i + 1 ) ( String . length s - i - 1 ) in a :: ( split_string ~ limit : nlimit c b ) |
module ACL = struct type perm = | NONE | READ | WRITE | RDWR let char_of_perm = function | READ -> ' r ' | WRITE -> ' w ' | RDWR -> ' b ' | NONE -> ' n ' let perm_of_char = function | ' r ' -> Some READ | ' w ' -> Some WRITE | ' b ' -> Some RDWR | ' n ' -> Some NONE | _ -> None type domid = int type t = { owner : domid ; other : perm ; acl : ( domid * perm ) list ; } let to_string perms = let string_of_perm ( id , perm ) = Printf . sprintf " % c % u " ( char_of_perm perm ) id in String . concat " \ 000 " ( List . map string_of_perm ( ( perms . owner , perms . other ) :: perms . acl ) ) let of_string s = let perm_of_char_exn x = match ( perm_of_char x ) with Some y -> y | None -> raise Not_found in try let perm_of_string s = if String . length s < 2 then invalid_arg ( Printf . sprintf " Permission string too short : ' % s ' " s ) ; int_of_string ( String . sub s 1 ( String . length s - 1 ) ) , perm_of_char_exn s . [ 0 ] in let l = List . map perm_of_string ( split_string ' \ 000 ' s ) in match l with | ( owner , other ) :: l -> Some { owner = owner ; other = other ; acl = l } | [ ] -> Some { owner = 0 ; other = NONE ; acl = [ ] } with e -> None end |
type t = { tid : int32 ; rid : int32 ; ty : Op . t ; len : int ; data : Buffer . t ; } uint32_t ty ; uint32_t rid ; uint32_t tid ; uint32_t len |
let to_string pkt = let header = Cstruct . create sizeof_header in let len = Int32 . of_int ( Buffer . length pkt . data ) in let ty = Op . to_int32 pkt . ty in set_header_ty header ty ; set_header_rid header pkt . rid ; set_header_tid header pkt . tid ; set_header_len header len ; Cstruct . to_string header ^ ( Buffer . contents pkt . data ) |
let get_tid pkt = pkt . tid |
let get_ty pkt = pkt . ty |
let get_data pkt = if pkt . len > 0 && Buffer . nth pkt . data ( pkt . len - 1 ) = ' \ 000 ' then Buffer . sub pkt . data 0 ( pkt . len - 1 ) else Buffer . contents pkt . data |
let get_rid pkt = pkt . rid |
module Parser = struct let header_size = 16 let xenstore_payload_max = 4096 let allow_oversize_packets = ref true type state = | Unknown_operation of int32 | Parser_failed of string | Need_more_data of int | Packet of t type parse = | ReadingHeader of int * string | ReadingBody of t | Finished of state let start ( ) = ReadingHeader ( 0 , String . make header_size ' \ 000 ' ) let state = function | ReadingHeader ( got_already , _ ) -> Need_more_data ( header_size - got_already ) | ReadingBody pkt -> Need_more_data ( pkt . len - ( Buffer . length pkt . data ) ) | Finished r -> r let parse_header str = let header = Cstruct . create sizeof_header in Cstruct . blit_from_string str 0 header 0 sizeof_header ; let ty = get_header_ty header in let rid = get_header_rid header in let tid = get_header_tid header in let len = get_header_len header in let len = Int32 . to_int len in let len = if ! allow_oversize_packets then len else max 0 ( min xenstore_payload_max len ) in begin match Op . of_int32 ty with | Some ty -> let t = { tid = tid ; rid = rid ; ty = ty ; len = len ; data = Buffer . create len ; } in if len = 0 then Finished ( Packet t ) else ReadingBody t | None -> Finished ( Unknown_operation ty ) end let input state bytes = match state with | ReadingHeader ( got_already , str ) -> String . blit bytes 0 str got_already ( String . length bytes ) ; let got_already = got_already + ( String . length bytes ) in if got_already < header_size then ReadingHeader ( got_already , str ) else parse_header str | ReadingBody x -> Buffer . add_string x . data bytes ; let needed = x . len - ( Buffer . length x . data ) in if needed > 0 then ReadingBody x else Finished ( Packet x ) | Finished f -> Finished f end |
module type IO = sig type ' a t val return : ' a -> ' a t val ( >>= ) : ' a t -> ( ' a -> ' b t ) -> ' b t type channel val read : channel -> string -> int -> int -> int t val write : channel -> string -> int -> int -> unit t end |
type ( ' a , ' b ) result = |
module PacketStream = functor ( IO : IO ) -> struct let ( >>= ) = IO . ( >>= ) let return = IO . return type stream = { channel : IO . channel ; mutable incoming_pkt : Parser . parse ; } let make t = { channel = t ; incoming_pkt = Parser . start ( ) ; } let rec recv t = let open Parser in match Parser . state t . incoming_pkt with | Packet pkt -> t . incoming_pkt <- start ( ) ; return ( Ok pkt ) | Need_more_data x -> let buf = String . make x ' \ 000 ' in IO . read t . channel buf 0 x >>= ( function | 0 -> return ( Exception EOF ) | n -> let fragment = String . sub buf 0 n in | Unknown_operation x -> return ( Exception ( Unknown_xenstore_operation x ) ) | Parser_failed x -> return ( Exception ( Response_parser_failed x ) ) let send t request = let req = to_string request in end |
let is_valid_path path = let result = ref true in let bad = " " // in for offset = 0 to String . length path - ( String . length bad ) do if String . sub path offset ( String . length bad ) = bad then result := false done ; if path <> " " / && path <> " " && path . [ String . length path - 1 ] = ' ' / then result := false ; ! result |
let is_valid_watch_path path = ( path <> " " && path . [ 0 ] = ' ' ) @ || ( is_valid_path path ) |
module Token = struct type t = string let to_user_string x = Scanf . sscanf x " % d :% s " ( fun _ x -> x ) let to_debug_string x = x let of_string x = x let to_string x = x end |
let data_concat ls = ( String . concat " \ 000 " ls ) ^ " \ 000 " |
let create tid rid ty data = let len = String . length data in let b = Buffer . create len in Buffer . add_string b data ; { tid = tid ; rid = rid ; ty = ty ; len = len ; data = b ; } |
let set_data pkt ( data : string ) = let len = String . length data in let b = Buffer . create len in Buffer . add_string b data ; { pkt with len = len ; data = b } |
module Response = struct type payload = | Read of string | Directory of string list | Getperms of ACL . t | Getdomainpath of string | Transaction_start of int32 | Write | Mkdir | Rm | Setperms | Watch | Unwatch | Transaction_end | Debug of string list | Introduce | Resume | Release | Set_target | Restrict | Isintroduced of bool | Error of string | Watchevent of string * string let prettyprint_payload = let open Printf in function | Read x -> sprintf " Read % s " x | Directory xs -> sprintf " Directory [ % s ] " ( String . concat " ; " xs ) | Getperms acl -> sprintf " Getperms % s " ( ACL . to_string acl ) | Getdomainpath p -> sprintf " Getdomainpath % s " p | Transaction_start x -> sprintf " Transaction_start % ld " x | Write -> " Write " | Mkdir -> " Mkdir " | Rm -> " Rm " | Setperms -> " Setperms " | Watch -> " Watch " | Unwatch -> " Unwatch " | Transaction_end -> " Transaction_end " | Debug xs -> sprintf " Debug [ % s ] " ( String . concat " ; " xs ) | Introduce -> " Introduce " | Resume -> " Resume " | Release -> " Release " | Set_target -> " Set_target " | Restrict -> " Restrict " | Isintroduced x -> sprintf " Isintroduced % b " x | Error x -> sprintf " Error % s " x | Watchevent ( x , y ) -> sprintf " Watchevent % s % s " x y let ty_of_payload = function | Read _ -> Op . Read | Directory _ -> Op . Directory | Getperms perms -> Op . Getperms | Getdomainpath _ -> Op . Getdomainpath | Transaction_start _ -> Op . Transaction_start | Debug _ -> Op . Debug | Isintroduced _ -> Op . Isintroduced | Watchevent ( _ , _ ) -> Op . Watchevent | Error _ -> Op . Error | Write -> Op . Write | Mkdir -> Op . Mkdir | Rm -> Op . Rm | Setperms -> Op . Setperms | Watch -> Op . Watch | Unwatch -> Op . Unwatch | Transaction_end -> Op . Transaction_end | Introduce -> Op . Introduce | Resume -> Op . Resume | Release -> Op . Release | Set_target -> Op . Set_target | Restrict -> Op . Restrict let ok = " OK \ 000 " let data_of_payload = function | Read x -> x | Directory ls -> if ls = [ ] then " " else data_concat ls | Getperms perms -> data_concat [ ACL . to_string perms ] | Getdomainpath x -> data_concat [ x ] | Transaction_start tid -> data_concat [ Int32 . to_string tid ] | Debug items -> data_concat items | Isintroduced b -> data_concat [ if b then " T " else " F " ] | Watchevent ( path , token ) -> data_concat [ path ; token ] | Error x -> data_concat [ x ] | _ -> ok let print x tid rid = create tid rid ( ty_of_payload x ) ( data_of_payload x ) end |
module Request = struct do done ; ! v end try ] create rid end |
module Unmarshal = struct let some x = Some x let int_of_string_opt x = try Some ( int_of_string x ) with _ -> None let int32_of_string_opt x = try Some ( Int32 . of_string x ) with _ -> None let unit_of_string_opt x = if x = " " then Some ( ) else None let ok x = if x = " OK " then Some ( ) else None let string = some ++ get_data let list = some ++ split_string ' \ 000 ' ++ get_data let acl = ACL . of_string ++ get_data let int = int_of_string_opt ++ get_data let int32 = int32_of_string_opt ++ get_data let unit = unit_of_string_opt ++ get_data let ok = ok ++ get_data end |
let response hint sent received f = match get_ty sent , get_ty received with | _ , Op . Error -> begin match get_data received with | " ENOENT " -> raise ( Enoent hint ) | " EAGAIN " -> raise Eagain | " EINVAL " -> raise Invalid | " EEXIST " -> raise Eexist | s -> raise ( Error s ) end | x , y when x = y -> begin match f received with | None -> raise ( Error ( Printf . sprintf " failed to parse response ( hint :% s ) ( payload :% s ) " hint ( get_data received ) ) ) | Some z -> z end | x , y -> raise ( Error ( Printf . sprintf " unexpected packet : expected % s ; got % s " ( Op . to_string x ) ( Op . to_string y ) ) ) |
let ( |> ) a b = b a |
let ( ++ ) f g x = f ( g x ) |
let debug fmt = Logging . debug " xs_server " fmt |
let error fmt = Logging . error " xs_server " fmt |
let store = List . iter store |
module type TRANSPORT = sig type ' a t = ' a Lwt . t val return : ' a -> ' a Lwt . t val ( >>= ) : ' a t -> ( ' a -> ' b Lwt . t ) -> ' b Lwt . t type server val listen : unit -> server Lwt . t type channel val read : channel -> string -> int -> int -> int Lwt . t val write : channel -> string -> int -> int -> unit Lwt . t val destroy : channel -> unit Lwt . t val address_of : channel -> Xs_protocol . address Lwt . t val namespace_of : channel -> ( module Namespace . IO ) option val accept_forever : server -> ( channel -> unit Lwt . t ) -> ' a Lwt . t end |
module Server = functor ( T : TRANSPORT ) -> struct Lwt_list . iter_s ) try_lwt ) end |
let ( |> ) a b = b a |
let id x = x |
let op_ids _ = let open Xs_protocol . Op in for i = 0 to 100 do let i ' = Int32 . of_int i in match of_int32 i ' with | None -> ( ) | Some x -> assert ( to_int32 x = i ' ) done |
let example_acl = { owner = 5 ; other = READ ; acl = [ 2 , WRITE ; 3 , RDWR ] } |
let acl_parser _ = let open Xs_protocol . ACL in let ts = [ { owner = 5 ; other = READ ; acl = [ 2 , WRITE ; 3 , RDWR ] } ; { owner = 1 ; other = WRITE ; acl = [ ] } ; ] in let ss = List . map to_string ts in let ts ' = List . map of_string ss in let printer = function | None -> " None " | Some x -> " Some " ^ to_string x in List . iter ( fun ( x , y ) -> assert_equal ~ msg " : acl " ~ printer x y ) ( List . combine ( List . map ( fun x -> Some x ) ts ) ts ' ) |
let test_packet_parser choose pkt ( ) = let open Xs_protocol in let p = ref ( Parser . start ( ) ) in let s = to_string pkt in let i = ref 0 in let finished = ref false in while not ! finished do match Parser . state ! p with done |
let test _ = let t = return ( ) in Lwt_main . run t |
type example_packet = { } |
let make_example_request op payload tid wire_fmt = { } |
let example_request_packets = " \ x01 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x05 \ x00 \ x00 \ x00 \ x13 \ x00 \ x00 \ x00 \ x2f \ x77 \ x68 \ x61 \ x74 \ x65 \ x76 \ x65 \ x72 \ x2f \ x77 \ x68 \ x65 \ x6e \ x65 \ x76 \ x65 \ x72 \ x00 " ; " \ x02 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x06 \ x00 \ x00 \ x00 \ x07 \ x00 \ x00 \ x00 \ x2f \ x61 \ x2f \ x62 \ x2f \ x63 \ x00 " ; " \ x03 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x07 \ x00 \ x00 \ x00 \ x05 \ x00 \ x00 \ x00 \ x2f \ x61 \ x2f \ x62 \ x00 " ; " \ x0d \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x02 \ x00 \ x00 \ x00 \ x2f \ x00 " ; " \ x0e \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x01 \ x00 \ x00 \ x00 \ x0b \ x00 \ x00 \ x00 \ x2f \ x00 \ x72 \ x35 \ x00 \ x77 \ x32 \ x00 \ x62 \ x33 \ x00 " ; " \ x0b \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x01 \ x00 \ x00 \ x00 \ x0a \ x00 \ x00 \ x00 \ x2f \ x6b \ x65 \ x79 \ x00 \ x76 \ x61 \ x6c \ x75 \ x65 " ; " \ x0c \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x04 \ x00 \ x00 \ x02 \ x00 \ x00 \ x00 \ x2f \ x00 " ; " \ x06 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x01 \ x00 \ x00 \ x00 \ x00 " ; " \ x07 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x01 \ x00 \ x00 \ x00 \ x02 \ x00 \ x00 \ x00 \ x54 \ x00 " ; " \ x08 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x06 \ x00 \ x00 \ x00 \ x34 \ x00 \ x35 \ x00 \ x31 \ x00 " ; " \ x09 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x02 \ x00 \ x00 \ x00 \ x32 \ x00 " ; " \ x12 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x02 \ x00 \ x00 \ x00 \ x33 \ x00 " ; " \ x0a \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x02 \ x00 \ x00 \ x00 \ x33 \ x00 " ; " \ x04 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x13 \ x00 \ x00 \ x00 \ x2f \ x66 \ x6f \ x6f \ x2f \ x62 \ x61 \ x72 \ x00 \ x73 \ x6f \ x6d \ x65 \ x74 \ x68 \ x69 \ x6e \ x67 \ x00 " ; " \ x05 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x16 \ x00 \ x00 \ x00 \ x2f \ x66 \ x6f \ x6f \ x2f \ x62 \ x61 \ x72 \ x00 \ x73 \ x6f \ x6d \ x65 \ x74 \ x68 \ x69 \ x6e \ x67 \ x6c \ x73 \ x65 \ x00 " ; " \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x0e \ x00 \ x00 \ x00 \ x61 \ x00 \ x62 \ x00 \ x73 \ x6f \ x6d \ x65 \ x74 \ x68 \ x69 \ x6e \ x67 \ x00 " ] |
let make_example_response op response wire_fmt = } |
let example_response_packets = " \ x02 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x06 \ x00 \ x00 \ x00 \ x09 \ x00 \ x00 \ x00 \ x74 \ x68 \ x65 \ x72 \ x65 \ x73 \ x75 \ x6c \ x74 " ; " \ x02 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x06 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 " ; " \ x03 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x07 \ x00 \ x00 \ x00 \ x06 \ x00 \ x00 \ x00 \ x72 \ x32 \ x00 \ x6e \ x34 \ x00 " ; " \ x0a \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x10 \ x00 \ x00 \ x00 \ x2f \ x6c \ x6f \ x63 \ x61 \ x6c \ x2f \ x64 \ x6f \ x6d \ x61 \ x69 \ x6e \ x2f \ x34 \ x00 " ; " \ x06 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x02 \ x00 \ x00 \ x00 \ x33 \ x00 " ; " \ x01 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x05 \ x00 \ x00 \ x00 \ x15 \ x00 \ x00 \ x00 \ x61 \ x00 \ x62 \ x00 \ x63 \ x00 \ x61 \ x73 \ x65 \ x61 \ x73 \ x79 \ x61 \ x73 \ x00 \ x31 \ x00 \ x32 \ x00 \ x33 \ x00 " ; " \ x0b \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x01 \ x00 \ x00 \ x00 \ x03 \ x00 \ x00 \ x00 \ x4f \ x4b \ x00 " ; " \ x0c \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x04 \ x00 \ x00 \ x03 \ x00 \ x00 \ x00 \ x4f \ x4b \ x00 " ; " \ x0d \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x03 \ x00 \ x00 \ x00 \ x4f \ x4b \ x00 " ; " \ x0e \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x01 \ x00 \ x00 \ x00 \ x03 \ x00 \ x00 \ x00 \ x4f \ x4b \ x00 " ; " \ x04 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x03 \ x00 \ x00 \ x00 \ x4f \ x4b \ x00 " ; " \ x05 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x03 \ x00 \ x00 \ x00 \ x4f \ x4b \ x00 " ; " \ x07 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x01 \ x00 \ x00 \ x00 \ x03 \ x00 \ x00 \ x00 \ x4f \ x4b \ x00 " ; { " \ x10 \ x00 \ x00 \ x00 \ x10 \ x00 \ x00 \ x00 \ x02 \ x00 \ x00 \ x00 \ x14 \ x00 \ x00 \ x00 \ x77 \ x68 \ x61 \ x74 \ x79 \ x6f \ x75 \ x74 \ x61 \ x6c \ x6b \ x69 \ x6e \ x67 \ x61 \ x62 \ x6f \ x75 \ x74 \ x00 " } ] |
let example_packets = example_request_packets @ example_response_packets |
let rec ints first last = |
let hexstring x = " " " ; \ " " " ; \ ] ) |
let _ = let verbose = ref false in Arg . parse [ " - verbose " , Arg . Unit ( fun _ -> verbose := true ) , " Run in verbose mode " ; ] ( fun x -> Printf . fprintf stderr " Ignoring argument : % s " x ) " Test xenstore protocol code " ; let packet_parsing choose = let f = test_packet_parser choose in " packet_parsing " >::: let packet_printing = let suite = " xenstore " >::: [ " op_ids " >:: op_ids ; " acl_parser " >:: acl_parser ; packet_parsing id ; packet_parsing ( fun _ -> 1 ) ; " test " >:: test ; ] in run_test_tt ~ verbose :! verbose suite |
let create ( ) = ( match Xs_transport . choose_xenstore_path ( ) with | None -> Printf . fprintf stderr " Failed to find xenstore socket . I tried the following :\ n " ; List . iter ( fun x -> Printf . fprintf stderr " % s \ n " x ) ( Xs_transport . get_xenstore_paths ( ) ) ; Printf . fprintf stderr " \ nOn linux you might not have xenfs mounted :\ n " ; Printf . fprintf stderr " sudo mount - t xenfs xenfs / proc / xen \ n " ; Printf . fprintf stderr " Or perhaps you just need to set the XENSTORED_PATH environment variable . \ n " ; fail Xs_transport . Could_not_find_xenstore | Some x -> return x ) >>= fun path -> Lwt_unix . stat path >>= fun stats -> match stats . Lwt_unix . st_kind with | Lwt_unix . S_SOCK -> let sockaddr = Lwt_unix . ADDR_UNIX ( path ) in let fd = Lwt_unix . socket Lwt_unix . PF_UNIX Lwt_unix . SOCK_STREAM 0 in Lwt . catch ( fun ( ) -> Lwt_unix . connect fd sockaddr ) ( fun e -> Lwt_unix . close fd >>= fun ( ) -> Lwt . fail e ) >>= fun ( ) -> return fd | _ -> let fd = Unix . openfile path [ Lwt_unix . O_RDWR ] 0o0 in return ( Lwt_unix . of_unix_file_descr ~ blocking : false fd ) |
let write fd bufs ofs len = Lwt_unix . write fd bufs ofs len >>= fun n -> if n <> len then fail End_of_file else return ( ) |
type ' a t = ' a Lwt . t |
let ( >>= ) = Lwt . bind |
type backend = [ ` unix | ` xen ] |
let create ( ) = let path = match Xs_transport . choose_xenstore_path ( ) with | None -> Printf . fprintf stderr " Failed to find xenstore socket . I tried the following :\ n " ; List . iter ( fun x -> Printf . fprintf stderr " % s \ n " x ) ( Xs_transport . get_xenstore_paths ( ) ) ; Printf . fprintf stderr " \ nOn linux you might not have xenfs mounted :\ n " ; Printf . fprintf stderr " sudo mount - t xenfs xenfs / proc / xen \ n " ; Printf . fprintf stderr " Or perhaps you just need to set the XENSTORED_PATH environment variable . \ n " ; raise Xs_transport . Could_not_find_xenstore | Some path -> path in let stats = Unix . stat path in match stats . Unix . st_kind with | Unix . S_SOCK -> begin let sockaddr = Unix . ADDR_UNIX ( path ) in let fd = Unix . socket Unix . PF_UNIX Unix . SOCK_STREAM 0 in try Unix . connect fd sockaddr ; fd with e -> Unix . close fd ; raise e end | _ -> Unix . openfile path [ Lwt_unix . O_RDWR ] 0o0 |
let destroy fd = Unix . close fd |
let read fd = Unix . read fd |
let write fd bufs ofs len = let n = Unix . write fd bufs ofs len in if n <> len then begin raise End_of_file end |
type ' a t = ' a |
let return x = x |
let ( >>= ) x f = f x |
type backend = [ ` unix | ` xen ] |
type t = | Test of ( unit -> unit ) | Labeled of string * t | List of t list |
let fun_ f = Test f |
let label s t = Labeled ( s , t ) |
let list ts = List ts |
module Config = struct type arg = | Do of Pcre . regexp | Dont of Pcre . regexp type t = { args : arg list ; show_tests : bool ; default_go : bool ; } let rev_args = ref [ ] let show_tests = ref false let from_args default_go = { args = List . rev ! rev_args ; show_tests = ! show_tests ; default_go = default_go ; } let is_go conf label = let s = String . concat " " / label in match List . find_map_opt ( function | Do rex when Pcre . pmatch ~ rex s -> Some true | Dont rex when Pcre . pmatch ~ rex s -> Some false | _ -> None ) conf . args with | Some b -> b | None -> conf . default_go end |
let arg_specs = let open Arg in let open Config in [ " -- test " , String ( fun s -> rev_args +::= Do ( Pcre . regexp s ) ) , " < string >: Perform tests match with the string " ; " -- no - test " , String ( fun s -> rev_args +::= Dont ( Pcre . regexp s ) ) , " < string >: Skip tests match with the string " ; " -- show - tests " , Set show_tests , " : List all the tests " ; ] |
module State = struct type t = { rev_label : string list ; config : Config . t ; } let add_label l st = { st with rev_label = l :: st . rev_label } let labels st = List . rev st . rev_label let is_go t = Config . is_go t . config & List . rev t . rev_label let from_args default_go = { rev_label = [ ] ; config = Config . from_args default_go ; } end |
let rec iter st go prim = function | Test t -> prim st t | Labeled ( l , t ) -> let st = State . add_label l st in go st ( fun ( ) -> iter st go prim t ) | List ts -> List . iter ( iter st go prim ) ts |
let show st t = let go _st f = f ( ) in let prim st _ = !!% " % s : % s . " @ ( String . concat " " / ( State . labels st ) ) ( if State . is_go st then " go " else " skip " ) in iter st go prim t ; exit 0 |
let run ' st = let go _st f = f ( ) in let prim st t = if State . is_go st then begin let name = String . concat " " / ( State . labels st ) in !!% " Testing % s . . . . " @ name ; begin try t ( ) with | e -> !!% " Error : % s : % s . " @ ( String . concat " . " ( State . labels st ) ) ( Exn . to_string e ) ; raise e end ; !!% " Done % s . " @ name ; end in iter st go prim |
let run default_go t = let st = State . from_args default_go in if st . State . config . Config . show_tests then show st t else run ' st t |
let sourceDirs = Node . Path . join [ " | lib " ; " bs " ; " . sourcedirs . json ] " | |
let lock = Xwatcher_util . makeLock ( ) |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.