• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 22045760807

16 Feb 2026 12:10AM UTC coverage: 72.384% (-0.3%) from 72.694%
22045760807

push

github

web-flow
boot: fix buffer alignment when doing block I/O (#40465)

UEFI Block I/O Protocol has `Media->IoAlign` field dictating the minimum
alignment for I/O buffer. It's quite surprising this has been lingering
here unnoticed for years, seems like most UEFI implementations have
small or no alignment requirements. U-Boot is not the case here, and
requires at least 512 byte alignment, hence attempt to read GPT
partition table fail and in effect systemd-boot can not find XBOOTLDR
partition.

These patches allow to boot from XBOOTLDR partition on U-Boot - tested
with latest systemd revision and U-Boot master
(`8de6e8f8a`) on x64 and ARM32, of which
both are failing without the patch.

Also fixes Bitlocker probing logic, which is the only other place where
raw block I/O is used, however this is untested.

311273 of 430029 relevant lines covered (72.38%)

1216197.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.34
/src/core/transaction.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include "sd-bus.h"
4
#include "sd-messages.h"
5

6
#include "alloc-util.h"
7
#include "ansi-color.h"
8
#include "bus-common-errors.h"
9
#include "bus-error.h"
10
#include "dbus-unit.h"
11
#include "hash-funcs.h"
12
#include "manager.h"
13
#include "set.h"
14
#include "slice.h"
15
#include "string-util.h"
16
#include "strv.h"
17
#include "transaction.h"
18

19
#define CYCLIC_TRANSACTIONS_MAX 4096U
20

21
static bool job_matters_to_anchor(Job *job);
22
static void transaction_unlink_job(Transaction *tr, Job *j, bool delete_dependencies);
23

24
static void transaction_delete_job(Transaction *tr, Job *j, bool delete_dependencies) {
74,861✔
25
        assert(tr);
74,861✔
26
        assert(j);
74,861✔
27

28
        /* Deletes one job from the transaction. */
29

30
        transaction_unlink_job(tr, j, delete_dependencies);
74,861✔
31

32
        job_free(j);
74,861✔
33
}
74,861✔
34

35
static void transaction_delete_unit(Transaction *tr, Unit *u) {
2✔
36
        Job *j;
2✔
37

38
        /* Deletes all jobs associated with a certain unit from the transaction. */
39

40
        while ((j = hashmap_get(tr->jobs, u)))
4✔
41
                transaction_delete_job(tr, j, true);
2✔
42
}
2✔
43

44
static void transaction_abort(Transaction *tr) {
29,154✔
45
        Job *j;
29,154✔
46

47
        assert(tr);
29,154✔
48

49
        while ((j = hashmap_first(tr->jobs)))
58,383✔
50
                transaction_delete_job(tr, j, false);
29,229✔
51

52
        assert(hashmap_isempty(tr->jobs));
29,154✔
53
}
29,154✔
54

55
static void transaction_find_jobs_that_matter_to_anchor(Job *j, unsigned generation) {
10,374✔
56
        assert(j);
10,374✔
57

58
        /* A recursive sweep through the graph that marks all units that matter to the anchor job, i.e. are
59
         * directly or indirectly a dependency of the anchor job via paths that are fully marked as
60
         * mattering. */
61

62
        j->matters_to_anchor = true;
10,374✔
63
        j->generation = generation;
10,374✔
64

65
        LIST_FOREACH(subject, l, j->subject_list) {
57,417✔
66

67
                /* This link does not matter. */
68
                if (!l->matters)
47,043✔
69
                        continue;
35,813✔
70

71
                /* This unit has already been marked. */
72
                if (l->object->generation == generation)
11,230✔
73
                        continue;
2,684✔
74

75
                transaction_find_jobs_that_matter_to_anchor(l->object, generation);
8,546✔
76
        }
77
}
10,374✔
78

79
static void transaction_merge_and_delete_job(Transaction *tr, Job *j, Job *other, JobType t) {
×
80
        JobDependency *last;
×
81

82
        assert(j);
×
83
        assert(other);
×
84
        assert(j->unit == other->unit);
×
85
        assert(!j->installed);
×
86

87
        /* Merges 'other' into 'j' and then deletes 'other'. */
88

89
        j->type = t;
×
90
        j->state = JOB_WAITING;
×
91
        j->irreversible = j->irreversible || other->irreversible;
×
92
        j->matters_to_anchor = j->matters_to_anchor || other->matters_to_anchor;
×
93

94
        /* Patch us in as new owner of the JobDependency objects. */
95
        last = NULL;
×
96
        LIST_FOREACH(subject, l, other->subject_list) {
×
97
                assert(l->subject == other);
×
98
                l->subject = j;
×
99
                last = l;
×
100
        }
101

102
        /* Merge both lists. */
103
        if (last) {
×
104
                last->subject_next = j->subject_list;
×
105
                if (j->subject_list)
×
106
                        j->subject_list->subject_prev = last;
×
107
                j->subject_list = other->subject_list;
×
108
        }
109

110
        /* Patch us in as new owner of the JobDependency objects. */
111
        last = NULL;
×
112
        LIST_FOREACH(object, l, other->object_list) {
×
113
                assert(l->object == other);
×
114
                l->object = j;
×
115
                last = l;
×
116
        }
117

118
        /* Merge both lists. */
119
        if (last) {
×
120
                last->object_next = j->object_list;
×
121
                if (j->object_list)
×
122
                        j->object_list->object_prev = last;
×
123
                j->object_list = other->object_list;
×
124
        }
125

126
        /* Kill the other job. */
127
        other->subject_list = NULL;
×
128
        other->object_list = NULL;
×
129
        transaction_delete_job(tr, other, true);
×
130
}
×
131

132
static bool job_is_conflicted_by(Job *j) {
×
133
        assert(j);
×
134

135
        /* Returns true if this job is pulled in by a least one ConflictedBy= dependency. */
136

137
        LIST_FOREACH(object, l, j->object_list)
×
138
                if (l->conflicts)
×
139
                        return true;
140

141
        return false;
142
}
143

144
static int delete_one_unmergeable_job(Transaction *tr, Job *job) {
11✔
145
        assert(job);
11✔
146

147
        /* Tries to delete one item in the linked list
148
         * j->transaction_next->transaction_next->... that conflicts with another one, in an attempt to make
149
         * an inconsistent transaction work. */
150

151
        /* We rely here on the fact that if a merged with b does not merge with c, either a or b merge with c
152
         * neither. */
153
        LIST_FOREACH(transaction, j, job)
11✔
154
                LIST_FOREACH(transaction, k, j->transaction_next) {
11✔
155
                        Job *d;
11✔
156

157
                        /* Is this one mergeable? Then skip it. */
158
                        if (job_type_is_mergeable(j->type, k->type))
11✔
159
                                continue;
×
160

161
                        /* Ok, we found two that conflict, let's see if we can drop one of them. */
162
                        if (!j->matters_to_anchor && !k->matters_to_anchor) {
11✔
163

164
                                /* Both jobs don't matter, so let's find the one that is smarter to remove.
165
                                 * Let's think positive and rather remove stops than starts -- except if
166
                                 * something is being stopped because it is conflicted by another unit in
167
                                 * which case we rather remove the start. */
168

169
                                bool j_is_conflicted_by = job_is_conflicted_by(j),
×
170
                                        k_is_conflicted_by = job_is_conflicted_by(k);
×
171

172
                                /* Update test/units/TEST-87-AUX-UTILS-VM.sh when logs below are changed. */
173
                                log_unit_debug(j->unit,
×
174
                                               "Looking at job %s/%s conflicted_by=%s",
175
                                               j->unit->id, job_type_to_string(j->type),
176
                                               yes_no(j->type == JOB_STOP && j_is_conflicted_by));
177
                                log_unit_debug(k->unit,
×
178
                                               "Looking at job %s/%s conflicted_by=%s",
179
                                               k->unit->id, job_type_to_string(k->type),
180
                                               yes_no(k->type == JOB_STOP && k_is_conflicted_by));
181

182
                                if (j->type == JOB_STOP && j_is_conflicted_by)
×
183
                                        d = k;
184
                                else if (k->type == JOB_STOP && k_is_conflicted_by)
×
185
                                        d = j;
186
                                else if (j->type == JOB_STOP)
×
187
                                        d = j;
188
                                else if (k->type == JOB_STOP)
×
189
                                        d = k;
×
190
                                else
191
                                        d = j;
192

193
                        } else if (!j->matters_to_anchor)
11✔
194
                                d = j;
195
                        else if (!k->matters_to_anchor)
×
196
                                d = k;
197
                        else
198
                                return -ENOEXEC;
199

200
                        /* Ok, we can drop one, so let's do so. */
201
                        log_unit_debug(d->unit,
11✔
202
                                       "Fixing conflicting jobs %s/%s,%s/%s by deleting job %s/%s",
203
                                       j->unit->id, job_type_to_string(j->type),
204
                                       k->unit->id, job_type_to_string(k->type),
205
                                       d->unit->id, job_type_to_string(d->type));
206
                        transaction_delete_job(tr, d, true);
11✔
207
                        return 0;
11✔
208
                }
209

210
        return -EINVAL;
211
}
212

213
static int transaction_ensure_mergeable(Transaction *tr, bool matters_to_anchor, sd_bus_error *e) {
3,651✔
214
        Job *j;
3,651✔
215
        int r;
3,651✔
216

217
        assert(tr);
3,651✔
218

219
        HASHMAP_FOREACH(j, tr->jobs) {
43,123✔
220
                JobType t;
39,483✔
221

222
                if (job_matters_to_anchor(j) != matters_to_anchor)
39,483✔
223
                        continue;
19,642✔
224

225
                t = j->type;
19,841✔
226
                LIST_FOREACH(transaction, k, j->transaction_next) {
19,841✔
227
                        if (job_type_merge_and_collapse(&t, k->type, j->unit) >= 0)
11✔
228
                                continue;
×
229

230
                        /* OK, we could not merge all jobs for this action. Let's see if we can get rid of
231
                         * one of them. */
232

233
                        r = delete_one_unmergeable_job(tr, j);
11✔
234
                        if (r >= 0)
11✔
235
                                /* Ok, we managed to drop one, now let's ask our callers to call us again
236
                                 * after garbage collecting. */
237
                                return -EAGAIN;
11✔
238

239
                        /* We couldn't merge anything. Failure. */
240
                        return sd_bus_error_setf(e, BUS_ERROR_TRANSACTION_JOBS_CONFLICTING,
×
241
                                                 "Transaction contains conflicting jobs '%s' and '%s' for %s. "
242
                                                 "Probably contradicting requirement dependencies configured.",
243
                                                 job_type_to_string(t),
244
                                                 job_type_to_string(k->type),
245
                                                 k->unit->id);
×
246
                }
247
        }
248

249
        return 0;
3,640✔
250
}
251

252
static int transaction_merge_jobs(Transaction *tr, sd_bus_error *e) {
1,831✔
253
        Job *j;
1,831✔
254
        int r;
1,831✔
255

256
        assert(tr);
1,831✔
257

258
        /* First step, try to drop unmergeable jobs for jobs that matter to anchor. */
259
        r = transaction_ensure_mergeable(tr, /* matters_to_anchor= */ true, e);
1,831✔
260
        if (r < 0)
1,831✔
261
                return r;
1,831✔
262

263
        /* Second step, do the same for jobs that not matter to anchor. */
264
        r = transaction_ensure_mergeable(tr, /* matters_to_anchor= */ false, e);
1,820✔
265
        if (r < 0)
1,820✔
266
                return r;
267

268
        /* Third step, merge the jobs. */
269
        HASHMAP_FOREACH(j, tr->jobs) {
21,395✔
270
                JobType t = j->type;
19,575✔
271

272
                /* Merge all transaction jobs for j->unit. */
273
                LIST_FOREACH(transaction, k, j->transaction_next)
19,575✔
274
                        assert_se(job_type_merge_and_collapse(&t, k->type, j->unit) == 0);
×
275

276
                Job *k;
277
                while ((k = j->transaction_next)) {
19,575✔
278
                        if (tr->anchor_job == k) {
×
279
                                transaction_merge_and_delete_job(tr, k, j, t);
×
280
                                j = k;
×
281
                        } else
282
                                transaction_merge_and_delete_job(tr, j, k, t);
×
283
                }
284

285
                assert(!j->transaction_next);
19,575✔
286
                assert(!j->transaction_prev);
19,575✔
287
        }
288

289
        return 0;
1,820✔
290
}
291

292
static void transaction_drop_redundant(Transaction *tr) {
3,642✔
293
        bool again;
3,642✔
294

295
        /* Goes through the transaction and removes all jobs of the units whose jobs are all noops. If not
296
         * all of a unit's jobs are redundant, they are kept. */
297

298
        assert(tr);
3,642✔
299

300
        do {
34,554✔
301
                Job *j;
34,554✔
302

303
                again = false;
34,554✔
304

305
                HASHMAP_FOREACH(j, tr->jobs) {
636,390✔
306
                        bool keep = false;
632,748✔
307

308
                        LIST_FOREACH(transaction, k, j)
664,599✔
309
                                if (tr->anchor_job == k ||
1,248,228✔
310
                                    !job_type_is_redundant(k->type, unit_active_state(k->unit)) ||
614,541✔
311
                                    (k->unit->job && job_type_is_conflicting(k->type, k->unit->job->type))) {
31,878✔
312
                                        keep = true;
313
                                        break;
314
                                }
315

316
                        if (!keep) {
632,748✔
317
                                log_trace("Found redundant job %s/%s, dropping from transaction.",
30,912✔
318
                                          j->unit->id, job_type_to_string(j->type));
319
                                transaction_delete_job(tr, j, false);
30,912✔
320
                                again = true;
321
                                break;
322
                        }
323
                }
324
        } while (again);
34,554✔
325
}
3,642✔
326

327
static bool job_matters_to_anchor(Job *job) {
39,491✔
328
        assert(job);
39,491✔
329
        assert(!job->transaction_prev);
39,491✔
330

331
        /* Checks whether at least one of the jobs for this transaction matters to the anchor. */
332

333
        LIST_FOREACH(transaction, j, job)
72,043✔
334
                if (j->matters_to_anchor)
39,506✔
335
                        return true;
336

337
        return false;
338
}
339

340
static int transaction_verify_order_one(Transaction *tr, Job *j, Job *from, unsigned generation, sd_bus_error *e) {
65,214✔
341

342
        static const UnitDependencyAtom directions[] = {
65,214✔
343
                UNIT_ATOM_BEFORE,
344
                UNIT_ATOM_AFTER,
345
        };
346

347
        int r;
65,214✔
348

349
        assert(tr);
65,214✔
350
        assert(j);
65,214✔
351
        assert(!j->transaction_prev);
65,214✔
352

353
        /* Does a recursive sweep through the ordering graph, looking for a cycle. If we find a cycle we try
354
         * to break it. */
355

356
        /* Have we seen this before? */
357
        if (j->generation == generation) {
65,214✔
358
                _cleanup_free_ char **array = NULL;
43,302✔
359
                Job *delete = NULL;
43,302✔
360

361
                /* If the marker is NULL we have been here already and decided the job was loop-free from
362
                 * here. Hence shortcut things and return right-away. */
363
                if (!j->marker)
43,302✔
364
                        return 0;
365

366
                /* So, the marker is not NULL and we already have been here. We have a cycle. Let's try to
367
                 * break it. We go backwards in our path and try to find a suitable job to remove. We use the
368
                 * marker to find our way back, since smart how we are we stored our way back in there. */
369
                for (Job *k = from; k; k = (k->generation == generation && k->marker != k) ? k->marker : NULL) {
11✔
370

371
                        /* For logging below. */
372
                        if (strv_push_pair(&array, k->unit->id, (char*) job_type_to_string(k->type)) < 0)
11✔
373
                                (void) log_oom_warning();
×
374

375
                        if (!delete && hashmap_contains(tr->jobs, k->unit) && !job_matters_to_anchor(k))
11✔
376
                                /* Ok, we can drop this one, so let's do so. */
377
                                delete = k;
2✔
378

379
                        /* Check if this in fact was the beginning of the cycle. */
380
                        if (k == j)
11✔
381
                                break;
382
                }
383

384
                _cleanup_free_ char *unit_ids = NULL;
×
385
                STRV_FOREACH_PAIR(unit_id, job_type, array)
15✔
386
                        (void) strextendf_with_separator(&unit_ids, "\n", "%s%s", unit_log_field(j->unit), *unit_id);
11✔
387

388
                _cleanup_free_ char *cycle_path_text = strdup("Found ordering cycle");
4✔
389
                if (!strv_isempty(array)) {
4✔
390
                        (void) strextendf(&cycle_path_text, ": %s/%s", array[0], array[1]);
4✔
391

392
                        STRV_FOREACH_PAIR(unit_id, job_type, strv_skip(array, 2))
11✔
393
                                (void) strextendf(&cycle_path_text, " after %s/%s", *unit_id, *job_type);
7✔
394

395
                        (void) strextendf(&cycle_path_text, " - after %s", array[0]);
4✔
396
                }
397

398
                /* logging for j not k here to provide a consistent narrative */
399
                if (cycle_path_text)
4✔
400
                        log_struct(LOG_ERR,
4✔
401
                                   LOG_UNIT_MESSAGE(j->unit, "%s", cycle_path_text),
402
                                   LOG_MESSAGE_ID(SD_MESSAGE_UNIT_ORDERING_CYCLE_STR),
403
                                   LOG_ITEM("%s", strempty(unit_ids)));
404

405
                if (set_size(j->manager->transactions_with_cycle) >= CYCLIC_TRANSACTIONS_MAX)
4✔
406
                        log_warning("Too many transactions with ordering cycle, suppressing record.");
×
407
                else {
408
                        uint64_t *id_buf = newdup(uint64_t, &tr->id, 1);
4✔
409
                        if (!id_buf)
4✔
410
                                log_oom_warning();
×
411
                        else
412
                                (void) set_ensure_consume(&j->manager->transactions_with_cycle, &uint64_hash_ops_value_free, id_buf);
4✔
413
                }
414

415
                if (delete) {
4✔
416
                        const char *status;
2✔
417
                        /* logging for j not k here to provide a consistent narrative */
418
                        log_struct(LOG_WARNING,
2✔
419
                                   LOG_UNIT_MESSAGE(j->unit,
420
                                                    "Job %s/%s deleted to break ordering cycle starting with %s/%s",
421
                                                    delete->unit->id, job_type_to_string(delete->type),
422
                                                    j->unit->id, job_type_to_string(j->type)),
423
                                   LOG_MESSAGE_ID(SD_MESSAGE_DELETING_JOB_BECAUSE_ORDERING_CYCLE_STR),
424
                                   LOG_ITEM("DELETED_UNIT=%s", delete->unit->id),
425
                                   LOG_ITEM("DELETED_TYPE=%s", job_type_to_string(delete->type)),
426
                                   LOG_ITEM("%s", strempty(unit_ids)));
427

428
                        if (log_get_show_color())
2✔
429
                                status = ANSI_HIGHLIGHT_RED " SKIP " ANSI_NORMAL;
430
                        else
431
                                status = " SKIP ";
2✔
432

433
                        unit_status_printf(delete->unit,
2✔
434
                                           STATUS_TYPE_NOTICE,
435
                                           status,
436
                                           "Ordering cycle found, skipping %s",
437
                                           unit_status_string(delete->unit, NULL));
438
                        transaction_delete_unit(tr, delete->unit);
2✔
439
                        return -EAGAIN;
440
                }
441

442
                log_struct(LOG_ERR,
2✔
443
                           LOG_UNIT_MESSAGE(j->unit, "Unable to break cycle starting with %s/%s",
444
                                            j->unit->id, job_type_to_string(j->type)),
445
                           LOG_MESSAGE_ID(SD_MESSAGE_CANT_BREAK_ORDERING_CYCLE_STR),
446
                           LOG_ITEM("%s", strempty(unit_ids)));
447

448
                return sd_bus_error_setf(e, BUS_ERROR_TRANSACTION_ORDER_IS_CYCLIC,
2✔
449
                                         "Transaction order is cyclic. See system logs for details.");
450
        }
451

452
        /* Make the marker point to where we come from, so that we can find our way backwards if we want to
453
         * break a cycle. We use a special marker for the beginning: we point to ourselves. */
454
        j->marker = from ?: j;
21,912✔
455
        j->generation = generation;
21,912✔
456

457
        /* Actual ordering of jobs depends on the unit ordering dependency and job types. We need to traverse
458
         * the graph over 'before' edges in the actual job execution order. We traverse over both unit
459
         * ordering dependencies and we test with job_compare() whether it is the 'before' edge in the job
460
         * execution ordering. */
461
        FOREACH_ELEMENT(d, directions) {
65,714✔
462
                Unit *u;
43,813✔
463

464
                UNIT_FOREACH_DEPENDENCY(u, j->unit, *d) {
305,599✔
465
                        Job *o;
176,909✔
466

467
                        /* Is there a job for this unit? */
468
                        o = hashmap_get(tr->jobs, u);
176,909✔
469
                        if (!o) {
176,909✔
470
                                /* Ok, there is no job for this in the transaction, but maybe there is
471
                                 * already one running? */
472
                                o = u->job;
96,112✔
473
                                if (!o)
96,112✔
474
                                        continue;
83,611✔
475
                        }
476

477
                        /* Cut traversing if the job j is not really *before* o. */
478
                        if (job_compare(j, o, *d) >= 0)
93,298✔
479
                                continue;
47,663✔
480

481
                        r = transaction_verify_order_one(tr, o, j, generation, e);
45,635✔
482
                        if (r < 0)
45,635✔
483
                                return r;
11✔
484
                }
485
        }
486

487
        /* Ok, let's backtrack, and remember that this entry is not on our path anymore. */
488
        j->marker = NULL;
21,901✔
489

490
        return 0;
21,901✔
491
}
492

493
static int transaction_verify_order(Transaction *tr, unsigned *generation, sd_bus_error *e) {
1,824✔
494
        Job *j;
1,824✔
495
        int r;
1,824✔
496
        unsigned g;
1,824✔
497

498
        assert(tr);
1,824✔
499
        assert(generation);
1,824✔
500

501
        /* Check if the ordering graph is cyclic. If it is, try to fix that up by dropping one of the jobs. */
502

503
        g = (*generation)++;
1,824✔
504

505
        HASHMAP_FOREACH(j, tr->jobs) {
21,399✔
506
                r = transaction_verify_order_one(tr, j, NULL, g, e);
19,579✔
507
                if (r < 0)
19,579✔
508
                        return r;
4✔
509
        }
510

511
        return 0;
1,820✔
512
}
513

514
static void transaction_collect_garbage(Transaction *tr) {
1,613✔
515
        bool again;
1,613✔
516

517
        assert(tr);
1,613✔
518

519
        /* Drop jobs that are not required by any other job. */
520

521
        do {
16,300✔
522
                Job *j;
16,300✔
523

524
                again = false;
16,300✔
525

526
                HASHMAP_FOREACH(j, tr->jobs) {
77,098✔
527
                        if (tr->anchor_job == j)
59,185✔
528
                                continue;
9,231✔
529

530
                        if (!j->object_list) {
49,954✔
531
                                log_trace("Garbage collecting job %s/%s", j->unit->id, job_type_to_string(j->type));
14,687✔
532
                                transaction_delete_job(tr, j, true);
14,687✔
533
                                again = true;
534
                                break;
535
                        }
536

537
                        log_trace("Keeping job %s/%s because of %s/%s",
60,798✔
538
                                  j->unit->id, job_type_to_string(j->type),
539
                                  j->object_list->subject ? j->object_list->subject->unit->id : "root",
540
                                  j->object_list->subject ? job_type_to_string(j->object_list->subject->type) : "root");
541
                }
542

543
        } while (again);
16,300✔
544
}
1,613✔
545

546
static int transaction_is_destructive(Transaction *tr, JobMode mode, sd_bus_error *e) {
1,820✔
547
        Job *j;
1,820✔
548

549
        assert(tr);
1,820✔
550

551
        /* Checks whether applying this transaction means that existing jobs would be replaced. */
552

553
        HASHMAP_FOREACH(j, tr->jobs) {
21,372✔
554

555
                /* Assume merged */
556
                assert(!j->transaction_prev);
19,556✔
557
                assert(!j->transaction_next);
19,556✔
558

559
                if (j->unit->job && (IN_SET(mode, JOB_FAIL, JOB_LENIENT) || j->unit->job->irreversible) &&
19,600✔
560
                    job_type_is_conflicting(j->unit->job->type, j->type))
44✔
561
                        return sd_bus_error_setf(e, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE,
8✔
562
                                                 "Transaction for %s/%s is destructive (%s has '%s' job queued, but '%s' is included in transaction).",
563
                                                 tr->anchor_job->unit->id, job_type_to_string(tr->anchor_job->type),
4✔
564
                                                 j->unit->id, job_type_to_string(j->unit->job->type), job_type_to_string(j->type));
565
        }
566

567
        return 0;
1,816✔
568
}
569

570
static int transaction_minimize_impact(Transaction *tr, JobMode mode, sd_bus_error *e) {
1,828✔
571
        Job *head;
1,828✔
572

573
        assert(tr);
1,828✔
574

575
        /* Drops all unnecessary jobs that reverse already active jobs or that stop a running service. */
576

577
        if (!IN_SET(mode, JOB_FAIL, JOB_LENIENT))
1,828✔
578
                return 0;
1,828✔
579

580
rescan:
505✔
581
        HASHMAP_FOREACH(head, tr->jobs) {
30,824✔
582
                LIST_FOREACH(transaction, j, head) {
60,649✔
583
                        bool stops_running_service, changes_existing_job;
30,330✔
584

585
                        /* If it matters, we shouldn't drop it. */
586
                        if (j->matters_to_anchor && mode != JOB_LENIENT)
30,330✔
587
                                continue;
4,250✔
588

589
                        /* Would this stop a running service? Would this change an existing job? If so, let's
590
                         * drop this entry. */
591

592
                        stops_running_service =
52,160✔
593
                                j->type == JOB_STOP && UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(j->unit));
26,080✔
594

595
                        changes_existing_job =
52,160✔
596
                                j->unit->job &&
26,103✔
597
                                job_type_is_conflicting(j->type, j->unit->job->type);
23✔
598

599
                        if (!stops_running_service && !changes_existing_job)
26,080✔
600
                                continue;
26,069✔
601

602
                        if (j->matters_to_anchor) {
11✔
603
                                assert(mode == JOB_LENIENT);
6✔
604
                                return sd_bus_error_setf(e, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE,
6✔
605
                                                         "%s/%s would stop a running unit or change existing job, bailing",
606
                                                         j->unit->id, job_type_to_string(j->type));
607
                        }
608

609
                        if (stops_running_service)
5✔
610
                                log_unit_debug(j->unit,
×
611
                                               "%s/%s would stop a running service.",
612
                                               j->unit->id, job_type_to_string(j->type));
613

614
                        if (changes_existing_job)
5✔
615
                                log_unit_debug(j->unit,
5✔
616
                                               "%s/%s would change existing job.",
617
                                               j->unit->id, job_type_to_string(j->type));
618

619
                        /* Ok, let's get rid of this. */
620
                        log_unit_debug(j->unit,
5✔
621
                                       "Deleting %s/%s to minimize impact.",
622
                                       j->unit->id, job_type_to_string(j->type));
623

624
                        transaction_delete_job(tr, j, true);
5✔
625
                        goto rescan;
5✔
626
                }
627
        }
628

629
        return 0;
494✔
630
}
631

