From 17c575dcd12f47d984562f8d5d95d81efb9a5d91 Mon Sep 17 00:00:00 2001 From: David Scott Date: Mon, 23 Aug 2010 14:03:02 +0100 Subject: [PATCH] [Tapctl.of_device x] now will throw an exception if [x] is not a device; or if [x] is a device owned by another driver. Signed-off-by: David Scott --- tapctl/tapctl.ml | 18 +++++++++++++++++- tapctl/tapctl.mli | 6 ++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/tapctl/tapctl.ml b/tapctl/tapctl.ml index c54e51f..8cba526 100644 --- a/tapctl/tapctl.ml +++ b/tapctl/tapctl.ml @@ -332,8 +332,24 @@ let is_active ctx t = | [(tapdev,state,Some _ )] -> true | _ -> false +(* We need to be able to check that a given device's major number corresponds to the right driver *) +let read_proc_devices () : (int * string) list = + let parse_line x = match List.filter (fun x -> x <> "") (String.split ' ' x) with + | [x; y] -> (try Some (int_of_string x, y) with _ -> None) + | _ -> None in + List.concat (List.map Opt.to_list ( Unixext.file_lines_fold (fun acc x -> parse_line x :: acc) [] "/proc/devices") ) + +let driver_of_major major = List.assoc major (read_proc_devices ()) + +exception Not_blktap +exception Not_a_device + let of_device ctx path = - let minor = (Unix.stat path).Unix.st_rdev mod 256 in + let stat = Unix.stat path in + if stat.Unix.st_kind <> Unix.S_BLK then raise Not_a_device; + let major = stat.Unix.st_rdev / 256 in + let minor = stat.Unix.st_rdev mod 256 in + if driver_of_major major <> "tapdev" then raise Not_blktap; match List.filter (fun (tapdev, _, _) -> tapdev.minor = minor) (list ctx) with | [ t ] -> t | _ -> raise Not_found diff --git a/tapctl/tapctl.mli b/tapctl/tapctl.mli index 4c8e922..8cd47e4 100644 --- a/tapctl/tapctl.mli +++ b/tapctl/tapctl.mli @@ -33,5 +33,11 @@ val list : ?t:tapdev -> context -> t list val is_paused : context -> tapdev -> bool val is_active : context -> tapdev -> bool +(** Thrown by [of_device x] when [x] is a device not owned by blktap *) +exception Not_blktap + +(** Thrown by [of_device x] when [x] is not a device *) +exception Not_a_device + (** Given a path to a device, return the corresponding tap information *) val of_device : context -> string -> t -- 2.39.5