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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
|
/*
* qemu_agent.c: interaction with QEMU guest agent
*
* Copyright (C) 2006-2014 Red Hat, Inc.
* Copyright (C) 2006 Daniel P. Berrange
*
* 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
* <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <poll.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <gio/gio.h>
#include "qemu_agent.h"
#include "qemu_domain.h"
#include "viralloc.h"
#include "virlog.h"
#include "virerror.h"
#include "virjson.h"
#include "virfile.h"
#include "virtime.h"
#include "virobject.h"
#include "virstring.h"
#include "virenum.h"
#include "virutil.h"
#define VIR_FROM_THIS VIR_FROM_QEMU
VIR_LOG_INIT("qemu.qemu_agent");
#define LINE_ENDING "\n"
/* We read from QEMU until seeing a \r\n pair to indicate a
* completed reply or event. To avoid memory denial-of-service
* though, we must have a size limit on amount of data we
* buffer. 10 MB is large enough that it ought to cope with
* normal QEMU replies, and small enough that we're not
* consuming unreasonable mem.
*/
#define QEMU_AGENT_MAX_RESPONSE (10 * 1024 * 1024)
typedef struct _qemuAgentMessage qemuAgentMessage;
struct _qemuAgentMessage {
char *txBuffer;
int txOffset;
int txLength;
/* Used by the JSON agent to hold reply / error */
char *rxBuffer;
int rxLength;
void *rxObject;
/* True if rxBuffer / rxObject are ready, or a
* fatal error occurred on the agent channel
*/
bool finished;
/* true for sync command */
bool sync;
/* id of the issued sync command */
unsigned long long id;
bool first;
};
struct _qemuAgent {
virObjectLockable parent;
virCond notify;
int fd;
GMainContext *context;
GSocket *socket;
GSource *watch;
bool running;
bool inSync;
virDomainObj *vm;
qemuAgentCallbacks *cb;
/* If there's a command being processed this will be
* non-NULL */
qemuAgentMessage *msg;
/* Buffer incoming data ready for agent
* code to process & find message boundaries */
size_t bufferOffset;
size_t bufferLength;
char *buffer;
/* If anything went wrong, this will be fed back
* the next agent msg */
virError lastError;
/* Some guest agent commands don't return anything
* but fire up an event on qemu agent instead.
* Take that as indication of successful completion */
qemuAgentEvent await_event;
int timeout;
};
static virClass *qemuAgentClass;
static void qemuAgentDispose(void *obj);
static int qemuAgentOnceInit(void)
{
if (!VIR_CLASS_NEW(qemuAgent, virClassForObjectLockable()))
return -1;
return 0;
}
VIR_ONCE_GLOBAL_INIT(qemuAgent);
static void qemuAgentDispose(void *obj)
{
qemuAgent *agent = obj;
VIR_DEBUG("agent=%p", agent);
if (agent->vm)
virObjectUnref(agent->vm);
virCondDestroy(&agent->notify);
g_free(agent->buffer);
g_main_context_unref(agent->context);
virResetError(&agent->lastError);
}
static int
qemuAgentOpenUnix(const char *socketpath)
{
struct sockaddr_un addr;
int agentfd;
if ((agentfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
virReportSystemError(errno,
"%s", _("failed to create socket"));
return -1;
}
if (virSetCloseExec(agentfd) < 0) {
virReportSystemError(errno, "%s",
_("Unable to set agent "
"close-on-exec flag"));
goto error;
}
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
if (virStrcpyStatic(addr.sun_path, socketpath) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Socket path %1$s too big for destination"), socketpath);
goto error;
}
if (connect(agentfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
virReportSystemError(errno, "%s",
_("failed to connect to agent socket"));
goto error;
}
return agentfd;
error:
VIR_FORCE_CLOSE(agentfd);
return -1;
}
static int
qemuAgentIOProcessEvent(qemuAgent *agent,
virJSONValue *obj)
{
const char *type;
VIR_DEBUG("agent=%p obj=%p", agent, obj);
type = virJSONValueObjectGetString(obj, "event");
if (!type) {
VIR_WARN("missing event type in message");
errno = EINVAL;
return -1;
}
return 0;
}
static int
qemuAgentIOProcessLine(qemuAgent *agent,
const char *line,
qemuAgentMessage *msg)
{
g_autoptr(virJSONValue) obj = NULL;
VIR_DEBUG("Line [%s]", line);
if (!(obj = virJSONValueFromString(line))) {
/* receiving garbage on first sync is regular situation */
if (msg && msg->sync && msg->first) {
VIR_DEBUG("Received garbage on sync");
msg->finished = true;
return 0;
}
return -1;
}
if (virJSONValueGetType(obj) != VIR_JSON_TYPE_OBJECT) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Parsed JSON reply '%1$s' isn't an object"), line);
return -1;
}
if (virJSONValueObjectHasKey(obj, "QMP")) {
return 0;
} else if (virJSONValueObjectHasKey(obj, "event")) {
return qemuAgentIOProcessEvent(agent, obj);
} else if (virJSONValueObjectHasKey(obj, "error") ||
virJSONValueObjectHasKey(obj, "return")) {
if (msg) {
if (msg->sync) {
unsigned long long id;
if (virJSONValueObjectGetNumberUlong(obj, "return", &id) < 0) {
VIR_DEBUG("Ignoring delayed reply on sync");
return 0;
}
VIR_DEBUG("Guest returned ID: %llu", id);
if (msg->id != id) {
VIR_DEBUG("Guest agent returned ID: %llu instead of %llu",
id, msg->id);
return 0;
}
}
msg->rxObject = g_steal_pointer(&obj);
msg->finished = true;
} else {
/* we are out of sync */
VIR_DEBUG("Ignoring delayed reply");
}
return 0;
}
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unknown JSON reply '%1$s'"), line);
return -1;
}
static int qemuAgentIOProcessData(qemuAgent *agent,
char *data,
size_t len,
qemuAgentMessage *msg)
{
int used = 0;
size_t i = 0;
while (used < len) {
char *nl = strstr(data + used, LINE_ENDING);
if (nl) {
int got = nl - (data + used);
for (i = 0; i < strlen(LINE_ENDING); i++)
data[used + got + i] = '\0';
if (qemuAgentIOProcessLine(agent, data + used, msg) < 0)
return -1;
used += got + strlen(LINE_ENDING);
} else {
break;
}
}
VIR_DEBUG("Total used %d bytes out of %zd available in buffer", used, len);
return used;
}
/* This method processes data that has been received
* from the agent. Looking for async events and
* replies/errors.
*/
static int
qemuAgentIOProcess(qemuAgent *agent)
{
int len;
qemuAgentMessage *msg = NULL;
/* See if there's a message ready for reply; that is,
* one that has completed writing all its data.
*/
if (agent->msg && agent->msg->txOffset == agent->msg->txLength)
msg = agent->msg;
len = qemuAgentIOProcessData(agent,
agent->buffer, agent->bufferOffset,
msg);
if (len < 0)
return -1;
if (len < agent->bufferOffset) {
memmove(agent->buffer, agent->buffer + len, agent->bufferOffset - len);
agent->bufferOffset -= len;
} else {
VIR_FREE(agent->buffer);
agent->bufferOffset = agent->bufferLength = 0;
}
if (msg && msg->finished)
virCondBroadcast(&agent->notify);
return len;
}
/*
* Called when the agent is able to write data
* Call this function while holding the agent lock.
*/
static int
qemuAgentIOWrite(qemuAgent *agent)
{
int done;
/* If no active message, or fully transmitted, then no-op */
if (!agent->msg || agent->msg->txOffset == agent->msg->txLength)
return 0;
done = safewrite(agent->fd,
agent->msg->txBuffer + agent->msg->txOffset,
agent->msg->txLength - agent->msg->txOffset);
if (done < 0) {
if (errno == EAGAIN)
return 0;
virReportSystemError(errno, "%s",
_("Unable to write to agent"));
return -1;
}
agent->msg->txOffset += done;
return done;
}
/*
* Called when the agent has incoming data to read
* Call this function while holding the agent lock.
*
* Returns -1 on error, or number of bytes read
*/
static int
qemuAgentIORead(qemuAgent *agent)
{
size_t avail = agent->bufferLength - agent->bufferOffset;
int ret = 0;
if (avail < 1024) {
if (agent->bufferLength >= QEMU_AGENT_MAX_RESPONSE) {
virReportSystemError(ERANGE,
_("No complete agent response found in %1$d bytes"),
QEMU_AGENT_MAX_RESPONSE);
return -1;
}
VIR_REALLOC_N(agent->buffer, agent->bufferLength + 1024);
agent->bufferLength += 1024;
avail += 1024;
}
/* Read as much as we can get into our buffer,
until we block on EAGAIN, or hit EOF */
while (avail > 1) {
int got;
got = read(agent->fd,
agent->buffer + agent->bufferOffset,
avail - 1);
if (got < 0) {
if (errno == EAGAIN)
break;
virReportSystemError(errno, "%s",
_("Unable to read from agent"));
ret = -1;
break;
}
if (got == 0)
break;
ret += got;
avail -= got;
agent->bufferOffset += got;
agent->buffer[agent->bufferOffset] = '\0';
}
return ret;
}
static gboolean
qemuAgentIO(GSocket *socket,
GIOCondition cond,
gpointer opaque);
static void
qemuAgentRegister(qemuAgent *agent)
{
GIOCondition cond = 0;
if (agent->lastError.code == VIR_ERR_OK) {
cond |= G_IO_IN;
if (agent->msg && agent->msg->txOffset < agent->msg->txLength)
cond |= G_IO_OUT;
}
agent->watch = g_socket_create_source(agent->socket,
cond,
NULL);
virObjectRef(agent);
g_source_set_callback(agent->watch,
(GSourceFunc)qemuAgentIO,
agent,
(GDestroyNotify)virObjectUnref);
g_source_attach(agent->watch,
agent->context);
}
static void
qemuAgentUnregister(qemuAgent *agent)
{
if (agent->watch) {
g_source_destroy(agent->watch);
vir_g_source_unref(agent->watch, agent->context);
agent->watch = NULL;
}
}
static void qemuAgentUpdateWatch(qemuAgent *agent)
{
qemuAgentUnregister(agent);
if (agent->socket)
qemuAgentRegister(agent);
}
static gboolean
qemuAgentIO(GSocket *socket G_GNUC_UNUSED,
GIOCondition cond,
gpointer opaque)
{
qemuAgent *agent = opaque;
bool error = false;
bool eof = false;
virObjectRef(agent);
/* lock access to the agent and protect fd */
virObjectLock(agent);
if (agent->fd == -1 || !agent->watch) {
virObjectUnlock(agent);
virObjectUnref(agent);
return G_SOURCE_REMOVE;
}
if (agent->lastError.code != VIR_ERR_OK) {
if (cond & (G_IO_HUP | G_IO_ERR))
eof = true;
error = true;
} else {
if (cond & G_IO_OUT) {
if (qemuAgentIOWrite(agent) < 0)
error = true;
}
if (!error &&
cond & G_IO_IN) {
int got = qemuAgentIORead(agent);
if (got < 0) {
error = true;
} else if (got == 0) {
eof = true;
} else {
/* Ignore hangup/error cond if we read some data, to
* give time for that data to be consumed */
cond = 0;
if (qemuAgentIOProcess(agent) < 0)
error = true;
}
}
if (!error &&
cond & G_IO_HUP) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("End of file from agent socket"));
eof = true;
}
if (!error && !eof &&
cond & G_IO_ERR) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Invalid file descriptor while waiting for agent"));
eof = true;
}
}
if (error || eof) {
if (agent->lastError.code != VIR_ERR_OK) {
/* Already have an error, so clear any new error */
virResetLastError();
} else {
if (virGetLastErrorCode() == VIR_ERR_OK)
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Error while processing agent IO"));
virCopyLastError(&agent->lastError);
virResetLastError();
}
VIR_DEBUG("Error on agent %s", NULLSTR(agent->lastError.message));
/* If IO process resulted in an error & we have a message,
* then wakeup that waiter */
if (agent->msg && !agent->msg->finished) {
agent->msg->finished = true;
virCondSignal(&agent->notify);
}
}
qemuAgentUpdateWatch(agent);
/* We have to unlock to avoid deadlock against command thread,
* but is this safe ? I think it is, because the callback
* will try to acquire the virDomainObj *mutex next */
if (eof) {
void (*eofNotify)(qemuAgent *, virDomainObj *)
= agent->cb->eofNotify;
virDomainObj *vm = agent->vm;
/* Make sure anyone waiting wakes up now */
virCondSignal(&agent->notify);
virObjectUnlock(agent);
virObjectUnref(agent);
VIR_DEBUG("Triggering EOF callback");
(eofNotify)(agent, vm);
} else if (error) {
void (*errorNotify)(qemuAgent *, virDomainObj *)
= agent->cb->errorNotify;
virDomainObj *vm = agent->vm;
/* Make sure anyone waiting wakes up now */
virCondSignal(&agent->notify);
virObjectUnlock(agent);
virObjectUnref(agent);
VIR_DEBUG("Triggering error callback");
(errorNotify)(agent, vm);
} else {
virObjectUnlock(agent);
virObjectUnref(agent);
}
return G_SOURCE_REMOVE;
}
qemuAgent *
qemuAgentOpen(virDomainObj *vm,
const virDomainChrSourceDef *config,
GMainContext *context,
qemuAgentCallbacks *cb)
{
qemuAgent *agent;
g_autoptr(GError) gerr = NULL;
if (!cb || !cb->eofNotify) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("EOF notify callback must be supplied"));
return NULL;
}
if (qemuAgentInitialize() < 0)
return NULL;
if (!(agent = virObjectLockableNew(qemuAgentClass)))
return NULL;
agent->timeout = QEMU_DOMAIN_PRIVATE(vm)->agentTimeout;
agent->fd = -1;
if (virCondInit(&agent->notify) < 0) {
virReportSystemError(errno, "%s",
_("cannot initialize agent condition"));
virObjectUnref(agent);
return NULL;
}
agent->vm = virObjectRef(vm);
agent->cb = cb;
if (config->type != VIR_DOMAIN_CHR_TYPE_UNIX) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("unable to handle agent type: %1$s"),
virDomainChrTypeToString(config->type));
goto cleanup;
}
virObjectUnlock(vm);
agent->fd = qemuAgentOpenUnix(config->data.nix.path);
virObjectLock(vm);
if (agent->fd == -1)
goto cleanup;
agent->context = g_main_context_ref(context);
agent->socket = g_socket_new_from_fd(agent->fd, &gerr);
if (!agent->socket) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unable to create socket object: %1$s"),
gerr->message);
goto cleanup;
}
qemuAgentRegister(agent);
agent->running = true;
VIR_DEBUG("New agent %p fd=%d", agent, agent->fd);
return agent;
cleanup:
qemuAgentClose(agent);
return NULL;
}
static void
qemuAgentNotifyCloseLocked(qemuAgent *agent)
{
if (agent) {
agent->running = false;
/* If there is somebody waiting for a message
* wake him up. No message will arrive anyway. */
if (agent->msg && !agent->msg->finished) {
agent->msg->finished = true;
virCondSignal(&agent->notify);
}
}
}
void
qemuAgentNotifyClose(qemuAgent *agent)
{
if (!agent)
return;
VIR_DEBUG("agent=%p", agent);
VIR_WITH_OBJECT_LOCK_GUARD(agent) {
qemuAgentNotifyCloseLocked(agent);
}
}
void qemuAgentClose(qemuAgent *agent)
{
if (!agent)
return;
VIR_DEBUG("agent=%p", agent);
VIR_WITH_OBJECT_LOCK_GUARD(agent) {
if (agent->socket) {
qemuAgentUnregister(agent);
g_clear_pointer(&agent->socket, g_object_unref);
agent->fd = -1;
}
qemuAgentNotifyCloseLocked(agent);
}
virObjectUnref(agent);
}
#define QEMU_AGENT_WAIT_TIME 5
/**
* qemuAgentSend:
* @agent: agent object
* @msg: Message
* @seconds: number of seconds to wait for the result, it can be either
* -2, -1, 0 or positive.
*
* Send @msg to agent @agent. If @seconds is equal to
* VIR_DOMAIN_QEMU_AGENT_COMMAND_BLOCK(-2), this function will block forever
* waiting for the result. The value of
* VIR_DOMAIN_QEMU_AGENT_COMMAND_DEFAULT(-1) means use default timeout value
* and VIR_DOMAIN_QEMU_AGENT_COMMAND_NOWAIT(0) makes this function return
* immediately without waiting. Any positive value means the number of seconds
* to wait for the result.
*
* Returns: 0 on success,
* -2 on timeout,
* -1 otherwise
*/
static int qemuAgentSend(qemuAgent *agent,
qemuAgentMessage *msg,
int seconds)
{
int ret = -1;
unsigned long long then = 0;
/* Check whether qemu quit unexpectedly */
if (agent->lastError.code != VIR_ERR_OK) {
VIR_DEBUG("Attempt to send command while error is set %s",
NULLSTR(agent->lastError.message));
virSetError(&agent->lastError);
return -1;
}
if (seconds > VIR_DOMAIN_QEMU_AGENT_COMMAND_BLOCK) {
unsigned long long now;
if (virTimeMillisNow(&now) < 0)
return -1;
if (seconds == VIR_DOMAIN_QEMU_AGENT_COMMAND_DEFAULT)
seconds = QEMU_AGENT_WAIT_TIME;
then = now + seconds * 1000ull;
}
agent->msg = msg;
qemuAgentUpdateWatch(agent);
while (!agent->msg->finished) {
if ((then && virCondWaitUntil(&agent->notify, &agent->parent.lock, then) < 0) ||
(!then && virCondWait(&agent->notify, &agent->parent.lock) < 0)) {
if (errno == ETIMEDOUT) {
virReportError(VIR_ERR_AGENT_UNRESPONSIVE, "%s",
_("Guest agent not available for now"));
ret = -2;
} else {
virReportSystemError(errno, "%s",
_("Unable to wait on agent socket "
"condition"));
}
agent->inSync = false;
goto cleanup;
}
}
if (agent->lastError.code != VIR_ERR_OK) {
VIR_DEBUG("Send command resulted in error %s",
NULLSTR(agent->lastError.message));
virSetError(&agent->lastError);
goto cleanup;
}
ret = 0;
cleanup:
agent->msg = NULL;
qemuAgentUpdateWatch(agent);
return ret;
}
/**
* qemuAgentGuestSyncSend:
* @agent: agent object
* @timeout: timeout for the command
* @first: true when this is the first invocation to drain possible leftovers
* from the pipe
*
* Sends a sync request to the guest agent.
* Returns: -1 on error
* 0 on successful send, but when no reply was received
* 1 when a reply was received
*/
static int
qemuAgentGuestSyncSend(qemuAgent *agent,
int timeout,
bool first)
{
g_autofree char *txMsg = NULL;
g_autoptr(virJSONValue) rxObj = NULL;
unsigned long long id;
qemuAgentMessage sync_msg;
int rc;
memset(&sync_msg, 0, sizeof(sync_msg));
if (virTimeMillisNow(&id) < 0)
return -1;
txMsg = g_strdup_printf("{\"execute\":\"guest-sync\", "
"\"arguments\":{\"id\":%llu}}\n", id);
sync_msg.txBuffer = txMsg;
sync_msg.txLength = strlen(txMsg);
sync_msg.sync = true;
sync_msg.id = id;
sync_msg.first = first;
VIR_DEBUG("Sending guest-sync command with ID: %llu", id);
rc = qemuAgentSend(agent, &sync_msg, timeout);
rxObj = g_steal_pointer(&sync_msg.rxObject);
VIR_DEBUG("qemuAgentSend returned: %d", rc);
if (rc < 0)
return -1;
if (rxObj)
return 1;
return 0;
}
/**
* qemuAgentGuestSync:
* @agent: agent object
*
* Send guest-sync with unique ID
* and wait for reply. If we get one, check if
* received ID is equal to given.
*
* Returns: 0 on success,
* -1 otherwise
*/
static int
qemuAgentGuestSync(qemuAgent *agent)
{
int timeout = VIR_DOMAIN_QEMU_AGENT_COMMAND_DEFAULT;
int rc;
if (agent->inSync)
return 0;
/* if user specified a custom agent timeout that is lower than the
* default timeout, use the shorter timeout instead */
if ((agent->timeout >= 0) && (agent->timeout < QEMU_AGENT_WAIT_TIME))
timeout = agent->timeout;
if ((rc = qemuAgentGuestSyncSend(agent, timeout, true)) < 0)
return -1;
/* successfully sync'd */
if (rc == 1) {
agent->inSync = true;
return 0;
}
/* send another sync */
if ((rc = qemuAgentGuestSyncSend(agent, timeout, false)) < 0)
return -1;
/* successfully sync'd */
if (rc == 1) {
agent->inSync = true;
return 0;
}
if (agent->running)
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Missing agent reply object"));
else
virReportError(VIR_ERR_AGENT_UNRESPONSIVE, "%s",
_("Guest agent disappeared while executing command"));
return -1;
}
static const char *
qemuAgentStringifyErrorClass(const char *klass)
{
if (STREQ_NULLABLE(klass, "BufferOverrun"))
return "Buffer overrun";
else if (STREQ_NULLABLE(klass, "CommandDisabled"))
return "The command has been disabled for this instance";
else if (STREQ_NULLABLE(klass, "CommandNotFound"))
return "The command has not been found";
else if (STREQ_NULLABLE(klass, "FdNotFound"))
return "File descriptor not found";
else if (STREQ_NULLABLE(klass, "InvalidParameter"))
return "Invalid parameter";
else if (STREQ_NULLABLE(klass, "InvalidParameterType"))
return "Invalid parameter type";
else if (STREQ_NULLABLE(klass, "InvalidParameterValue"))
return "Invalid parameter value";
else if (STREQ_NULLABLE(klass, "OpenFileFailed"))
return "Cannot open file";
else if (STREQ_NULLABLE(klass, "QgaCommandFailed"))
return "Guest agent command failed";
else if (STREQ_NULLABLE(klass, "QMPBadInputObjectMember"))
return "Bad QMP input object member";
else if (STREQ_NULLABLE(klass, "QMPExtraInputObjectMember"))
return "Unexpected extra object member";
else if (STREQ_NULLABLE(klass, "UndefinedError"))
return "An undefined error has occurred";
else if (STREQ_NULLABLE(klass, "Unsupported"))
return "this feature or command is not currently supported";
else if (klass)
return klass;
else
return "unknown QEMU command error";
}
/* Ignoring OOM in this method, since we're already reporting
* a more important error
*
* XXX see qerror.h for different klasses & fill out useful params
*/
static const char *
qemuAgentStringifyError(virJSONValue *error)
{
const char *klass = virJSONValueObjectGetString(error, "class");
const char *detail = virJSONValueObjectGetString(error, "desc");
/* The QMP 'desc' field is usually sufficient for our generic
* error reporting needs. However, if not present, translate
* the class into something readable.
*/
if (!detail)
detail = qemuAgentStringifyErrorClass(klass);
return detail;
}
static const char *
qemuAgentCommandName(virJSONValue *cmd)
{
const char *name = virJSONValueObjectGetString(cmd, "execute");
if (name)
return name;
return "<unknown>";
}
static int
qemuAgentCheckError(virJSONValue *cmd,
virJSONValue *reply,
bool report_unsupported)
{
if (virJSONValueObjectHasKey(reply, "error")) {
virJSONValue *error = virJSONValueObjectGet(reply, "error");
g_autofree char *cmdstr = virJSONValueToString(cmd, false);
g_autofree char *replystr = virJSONValueToString(reply, false);
/* Log the full JSON formatted command & error */
VIR_DEBUG("unable to execute QEMU agent command %s: %s",
NULLSTR(cmdstr), NULLSTR(replystr));
/* Only send the user the command name + friendly error */
if (!error) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("unable to execute QEMU agent command '%1$s'"),
qemuAgentCommandName(cmd));
return -1;
}
if (!report_unsupported) {
const char *klass = virJSONValueObjectGetString(error, "class");
if (STREQ_NULLABLE(klass, "CommandNotFound") ||
STREQ_NULLABLE(klass, "CommandDisabled"))
return -2;
}
virReportError(VIR_ERR_INTERNAL_ERROR,
_("unable to execute QEMU agent command '%1$s': %2$s"),
qemuAgentCommandName(cmd),
qemuAgentStringifyError(error));
return -1;
}
if (!virJSONValueObjectHasKey(reply, "return")) {
g_autofree char *cmdstr = virJSONValueToString(cmd, false);
g_autofree char *replystr = virJSONValueToString(reply, false);
VIR_DEBUG("Neither 'return' nor 'error' is set in the JSON reply %s: %s",
NULLSTR(cmdstr), NULLSTR(replystr));
virReportError(VIR_ERR_INTERNAL_ERROR,
_("unable to execute QEMU agent command '%1$s'"),
qemuAgentCommandName(cmd));
return -1;
}
return 0;
}
static int
qemuAgentCommandFull(qemuAgent *agent,
virJSONValue *cmd,
virJSONValue **reply,
int seconds,
bool report_unsupported)
{
int ret = -1;
qemuAgentMessage msg;
g_autofree char *cmdstr = NULL;
int await_event = agent->await_event;
*reply = NULL;
memset(&msg, 0, sizeof(msg));
if (!agent->running) {
virReportError(VIR_ERR_AGENT_UNRESPONSIVE, "%s",
_("Guest agent disappeared while executing command"));
goto cleanup;
}
if (qemuAgentGuestSync(agent) < 0)
goto cleanup;
if (!(cmdstr = virJSONValueToString(cmd, false)))
goto cleanup;
msg.txBuffer = g_strdup_printf("%s" LINE_ENDING, cmdstr);
msg.txLength = strlen(msg.txBuffer);
VIR_DEBUG("Send command '%s' for write, seconds = %d", cmdstr, seconds);
ret = qemuAgentSend(agent, &msg, seconds);
VIR_DEBUG("Receive command reply ret=%d rxObject=%p",
ret, msg.rxObject);
if (ret < 0)
goto cleanup;
/* If we haven't obtained any reply but we wait for an
* event, then don't report this as error */
if (!msg.rxObject) {
if (!await_event) {
if (agent->running) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Missing agent reply object"));
} else {
virReportError(VIR_ERR_AGENT_UNRESPONSIVE, "%s",
_("Guest agent disappeared while executing command"));
}
ret = -1;
}
goto cleanup;
}
*reply = msg.rxObject;
ret = qemuAgentCheckError(cmd, *reply, report_unsupported);
cleanup:
VIR_FREE(msg.txBuffer);
agent->await_event = QEMU_AGENT_EVENT_NONE;
return ret;
}
static int
qemuAgentCommand(qemuAgent *agent,
virJSONValue *cmd,
virJSONValue **reply,
int seconds)
{
return qemuAgentCommandFull(agent, cmd, reply, seconds, true);
}
static virJSONValue *G_GNUC_NULL_TERMINATED
qemuAgentMakeCommand(const char *cmdname,
...)
{
g_autoptr(virJSONValue) obj = NULL;
g_autoptr(virJSONValue) jargs = NULL;
va_list args;
va_start(args, cmdname);
if (virJSONValueObjectAddVArgs(&jargs, args) < 0) {
va_end(args);
return NULL;
}
va_end(args);
if (virJSONValueObjectAdd(&obj,
"s:execute", cmdname,
"A:arguments", &jargs,
NULL) < 0)
return NULL;
return g_steal_pointer(&obj);
}
static virJSONValue *
qemuAgentMakeStringsArray(const char **strings, unsigned int len)
{
size_t i;
g_autoptr(virJSONValue) ret = virJSONValueNewArray();
for (i = 0; i < len; i++) {
if (virJSONValueArrayAppendString(ret, strings[i]) < 0)
return NULL;
}
return g_steal_pointer(&ret);
}
void qemuAgentNotifyEvent(qemuAgent *agent,
qemuAgentEvent event)
{
VIR_LOCK_GUARD lock = virObjectLockGuard(agent);
VIR_DEBUG("agent=%p event=%d await_event=%d", agent, event, agent->await_event);
if (agent->await_event == event) {
agent->await_event = QEMU_AGENT_EVENT_NONE;
/* somebody waiting for this event, wake him up. */
if (agent->msg && !agent->msg->finished) {
agent->msg->finished = true;
virCondSignal(&agent->notify);
}
}
}
VIR_ENUM_DECL(qemuAgentShutdownMode);
VIR_ENUM_IMPL(qemuAgentShutdownMode,
QEMU_AGENT_SHUTDOWN_LAST,
"powerdown", "reboot", "halt",
);
int qemuAgentShutdown(qemuAgent *agent,
qemuAgentShutdownMode mode)
{
g_autoptr(virJSONValue) cmd = NULL;
g_autoptr(virJSONValue) reply = NULL;
cmd = qemuAgentMakeCommand("guest-shutdown",
"s:mode", qemuAgentShutdownModeTypeToString(mode),
NULL);
if (!cmd)
return -1;
if (mode == QEMU_AGENT_SHUTDOWN_REBOOT)
agent->await_event = QEMU_AGENT_EVENT_RESET;
else
agent->await_event = QEMU_AGENT_EVENT_SHUTDOWN;
return qemuAgentCommand(agent, cmd, &reply,
VIR_DOMAIN_QEMU_AGENT_COMMAND_SHUTDOWN);
}
/*
* qemuAgentFSFreeze:
* @agent: agent object
* @mountpoints: Array of mountpoint paths to be frozen, or NULL for all
* @nmountpoints: Number of mountpoints to be frozen, or 0 for all
*
* Issue guest-fsfreeze-freeze command to guest agent,
* which freezes file systems mounted on specified mountpoints
* (or all file systems when @mountpoints is NULL), and returns
* number of frozen file systems on success.
*
* Returns: number of file system frozen on success,
* -1 on error.
*/
int qemuAgentFSFreeze(qemuAgent *agent, const char **mountpoints,
unsigned int nmountpoints)
{
int nfrozen = 0;
g_autoptr(virJSONValue) cmd = NULL;
g_autoptr(virJSONValue) reply = NULL;
if (mountpoints && nmountpoints) {
g_autoptr(virJSONValue) arg = qemuAgentMakeStringsArray(mountpoints, nmountpoints);
if (!arg)
return -1;
cmd = qemuAgentMakeCommand("guest-fsfreeze-freeze-list",
"a:mountpoints", &arg, NULL);
} else {
cmd = qemuAgentMakeCommand("guest-fsfreeze-freeze", NULL);
}
if (!cmd)
return -1;
if (qemuAgentCommand(agent, cmd, &reply, agent->timeout) < 0)
return -1;
if (virJSONValueObjectGetNumberInt(reply, "return", &nfrozen) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("malformed return value"));
return -1;
}
return nfrozen;
}
/*
* qemuAgentFSThaw:
* @agent: agent object
*
* Issue guest-fsfreeze-thaw command to guest agent,
* which unfreezes all mounted file systems and returns
* number of thawed file systems on success.
*
* Returns: number of file system thawed on success,
* -1 on error.
*/
int qemuAgentFSThaw(qemuAgent *agent)
{
int nthawed = 0;
g_autoptr(virJSONValue) cmd = qemuAgentMakeCommand("guest-fsfreeze-thaw", NULL);
g_autoptr(virJSONValue) reply = NULL;
if (!cmd)
return -1;
if (qemuAgentCommand(agent, cmd, &reply, agent->timeout) < 0)
return -1;
if (virJSONValueObjectGetNumberInt(reply, "return", &nthawed) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("malformed return value"));
return -1;
}
return nthawed;
}
VIR_ENUM_DECL(qemuAgentSuspendMode);
VIR_ENUM_IMPL(qemuAgentSuspendMode,
VIR_NODE_SUSPEND_TARGET_LAST,
"guest-suspend-ram",
"guest-suspend-disk",
"guest-suspend-hybrid",
);
int
qemuAgentSuspend(qemuAgent *agent,
unsigned int target)
{
g_autoptr(virJSONValue) cmd = NULL;
g_autoptr(virJSONValue) reply = NULL;
cmd = qemuAgentMakeCommand(qemuAgentSuspendModeTypeToString(target),
NULL);
if (!cmd)
return -1;
agent->await_event = QEMU_AGENT_EVENT_SUSPEND;
return qemuAgentCommand(agent, cmd, &reply, agent->timeout);
}
int
qemuAgentArbitraryCommand(qemuAgent *agent,
const char *cmd_str,
char **result,
int timeout)
{
int rc;
g_autoptr(virJSONValue) cmd = NULL;
g_autoptr(virJSONValue) reply = NULL;
*result = NULL;
if (timeout < VIR_DOMAIN_QEMU_AGENT_COMMAND_MIN) {
virReportError(VIR_ERR_INVALID_ARG,
_("guest agent timeout '%1$d' is less than the minimum '%2$d'"),
timeout, VIR_DOMAIN_QEMU_AGENT_COMMAND_MIN);
return -1;
}
if (!(cmd = virJSONValueFromString(cmd_str)))
return -1;
if ((rc = qemuAgentCommand(agent, cmd, &reply, timeout)) < 0)
return rc;
if (!(*result = virJSONValueToString(reply, false)))
return -1;
return rc;
}
int
qemuAgentFSTrim(qemuAgent *agent,
unsigned long long minimum)
{
g_autoptr(virJSONValue) cmd = NULL;
g_autoptr(virJSONValue) reply = NULL;
cmd = qemuAgentMakeCommand("guest-fstrim",
"U:minimum", minimum,
NULL);
if (!cmd)
return -1;
return qemuAgentCommand(agent, cmd, &reply, agent->timeout);
}
int
qemuAgentGetVCPUs(qemuAgent *agent,
qemuAgentCPUInfo **info)
{
size_t i;
g_autoptr(virJSONValue) cmd = NULL;
g_autoptr(virJSONValue) reply = NULL;
virJSONValue *data = NULL;
size_t ndata;
if (!(cmd = qemuAgentMakeCommand("guest-get-vcpus", NULL)))
return -1;
if (qemuAgentCommand(agent, cmd, &reply, agent->timeout) < 0)
return -1;
if (!(data = virJSONValueObjectGetArray(reply, "return"))) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("guest-get-vcpus reply was missing return data"));
return -1;
}
ndata = virJSONValueArraySize(data);
*info = g_new0(qemuAgentCPUInfo, ndata);
for (i = 0; i < ndata; i++) {
virJSONValue *entry = virJSONValueArrayGet(data, i);
qemuAgentCPUInfo *in = *info + i;
if (!entry) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("array element missing in guest-get-vcpus return "
"value"));
return -1;
}
if (virJSONValueObjectGetNumberUint(entry, "logical-id", &in->id) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("'logical-id' missing in reply of guest-get-vcpus"));
return -1;
}
if (virJSONValueObjectGetBoolean(entry, "online", &in->online) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("'online' missing in reply of guest-get-vcpus"));
return -1;
}
in->offlinable = false;
ignore_value(virJSONValueObjectGetBoolean(entry, "can-offline", &in->offlinable));
}
return ndata;
}
/* returns the value provided by the guest agent or -1 on internal error */
static int
qemuAgentSetVCPUsCommand(qemuAgent *agent,
qemuAgentCPUInfo *info,
size_t ninfo,
int *nmodified)
{
g_autoptr(virJSONValue) cmd = NULL;
g_autoptr(virJSONValue) reply = NULL;
g_autoptr(virJSONValue) cpus = virJSONValueNewArray();
size_t i;
int ret;
*nmodified = 0;
for (i = 0; i < ninfo; i++) {
qemuAgentCPUInfo *in = &info[i];
g_autoptr(virJSONValue) cpu = virJSONValueNewObject();
/* don't set state for cpus that were not touched */
if (!in->modified)
continue;
(*nmodified)++;
if (virJSONValueObjectAppendNumberInt(cpu, "logical-id", in->id) < 0)
return -1;
if (virJSONValueObjectAppendBoolean(cpu, "online", in->online) < 0)
return -1;
if (virJSONValueArrayAppend(cpus, &cpu) < 0)
return -1;
}
if (*nmodified == 0)
return 0;
if (!(cmd = qemuAgentMakeCommand("guest-set-vcpus",
"a:vcpus", &cpus,
NULL)))
return -1;
if (qemuAgentCommand(agent, cmd, &reply, agent->timeout) < 0)
return -1;
/* All negative values are invalid. Return of 0 is bogus since we wouldn't
* call the guest agent so that 0 cpus would be set successfully. Reporting
* more successfully set vcpus that we've asked for is invalid. */
if (virJSONValueObjectGetNumberInt(reply, "return", &ret) < 0 ||
ret <= 0 || ret > *nmodified) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("guest agent returned malformed or invalid return value"));
return -1;
}
return ret;
}
/**
* Set the VCPU state using guest agent.
*
* Attempts to set the guest agent state for all cpus or until a proper error is
* reported by the guest agent. This may require multiple calls.
*
* Returns -1 on error, 0 on success.
*/
int
qemuAgentSetVCPUs(qemuAgent *agent,
qemuAgentCPUInfo *info,
size_t ninfo)
{
int rv;
int nmodified;
size_t i;
do {
if ((rv = qemuAgentSetVCPUsCommand(agent, info, ninfo, &nmodified)) < 0)
return -1;
/* all vcpus were set successfully */
if (rv == nmodified)
return 0;
/* un-mark vcpus that were already set */
for (i = 0; i < ninfo && rv > 0; i++) {
if (!info[i].modified)
continue;
info[i].modified = false;
rv--;
}
} while (1);
return 0;
}
/* modify the cpu info structure to set the correct amount of cpus */
int
qemuAgentUpdateCPUInfo(unsigned int nvcpus,
qemuAgentCPUInfo *cpuinfo,
int ncpuinfo)
{
size_t i;
int nonline = 0;
int nofflinable = 0;
ssize_t cpu0 = -1;
/* count the active and offlinable cpus */
for (i = 0; i < ncpuinfo; i++) {
if (cpuinfo[i].id == 0)
cpu0 = i;
if (cpuinfo[i].online)
nonline++;
if (cpuinfo[i].offlinable && cpuinfo[i].online)
nofflinable++;
/* This shouldn't happen, but we can't trust the guest agent */
if (!cpuinfo[i].online && !cpuinfo[i].offlinable) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Invalid data provided by guest agent"));
return -1;
}
}
/* CPU0 was made offlinable in linux a while ago, but certain parts (suspend
* to ram) of the kernel still don't cope well with that. Make sure that if
* all remaining vCPUs are offlinable, vCPU0 will not be selected to be
* offlined automatically */
if (nofflinable == nonline && cpu0 >= 0 && cpuinfo[cpu0].online) {
cpuinfo[cpu0].offlinable = false;
nofflinable--;
}
/* the guest agent reported less cpus than requested */
if (nvcpus > ncpuinfo) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("guest agent reports less cpu than requested"));
return -1;
}
/* not enough offlinable CPUs to support the request */
if (nvcpus < nonline - nofflinable) {
virReportError(VIR_ERR_INVALID_ARG, "%s",
_("Cannot offline enough CPUs"));
return -1;
}
for (i = 0; i < ncpuinfo; i++) {
if (nvcpus < nonline) {
/* unplug */
if (cpuinfo[i].offlinable && cpuinfo[i].online) {
cpuinfo[i].online = false;
cpuinfo[i].modified = true;
nonline--;
}
} else if (nvcpus > nonline) {
/* plug */
if (!cpuinfo[i].online) {
cpuinfo[i].online = true;
cpuinfo[i].modified = true;
nonline++;
}
} else {
/* done */
break;
}
}
return 0;
}
/**
* qemuAgentGetHostname:
*
* Gets the guest hostname using the guest agent.
*
* Returns 0 on success and fills @hostname. On error -1 is returned with an
* error reported and if '@report_unsupported' is false -2 is returned if the
* guest agent does not support the command without reporting an error
*/
int
qemuAgentGetHostname(qemuAgent *agent,
char **hostname,
bool report_unsupported)
{
g_autoptr(virJSONValue) cmd = qemuAgentMakeCommand("guest-get-host-name", NULL);
g_autoptr(virJSONValue) reply = NULL;
virJSONValue *data = NULL;
const char *result = NULL;
int rc;
if (!cmd)
return -1;
if ((rc = qemuAgentCommandFull(agent, cmd, &reply, agent->timeout,
report_unsupported)) < 0)
return rc;
if (!(data = virJSONValueObjectGet(reply, "return"))) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("malformed return value"));
return -1;
}
if (!(result = virJSONValueObjectGetString(data, "host-name"))) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("'host-name' missing in guest-get-host-name reply"));
return -1;
}
*hostname = g_strdup(result);
return 0;
}
int
qemuAgentGetTime(qemuAgent *agent,
long long *seconds,
unsigned int *nseconds)
{
unsigned long long json_time;
g_autoptr(virJSONValue) cmd = NULL;
g_autoptr(virJSONValue) reply = NULL;
cmd = qemuAgentMakeCommand("guest-get-time",
NULL);
if (!cmd)
return -1;
if (qemuAgentCommand(agent, cmd, &reply, agent->timeout) < 0)
return -1;
if (virJSONValueObjectGetNumberUlong(reply, "return", &json_time) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("malformed return value"));
return -1;
}
/* guest agent returns time in nanoseconds,
* we need it in seconds here */
*seconds = json_time / 1000000000LL;
*nseconds = json_time % 1000000000LL;
return 0;
}
/**
* qemuAgentSetTime:
* @setTime: time to set
* @sync: let guest agent to read domain's RTC (@setTime is ignored)
*/
int
qemuAgentSetTime(qemuAgent *agent,
long long seconds,
unsigned int nseconds,
bool rtcSync)
{
g_autoptr(virJSONValue) cmd = NULL;
g_autoptr(virJSONValue) reply = NULL;
if (rtcSync) {
cmd = qemuAgentMakeCommand("guest-set-time", NULL);
} else {
/* guest agent expect time with nanosecond granularity.
* Impressing. */
long long json_time;
/* Check if we overflow. For some reason qemu doesn't handle unsigned
* long long on the agent well as it silently truncates numbers to
* signed long long. Therefore we must check overflow against LLONG_MAX
* not ULLONG_MAX. */
if (seconds > LLONG_MAX / 1000000000LL) {
virReportError(VIR_ERR_INVALID_ARG,
_("Time '%1$lld' is too big for guest agent"),
seconds);
return -1;
}
json_time = seconds * 1000000000LL;
json_time += nseconds;
cmd = qemuAgentMakeCommand("guest-set-time",
"I:time", json_time,
NULL);
}
if (!cmd)
return -1;
if (qemuAgentCommand(agent, cmd, &reply, agent->timeout) < 0)
return -1;
return 0;
}
void
qemuAgentDiskAddressFree(qemuAgentDiskAddress *info)
{
if (!info)
return;
g_free(info->serial);
g_free(info->bus_type);
g_free(info->devnode);
g_free(info->ccw_addr);
g_free(info);
}
void
qemuAgentDiskInfoFree(qemuAgentDiskInfo *info)
{
if (!info)
return;
g_free(info->name);
g_strfreev(info->dependencies);
qemuAgentDiskAddressFree(info->address);
g_free(info->alias);
g_free(info);
}
void
qemuAgentFSInfoFree(qemuAgentFSInfo *info)
{
size_t i;
if (!info)
return;
g_free(info->mountpoint);
g_free(info->name);
g_free(info->fstype);
for (i = 0; i < info->ndisks; i++)
qemuAgentDiskAddressFree(info->disks[i]);
g_free(info->disks);
g_free(info);
}
static qemuAgentDiskAddress *
qemuAgentGetDiskAddress(virJSONValue *json)
{
virJSONValue *pci;
virJSONValue *ccw;
g_autoptr(qemuAgentDiskAddress) addr = NULL;
addr = g_new0(qemuAgentDiskAddress, 1);
addr->bus_type = g_strdup(virJSONValueObjectGetString(json, "bus-type"));
addr->serial = g_strdup(virJSONValueObjectGetString(json, "serial"));
addr->devnode = g_strdup(virJSONValueObjectGetString(json, "dev"));
#define GET_DISK_ADDR(jsonObject, var, name) \
do { \
if (virJSONValueObjectGetNumberUint(jsonObject, name, var) < 0) { \
virReportError(VIR_ERR_INTERNAL_ERROR, \
_("'%1$s' missing"), name); \
return NULL; \
} \
} while (0)
GET_DISK_ADDR(json, &addr->bus, "bus");
GET_DISK_ADDR(json, &addr->target, "target");
GET_DISK_ADDR(json, &addr->unit, "unit");
if (!(pci = virJSONValueObjectGet(json, "pci-controller"))) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("'pci-controller' missing"));
return NULL;
}
GET_DISK_ADDR(pci, &addr->pci_controller.domain, "domain");
GET_DISK_ADDR(pci, &addr->pci_controller.bus, "bus");
GET_DISK_ADDR(pci, &addr->pci_controller.slot, "slot");
GET_DISK_ADDR(pci, &addr->pci_controller.function, "function");
if ((ccw = virJSONValueObjectGet(json, "ccw-address"))) {
g_autofree virCCWDeviceAddress *ccw_addr = NULL;
ccw_addr = g_new0(virCCWDeviceAddress, 1);
GET_DISK_ADDR(ccw, &ccw_addr->cssid, "cssid");
if (ccw_addr->cssid == 0) /* Guest CSSID 0 is 0xfe on host */
ccw_addr->cssid = 0xfe;
GET_DISK_ADDR(ccw, &ccw_addr->ssid, "ssid");
GET_DISK_ADDR(ccw, &ccw_addr->devno, "devno");
addr->ccw_addr = g_steal_pointer(&ccw_addr);
}
#undef GET_DISK_ADDR
return g_steal_pointer(&addr);
}
static int
qemuAgentGetFSInfoFillDisks(virJSONValue *jsondisks,
qemuAgentFSInfo *fsinfo)
{
size_t ndisks;
size_t i;
ndisks = virJSONValueArraySize(jsondisks);
if (ndisks)
fsinfo->disks = g_new0(qemuAgentDiskAddress *, ndisks);
fsinfo->ndisks = ndisks;
for (i = 0; i < fsinfo->ndisks; i++) {
virJSONValue *jsondisk = virJSONValueArrayGet(jsondisks, i);
if (!jsondisk) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("array element '%1$zd' of '%2$zd' missing in guest-get-fsinfo 'disk' data"),
i, fsinfo->ndisks);
return -1;
}
if (!(fsinfo->disks[i] = qemuAgentGetDiskAddress(jsondisk)))
return -1;
}
return 0;
}
/* Returns: number of entries in '@info' on success
* -2 when agent command is not supported by the agent and
* 'report_unsupported' is false (libvirt error is not reported)
* -1 otherwise (libvirt error is reported)
*/
int
qemuAgentGetFSInfo(qemuAgent *agent,
qemuAgentFSInfo ***info,
bool report_unsupported)
{
size_t i;
int ret = -1;
g_autoptr(virJSONValue) cmd = NULL;
g_autoptr(virJSONValue) reply = NULL;
virJSONValue *data;
size_t ndata = 0;
qemuAgentFSInfo **info_ret = NULL;
int rc;
cmd = qemuAgentMakeCommand("guest-get-fsinfo", NULL);
if (!cmd)
return ret;
if ((rc = qemuAgentCommandFull(agent, cmd, &reply, agent->timeout,
report_unsupported)) < 0)
return rc;
if (!(data = virJSONValueObjectGetArray(reply, "return"))) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("guest-get-fsinfo reply was missing or not an array"));
goto cleanup;
}
ndata = virJSONValueArraySize(data);
if (ndata == 0) {
ret = 0;
*info = NULL;
goto cleanup;
}
info_ret = g_new0(qemuAgentFSInfo *, ndata);
for (i = 0; i < ndata; i++) {
/* Reverse the order to arrange in mount order */
virJSONValue *entry = virJSONValueArrayGet(data, ndata - 1 - i);
virJSONValue *disk;
unsigned long long bytes_val;
const char *result = NULL;
if (!entry) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("array element '%1$zd' of '%2$zd' missing in guest-get-fsinfo return data"),
i, ndata);
goto cleanup;
}
info_ret[i] = g_new0(qemuAgentFSInfo, 1);
if (!(result = virJSONValueObjectGetString(entry, "mountpoint"))) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("'mountpoint' missing in reply of "
"guest-get-fsinfo"));
goto cleanup;
}
info_ret[i]->mountpoint = g_strdup(result);
if (!(result = virJSONValueObjectGetString(entry, "name"))) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("'name' missing in reply of guest-get-fsinfo"));
goto cleanup;
}
info_ret[i]->name = g_strdup(result);
if (!(result = virJSONValueObjectGetString(entry, "type"))) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("'type' missing in reply of guest-get-fsinfo"));
goto cleanup;
}
info_ret[i]->fstype = g_strdup(result);
/* 'used-bytes' and 'total-bytes' were added in qemu-ga 3.0 */
if (virJSONValueObjectHasKey(entry, "used-bytes")) {
if (virJSONValueObjectGetNumberUlong(entry, "used-bytes",
&bytes_val) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Error getting 'used-bytes' in reply of guest-get-fsinfo"));
goto cleanup;
}
info_ret[i]->used_bytes = bytes_val;
} else {
info_ret[i]->used_bytes = -1;
}
if (virJSONValueObjectHasKey(entry, "total-bytes")) {
if (virJSONValueObjectGetNumberUlong(entry, "total-bytes",
&bytes_val) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Error getting 'total-bytes' in reply of guest-get-fsinfo"));
goto cleanup;
}
info_ret[i]->total_bytes = bytes_val;
} else {
info_ret[i]->total_bytes = -1;
}
if (!(disk = virJSONValueObjectGetArray(entry, "disk"))) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("'disk' missing or not an array in reply of guest-get-fsinfo"));
goto cleanup;
}
if (qemuAgentGetFSInfoFillDisks(disk, info_ret[i]) < 0)
goto cleanup;
}
*info = g_steal_pointer(&info_ret);
ret = ndata;
cleanup:
if (info_ret) {
for (i = 0; i < ndata; i++)
qemuAgentFSInfoFree(info_ret[i]);
g_free(info_ret);
}
return ret;
}
static int
qemuAgentGetInterfaceOneAddress(virDomainIPAddressPtr ip_addr,
virJSONValue *ip_addr_obj,
const char *name)
{
const char *type, *addr;
type = virJSONValueObjectGetString(ip_addr_obj, "ip-address-type");
if (!type) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("qemu agent didn't provide 'ip-address-type' field for interface '%1$s'"),
name);
return -1;
}
if (STRNEQ(type, "ipv4") && STRNEQ(type, "ipv6")) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("unknown ip address type '%1$s'"),
type);
return -1;
}
addr = virJSONValueObjectGetString(ip_addr_obj, "ip-address");
if (!addr) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("qemu agent didn't provide 'ip-address' field for interface '%1$s'"),
name);
return -1;
}
if (virJSONValueObjectGetNumberUint(ip_addr_obj, "prefix",
&ip_addr->prefix) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("malformed 'prefix' field"));
return -1;
}
if (STREQ(type, "ipv4"))
ip_addr->type = VIR_IP_ADDR_TYPE_IPV4;
else
ip_addr->type = VIR_IP_ADDR_TYPE_IPV6;
ip_addr->addr = g_strdup(addr);
return 0;
}
/**
* qemuAgentGetInterfaceAddresses:
* @ifaces_ret: the array to put/update the interface in
* @ifaces_count: the number of interfaces in that array
* @ifaces_store: hash table into @ifaces_ret by interface name
* @iface_obj: one item from the JSON array of interfaces
*
* This function processes @iface_obj (which represents
* information about a single interface) and adds the information
* into the ifaces_ret array.
*
* If we're processing an interface alias, the suffix is stripped
* and information is appended to the entry found via the @ifaces_store
* hash table.
*
* Otherwise, the next free position in @ifaces_ret is used,
* its address added to @ifaces_store, and @ifaces_count incremented.
*/
static int
qemuAgentGetInterfaceAddresses(virDomainInterfacePtr **ifaces_ret,
size_t *ifaces_count,
GHashTable *ifaces_store,
virJSONValue *iface_obj)
{
virJSONValue *ip_addr_arr = NULL;
const char *hwaddr, *name = NULL;
virDomainInterfacePtr iface = NULL;
g_autofree char *ifname = NULL;
size_t addrs_count = 0;
size_t j;
/* interface name is required to be presented */
name = virJSONValueObjectGetString(iface_obj, "name");
if (!name) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("qemu agent didn't provide 'name' field"));
return -1;
}
/* Handle interface alias (<ifname>:<alias>) */
ifname = g_strdelimit(g_strdup(name), ":", '\0');
iface = virHashLookup(ifaces_store, ifname);
/* If the hash table doesn't contain this iface, add it */
if (!iface) {
VIR_EXPAND_N(*ifaces_ret, *ifaces_count, 1);
iface = g_new0(virDomainInterface, 1);
(*ifaces_ret)[*ifaces_count - 1] = iface;
if (virHashAddEntry(ifaces_store, ifname, iface) < 0)
return -1;
iface->naddrs = 0;
iface->name = g_strdup(ifname);
hwaddr = virJSONValueObjectGetString(iface_obj, "hardware-address");
iface->hwaddr = g_strdup(hwaddr);
}
/* as well as IP address which - moreover -
* can be presented multiple times */
if (!(ip_addr_arr = virJSONValueObjectGetArray(iface_obj, "ip-addresses")))
return 0;
/* If current iface already exists, continue with the count */
addrs_count = iface->naddrs;
VIR_EXPAND_N(iface->addrs, addrs_count, virJSONValueArraySize(ip_addr_arr));
for (j = 0; j < virJSONValueArraySize(ip_addr_arr); j++) {
virJSONValue *ip_addr_obj = virJSONValueArrayGet(ip_addr_arr, j);
virDomainIPAddressPtr ip_addr = iface->addrs + iface->naddrs;
iface->naddrs++;
if (qemuAgentGetInterfaceOneAddress(ip_addr, ip_addr_obj, name) < 0)
return -1;
}
return 0;
}
static int
qemuAgentGetAllInterfaceAddresses(virDomainInterfacePtr **ifaces_ret,
virJSONValue *ret_array)
{
g_autoptr(GHashTable) ifaces_store = NULL;
size_t ifaces_count = 0;
size_t i;
*ifaces_ret = NULL;
/* Hash table to handle the interface alias */
ifaces_store = virHashNew(NULL);
for (i = 0; i < virJSONValueArraySize(ret_array); i++) {
virJSONValue *iface_obj = virJSONValueArrayGet(ret_array, i);
if (qemuAgentGetInterfaceAddresses(ifaces_ret, &ifaces_count,
ifaces_store, iface_obj) < 0)
goto error;
}
return ifaces_count;
error:
if (*ifaces_ret) {
for (i = 0; i < ifaces_count; i++)
virDomainInterfaceFree((*ifaces_ret)[i]);
}
VIR_FREE(*ifaces_ret);
return -1;
}
/*
* qemuAgentGetInterfaces:
* @agent: agent object
* @ifaces: pointer to an array of pointers pointing to interface objects
*
* Issue guest-network-get-interfaces to guest agent, which returns a
* list of interfaces of a running domain along with their IP and MAC
* addresses.
*
* Returns: number of interfaces on success, -1 on error.
*/
int
qemuAgentGetInterfaces(qemuAgent *agent,
virDomainInterfacePtr **ifaces,
bool report_unsupported)
{
g_autoptr(virJSONValue) cmd = NULL;
g_autoptr(virJSONValue) reply = NULL;
virJSONValue *ret_array = NULL;
int rc;
if (!(cmd = qemuAgentMakeCommand("guest-network-get-interfaces", NULL)))
return -1;
if ((rc = qemuAgentCommandFull(agent, cmd, &reply, agent->timeout,
report_unsupported)) < 0)
return rc;
if (!(ret_array = virJSONValueObjectGetArray(reply, "return"))) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("qemu agent didn't return an array of interfaces"));
return -1;
}
return qemuAgentGetAllInterfaceAddresses(ifaces, ret_array);
}
int
qemuAgentSetUserPassword(qemuAgent *agent,
const char *user,
const char *password,
bool crypted)
{
g_autoptr(virJSONValue) cmd = NULL;
g_autoptr(virJSONValue) reply = NULL;
g_autofree char *password64 = NULL;
password64 = g_base64_encode((unsigned char *)password,
strlen(password));
if (!(cmd = qemuAgentMakeCommand("guest-set-user-password",
"b:crypted", crypted,
"s:username", user,
"s:password", password64,
NULL)))
return -1;
if (qemuAgentCommand(agent, cmd, &reply, agent->timeout) < 0)
return -1;
return 0;
}
/* Returns: 0 on success
* -2 when agent command is not supported by the agent and
* 'report_unsupported' is false (libvirt error is not reported)
* -1 otherwise (libvirt error is reported)
*/
int
qemuAgentGetUsers(qemuAgent *agent,
virTypedParameterPtr *params,
int *nparams,
int *maxparams,
bool report_unsupported)
{
g_autoptr(virJSONValue) cmd = NULL;
g_autoptr(virJSONValue) reply = NULL;
virJSONValue *data = NULL;
size_t ndata;
size_t i;
int rc;
if (!(cmd = qemuAgentMakeCommand("guest-get-users", NULL)))
return -1;
if ((rc = qemuAgentCommandFull(agent, cmd, &reply, agent->timeout,
report_unsupported)) < 0)
return rc;
if (!(data = virJSONValueObjectGetArray(reply, "return"))) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("guest-get-users reply was missing return data"));
return -1;
}
ndata = virJSONValueArraySize(data);
if (virTypedParamsAddUInt(params, nparams, maxparams,
"user.count", ndata) < 0)
return -1;
for (i = 0; i < ndata; i++) {
virJSONValue *entry = virJSONValueArrayGet(data, i);
char param_name[VIR_TYPED_PARAM_FIELD_LENGTH];
const char *strvalue;
double logintime;
if (!entry) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("array element missing in guest-get-users return "
"value"));
return -1;
}
if (!(strvalue = virJSONValueObjectGetString(entry, "user"))) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("'user' missing in reply of guest-get-users"));
return -1;
}
g_snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH, "user.%zu.name", i);
if (virTypedParamsAddString(params, nparams, maxparams,
param_name, strvalue) < 0)
return -1;
/* 'domain' is only present for windows guests */
if ((strvalue = virJSONValueObjectGetString(entry, "domain"))) {
g_snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH,
"user.%zu.domain", i);
if (virTypedParamsAddString(params, nparams, maxparams,
param_name, strvalue) < 0)
return -1;
}
if (virJSONValueObjectGetNumberDouble(entry, "login-time", &logintime) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("'login-time' missing in reply of guest-get-users"));
return -1;
}
g_snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH,
"user.%zu.login-time", i);
if (virTypedParamsAddULLong(params, nparams, maxparams,
param_name, logintime * 1000) < 0)
return -1;
}
return 0;
}
/* Returns: 0 on success
* -2 when agent command is not supported by the agent and
* 'report_unsupported' is false (libvirt error is not reported)
* -1 otherwise (libvirt error is reported)
*/
int
qemuAgentGetOSInfo(qemuAgent *agent,
virTypedParameterPtr *params,
int *nparams,
int *maxparams,
bool report_unsupported)
{
g_autoptr(virJSONValue) cmd = NULL;
g_autoptr(virJSONValue) reply = NULL;
virJSONValue *data = NULL;
int rc;
if (!(cmd = qemuAgentMakeCommand("guest-get-osinfo", NULL)))
return -1;
if ((rc = qemuAgentCommandFull(agent, cmd, &reply, agent->timeout,
report_unsupported)) < 0)
return rc;
if (!(data = virJSONValueObjectGetObject(reply, "return"))) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("guest-get-osinfo reply was missing return data"));
return -1;
}
#define OSINFO_ADD_PARAM(agent_string_, param_string_) \
do { \
const char *result; \
if ((result = virJSONValueObjectGetString(data, agent_string_))) { \
if (virTypedParamsAddString(params, nparams, maxparams, \
param_string_, result) < 0) { \
return -1; \
} \
} \
} while (0)
OSINFO_ADD_PARAM("id", "os.id");
OSINFO_ADD_PARAM("name", "os.name");
OSINFO_ADD_PARAM("pretty-name", "os.pretty-name");
OSINFO_ADD_PARAM("version", "os.version");
OSINFO_ADD_PARAM("version-id", "os.version-id");
OSINFO_ADD_PARAM("machine", "os.machine");
OSINFO_ADD_PARAM("variant", "os.variant");
OSINFO_ADD_PARAM("variant-id", "os.variant-id");
OSINFO_ADD_PARAM("kernel-release", "os.kernel-release");
OSINFO_ADD_PARAM("kernel-version", "os.kernel-version");
return 0;
}
/* Returns: 0 on success
* -2 when agent command is not supported by the agent and
* 'report_unsupported' is false (libvirt error is not reported)
* -1 otherwise (libvirt error is reported)
*/
int
qemuAgentGetTimezone(qemuAgent *agent,
virTypedParameterPtr *params,
int *nparams,
int *maxparams,
bool report_unsupported)
{
g_autoptr(virJSONValue) cmd = NULL;
g_autoptr(virJSONValue) reply = NULL;
virJSONValue *data = NULL;
const char *name;
int offset;
int rc;
if (!(cmd = qemuAgentMakeCommand("guest-get-timezone", NULL)))
return -1;
if ((rc = qemuAgentCommandFull(agent, cmd, &reply, agent->timeout,
report_unsupported)) < 0)
return rc;
if (!(data = virJSONValueObjectGetObject(reply, "return"))) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("guest-get-timezone reply was missing return data"));
return -1;
}
if ((name = virJSONValueObjectGetString(data, "zone")) &&
virTypedParamsAddString(params, nparams, maxparams,
"timezone.name", name) < 0)
return -1;
if ((virJSONValueObjectGetNumberInt(data, "offset", &offset)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("'offset' missing in reply of guest-get-timezone"));
return -1;
}
if (virTypedParamsAddInt(params, nparams, maxparams,
"timezone.offset", offset) < 0)
return -1;
return 0;
}
/* qemuAgentSetResponseTimeout:
* @agent: agent object
* @timeout: number of seconds to wait for agent response
*
* The agent object must be locked prior to calling this function.
*/
void
qemuAgentSetResponseTimeout(qemuAgent *agent,
int timeout)
{
agent->timeout = timeout;
}
/**
* qemuAgentSSHGetAuthorizedKeys:
* @agent: agent object
* @user: user to get authorized keys for
* @keys: Array of authorized keys
*
* Fetch the public keys from @user's $HOME/.ssh/authorized_keys.
*
* Returns: number of keys returned on success,
* -1 otherwise (error is reported)
*/
int
qemuAgentSSHGetAuthorizedKeys(qemuAgent *agent,
const char *user,
char ***keys)
{
g_autoptr(virJSONValue) cmd = NULL;
g_autoptr(virJSONValue) reply = NULL;
virJSONValue *data = NULL;
virJSONValue *arr = NULL;
if (!(cmd = qemuAgentMakeCommand("guest-ssh-get-authorized-keys",
"s:username", user,
NULL)))
return -1;
if (qemuAgentCommand(agent, cmd, &reply, agent->timeout) < 0)
return -1;
if (!(data = virJSONValueObjectGetObject(reply, "return")) ||
!(arr = virJSONValueObjectGetArray(data, "keys"))) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("qemu agent didn't return an array of keys"));
return -1;
}
if (!(*keys = virJSONValueArrayToStringList(arr)))
return -1;
return g_strv_length(*keys);
}
/**
* qemuAgentSSHAddAuthorizedKeys:
* @agent: agent object
* @user: user to add authorized keys for
* @keys: Array of authorized keys
* @nkeys: number of items in @keys array
* @reset: whether to truncate authorized keys file before writing
*
* Append SSH @keys into the @user's authorized keys file. If
* @reset is true then the file is truncated before write and
* thus contains only newly added @keys.
*
* Returns: 0 on success,
* -1 otherwise (error is reported)
*/
int
qemuAgentSSHAddAuthorizedKeys(qemuAgent *agent,
const char *user,
const char **keys,
size_t nkeys,
bool reset)
{
g_autoptr(virJSONValue) cmd = NULL;
g_autoptr(virJSONValue) reply = NULL;
g_autoptr(virJSONValue) jkeys = NULL;
jkeys = qemuAgentMakeStringsArray(keys, nkeys);
if (jkeys == NULL)
return -1;
if (!(cmd = qemuAgentMakeCommand("guest-ssh-add-authorized-keys",
"s:username", user,
"a:keys", &jkeys,
"b:reset", reset,
NULL)))
return -1;
return qemuAgentCommand(agent, cmd, &reply, agent->timeout);
}
/**
* qemuAgentSSHRemoveAuthorizedKeys:
* @agent: agent object
* @user: user to remove authorized keys for
* @keys: Array of authorized keys
* @nkeys: number of items in @keys array
*
* Remove SSH @keys from the @user's authorized keys file. It's
* not considered an error when trying to remove a non-existent
* key.
*
* Returns: 0 on success,
* -1 otherwise (error is reported)
*/
int
qemuAgentSSHRemoveAuthorizedKeys(qemuAgent *agent,
const char *user,
const char **keys,
size_t nkeys)
{
g_autoptr(virJSONValue) cmd = NULL;
g_autoptr(virJSONValue) reply = NULL;
g_autoptr(virJSONValue) jkeys = NULL;
jkeys = qemuAgentMakeStringsArray(keys, nkeys);
if (jkeys == NULL)
return -1;
if (!(cmd = qemuAgentMakeCommand("guest-ssh-remove-authorized-keys",
"s:username", user,
"a:keys", &jkeys,
NULL)))
return -1;
return qemuAgentCommand(agent, cmd, &reply, agent->timeout);
}
int qemuAgentGetDisks(qemuAgent *agent,
qemuAgentDiskInfo ***disks,
bool report_unsupported)
{
g_autoptr(virJSONValue) cmd = NULL;
g_autoptr(virJSONValue) reply = NULL;
virJSONValue *data = NULL;
size_t ndata;
size_t i;
int rc;
if (!(cmd = qemuAgentMakeCommand("guest-get-disks", NULL)))
return -1;
if ((rc = qemuAgentCommandFull(agent, cmd, &reply, agent->timeout,
report_unsupported)) < 0)
return rc;
if (!(data = virJSONValueObjectGetArray(reply, "return"))) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("qemu agent didn't return an array of disks"));
return -1;
}
ndata = virJSONValueArraySize(data);
*disks = g_new0(qemuAgentDiskInfo *, ndata);
for (i = 0; i < ndata; i++) {
virJSONValue *addr;
virJSONValue *entry = virJSONValueArrayGet(data, i);
virJSONValue *dependencies;
qemuAgentDiskInfo *disk;
if (!entry) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("array element missing in guest-get-disks return "
"value"));
goto error;
}
disk = g_new0(qemuAgentDiskInfo, 1);
(*disks)[i] = disk;
disk->name = g_strdup(virJSONValueObjectGetString(entry, "name"));
if (!disk->name) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("'name' missing in reply of guest-get-disks"));
goto error;
}
if (virJSONValueObjectGetBoolean(entry, "partition", &disk->partition) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("'partition' missing in reply of guest-get-disks"));
goto error;
}
if ((dependencies = virJSONValueObjectGetArray(entry, "dependencies"))) {
if (!(disk->dependencies = virJSONValueArrayToStringList(dependencies)))
goto error;
}
disk->alias = g_strdup(virJSONValueObjectGetString(entry, "alias"));
addr = virJSONValueObjectGetObject(entry, "address");
if (addr) {
disk->address = qemuAgentGetDiskAddress(addr);
if (!disk->address)
goto error;
}
}
return ndata;
error:
for (i = 0; i < ndata; i++) {
qemuAgentDiskInfoFree((*disks)[i]);
}
g_free(*disks);
return -1;
}
|