632
static int transaction_apply(
1,816✔
633
                Transaction *tr,
634
                Manager *m,
635
                JobMode mode,
636
                Set *affected_jobs) {
637

638
        Job *j;
1,816✔
639
        int r;
1,816✔
640

641
        assert(tr);
1,816✔
642
        assert(m);
1,816✔
643

644
        /* Moves the transaction jobs to the set of active jobs. */
645

646
        if (IN_SET(mode, JOB_ISOLATE, JOB_FLUSH)) {
1,816✔
647

648
                /* When isolating first kill all installed jobs which aren't part of the new transaction. */
649
                HASHMAP_FOREACH(j, m->jobs) {
228✔
650
                        assert(j->installed);
17✔
651

652
                        if (j->unit->ignore_on_isolate)
17✔
653
                                continue;
×
654

655
                        if (hashmap_contains(tr->jobs, j->unit))
17✔
656
                                continue;
17✔
657

658
                        /* Not invalidating recursively. Avoids triggering OnFailure= actions of dependent
659
                         * jobs. Also avoids invalidating our iterator. */
660
                        job_finish_and_invalidate(j, JOB_CANCELED, false, false);
×
661
                }
662
        }
663

664
        HASHMAP_FOREACH(j, tr->jobs) {
21,364✔
665
                /* Assume merged. */
666
                assert(!j->transaction_prev);
19,548✔
667
                assert(!j->transaction_next);
19,548✔
668

669
                r = hashmap_ensure_put(&m->jobs, NULL, UINT32_TO_PTR(j->id), j);
19,548✔
670
                if (r < 0)
19,548✔
671
                        goto rollback;
×
672
        }
673

674
        while ((j = hashmap_steal_first(tr->jobs))) {
23,180✔
675
                Job *installed_job;
19,548✔
676

677
                /* Clean the job dependencies. */
678
                transaction_unlink_job(tr, j, false);
19,548✔
679

680
                installed_job = job_install(j);
19,548✔
681
                if (installed_job != j) {
19,548✔
682
                        /* j has been merged into a previously installed job. */
683
                        if (tr->anchor_job == j)
71✔
684
                                tr->anchor_job = installed_job;
4✔
685

686
                        hashmap_remove_value(m->jobs, UINT32_TO_PTR(j->id), j);
71✔
687
                        free_and_replace_full(j, installed_job, job_free);
71✔
688
                }
689

690
                job_add_to_run_queue(j);
19,548✔
691
                job_add_to_dbus_queue(j);
19,548✔
692
                job_start_timer(j, false);
19,548✔
693
                job_shutdown_magic(j);
19,548✔
694

695
                /* When 'affected' is specified, let's track all in it all jobs that were touched because of
696
                 * this transaction. */
697
                if (affected_jobs)
19,548✔
698
                        (void) set_put(affected_jobs, j);
×
699
        }
700

701
        return 0;
1,816✔
702

703
rollback:
×
704

705
        HASHMAP_FOREACH(j, tr->jobs)
×
706
                hashmap_remove_value(m->jobs, UINT32_TO_PTR(j->id), j);
×
707

708
        return r;
×
709
}
710

