-
Notifications
You must be signed in to change notification settings - Fork 409
/
fwupd-client.c
6403 lines (5855 loc) · 185 KB
/
fwupd-client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2016 Richard Hughes <richard@hughsie.com>
*
* SPDX-License-Identifier: LGPL-2.1+
*/
#include "config.h"
#include <gio/gio.h>
#include <gmodule.h>
#ifdef HAVE_LIBCURL
#include <curl/curl.h>
#endif
#ifdef HAVE_GIO_UNIX
#include <gio/gunixfdlist.h>
#endif
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "fwupd-bios-setting-private.h"
#include "fwupd-client-private.h"
#include "fwupd-client-sync.h"
#include "fwupd-common-private.h"
#include "fwupd-device-private.h"
#include "fwupd-enums-private.h"
#include "fwupd-error.h"
#include "fwupd-plugin-private.h"
#include "fwupd-release-private.h"
#include "fwupd-remote-private.h"
#include "fwupd-request-private.h"
#include "fwupd-security-attr-private.h"
static void
fwupd_client_fixup_dbus_error(GError *error);
typedef GObject *(*FwupdClientObjectNewFunc)(void);
#define FWUPD_CLIENT_DBUS_PROXY_TIMEOUT 180000 /* ms */
/**
* FwupdClient:
*
* Allow client code to call the daemon methods.
*
* See also: [class@FwupdDevice]
*/
static void
fwupd_client_finalize(GObject *object);
typedef struct {
GMainContext *main_ctx;
FwupdStatus status;
gboolean tainted;
gboolean interactive;
guint percentage;
guint32 battery_level;
guint32 battery_threshold;
GMutex idle_mutex; /* for @idle_id and @idle_sources */
guint idle_id;
GPtrArray *idle_sources; /* element-type FwupdClientContextHelper */
gchar *daemon_version;
gchar *host_bkc;
gchar *host_product;
gchar *host_vendor;
gchar *host_machine_id;
gchar *host_security_id;
gboolean only_trusted;
GMutex proxy_mutex; /* for @proxy */
GDBusProxy *proxy;
GProxyResolver *proxy_resolver;
gchar *package_name;
gchar *package_version;
gchar *user_agent;
GHashTable *hints; /* str:str */
#ifdef SOUP_SESSION_COMPAT
GObject *soup_session;
GModule *soup_module; /* we leak this */
#endif
} FwupdClientPrivate;
#ifdef HAVE_LIBCURL
typedef struct {
GPtrArray *urls;
CURL *curl;
curl_mime *mime;
struct curl_slist *headers;
} FwupdCurlHelper;
#endif
enum {
SIGNAL_CHANGED,
SIGNAL_STATUS_CHANGED,
SIGNAL_DEVICE_ADDED,
SIGNAL_DEVICE_REMOVED,
SIGNAL_DEVICE_CHANGED,
SIGNAL_DEVICE_REQUEST,
SIGNAL_LAST
};
enum {
PROP_0,
PROP_STATUS,
PROP_PERCENTAGE,
PROP_DAEMON_VERSION,
PROP_TAINTED,
PROP_SOUP_SESSION, /* compat ABI, do not use! */
PROP_HOST_PRODUCT,
PROP_HOST_VENDOR,
PROP_HOST_MACHINE_ID,
PROP_HOST_SECURITY_ID,
PROP_HOST_BKC,
PROP_INTERACTIVE,
PROP_ONLY_TRUSTED,
PROP_BATTERY_LEVEL,
PROP_BATTERY_THRESHOLD,
PROP_LAST
};
static guint signals[SIGNAL_LAST] = {0};
G_DEFINE_TYPE_WITH_PRIVATE(FwupdClient, fwupd_client, G_TYPE_OBJECT)
#define GET_PRIVATE(o) (fwupd_client_get_instance_private(o))
#ifdef HAVE_LIBCURL_7_62_0
G_DEFINE_AUTOPTR_CLEANUP_FUNC(CURLU, curl_url_cleanup)
#endif
#ifdef HAVE_LIBCURL
static void
fwupd_client_curl_helper_free(FwupdCurlHelper *helper)
{
if (helper->curl != NULL)
curl_easy_cleanup(helper->curl);
if (helper->mime != NULL)
curl_mime_free(helper->mime);
if (helper->headers != NULL)
curl_slist_free_all(helper->headers);
if (helper->urls != NULL)
g_ptr_array_unref(helper->urls);
g_free(helper);
}
G_DEFINE_AUTOPTR_CLEANUP_FUNC(FwupdCurlHelper, fwupd_client_curl_helper_free)
#endif
typedef struct {
FwupdClient *self;
gchar *property_name;
guint signal_id;
GObject *payload;
} FwupdClientContextHelper;
static void
fwupd_client_context_helper_free(FwupdClientContextHelper *helper)
{
g_clear_object(&helper->payload);
g_object_unref(helper->self);
g_free(helper->property_name);
g_free(helper);
}
/* always executed in the main context given by priv->main_ctx */
static void
fwupd_client_context_object_notify(FwupdClient *self, const gchar *property_name)
{
FwupdClientPrivate *priv = GET_PRIVATE(self);
g_return_if_fail(g_main_context_is_owner(priv->main_ctx));
/* property */
g_object_notify(G_OBJECT(self), property_name);
/* legacy signal name */
if (g_strcmp0(property_name, "status") == 0)
g_signal_emit(self, signals[SIGNAL_STATUS_CHANGED], 0, priv->status);
}
/* emits all pending context helpers in the correct GMainContext */
static gboolean
fwupd_client_context_idle_cb(gpointer user_data)
{
FwupdClient *self = FWUPD_CLIENT(user_data);
FwupdClientPrivate *priv = GET_PRIVATE(self);
g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->idle_mutex);
g_assert(locker != NULL);
for (guint i = 0; i < priv->idle_sources->len; i++) {
FwupdClientContextHelper *helper = g_ptr_array_index(priv->idle_sources, i);
/* property */
if (helper->property_name != NULL)
fwupd_client_context_object_notify(self, helper->property_name);
/* payload signal */
if (helper->signal_id != 0 && helper->payload != NULL)
g_signal_emit(self, signals[helper->signal_id], 0, helper->payload);
}
/* all done */
g_ptr_array_set_size(priv->idle_sources, 0);
priv->idle_id = 0;
return G_SOURCE_REMOVE;
}
static void
fwupd_client_context_helper(FwupdClient *self, FwupdClientContextHelper *helper)
{
FwupdClientPrivate *priv = GET_PRIVATE(self);
g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->idle_mutex);
g_assert(locker != NULL);
/* no source already attached to the context */
if (priv->idle_id == 0) {
g_autoptr(GSource) source = g_idle_source_new();
g_source_set_callback(source, fwupd_client_context_idle_cb, self, NULL);
priv->idle_id = g_source_attach(g_steal_pointer(&source), priv->main_ctx);
}
/* run in the correct GMainContext and thread */
g_ptr_array_add(priv->idle_sources, helper);
}
/* run callback in the correct thread */
static void
fwupd_client_object_notify(FwupdClient *self, const gchar *property_name)
{
FwupdClientPrivate *priv = GET_PRIVATE(self);
FwupdClientContextHelper *helper = NULL;
/* shortcut */
if (g_main_context_is_owner(priv->main_ctx)) {
fwupd_client_context_object_notify(self, property_name);
return;
}
/* run in the correct GMainContext and thread */
helper = g_new0(FwupdClientContextHelper, 1);
helper->self = g_object_ref(self);
helper->property_name = g_strdup(property_name);
fwupd_client_context_helper(self, helper);
}
/* run callback in the correct thread */
static void
fwupd_client_signal_emit_object(FwupdClient *self, guint signal_id, GObject *payload)
{
FwupdClientPrivate *priv = GET_PRIVATE(self);
FwupdClientContextHelper *helper = NULL;
/* shortcut */
if (g_main_context_is_owner(priv->main_ctx)) {
g_signal_emit(self, signals[signal_id], 0, payload);
return;
}
/* run in the correct GMainContext and thread */
helper = g_new0(FwupdClientContextHelper, 1);
helper->self = g_object_ref(self);
helper->signal_id = signal_id;
helper->payload = g_object_ref(payload);
fwupd_client_context_helper(self, helper);
}
static void
fwupd_client_rebuild_user_agent(FwupdClient *self)
{
FwupdClientPrivate *priv = GET_PRIVATE(self);
g_autoptr(GString) str = g_string_new(NULL);
g_autofree gchar *system = NULL;
/* application name and version */
if (priv->package_name != NULL && priv->package_version != NULL)
g_string_append_printf(str, "%s/%s ", priv->package_name, priv->package_version);
/* system information */
system = fwupd_build_user_agent_system();
if (system != NULL)
g_string_append_printf(str, "(%s) ", system);
/* platform, unless the application name is fwupd itself */
if (priv->daemon_version != NULL && g_strcmp0(priv->package_name, "fwupd") != 0)
g_string_append_printf(str, "fwupd/%s", priv->daemon_version);
/* success */
g_free(priv->user_agent);
priv->user_agent = g_string_free(g_steal_pointer(&str), FALSE);
}
static void
fwupd_client_set_host_vendor(FwupdClient *self, const gchar *host_vendor)
{
FwupdClientPrivate *priv = GET_PRIVATE(self);
/* not changed */
if (g_strcmp0(priv->host_vendor, host_vendor) == 0)
return;
g_free(priv->host_vendor);
priv->host_vendor = g_strdup(host_vendor);
fwupd_client_object_notify(self, "host-vendor");
}
static void
fwupd_client_set_host_product(FwupdClient *self, const gchar *host_product)
{
FwupdClientPrivate *priv = GET_PRIVATE(self);
/* not changed */
if (g_strcmp0(priv->host_product, host_product) == 0)
return;
g_free(priv->host_product);
priv->host_product = g_strdup(host_product);
fwupd_client_object_notify(self, "host-product");
}
static void
fwupd_client_set_host_machine_id(FwupdClient *self, const gchar *host_machine_id)
{
FwupdClientPrivate *priv = GET_PRIVATE(self);
/* not changed */
if (g_strcmp0(priv->host_machine_id, host_machine_id) == 0)
return;
g_free(priv->host_machine_id);
priv->host_machine_id = g_strdup(host_machine_id);
fwupd_client_object_notify(self, "host-machine-id");
}
static void
fwupd_client_set_host_security_id(FwupdClient *self, const gchar *host_security_id)
{
FwupdClientPrivate *priv = GET_PRIVATE(self);
/* not changed */
if (g_strcmp0(priv->host_security_id, host_security_id) == 0)
return;
g_free(priv->host_security_id);
priv->host_security_id = g_strdup(host_security_id);
fwupd_client_object_notify(self, "host-security-id");
}
/**
* fwupd_client_set_daemon_version:
* @self: a #FwupdClient
* @daemon_version: A semantic version, e.g. "1.2.3"
*
* Sets the daemon version number.
*
* Since: 1.8.11
**/
void
fwupd_client_set_daemon_version(FwupdClient *self, const gchar *daemon_version)
{
FwupdClientPrivate *priv = GET_PRIVATE(self);
/* not changed */
if (g_strcmp0(priv->daemon_version, daemon_version) == 0)
return;
g_free(priv->daemon_version);
priv->daemon_version = g_strdup(daemon_version);
fwupd_client_object_notify(self, "daemon-version");
fwupd_client_rebuild_user_agent(self);
}
static void
fwupd_client_set_host_bkc(FwupdClient *self, const gchar *host_bkc)
{
FwupdClientPrivate *priv = GET_PRIVATE(self);
/* emulate a D-Bus maybe type */
if (g_strcmp0(host_bkc, "") == 0)
host_bkc = NULL;
/* not changed */
if (g_strcmp0(priv->host_bkc, host_bkc) == 0)
return;
g_free(priv->host_bkc);
priv->host_bkc = g_strdup(host_bkc);
fwupd_client_object_notify(self, "host-bkc");
}
static void
fwupd_client_set_status(FwupdClient *self, FwupdStatus status)
{
FwupdClientPrivate *priv = GET_PRIVATE(self);
if (priv->status == status)
return;
priv->status = status;
g_debug("Emitting ::status-changed() [%s]", fwupd_status_to_string(priv->status));
fwupd_client_object_notify(self, "status");
}
static void
fwupd_client_set_percentage(FwupdClient *self, guint percentage)
{
FwupdClientPrivate *priv = GET_PRIVATE(self);
if (priv->percentage == percentage)
return;
priv->percentage = percentage;
fwupd_client_object_notify(self, "percentage");
}
static void
fwupd_client_set_battery_level(FwupdClient *self, guint32 battery_level)
{
FwupdClientPrivate *priv = GET_PRIVATE(self);
if (priv->battery_level == battery_level)
return;
priv->battery_level = battery_level;
g_object_notify(G_OBJECT(self), "battery-level");
}
static void
fwupd_client_set_battery_threshold(FwupdClient *self, guint32 battery_threshold)
{
FwupdClientPrivate *priv = GET_PRIVATE(self);
if (priv->battery_threshold == battery_threshold)
return;
priv->battery_threshold = battery_threshold;
g_object_notify(G_OBJECT(self), "battery-threshold");
}
static void
fwupd_client_properties_changed_cb(GDBusProxy *proxy,
GVariant *changed_properties,
GStrv invalidated_properties,
FwupdClient *self)
{
FwupdClientPrivate *priv = GET_PRIVATE(self);
g_autoptr(GVariantDict) dict = NULL;
/* print to the console */
dict = g_variant_dict_new(changed_properties);
if (g_variant_dict_contains(dict, "Status")) {
g_autoptr(GVariant) val = NULL;
val = g_dbus_proxy_get_cached_property(proxy, "Status");
if (val != NULL)
fwupd_client_set_status(self, g_variant_get_uint32(val));
}
if (g_variant_dict_contains(dict, "Tainted")) {
g_autoptr(GVariant) val = NULL;
val = g_dbus_proxy_get_cached_property(proxy, "Tainted");
if (val != NULL) {
priv->tainted = g_variant_get_boolean(val);
fwupd_client_object_notify(self, "tainted");
}
}
if (g_variant_dict_contains(dict, "Interactive")) {
g_autoptr(GVariant) val = NULL;
val = g_dbus_proxy_get_cached_property(proxy, "Interactive");
if (val != NULL) {
priv->interactive = g_variant_get_boolean(val);
fwupd_client_object_notify(self, "interactive");
}
}
if (g_variant_dict_contains(dict, "Percentage")) {
g_autoptr(GVariant) val = NULL;
val = g_dbus_proxy_get_cached_property(proxy, "Percentage");
if (val != NULL)
fwupd_client_set_percentage(self, g_variant_get_uint32(val));
}
if (g_variant_dict_contains(dict, FWUPD_RESULT_KEY_BATTERY_LEVEL)) {
g_autoptr(GVariant) val = NULL;
val = g_dbus_proxy_get_cached_property(proxy, FWUPD_RESULT_KEY_BATTERY_LEVEL);
if (val != NULL)
fwupd_client_set_battery_level(self, g_variant_get_uint32(val));
}
if (g_variant_dict_contains(dict, FWUPD_RESULT_KEY_BATTERY_THRESHOLD)) {
g_autoptr(GVariant) val = NULL;
val = g_dbus_proxy_get_cached_property(proxy, FWUPD_RESULT_KEY_BATTERY_THRESHOLD);
if (val != NULL)
fwupd_client_set_battery_threshold(self, g_variant_get_uint32(val));
}
if (g_variant_dict_contains(dict, "DaemonVersion")) {
g_autoptr(GVariant) val = NULL;
val = g_dbus_proxy_get_cached_property(proxy, "DaemonVersion");
if (val != NULL)
fwupd_client_set_daemon_version(self, g_variant_get_string(val, NULL));
}
if (g_variant_dict_contains(dict, "HostBkc")) {
g_autoptr(GVariant) val = g_dbus_proxy_get_cached_property(proxy, "HostBkc");
if (val != NULL)
fwupd_client_set_host_bkc(self, g_variant_get_string(val, NULL));
}
if (g_variant_dict_contains(dict, "HostVendor")) {
g_autoptr(GVariant) val = NULL;
val = g_dbus_proxy_get_cached_property(proxy, "HostVendor");
if (val != NULL)
fwupd_client_set_host_vendor(self, g_variant_get_string(val, NULL));
}
if (g_variant_dict_contains(dict, "HostProduct")) {
g_autoptr(GVariant) val = NULL;
val = g_dbus_proxy_get_cached_property(proxy, "HostProduct");
if (val != NULL)
fwupd_client_set_host_product(self, g_variant_get_string(val, NULL));
}
if (g_variant_dict_contains(dict, "HostMachineId")) {
g_autoptr(GVariant) val = NULL;
val = g_dbus_proxy_get_cached_property(proxy, "HostMachineId");
if (val != NULL)
fwupd_client_set_host_machine_id(self, g_variant_get_string(val, NULL));
}
if (g_variant_dict_contains(dict, "HostSecurityId")) {
g_autoptr(GVariant) val = NULL;
val = g_dbus_proxy_get_cached_property(proxy, "HostSecurityId");
if (val != NULL)
fwupd_client_set_host_security_id(self, g_variant_get_string(val, NULL));
}
if (g_variant_dict_contains(dict, "OnlyTrusted")) {
g_autoptr(GVariant) val = NULL;
val = g_dbus_proxy_get_cached_property(proxy, "OnlyTrusted");
if (val != NULL) {
priv->only_trusted = g_variant_get_boolean(val);
fwupd_client_object_notify(self, "only-trusted");
}
}
}
static void
fwupd_client_signal_cb(GDBusProxy *proxy,
const gchar *sender_name,
const gchar *signal_name,
GVariant *parameters,
FwupdClient *self)
{
g_autoptr(FwupdDevice) dev = NULL;
if (g_strcmp0(signal_name, "Changed") == 0) {
g_debug("Emitting ::changed()");
g_signal_emit(self, signals[SIGNAL_CHANGED], 0);
return;
}
if (g_strcmp0(signal_name, "DeviceAdded") == 0) {
dev = fwupd_device_from_variant(parameters);
g_debug("Emitting ::device-added(%s)", fwupd_device_get_id(dev));
fwupd_client_signal_emit_object(self, SIGNAL_DEVICE_ADDED, G_OBJECT(dev));
return;
}
if (g_strcmp0(signal_name, "DeviceRemoved") == 0) {
dev = fwupd_device_from_variant(parameters);
g_debug("Emitting ::device-removed(%s)", fwupd_device_get_id(dev));
fwupd_client_signal_emit_object(self, SIGNAL_DEVICE_REMOVED, G_OBJECT(dev));
return;
}
if (g_strcmp0(signal_name, "DeviceChanged") == 0) {
dev = fwupd_device_from_variant(parameters);
g_debug("Emitting ::device-changed(%s)", fwupd_device_get_id(dev));
fwupd_client_signal_emit_object(self, SIGNAL_DEVICE_CHANGED, G_OBJECT(dev));
return;
}
if (g_strcmp0(signal_name, "DeviceRequest") == 0) {
g_autoptr(FwupdRequest) req = fwupd_request_from_variant(parameters);
g_debug("Emitting ::device-request(%s)", fwupd_request_get_id(req));
fwupd_client_signal_emit_object(self, SIGNAL_DEVICE_REQUEST, G_OBJECT(req));
return;
}
g_debug("Unknown signal name '%s' from %s", signal_name, sender_name);
}
/**
* fwupd_client_get_main_context:
* @self: a #FwupdClient
*
* Gets the internal #GMainContext to use for synchronous methods.
* By default the value is set a new #GMainContext.
*
* Returns: (transfer full): the main context
*
* Since: 1.5.3
**/
GMainContext *
fwupd_client_get_main_context(FwupdClient *self)
{
FwupdClientPrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FWUPD_IS_CLIENT(self), NULL);
if (priv->main_ctx != NULL)
return g_main_context_ref(priv->main_ctx);
return g_main_context_new();
}
/**
* fwupd_client_set_main_context:
* @self: a #FwupdClient
* @main_ctx: (nullable): the global default main context to use
*
* Sets the internal main context to use for returning progress signals.
*
* Since: 1.5.3
**/
void
fwupd_client_set_main_context(FwupdClient *self, GMainContext *main_ctx)
{
FwupdClientPrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FWUPD_IS_CLIENT(self));
if (main_ctx == priv->main_ctx)
return;
g_clear_pointer(&priv->main_ctx, g_main_context_unref);
if (main_ctx != NULL)
priv->main_ctx = g_main_context_ref(main_ctx);
}
/**
* fwupd_client_ensure_networking:
* @self: a #FwupdClient
* @error: (nullable): optional return location for an error
*
* Sets up the client networking support ready for use. Most other download and
* upload methods call this automatically, and do you only need to call this if
* the session is being used outside the [class@FwupdClient].
*
* Returns: %TRUE for success
*
* Since: 1.4.5
**/
gboolean
fwupd_client_ensure_networking(FwupdClient *self, GError **error)
{
FwupdClientPrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FWUPD_IS_CLIENT(self), FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
/* check the user agent is sane */
if (priv->user_agent == NULL) {
g_set_error_literal(error, FWUPD_ERROR, FWUPD_ERROR_INTERNAL, "user agent unset");
return FALSE;
}
if (g_strstr_len(priv->user_agent, -1, "fwupd/") == NULL) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_INTERNAL,
"user agent unsuitable; fwupd version required");
return FALSE;
}
#ifdef SOUP_SESSION_COMPAT
if (priv->soup_session != NULL) {
g_object_set(priv->soup_session, "user-agent", priv->user_agent, NULL);
}
#endif
return TRUE;
}
#ifdef HAVE_LIBCURL
static int
fwupd_client_progress_callback_cb(void *clientp,
curl_off_t dltotal,
curl_off_t dlnow,
curl_off_t ultotal,
curl_off_t ulnow)
{
FwupdClient *self = FWUPD_CLIENT(clientp);
/* calculate percentage */
if (dltotal > 0 && dlnow >= 0 && dlnow <= dltotal) {
guint percentage = (guint)((100 * dlnow) / dltotal);
g_debug("download progress: %u%%", percentage);
fwupd_client_set_percentage(self, percentage);
} else if (ultotal > 0 && ulnow >= 0 && ulnow <= ultotal) {
guint percentage = (guint)((100 * ulnow) / ultotal);
g_debug("upload progress: %u%%", percentage);
fwupd_client_set_percentage(self, percentage);
}
return 0;
}
static void
fwupd_client_curl_helper_set_proxy(FwupdClient *self, FwupdCurlHelper *helper, const gchar *url)
{
FwupdClientPrivate *priv = GET_PRIVATE(self);
g_auto(GStrv) proxies = NULL;
g_autoptr(GError) error_local = NULL;
proxies = g_proxy_resolver_lookup(priv->proxy_resolver, url, NULL, &error_local);
if (proxies == NULL) {
g_warning("failed to lookup proxy for %s: %s", url, error_local->message);
return;
}
if (g_strcmp0(proxies[0], "direct://") != 0)
(void)curl_easy_setopt(helper->curl, CURLOPT_PROXY, proxies[0]);
}
static FwupdCurlHelper *
fwupd_client_curl_new(FwupdClient *self, GError **error)
{
FwupdClientPrivate *priv = GET_PRIVATE(self);
g_autoptr(FwupdCurlHelper) helper = g_new0(FwupdCurlHelper, 1);
/* check the user agent is sane */
if (!fwupd_client_ensure_networking(self, error))
return NULL;
/* create the session */
helper->curl = curl_easy_init();
if (helper->curl == NULL) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_INTERNAL,
"failed to setup networking");
return NULL;
}
if (g_getenv("FWUPD_CURL_VERBOSE") != NULL)
(void)curl_easy_setopt(helper->curl, CURLOPT_VERBOSE, 1L);
(void)curl_easy_setopt(helper->curl,
CURLOPT_XFERINFOFUNCTION,
fwupd_client_progress_callback_cb);
(void)curl_easy_setopt(helper->curl, CURLOPT_XFERINFODATA, self);
(void)curl_easy_setopt(helper->curl, CURLOPT_USERAGENT, priv->user_agent);
(void)curl_easy_setopt(helper->curl, CURLOPT_CONNECTTIMEOUT, 60L);
(void)curl_easy_setopt(helper->curl, CURLOPT_NOPROGRESS, 0L);
(void)curl_easy_setopt(helper->curl, CURLOPT_FOLLOWLOCATION, 1L);
(void)curl_easy_setopt(helper->curl, CURLOPT_MAXREDIRS, 5L);
#if CURL_AT_LEAST_VERSION(7, 71, 0)
(void)curl_easy_setopt(helper->curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA);
#endif
/* relax the SSL checks for broken corporate proxies */
if (g_getenv("DISABLE_SSL_STRICT") != NULL)
(void)curl_easy_setopt(helper->curl, CURLOPT_SSL_VERIFYPEER, 0L);
/* this disables the double-compression of the firmware.xml.gz file */
(void)curl_easy_setopt(helper->curl, CURLOPT_HTTP_CONTENT_DECODING, 0L);
return g_steal_pointer(&helper);
}
#endif
static void
fwupd_client_set_hints_cb(GObject *source, GAsyncResult *res, gpointer user_data)
{
g_autoptr(GTask) task = G_TASK(user_data);
g_autoptr(GError) error = NULL;
g_autoptr(GVariant) val = NULL;
val = g_dbus_proxy_call_finish(G_DBUS_PROXY(source), res, &error);
if (val == NULL) {
/* new libfwupd and old daemon, just swallow the error */
if (g_error_matches(error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD)) {
g_debug("ignoring %s", error->message);
g_task_return_boolean(task, TRUE);
return;
}
g_task_return_error(task, g_steal_pointer(&error));
return;
}
/* success */
g_task_return_boolean(task, TRUE);
}
static void
fwupd_client_connect_get_proxy_cb(GObject *source, GAsyncResult *res, gpointer user_data)
{
g_autoptr(GTask) task = G_TASK(user_data);
GVariantBuilder builder;
GHashTableIter iter;
gpointer key, value;
FwupdClient *self = g_task_get_source_object(task);
FwupdClientPrivate *priv = GET_PRIVATE(self);
GCancellable *cancellable = g_task_get_cancellable(task);
g_autoptr(GDBusProxy) proxy = NULL;
g_autoptr(GError) error = NULL;
g_autoptr(GVariant) val = NULL;
g_autoptr(GVariant) val2 = NULL;
g_autoptr(GVariant) val3 = NULL;
g_autoptr(GVariant) val4 = NULL;
g_autoptr(GVariant) val5 = NULL;
g_autoptr(GVariant) val6 = NULL;
g_autoptr(GVariant) val7 = NULL;
g_autoptr(GVariant) val8 = NULL;
g_autoptr(GVariant) val9 = NULL;
g_autoptr(GVariant) val10 = NULL;
g_autoptr(GMutexLocker) locker = NULL;
proxy = g_dbus_proxy_new_finish(res, &error);
if (proxy == NULL) {
g_task_return_error(task, g_steal_pointer(&error));
return;
}
/* another thread did this for us */
locker = g_mutex_locker_new(&priv->proxy_mutex);
if (locker == NULL || priv->proxy != NULL) {
g_task_return_boolean(task, TRUE);
return;
}
priv->proxy = g_steal_pointer(&proxy);
/* connect signals, etc. */
g_signal_connect(G_DBUS_PROXY(priv->proxy),
"g-properties-changed",
G_CALLBACK(fwupd_client_properties_changed_cb),
self);
g_signal_connect(G_DBUS_PROXY(priv->proxy),
"g-signal",
G_CALLBACK(fwupd_client_signal_cb),
self);
val = g_dbus_proxy_get_cached_property(priv->proxy, "DaemonVersion");
if (val != NULL)
fwupd_client_set_daemon_version(self, g_variant_get_string(val, NULL));
val2 = g_dbus_proxy_get_cached_property(priv->proxy, "Tainted");
if (val2 != NULL)
priv->tainted = g_variant_get_boolean(val2);
val3 = g_dbus_proxy_get_cached_property(priv->proxy, "Status");
if (val3 != NULL)
fwupd_client_set_status(self, g_variant_get_uint32(val3));
val4 = g_dbus_proxy_get_cached_property(priv->proxy, "Interactive");
if (val4 != NULL)
priv->interactive = g_variant_get_boolean(val4);
val5 = g_dbus_proxy_get_cached_property(priv->proxy, "HostProduct");
if (val5 != NULL)
fwupd_client_set_host_product(self, g_variant_get_string(val5, NULL));
val10 = g_dbus_proxy_get_cached_property(priv->proxy, "HostVendor");
if (val10 != NULL)
fwupd_client_set_host_vendor(self, g_variant_get_string(val10, NULL));
val6 = g_dbus_proxy_get_cached_property(priv->proxy, "HostMachineId");
if (val6 != NULL)
fwupd_client_set_host_machine_id(self, g_variant_get_string(val6, NULL));
val7 = g_dbus_proxy_get_cached_property(priv->proxy, "HostSecurityId");
if (val7 != NULL)
fwupd_client_set_host_security_id(self, g_variant_get_string(val7, NULL));
val8 = g_dbus_proxy_get_cached_property(priv->proxy, "HostBkc");
if (val8 != NULL)
fwupd_client_set_host_bkc(self, g_variant_get_string(val8, NULL));
val9 = g_dbus_proxy_get_cached_property(priv->proxy, "OnlyTrusted");
if (val9 != NULL)
priv->only_trusted = g_variant_get_boolean(val9);
/* build client hints */
g_variant_builder_init(&builder, G_VARIANT_TYPE("a{ss}"));
g_hash_table_iter_init(&iter, priv->hints);
while (g_hash_table_iter_next(&iter, &key, &value)) {
if (value == NULL)
continue;
g_variant_builder_add(&builder, "{ss}", (const gchar *)key, (const gchar *)value);
}
/* only supported on fwupd >= 1.7.1 */
g_dbus_proxy_call(priv->proxy,
"SetHints",
g_variant_new("(a{ss})", &builder),
G_DBUS_CALL_FLAGS_NONE,
FWUPD_CLIENT_DBUS_PROXY_TIMEOUT,
cancellable,
fwupd_client_set_hints_cb,
g_steal_pointer(&task));
}
static void
fwupd_client_connect_get_connection_cb(GObject *source, GAsyncResult *res, gpointer user_data)
{
g_autoptr(GTask) task = G_TASK(user_data);
GCancellable *cancellable = g_task_get_cancellable(task);
g_autoptr(GDBusConnection) connection = NULL;
g_autoptr(GError) error = NULL;
connection = g_dbus_connection_new_for_address_finish(res, &error);
if (connection == NULL) {
g_task_return_error(task, g_steal_pointer(&error));
return;
}
g_dbus_proxy_new(connection,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
NULL, /* bus_name */
FWUPD_DBUS_PATH,
FWUPD_DBUS_INTERFACE,
cancellable,
fwupd_client_connect_get_proxy_cb,
g_steal_pointer(&task));
}
/**
* fwupd_client_connect_async:
* @self: a #FwupdClient
* @cancellable: (nullable): optional #GCancellable
* @callback: (scope async) (closure callback_data): the function to run on completion
* @callback_data: the data to pass to @callback
*
* Sets up the client ready for use. This is probably the first method you call
* when wanting to use libfwupd in an asynchronous manner.
*
* Other methods such as [method@FwupdClient.get_devices_async] should only be called
* after [method@FwupdClient.connect_finish] has been called without an error.
*
* Since: 1.5.0
**/
void
fwupd_client_connect_async(FwupdClient *self,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer callback_data)
{
FwupdClientPrivate *priv = GET_PRIVATE(self);
const gchar *socket_filename = g_getenv("FWUPD_DBUS_SOCKET");
g_autofree gchar *socket_address = NULL;
g_autoptr(GTask) task = g_task_new(self, cancellable, callback, callback_data);
g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->proxy_mutex);
g_return_if_fail(FWUPD_IS_CLIENT(self));
g_return_if_fail(cancellable == NULL || G_IS_CANCELLABLE(cancellable));
g_assert(locker != NULL);
/* nothing to do */
if (priv->proxy != NULL) {
g_task_return_boolean(task, TRUE);
return;
}
/* convert from filename to address, if required */
if (socket_filename != NULL) {
if (g_strrstr(socket_filename, "=") == NULL) {
socket_address = g_strdup_printf("unix:path=%s", socket_filename);
} else {
socket_address = g_strdup(socket_filename);
}
} else {
#ifdef _WIN32
socket_address = g_strdup(FWUPD_DBUS_P2P_SOCKET_ADDRESS);
#endif
}
/* use peer-to-peer only if the env variable is set */
if (socket_address != NULL) {
g_dbus_connection_new_for_address(socket_address,
G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
NULL,
cancellable,
fwupd_client_connect_get_connection_cb,
g_steal_pointer(&task));
return;
}
/* typical case */
g_dbus_proxy_new_for_bus(G_BUS_TYPE_SYSTEM,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
FWUPD_DBUS_SERVICE,
FWUPD_DBUS_PATH,
FWUPD_DBUS_INTERFACE,
cancellable,
fwupd_client_connect_get_proxy_cb,
g_steal_pointer(&task));
}
/**
* fwupd_client_connect_finish:
* @self: a #FwupdClient
* @res: (not nullable): the asynchronous result
* @error: (nullable): optional return location for an error
*
* Gets the result of [method@Client.connect_async].
*
* Returns: %TRUE for success
*
* Since: 1.5.0
**/
gboolean
fwupd_client_connect_finish(FwupdClient *self, GAsyncResult *res, GError **error)
{
g_return_val_if_fail(FWUPD_IS_CLIENT(self), FALSE);
g_return_val_if_fail(g_task_is_valid(res, self), FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
return g_task_propagate_boolean(G_TASK(res), error);
}
/**
* fwupd_client_disconnect: (skip)
* @self: a #FwupdClient
* @error: (nullable): optional return location for an error
*
* Tears down client after use. You only need to call this method if you are:
*
* - connecting to the daemon in one thread and finalizing the client in another one
* - to change the `FWUPD_DBUS_SOCKET` for a different peer-to-peer connection
* - to add or change connection hints as specified by [method@FwupdClient.add_hint].
*
* Returns: %TRUE for success
*
* Since: 1.8.0
**/
gboolean
fwupd_client_disconnect(FwupdClient *self, GError **error)
{
FwupdClientPrivate *priv = GET_PRIVATE(self);
g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->proxy_mutex);
g_return_val_if_fail(FWUPD_IS_CLIENT(self), FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);