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
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <getopt.h>
#include <locale.h>
#include "sd-bus.h"
#include "sd-json.h"
#include "build.h"
#include "bus-error.h"
#include "bus-label.h"
#include "bus-locator.h"
#include "bus-map-properties.h"
#include "bus-util.h"
#include "errno-list.h"
#include "fileio.h"
#include "format-table.h"
#include "json-util.h"
#include "main-func.h"
#include "pager.h"
#include "pretty-print.h"
#include "strv.h"
#include "sysupdate-update-set-flags.h"
#include "sysupdate-util.h"
#include "terminal-util.h"
#include "verbs.h"
static PagerFlags arg_pager_flags = 0;
static bool arg_legend = true;
static bool arg_reboot = false;
static bool arg_offline = false;
static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
static char *arg_host = NULL;
#define SYSUPDATE_TARGET_INTERFACE "org.freedesktop.sysupdate1.Target"
typedef struct Version {
char *version;
UpdateSetFlags flags;
char **changelog;
char *contents_json;
} Version;
static void version_done(Version *v) {
assert(v);
v->version = mfree(v->version);
v->changelog = strv_free(v->changelog);
v->flags = 0;
v->contents_json = mfree(v->contents_json);
}
typedef struct Operation {
void *userdata;
sd_bus *bus;
sd_event *event;
unsigned *remaining;
const char *target_path;
const char *target_id;
uint64_t job_id;
char *job_path;
sd_event_source *job_interrupt_source;
sd_bus_slot *job_properties_slot;
sd_bus_slot *job_finished_slot;
} Operation;
static Operation* operation_free(Operation *p) {
if (!p)
return NULL;
assert(*p->remaining > 0);
*p->remaining -= 1;
if (*p->remaining == 0)
/* We want to crash the program if we can't exit the loop
* cleanly, otherwise it will just hang */
assert_se(sd_event_exit(p->event, 0) >= 0);
free(p->job_path);
sd_event_source_disable_unref(p->job_interrupt_source);
sd_bus_slot_unref(p->job_properties_slot);
sd_bus_slot_unref(p->job_finished_slot);
return mfree(p);
}
DEFINE_TRIVIAL_CLEANUP_FUNC(Operation*, operation_free);
static Operation* operation_new(
void *userdata,
sd_bus *bus,
unsigned *remaining,
const char *target_path,
const char *target_id) {
_cleanup_(operation_freep) Operation *o = NULL;
o = new(Operation, 1);
if (!o)
return NULL;
*o = (Operation) {
.userdata = userdata,
.bus = bus,
.event = sd_bus_get_event(bus),
.remaining = remaining,
.target_path = target_path,
.target_id = target_id,
};
return TAKE_PTR(o);
}
static int ensure_targets(sd_bus *bus, char **argv, char ***ret_targets) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_strv_free_ char **targets = NULL;
int r;
assert(bus);
assert(ret_targets);
if (strv_isempty(argv)) {
const char *class, *name, *path;
r = bus_call_method(bus, bus_sysupdate_mgr, "ListTargets", &error, &reply, NULL);
if (r < 0)
return log_error_errno(r, "Failed to call ListTargets: %s", bus_error_message(&error, r));
r = sd_bus_message_enter_container(reply, 'a', "(sso)");
if (r < 0)
return bus_log_parse_error(r);
while ((r = sd_bus_message_read(reply, "(sso)", &class, &name, &path)) > 0) {
_cleanup_free_ char *id = NULL;
if (streq(class, "host"))
id = strdup("host");
else
id = strjoin(class, ":", name);
if (!id)
return log_oom();
r = strv_consume(&targets, TAKE_PTR(id));
if (r < 0)
return log_oom();
}
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(reply);
if (r < 0)
return bus_log_parse_error(r);
} else {
r = strv_extend_strv(&targets, argv, true);
if (r < 0)
return log_oom();
}
*ret_targets = TAKE_PTR(targets);
return 0;
}
static int parse_target(
const char *in,
char **ret_bus_path,
char **ret_version) {
_cleanup_free_ char *id = NULL, *version = NULL, *escaped = NULL, *objpath = NULL;
const char *s;
/*
* Parses the TARGET[@VERSION] syntax from the command line into
* a bus object path and an optional version number.
*/
assert(in);
assert(ret_bus_path);
assert(ret_version);
s = strrchr(in, '@');
if (s) {
version = strdup(s + 1);
if (!version)
return -ENOMEM;
id = strndup(in, s - in);
} else
id = strdup(in);
if (!id)
return -ENOMEM;
escaped = bus_label_escape(id);
if (!escaped)
return -ENOMEM;
objpath = strjoin("/org/freedesktop/sysupdate1/target/", escaped);
if (!objpath)
return -ENOMEM;
*ret_bus_path = TAKE_PTR(objpath);
*ret_version = TAKE_PTR(version);
return 0;
}
static int parse_targets(
char **targets,
size_t *ret_n,
char ***ret_bus_paths,
char ***ret_versions) {
_cleanup_strv_free_ char **bus_paths = NULL;
_cleanup_strv_free_ char **versions = NULL;
size_t n = 0;
int r;
assert(ret_bus_paths);
assert(ret_n);
if (strv_isempty(targets))
return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "No targets found.");
STRV_FOREACH(id, targets) {
_cleanup_free_ char *bus_path = NULL, *version = NULL;
r = parse_target(*id, &bus_path, &version);
if (r < 0)
return log_oom();
if (version && !ret_versions)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Unexpected version specifier in target: %s",
*id);
r = strv_extend(&bus_paths, strempty(bus_path));
if (r < 0)
return log_oom();
r = strv_extend(&versions, strempty(version));
if (r < 0)
return log_oom();
n++;
}
*ret_n = n;
*ret_bus_paths = TAKE_PTR(bus_paths);
if (ret_versions)
*ret_versions = TAKE_PTR(versions);
return 0;
}
static int log_bus_error(int r, const sd_bus_error *error, const char *target, const char *action) {
assert(action);
if (r == 0) {
assert(sd_bus_error_is_set(error));
r = sd_bus_error_get_errno(error);
}
if (sd_bus_error_has_name(error, SD_BUS_ERROR_UNKNOWN_OBJECT)) {
if (target)
return log_error_errno(r, "Invalid target: %s", target);
return log_error_errno(r, "Invalid target");
}
if (target)
return log_error_errno(r, "Failed to %s for '%s': %s", action, target,
bus_error_message(error, r));
return log_error_errno(r, "Failed to %s: %s", action, bus_error_message(error, r));
}
static int list_targets(sd_bus *bus) {
_cleanup_(table_unrefp) Table *table = NULL;
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_strv_free_ char **targets = NULL, **target_paths = NULL;
size_t n;
int r;
assert(bus);
r = ensure_targets(bus, /* argv= */ NULL, &targets);
if (r < 0)
return log_error_errno(r, "Failed to find targets: %m");
r = parse_targets(targets, &n, &target_paths, /* ret_versions= */ NULL);
if (r < 0)
return log_error_errno(r, "Failed to parse targets: %m");
table = table_new("target", "version", "path");
if (!table)
return log_oom();
for (size_t i = 0; i < n; i++) {
char *version = NULL;
_cleanup_free_ char *path = NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
r = sd_bus_call_method(bus, bus_sysupdate_mgr->destination,
target_paths[i], SYSUPDATE_TARGET_INTERFACE,
"GetVersion", &error, &reply, NULL);
if (r < 0)
return log_bus_error(r, &error, targets[i], "get current version");
r = sd_bus_message_read_basic(reply, 's', &version);
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_get_property_string(bus, bus_sysupdate_mgr->destination,
target_paths[i], SYSUPDATE_TARGET_INTERFACE,
"Path", &error, &path);
if (r < 0)
return log_bus_error(r, &error, targets[i], "get target bus path");
r = table_add_many(table,
TABLE_STRING, targets[i],
TABLE_STRING, empty_to_dash(version),
TABLE_STRING, path);
if (r < 0)
return table_log_add_error(r);
}
return table_print_with_pager(table, SD_JSON_FORMAT_OFF, arg_pager_flags, arg_legend);
}
typedef struct DescribeParams {
Version v;
sd_json_variant *contents_json;
bool newest;
bool available;
bool installed;
bool obsolete;
bool protected;
bool incomplete;
} DescribeParams;
static void describe_params_done(DescribeParams *p) {
assert(p);
version_done(&p->v);
sd_json_variant_unref(p->contents_json);
}
static int parse_describe(sd_bus_message *reply, Version *ret) {
char *version_json = NULL;
_cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
int r;
assert(reply);
assert(ret);
r = sd_bus_message_read_basic(reply, 's', &version_json);
if (r < 0)
return bus_log_parse_error(r);
r = sd_json_parse(version_json, 0, &json, NULL, NULL);
if (r < 0)
return log_error_errno(r, "Failed to parse JSON: %m");
assert(sd_json_variant_is_object(json));
static const sd_json_dispatch_field dispatch_table[] = {
{ "version", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(DescribeParams, v.version), 0 },
{ "newest", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(DescribeParams, newest), 0 },
{ "available", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(DescribeParams, available), 0 },
{ "installed", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(DescribeParams, installed), 0 },
{ "obsolete", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(DescribeParams, obsolete), 0 },
{ "protected", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(DescribeParams, protected), 0 },
{ "incomplete", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(DescribeParams, incomplete), 0 },
{ "changelog_urls", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_strv, offsetof(DescribeParams, v.changelog), 0 },
{ "contents", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_variant, offsetof(DescribeParams, contents_json), 0 },
{},
};
_cleanup_(describe_params_done) DescribeParams p = {};
r = sd_json_dispatch(json, dispatch_table, SD_JSON_ALLOW_EXTENSIONS, &p);
if (r < 0)
return log_error_errno(r, "Failed to parse JSON: %m");
SET_FLAG(p.v.flags, UPDATE_NEWEST, p.newest);
SET_FLAG(p.v.flags, UPDATE_AVAILABLE, p.available);
SET_FLAG(p.v.flags, UPDATE_INSTALLED, p.installed);
SET_FLAG(p.v.flags, UPDATE_OBSOLETE, p.obsolete);
SET_FLAG(p.v.flags, UPDATE_PROTECTED, p.protected);
SET_FLAG(p.v.flags, UPDATE_INCOMPLETE, p.incomplete);
r = sd_json_variant_format(p.contents_json, 0, &p.v.contents_json);
if (r < 0)
return log_error_errno(r, "Failed to format JSON for contents: %m");
*ret = TAKE_STRUCT(p.v);
return 0;
}
static int list_versions_finished(sd_bus_message *reply, void *userdata, sd_bus_error *ret_error) {
_cleanup_(operation_freep) Operation *op = ASSERT_PTR(userdata);
Table *table = ASSERT_PTR(op->userdata);
const sd_bus_error *e;
_cleanup_(version_done) Version v = {};
_cleanup_free_ char *version_link = NULL;
const char *color;
int r;
assert(reply);
e = sd_bus_message_get_error(reply);
if (e)
return log_bus_error(0, e, NULL, "call Describe");
r = parse_describe(reply, &v);
if (r < 0)
return log_error_errno(r, "Failed to parse Describe output: %m");
color = update_set_flags_to_color(v.flags);
if (urlify_enabled() && !strv_isempty(v.changelog)) {
version_link = strjoin(v.version, special_glyph(SPECIAL_GLYPH_EXTERNAL_LINK));
if (!version_link)
return log_oom();
}
r = table_add_many(table,
TABLE_STRING, update_set_flags_to_glyph(v.flags),
TABLE_SET_COLOR, color,
TABLE_STRING, version_link ?: v.version,
TABLE_SET_COLOR, color,
TABLE_SET_URL, strv_isempty(v.changelog) ? NULL : v.changelog[0],
TABLE_STRING, update_set_flags_to_string(v.flags),
TABLE_SET_COLOR, color);
if (r < 0)
return table_log_add_error(r);
return 0;
}
static int list_versions(sd_bus *bus, const char *target_path) {
_cleanup_(sd_event_unrefp) sd_event *event = NULL;
_cleanup_(table_unrefp) Table *table = NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_strv_free_ char **versions = NULL;
unsigned remaining = 0;
int r;
r = sd_bus_call_method(
bus,
bus_sysupdate_mgr->destination,
target_path,
SYSUPDATE_TARGET_INTERFACE,
"List",
&error,
&reply,
"t",
arg_offline ? SD_SYSUPDATE_OFFLINE : 0);
if (r < 0)
return log_bus_error(r, &error, NULL, "call List");
r = sd_bus_message_read_strv(reply, &versions);
if (r < 0)
return bus_log_parse_error(r);
table = table_new("", "version", "status");
if (!table)
return log_oom();
(void) table_set_sort(table, 1);
(void) table_set_reverse(table, 1, true);
r = sd_event_default(&event);
if (r < 0)
return log_error_errno(r, "Failed to get event loop: %m");
r = sd_bus_attach_event(bus, event, 0);
if (r < 0)
return log_error_errno(r, "Failed to attach bus to event loop: %m");
r = sd_event_set_signal_exit(event, true);
if (r < 0)
return log_error_errno(r, "Failed to set up interrupt handler: %m");
STRV_FOREACH(version, versions) {
_cleanup_(operation_freep) Operation *op = NULL;
op = operation_new(table, bus, &remaining, NULL, NULL);
if (!op)
return log_oom();
r = sd_bus_call_method_async(bus,
NULL,
bus_sysupdate_mgr->destination,
target_path,
SYSUPDATE_TARGET_INTERFACE,
"Describe",
list_versions_finished,
op,
"st",
*version,
arg_offline ? SD_SYSUPDATE_OFFLINE : 0);
if (r < 0)
return log_error_errno(r, "Failed to call Describe: %m");
TAKE_PTR(op);
remaining++;
}
r = sd_event_loop(event);
if (r < 0)
return log_error_errno(r, "Failed to start event loop: %m");
return table_print_with_pager(table, SD_JSON_FORMAT_OFF, arg_pager_flags, arg_legend);
}
static int describe(sd_bus *bus, const char *target_path, const char *version) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(table_unrefp) Table *table = NULL;
_cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
_cleanup_(version_done) Version v = {};
sd_json_variant *entry;
const char *color;
int r;
r = sd_bus_call_method(
bus,
bus_sysupdate_mgr->destination,
target_path,
SYSUPDATE_TARGET_INTERFACE,
"Describe",
&error,
&reply,
"st",
version,
arg_offline ? SD_SYSUPDATE_OFFLINE : 0);
if (r < 0)
return log_bus_error(r, &error, NULL, "call Describe");
r = parse_describe(reply, &v);
if (r < 0)
return log_error_errno(r, "Failed to parse Describe output: %m");
color = strempty(update_set_flags_to_color(v.flags));
printf("%s%s%s Version: %s\n"
" State: %s%s%s\n",
color,
update_set_flags_to_glyph(v.flags),
ansi_normal(),
v.version,
color,
update_set_flags_to_string(v.flags),
ansi_normal());
STRV_FOREACH(url, v.changelog) {
_cleanup_free_ char *changelog_link = NULL;
r = terminal_urlify(*url, NULL, &changelog_link);
if (r < 0)
return log_error_errno(r, "Could not urlify link to change-log: %m");
printf("ChangeLog: %s\n", strna(changelog_link));
}
printf("\n");
r = sd_json_parse(v.contents_json, 0, &json, NULL, NULL);
if (r < 0)
return log_error_errno(r, "Failed to parse JSON: %m");
assert(sd_json_variant_is_array(json));
JSON_VARIANT_ARRAY_FOREACH(entry, json) {
assert(sd_json_variant_is_object(entry));
const char *key;
sd_json_variant *value;
if (!table) {
table = table_new_raw(sd_json_variant_elements(entry) / 2);
if (!table)
return log_oom();
JSON_VARIANT_OBJECT_FOREACH(key, value, entry) {
r = table_add_cell(table, NULL, TABLE_HEADER, key);
if (r < 0)
return table_log_add_error(r);
}
}
JSON_VARIANT_OBJECT_FOREACH(key, value, entry) {
TableDataType type;
uint64_t number;
bool boolean;
const void *data;
if (sd_json_variant_is_string(value)) {
type = TABLE_STRING;
assert_se(data = sd_json_variant_string(value));
} else if (sd_json_variant_is_unsigned(value)) {
type = TABLE_UINT64;
number = sd_json_variant_unsigned(value);
data = &number;
} else if (sd_json_variant_is_boolean(value)) {
type = TABLE_BOOLEAN;
boolean = sd_json_variant_boolean(value);
data = &boolean;
} else if (sd_json_variant_is_null(value)) {
type = TABLE_EMPTY;
data = NULL;
} else
assert_not_reached();
if (streq(key, "ptflags"))
type = TABLE_UINT64_HEX;
else if (streq(key, "size"))
type = TABLE_SIZE;
else if (streq(key, "mode"))
type = TABLE_MODE;
else if (streq(key, "mtime"))
type = TABLE_TIMESTAMP;
r = table_add_cell(table, NULL, type, data);
if (r < 0)
return table_log_add_error(r);
}
}
return table_print_with_pager(table, SD_JSON_FORMAT_OFF, arg_pager_flags, arg_legend);
}
static int verb_list(int argc, char **argv, void *userdata) {
sd_bus *bus = ASSERT_PTR(userdata);
_cleanup_free_ char *target_path = NULL, *version = NULL;
int r;
if (argc == 1)
return list_targets(bus);
r = parse_target(argv[1], &target_path, &version);
if (r < 0)
return log_oom();
if (version)
return describe(bus, target_path, version);
else
return list_versions(bus, target_path);
}
static int check_describe_finished(sd_bus_message *reply, void *userdata, sd_bus_error *ret_error) {
_cleanup_(operation_freep) Operation *op = ASSERT_PTR(userdata);
Table *table = ASSERT_PTR(op->userdata);
_cleanup_(version_done) Version v = {};
_cleanup_free_ char *update = NULL;
const sd_bus_error *e;
sd_bus_error error = {};
const char *lnk = NULL;
char *current;
int r;
assert(reply);
e = sd_bus_message_get_error(reply);
if (e)
return log_bus_error(0, e, NULL, "call Describe");
r = parse_describe(reply, &v);
if (r < 0)
return log_error_errno(r, "Failed to parse output of Describe: %m");
r = sd_bus_call_method(
op->bus,
bus_sysupdate_mgr->destination,
op->target_path,
SYSUPDATE_TARGET_INTERFACE,
"GetVersion",
&error,
&reply,
NULL);
if (r < 0)
return log_bus_error(r, &error, op->target_id, "get current version");
r = sd_bus_message_read_basic(reply, 's', ¤t);
if (r < 0)
return bus_log_parse_error(r);
if (urlify_enabled() && !strv_isempty(v.changelog))
lnk = special_glyph(SPECIAL_GLYPH_EXTERNAL_LINK);
update = strjoin(empty_to_dash(current), " ",
special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), " ",
v.version, strempty(lnk));
if (!update)
return log_oom();
r = table_add_many(table,
TABLE_STRING, op->target_id,
TABLE_STRING, update,
TABLE_SET_URL, strv_isempty(v.changelog) ? NULL : v.changelog[0]);
if (r < 0)
return table_log_add_error(r);
return 0;
}
static int check_finished(sd_bus_message *reply, void *userdata, sd_bus_error *ret_error) {
_cleanup_(operation_freep) Operation *op = ASSERT_PTR(userdata);
const sd_bus_error *e;
const char *new_version = NULL;
int r;
assert(reply);
e = sd_bus_message_get_error(reply);
if (e)
return log_bus_error(0, e, op->target_id, "call CheckNew");
r = sd_bus_message_read(reply, "s", &new_version);
if (r < 0)
return bus_log_parse_error(r);
if (isempty(new_version))
return 0;
r = sd_bus_call_method_async(op->bus,
NULL,
bus_sysupdate_mgr->destination,
op->target_path,
SYSUPDATE_TARGET_INTERFACE,
"Describe",
check_describe_finished,
op,
"st",
new_version,
arg_offline ? SD_SYSUPDATE_OFFLINE : 0);
if (r < 0)
return log_error_errno(r, "Failed to call Describe: %m");
TAKE_PTR(op);
return 0;
}
static int verb_check(int argc, char **argv, void *userdata) {
sd_bus *bus = ASSERT_PTR(userdata);
_cleanup_(table_unrefp) Table *table = NULL;
_cleanup_(sd_event_unrefp) sd_event *event = NULL;
_cleanup_strv_free_ char **targets = NULL, **target_paths = NULL;
size_t n;
unsigned remaining = 0;
int r;
r = ensure_targets(bus, argv + 1, &targets);
if (r < 0)
return log_error_errno(r, "Failed to find targets: %m");
r = parse_targets(targets, &n, &target_paths, /* ret_versions= */ NULL);
if (r < 0)
return log_error_errno(r, "Failed to parse targets: %m");
table = table_new("target", "update");
if (!table)
return log_oom();
(void) table_set_sort(table, 0);
r = sd_event_default(&event);
if (r < 0)
return log_error_errno(r, "Failed to get event loop: %m");
r = sd_bus_attach_event(bus, event, 0);
if (r < 0)
return log_error_errno(r, "Failed to attach bus to event loop: %m");
r = sd_event_set_signal_exit(event, true);
if (r < 0)
return log_error_errno(r, "Failed to set up interrupt handler: %m");
for (size_t i = 0; i < n; i++) {
_cleanup_(operation_freep) Operation *op = NULL;
op = operation_new(table, bus, &remaining, target_paths[i], targets[i]);
if (!op)
return log_oom();
r = sd_bus_call_method_async(bus, NULL, bus_sysupdate_mgr->destination, target_paths[i], SYSUPDATE_TARGET_INTERFACE, "CheckNew", check_finished, op, NULL);
if (r < 0)
return log_error_errno(r, "Failed to call CheckNew for target %s: %m", targets[i]);
TAKE_PTR(op);
remaining++;
}
r = sd_event_loop(event);
if (r < 0)
return log_error_errno(r, "Failed to start event loop: %m");
if (table_isempty(table)) {
log_info("No updates available.");
return 0;
}
return table_print_with_pager(table, SD_JSON_FORMAT_OFF, arg_pager_flags, arg_legend);
}
#define UPDATE_PROGRESS_FAILED INT_MIN
#define UPDATE_PROGRESS_DONE INT_MAX
/* Make sure it doesn't overlap w/ errno values */
assert_cc(UPDATE_PROGRESS_FAILED < -ERRNO_MAX);
static int update_render_progress(sd_event_source *source, void *userdata) {
OrderedHashmap *map = ASSERT_PTR(userdata);
const char *target;
void *p;
unsigned total;
size_t n;
bool exiting;
exiting = sd_event_get_state(sd_event_source_get_event(source)) == SD_EVENT_EXITING;
total = 0;
n = ordered_hashmap_size(map);
if (n == 0)
return 0;
/* We're outputting lots of small strings to STDERR, which is unbuffered by default. So let's turn
* on full buffering, so we pass this all to the TTY in one go, to make things more efficient */
WITH_BUFFERED_STDERR;
if (!terminal_is_dumb()) {
for (size_t i = 0; i <= n; i++)
fputs("\n", stderr); /* Possibly scroll the terminal to make room (including total)*/
fprintf(stderr, "\e[%zuF", n+1); /* Go back */
fputs("\e7", stderr); /* Save cursor position */
fputs("\e[?25l", stderr); /* Hide cursor */
}
ORDERED_HASHMAP_FOREACH_KEY(p, target, map) {
int progress = PTR_TO_INT(p);
if (progress == UPDATE_PROGRESS_FAILED) {
clear_progress_bar_impl(target);
fprintf(stderr, "%s: %s Unknown failure\n", target, RED_CROSS_MARK());
total += 100;
} else if (progress == -EALREADY) {
clear_progress_bar_impl(target);
fprintf(stderr, "%s: %s Already up-to-date\n", target, GREEN_CHECK_MARK());
n--; /* Don't consider this target in the total */
} else if (progress < 0) {
clear_progress_bar_impl(target);
fprintf(stderr, "%s: %s %s\n", target, RED_CROSS_MARK(), STRERROR(progress));
total += 100;
} else if (progress == UPDATE_PROGRESS_DONE) {
clear_progress_bar_impl(target);
fprintf(stderr, "%s: %s Done\n", target, GREEN_CHECK_MARK());
total += 100;
} else {
draw_progress_bar_impl(target, progress);
fputs("\n", stderr);
total += progress;
}
}
if (n > 1) {
if (exiting)
clear_progress_bar_impl(target);
else {
draw_progress_bar_impl("Total", (double) total / n);
if (terminal_is_dumb())
fputs("\n", stderr);
}
}
if (!terminal_is_dumb()) {
if (exiting)
fputs("\e[?25h", stderr); /* Show cursor again */
else
fputs("\e8", stderr); /* Restore cursor position */
} else if (!exiting)
fputs("------\n", stderr);
return 0;
}
static int update_properties_changed(sd_bus_message *m, void *userdata, sd_bus_error *error) {
Operation *op = ASSERT_PTR(userdata);
OrderedHashmap *map = ASSERT_PTR(op->userdata);
const char *interface;
uint32_t progress = UINT32_MAX;
static const struct bus_properties_map prop_map[] = {
{ "Progress", "u", NULL, 0 },
{}
};
int r;
assert(m);
r = sd_bus_message_read(m, "s", &interface);
if (r < 0) {
bus_log_parse_error_debug(r);
return 0;
}
if (!streq(interface, "org.freedesktop.sysupdate1.Job"))
return 0;
r = bus_message_map_all_properties(m, prop_map, /* flags= */ 0, error, &progress);
if (r < 0)
return 0; /* map_all_properties does the debug logging internally... */
if (progress == UINT_MAX)
return 0;
r = ordered_hashmap_replace(map, op->target_id, INT_TO_PTR((int) progress));
if (r < 0)
log_debug_errno(r, "Failed to update hashmap: %m");
return 0;
}
static int update_finished(sd_bus_message *m, void *userdata, sd_bus_error *error) {
_cleanup_(operation_freep) Operation *op = ASSERT_PTR(userdata);
OrderedHashmap *map = ASSERT_PTR(op->userdata);
uint64_t id;
int r, status;
assert(m);
r = sd_bus_message_read(m, "toi", &id, NULL, &status);
if (r < 0) {
bus_log_parse_error_debug(r);
return 0;
}
if (id != op->job_id) {
TAKE_PTR(op);
return 0;
}
if (status == 0) /* success */
status = UPDATE_PROGRESS_DONE;
else if (status > 0) /* exit status without errno */
status = UPDATE_PROGRESS_FAILED; /* i.e. EXIT_FAILURE */
/* else errno */
r = ordered_hashmap_replace(map, op->target_id, INT_TO_PTR(status));
if (r < 0)
log_debug_errno(r, "Failed to update hashmap: %m");
return 0;
}
static int update_interrupted(sd_event_source *source, void *userdata) {
/* Since the event loop is exiting, we will never receive the JobRemoved
* signal. So, we must free the userdata here. */
_cleanup_(operation_freep) Operation *op = ASSERT_PTR(userdata);
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
OrderedHashmap *map = ASSERT_PTR(op->userdata);
int r;
r = sd_bus_call_method(op->bus,
bus_sysupdate_mgr->destination,
op->job_path,
"org.freedesktop.sysupdate1.Job",
"Cancel",
&error, /* reply= */ NULL,
NULL);
if (r < 0)
return log_bus_error(r, &error, NULL, "call Cancel");
r = ordered_hashmap_replace(map, op->target_id, INT_TO_PTR(-ECANCELED));
if (r < 0)
log_debug_errno(r, "Failed to update hashmap: %m");
return 0;
}
static int update_started(sd_bus_message *reply, void *userdata, sd_bus_error *ret_error) {
_cleanup_(operation_freep) Operation *op = ASSERT_PTR(userdata);
OrderedHashmap *map = ASSERT_PTR(op->userdata);
const sd_bus_error *e;
_cleanup_free_ char *key = NULL;
const char *new_version, *job_path;
int r;
assert(reply);
e = sd_bus_message_get_error(reply);
if (e) {
r = -sd_bus_error_get_errno(e);
key = strdup(op->target_id);
if (!key)
return log_oom();
r = ordered_hashmap_put(map, key, INT_TO_PTR(r));
if (r < 0)
return log_error_errno(r, "Failed to update hashmap: %m");
TAKE_PTR(key);
return 0;
}
r = sd_bus_message_read(reply, "sto", &new_version, &op->job_id, &job_path);
if (r < 0)
return bus_log_parse_error(r);
op->job_path = strdup(job_path);
if (!op->job_path)
return log_oom();
if (isempty(new_version))
new_version = "latest";
/* Register this job into the hashmap. This will give it a progress bar */
if (strchr(op->target_id, '@'))
key = strdup(op->target_id);
else
key = strjoin(op->target_id, "@", new_version);
if (!key)
return log_oom();
r = ordered_hashmap_put(map, key, INT_TO_PTR(0)); /* takes ownership of key */
if (r < 0)
return log_error_errno(r, "Failed to add target to tracking map: %m");
op->target_id = TAKE_PTR(key); /* just borrowing */
/* Cancel the job if the event loop exits */
r = sd_event_add_exit(op->event, &op->job_interrupt_source, update_interrupted, op);
if (r < 0)
return log_error_errno(r, "Failed to set up interrupt handler: %m");
/* We need to cancel the job before the final iteration of the renderer runs */
r = sd_event_source_set_priority(op->job_interrupt_source, SD_EVENT_PRIORITY_IMPORTANT);
if (r < 0)
return log_error_errno(r, "Failed to set interrupt priority: %m");
/* Register for progress notifications */
r = sd_bus_match_signal_async(
op->bus,
&op->job_properties_slot,
bus_sysupdate_mgr->destination,
job_path,
"org.freedesktop.DBus.Properties",
"PropertiesChanged",
update_properties_changed,
NULL,
op);
if (r < 0)
return log_bus_error(r, NULL, op->target_id, "listen for PropertiesChanged");
TAKE_PTR(op); /* update_finished/update_interrupted take ownership of the data */
return 0;
}
static int verb_update(int argc, char **argv, void *userdata) {
sd_bus *bus = ASSERT_PTR(userdata);
_cleanup_(sd_event_unrefp) sd_event *event = NULL;
_cleanup_(sd_event_source_unrefp) sd_event_source *render_exit = NULL;
_cleanup_ordered_hashmap_free_ OrderedHashmap *map = NULL;
_cleanup_strv_free_ char **targets = NULL, **versions = NULL, **target_paths = NULL;
size_t n;
unsigned remaining = 0;
void *p;
bool did_anything = false;
int r;
r = ensure_targets(bus, argv + 1, &targets);
if (r < 0)
return log_error_errno(r, "Could not find targets: %m");
r = parse_targets(targets, &n, &target_paths, &versions);
if (r < 0)
return log_error_errno(r, "Could not parse targets: %m");
map = ordered_hashmap_new(&string_hash_ops_free);
if (!map)
return log_oom();
r = sd_event_default(&event);
if (r < 0)
return log_error_errno(r, "Failed to get event loop: %m");
r = sd_bus_attach_event(bus, event, 0);
if (r < 0)
return log_error_errno(r, "Failed to attach bus to event loop: %m");
r = sd_event_set_signal_exit(event, true);
if (r < 0)
return log_error_errno(r, "Failed to set up interrupt handler: %m");
for (size_t i = 0; i < n; i++) {
_cleanup_(operation_freep) Operation *op = NULL;
op = operation_new(map, bus, &remaining, target_paths[i], targets[i]);
if (!op)
return log_oom();
/* Sign up for notification when the associated job finishes */
r = bus_match_signal_async(
op->bus, &op->job_finished_slot, bus_sysupdate_mgr, "JobRemoved", update_finished, NULL, op);
if (r < 0)
return log_bus_error(r, NULL, op->target_id, "listen for JobRemoved");
r = sd_bus_call_method_async(
bus,
NULL,
bus_sysupdate_mgr->destination,
target_paths[i],
SYSUPDATE_TARGET_INTERFACE,
"Update",
update_started,
op,
"st",
versions[i],
0LU);
if (r < 0)
return log_bus_error(r, NULL, targets[i], "call Update");
TAKE_PTR(op);
remaining++;
}
/* Set up the rendering */
r = sd_event_add_post(event, NULL, update_render_progress, map);
if (r < 0)
return log_error_errno(r, "Failed to add progress rendering callback: %m");
r = sd_event_add_exit(event, &render_exit, update_render_progress, map);
if (r < 0)
return log_error_errno(r, "Failed to add exit callback: %m");
r = sd_event_source_set_priority(render_exit, SD_EVENT_PRIORITY_IDLE);
if (r < 0)
return log_error_errno(r, "Failed to set priority of update job");
r = sd_event_loop(event);
if (r < 0)
return log_error_errno(r, "Failed to start event loop");
ORDERED_HASHMAP_FOREACH(p, map) {
r = PTR_TO_INT(p);
if (r == -EALREADY)
continue;
if (r == UPDATE_PROGRESS_FAILED)
return EXIT_FAILURE;
if (r < 0)
return r;
did_anything = true;
}
if (arg_reboot) {
if (did_anything)
return reboot_now();
log_info("Nothing was updated... skipping reboot.");
}
return 0;
}
static int verb_vacuum(int argc, char **argv, void *userdata) {
sd_bus *bus = ASSERT_PTR(userdata);
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_strv_free_ char **targets = NULL, **target_paths = NULL;
size_t n;
int r;
r = ensure_targets(bus, argv + 1, &targets);
if (r < 0)
return log_error_errno(r, "Failed to find targets: %m");
r = parse_targets(targets, &n, &target_paths, /* ret_versions= */ NULL);
if (r < 0)
return log_error_errno(r, "Failed to parse targets: %m");
for (size_t i = 0; i < n; i++) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
uint32_t count;
r = sd_bus_call_method(bus, bus_sysupdate_mgr->destination, target_paths[i], SYSUPDATE_TARGET_INTERFACE, "Vacuum", &error, &reply, NULL);
if (r < 0)
return log_bus_error(r, &error, targets[i], "call Vacuum");
r = sd_bus_message_read(reply, "u", &count);
if (r < 0)
return bus_log_parse_error(r);
log_info("Deleted %u instance(s) of %s.\n", count, targets[i]);
}
return 0;
}
static int help(void) {
_cleanup_free_ char *link = NULL;
int r;
r = terminal_urlify_man("updatectl", "1", &link);
if (r < 0)
return log_oom();
printf("%1$s [OPTIONS...] [VERSION]\n"
"\n%5$sManage system updates.%6$s\n"
"\n%3$sCommands:%4$s\n"
" list [TARGET[@VERSION]] List available targets and versions\n"
" check [TARGET...] Check for updates\n"
" update [TARGET[@VERSION]...] Install updates\n"
" vacuum [TARGET...] Clean up old updates\n"
" -h --help Show this help\n"
" --version Show package version\n"
"\n%3$sOptions:%4$s\n"
" --reboot Reboot after updating to newer version\n"
" --offline Do not fetch metadata from the network\n"
" -H --host=[USER@]HOST Operate on remote host\n"
" --no-pager Do not pipe output into a pager\n"
" --no-legend Do not show the headers and footers\n"
"\nSee the %2$s for details.\n"
, program_invocation_short_name
, link
, ansi_underline(), ansi_normal()
, ansi_highlight(), ansi_normal()
);
return 0;
}
static int parse_argv(int argc, char *argv[]) {
enum {
ARG_VERSION = 0x100,
ARG_NO_PAGER,
ARG_NO_LEGEND,
ARG_REBOOT,
ARG_OFFLINE,
};
static const struct option options[] = {
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, ARG_VERSION },
{ "no-pager", no_argument, NULL, ARG_NO_PAGER },
{ "no-legend", no_argument, NULL, ARG_NO_LEGEND },
{ "host", required_argument, NULL, 'H' },
{ "reboot", no_argument, NULL, ARG_REBOOT },
{ "offline", no_argument, NULL, ARG_OFFLINE },
{}
};
int c;
assert(argc >= 0);
assert(argv);
while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0) {
switch (c) {
case 'h':
return help();
case ARG_VERSION:
return version();
case ARG_NO_PAGER:
arg_pager_flags |= PAGER_DISABLE;
break;
case ARG_NO_LEGEND:
arg_legend = false;
break;
case 'H':
arg_transport = BUS_TRANSPORT_REMOTE;
arg_host = optarg;
break;
case ARG_REBOOT:
arg_reboot = true;
break;
case ARG_OFFLINE:
arg_offline = true;
break;
case '?':
return -EINVAL;
default:
assert_not_reached();
}
}
return 1;
}
static int run(int argc, char *argv[]) {
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
int r;
static const Verb verbs[] = {
{ "list", VERB_ANY, 2, VERB_DEFAULT|VERB_ONLINE_ONLY, verb_list },
{ "check", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_check },
{ "update", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_update },
{ "vacuum", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_vacuum },
{}
};
setlocale(LC_ALL, "");
log_setup();
(void) signal(SIGWINCH, columns_lines_cache_reset);
r = parse_argv(argc, argv);
if (r <= 0)
return r;
r = bus_connect_transport(arg_transport, arg_host, RUNTIME_SCOPE_SYSTEM, &bus);
if (r < 0)
return bus_log_connect_error(r, arg_transport, RUNTIME_SCOPE_SYSTEM);
if (arg_transport == BUS_TRANSPORT_LOCAL)
polkit_agent_open();
return dispatch_verb(argc, argv, verbs, bus);
}
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);
|