711
int transaction_activate(
1,828✔
712
                Transaction *tr,
713
                Manager *m,
714
                JobMode mode,
715
                Set *affected_jobs,
716
                sd_bus_error *e) {
717

718
        Job *j;
1,828✔
719
        int r;
1,828✔
720
        unsigned generation = 1;
1,828✔
721

722
        /* This applies the changes recorded in tr->jobs to the actual list of jobs, if possible. */
723

724
        assert(tr);
1,828✔
725
        assert(m);
1,828✔
726

727
        /* Reset the generation counter of all installed jobs. The detection of cycles looks at installed
728
         * jobs. If they had a non-zero generation from some previous walk of the graph, the algorithm would
729
         * break. */
730
        HASHMAP_FOREACH(j, m->jobs)
8,611✔
731
                j->generation = 0;
6,783✔
732

733
        /* First step: figure out which jobs matter. */
734
        transaction_find_jobs_that_matter_to_anchor(tr->anchor_job, generation++);
1,828✔
735

736
        /* Second step: Try not to stop any running services if we don't have to. Don't try to reverse
737
         * running jobs if we don't have to. */
738
        r = transaction_minimize_impact(tr, mode, e);
1,828✔
739
        if (r < 0)
1,828✔
740
                return r; /* Note that we don't log here, because for JOB_LENIENT conflicts are very much
1,828✔
741
                           * expected and shouldn't appear to be fatal for the unit. Only inform the caller
742
                           * via bus error. */
743

744
        /* Third step: Drop redundant jobs. */
745
        transaction_drop_redundant(tr);
1,822✔
746

747
        for (;;) {
1,824✔
748
                /* Fourth step: Let's remove unneeded jobs that might be lurking. */
749
                if (mode != JOB_ISOLATE)
1,824✔
750
                        transaction_collect_garbage(tr);
1,613✔
751

752
                /* Fifth step: verify order makes sense and correct cycles if necessary and possible. */
753
                r = transaction_verify_order(tr, &generation, e);
1,824✔
754
                if (r >= 0)
1,824✔
755
                        break;
756
                if (r != -EAGAIN)
4✔
757
                        return log_warning_errno(r, "Requested transaction contains an unfixable cyclic ordering dependency: %s",
2✔
758
                                                 bus_error_message(e, r));
759

760
                /* Let's see if the resulting transaction ordering graph is still cyclic... */
761
        }
762

763
        for (;;) {
1,831✔
764
                /* Sixth step: let's drop unmergeable entries if necessary and possible, merge entries we can
765
                 * merge. */
766
                r = transaction_merge_jobs(tr, e);
1,831✔
767
                if (r >= 0)
1,831✔
768
                        break;
769
                if (r != -EAGAIN)
11✔
770
                        return log_warning_errno(r, "Requested transaction contains unmergeable jobs: %s",
×
771
                                                 bus_error_message(e, r));
772

773
                /* Seventh step: an entry got dropped, let's garbage collect its dependencies. */
774
                if (mode != JOB_ISOLATE)
11✔
775
                        transaction_collect_garbage(tr);
×
776

777
                /* Let's see if the resulting transaction still has unmergeable entries... */
778
        }
779

780
        /* Eights step: Drop redundant jobs again, if the merging now allows us to drop more. */
781
        transaction_drop_redundant(tr);
1,820✔
782

783
        /* Ninth step: check whether we can actually apply this. */
784
        r = transaction_is_destructive(tr, mode, e);
1,820✔
785
        if (r < 0)
1,820✔
786
                return log_notice_errno(r, "Requested transaction contradicts existing jobs: %s",
4✔
787
                                        bus_error_message(e, r));
788

789
        /* Tenth step: apply changes. */
790
        r = transaction_apply(tr, m, mode, affected_jobs);
1,816✔
791
        if (r < 0)
1,816✔
792
                return log_warning_errno(r, "Failed to apply transaction: %m");
×
793

794
        assert(hashmap_isempty(tr->jobs));
1,816✔
795

796
        /* Are there any jobs now? Then make sure we have the idle pipe around. We don't really care too much
797
         * whether this works or not, as the idle pipe is a feature for cosmetics, not actually useful for
798
         * anything beyond that. */
799
        if (!hashmap_isempty(m->jobs))
1,816✔
800
                (void) manager_allocate_idle_pipe(m);
1,816✔
801

802
        return 0;
803
}
804

