-
Notifications
You must be signed in to change notification settings - Fork 3
/
libvirt.c
1518 lines (1157 loc) · 42.3 KB
/
libvirt.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "php_libvirt.h"
#include <libvirt/libvirt.h>
#include <libvirt/virterror.h>
//----------------- ZEND thread safe per request globals definition
int le_libvirt_connection;
int le_libvirt_domain;
int le_libvirt_storagepool;
int le_libvirt_volume;
ZEND_DECLARE_MODULE_GLOBALS(libvirt)
//static
ZEND_BEGIN_ARG_INFO_EX(arginfo_libvirt_connect, 0, 0, 0)
ZEND_ARG_INFO(0, url)
ZEND_ARG_INFO(0, readonly)
ZEND_END_ARG_INFO()
static function_entry libvirt_functions[] = {
PHP_FE(libvirt_get_last_error,NULL)
PHP_FE(libvirt_connect, arginfo_libvirt_connect)
PHP_FE(libvirt_get_hostname, NULL)
PHP_FE(libvirt_node_get_info,NULL)
PHP_FE(libvirt_get_active_domain_count, NULL)
PHP_FE(libvirt_get_inactive_domain_count, NULL)
PHP_FE(libvirt_get_domain_count, NULL)
PHP_FE(libvirt_domain_lookup_by_name, NULL)
PHP_FE(libvirt_domain_get_xml_desc, NULL)
PHP_FE(libvirt_domain_get_info, NULL)
PHP_FE(libvirt_domain_get_name, NULL)
PHP_FE(libvirt_domain_get_uuid, NULL)
PHP_FE(libvirt_domain_get_uuid_string, NULL)
PHP_FE(libvirt_list_domains, NULL)
PHP_FE(libvirt_list_active_domains, NULL)
PHP_FE(libvirt_list_defined_domains, NULL)
PHP_FE(libvirt_domain_get_id, NULL)
PHP_FE(libvirt_domain_lookup_by_id, NULL)
PHP_FE(libvirt_domain_lookup_by_uuid, NULL)
PHP_FE(libvirt_domain_lookup_by_uuid_string, NULL)
PHP_FE(libvirt_domain_destroy, NULL)
PHP_FE(libvirt_domain_create, NULL)
PHP_FE(libvirt_domain_resume, NULL)
PHP_FE(libvirt_domain_shutdown, NULL)
PHP_FE(libvirt_domain_suspend, NULL)
PHP_FE(libvirt_domain_undefine, NULL)
PHP_FE(libvirt_domain_reboot, NULL)
PHP_FE(libvirt_domain_define_xml, NULL)
PHP_FE(libvirt_domain_create_xml, NULL)
PHP_FE(libvirt_domain_memory_peek,NULL)
#if LIBVIR_VERSION_NUMBER>=7005
PHP_FE(libvirt_domain_memory_stats,NULL)
#endif
PHP_FE(libvirt_domain_block_stats,NULL)
PHP_FE(libvirt_domain_interface_stats,NULL)
PHP_FE(libvirt_version,NULL)
PHP_FE(libvirt_domain_get_connect, NULL)
PHP_FE(libvirt_domain_migrate, NULL)
PHP_FE(libvirt_domain_migrate_to_uri, NULL)
#if LIBVIR_VERSION_NUMBER>=7007
PHP_FE(libvirt_domain_get_job_info, NULL)
#endif
PHP_FE(libvirt_list_storagepools,NULL)
PHP_FE(libvirt_storagepool_lookup_by_name,NULL)
PHP_FE(libvirt_storagepool_list_volumes,NULL)
PHP_FE(libvirt_storagepool_get_info,NULL)
PHP_FE(libvirt_storagevolume_lookup_by_name,NULL)
PHP_FE(libvirt_storagevolume_get_info,NULL)
PHP_FE(libvirt_storagevolume_get_xml_desc,NULL)
PHP_FE(libvirt_storagevolume_create_xml,NULL)
{NULL, NULL, NULL}
};
//----------------- ZEND module basic definition
zend_module_entry libvirt_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
PHP_LIBVIRT_WORLD_EXTNAME,
libvirt_functions,
PHP_MINIT(libvirt),
PHP_MSHUTDOWN(libvirt),
PHP_RINIT(libvirt),
PHP_RSHUTDOWN(libvirt),
PHP_MINFO(libvirt),
#if ZEND_MODULE_API_NO >= 20010901
PHP_LIBVIRT_WORLD_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_LIBVIRT
ZEND_GET_MODULE(libvirt)
#endif
//----------------- PHP init options
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("libvirt.longlong_to_string", "1", PHP_INI_ALL, OnUpdateBool, longlong_to_string_ini, zend_libvirt_globals, libvirt_globals)
PHP_INI_END()
//PHP requires to have this function defined
static void php_libvirt_init_globals(zend_libvirt_globals *libvirt_globals)
{
libvirt_globals->longlong_to_string_ini = 1;
}
//PHP request initialization
PHP_RINIT_FUNCTION(libvirt)
{
LIBVIRT_G (last_error)=NULL;
return SUCCESS;
}
//PHP request destruction
PHP_RSHUTDOWN_FUNCTION(libvirt)
{
if (LIBVIRT_G (last_error)!=NULL) efree(LIBVIRT_G (last_error));
return SUCCESS;
}
//phpinfo()
PHP_MINFO_FUNCTION(libvirt)
{
unsigned long libVer;
unsigned long typeVer;
char *version;
php_info_print_table_start();
php_info_print_table_row(2, "Libvirt support", "enabled");
php_info_print_table_row(2, "Extension version", PHP_LIBVIRT_WORLD_VERSION);
if (virGetVersion (&libVer,NULL,&typeVer)== 0)
{
version=emalloc(100);
snprintf(version, 100, "%i.%i.%i", (long)((libVer/1000000) % 1000),(long)((libVer/1000) % 1000),(long)(libVer % 1000));
php_info_print_table_row(2, "Libvirt version", version);
}
php_info_print_table_end();
}
//calback error for receiving errors from libvirt. Pass them to PHP and stores for last_error function
catch_error(void *userData, virErrorPtr error)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING,"%s",error->message);
if (LIBVIRT_G (last_error)!=NULL) efree(LIBVIRT_G (last_error));
LIBVIRT_G (last_error)=estrndup(error->message,strlen(error->message));
}
//Destructor for connection resource
static void php_libvirt_connection_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
php_libvirt_connection *conn = (php_libvirt_connection*)rsrc->ptr;
int rv;
rv = virConnectClose(conn->conn);
if (rv!=0)
php_error_docref(NULL TSRMLS_CC, E_WARNING,"virConnectClose failed with %i on destructor",rv);
conn->conn=NULL;
}
//Destructor for domain resource
static void php_libvirt_domain_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
php_libvirt_domain *domain = (php_libvirt_domain*)rsrc->ptr;
int rv;
rv = virDomainFree (domain->domain);
if (rv != 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING,"virDomainFree failed with %i on destructor",rv); }
domain->domain=NULL;
}
//Destructor for storagepool resource
static void php_libvirt_storagepool_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
php_libvirt_storagepool *pool = (php_libvirt_storagepool*)rsrc->ptr;
if (pool != NULL)
{
if (pool->pool != NULL)
{
virStoragePoolFree (pool->pool);
pool->pool=NULL;
}
efree(pool);
}
}
//Destructor for volume resource
static void php_libvirt_volume_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
php_libvirt_volume *volume = (php_libvirt_volume*)rsrc->ptr;
if (volume != NULL)
{
if (volume->volume != NULL)
{
virStorageVolFree (volume->volume);
volume->volume=NULL;
}
efree(volume);
}
}
//ZEND Module inicialization function
PHP_MINIT_FUNCTION(libvirt)
{
le_libvirt_connection = zend_register_list_destructors_ex(php_libvirt_connection_dtor, NULL, PHP_LIBVIRT_CONNECTION_RES_NAME, module_number);
le_libvirt_domain = zend_register_list_destructors_ex(php_libvirt_domain_dtor, NULL, PHP_LIBVIRT_DOMAIN_RES_NAME, module_number); //register resource types and theis descriptors
le_libvirt_storagepool = zend_register_list_destructors_ex(php_libvirt_storagepool_dtor, NULL, PHP_LIBVIRT_STORAGEPOOL_RES_NAME, module_number);
le_libvirt_volume = zend_register_list_destructors_ex(php_libvirt_volume_dtor, NULL, PHP_LIBVIRT_VOLUME_RES_NAME, module_number);
ZEND_INIT_MODULE_GLOBALS(libvirt, php_libvirt_init_globals, NULL);
//LIBVIRT CONSTANTS
REGISTER_LONG_CONSTANT("VIR_DOMAIN_XML_SECURE", 1, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("VIR_DOMAIN_XML_INACTIVE", 2, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("VIR_DOMAIN_NOSTATE", 0, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("VIR_DOMAIN_RUNNING", 1, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("VIR_DOMAIN_BLOCKED", 2, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("VIR_DOMAIN_PAUSED", 3, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("VIR_DOMAIN_SHUTDOWN", 4, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("VIR_DOMAIN_SHUTOFF", 5, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("VIR_DOMAIN_CRASHED", 6, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("VIR_MEMORY_VIRTUAL ", 1, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("VIR_CRED_USERNAME",1, CONST_CS | CONST_PERSISTENT);// : Identity to act as
REGISTER_LONG_CONSTANT("VIR_CRED_AUTHNAME",2,CONST_CS | CONST_PERSISTENT);// : Identify to authorize as
REGISTER_LONG_CONSTANT("VIR_CRED_LANGUAGE",3, CONST_CS | CONST_PERSISTENT);// : RFC 1766 languages, comma separated
REGISTER_LONG_CONSTANT("VIR_CRED_CNONCE",4 , CONST_CS | CONST_PERSISTENT);//: client supplies a nonce
REGISTER_LONG_CONSTANT("VIR_CRED_PASSPHRASE",5, CONST_CS | CONST_PERSISTENT);// : Passphrase secret
REGISTER_LONG_CONSTANT("VIR_CRED_ECHOPROMPT",6, CONST_CS | CONST_PERSISTENT);// : Challenge response
REGISTER_LONG_CONSTANT("VIR_CRED_NOECHOPROMP",7, CONST_CS | CONST_PERSISTENT);// : Challenge response
REGISTER_LONG_CONSTANT("VIR_CRED_REALM",8, CONST_CS | CONST_PERSISTENT);// : Authentication realm
REGISTER_LONG_CONSTANT("VIR_CRED_EXTERNAL",9, CONST_CS | CONST_PERSISTENT);// : Externally managed credential More may be added - expect the unexpected
REGISTER_LONG_CONSTANT("VIR_DOMAIN_MEMORY_STAT_SWAP_IN",0,CONST_CS | CONST_PERSISTENT);// : The total amount of memory written out to swap space (in kB).
REGISTER_LONG_CONSTANT("VIR_DOMAIN_MEMORY_STAT_SWAP_OUT",1, CONST_CS | CONST_PERSISTENT);// : * Page faults occur when a process makes a valid access to virtual memory * that is not available. When servicing the page fault, if disk IO is * required, it is considered a major fault. If not, it is a minor fault. * These are expressed as the number of faults that have occurred. *
REGISTER_LONG_CONSTANT("VIR_DOMAIN_MEMORY_STAT_MAJOR_FAULT",2, CONST_CS | CONST_PERSISTENT);//
REGISTER_LONG_CONSTANT("VIR_DOMAIN_MEMORY_STAT_MINOR_FAULT",3,CONST_CS | CONST_PERSISTENT);// : * The amount of memory left completely unused by the system. Memory that * is available but used for reclaimable caches should NOT be reported as * free. This value is expressed in kB. *
REGISTER_LONG_CONSTANT("VIR_DOMAIN_MEMORY_STAT_UNUSED",4,CONST_CS | CONST_PERSISTENT);// : * The total amount of usable memory as seen by the domain. This value * may be less than the amount of memory assigned to the domain if a * balloon driver is in use or if the guest OS does not initialize all * assigned pages. This value is expressed in kB. *
REGISTER_LONG_CONSTANT("VIR_DOMAIN_MEMORY_STAT_AVAILABLE",5,CONST_CS | CONST_PERSISTENT);// : * The number of statistics supported by this version of the interface. * To add new statistics, add them to the enum and increase this value. *
REGISTER_LONG_CONSTANT("VIR_DOMAIN_MEMORY_STAT_NR",6,CONST_CS | CONST_PERSISTENT);//
REGISTER_LONG_CONSTANT("VIR_DOMAIN_JOB_NONE",0,CONST_CS | CONST_PERSISTENT); // No job is active
REGISTER_LONG_CONSTANT("VIR_DOMAIN_JOB_BOUNDED",1,CONST_CS | CONST_PERSISTENT); //Job with a finite completion time
REGISTER_LONG_CONSTANT("VIR_DOMAIN_JOB_UNBOUNDED",2,CONST_CS | CONST_PERSISTENT); // Job without a finite completion time
REGISTER_LONG_CONSTANT("VIR_DOMAIN_JOB_COMPLETED", 3,CONST_CS | CONST_PERSISTENT); // Job has finished, but isn't cleaned up
REGISTER_LONG_CONSTANT("VIR_DOMAIN_JOB_FAILED",4,CONST_CS | CONST_PERSISTENT); //Job hit error, but isn't cleaned up
REGISTER_LONG_CONSTANT("VIR_DOMAIN_JOB_CANCELLED",5,CONST_CS | CONST_PERSISTENT); // Job was aborted, but isn't cleaned up
REGISTER_LONG_CONSTANT("VIR_MIGRATE_LIVE",1,CONST_CS | CONST_PERSISTENT); // live migration
REGISTER_LONG_CONSTANT("VIR_MIGRATE_PEER2PEER",2,CONST_CS | CONST_PERSISTENT); // direct source -> dest host control channel Note the less-common spelling that we're stuck with: VIR_MIGRATE_TUNNELLED should be VIR_MIGRATE_TUNNELED
REGISTER_LONG_CONSTANT("VIR_MIGRATE_TUNNELLED",4,CONST_CS | CONST_PERSISTENT); // tunnel migration data over libvirtd connection
REGISTER_LONG_CONSTANT("VIR_MIGRATE_PERSIST_DEST",8,CONST_CS | CONST_PERSISTENT); // persist the VM on the destination
REGISTER_LONG_CONSTANT("VIR_MIGRATE_UNDEFINE_SOURCE",16,CONST_CS | CONST_PERSISTENT); // undefine the VM on the source
REGISTER_LONG_CONSTANT("VIR_MIGRATE_PAUSED",32,CONST_CS | CONST_PERSISTENT); // pause on remote side
REGISTER_LONG_CONSTANT("VIR_MIGRATE_NON_SHARED_DISK",64,CONST_CS | CONST_PERSISTENT); // migration with non-shared storage with full disk copy
REGISTER_LONG_CONSTANT("VIR_MIGRATE_NON_SHARED_INC",128,CONST_CS | CONST_PERSISTENT); // migration with non-shared storage with incremental copy (same base image shared between source and destination)
REGISTER_INI_ENTRIES();
//Initialize libvirt and set up error callback
virInitialize();
virSetErrorFunc(NULL, catch_error);
return SUCCESS;
}
//ZEND module destruction
PHP_MSHUTDOWN_FUNCTION(libvirt)
{
UNREGISTER_INI_ENTRIES();
//return error callback back to default (outouts to STDOUT)
virSetErrorFunc(NULL, NULL);
return SUCCESS;
}
//Macros for obtaining resources from arguments
#define GET_CONNECTION_FROM_ARGS(args, ...) \
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, args, __VA_ARGS__) == FAILURE) {\
RETURN_FALSE;\
}\
\
ZEND_FETCH_RESOURCE(conn, php_libvirt_connection*, &zconn, -1, PHP_LIBVIRT_CONNECTION_RES_NAME, le_libvirt_connection);\
if ((conn==NULL) || (conn->conn==NULL)) RETURN_FALSE;\
#define GET_DOMAIN_FROM_ARGS(args, ...) \
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, args, __VA_ARGS__) == FAILURE) {\
RETURN_FALSE;\
}\
\
ZEND_FETCH_RESOURCE(domain, php_libvirt_domain*, &zdomain, -1, PHP_LIBVIRT_DOMAIN_RES_NAME, le_libvirt_domain);\
if ((domain==NULL) || (domain->domain==NULL)) RETURN_FALSE;\
#define GET_STORAGEPOOL_FROM_ARGS(args, ...) \
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, args, __VA_ARGS__) == FAILURE) {\
RETURN_FALSE;\
}\
\
ZEND_FETCH_RESOURCE(pool, php_libvirt_storagepool*, &zpool, -1, PHP_LIBVIRT_STORAGEPOOL_RES_NAME, le_libvirt_storagepool);\
if ((pool==NULL) || (pool->pool==NULL)) RETURN_FALSE;\
#define GET_VOLUME_FROM_ARGS(args, ...) \
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, args, __VA_ARGS__) == FAILURE) {\
RETURN_FALSE;\
}\
\
ZEND_FETCH_RESOURCE(volume, php_libvirt_volume*, &zvolume, -1, PHP_LIBVIRT_VOLUME_RES_NAME, le_libvirt_volume);\
if ((volume==NULL) || (volume->volume==NULL)) RETURN_FALSE;\
//Macro to "recreate" string with emalloc and free the original one
#define RECREATE_STRING_WITH_E(str_out, str_in) \
str_out = estrndup(str_in, strlen(str_in)); \
free(str_in); \
#define LONGLONG_INIT \
char tmpnumber[64];
#define LONGLONG_ASSOC(out,key,in) \
if (LIBVIRT_G(longlong_to_string_ini)) { \
snprintf(tmpnumber,63,"%llu",in); \
add_assoc_string(out,key,tmpnumber,1); \
} \
else \
{ \
add_assoc_long(out,key,in); \
}
#define LONGLONG_INDEX(out,key,in) \
if (LIBVIRT_G(longlong_to_string_ini)) { \
snprintf(tmpnumber,63,"%llu",in); \
add_index_string(out,key,tmpnumber,1); \
} \
else \
{ \
add_index_long(out, key,in); \
}
//Authentication callback function. Should receive list of credentials via cbdata and pass the requested one to libvirt
static int libvirt_virConnectAuthCallback(virConnectCredentialPtr cred, unsigned int ncred, void *cbdata)
{
int i,j;
php_libvirt_cred_value *creds=(php_libvirt_cred_value*) cbdata;
for(i=0;i<ncred;i++)
{
//printf ("Cred %i: type %i, prompt %s challenge %s\n",i,cred[i].type,cred[i].prompt,cred[i].challenge);
if (creds != NULL)
for (j=0;j<creds[0].count;j++)
{
if (creds[j].type==cred[i].type)
{
cred[i].resultlen=creds[j].resultlen;
cred[i].result=malloc(creds[j].resultlen);
strncpy(cred[i].result,creds[j].result,creds[j].resultlen);
}
}
//printf ("Result: %s (%i)\n",cred[i].result,cred[i].resultlen);
}
}
static int libvirt_virConnectCredType[] = {
VIR_CRED_AUTHNAME,
VIR_CRED_ECHOPROMPT,
VIR_CRED_REALM,
VIR_CRED_PASSPHRASE,
VIR_CRED_NOECHOPROMPT,
// VIR_CRED_EXTERNAL,
};
PHP_FUNCTION(libvirt_connect)
{
php_libvirt_connection *conn;
php_libvirt_cred_value *creds=NULL;
zval* zcreds=NULL;
zval **data;
int i;
int j;
int credscount=0;
virConnectAuth libvirt_virConnectAuth= { libvirt_virConnectCredType, sizeof(libvirt_virConnectCredType)/sizeof(int), libvirt_virConnectAuthCallback,NULL};
char *url=NULL;
int url_len=0;
int readonly=1;
HashTable *arr_hash;
HashPosition pointer;
int array_count;
char *key;int key_len;long index;
unsigned long libVer;
unsigned long typeVer;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sba", &url,&url_len,&readonly,&zcreds) == FAILURE) {
RETURN_FALSE;
}
if (virGetVersion (&libVer,NULL,&typeVer)!= 0)
RETURN_FALSE;
if (libVer<6002)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING,"Only libvirt 0.6.2 and higher supported. Please upgrade your libvirt.");
if (LIBVIRT_G (last_error)!=NULL) efree(LIBVIRT_G (last_error));
LIBVIRT_G (last_error)=estrdup("Only libvirt 0.6.2 and higher supported. Please upgrade your libvirt.");
RETURN_FALSE;
}
conn=emalloc(sizeof(php_libvirt_connection));
if (zcreds==NULL)
{ //connecting without providing authentication
if (readonly)
{
conn->conn = virConnectOpenReadOnly(url);
}
else
{
conn->conn = virConnectOpen(url);
}
}
else
{ //connecting with authentication (using callback)
arr_hash = Z_ARRVAL_P(zcreds);
array_count = zend_hash_num_elements(arr_hash);
credscount=array_count;
creds=emalloc(credscount*sizeof(php_libvirt_cred_value));
j=0;
//parse the input Array and create list of credentials. The list (array) is passed to callback function.
for(zend_hash_internal_pointer_reset_ex(arr_hash, &pointer); zend_hash_get_current_data_ex(arr_hash, (void**) &data, &pointer) == SUCCESS; zend_hash_move_forward_ex(arr_hash, &pointer)) {
if (Z_TYPE_PP(data) == IS_STRING) {
if (zend_hash_get_current_key_ex(arr_hash, &key, &key_len, &index, 0, &pointer) == HASH_KEY_IS_STRING) {
PHPWRITE(key, key_len);
} else {
creds[j].type=index;
creds[j].result=emalloc(Z_STRLEN_PP(data));
creds[j].resultlen=Z_STRLEN_PP(data);
strncpy(creds[j].result,Z_STRVAL_PP(data),Z_STRLEN_PP(data));
j++;
}
}
}
creds[0].count=j;
libvirt_virConnectAuth.cbdata= (void*)creds ;
conn->conn= virConnectOpenAuth (url, &libvirt_virConnectAuth, readonly ? VIR_CONNECT_RO : 0);
for (i=0;i<creds[0].count;i++) efree(creds[i].result);
efree(creds);
}
if (conn->conn == NULL) RETURN_FALSE;
ZEND_REGISTER_RESOURCE(return_value, conn, le_libvirt_connection);
conn->resource_id=Z_LVAL_P(return_value);
}
PHP_FUNCTION(libvirt_get_hostname)
{
//char * virConnectGetHostname (virConnectPtr conn);
php_libvirt_connection *conn=NULL;
zval *zconn;
char *hostname;
char *hostname_out;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zconn) == FAILURE) {
RETURN_FALSE;
}
ZEND_FETCH_RESOURCE(conn, php_libvirt_connection*, &zconn, -1, PHP_LIBVIRT_CONNECTION_RES_NAME, le_libvirt_connection);
if ((conn==NULL) || (conn->conn==NULL)) RETURN_FALSE;
if (conn==NULL) RETURN_FALSE;
if (conn->conn==NULL) RETURN_FALSE;
hostname=virConnectGetHostname(conn->conn);
if (hostname==NULL) RETURN_FALSE;
RECREATE_STRING_WITH_E(hostname_out,hostname);
RETURN_STRING(hostname_out,0);
}
PHP_FUNCTION(libvirt_node_get_info)
{
virNodeInfo info;
php_libvirt_connection *conn=NULL;
zval *zconn;
int retval;
GET_CONNECTION_FROM_ARGS("r",&zconn);
retval=virNodeGetInfo (conn->conn,&info);
if (retval==-1) RETURN_FALSE;
array_init(return_value);
add_assoc_string(return_value, "model", (long)info.model,1);
add_assoc_long(return_value, "memory", (long)info.memory);
add_assoc_long(return_value, "cpus", (long)info.cpus);
add_assoc_long(return_value, "nodes", (long)info.nodes);
add_assoc_long(return_value, "sockets", (long)info.sockets);
add_assoc_long(return_value, "cores", (long)info.cores);
add_assoc_long(return_value, "threads", (long)info.threads);
add_assoc_long(return_value, "mhz", (long)info.mhz);
}
PHP_FUNCTION(libvirt_get_active_domain_count)
{
php_libvirt_connection *conn=NULL;
zval *zconn;
int count;
GET_CONNECTION_FROM_ARGS("r",&zconn);
count=virConnectNumOfDomains (conn->conn);
if (count <0) RETURN_FALSE;
RETURN_LONG(count);
}
PHP_FUNCTION(libvirt_get_inactive_domain_count)
{
php_libvirt_connection *conn=NULL;
zval *zconn;
int count;
GET_CONNECTION_FROM_ARGS("r",&zconn);
count=virConnectNumOfDefinedDomains (conn->conn);
if (count <0) RETURN_FALSE;
RETURN_LONG(count);
}
PHP_FUNCTION(libvirt_get_domain_count)
{
php_libvirt_connection *conn=NULL;
zval *zconn;
int count_defined;
int count_active;
GET_CONNECTION_FROM_ARGS("r",&zconn);
count_defined=virConnectNumOfDefinedDomains (conn->conn);
if (count_defined <0) RETURN_FALSE;
count_active=virConnectNumOfDomains (conn->conn);
if (count_active <0) RETURN_FALSE;
RETURN_LONG(count_defined+count_active);
}
PHP_FUNCTION(libvirt_domain_lookup_by_name)
{
php_libvirt_connection *conn=NULL;
zval *zconn;
int name_len;
char *name=NULL;
virDomainPtr domain=NULL;
php_libvirt_domain *res_domain;
GET_CONNECTION_FROM_ARGS("rs",&zconn,&name,&name_len);
if ( (name == NULL) || (name_len<1)) RETURN_FALSE;
domain=virDomainLookupByName (conn->conn,name);
if (domain==NULL) RETURN_FALSE;
res_domain= emalloc(sizeof(php_libvirt_domain));
res_domain->domain = domain;
res_domain->conn= conn;
ZEND_REGISTER_RESOURCE(return_value, res_domain, le_libvirt_domain);
}
PHP_FUNCTION(libvirt_domain_lookup_by_uuid)
{
php_libvirt_connection *conn=NULL;
zval *zconn;
int uuid_len;
char *uuid=NULL;
virDomainPtr domain=NULL;
php_libvirt_domain *res_domain;
GET_CONNECTION_FROM_ARGS("rs",&zconn,&uuid,&uuid_len);
if ( (uuid == NULL) || (uuid_len<1)) RETURN_FALSE;
domain=virDomainLookupByUUID (conn->conn,uuid);
if (domain==NULL) RETURN_FALSE;
res_domain= emalloc(sizeof(php_libvirt_domain));
res_domain->domain = domain;
res_domain->conn=conn;
ZEND_REGISTER_RESOURCE(return_value, res_domain, le_libvirt_domain);
}
PHP_FUNCTION(libvirt_domain_lookup_by_uuid_string)
{
php_libvirt_connection *conn=NULL;
zval *zconn;
int uuid_len;
char *uuid=NULL;
virDomainPtr domain=NULL;
php_libvirt_domain *res_domain;
GET_CONNECTION_FROM_ARGS("rs",&zconn,&uuid,&uuid_len);
if ( (uuid == NULL) || (uuid_len<1)) RETURN_FALSE;
domain=virDomainLookupByUUIDString (conn->conn,uuid);
if (domain==NULL) RETURN_FALSE;
res_domain= emalloc(sizeof(php_libvirt_domain));
res_domain->domain = domain;
res_domain->conn=conn;
ZEND_REGISTER_RESOURCE(return_value, res_domain, le_libvirt_domain);
}
PHP_FUNCTION(libvirt_domain_lookup_by_id)
{
php_libvirt_connection *conn=NULL;
zval *zconn;
long id;
virDomainPtr domain=NULL;
php_libvirt_domain *res_domain;
GET_CONNECTION_FROM_ARGS("rl",&zconn,&id);
domain=virDomainLookupByID (conn->conn,(int)id);
if (domain==NULL) RETURN_FALSE;
res_domain= emalloc(sizeof(php_libvirt_domain));
res_domain->domain = domain;
res_domain->conn=conn;
ZEND_REGISTER_RESOURCE(return_value, res_domain, le_libvirt_domain);
}
PHP_FUNCTION(libvirt_get_last_error)
{
if (LIBVIRT_G (last_error) == NULL) RETURN_NULL();
RETURN_STRING(LIBVIRT_G (last_error),1);
}
PHP_FUNCTION(libvirt_list_storagepools)
{
php_libvirt_connection *conn=NULL;
zval *zconn;
zval *zdomain;
int count=-1;
int maxids=-1;
int expectedcount=-1;
int *ids;
char **names;
int i;
GET_CONNECTION_FROM_ARGS("r",&zconn);
expectedcount=virConnectNumOfStoragePools(conn->conn);
names=emalloc(expectedcount*sizeof(char *));
count=virConnectListStoragePools(conn->conn,names,expectedcount);
if ((count != expectedcount) || (count<0)) RETURN_FALSE;
array_init(return_value);
for (i=0;i<count;i++)
{
add_next_index_string(return_value, names[i],1);
free(names[i]);
}
efree(names);
}
PHP_FUNCTION(libvirt_storagepool_lookup_by_name)
{
php_libvirt_connection *conn=NULL;
zval *zconn;
zval *zpool;
int name_len;
char *name=NULL;
virStoragePoolPtr pool=NULL;
php_libvirt_storagepool *res_pool;
GET_CONNECTION_FROM_ARGS("rs",&zconn,&name,&name_len);
if ( (name == NULL) || (name_len<1)) RETURN_FALSE;
pool=virStoragePoolLookupByName (conn->conn,name);
if (pool==NULL) RETURN_FALSE;
res_pool = emalloc(sizeof(php_libvirt_storagepool));
res_pool->pool = pool;
ZEND_REGISTER_RESOURCE(return_value, res_pool, le_libvirt_storagepool);
}
PHP_FUNCTION(libvirt_storagepool_list_volumes)
{
php_libvirt_connection *conn=NULL;
php_libvirt_storagepool *pool=NULL;
zval *zconn;
zval *zpool;
char **names=NULL;
int expectedcount=-1;
int i;
int count=-1;
GET_STORAGEPOOL_FROM_ARGS("r",&zpool);
expectedcount=virStoragePoolNumOfVolumes (pool->pool);
names=emalloc(expectedcount*sizeof(char *));
count=virStoragePoolListVolumes(pool->pool,names,expectedcount);
array_init(return_value);
if ((count != expectedcount) || (count<0)) RETURN_FALSE;
for (i=0;i<count;i++)
{
add_next_index_string(return_value, names[i],1);
free(names[i]);
}
efree(names);
}
PHP_FUNCTION(libvirt_storagepool_get_info)
{
php_libvirt_storagepool *pool=NULL;
zval *zpool;
virStoragePoolInfo poolInfo;
int retval;
GET_STORAGEPOOL_FROM_ARGS("r",&zpool);
retval=virStoragePoolGetInfo(pool->pool,&poolInfo);
if (retval != 0) RETURN_FALSE;
array_init(return_value);
// @todo: fix the long long returns
add_assoc_long(return_value, "state", (long)poolInfo.state);
add_assoc_long(return_value, "capacity", poolInfo.capacity);
add_assoc_long(return_value, "allocation", poolInfo.allocation);
add_assoc_long(return_value, "available", poolInfo.available);
}
PHP_FUNCTION(libvirt_storagevolume_lookup_by_name)
{
php_libvirt_connection *conn=NULL;
php_libvirt_storagepool *pool=NULL;
php_libvirt_volume *res_volume;
zval *zconn;
zval *zpool;
zval *zvolume;
int name_len;
char *name=NULL;
virStorageVolPtr volume=NULL;
GET_STORAGEPOOL_FROM_ARGS("rs",&zpool,&name,&name_len);
if ( (name == NULL) || (name_len<1)) RETURN_FALSE;
volume=virStorageVolLookupByName (pool->pool,name);
if (volume==NULL) RETURN_FALSE;
res_volume = emalloc(sizeof(php_libvirt_volume));
res_volume->volume = volume;
ZEND_REGISTER_RESOURCE(return_value, res_volume, le_libvirt_volume);
}
PHP_FUNCTION(libvirt_storagevolume_get_info)
{
php_libvirt_volume *volume=NULL;
zval *zvolume;
virStorageVolInfo volumeInfo;
int retval;
GET_VOLUME_FROM_ARGS("r",&zvolume);
retval=virStorageVolGetInfo(volume->volume,&volumeInfo);
if (retval != 0) RETURN_FALSE;
array_init(return_value);
LONGLONG_INIT
add_assoc_long(return_value, "type", (long)volumeInfo.type);
LONGLONG_ASSOC(return_value, "capacity", volumeInfo.capacity);
LONGLONG_ASSOC(return_value, "allocation", volumeInfo.allocation);
}
PHP_FUNCTION(libvirt_storagevolume_get_xml_desc)
{
php_libvirt_volume *volume=NULL;
zval *zvolume;
char *xml;
char *xml_out;
long flags=0;
GET_VOLUME_FROM_ARGS("r|l",&zvolume,&flags);
xml=virStorageVolGetXMLDesc(volume->volume,flags);
if (xml==NULL) RETURN_FALSE;
RECREATE_STRING_WITH_E(xml_out,xml);
RETURN_STRING(xml_out,0);
}
PHP_FUNCTION(libvirt_storagevolume_create_xml)
{
php_libvirt_volume *res_volume=NULL;
php_libvirt_storagepool *pool=NULL;
zval *zpool;
virStorageVolPtr volume=NULL;
char *xml;
int xml_len;
GET_STORAGEPOOL_FROM_ARGS("rs",&zpool,&xml,&xml_len);
volume=virStorageVolCreateXML(pool->pool,xml,0);
if (volume==NULL) RETURN_FALSE;
res_volume= emalloc(sizeof(php_libvirt_volume));
res_volume->volume = volume;
ZEND_REGISTER_RESOURCE(return_value, res_volume, le_libvirt_volume);
}
PHP_FUNCTION(libvirt_list_domains)
{
php_libvirt_connection *conn=NULL;
zval *zconn;
zval *zdomain;
int count=-1;
int maxids=-1;
int expectedcount=-1;
int *ids;
char **names;
int i;
virDomainPtr domain=NULL;
php_libvirt_domain *res_domain;
GET_CONNECTION_FROM_ARGS("r",&zconn);
expectedcount=virConnectNumOfDomains (conn->conn);
ids=emalloc(sizeof(int)*expectedcount);
count=virConnectListDomains (conn->conn,ids,expectedcount);
if ((count != expectedcount) || (count<0)) RETURN_FALSE;
array_init(return_value);
for (i=0;i<count;i++)
{
domain=virDomainLookupByID (conn->conn,ids[i]);
if (domain!=NULL)
{
res_domain= emalloc(sizeof(php_libvirt_domain));
res_domain->domain = domain;
ALLOC_INIT_ZVAL(zdomain);
res_domain->conn=conn;
ZEND_REGISTER_RESOURCE(zdomain, res_domain, le_libvirt_domain);
add_next_index_zval(return_value, zdomain);
}
}
efree(ids);
expectedcount=virConnectNumOfDefinedDomains (conn->conn);
names=emalloc(expectedcount*sizeof(char *));
count=virConnectListDefinedDomains (conn->conn,names ,expectedcount);
if ((count != expectedcount) || (count<0)) RETURN_FALSE;
for (i=0;i<count;i++)
{
domain=virDomainLookupByName (conn->conn,names[i]);
if (domain!=NULL)
{
res_domain= emalloc(sizeof(php_libvirt_domain));
res_domain->domain = domain;
ALLOC_INIT_ZVAL(zdomain);
res_domain->conn=conn;
ZEND_REGISTER_RESOURCE(zdomain, res_domain, le_libvirt_domain);
add_next_index_zval(return_value, zdomain);
}
free(names[i]);
}
efree(names);
}
PHP_FUNCTION(libvirt_list_active_domains)
{
php_libvirt_connection *conn=NULL;
zval *zconn;
zval *zdomain;
int count=-1;
int maxids=-1;
int expectedcount=-1;
int *ids;
char **names;
int i;
virDomainPtr domain=NULL;
php_libvirt_domain *res_domain;
GET_CONNECTION_FROM_ARGS("r",&zconn);
expectedcount=virConnectNumOfDomains (conn->conn);
ids=emalloc(sizeof(int)*expectedcount);
count=virConnectListDomains (conn->conn,ids,expectedcount);
if ((count != expectedcount) || (count<0)) RETURN_FALSE;
array_init(return_value);
for (i=0;i<count;i++)
{
add_next_index_long(return_value, ids[i]);
}
efree(ids);
}
PHP_FUNCTION(libvirt_list_defined_domains)
{
php_libvirt_connection *conn=NULL;
zval *zconn;
zval *zdomain;
int count=-1;
int maxids=-1;
int expectedcount=-1;
int *ids;
char **names;
int i;
virDomainPtr domain=NULL;
php_libvirt_domain *res_domain;
GET_CONNECTION_FROM_ARGS("r",&zconn);
array_init(return_value);
expectedcount=virConnectNumOfDefinedDomains (conn->conn);
names=emalloc(expectedcount*sizeof(char *));
count=virConnectListDefinedDomains (conn->conn,names ,expectedcount);
if ((count != expectedcount) || (count<0)) RETURN_FALSE;
for (i=0;i<count;i++)
{
add_next_index_string(return_value, names[i],1);
free(names[i]);