-
Notifications
You must be signed in to change notification settings - Fork 3
/
host.c
145 lines (116 loc) · 3.39 KB
/
host.c
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
#include <stdio.h>
#include <string.h>
#include <ruby.h>
#include <vix.h>
#include "functions.h"
/* _connect(user,pass,type): async connect */
VALUE
_connect(VALUE self, VALUE rhosttype, VALUE rhostname, VALUE rport, VALUE ruser, VALUE rpass)
{
VixHandle job = 0;
VixHandle session = VIX_INVALID_HANDLE;
VixError err;
char *hostname,*pass,*user,str[200];
int hosttype,port;
hosttype = NUM2INT(rhosttype);
hostname = (rhostname!=Qnil)?StringValueCStr(rhostname):NULL;
port = (rport!=Qnil)?NUM2INT(rport):0;
user = (ruser!=Qnil)?StringValueCStr(ruser):NULL;
pass = (rpass!=Qnil)?StringValueCStr(rpass):NULL;
//fprintf(stderr,"calling connect(ht:%d,host:%s,port:%d,user:%s,pass.length:%d);\n",hosttype,hostname,port,user,(pass)?strlen(pass):0);
/* Start the async connect */
job = VixHost_Connect(VIX_API_VERSION,
hosttype,hostname,port,user,pass,
/*VIX_HOSTOPTION_USE_EVENT_PUMP,*/
0, VIX_INVALID_HANDLE, NULL,NULL);
/* Wait on connect */
err = VixJob_Wait(job, VIX_PROPERTY_JOB_RESULT_HANDLE, &session, VIX_PROPERTY_NONE);
Vix_ReleaseHandle(job);
if(VIX_FAILED(err) || session == VIX_INVALID_HANDLE)
{
fprintf(stderr,"Failed to connect to vmware host machine: %s\n",
Vix_GetErrorText(err,NULL));
return Qnil;
}
sprintf(str,"VixAPI::Handle.new(%d)",session);
return rb_eval_string(str);
}
VALUE
_disconnect(VALUE self, VALUE rhandle)
{
VixHandle host = NUM2INT(rhandle);
VixHost_Disconnect(host);
return Qtrue;
}
VALUE
_open_vmx(VALUE self,VALUE rhosthandle, VALUE rpath)
{
VixHandle host = NUM2INT(rb_iv_get(rhosthandle,"@handle"));
VixHandle job;
VixHandle vm = VIX_INVALID_HANDLE;
VixError err;
char *path = StringValueCStr(rpath);
char str[200];
// fprintf(stderr,"calling vmx_open(%d,\"%s\");\n",session,path);
job = VixVM_Open(host,path,NULL,NULL);
err = VixJob_Wait(job, VIX_PROPERTY_JOB_RESULT_HANDLE, &vm, VIX_PROPERTY_NONE);
Vix_ReleaseHandle(job);
if(VIX_FAILED(err))
{
fprintf(stderr,"Failed open vmx file (@%s): %s\n",path, Vix_GetErrorText(err,NULL));
return Qnil;
}
// fprintf(stderr,"VM handle is %d\n",vm);
sprintf(str,"VixAPI::Handle.new(%d)",(int)vm);
return rb_eval_string(str);
}
static void
VixDiscoveryProc(VixHandle jobHandle, VixEventType eventType, VixHandle moreEventInfo, void *clientData)
{
VALUE arr = (VALUE)clientData;
VixError err = VIX_OK;
char *url = NULL;
VALUE str;
// Check callback event; ignore progress reports.
if (VIX_EVENTTYPE_FIND_ITEM != eventType)
return;
// Found a virtual machine.
err = Vix_GetProperties(moreEventInfo,
VIX_PROPERTY_FOUND_ITEM_LOCATION,
&url,
VIX_PROPERTY_NONE);
if (VIX_OK != err)
{
// Handle the error...
Vix_FreeBuffer(url);
return;
}
// puts(url);
str = rb_str_new2(url);
rb_ary_push(arr, str);
Vix_FreeBuffer(url);
}
VALUE
_find_items(VALUE self, VALUE rhosthandle, VALUE rstype)
{
VixHandle job;
VixHandle host;
int stype;
VixError err;
VALUE arr;
host = NUM2INT(rb_iv_get(rhosthandle,"@handle"));
stype = (rstype!=Qnil)?NUM2INT(rstype):0;
arr = rb_ary_new();
job = VixHost_FindItems(host, stype,
VIX_INVALID_HANDLE, // searchCriteria
-1, // timeout
VixDiscoveryProc,
(void *)arr);
err = VixJob_Wait(job, VIX_PROPERTY_NONE);
if (VIX_FAILED(err)) {
// Handle the error...
printf("ERROR in _find_items: %s\n", Vix_GetErrorText(err, NULL));
}
Vix_ReleaseHandle(job);
return arr;
}