805
static Job* transaction_add_one_job(Transaction *tr, JobType type, Unit *unit, bool *is_new) {
186,342✔
806
        Job *j, *f;
186,342✔
807

808
        assert(tr);
186,342✔
809
        assert(unit);
186,342✔
810

811
        /* Looks for an existing prospective job and returns that. If it doesn't exist it is created and
812
         * added to the prospective jobs list. */
813

814
        f = hashmap_get(tr->jobs, unit);
186,342✔
815

816
        LIST_FOREACH(transaction, i, f) {
186,385✔
817
                assert(i->unit == unit);
91,976✔
818

819
                if (i->type == type) {
91,976✔
820
                        if (is_new)
91,933✔
821
                                *is_new = false;
91,933✔
822
                        return i;
91,933✔
823
                }
824
        }
825

826
        j = job_new(unit, type);
94,409✔
827
        if (!j)
94,409✔
828
                return NULL;
829

830
        j->irreversible = tr->irreversible;
94,409✔
831

832
        LIST_PREPEND(transaction, f, j);
94,409✔
833

834
        if (hashmap_replace(tr->jobs, unit, f) < 0) {
94,409✔
835
                LIST_REMOVE(transaction, f, j);
×
836
                job_free(j);
×
837
                return NULL;
×
838
        }
839

840
        if (is_new)
94,409✔
841
                *is_new = true;
94,409✔
842

843
        log_trace("Added job %s/%s to transaction.", unit->id, job_type_to_string(type));
844

845
        return j;
846
}
847

