File: nodedevice.c

package info (click to toggle)
ruby-libvirt 0.8.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 832 kB
  • sloc: ansic: 8,052; ruby: 2,541; makefile: 6
file content (283 lines) | stat: -rw-r--r-- 9,566 bytes parent folder | download
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 * nodedevice.c: virNodeDevice methods
 *
 * Copyright (C) 2010 Red Hat Inc.
 * Copyright (C) 2013-2016 Chris Lalancette <clalancette@gmail.com>
 *
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 */

#include <ruby.h>
#include <libvirt/libvirt.h>
#include <libvirt/virterror.h>
#include "common.h"
#include "connect.h"
#include "extconf.h"

static VALUE c_nodedevice;

static void nodedevice_free(void *s)
{
    ruby_libvirt_free_struct(NodeDevice, s);
}

static virNodeDevicePtr nodedevice_get(VALUE n)
{
    ruby_libvirt_get_struct(NodeDevice, n);
}

VALUE ruby_libvirt_nodedevice_new(virNodeDevicePtr n, VALUE conn)
{
    return ruby_libvirt_new_class(c_nodedevice, n, conn, nodedevice_free);
}

/*
 * call-seq:
 *   nodedevice.name -> String
 *
 * Call virNodeDeviceGetName[https://libvirt.org/html/libvirt-libvirt-nodedev.html#virNodeDeviceGetName]
 * to retrieve the name of the node device.
 */
static VALUE libvirt_nodedevice_name(VALUE c)
{
    ruby_libvirt_generate_call_string(virNodeDeviceGetName,
                                      ruby_libvirt_connect_get(c), 0,
                                      nodedevice_get(c));
}

/*
 * call-seq:
 *   nodedevice.parent -> String
 *
 * Call virNodeDeviceGetParent[https://libvirt.org/html/libvirt-libvirt-nodedev.html#virNodeDeviceGetParent]
 * to retrieve the parent of the node device.
 */
static VALUE libvirt_nodedevice_parent(VALUE c)
{
    /* unfortunately we can't use ruby_libvirt_generate_call_string() here
     * because virNodeDeviceGetParent() returns NULL as a valid value (when this
     * device has no parent).  Hand-code it instead
     */

    const char *str;

    str = virNodeDeviceGetParent(nodedevice_get(c));
    if (str == NULL) {
        return Qnil;
    }
    else {
        return rb_str_new2(str);
    }
}

/*
 * call-seq:
 *   nodedevice.num_of_caps -> Fixnum
 *
 * Call virNodeDeviceNumOfCaps[https://libvirt.org/html/libvirt-libvirt-nodedev.html#virNodeDeviceNumOfCaps]
 * to retrieve the number of capabilities of the node device.
 */
static VALUE libvirt_nodedevice_num_of_caps(VALUE c)
{
    ruby_libvirt_generate_call_int(virNodeDeviceNumOfCaps,
                                   ruby_libvirt_connect_get(c),
                                   nodedevice_get(c));
}

/*
 * call-seq:
 *   nodedevice.list_caps -> list
 *
 * Call virNodeDeviceListCaps[https://libvirt.org/html/libvirt-libvirt-nodedev.html#virNodeDeviceListCaps]
 * to retrieve a list of capabilities of the node device.
 */
static VALUE libvirt_nodedevice_list_caps(VALUE c)
{
    int r, num;
    char **names;

    num = virNodeDeviceNumOfCaps(nodedevice_get(c));
    ruby_libvirt_raise_error_if(num < 0, e_RetrieveError,
                                "virNodeDeviceNumOfCaps",
                                ruby_libvirt_connect_get(c));
    if (num == 0) {
        /* if num is 0, don't call virNodeDeviceListCaps function */
        return rb_ary_new2(num);
    }

    names = alloca(sizeof(char *) * num);
    r = virNodeDeviceListCaps(nodedevice_get(c), names, num);
    ruby_libvirt_raise_error_if(r < 0, e_RetrieveError,
                                "virNodeDeviceListCaps",
                                ruby_libvirt_connect_get(c));

    return ruby_libvirt_generate_list(r, names);
}

/*
 * call-seq:
 *   nodedevice.xml_desc(flags=0) -> String
 *
 * Call virNodeDeviceGetXMLDesc[https://libvirt.org/html/libvirt-libvirt-nodedev.html#virNodeDeviceGetXMLDesc]
 * to retrieve the XML for the node device.
 */
static VALUE libvirt_nodedevice_xml_desc(int argc, VALUE *argv, VALUE n)
{
    VALUE flags = RUBY_Qnil;

    rb_scan_args(argc, argv, "01", &flags);

    ruby_libvirt_generate_call_string(virNodeDeviceGetXMLDesc,
                                      ruby_libvirt_connect_get(n),
                                      1, nodedevice_get(n),
                                      ruby_libvirt_value_to_uint(flags));
}

/*
 * call-seq:
 *   nodedevice.detach(driver=nil, flags=0) -> nil
 *
 * Call virNodeDeviceDettach[https://libvirt.org/html/libvirt-libvirt-nodedev.html#virNodeDeviceDettach]
 * to detach the node device from the node.
 */
