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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* This library 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; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <https://www.gnu.org/licenses/>.
*
* Sahid Orentino Ferdjaoui <sahid.ferdjaoui@redhat.com>
*/
//! A Rust bindings for libvirt.
//!
//! Libvirt is a portable toolkit to interact with the virtualisation
//! capabilities of Linux, Solaris and other operating systems.
//!
//! The binding tries to be a fairly direct mapping of the underling C
//! API with some differences to respect Rust conventions. So for
//! example C functions related to a domain like: `virDomainCreate`
//! will be mapped in the binding like `dom.create()` or
//! `virDomainPinVcpu` as `dom.pin_vcpu`.
//!
//! The binding uses standard errors handling from Rust. Each method
//! (there are some exceptions) is returning a type `Option` or
//! `Result`.
//!
//! ```
//! use virt::connect::Connect;
//!
//! if let Ok(mut conn) = Connect::open(Some("test:///default")) {
//! assert_eq!(Ok(0), conn.close());
//! }
//! ```
//!
//! Most of the structs are automatically release their references by
//! implemementing `Drop` trait but for structs which are reference
//! counted at C level, it is still possible to explicitly release the
//! reference at Rust level. For instance if a Rust method returns a
//! *Domain, it is possible to call `free` on it when no longer
//! required.
//!
//! ```
//! use virt::connect::Connect;
//! use virt::domain::Domain;
//!
//! if let Ok(mut conn) = Connect::open(Some("test:///default")) {
//! if let Ok(mut dom) = Domain::lookup_by_name(&conn, "myguest") {
//! assert_eq!(Ok(()), dom.free()); // Explicitly releases memory at Rust level.
//! assert_eq!(Ok(0), conn.close());
//! }
//! }
//! ```
//!
//! For each methods accepting or returning a virTypedParameter array
//! a new Rust struct has been defined where each attribute is
//! handling a type Option.
//!
//! ```
//! use virt::connect::Connect;
//! use virt::domain::Domain;
//!
//! if let Ok(mut conn) = Connect::open(Some("test://default")) {
//! if let Ok(dom) = Domain::lookup_by_name(&conn, "myguest") {
//! if let Ok(memp) = dom.get_memory_parameters(0) {
//! if memp.hard_limit.is_some() {
//! println!("hard limit: {}", memp.hard_limit.unwrap())
//! }
//! }
//! }
//! assert_eq!(Ok(0), conn.close());
//! }
//! ```
pub extern crate virt_sys as sys;
// The caller must do 'let _ptr_cleanup = CString::from_raw(ptr)'
// to release the memory associated with the returned pointer.
// Also note it is not valid to use C's free(ptr) call, it must
// be released via the CString API.
// To be used when handling Option<&str> parameters which need
// to be passed to libvirt. General usage pattern is:
//
// pub fn something(foo: Option<&str>) -> Result<int, Error> {
// let foo_buf = some_string_to_cstring!(foo);
// unsafe {
// sys::virConnectSomething(self.as_ptr(),
// some_cstring_to_c_chars!(foo_buf));
// }
// ...
//