848
static void transaction_unlink_job(Transaction *tr, Job *j, bool delete_dependencies) {
94,409✔
849
        assert(tr);
94,409✔
850
        assert(j);
94,409✔
851

852
        if (j->transaction_prev)
94,409✔
853
                j->transaction_prev->transaction_next = j->transaction_next;
11✔
854
        else if (j->transaction_next)
94,398✔
855
                hashmap_replace(tr->jobs, j->unit, j->transaction_next);
26✔
856
        else
857
                hashmap_remove_value(tr->jobs, j->unit, j);
94,372✔
858

859
        if (j->transaction_next)
94,409✔
860
                j->transaction_next->transaction_prev = j->transaction_prev;
26✔
861

862
        j->transaction_prev = j->transaction_next = NULL;
94,409✔
863

864
        while (j->subject_list)
153,685✔
865
                job_dependency_free(j->subject_list);
59,276✔
866

867
        while (j->object_list) {
190,515✔
868
                Job *other = j->object_list->matters ? j->object_list->subject : NULL;
96,106✔
869

870
                job_dependency_free(j->object_list);
96,106✔
871

872
                if (other && delete_dependencies) {
96,106✔
873
                        log_unit_debug(other->unit,
12✔
874
                                       "Deleting job %s/%s as dependency of job %s/%s",
875
                                       other->unit->id, job_type_to_string(other->type),
876
                                       j->unit->id, job_type_to_string(j->type));
877
                        transaction_delete_job(tr, other, delete_dependencies);
12✔
878
                }
879
        }
880
}
94,409✔
881

