~ubuntu-branches/ubuntu/quantal/xen-api/quantal

« back to all changes in this revision

Viewing changes to ocaml/xenops/xenvmlib.ml

  • Committer: Package Import Robot
  • Author(s): Jon Ludlam
  • Date: 2011-07-07 21:50:18 UTC
  • Revision ID: package-import@ubuntu.com-20110707215018-3t9ekbh7qy5y2b1p
Tags: upstream-1.3
ImportĀ upstreamĀ versionĀ 1.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
(*
 
2
 * Copyright (C) 2006-2009 Citrix Systems Inc.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU Lesser General Public License as published
 
6
 * by the Free Software Foundation; version 2.1 only. with the special
 
7
 * exception on linking described in file LICENSE.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU Lesser General Public License for more details.
 
13
 *)
 
14
(* api to connect/start mini monitor *)
 
15
 
 
16
(** xenvm library *)
 
17
 
 
18
open Stringext
 
19
 
 
20
type answer = Ok | Error of string | Msg of string | Unknown of string
 
21
 
 
22
let string_of_answer answer =
 
23
        match answer with
 
24
        | Ok        -> "CMD_OK"
 
25
        | Error s   -> "CMD_ERROR " ^ s
 
26
        | Msg s     -> "CMD_MSG " ^ s
 
27
        | Unknown s -> s
 
28
 
 
29
let answer_of_string s =
 
30
        if s = "CMD_OK" then
 
31
                Ok
 
32
        else if String.startswith "CMD_ERROR " s then
 
33
                Error (String.sub s 10 (String.length s - 10))
 
34
        else if String.startswith "CMD_MSG " s then
 
35
                Msg (String.sub s 8 (String.length s - 8))
 
36
        else
 
37
                Unknown s
 
38
 
 
39
let dowrite fd buf =
 
40
        let len = String.length buf in
 
41
        let left = ref len in
 
42
        while !left > 0
 
43
        do
 
44
                let wr = Unix.write fd buf (len - !left) (!left) in
 
45
                left := !left - wr
 
46
        done
 
47
 
 
48
let doread fd =
 
49
        let b = Buffer.create 1024 in
 
50
        let buf = String.create 1024 in
 
51
        let quit = ref false in
 
52
        while not !quit
 
53
        do
 
54
                let rd = Unix.read fd buf 0 1024 in
 
55
                if rd = 0 then
 
56
                        quit := true
 
57
                else
 
58
                        Buffer.add_substring b buf 0 rd
 
59
        done;
 
60
        Buffer.contents b
 
61
 
 
62
(** request s from a running xenvm identified by id (uuid or name) *)
 
63
let request id s =
 
64
        let filename = Printf.sprintf "/var/lib/xenvm/vm-%s" id in
 
65
        let fd = Unix.socket Unix.PF_UNIX Unix.SOCK_STREAM 0 in
 
66
        Unix.connect fd (Unix.ADDR_UNIX filename);
 
67
 
 
68
        dowrite fd (s ^ "!");
 
69
        let answer = doread fd in
 
70
        Unix.close fd;
 
71
 
 
72
        answer_of_string answer