-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdnf_state.cpp
More file actions
481 lines (419 loc) · 17.7 KB
/
Copy pathdnf_state.cpp
File metadata and controls
481 lines (419 loc) · 17.7 KB
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
// -----------------------------------------------------------------------------
// src/dnf_backend/dnf_state.cpp
// Installed package cache and UI install-state helpers
//
// Owns backend global state used by the UI to mark exact installed packages,
// classify visible package rows, and prevent modifying the running application package from inside the app itself.
// -----------------------------------------------------------------------------
#include "dnf_backend/dnf_internal.hpp"
#include "dnf_backend/base_manager.hpp"
#include <exception>
#include <filesystem>
#include <map>
#include <mutex>
#include <set>
#include <string>
#ifdef DNFUI_BUILD_TESTS
#include <glib.h>
#endif
#include <libdnf5/rpm/package_query.hpp>
#include <libdnf5/rpm/nevra.hpp>
namespace {
struct InstalledStateSnapshot {
std::set<std::string> nevras;
std::map<std::string, PackageRow> rows_by_name_arch;
std::set<std::string> self_protected_package_names;
};
// Installed-package data published together after a complete installed scan.
InstalledStateSnapshot g_installed_state;
// Mutex for thread-safe access to the installed snapshot.
std::mutex g_installed_mutex;
// -----------------------------------------------------------------------------
// Return true when one package spec names a protected package.
// -----------------------------------------------------------------------------
static bool
package_spec_matches_protected_name(const std::string &spec, const std::set<std::string> &protected_names)
{
if (protected_names.count(spec) > 0) {
return true;
}
try {
for (const auto &nevra : libdnf5::rpm::Nevra::parse(spec)) {
if (protected_names.count(nevra.get_name()) > 0) {
return true;
}
}
} catch (const std::exception &) {
return false;
}
return false;
}
// -----------------------------------------------------------------------------
// Resolve the current GUI executable path so the app can block self-modification
// without hard-coding the RPM package name.
// -----------------------------------------------------------------------------
std::string
self_protected_file_path()
{
try {
return std::filesystem::canonical("/proc/self/exe").string();
} catch (const std::exception &) {
}
return {};
}
// -----------------------------------------------------------------------------
// Resolve all installed-state values for one row while holding g_installed_mutex.
// -----------------------------------------------------------------------------
InstalledPackageResolution
resolve_installed_package_locked(const PackageRow &row)
{
InstalledPackageResolution resolution;
resolution.exact_installed = g_installed_state.nevras.count(row.nevra) > 0;
resolution.self_protected = g_installed_state.self_protected_package_names.count(row.name) > 0;
auto installed_it = g_installed_state.rows_by_name_arch.find(row.name_arch_key());
if (installed_it != g_installed_state.rows_by_name_arch.end()) {
resolution.has_installed_row = true;
resolution.installed_row = installed_it->second;
}
if (resolution.exact_installed) {
if (resolution.has_installed_row && libdnf5::rpm::evrcmp(row, resolution.installed_row) < 0) {
resolution.state = PackageInstallState::INSTALLED;
return resolution;
}
switch (row.repo_candidate_relation) {
case PackageRepoCandidateRelation::UNKNOWN:
// Annotation was not run or failed. The package is known-installed, but the repo relation is unknown.
// Without a successful repo query, use INSTALLED so the UI does not misrepresent the package state.
case PackageRepoCandidateRelation::SAME:
resolution.state = PackageInstallState::INSTALLED;
break;
case PackageRepoCandidateRelation::NONE:
resolution.state = PackageInstallState::LOCAL_ONLY;
break;
case PackageRepoCandidateRelation::NEWER:
resolution.state = PackageInstallState::UPGRADEABLE;
break;
case PackageRepoCandidateRelation::OLDER:
resolution.state = PackageInstallState::INSTALLED_NEWER_THAN_REPO;
break;
default:
resolution.state = PackageInstallState::INSTALLED;
break;
}
return resolution;
}
if (!resolution.has_installed_row) {
resolution.state = PackageInstallState::AVAILABLE;
return resolution;
}
int cmp = libdnf5::rpm::evrcmp(row, resolution.installed_row);
if (cmp > 0) {
resolution.state = PackageInstallState::UPGRADEABLE;
} else if (cmp < 0) {
resolution.state = PackageInstallState::DOWNGRADEABLE;
} else {
resolution.state = PackageInstallState::INSTALLED_NEWER_THAN_REPO;
}
return resolution;
}
// -----------------------------------------------------------------------------
// Keep repo-derived reinstall availability across a local rpmdb-only refresh
// only when the refreshed row is the same exact installed package.
// -----------------------------------------------------------------------------
void
preserve_local_refresh_reinstall_availability_locked(dnf_backend_internal::InstalledQueryResult &installed)
{
for (auto &row : installed.rows) {
auto old_it = g_installed_state.rows_by_name_arch.find(row.name_arch_key());
if (old_it != g_installed_state.rows_by_name_arch.end() && old_it->second.nevra == row.nevra) {
row.repo_candidate_exact_available = old_it->second.repo_candidate_exact_available;
}
}
for (auto &[key, row] : installed.rows_by_name_arch) {
auto old_it = g_installed_state.rows_by_name_arch.find(key);
if (old_it != g_installed_state.rows_by_name_arch.end() && old_it->second.nevra == row.nevra) {
row.repo_candidate_exact_available = old_it->second.repo_candidate_exact_available;
}
}
}
// -----------------------------------------------------------------------------
// Publish installed-package state while g_installed_mutex is already held.
// -----------------------------------------------------------------------------
bool
publish_installed_snapshot_locked(dnf_backend_internal::InstalledQueryResult installed,
std::set<std::string> protected_names)
{
std::set<std::string> next_protected_names = g_installed_state.self_protected_package_names;
if (!protected_names.empty()) {
next_protected_names.insert(protected_names.begin(), protected_names.end());
}
const bool changed = g_installed_state.nevras != installed.nevras ||
g_installed_state.self_protected_package_names != next_protected_names;
g_installed_state.nevras.swap(installed.nevras);
g_installed_state.rows_by_name_arch.swap(installed.rows_by_name_arch);
g_installed_state.self_protected_package_names.swap(next_protected_names);
return changed;
}
} // namespace
namespace dnf_backend_internal {
// -----------------------------------------------------------------------------
// Collect installed package names that own the currently running GUI binary.
// The result is stored in the installed-state snapshot and used to block
// self-modification actions from inside the app.
// -----------------------------------------------------------------------------
std::set<std::string>
collect_self_protected_package_names(libdnf5::Base &base)
{
std::set<std::string> protected_names;
const std::string path = self_protected_file_path();
if (!path.empty()) {
libdnf5::rpm::PackageQuery query(base);
query.filter_installed();
query.filter_file(path);
for (const auto &pkg : query) {
protected_names.insert(pkg.get_name());
}
}
#ifdef DNFUI_BUILD_TESTS
// Let tests exercise self-protection with an installed package chosen by the test case.
// Production builds only use the running executable owner above.
const char *test_name = g_getenv("DNFUI_TEST_SELF_PROTECTED_PACKAGE_NAME");
if (test_name && *test_name) {
protected_names.insert(test_name);
}
#endif
return protected_names;
}
// -----------------------------------------------------------------------------
// Return protected package names, refreshing from rpmdb if installed state has
// not been published yet.
// -----------------------------------------------------------------------------
std::set<std::string>
self_protected_package_name_snapshot()
{
std::set<std::string> protected_names;
{
std::lock_guard<std::mutex> lock(g_installed_mutex);
protected_names = g_installed_state.self_protected_package_names;
}
if (!protected_names.empty()) {
return protected_names;
}
auto read = BaseManager::instance().acquire_system_only_read();
protected_names = collect_self_protected_package_names(*read.base);
if (!protected_names.empty()) {
std::lock_guard<std::mutex> lock(g_installed_mutex);
if (g_installed_state.self_protected_package_names.empty()) {
g_installed_state.self_protected_package_names = protected_names;
} else {
protected_names = g_installed_state.self_protected_package_names;
}
}
return protected_names;
}
// -----------------------------------------------------------------------------
// Publish installed-package state only after callers have finished all libdnf
// Base reads. Do not hold the Base lock while taking g_installed_mutex.
// The return value tracks installed identities and self-protection changes.
// -----------------------------------------------------------------------------
bool
publish_installed_snapshot(InstalledQueryResult installed, std::set<std::string> protected_names)
{
std::lock_guard<std::mutex> lock(g_installed_mutex);
return publish_installed_snapshot_locked(std::move(installed), std::move(protected_names));
}
// -----------------------------------------------------------------------------
// Publish a local rpmdb-only installed scan.
// A local refresh cannot prove repository availability, so keep the previous
// exact reinstall value only when the same installed NEVRA is still present.
// -----------------------------------------------------------------------------
bool
publish_local_installed_snapshot(InstalledQueryResult installed, std::set<std::string> protected_names)
{
std::lock_guard<std::mutex> lock(g_installed_mutex);
preserve_local_refresh_reinstall_availability_locked(installed);
return publish_installed_snapshot_locked(std::move(installed), std::move(protected_names));
}
} // namespace dnf_backend_internal
using namespace dnf_backend_internal;
#ifdef DNFUI_BUILD_TESTS
// -----------------------------------------------------------------------------
// Return the number of exact NEVRAs in the installed-package snapshot.
// -----------------------------------------------------------------------------
size_t
dnf_backend_installed_snapshot_size_for_tests()
{
std::lock_guard<std::mutex> lock(g_installed_mutex);
return g_installed_state.nevras.size();
}
// -----------------------------------------------------------------------------
// Return true when the installed-package snapshot contains the exact NEVRA.
// -----------------------------------------------------------------------------
bool
dnf_backend_installed_snapshot_contains_for_tests(const std::string &nevra)
{
std::lock_guard<std::mutex> lock(g_installed_mutex);
return g_installed_state.nevras.count(nevra) > 0;
}
#endif
// -----------------------------------------------------------------------------
// Refresh the installed snapshot used by UI state classification.
// Returns true when installed identities or self-protected package names changed.
// This path uses only the local rpmdb.
// The short-lived system-only Base prevents future queries from inheriting that mode.
//
// Thread-safety:
// The Base read lock and g_installed_mutex must never be held simultaneously.
// Installed rows are collected into local containers while the Base lock is
// held, then published after that lock has been released.
// -----------------------------------------------------------------------------
bool
dnf_backend_refresh_installed_snapshot()
{
InstalledQueryResult installed;
std::set<std::string> protected_names;
{
auto read = BaseManager::instance().acquire_system_only_read_after_dropping_cached_base();
libdnf5::Base &base = *read.base;
const DnfBackendSearchOptions search_options {};
installed = collect_installed_rows(base, nullptr, search_options);
protected_names = collect_self_protected_package_names(base);
} // Base read lock released before acquiring g_installed_mutex
return publish_local_installed_snapshot(installed, protected_names);
}
// -----------------------------------------------------------------------------
// Resolve installed-package state for one visible row from one installed snapshot.
// -----------------------------------------------------------------------------
InstalledPackageResolution
dnf_backend_resolve_installed_package(const PackageRow &row)
{
std::lock_guard<std::mutex> lock(g_installed_mutex);
return resolve_installed_package_locked(row);
}
// -----------------------------------------------------------------------------
// Return the default package table sort priority for one install state. Lower
// values sort first and keep installed rows ahead of repo-only rows.
// -----------------------------------------------------------------------------
int
dnf_backend_get_install_state_sort_rank(PackageInstallState state)
{
switch (state) {
case PackageInstallState::INSTALLED:
return 0;
case PackageInstallState::INSTALLED_NEWER_THAN_REPO:
return 1;
case PackageInstallState::LOCAL_ONLY:
return 2;
case PackageInstallState::UPGRADEABLE:
return 3;
case PackageInstallState::DOWNGRADEABLE:
return 4;
case PackageInstallState::AVAILABLE:
default:
return 5;
}
}
// -----------------------------------------------------------------------------
// Check one package name against the current installed snapshot.
// The caller refreshes that snapshot before this check when fresh rpmdb state matters.
// -----------------------------------------------------------------------------
bool
dnf_backend_is_self_protected_package_name(const std::string &name)
{
if (name.empty()) {
return false;
}
std::set<std::string> protected_names;
{
std::lock_guard<std::mutex> lock(g_installed_mutex);
protected_names = g_installed_state.self_protected_package_names;
}
return protected_names.count(name) > 0;
}
// -----------------------------------------------------------------------------
// Resolve one queued transaction spec back to the installed rpmdb so request
// validation can reject self-modification even if the UI state is outdated or bypassed.
// -----------------------------------------------------------------------------
bool
dnf_backend_is_self_protected_transaction_spec(const std::string &spec)
{
std::set<std::string> protected_names = self_protected_package_name_snapshot();
if (protected_names.empty()) {
return false;
}
if (package_spec_matches_protected_name(spec, protected_names)) {
return true;
}
auto read = BaseManager::instance().acquire_system_only_read();
libdnf5::Base &base = *read.base;
libdnf5::rpm::PackageQuery query(base);
query.filter_installed();
query.filter_nevra(spec);
for (const auto &pkg : query) {
if (protected_names.count(pkg.get_name()) > 0) {
return true;
}
}
libdnf5::rpm::PackageQuery name_query(base);
name_query.filter_installed();
name_query.filter_name(spec, libdnf5::sack::QueryCmp::EQ);
for (const auto &pkg : name_query) {
if (protected_names.count(pkg.get_name()) > 0) {
return true;
}
}
return false;
}
#ifdef DNFUI_BUILD_TESTS
// -----------------------------------------------------------------------------
// Clear the installed-package snapshot for tests that seed exact NEVRA state without querying the host rpmdb.
// -----------------------------------------------------------------------------
void
dnf_backend_testonly_clear_installed_snapshot()
{
std::lock_guard<std::mutex> lock(g_installed_mutex);
g_installed_state = {};
}
// -----------------------------------------------------------------------------
// Replace the installed-package snapshot for tests that need deterministic
// install-state classification without depending on host package state.
// -----------------------------------------------------------------------------
void
dnf_backend_testonly_replace_installed_snapshot(const std::set<std::string> &nevras)
{
std::lock_guard<std::mutex> lock(g_installed_mutex);
g_installed_state.nevras = nevras;
g_installed_state.rows_by_name_arch.clear();
g_installed_state.self_protected_package_names.clear();
}
// -----------------------------------------------------------------------------
// Replace the installed-package snapshot with full rows for name and architecture lookup tests.
// -----------------------------------------------------------------------------
void
dnf_backend_testonly_replace_installed_snapshot_rows(const std::vector<PackageRow> &rows)
{
std::lock_guard<std::mutex> lock(g_installed_mutex);
g_installed_state = {};
for (const auto &row : rows) {
g_installed_state.nevras.insert(row.nevra);
g_installed_state.rows_by_name_arch[row.name_arch_key()] = row;
}
}
// -----------------------------------------------------------------------------
// Publish an installed-package snapshot through the normal publication path for tests.
// -----------------------------------------------------------------------------
bool
dnf_backend_testonly_publish_installed_snapshot_rows(const std::vector<PackageRow> &rows,
const std::set<std::string> &protected_names)
{
InstalledQueryResult installed;
for (const auto &row : rows) {
installed.nevras.insert(row.nevra);
installed.rows_by_name_arch[row.name_arch_key()] = row;
}
return publish_installed_snapshot(std::move(installed), protected_names);
}
#endif
// -----------------------------------------------------------------------------
// EOF
// -----------------------------------------------------------------------------