882
void transaction_add_propagate_reload_jobs(
29,135✔
883
                Transaction *tr,
884
                Unit *unit,
885
                Job *by,
886
                TransactionAddFlags flags) {
887

888
        JobType nt;
29,135✔
889
        Unit *dep;
29,135✔
890
        int r;
29,135✔
891

892
        assert(tr);
29,135✔
893
        assert(unit);
29,135✔
894

895
        UNIT_FOREACH_DEPENDENCY_SAFE(dep, unit, UNIT_ATOM_PROPAGATES_RELOAD_TO) {
58,270✔
896
                _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
×
897

898
                nt = job_type_collapse(JOB_TRY_RELOAD, dep);
×
899
                if (nt == JOB_NOP)
×
900
                        continue;
×
901

902
                r = transaction_add_job_and_dependencies(tr, nt, dep, by, flags, &e);
×
903
                if (r < 0)
×
904
                        log_unit_warning(dep,
×
905
                                         "Cannot add dependency reload job, ignoring: %s",
906
                                         bus_error_message(&e, r));
907
        }
908
}
29,135✔
909

910
static JobType job_type_propagate_stop_graceful(Job *j) {
×
911
        JobType type;
×
912

913
        if (!j)
×
914
                return JOB_STOP;
915

916
        type = JOB_STOP;
917

918
        LIST_FOREACH(transaction, i, j)
×
919
                switch (i->type) {
×
920

921
                case JOB_STOP:
922
                case JOB_RESTART:
923
                        /* Nothing to worry about, an appropriate job is in-place. */
924
                        return JOB_NOP;
925

926
                case JOB_START:
×
927
                        /* This unit is pulled in by other dependency types in this transaction. We will run
928
                         * into job type conflict if we enqueue a stop job, so let's enqueue a restart job
929
                         * instead. */
930
                        type = JOB_RESTART;
×
931
                        break;
×
932

933
                default: /* We don't care about others. */
934
                        ;
935

936
                }
937

938
        return type;
939
}
940