static VALUE libvirt_nodedevice_detach(int argc, VALUE *argv, VALUE n)
{
    VALUE driver = RUBY_Qnil, flags = RUBY_Qnil;

    rb_scan_args(argc, argv, "02", &driver, &flags);

    if (ruby_libvirt_value_to_uint(flags) != 0 ||
        ruby_libvirt_get_cstring_or_null(driver) != NULL) {
        ruby_libvirt_generate_call_nil(virNodeDeviceDetachFlags,
                                       ruby_libvirt_connect_get(n),
                                       nodedevice_get(n),
                                       ruby_libvirt_get_cstring_or_null(driver),
                                       ruby_libvirt_value_to_uint(flags));
    } else {
        ruby_libvirt_generate_call_nil(virNodeDeviceDettach,
                                       ruby_libvirt_connect_get(n),
                                       nodedevice_get(n));
    }
}

/*
 * call-seq:
 *   nodedevice.reattach -> nil
 *
 * Call virNodeDeviceReAttach[https://libvirt.org/html/libvirt-libvirt-nodedev.html#virNodeDeviceReAttach]
 * to reattach the node device to the node.
 */
static VALUE libvirt_nodedevice_reattach(VALUE n)
{
    ruby_libvirt_generate_call_nil(virNodeDeviceReAttach,
                                   ruby_libvirt_connect_get(n),
                                   nodedevice_get(n));
}

/*
 * call-seq:
 *   nodedevice.reset -> nil
 *
 * Call virNodeDeviceReset[https://libvirt.org/html/libvirt-libvirt-nodedev.html#virNodeDeviceReset]
 * to reset the node device.
 */
static VALUE libvirt_nodedevice_reset(VALUE n)
{
    ruby_libvirt_generate_call_nil(virNodeDeviceReset,
                                   ruby_libvirt_connect_get(n),
                                   nodedevice_get(n));
}

/*
 * call-seq:
 *   nodedevice.destroy -> nil
 *
 * Call virNodeDeviceDestroy[https://libvirt.org/html/libvirt-libvirt-nodedev.html#virNodeDeviceDestroy]
 * to shutdown the node device.
 */
static VALUE libvirt_nodedevice_destroy(VALUE n)
{
    ruby_libvirt_generate_call_nil(virNodeDeviceDestroy,
                                   ruby_libvirt_connect_get(n),
                                   nodedevice_get(n));
}

/*
 * call-seq:
 *   nodedevice.free -> nil
 *
 * Call virNodeDeviceFree[https://libvirt.org/html/libvirt-libvirt-nodedev.html#virNodeDeviceFree]
 * to free the node device object.  After this call the node device object is
 * no longer valid.
 */
static VALUE libvirt_nodedevice_free(VALUE n)
{
    ruby_libvirt_generate_call_free(NodeDevice, n);
}

/*
 * call-seq:
 *   nodedevice.lookup_scsi_host_by_wwn(wwnn, wwpn, flags=0) -> Libvirt::NodeDevice
 *
 * Call virNodeDeviceLookupSCSIHostByWWN[https://libvirt.org/html/libvirt-libvirt-nodedev.html#virNodeDeviceLookupSCSIHostByWWN]
 * to look up a SCSI host by its WWNN and WWPN.
 */
static VALUE libvirt_nodedevice_lookup_scsi_host_by_wwn(int argc, VALUE *argv,
                                                        VALUE n)
{
    VALUE wwnn, wwpn, flags = RUBY_Qnil;
    virNodeDevicePtr nd;

    rb_scan_args(argc, argv, "21", &wwnn, &wwpn, &flags);

    nd = virNodeDeviceLookupSCSIHostByWWN(ruby_libvirt_connect_get(n),
                                          StringValueCStr(wwnn),
                                          StringValueCStr(wwpn),
                                          ruby_libvirt_value_to_uint(flags));
    if (nd == NULL) {
        return Qnil;
    }

    return ruby_libvirt_nodedevice_new(nd, ruby_libvirt_conn_attr(n));
}


/*
 * Class Libvirt::NodeDevice
 */
void ruby_libvirt_nodedevice_init(void)
{
    c_nodedevice = rb_define_class_under(m_libvirt, "NodeDevice", rb_cObject);
    rb_undef_alloc_func(c_nodedevice);
    rb_define_singleton_method(c_nodedevice, "new",
                               ruby_libvirt_new_not_allowed, -1);

    rb_define_attr(c_nodedevice, "connection", 1, 0);

    rb_define_method(c_nodedevice, "name", libvirt_nodedevice_name, 0);
    rb_define_method(c_nodedevice, "parent", libvirt_nodedevice_parent, 0);
    rb_define_method(c_nodedevice, "num_of_caps",
                     libvirt_nodedevice_num_of_caps, 0);
    rb_define_method(c_nodedevice, "list_caps",
                     libvirt_nodedevice_list_caps, 0);
    rb_define_method(c_nodedevice, "xml_desc", libvirt_nodedevice_xml_desc, -1);
    rb_define_method(c_nodedevice, "detach", libvirt_nodedevice_detach, -1);
    rb_define_method(c_nodedevice, "reattach", libvirt_nodedevice_reattach, 0);
    rb_define_method(c_nodedevice, "reset", libvirt_nodedevice_reset, 0);
    rb_define_method(c_nodedevice, "destroy", libvirt_nodedevice_destroy, 0);
    rb_define_method(c_nodedevice, "free", libvirt_nodedevice_free, 0);
    rb_define_method(c_nodedevice, "lookup_scsi_host_by_wwn",
                     libvirt_nodedevice_lookup_scsi_host_by_wwn, -1);
}