941
int transaction_add_job_and_dependencies(
186,538✔
942
                Transaction *tr,
943
                JobType type,
944
                Unit *unit,
945
                Job *by,
946
                TransactionAddFlags flags,
947
                sd_bus_error *e) {
948

949
        bool is_new;
186,538✔
950
        Job *job;
186,538✔
951
        int r;
186,538✔
952

953
        assert(tr);
186,538✔
954
        assert(type >= 0);
186,538✔
955
        assert(type < _JOB_TYPE_MAX);
186,538✔
956
        assert(type < _JOB_TYPE_MAX_IN_TRANSACTION);
186,538✔
957
        assert(unit);
186,538✔
958

959
        /* Before adding jobs for this unit, let's ensure that its state has been loaded. This matters when
960
         * jobs are spawned as part of coldplugging itself (see e. g. path_coldplug()). This way, we
961
         * "recursively" coldplug units, ensuring that we do not look at state of not-yet-coldplugged units. */
962
        if (MANAGER_IS_RELOADING(unit->manager))
186,538✔
963
                unit_coldplug(unit);
×
964

965
        if (by)
186,538✔
966
                log_trace("Pulling in %s/%s from %s/%s",
186,538✔
967
                          unit->id, job_type_to_string(type),
968
                          by->unit->id, job_type_to_string(by->type));
969

970
        /* Safety check that the unit is a valid state, i.e. not in UNIT_STUB or UNIT_MERGED which should
971
         * only be set temporarily. */
972
        if (!UNIT_IS_LOAD_COMPLETE(unit->load_state))
186,538✔
973
                return sd_bus_error_setf(e, BUS_ERROR_LOAD_FAILED, "Unit %s is not loaded properly.", unit->id);
186,538✔
974

975
        if (type != JOB_STOP) {
186,538✔
976
                /* The time-based cache allows new units to be started without daemon-reload, but if they are
977
                 * already referenced (because of dependencies or ordering) then we have to force a load of
978
                 * the fragment. As an optimization, check first if anything in the usual paths was modified
979
                 * since the last time the cache was loaded. Also check if the last time an attempt to load
980
                 * the unit was made was before the most recent cache refresh, so that we know we need to try
981
                 * again — even if the cache is current, it might have been updated in a different context
982
                 * before we had a chance to retry loading this particular unit.
983
                 *
984
                 * Given building up the transaction is a synchronous operation, attempt to load the unit
985
                 * immediately. */
986
                if (manager_unit_cache_should_retry_load(unit)) {
121,239✔
987
                        assert(unit->load_state == UNIT_NOT_FOUND);
×
988
                        unit->load_state = UNIT_STUB;
×
989
                        unit->load_error = 0;
×
990
                        (void) unit_load(unit);
×
991
                        assert(unit->load_state != UNIT_STUB);
×
992
                }
993

994
                r = bus_unit_validate_load_state(unit, e);
121,239✔
995
                if (r < 0)
121,239✔
996
                        return r;
997
        }
998

999
        if (!unit_job_is_applicable(unit, type))
186,344✔
1000
                return sd_bus_error_setf(e, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE,
×
1001
                                         "Job type %s is not applicable for unit %s.",
1002
                                         job_type_to_string(type), unit->id);
1003

1004
        if (type == JOB_START) {
186,344✔
1005
                /* The hard concurrency limit for slice units we already enforce when a job is enqueued. */
1006
                Slice *slice = SLICE(UNIT_GET_SLICE(unit));
91,880✔
1007
                if (slice && slice_concurrency_hard_max_reached(slice, unit))
71,775✔
1008
                        return sd_bus_error_setf(
4✔
1009
                                        e, BUS_ERROR_CONCURRENCY_LIMIT_REACHED,
1010
                                        "Concurrency limit of the slice unit '%s' (or any of its parents) the unit '%s' is contained in has been reached, refusing start job.",
1011
                                        UNIT(slice)->id, unit->id);
2✔
1012
        }
1013

1014
        /* First add the job. */
1015
        job = transaction_add_one_job(tr, type, unit, &is_new);
186,342✔
1016
        if (!job)
186,342✔
1017
                return -ENOMEM;
1018

1019
        if (FLAGS_SET(flags, TRANSACTION_IGNORE_ORDER))
186,342✔
1020
                job->ignore_order = true;
29,129✔
1021

1022
        /* Then, add a link to the job. */
1023
        if (by) {
186,342✔
1024
                if (!job_dependency_new(by, job, FLAGS_SET(flags, TRANSACTION_MATTERS), FLAGS_SET(flags, TRANSACTION_CONFLICTS)))
155,382✔
1025
                        return -ENOMEM;
1026
        } else {
1027
                /* If the job has no parent job, it is the anchor job. */
1028
                assert(!tr->anchor_job);
30,960✔
1029
                tr->anchor_job = job;
30,960✔
1030

1031
                if (FLAGS_SET(flags, TRANSACTION_REENQUEUE_ANCHOR))
30,960✔
1032
                        job->refuse_late_merge = true;
5✔
1033
        }
1034

1035
        if (!is_new || FLAGS_SET(flags, TRANSACTION_IGNORE_REQUIREMENTS) || type == JOB_NOP)
186,342✔
1036
                return 0;
1037

1038
        _cleanup_set_free_ Set *following = NULL;
65,280✔
1039
        Unit *dep;
65,280✔
1040

1041
        /* If we are following some other unit, make sure we add all dependencies of everybody following. */
1042
        if (unit_following_set(job->unit, &following) > 0)
65,280✔
1043
                SET_FOREACH(dep, following) {
3,372✔
1044
                        r = transaction_add_job_and_dependencies(tr, type, dep, job, flags & TRANSACTION_IGNORE_ORDER, e);
2,916✔
1045
                        if (r < 0) {
2,916✔
1046
                                log_unit_full_errno(dep, r == -ERFKILL ? LOG_INFO : LOG_WARNING, r,
×
1047
                                                    "Cannot add dependency job, ignoring: %s",
1048
                                                    bus_error_message(e, r));
1049
                                sd_bus_error_free(e);
×
1050
                        }
1051
                }
1052

1053
        /* Finally, recursively add in all dependencies. */
1054
        if (IN_SET(type, JOB_START, JOB_RESTART)) {
65,280✔
1055
                UNIT_FOREACH_DEPENDENCY_SAFE(dep, job->unit, UNIT_ATOM_PULL_IN_START) {
561,894✔
1056
                        r = transaction_add_job_and_dependencies(tr, JOB_START, dep, job, TRANSACTION_MATTERS | (flags & TRANSACTION_IGNORE_ORDER), e);
43,663✔
1057
                        if (r < 0) {
43,663✔
1058
                                if (r != -EBADR) /* job type not applicable */
3✔
1059
                                        goto fail;
3✔
1060

1061
                                sd_bus_error_free(e);
×
1062
                        }
1063
                }
1064

1065
                UNIT_FOREACH_DEPENDENCY_SAFE(dep, job->unit, UNIT_ATOM_PULL_IN_START_IGNORED) {
531,716✔
1066
                        r = transaction_add_job_and_dependencies(tr, JOB_START, dep, job, flags & TRANSACTION_IGNORE_ORDER, e);
43,786✔
1067
                        if (r < 0) {
43,786✔
1068
                                /* unit masked, job type not applicable and unit not found are not considered
1069
                                 * as errors. */
1070
                                log_unit_full_errno(dep,
183✔
1071
                                                    IN_SET(r, -ERFKILL, -EBADR, -ENOENT) ? LOG_DEBUG : LOG_WARNING,
1072
                                                    r, "Cannot add dependency job, ignoring: %s",
1073
                                                    bus_error_message(e, r));
1074
                                sd_bus_error_free(e);
183✔
1075
                        }
1076
                }
1077

1078
                UNIT_FOREACH_DEPENDENCY_SAFE(dep, job->unit, UNIT_ATOM_PULL_IN_VERIFY) {
98,356✔
1079
                        r = transaction_add_job_and_dependencies(tr, JOB_VERIFY_ACTIVE, dep, job, TRANSACTION_MATTERS | (flags & TRANSACTION_IGNORE_ORDER), e);
×
1080
                        if (r < 0) {
×
1081
                                if (r != -EBADR) /* job type not applicable */
×
1082
                                        goto fail;
×
1083

1084
                                sd_bus_error_free(e);
×
1085
                        }
1086
                }
1087

1088
                UNIT_FOREACH_DEPENDENCY_SAFE(dep, job->unit, UNIT_ATOM_PULL_IN_STOP) {
214,435✔
1089
                        r = transaction_add_job_and_dependencies(tr, JOB_STOP, dep, job, TRANSACTION_MATTERS | TRANSACTION_CONFLICTS | (flags & TRANSACTION_IGNORE_ORDER), e);
43,273✔
1090
                        if (r < 0) {
43,273✔
1091
                                if (r != -EBADR) /* job type not applicable */
×
1092
                                        goto fail;
×
1093

1094
                                sd_bus_error_free(e);
×
1095
                        }
1096
                }
1097

1098
                UNIT_FOREACH_DEPENDENCY_SAFE(dep, job->unit, UNIT_ATOM_PULL_IN_STOP_IGNORED) {
110,435✔
1099
                        r = transaction_add_job_and_dependencies(tr, JOB_STOP, dep, job, flags & TRANSACTION_IGNORE_ORDER, e);
9,695✔
1100
                        if (r < 0) {
9,695✔
1101
                                log_unit_warning(dep,
×
1102
                                                 "Cannot add dependency job, ignoring: %s",
1103
                                                 bus_error_message(e, r));
1104
                                sd_bus_error_free(e);
×
1105
                        }
1106
                }
1107
        }
1108

1109
        if (IN_SET(type, JOB_RESTART, JOB_STOP) || (type == JOB_START && FLAGS_SET(flags, TRANSACTION_PROPAGATE_START_AS_RESTART))) {
65,277✔
1110
                bool is_stop = type == JOB_STOP;
16,128✔
1111

1112
                UNIT_FOREACH_DEPENDENCY_SAFE(dep, job->unit, UNIT_ATOM_PROPAGATE_STOP) {
165,411✔
1113
                        /* We propagate RESTART only as TRY_RESTART, in order not to start dependencies that
1114
                         * are not around. */
1115
                        JobType nt;
11,720✔
1116

1117
                        nt = job_type_collapse(is_stop ? JOB_STOP : JOB_TRY_RESTART, dep);
11,720✔
1118
                        if (nt == JOB_NOP)
11,720✔
1119
                                continue;
×
1120

1121
                        r = transaction_add_job_and_dependencies(tr, nt, dep, job, TRANSACTION_MATTERS | (flags & TRANSACTION_IGNORE_ORDER), e);
11,720✔
1122
                        if (r < 0) {
11,720✔
1123
                                if (r != -EBADR) /* job type not applicable */
×
1124
                                        return r;
×
1125

1126
                                sd_bus_error_free(e);
×
1127
                        }
1128
                }
1129

1130
                /* Process UNIT_ATOM_PROPAGATE_STOP_GRACEFUL (PropagatesStopTo=) units. We need to wait until
1131
                 * all other dependencies are processed, i.e. we're the anchor job or already in the
1132
                 * recursion that handles it. */
1133
                if (!by || FLAGS_SET(flags, TRANSACTION_PROCESS_PROPAGATE_STOP_GRACEFUL))
16,128✔
1134
                        UNIT_FOREACH_DEPENDENCY_SAFE(dep, job->unit, UNIT_ATOM_PROPAGATE_STOP_GRACEFUL) {
262✔
1135
                                JobType nt;
×
1136
                                Job *j;
×
1137

1138
                                j = hashmap_get(tr->jobs, dep);
×
1139
                                nt = job_type_propagate_stop_graceful(j);
×
1140

1141
                                if (nt == JOB_NOP)
×
1142
                                        continue;
×
1143

1144
                                r = transaction_add_job_and_dependencies(tr, nt, dep, job, TRANSACTION_MATTERS | (flags & TRANSACTION_IGNORE_ORDER) | TRANSACTION_PROCESS_PROPAGATE_STOP_GRACEFUL, e);
×
1145
                                if (r < 0) {
×
1146
                                        if (r != -EBADR) /* job type not applicable */
×
1147
                                                return r;
×
1148

1149
                                        sd_bus_error_free(e);
×
1150
                                }
1151
                        }
1152
        }
1153

1154
        if (type == JOB_RELOAD)
65,277✔
1155
                transaction_add_propagate_reload_jobs(tr, job->unit, job, flags & TRANSACTION_IGNORE_ORDER);
6✔
1156

1157
        /* JOB_VERIFY_ACTIVE requires no dependency handling. */
1158

1159
        return 0;
1160

1161
fail:
3✔
1162
        /* Recursive call failed to add required jobs so let's drop top level job as well. */
1163
        log_unit_debug_errno(unit, r, "Cannot add dependency job to transaction, deleting job %s/%s again: %s",
3✔
1164
                             unit->id, job_type_to_string(type), bus_error_message(e, r));
1165

1166
        transaction_delete_job(tr, job, /* delete_dependencies= */ false);
3✔
1167
        return r;
1168
}
1169

1170
static bool shall_stop_on_isolate(Transaction *tr, Unit *u) {
13,528✔
1171
        assert(tr);
13,528✔
1172
        assert(u);
13,528✔
1173

1174
        if (u->ignore_on_isolate)
13,528✔
1175
                return false;
13,528✔
1176

1177
        /* Is there already something listed for this? */
1178
        if (hashmap_contains(tr->jobs, u))
726✔
1179
                return false;
1180

1181
        /* Keep units that are triggered by units we want to keep around. */
1182
        Unit *other;
515✔
1183
        UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_TRIGGERED_BY) {
1,034✔
1184
                if (UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
2✔
1185
                        continue;
×
1186

1187
                /* Is the trigger about to go down? */
1188
                Job *other_job = hashmap_get(tr->jobs, other);
2✔
1189

1190
                bool has_stop = false;
2✔
1191
                LIST_FOREACH(transaction, j, other_job)
2✔
1192
                        if (j->type == JOB_STOP) {
×
1193
                                has_stop = true;
1194
                                break;
1195
                        }
1196
                if (has_stop)
2✔
1197
                        continue;
×
1198

1199
                if (other->ignore_on_isolate || other_job)
2✔
1200
                        return false;
×
1201
        }
1202

1203
        return true;
515✔
1204
}
1205

1206
int transaction_add_isolate_jobs(Transaction *tr, Manager *m) {
211✔
1207
        Unit *u;
211✔
1208
        char *k;
211✔
1209
        int r;
211✔
1210

1211
        assert(tr);
211✔
1212
        assert(m);
211✔
1213

1214
        HASHMAP_FOREACH_KEY(u, k, m->units) {
29,699✔
1215
                _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
29,488✔
1216

1217
                /* Ignore aliases. */
1218
                if (u->id != k)
29,488✔
1219
                        continue;
809✔
1220

1221
                /* No need to stop inactive units. */
1222
                if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u)) && !u->job)
28,679✔
1223
                        continue;
15,151✔
1224

1225
                if (!shall_stop_on_isolate(tr, u))
13,528✔
1226
                        continue;
13,013✔
1227

1228
                r = transaction_add_job_and_dependencies(tr, JOB_STOP, u, tr->anchor_job, TRANSACTION_MATTERS, &e);
515✔
1229
                if (r < 0)
515✔
1230
                        log_unit_warning_errno(u, r, "Cannot add isolate job, ignoring: %s", bus_error_message(&e, r));
×
1231
        }
1232

1233
        return 0;
211✔
1234
}
1235

1236
int transaction_add_triggering_jobs(Transaction *tr, Unit *u) {
×
1237
        Unit *trigger;
×
1238
        int r;
×
1239

1240
        assert(tr);
×
1241
        assert(u);
×
1242

1243
        UNIT_FOREACH_DEPENDENCY_SAFE(trigger, u, UNIT_ATOM_TRIGGERED_BY) {
×
1244
                _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
×
1245

1246
                /* No need to stop inactive jobs. */
1247
                if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(trigger)) && !trigger->job)
×
1248
                        continue;
×
1249

1250
                /* Is there already something listed for this? */
1251
                if (hashmap_contains(tr->jobs, trigger))
×
1252
                        continue;
×
1253

1254
                r = transaction_add_job_and_dependencies(tr, JOB_STOP, trigger, tr->anchor_job, TRANSACTION_MATTERS, &e);
×
1255
                if (r < 0)
×
1256
                        log_unit_warning_errno(u, r, "Cannot add triggered by job, ignoring: %s", bus_error_message(&e, r));
×
1257
        }
1258

1259
        return 0;
×
1260
}
1261

1262
Transaction* transaction_new(bool irreversible, uint64_t id) {
30,970✔
1263
        _cleanup_free_ Transaction *tr = NULL;
30,970✔
1264

1265
        assert(id != 0);
30,970✔
1266

1267
        tr = new(Transaction, 1);
30,970✔
1268
        if (!tr)
30,970✔
1269
                return NULL;
1270

1271
        *tr = (Transaction) {
61,940✔
1272
                .jobs = hashmap_new(NULL),
30,970✔
1273
                .irreversible = irreversible,
1274
                .id = id,
1275
        };
1276
        if (!tr->jobs)
30,970✔
1277
                return NULL;
×
1278

1279
        return TAKE_PTR(tr);
1280
}
1281

1282
Transaction* transaction_free(Transaction *tr) {
30,970✔
1283
        if (!tr)
30,970✔
1284
                return NULL;
1285

1286
        assert(hashmap_isempty(tr->jobs));
30,970✔
1287
        hashmap_free(tr->jobs);
30,970✔
1288

1289
        return mfree(tr);
30,970✔
1290
}
1291

1292
Transaction* transaction_abort_and_free(Transaction *tr) {
29,154✔
1293
        if (!tr)
29,154✔
1294
                return NULL;
1295

1296
        transaction_abort(tr);
29,154✔
1297

1298
        return transaction_free(tr);
29,154✔
1299
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc