| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-bus.h" |
| 4 |
#include "sd-messages.h" |
| 5 |
#include "sd-varlink.h" |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "ansi-color.h" |
| 9 |
#include "async.h" |
| 10 |
#include "cgroup.h" |
| 11 |
#include "condition.h" |
| 12 |
#include "dbus.h" |
| 13 |
#include "dbus-job.h" |
| 14 |
#include "escape.h" |
| 15 |
#include "job.h" |
| 16 |
#include "log.h" |
| 17 |
#include "manager.h" |
| 18 |
#include "parse-util.h" |
| 19 |
#include "prioq.h" |
| 20 |
#include "serialize.h" |
| 21 |
#include "set.h" |
| 22 |
#include "sort-util.h" |
| 23 |
#include "special.h" |
| 24 |
#include "string-table.h" |
| 25 |
#include "string-util.h" |
| 26 |
#include "strv.h" |
| 27 |
#include "unit.h" |
| 28 |
#include "varlink-unit.h" |
| 29 |
#include "virt.h" |
| 30 |
|
| 31 |
Job* job_new_raw(Unit *unit) { |
| 32 |
Job *j; |
| 33 |
|
| 34 |
/* used for deserialization */ |
| 35 |
|
| 36 |
assert(unit); |
| 37 |
|
| 38 |
j = new(Job, 1); |
| 39 |
if (!j) |
| 40 |
return NULL; |
| 41 |
|
| 42 |
*j = (Job) { |
| 43 |
.manager = unit->manager, |
| 44 |
.unit = unit, |
| 45 |
.type = _JOB_TYPE_INVALID, |
| 46 |
.state = JOB_WAITING, |
| 47 |
.result = _JOB_RESULT_INVALID, |
| 48 |
}; |
| 49 |
|
| 50 |
return j; |
| 51 |
} |
| 52 |
|
| 53 |
static uint32_t manager_get_new_job_id(Manager *m) { |
| 54 |
bool overflow = false; |
| 55 |
|
| 56 |
assert(m); |
| 57 |
|
| 58 |
for (;;) { |
| 59 |
uint32_t id = m->current_job_id; |
| 60 |
|
| 61 |
if (_unlikely_(id == UINT32_MAX)) { |
| 62 |
assert_se(!overflow); |
| 63 |
m->current_job_id = 1; |
| 64 |
overflow = true; |
| 65 |
} else |
| 66 |
m->current_job_id++; |
| 67 |
|
| 68 |
if (hashmap_contains(m->jobs, UINT32_TO_PTR(id))) |
| 69 |
continue; |
| 70 |
|
| 71 |
return id; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
Job* job_new(Unit *unit, JobType type) { |
| 76 |
Job *j; |
| 77 |
|
| 78 |
assert(type < _JOB_TYPE_MAX); |
| 79 |
|
| 80 |
j = job_new_raw(unit); |
| 81 |
if (!j) |
| 82 |
return NULL; |
| 83 |
|
| 84 |
j->id = manager_get_new_job_id(j->manager); |
| 85 |
j->type = type; |
| 86 |
|
| 87 |
/* We don't link it here, that's what job_dependency() is for */ |
| 88 |
|
| 89 |
return j; |
| 90 |
} |
| 91 |
|
| 92 |
void job_unlink(Job *j) { |
| 93 |
assert(j); |
| 94 |
assert(!j->installed); |
| 95 |
assert(!j->transaction_prev); |
| 96 |
assert(!j->transaction_next); |
| 97 |
assert(!j->subject_list); |
| 98 |
assert(!j->object_list); |
| 99 |
|
| 100 |
if (j->in_run_queue) { |
| 101 |
prioq_remove(j->manager->run_queue, j, &j->run_queue_idx); |
| 102 |
j->in_run_queue = false; |
| 103 |
} |
| 104 |
|
| 105 |
if (j->in_dbus_queue) { |
| 106 |
LIST_REMOVE(dbus_queue, j->manager->dbus_job_queue, j); |
| 107 |
j->in_dbus_queue = false; |
| 108 |
} |
| 109 |
|
| 110 |
if (j->in_gc_queue) { |
| 111 |
LIST_REMOVE(gc_queue, j->manager->gc_job_queue, j); |
| 112 |
j->in_gc_queue = false; |
| 113 |
} |
| 114 |
|
| 115 |
j->timer_event_source = sd_event_source_disable_unref(j->timer_event_source); |
| 116 |
} |
| 117 |
|
| 118 |
Job* job_free(Job *j) { |
| 119 |
assert(j); |
| 120 |
assert(!j->installed); |
| 121 |
assert(!j->transaction_prev); |
| 122 |
assert(!j->transaction_next); |
| 123 |
assert(!j->subject_list); |
| 124 |
assert(!j->object_list); |
| 125 |
|
| 126 |
job_unlink(j); |
| 127 |
|
| 128 |
sd_bus_track_unref(j->bus_track); |
| 129 |
sd_varlink_unref(j->varlink); |
| 130 |
strv_free(j->deserialized_clients); |
| 131 |
|
| 132 |
activation_details_unref(j->activation_details); |
| 133 |
|
| 134 |
return mfree(j); |
| 135 |
} |
| 136 |
|
| 137 |
static void job_set_state(Job *j, JobState state) { |
| 138 |
assert(j); |
| 139 |
assert(j->manager); |
| 140 |
assert(state >= 0); |
| 141 |
assert(state < _JOB_STATE_MAX); |
| 142 |
|
| 143 |
if (j->state == state) |
| 144 |
return; |
| 145 |
|
| 146 |
JobState old_state = j->state; |
| 147 |
j->state = state; |
| 148 |
|
| 149 |
if (!j->installed) |
| 150 |
return; |
| 151 |
|
| 152 |
if (j->state == JOB_RUNNING) |
| 153 |
/* This job changed into running, count up */ |
| 154 |
j->manager->n_running_jobs++; |
| 155 |
else if (old_state == JOB_RUNNING) { |
| 156 |
/* This job changed away from running into another state, count down. */ |
| 157 |
assert(j->manager->n_running_jobs > 0); |
| 158 |
j->manager->n_running_jobs--; |
| 159 |
|
| 160 |
if (j->manager->n_running_jobs <= 0) |
| 161 |
j->manager->jobs_in_progress_event_source = sd_event_source_disable_unref(j->manager->jobs_in_progress_event_source); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
void job_uninstall(Job *j) { |
| 166 |
Job **pj; |
| 167 |
|
| 168 |
assert(j); |
| 169 |
assert(j->installed); |
| 170 |
|
| 171 |
job_set_state(j, JOB_FINISHED); |
| 172 |
|
| 173 |
pj = j->type == JOB_NOP ? &j->unit->nop_job : &j->unit->job; |
| 174 |
assert(*pj == j); |
| 175 |
|
| 176 |
/* Detach from next 'bigger' objects */ |
| 177 |
|
| 178 |
/* daemon-reload should be transparent to job observers */ |
| 179 |
if (!MANAGER_IS_RELOADING(j->manager)) { |
| 180 |
bus_job_send_removed_signal(j); |
| 181 |
varlink_job_send_removed_signal(j); |
| 182 |
} |
| 183 |
|
| 184 |
*pj = NULL; |
| 185 |
|
| 186 |
unit_add_to_gc_queue(j->unit); |
| 187 |
|
| 188 |
unit_add_to_dbus_queue(j->unit); /* The Job property of the unit has changed now */ |
| 189 |
|
| 190 |
hashmap_remove_value(j->manager->jobs, UINT32_TO_PTR(j->id), j); |
| 191 |
j->installed = false; |
| 192 |
} |
| 193 |
|
| 194 |
static bool jobs_may_late_merge(const Job *j, const Job *uj) { |
| 195 |
assert(j); |
| 196 |
assert(!j->installed); |
| 197 |
assert(uj); |
| 198 |
assert(uj->installed); |
| 199 |
assert(uj->state == JOB_RUNNING); |
| 200 |
|
| 201 |
/* Tells whether it is OK to merge a job with an already running job. */ |
| 202 |
|
| 203 |
if (j->refuse_late_merge) /* refused when constructing transaction? */ |
| 204 |
return false; |
| 205 |
|
| 206 |
/* Reloads cannot be merged this way. Think of the sequence: |
| 207 |
* 1. Reload of a daemon is in progress; the daemon has already loaded its config file, but hasn't |
| 208 |
* completed the reload operation yet. |
| 209 |
* 2. Edit foo's config file. |
| 210 |
* 3. Trigger another reload to have the daemon use the new config. |
| 211 |
* Should the second reload job be merged into the first one, the daemon would not know about the new config. |
| 212 |
* JOB_RESTART jobs on the other hand can be merged, because they get patched into JOB_START |
| 213 |
* after stopping the unit. So if we see a JOB_RESTART running, it means the unit hasn't stopped yet |
| 214 |
* and at this time the merge is still allowed. */ |
| 215 |
if (j->type == JOB_RELOAD) |
| 216 |
return false; |
| 217 |
|
| 218 |
return job_type_is_superset(uj->type, j->type); |
| 219 |
} |
| 220 |
|
| 221 |
static void job_merge_into_installed(Job *j, Job *other) { |
| 222 |
assert(j); |
| 223 |
assert(j->installed); |
| 224 |
assert(other); |
| 225 |
assert(j->unit == other->unit); |
| 226 |
|
| 227 |
if (j->type != JOB_NOP) { |
| 228 |
assert_se(job_type_merge_and_collapse(&j->type, other->type, j->unit) == 0); |
| 229 |
|
| 230 |
/* Keep the oldest ActivationDetails, if any */ |
| 231 |
if (!j->activation_details) |
| 232 |
j->activation_details = TAKE_PTR(other->activation_details); |
| 233 |
} else |
| 234 |
assert(other->type == JOB_NOP); |
| 235 |
|
| 236 |
j->irreversible = j->irreversible || other->irreversible; |
| 237 |
j->ignore_order = j->ignore_order || other->ignore_order; |
| 238 |
} |
| 239 |
|
| 240 |
Job* job_install(Job *j) { |
| 241 |
Job **pj; |
| 242 |
Job *uj; |
| 243 |
|
| 244 |
assert(j); |
| 245 |
assert(!j->installed); |
| 246 |
assert(j->type >= 0 && j->type < _JOB_TYPE_MAX_IN_TRANSACTION); |
| 247 |
assert(j->state == JOB_WAITING); |
| 248 |
|
| 249 |
pj = j->type == JOB_NOP ? &j->unit->nop_job : &j->unit->job; |
| 250 |
uj = *pj; |
| 251 |
|
| 252 |
if (uj) { |
| 253 |
if (job_type_is_conflicting(uj->type, j->type)) |
| 254 |
job_finish_and_invalidate(uj, JOB_CANCELED, false, false); |
| 255 |
else { |
| 256 |
/* not conflicting, i.e. mergeable */ |
| 257 |
|
| 258 |
if (uj->state == JOB_WAITING || jobs_may_late_merge(j, uj)) { |
| 259 |
job_merge_into_installed(uj, j); |
| 260 |
log_unit_debug(uj->unit, |
| 261 |
"Merged %s/%s into installed job %s/%s as %"PRIu32, |
| 262 |
j->unit->id, job_type_to_string(j->type), uj->unit->id, |
| 263 |
job_type_to_string(uj->type), uj->id); |
| 264 |
return uj; |
| 265 |
} else { |
| 266 |
/* already running and not safe to merge into */ |
| 267 |
/* Patch uj to become a merged job and re-run it. */ |
| 268 |
/* XXX It should be safer to queue j to run after uj finishes, but it is |
| 269 |
* not currently possible to have more than one installed job per unit. */ |
| 270 |
job_merge_into_installed(uj, j); |
| 271 |
log_unit_debug(uj->unit, |
| 272 |
"Merged into running job, re-running: %s/%s as %"PRIu32, |
| 273 |
uj->unit->id, job_type_to_string(uj->type), uj->id); |
| 274 |
|
| 275 |
job_set_state(uj, JOB_WAITING); |
| 276 |
return uj; |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
/* Install the job */ |
| 282 |
assert(!*pj); |
| 283 |
*pj = j; |
| 284 |
j->installed = true; |
| 285 |
|
| 286 |
j->manager->n_installed_jobs++; |
| 287 |
log_unit_debug(j->unit, |
| 288 |
"Installed new job %s/%s as %u", |
| 289 |
j->unit->id, job_type_to_string(j->type), (unsigned) j->id); |
| 290 |
|
| 291 |
job_add_to_gc_queue(j); |
| 292 |
|
| 293 |
job_add_to_dbus_queue(j); /* announce this job to clients */ |
| 294 |
unit_add_to_dbus_queue(j->unit); /* The Job property of the unit has changed now */ |
| 295 |
|
| 296 |
return j; |
| 297 |
} |
| 298 |
|
| 299 |
int job_install_deserialized(Job *j) { |
| 300 |
Job **pj; |
| 301 |
int r; |
| 302 |
|
| 303 |
assert(j); |
| 304 |
assert(j->manager); |
| 305 |
assert(!j->installed); |
| 306 |
|
| 307 |
if (j->type < 0 || j->type >= _JOB_TYPE_MAX_IN_TRANSACTION) |
| 308 |
return log_unit_debug_errno(j->unit, SYNTHETIC_ERRNO(EINVAL), |
| 309 |
"Invalid job type %s in deserialization.", |
| 310 |
strna(job_type_to_string(j->type))); |
| 311 |
|
| 312 |
pj = j->type == JOB_NOP ? &j->unit->nop_job : &j->unit->job; |
| 313 |
if (*pj) |
| 314 |
return log_unit_debug_errno(j->unit, SYNTHETIC_ERRNO(EEXIST), |
| 315 |
"Unit already has a job installed. Not installing deserialized job."); |
| 316 |
|
| 317 |
/* When the job does not have ID, or we failed to deserialize the job ID, then use a new ID. */ |
| 318 |
if (j->id <= 0) |
| 319 |
j->id = manager_get_new_job_id(j->manager); |
| 320 |
|
| 321 |
r = hashmap_ensure_put(&j->manager->jobs, NULL, UINT32_TO_PTR(j->id), j); |
| 322 |
if (r == -EEXIST) |
| 323 |
return log_unit_debug_errno(j->unit, r, "Job ID %" PRIu32 " already used, cannot deserialize job.", j->id); |
| 324 |
if (r < 0) |
| 325 |
return log_unit_debug_errno(j->unit, r, "Failed to insert job into jobs hash table: %m"); |
| 326 |
|
| 327 |
*pj = j; |
| 328 |
j->installed = true; |
| 329 |
|
| 330 |
if (j->state == JOB_RUNNING) |
| 331 |
j->manager->n_running_jobs++; |
| 332 |
|
| 333 |
log_unit_debug(j->unit, |
| 334 |
"Reinstalled deserialized job %s/%s as %u", |
| 335 |
j->unit->id, job_type_to_string(j->type), (unsigned) j->id); |
| 336 |
return 0; |
| 337 |
} |
| 338 |
|
| 339 |
JobDependency* job_dependency_new(Job *subject, Job *object, bool matters, bool conflicts) { |
| 340 |
JobDependency *l; |
| 341 |
|
| 342 |
assert(object); |
| 343 |
|
| 344 |
/* Adds a new job link, which encodes that the 'subject' job |
| 345 |
* needs the 'object' job in some way. If 'subject' is NULL |
| 346 |
* this means the 'anchor' job (i.e. the one the user |
| 347 |
* explicitly asked for) is the requester. */ |
| 348 |
|
| 349 |
l = new(JobDependency, 1); |
| 350 |
if (!l) |
| 351 |
return NULL; |
| 352 |
|
| 353 |
*l = (JobDependency) { |
| 354 |
.subject = subject, |
| 355 |
.object = object, |
| 356 |
.matters = matters, |
| 357 |
.conflicts = conflicts, |
| 358 |
}; |
| 359 |
|
| 360 |
if (subject) |
| 361 |
LIST_PREPEND(subject, subject->subject_list, l); |
| 362 |
|
| 363 |
LIST_PREPEND(object, object->object_list, l); |
| 364 |
|
| 365 |
return l; |
| 366 |
} |
| 367 |
|
| 368 |
void job_dependency_free(JobDependency *l) { |
| 369 |
assert(l); |
| 370 |
|
| 371 |
if (l->subject) |
| 372 |
LIST_REMOVE(subject, l->subject->subject_list, l); |
| 373 |
|
| 374 |
LIST_REMOVE(object, l->object->object_list, l); |
| 375 |
|
| 376 |
free(l); |
| 377 |
} |
| 378 |
|
| 379 |
void job_dump(Job *j, FILE *f, const char *prefix) { |
| 380 |
assert(j); |
| 381 |
assert(f); |
| 382 |
|
| 383 |
prefix = strempty(prefix); |
| 384 |
|
| 385 |
fprintf(f, |
| 386 |
"%s-> Job %u:\n" |
| 387 |
"%s\tAction: %s -> %s\n" |
| 388 |
"%s\tState: %s\n" |
| 389 |
"%s\tIrreversible: %s\n" |
| 390 |
"%s\tMay GC: %s\n", |
| 391 |
prefix, j->id, |
| 392 |
prefix, j->unit->id, job_type_to_string(j->type), |
| 393 |
prefix, job_state_to_string(j->state), |
| 394 |
prefix, yes_no(j->irreversible), |
| 395 |
prefix, yes_no(job_may_gc(j))); |
| 396 |
} |
| 397 |
|
| 398 |
/* |
| 399 |
* Merging is commutative, so imagine the matrix as symmetric. We store only |
| 400 |
* its lower triangle to avoid duplication. We don't store the main diagonal, |
| 401 |
* because A merged with A is simply A. |
| 402 |
* |
| 403 |
* If the resulting type is collapsed immediately afterwards (to get rid of |
| 404 |
* the JOB_RELOAD_OR_START, which lies outside the lookup function's domain), |
| 405 |
* the following properties hold: |
| 406 |
* |
| 407 |
* Merging is associative! A merged with B, and then merged with C is the same |
| 408 |
* as A merged with the result of B merged with C. |
| 409 |
* |
| 410 |
* Mergeability is transitive! If A can be merged with B and B with C then |
| 411 |
* A also with C. |
| 412 |
* |
| 413 |
* Also, if A merged with B cannot be merged with C, then either A or B cannot |
| 414 |
* be merged with C either. |
| 415 |
*/ |
| 416 |
static const JobType job_merging_table[] = { |
| 417 |
/* What \ With * JOB_START JOB_VERIFY_ACTIVE JOB_STOP JOB_RELOAD */ |
| 418 |
/*********************************************************************************/ |
| 419 |
/* JOB_START */ |
| 420 |
/* JOB_VERIFY_ACTIVE */ JOB_START, |
| 421 |
/* JOB_STOP */ -1, -1, |
| 422 |
/* JOB_RELOAD */ JOB_RELOAD_OR_START, JOB_RELOAD, -1, |
| 423 |
/* JOB_RESTART */ JOB_RESTART, JOB_RESTART, -1, JOB_RESTART, |
| 424 |
}; |
| 425 |
|
| 426 |
JobType job_type_lookup_merge(JobType a, JobType b) { |
| 427 |
assert_cc(ELEMENTSOF(job_merging_table) == _JOB_TYPE_MAX_MERGING * (_JOB_TYPE_MAX_MERGING - 1) / 2); |
| 428 |
assert(a >= 0 && a < _JOB_TYPE_MAX_MERGING); |
| 429 |
assert(b >= 0 && b < _JOB_TYPE_MAX_MERGING); |
| 430 |
|
| 431 |
if (a == b) |
| 432 |
return a; |
| 433 |
|
| 434 |
if (a < b) { |
| 435 |
JobType tmp = a; |
| 436 |
a = b; |
| 437 |
b = tmp; |
| 438 |
} |
| 439 |
|
| 440 |
return job_merging_table[(a - 1) * a / 2 + b]; |
| 441 |
} |
| 442 |
|
| 443 |
bool job_type_is_redundant(JobType a, UnitActiveState b) { |
| 444 |
switch (a) { |
| 445 |
|
| 446 |
case JOB_START: |
| 447 |
case JOB_VERIFY_ACTIVE: |
| 448 |
return UNIT_IS_ACTIVE_OR_RELOADING(b); |
| 449 |
|
| 450 |
case JOB_STOP: |
| 451 |
return UNIT_IS_INACTIVE_OR_FAILED(b); |
| 452 |
|
| 453 |
case JOB_RELOAD: |
| 454 |
/* Reload jobs are never considered redundant/duplicate. Refer to jobs_may_late_merge() for |
| 455 |
* a detailed justification. */ |
| 456 |
case JOB_RESTART: |
| 457 |
/* Restart jobs must always be kept. |
| 458 |
* |
| 459 |
* For ACTIVE/RELOADING units, this is obvious. |
| 460 |
* |
| 461 |
* For ACTIVATING units, it's more subtle: |
| 462 |
* |
| 463 |
* Generally, if a service Requires= another unit, restarts of |
| 464 |
* the unit must be propagated to the service. If the service is |
| 465 |
* ACTIVATING, it must still be restarted since it might have |
| 466 |
* stale information regarding the other unit. |
| 467 |
* |
| 468 |
* For example, consider a service that Requires= a socket: if |
| 469 |
* the socket is restarted, but the service is still ACTIVATING, |
| 470 |
* it's necessary to restart the service so that it gets the new |
| 471 |
* socket. */ |
| 472 |
return false; |
| 473 |
|
| 474 |
case JOB_NOP: |
| 475 |
return true; |
| 476 |
|
| 477 |
default: |
| 478 |
assert_not_reached(); |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
JobType job_type_collapse(JobType t, Unit *u) { |
| 483 |
UnitActiveState s; |
| 484 |
|
| 485 |
switch (t) { |
| 486 |
|
| 487 |
case JOB_TRY_RESTART: |
| 488 |
/* Be sure to keep the restart job even if the unit is |
| 489 |
* ACTIVATING. |
| 490 |
* |
| 491 |
* See the job_type_is_redundant(JOB_RESTART) for more info */ |
| 492 |
s = unit_active_state(u); |
| 493 |
if (!UNIT_IS_ACTIVE_OR_ACTIVATING(s)) |
| 494 |
return JOB_NOP; |
| 495 |
|
| 496 |
return JOB_RESTART; |
| 497 |
|
| 498 |
case JOB_TRY_RELOAD: |
| 499 |
s = unit_active_state(u); |
| 500 |
if (!UNIT_IS_ACTIVE_OR_RELOADING(s)) |
| 501 |
return JOB_NOP; |
| 502 |
|
| 503 |
return JOB_RELOAD; |
| 504 |
|
| 505 |
case JOB_RELOAD_OR_START: |
| 506 |
s = unit_active_state(u); |
| 507 |
if (!UNIT_IS_ACTIVE_OR_RELOADING(s)) |
| 508 |
return JOB_START; |
| 509 |
|
| 510 |
return JOB_RELOAD; |
| 511 |
|
| 512 |
default: |
| 513 |
assert(t >= 0 && t < _JOB_TYPE_MAX_IN_TRANSACTION); |
| 514 |
return t; |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
int job_type_merge_and_collapse(JobType *a, JobType b, Unit *u) { |
| 519 |
JobType t; |
| 520 |
|
| 521 |
assert(a); |
| 522 |
|
| 523 |
t = job_type_lookup_merge(*a, b); |
| 524 |
if (t < 0) |
| 525 |
return -EEXIST; |
| 526 |
|
| 527 |
*a = job_type_collapse(t, u); |
| 528 |
return 0; |
| 529 |
} |
| 530 |
|
| 531 |
static bool job_is_runnable(Job *j) { |
| 532 |
Unit *other; |
| 533 |
|
| 534 |
assert(j); |
| 535 |
assert(j->installed); |
| 536 |
|
| 537 |
/* Checks whether there is any job running for the units this |
| 538 |
* job needs to be running after (in the case of a 'positive' |
| 539 |
* job type) or before (in the case of a 'negative' job |
| 540 |
* type. */ |
| 541 |
|
| 542 |
/* Note that unit types have a say in what is runnable, |
| 543 |
* too. For example, if they return -EAGAIN from |
| 544 |
* unit_start() they can indicate they are not |
| 545 |
* runnable yet. */ |
| 546 |
|
| 547 |
/* First check if there is an override */ |
| 548 |
if (j->ignore_order) |
| 549 |
return true; |
| 550 |
|
| 551 |
if (j->type == JOB_NOP) |
| 552 |
return true; |
| 553 |
|
| 554 |
UNIT_FOREACH_DEPENDENCY(other, j->unit, UNIT_ATOM_AFTER) |
| 555 |
if (other->job && job_compare(j, other->job, UNIT_ATOM_AFTER) > 0) { |
| 556 |
log_unit_debug(j->unit, |
| 557 |
"starting held back, waiting for: %s", |
| 558 |
other->id); |
| 559 |
return false; |
| 560 |
} |
| 561 |
|
| 562 |
UNIT_FOREACH_DEPENDENCY(other, j->unit, UNIT_ATOM_BEFORE) |
| 563 |
if (other->job && job_compare(j, other->job, UNIT_ATOM_BEFORE) > 0) { |
| 564 |
log_unit_debug(j->unit, |
| 565 |
"stopping held back, waiting for: %s", |
| 566 |
other->id); |
| 567 |
return false; |
| 568 |
} |
| 569 |
|
| 570 |
return true; |
| 571 |
} |
| 572 |
|
| 573 |
static void job_change_type(Job *j, JobType newtype) { |
| 574 |
assert(j); |
| 575 |
|
| 576 |
log_unit_debug(j->unit, |
| 577 |
"Converting job %s/%s -> %s/%s", |
| 578 |
j->unit->id, job_type_to_string(j->type), |
| 579 |
j->unit->id, job_type_to_string(newtype)); |
| 580 |
|
| 581 |
j->type = newtype; |
| 582 |
} |
| 583 |
|
| 584 |
static const char* job_start_message_format(Unit *u, JobType t) { |
| 585 |
assert(u); |
| 586 |
assert(IN_SET(t, JOB_START, JOB_STOP, JOB_RELOAD)); |
| 587 |
|
| 588 |
if (t == JOB_RELOAD) |
| 589 |
return "Reloading %s..."; |
| 590 |
else if (t == JOB_START) |
| 591 |
return UNIT_VTABLE(u)->status_message_formats.starting_stopping[0] ?: "Starting %s..."; |
| 592 |
else |
| 593 |
return UNIT_VTABLE(u)->status_message_formats.starting_stopping[1] ?: "Stopping %s..."; |
| 594 |
} |
| 595 |
|
| 596 |
static void job_emit_start_message(Unit *u, uint32_t job_id, JobType t) { |
| 597 |
_cleanup_free_ char *free_ident = NULL; |
| 598 |
const char *ident, *format; |
| 599 |
|
| 600 |
assert(u); |
| 601 |
assert(t >= 0); |
| 602 |
assert(t < _JOB_TYPE_MAX); |
| 603 |
assert(u->id); /* We better don't try to run a unit that doesn't even have an id. */ |
| 604 |
|
| 605 |
if (!IN_SET(t, JOB_START, JOB_STOP, JOB_RELOAD)) |
| 606 |
return; |
| 607 |
|
| 608 |
if (!unit_log_level_test(u, LOG_INFO)) |
| 609 |
return; |
| 610 |
|
| 611 |
format = job_start_message_format(u, t); |
| 612 |
ident = unit_status_string(u, &free_ident); |
| 613 |
|
| 614 |
bool do_console = t != JOB_RELOAD; |
| 615 |
bool console_only = do_console && log_on_console(); /* Reload status messages have traditionally |
| 616 |
* not been printed to the console. */ |
| 617 |
|
| 618 |
/* Print to the log first. */ |
| 619 |
if (!console_only) { /* Skip this if it would only go on the console anyway */ |
| 620 |
|
| 621 |
const char *mid = |
| 622 |
t == JOB_START ? "MESSAGE_ID=" SD_MESSAGE_UNIT_STARTING_STR : |
| 623 |
t == JOB_STOP ? "MESSAGE_ID=" SD_MESSAGE_UNIT_STOPPING_STR : |
| 624 |
"MESSAGE_ID=" SD_MESSAGE_UNIT_RELOADING_STR; |
| 625 |
const char *msg_fmt = strjoina("MESSAGE=", format); |
| 626 |
|
| 627 |
/* Note that we deliberately use LOG_MESSAGE() instead of LOG_UNIT_MESSAGE() here, since this |
| 628 |
* is supposed to mimic closely what is written to screen using the status output, which is |
| 629 |
* supposed to be high level friendly output. */ |
| 630 |
|
| 631 |
DISABLE_WARNING_FORMAT_NONLITERAL; |
| 632 |
log_unit_struct(u, LOG_INFO, |
| 633 |
msg_fmt, ident, |
| 634 |
LOG_ITEM("JOB_ID=%" PRIu32, job_id), |
| 635 |
LOG_ITEM("JOB_TYPE=%s", job_type_to_string(t)), |
| 636 |
LOG_UNIT_INVOCATION_ID(u), |
| 637 |
mid); |
| 638 |
REENABLE_WARNING; |
| 639 |
} |
| 640 |
|
| 641 |
/* Log to the console second. */ |
| 642 |
if (do_console) { |
| 643 |
DISABLE_WARNING_FORMAT_NONLITERAL; |
| 644 |
unit_status_printf(u, STATUS_TYPE_NORMAL, "", format, ident); |
| 645 |
REENABLE_WARNING; |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
static const char* job_done_message_format(Unit *u, JobType t, JobResult result) { |
| 650 |
static const char* const generic_finished_start_job[_JOB_RESULT_MAX] = { |
| 651 |
[JOB_DONE] = "Started %s.", |
| 652 |
[JOB_TIMEOUT] = "Timed out starting %s.", |
| 653 |
[JOB_FAILED] = "Failed to start %s.", |
| 654 |
[JOB_DEPENDENCY] = "Dependency failed for %s.", |
| 655 |
[JOB_ASSERT] = "Assertion failed for %s.", |
| 656 |
[JOB_UNSUPPORTED] = "Starting of %s unsupported.", |
| 657 |
[JOB_COLLECTED] = "Unnecessary job was removed for %s.", |
| 658 |
[JOB_ONCE] = "Unit %s has been started before and cannot be started again.", |
| 659 |
[JOB_FROZEN] = "Cannot start frozen unit %s.", |
| 660 |
[JOB_CONCURRENCY] = "Hard concurrency limit hit for slice of unit %s.", |
| 661 |
}; |
| 662 |
static const char* const generic_finished_stop_job[_JOB_RESULT_MAX] = { |
| 663 |
[JOB_DONE] = "Stopped %s.", |
| 664 |
[JOB_FAILED] = "Stopped %s with error.", |
| 665 |
[JOB_TIMEOUT] = "Timed out stopping %s.", |
| 666 |
[JOB_FROZEN] = "Cannot stop frozen unit %s.", |
| 667 |
}; |
| 668 |
static const char* const generic_finished_reload_job[_JOB_RESULT_MAX] = { |
| 669 |
[JOB_DONE] = "Reloaded %s.", |
| 670 |
[JOB_FAILED] = "Reload failed for %s.", |
| 671 |
[JOB_TIMEOUT] = "Timed out reloading %s.", |
| 672 |
[JOB_FROZEN] = "Cannot reload frozen unit %s.", |
| 673 |
}; |
| 674 |
/* When verify-active detects the unit is inactive, report it. |
| 675 |
* Most likely a DEPEND warning from a requisiting unit will |
| 676 |
* occur next and it's nice to see what was requisited. */ |
| 677 |
static const char* const generic_finished_verify_active_job[_JOB_RESULT_MAX] = { |
| 678 |
[JOB_SKIPPED] = "%s is inactive.", |
| 679 |
}; |
| 680 |
const char *format; |
| 681 |
|
| 682 |
assert(u); |
| 683 |
assert(t >= 0); |
| 684 |
assert(t < _JOB_TYPE_MAX); |
| 685 |
|
| 686 |
/* Show condition check message if the job did not actually do anything due to unmet condition. */ |
| 687 |
if (t == JOB_START && result == JOB_DONE && !u->condition_result) |
| 688 |
return "Condition check resulted in %s being skipped."; |
| 689 |
|
| 690 |
if (IN_SET(t, JOB_START, JOB_STOP, JOB_RESTART)) { |
| 691 |
const UnitStatusMessageFormats *formats = &UNIT_VTABLE(u)->status_message_formats; |
| 692 |
if (formats->finished_job) { |
| 693 |
format = formats->finished_job(u, t, result); |
| 694 |
if (format) |
| 695 |
return format; |
| 696 |
} |
| 697 |
|
| 698 |
format = (t == JOB_START ? formats->finished_start_job : formats->finished_stop_job)[result]; |
| 699 |
if (format) |
| 700 |
return format; |
| 701 |
} |
| 702 |
|
| 703 |
/* Return generic strings */ |
| 704 |
switch (t) { |
| 705 |
case JOB_START: |
| 706 |
return generic_finished_start_job[result]; |
| 707 |
case JOB_STOP: |
| 708 |
case JOB_RESTART: |
| 709 |
return generic_finished_stop_job[result]; |
| 710 |
case JOB_RELOAD: |
| 711 |
return generic_finished_reload_job[result]; |
| 712 |
case JOB_VERIFY_ACTIVE: |
| 713 |
return generic_finished_verify_active_job[result]; |
| 714 |
default: |
| 715 |
return NULL; |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
static const struct { |
| 720 |
int log_level; |
| 721 |
const char *color, *word; |
| 722 |
} job_done_messages[_JOB_RESULT_MAX] = { |
| 723 |
[JOB_DONE] = { LOG_INFO, ANSI_OK_COLOR, " OK " }, |
| 724 |
[JOB_CANCELED] = { LOG_INFO, }, |
| 725 |
[JOB_TIMEOUT] = { LOG_ERR, ANSI_HIGHLIGHT_RED, " TIME " }, |
| 726 |
[JOB_FAILED] = { LOG_ERR, ANSI_HIGHLIGHT_RED, "FAILED" }, |
| 727 |
[JOB_DEPENDENCY] = { LOG_WARNING, ANSI_HIGHLIGHT_YELLOW, "DEPEND" }, |
| 728 |
[JOB_SKIPPED] = { LOG_NOTICE, ANSI_HIGHLIGHT, " INFO " }, |
| 729 |
[JOB_INVALID] = { LOG_INFO, }, |
| 730 |
[JOB_ASSERT] = { LOG_WARNING, ANSI_HIGHLIGHT_YELLOW, "ASSERT" }, |
| 731 |
[JOB_UNSUPPORTED] = { LOG_WARNING, ANSI_HIGHLIGHT_YELLOW, "UNSUPP" }, |
| 732 |
[JOB_COLLECTED] = { LOG_INFO, }, |
| 733 |
[JOB_ONCE] = { LOG_ERR, ANSI_HIGHLIGHT_RED, " ONCE " }, |
| 734 |
[JOB_FROZEN] = { LOG_ERR, ANSI_HIGHLIGHT_RED, "FROZEN" }, |
| 735 |
[JOB_CONCURRENCY] = { LOG_ERR, ANSI_HIGHLIGHT_RED, "CONCUR" }, |
| 736 |
}; |
| 737 |
|
| 738 |
static const char* job_done_mid(JobType type, JobResult result) { |
| 739 |
switch (type) { |
| 740 |
case JOB_START: |
| 741 |
if (result == JOB_DONE) |
| 742 |
return "MESSAGE_ID=" SD_MESSAGE_UNIT_STARTED_STR; |
| 743 |
else |
| 744 |
return "MESSAGE_ID=" SD_MESSAGE_UNIT_FAILED_STR; |
| 745 |
|
| 746 |
case JOB_RELOAD: |
| 747 |
return "MESSAGE_ID=" SD_MESSAGE_UNIT_RELOADED_STR; |
| 748 |
|
| 749 |
case JOB_STOP: |
| 750 |
case JOB_RESTART: |
| 751 |
return "MESSAGE_ID=" SD_MESSAGE_UNIT_STOPPED_STR; |
| 752 |
|
| 753 |
default: |
| 754 |
return NULL; |
| 755 |
} |
| 756 |
} |
| 757 |
|
| 758 |
static void job_emit_done_message(Unit *u, uint32_t job_id, JobType t, JobResult result) { |
| 759 |
_cleanup_free_ char *free_ident = NULL; |
| 760 |
const char *ident, *format; |
| 761 |
|
| 762 |
assert(u); |
| 763 |
assert(t >= 0); |
| 764 |
assert(t < _JOB_TYPE_MAX); |
| 765 |
|
| 766 |
if (!unit_log_level_test(u, job_done_messages[result].log_level)) |
| 767 |
return; |
| 768 |
|
| 769 |
format = job_done_message_format(u, t, result); |
| 770 |
if (!format) |
| 771 |
return; |
| 772 |
|
| 773 |
ident = unit_status_string(u, &free_ident); |
| 774 |
|
| 775 |
const char *status = job_done_messages[result].word; |
| 776 |
bool do_console = t != JOB_RELOAD && status; |
| 777 |
bool console_only = do_console && log_on_console(); |
| 778 |
|
| 779 |
if (t == JOB_START && result == JOB_DONE && !u->condition_result) { |
| 780 |
/* No message on the console if the job did not actually do anything due to unmet condition. */ |
| 781 |
if (console_only) |
| 782 |
return; |
| 783 |
|
| 784 |
do_console = false; |
| 785 |
} |
| 786 |
|
| 787 |
if (!console_only) { /* Skip printing if output goes to the console, and job_print_status_message() |
| 788 |
* will actually print something to the console. */ |
| 789 |
Condition *c; |
| 790 |
const char *mid = job_done_mid(t, result); /* mid may be NULL. log_unit_struct() will ignore it. */ |
| 791 |
|
| 792 |
c = t == JOB_START && result == JOB_DONE ? unit_find_failed_condition(u) : NULL; |
| 793 |
if (c) { |
| 794 |
/* Special case units that were skipped because of a unmet condition check so that |
| 795 |
* we can add more information to the message. */ |
| 796 |
if (c->trigger) |
| 797 |
log_unit_struct( |
| 798 |
u, |
| 799 |
job_done_messages[result].log_level, |
| 800 |
LOG_MESSAGE("%s skipped, no trigger condition checks were met.", |
| 801 |
ident), |
| 802 |
LOG_ITEM("JOB_ID=%" PRIu32, job_id), |
| 803 |
LOG_ITEM("JOB_TYPE=%s", job_type_to_string(t)), |
| 804 |
LOG_ITEM("JOB_RESULT=%s", job_result_to_string(result)), |
| 805 |
LOG_UNIT_INVOCATION_ID(u), |
| 806 |
mid); |
| 807 |
else |
| 808 |
log_unit_struct( |
| 809 |
u, |
| 810 |
job_done_messages[result].log_level, |
| 811 |
LOG_MESSAGE("%s skipped, unmet condition check %s=%s%s", |
| 812 |
ident, |
| 813 |
condition_type_to_string(c->type), |
| 814 |
c->negate ? "!" : "", |
| 815 |
c->parameter), |
| 816 |
LOG_ITEM("JOB_ID=%" PRIu32, job_id), |
| 817 |
LOG_ITEM("JOB_TYPE=%s", job_type_to_string(t)), |
| 818 |
LOG_ITEM("JOB_RESULT=%s", job_result_to_string(result)), |
| 819 |
LOG_UNIT_INVOCATION_ID(u), |
| 820 |
mid); |
| 821 |
} else { |
| 822 |
const char *msg_fmt = strjoina("MESSAGE=", format); |
| 823 |
|
| 824 |
DISABLE_WARNING_FORMAT_NONLITERAL; |
| 825 |
log_unit_struct(u, job_done_messages[result].log_level, |
| 826 |
msg_fmt, ident, |
| 827 |
LOG_ITEM("JOB_ID=%" PRIu32, job_id), |
| 828 |
LOG_ITEM("JOB_TYPE=%s", job_type_to_string(t)), |
| 829 |
LOG_ITEM("JOB_RESULT=%s", job_result_to_string(result)), |
| 830 |
LOG_UNIT_INVOCATION_ID(u), |
| 831 |
mid); |
| 832 |
REENABLE_WARNING; |
| 833 |
} |
| 834 |
} |
| 835 |
|
| 836 |
if (do_console) { |
| 837 |
if (log_get_show_color()) |
| 838 |
status = strjoina(job_done_messages[result].color, |
| 839 |
status, |
| 840 |
ANSI_NORMAL); |
| 841 |
|
| 842 |
DISABLE_WARNING_FORMAT_NONLITERAL; |
| 843 |
unit_status_printf(u, |
| 844 |
result == JOB_DONE ? STATUS_TYPE_NORMAL : STATUS_TYPE_NOTICE, |
| 845 |
status, format, ident); |
| 846 |
REENABLE_WARNING; |
| 847 |
|
| 848 |
if (t == JOB_START && result == JOB_FAILED) { |
| 849 |
_cleanup_free_ char *quoted = NULL; |
| 850 |
|
| 851 |
quoted = shell_maybe_quote(u->id, 0); |
| 852 |
if (quoted) |
| 853 |
manager_status_printf(u->manager, STATUS_TYPE_NORMAL, NULL, |
| 854 |
"See 'systemctl status %s' for details.", quoted); |
| 855 |
} |
| 856 |
} |
| 857 |
} |
| 858 |
|
| 859 |
static int job_perform_on_unit(Job **j) { |
| 860 |
ActivationDetails *a; |
| 861 |
uint32_t id; |
| 862 |
Manager *m; |
| 863 |
JobType t; |
| 864 |
Unit *u; |
| 865 |
bool wait_only; |
| 866 |
int r; |
| 867 |
|
| 868 |
/* While we execute this operation the job might go away (for example: because it finishes immediately |
| 869 |
* or is replaced by a new, conflicting job). To make sure we don't access a freed job later on we |
| 870 |
* store the id here, so that we can verify the job is still valid. */ |
| 871 |
|
| 872 |
assert(j); |
| 873 |
assert(*j); |
| 874 |
|
| 875 |
m = (*j)->manager; |
| 876 |
u = (*j)->unit; |
| 877 |
t = (*j)->type; |
| 878 |
id = (*j)->id; |
| 879 |
a = (*j)->activation_details; |
| 880 |
|
| 881 |
switch (t) { |
| 882 |
case JOB_START: |
| 883 |
r = unit_start(u, a); |
| 884 |
wait_only = r == -EBADR; /* If the unit type does not support starting, then simply wait. */ |
| 885 |
break; |
| 886 |
|
| 887 |
case JOB_RESTART: |
| 888 |
t = JOB_STOP; |
| 889 |
_fallthrough_; |
| 890 |
case JOB_STOP: |
| 891 |
r = unit_stop(u); |
| 892 |
wait_only = r == -EBADR; /* If the unit type does not support stopping, then simply wait. */ |
| 893 |
break; |
| 894 |
|
| 895 |
case JOB_RELOAD: |
| 896 |
r = unit_reload(u); |
| 897 |
wait_only = false; /* A clear error is generated if reload is not supported. */ |
| 898 |
break; |
| 899 |
|
| 900 |
default: |
| 901 |
assert_not_reached(); |
| 902 |
} |
| 903 |
|
| 904 |
/* Log if the job still exists and the start/stop/reload function actually did something or we're |
| 905 |
* only waiting for unit status change (common for device units). The latter ensures that job start |
| 906 |
* messages for device units are correctly shown. Note that if the job disappears too quickly, e.g. |
| 907 |
* for units for which there's no 'activating' phase (i.e. because we transition directly from |
| 908 |
* 'inactive' to 'active'), we'll possibly skip the "Starting..." message. */ |
| 909 |
*j = manager_get_job(m, id); |
| 910 |
if (*j && (r > 0 || wait_only)) |
| 911 |
job_emit_start_message(u, id, t); |
| 912 |
|
| 913 |
return wait_only ? 0 : r; |
| 914 |
} |
| 915 |
|
| 916 |
int job_run_and_invalidate(Job *j) { |
| 917 |
int r; |
| 918 |
|
| 919 |
assert(j); |
| 920 |
assert(j->installed); |
| 921 |
assert(j->type < _JOB_TYPE_MAX_IN_TRANSACTION); |
| 922 |
assert(j->in_run_queue); |
| 923 |
|
| 924 |
prioq_remove(j->manager->run_queue, j, &j->run_queue_idx); |
| 925 |
j->in_run_queue = false; |
| 926 |
|
| 927 |
if (j->state != JOB_WAITING) |
| 928 |
return 0; |
| 929 |
|
| 930 |
if (!job_is_runnable(j)) |
| 931 |
return -EAGAIN; |
| 932 |
|
| 933 |
job_start_timer(j, true); |
| 934 |
job_set_state(j, JOB_RUNNING); |
| 935 |
job_add_to_dbus_queue(j); |
| 936 |
|
| 937 |
switch (j->type) { |
| 938 |
|
| 939 |
case JOB_VERIFY_ACTIVE: { |
| 940 |
UnitActiveState t; |
| 941 |
|
| 942 |
t = unit_active_state(j->unit); |
| 943 |
if (UNIT_IS_ACTIVE_OR_RELOADING(t)) |
| 944 |
r = -EALREADY; |
| 945 |
else if (t == UNIT_ACTIVATING) |
| 946 |
r = -EAGAIN; |
| 947 |
else |
| 948 |
r = -EBADR; |
| 949 |
break; |
| 950 |
} |
| 951 |
|
| 952 |
case JOB_START: |
| 953 |
case JOB_STOP: |
| 954 |
case JOB_RESTART: |
| 955 |
case JOB_RELOAD: |
| 956 |
r = job_perform_on_unit(&j); |
| 957 |
break; |
| 958 |
|
| 959 |
case JOB_NOP: |
| 960 |
r = -EALREADY; |
| 961 |
break; |
| 962 |
|
| 963 |
default: |
| 964 |
assert_not_reached(); |
| 965 |
} |
| 966 |
|
| 967 |
if (j) { |
| 968 |
if (r == -EAGAIN) |
| 969 |
job_set_state(j, JOB_WAITING); /* Hmm, not ready after all, let's return to JOB_WAITING state */ |
| 970 |
else if (r == -EALREADY) /* already being executed */ |
| 971 |
r = job_finish_and_invalidate(j, JOB_DONE, true, true); |
| 972 |
else if (r == -ECOMM) |
| 973 |
r = job_finish_and_invalidate(j, JOB_DONE, true, false); |
| 974 |
else if (r == -EBADR) |
| 975 |
r = job_finish_and_invalidate(j, JOB_SKIPPED, true, false); |
| 976 |
else if (r == -ENOEXEC) |
| 977 |
r = job_finish_and_invalidate(j, JOB_INVALID, true, false); |
| 978 |
else if (r == -EPROTO) |
| 979 |
r = job_finish_and_invalidate(j, JOB_ASSERT, true, false); |
| 980 |
else if (r == -EOPNOTSUPP) |
| 981 |
r = job_finish_and_invalidate(j, JOB_UNSUPPORTED, true, false); |
| 982 |
else if (r == -ENOLINK) |
| 983 |
r = job_finish_and_invalidate(j, JOB_DEPENDENCY, true, false); |
| 984 |
else if (r == -ESTALE) |
| 985 |
r = job_finish_and_invalidate(j, JOB_ONCE, true, false); |
| 986 |
else if (r == -EDEADLK) |
| 987 |
r = job_finish_and_invalidate(j, JOB_FROZEN, true, false); |
| 988 |
else if (r == -ETOOMANYREFS) |
| 989 |
r = job_finish_and_invalidate(j, JOB_CONCURRENCY, /* recursive= */ true, /* already= */ false); |
| 990 |
else if (r < 0) |
| 991 |
r = job_finish_and_invalidate(j, JOB_FAILED, true, false); |
| 992 |
} |
| 993 |
|
| 994 |
return r; |
| 995 |
} |
| 996 |
|
| 997 |
static void job_fail_dependencies(Unit *u, UnitDependencyAtom match_atom) { |
| 998 |
Unit *other; |
| 999 |
|
| 1000 |
assert(u); |
| 1001 |
|
| 1002 |
UNIT_FOREACH_DEPENDENCY(other, u, match_atom) { |
| 1003 |
Job *j = other->job; |
| 1004 |
|
| 1005 |
if (!j) |
| 1006 |
continue; |
| 1007 |
if (!IN_SET(j->type, JOB_START, JOB_VERIFY_ACTIVE)) |
| 1008 |
continue; |
| 1009 |
|
| 1010 |
job_finish_and_invalidate(j, JOB_DEPENDENCY, true, false); |
| 1011 |
} |
| 1012 |
} |
| 1013 |
|
| 1014 |
int job_finish_and_invalidate(Job *j, JobResult result, bool recursive, bool already) { |
| 1015 |
Unit *u, *other; |
| 1016 |
JobType t; |
| 1017 |
|
| 1018 |
assert(j); |
| 1019 |
assert(j->installed); |
| 1020 |
assert(j->type < _JOB_TYPE_MAX_IN_TRANSACTION); |
| 1021 |
|
| 1022 |
u = j->unit; |
| 1023 |
t = j->type; |
| 1024 |
|
| 1025 |
j->result = result; |
| 1026 |
|
| 1027 |
job_set_state(j, JOB_FINISHED); |
| 1028 |
|
| 1029 |
log_unit_debug(u, "Job %" PRIu32 " %s/%s finished, result=%s", |
| 1030 |
j->id, u->id, job_type_to_string(t), job_result_to_string(result)); |
| 1031 |
|
| 1032 |
/* If this job did nothing to the respective unit we don't log the status message */ |
| 1033 |
if (!already) |
| 1034 |
job_emit_done_message(u, j->id, t, result); |
| 1035 |
|
| 1036 |
/* Patch restart jobs so that they become normal start jobs */ |
| 1037 |
if (result == JOB_DONE && t == JOB_RESTART) { |
| 1038 |
|
| 1039 |
job_change_type(j, JOB_START); |
| 1040 |
job_set_state(j, JOB_WAITING); |
| 1041 |
|
| 1042 |
job_add_to_dbus_queue(j); |
| 1043 |
job_add_to_run_queue(j); |
| 1044 |
job_add_to_gc_queue(j); |
| 1045 |
|
| 1046 |
goto finish; |
| 1047 |
} |
| 1048 |
|
| 1049 |
if (IN_SET(result, JOB_FAILED, JOB_INVALID, JOB_FROZEN, JOB_CONCURRENCY)) |
| 1050 |
j->manager->n_failed_jobs++; |
| 1051 |
|
| 1052 |
job_uninstall(j); |
| 1053 |
job_free(j); |
| 1054 |
|
| 1055 |
/* Fail depending jobs on failure */ |
| 1056 |
if (result != JOB_DONE && recursive) { |
| 1057 |
if (IN_SET(t, JOB_START, JOB_VERIFY_ACTIVE)) |
| 1058 |
job_fail_dependencies(u, UNIT_ATOM_PROPAGATE_START_FAILURE); |
| 1059 |
else if (t == JOB_STOP) |
| 1060 |
job_fail_dependencies(u, UNIT_ATOM_PROPAGATE_STOP_FAILURE); |
| 1061 |
} |
| 1062 |
|
| 1063 |
/* A special check to make sure we take down anything RequisiteOf= if we aren't active. This is when |
| 1064 |
* the verify-active job merges with a satisfying job type, and then loses its invalidation effect, |
| 1065 |
* as the result there is JOB_DONE for the start job we merged into, while we should be failing the |
| 1066 |
* depending job if the said unit isn't in fact active. Oneshots are an example of this, where going |
| 1067 |
* directly from activating to inactive is success. |
| 1068 |
* |
| 1069 |
* This happens when you use ConditionXYZ= in a unit too, since in that case the job completes with |
| 1070 |
* the JOB_DONE result, but the unit never really becomes active. Note that such a case still |
| 1071 |
* involves merging: |
| 1072 |
* |
| 1073 |
* A start job waits for something else, and a verify-active comes in and merges in the installed |
| 1074 |
* job. Then, later, when it becomes runnable, it finishes with JOB_DONE result as execution on |
| 1075 |
* conditions not being met is skipped, breaking our dependency semantics. |
| 1076 |
* |
| 1077 |
* Also, depending on if start job waits or not, the merging may or may not happen (the verify-active |
| 1078 |
* job may trigger after it finishes), so you get undeterministic results without this check. |
| 1079 |
*/ |
| 1080 |
if (result == JOB_DONE && recursive && |
| 1081 |
IN_SET(t, JOB_START, JOB_RELOAD) && |
| 1082 |
!UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u))) |
| 1083 |
job_fail_dependencies(u, UNIT_ATOM_PROPAGATE_INACTIVE_START_AS_FAILURE); |
| 1084 |
|
| 1085 |
/* Trigger OnFailure= dependencies manually here. We need to do that because a failed job might not |
| 1086 |
* cause a unit state change. Note that we don't treat JOB_CANCELED as failure in this context. |
| 1087 |
* And JOB_FAILURE is already handled by the unit itself (unit_notify). */ |
| 1088 |
if (IN_SET(result, JOB_TIMEOUT, JOB_DEPENDENCY)) { |
| 1089 |
log_unit_struct(u, LOG_NOTICE, |
| 1090 |
LOG_ITEM("JOB_TYPE=%s", job_type_to_string(t)), |
| 1091 |
LOG_ITEM("JOB_RESULT=%s", job_result_to_string(result)), |
| 1092 |
LOG_UNIT_MESSAGE(u, "Job %s/%s failed with result '%s'.", |
| 1093 |
u->id, |
| 1094 |
job_type_to_string(t), |
| 1095 |
job_result_to_string(result))); |
| 1096 |
|
| 1097 |
unit_start_on_termination_deps(u, UNIT_ATOM_ON_FAILURE); |
| 1098 |
} |
| 1099 |
|
| 1100 |
unit_trigger_notify(u); |
| 1101 |
|
| 1102 |
finish: |
| 1103 |
/* Try to start the next jobs that can be started */ |
| 1104 |
UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_AFTER) |
| 1105 |
if (other->job) { |
| 1106 |
job_add_to_run_queue(other->job); |
| 1107 |
job_add_to_gc_queue(other->job); |
| 1108 |
} |
| 1109 |
UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_BEFORE) |
| 1110 |
if (other->job) { |
| 1111 |
job_add_to_run_queue(other->job); |
| 1112 |
job_add_to_gc_queue(other->job); |
| 1113 |
} |
| 1114 |
|
| 1115 |
/* Ensure that when an upheld/unneeded/bound unit activation job fails we requeue it, if it still |
| 1116 |
* necessary. If there are no state changes in the triggerer, it would not be retried otherwise. */ |
| 1117 |
unit_submit_to_start_when_upheld_queue(u); |
| 1118 |
unit_submit_to_stop_when_bound_queue(u); |
| 1119 |
unit_submit_to_stop_when_unneeded_queue(u); |
| 1120 |
|
| 1121 |
/* All jobs might have finished, let's see */ |
| 1122 |
if (u->manager->may_dispatch_stop_notify_queue == 0) |
| 1123 |
u->manager->may_dispatch_stop_notify_queue = -1; |
| 1124 |
|
| 1125 |
manager_check_finished(u->manager); |
| 1126 |
|
| 1127 |
return 0; |
| 1128 |
} |
| 1129 |
|
| 1130 |
static int job_dispatch_timer(sd_event_source *s, uint64_t monotonic, void *userdata) { |
| 1131 |
Job *j = ASSERT_PTR(userdata); |
| 1132 |
Unit *u; |
| 1133 |
|
| 1134 |
assert(s == j->timer_event_source); |
| 1135 |
|
| 1136 |
log_unit_warning(j->unit, "Job %s/%s timed out.", j->unit->id, job_type_to_string(j->type)); |
| 1137 |
|
| 1138 |
u = j->unit; |
| 1139 |
job_finish_and_invalidate(j, JOB_TIMEOUT, true, false); |
| 1140 |
|
| 1141 |
emergency_action( |
| 1142 |
u->manager, |
| 1143 |
u->job_timeout_action, |
| 1144 |
EMERGENCY_ACTION_IS_WATCHDOG|EMERGENCY_ACTION_WARN|EMERGENCY_ACTION_SLEEP_5S, |
| 1145 |
u->job_timeout_reboot_arg, |
| 1146 |
/* exit_status= */ -1, |
| 1147 |
"job timed out"); |
| 1148 |
|
| 1149 |
return 0; |
| 1150 |
} |
| 1151 |
|
| 1152 |
int job_start_timer(Job *j, bool job_running) { |
| 1153 |
int r; |
| 1154 |
usec_t timeout_time, old_timeout_time; |
| 1155 |
|
| 1156 |
if (job_running) { |
| 1157 |
j->begin_running_usec = now(CLOCK_MONOTONIC); |
| 1158 |
|
| 1159 |
if (j->unit->job_running_timeout == USEC_INFINITY) |
| 1160 |
return 0; |
| 1161 |
|
| 1162 |
timeout_time = usec_add(j->begin_running_usec, j->unit->job_running_timeout); |
| 1163 |
|
| 1164 |
if (j->timer_event_source) { |
| 1165 |
/* Update only if JobRunningTimeoutSec= results in earlier timeout */ |
| 1166 |
r = sd_event_source_get_time(j->timer_event_source, &old_timeout_time); |
| 1167 |
if (r < 0) |
| 1168 |
return r; |
| 1169 |
|
| 1170 |
if (old_timeout_time <= timeout_time) |
| 1171 |
return 0; |
| 1172 |
|
| 1173 |
return sd_event_source_set_time(j->timer_event_source, timeout_time); |
| 1174 |
} |
| 1175 |
} else { |
| 1176 |
if (j->timer_event_source) |
| 1177 |
return 0; |
| 1178 |
|
| 1179 |
j->begin_usec = now(CLOCK_MONOTONIC); |
| 1180 |
|
| 1181 |
if (j->unit->job_timeout == USEC_INFINITY) |
| 1182 |
return 0; |
| 1183 |
|
| 1184 |
timeout_time = usec_add(j->begin_usec, j->unit->job_timeout); |
| 1185 |
} |
| 1186 |
|
| 1187 |
r = sd_event_add_time( |
| 1188 |
j->manager->event, |
| 1189 |
&j->timer_event_source, |
| 1190 |
CLOCK_MONOTONIC, |
| 1191 |
timeout_time, 0, |
| 1192 |
job_dispatch_timer, j); |
| 1193 |
if (r < 0) |
| 1194 |
return r; |
| 1195 |
|
| 1196 |
(void) sd_event_source_set_description(j->timer_event_source, "job-start"); |
| 1197 |
|
| 1198 |
return 0; |
| 1199 |
} |
| 1200 |
|
| 1201 |
void job_add_to_run_queue(Job *j) { |
| 1202 |
int r; |
| 1203 |
|
| 1204 |
assert(j); |
| 1205 |
assert(j->installed); |
| 1206 |
|
| 1207 |
if (j->in_run_queue) |
| 1208 |
return; |
| 1209 |
|
| 1210 |
r = prioq_put(j->manager->run_queue, j, &j->run_queue_idx); |
| 1211 |
if (r < 0) |
| 1212 |
log_warning_errno(r, "Failed to put job in run queue, ignoring: %m"); |
| 1213 |
else |
| 1214 |
j->in_run_queue = true; |
| 1215 |
|
| 1216 |
manager_trigger_run_queue(j->manager); |
| 1217 |
} |
| 1218 |
|
| 1219 |
void job_add_to_dbus_queue(Job *j) { |
| 1220 |
assert(j); |
| 1221 |
assert(j->installed); |
| 1222 |
|
| 1223 |
if (j->in_dbus_queue) |
| 1224 |
return; |
| 1225 |
|
| 1226 |
/* We don't check if anybody is subscribed here, since this |
| 1227 |
* job might just have been created and not yet assigned to a |
| 1228 |
* connection/client. */ |
| 1229 |
|
| 1230 |
LIST_PREPEND(dbus_queue, j->manager->dbus_job_queue, j); |
| 1231 |
j->in_dbus_queue = true; |
| 1232 |
} |
| 1233 |
|
| 1234 |
char* job_dbus_path(Job *j) { |
| 1235 |
char *p; |
| 1236 |
|
| 1237 |
assert(j); |
| 1238 |
|
| 1239 |
if (asprintf(&p, "/org/freedesktop/systemd1/job/%"PRIu32, j->id) < 0) |
| 1240 |
return NULL; |
| 1241 |
|
| 1242 |
return p; |
| 1243 |
} |
| 1244 |
|
| 1245 |
int job_serialize(Job *j, FILE *f) { |
| 1246 |
assert(j); |
| 1247 |
assert(f); |
| 1248 |
|
| 1249 |
(void) serialize_item_format(f, "job-id", "%u", j->id); |
| 1250 |
(void) serialize_item(f, "job-type", job_type_to_string(j->type)); |
| 1251 |
(void) serialize_item(f, "job-state", job_state_to_string(j->state)); |
| 1252 |
(void) serialize_bool(f, "job-irreversible", j->irreversible); |
| 1253 |
(void) serialize_bool(f, "job-sent-dbus-new-signal", j->sent_dbus_new_signal); |
| 1254 |
(void) serialize_bool(f, "job-ignore-order", j->ignore_order); |
| 1255 |
|
| 1256 |
if (j->begin_usec > 0) |
| 1257 |
(void) serialize_usec(f, "job-begin", j->begin_usec); |
| 1258 |
if (j->begin_running_usec > 0) |
| 1259 |
(void) serialize_usec(f, "job-begin-running", j->begin_running_usec); |
| 1260 |
|
| 1261 |
bus_track_serialize(j->bus_track, f, "subscribed"); |
| 1262 |
|
| 1263 |
activation_details_serialize(j->activation_details, f); |
| 1264 |
|
| 1265 |
/* End marker */ |
| 1266 |
fputc('\n', f); |
| 1267 |
return 0; |
| 1268 |
} |
| 1269 |
|
| 1270 |
int job_deserialize(Job *j, FILE *f) { |
| 1271 |
int r; |
| 1272 |
|
| 1273 |
assert(j); |
| 1274 |
assert(f); |
| 1275 |
|
| 1276 |
for (;;) { |
| 1277 |
_cleanup_free_ char *l = NULL; |
| 1278 |
size_t k; |
| 1279 |
const char *v; |
| 1280 |
|
| 1281 |
r = deserialize_read_line(f, &l); |
| 1282 |
if (r < 0) |
| 1283 |
return r; |
| 1284 |
if (r == 0) /* eof or end marker */ |
| 1285 |
break; |
| 1286 |
|
| 1287 |
k = strcspn(l, "="); |
| 1288 |
|
| 1289 |
if (l[k] == '=') { |
| 1290 |
l[k] = 0; |
| 1291 |
v = l+k+1; |
| 1292 |
} else |
| 1293 |
v = l+k; |
| 1294 |
|
| 1295 |
if (streq(l, "job-id")) { |
| 1296 |
|
| 1297 |
if (safe_atou32(v, &j->id) < 0) |
| 1298 |
log_debug("Failed to parse job id value: %s", v); |
| 1299 |
|
| 1300 |
} else if (streq(l, "job-type")) { |
| 1301 |
JobType t; |
| 1302 |
|
| 1303 |
t = job_type_from_string(v); |
| 1304 |
if (t < 0) |
| 1305 |
log_debug("Failed to parse job type: %s", v); |
| 1306 |
else if (t >= _JOB_TYPE_MAX_IN_TRANSACTION) |
| 1307 |
log_debug("Cannot deserialize job of type: %s", v); |
| 1308 |
else |
| 1309 |
j->type = t; |
| 1310 |
|
| 1311 |
} else if (streq(l, "job-state")) { |
| 1312 |
JobState s; |
| 1313 |
|
| 1314 |
s = job_state_from_string(v); |
| 1315 |
if (s < 0) |
| 1316 |
log_debug("Failed to parse job state: %s", v); |
| 1317 |
else |
| 1318 |
job_set_state(j, s); |
| 1319 |
|
| 1320 |
} else if (streq(l, "job-irreversible")) { |
| 1321 |
int b; |
| 1322 |
|
| 1323 |
b = parse_boolean(v); |
| 1324 |
if (b < 0) |
| 1325 |
log_debug("Failed to parse job irreversible flag: %s", v); |
| 1326 |
else |
| 1327 |
j->irreversible = j->irreversible || b; |
| 1328 |
|
| 1329 |
} else if (streq(l, "job-sent-dbus-new-signal")) { |
| 1330 |
int b; |
| 1331 |
|
| 1332 |
b = parse_boolean(v); |
| 1333 |
if (b < 0) |
| 1334 |
log_debug("Failed to parse job sent_dbus_new_signal flag: %s", v); |
| 1335 |
else |
| 1336 |
j->sent_dbus_new_signal = j->sent_dbus_new_signal || b; |
| 1337 |
|
| 1338 |
} else if (streq(l, "job-ignore-order")) { |
| 1339 |
int b; |
| 1340 |
|
| 1341 |
b = parse_boolean(v); |
| 1342 |
if (b < 0) |
| 1343 |
log_debug("Failed to parse job ignore_order flag: %s", v); |
| 1344 |
else |
| 1345 |
j->ignore_order = j->ignore_order || b; |
| 1346 |
|
| 1347 |
} else if (streq(l, "job-begin")) |
| 1348 |
(void) deserialize_usec(v, &j->begin_usec); |
| 1349 |
|
| 1350 |
else if (streq(l, "job-begin-running")) |
| 1351 |
(void) deserialize_usec(v, &j->begin_running_usec); |
| 1352 |
|
| 1353 |
else if (streq(l, "subscribed")) { |
| 1354 |
if (strv_extend(&j->deserialized_clients, v) < 0) |
| 1355 |
return log_oom(); |
| 1356 |
|
| 1357 |
} else if (startswith(l, "activation-details")) { |
| 1358 |
if (activation_details_deserialize(l, v, &j->activation_details) < 0) |
| 1359 |
log_debug("Failed to parse job ActivationDetails element: %s", v); |
| 1360 |
|
| 1361 |
} else |
| 1362 |
log_debug("Unknown job serialization key: %s", l); |
| 1363 |
} |
| 1364 |
|
| 1365 |
return 0; |
| 1366 |
} |
| 1367 |
|
| 1368 |
int job_coldplug(Job *j) { |
| 1369 |
int r; |
| 1370 |
usec_t timeout_time = USEC_INFINITY; |
| 1371 |
|
| 1372 |
assert(j); |
| 1373 |
|
| 1374 |
/* After deserialization is complete and the bus connection |
| 1375 |
* set up again, let's start watching our subscribers again */ |
| 1376 |
(void) bus_job_coldplug_bus_track(j); |
| 1377 |
|
| 1378 |
if (j->state == JOB_WAITING) |
| 1379 |
job_add_to_run_queue(j); |
| 1380 |
|
| 1381 |
/* Maybe due to new dependencies we don't actually need this job anymore? */ |
| 1382 |
job_add_to_gc_queue(j); |
| 1383 |
|
| 1384 |
/* Create timer only when job began or began running and the respective timeout is finite. |
| 1385 |
* Follow logic of job_start_timer() if both timeouts are finite */ |
| 1386 |
if (j->begin_usec == 0) |
| 1387 |
return 0; |
| 1388 |
|
| 1389 |
if (j->unit->job_timeout != USEC_INFINITY) |
| 1390 |
timeout_time = usec_add(j->begin_usec, j->unit->job_timeout); |
| 1391 |
|
| 1392 |
if (timestamp_is_set(j->begin_running_usec)) |
| 1393 |
timeout_time = MIN(timeout_time, usec_add(j->begin_running_usec, j->unit->job_running_timeout)); |
| 1394 |
|
| 1395 |
if (timeout_time == USEC_INFINITY) |
| 1396 |
return 0; |
| 1397 |
|
| 1398 |
j->timer_event_source = sd_event_source_disable_unref(j->timer_event_source); |
| 1399 |
|
| 1400 |
r = sd_event_add_time( |
| 1401 |
j->manager->event, |
| 1402 |
&j->timer_event_source, |
| 1403 |
CLOCK_MONOTONIC, |
| 1404 |
timeout_time, 0, |
| 1405 |
job_dispatch_timer, j); |
| 1406 |
if (r < 0) |
| 1407 |
log_debug_errno(r, "Failed to restart timeout for job: %m"); |
| 1408 |
|
| 1409 |
(void) sd_event_source_set_description(j->timer_event_source, "job-timeout"); |
| 1410 |
|
| 1411 |
return r; |
| 1412 |
} |
| 1413 |
|
| 1414 |
void job_shutdown_magic(Job *j) { |
| 1415 |
assert(j); |
| 1416 |
assert(j->manager); |
| 1417 |
|
| 1418 |
/* The shutdown target gets some special treatment here: we |
| 1419 |
* tell the kernel to begin with flushing its disk caches, to |
| 1420 |
* optimize shutdown time a bit. Ideally we wouldn't hardcode |
| 1421 |
* this magic into PID 1. However all other processes aren't |
| 1422 |
* options either since they'd exit much sooner than PID 1 and |
| 1423 |
* asynchronous sync() would cause their exit to be |
| 1424 |
* delayed. */ |
| 1425 |
|
| 1426 |
if (j->type != JOB_START) |
| 1427 |
return; |
| 1428 |
|
| 1429 |
if (!unit_has_name(j->unit, SPECIAL_SHUTDOWN_TARGET)) |
| 1430 |
return; |
| 1431 |
|
| 1432 |
/* This is the very beginning of the shutdown phase, so take the timestamp here */ |
| 1433 |
dual_timestamp_now(j->manager->timestamps + MANAGER_TIMESTAMP_SHUTDOWN_START); |
| 1434 |
|
| 1435 |
if (!MANAGER_IS_SYSTEM(j->manager)) |
| 1436 |
return; |
| 1437 |
|
| 1438 |
/* In case messages on console has been disabled on boot */ |
| 1439 |
j->manager->no_console_output = false; |
| 1440 |
|
| 1441 |
manager_invalidate_startup_units(j->manager); |
| 1442 |
|
| 1443 |
if (detect_container() > 0) |
| 1444 |
return; |
| 1445 |
|
| 1446 |
(void) asynchronous_sync(NULL); |
| 1447 |
} |
| 1448 |
|
| 1449 |
int job_get_timeout(Job *j, usec_t *ret) { |
| 1450 |
usec_t x = USEC_INFINITY, y = USEC_INFINITY; |
| 1451 |
Unit *u = ASSERT_PTR(ASSERT_PTR(j)->unit); |
| 1452 |
int r; |
| 1453 |
|
| 1454 |
assert(ret); |
| 1455 |
|
| 1456 |
if (j->timer_event_source) { |
| 1457 |
r = sd_event_source_get_time(j->timer_event_source, &x); |
| 1458 |
if (r < 0) |
| 1459 |
return r; |
| 1460 |
} |
| 1461 |
|
| 1462 |
if (UNIT_VTABLE(u)->get_timeout) { |
| 1463 |
r = UNIT_VTABLE(u)->get_timeout(u, &y); |
| 1464 |
if (r < 0) |
| 1465 |
return r; |
| 1466 |
} |
| 1467 |
|
| 1468 |
if (x == USEC_INFINITY && y == USEC_INFINITY) { |
| 1469 |
*ret = 0; |
| 1470 |
return 0; |
| 1471 |
} |
| 1472 |
|
| 1473 |
*ret = MIN(x, y); |
| 1474 |
return 1; |
| 1475 |
} |
| 1476 |
|
| 1477 |
bool job_may_gc(Job *j) { |
| 1478 |
Unit *other; |
| 1479 |
|
| 1480 |
assert(j); |
| 1481 |
assert(j->manager); |
| 1482 |
|
| 1483 |
/* Checks whether this job should be GC'ed away. We only do this for jobs of units that have no effect on their |
| 1484 |
* own and just track external state. For now the only unit type that qualifies for this are .device units. |
| 1485 |
* Returns true if the job can be collected. */ |
| 1486 |
|
| 1487 |
if (!UNIT_VTABLE(j->unit)->gc_jobs) |
| 1488 |
return false; |
| 1489 |
|
| 1490 |
/* Make sure to send out pending D-Bus events before we unload the unit */ |
| 1491 |
if (j->in_dbus_queue) |
| 1492 |
return false; |
| 1493 |
|
| 1494 |
if (sd_bus_track_count(j->bus_track) > 0) |
| 1495 |
return false; |
| 1496 |
|
| 1497 |
/* FIXME: So this is a bit ugly: for now we don't properly track references made via private bus connections |
| 1498 |
* (because it's nasty, as sd_bus_track doesn't apply to it). We simply remember that the job was once |
| 1499 |
* referenced by one, and reset this whenever we notice that no private bus connections are around. This means |
| 1500 |
* the GC is a bit too conservative when it comes to jobs created by private bus connections. */ |
| 1501 |
if (j->ref_by_private_bus) { |
| 1502 |
if (set_isempty(j->manager->private_buses)) |
| 1503 |
j->ref_by_private_bus = false; |
| 1504 |
else |
| 1505 |
return false; |
| 1506 |
} |
| 1507 |
|
| 1508 |
if (j->type == JOB_NOP) |
| 1509 |
return false; |
| 1510 |
|
| 1511 |
/* The logic is inverse to job_is_runnable, we cannot GC as long as we block any job. */ |
| 1512 |
UNIT_FOREACH_DEPENDENCY(other, j->unit, UNIT_ATOM_BEFORE) |
| 1513 |
if (other->job && job_compare(j, other->job, UNIT_ATOM_BEFORE) < 0) |
| 1514 |
return false; |
| 1515 |
|
| 1516 |
UNIT_FOREACH_DEPENDENCY(other, j->unit, UNIT_ATOM_AFTER) |
| 1517 |
if (other->job && job_compare(j, other->job, UNIT_ATOM_AFTER) < 0) |
| 1518 |
return false; |
| 1519 |
|
| 1520 |
return true; |
| 1521 |
} |
| 1522 |
|
| 1523 |
void job_add_to_gc_queue(Job *j) { |
| 1524 |
assert(j); |
| 1525 |
assert(j->manager); |
| 1526 |
|
| 1527 |
if (j->in_gc_queue) |
| 1528 |
return; |
| 1529 |
|
| 1530 |
if (!job_may_gc(j)) |
| 1531 |
return; |
| 1532 |
|
| 1533 |
LIST_PREPEND(gc_queue, j->manager->gc_job_queue, j); |
| 1534 |
j->in_gc_queue = true; |
| 1535 |
} |
| 1536 |
|
| 1537 |
static int job_compare_id(Job * const *a, Job * const *b) { |
| 1538 |
/* This is called from qsort()s inner loops. Correctly implemented qsort will never pass NULL so we |
| 1539 |
just suppress the check via POINTER_MAY_BE_NULL instead of assert() to avoid the runtime cost. */ |
| 1540 |
POINTER_MAY_BE_NULL(a); |
| 1541 |
POINTER_MAY_BE_NULL(b); |
| 1542 |
|
| 1543 |
return CMP((*a)->id, (*b)->id); |
| 1544 |
} |
| 1545 |
|
| 1546 |
static size_t sort_job_list(Job **list, size_t n) { |
| 1547 |
Job *previous = NULL; |
| 1548 |
size_t a, b; |
| 1549 |
|
| 1550 |
/* Order by numeric IDs */ |
| 1551 |
typesafe_qsort(list, n, job_compare_id); |
| 1552 |
|
| 1553 |
/* Filter out duplicates */ |
| 1554 |
for (a = 0, b = 0; a < n; a++) { |
| 1555 |
|
| 1556 |
if (previous == list[a]) |
| 1557 |
continue; |
| 1558 |
|
| 1559 |
previous = list[b++] = list[a]; |
| 1560 |
} |
| 1561 |
|
| 1562 |
return b; |
| 1563 |
} |
| 1564 |
|
| 1565 |
int job_get_before(Job *j, Job*** ret) { |
| 1566 |
_cleanup_free_ Job** list = NULL; |
| 1567 |
Unit *other = NULL; |
| 1568 |
size_t n = 0; |
| 1569 |
|
| 1570 |
/* Returns a list of all pending jobs that need to finish before this job may be started. */ |
| 1571 |
|
| 1572 |
assert(j); |
| 1573 |
assert(ret); |
| 1574 |
|
| 1575 |
if (j->ignore_order) { |
| 1576 |
*ret = NULL; |
| 1577 |
return 0; |
| 1578 |
} |
| 1579 |
|
| 1580 |
UNIT_FOREACH_DEPENDENCY(other, j->unit, UNIT_ATOM_AFTER) { |
| 1581 |
if (!other->job) |
| 1582 |
continue; |
| 1583 |
if (job_compare(j, other->job, UNIT_ATOM_AFTER) <= 0) |
| 1584 |
continue; |
| 1585 |
|
| 1586 |
if (!GREEDY_REALLOC(list, n+1)) |
| 1587 |
return -ENOMEM; |
| 1588 |
list[n++] = other->job; |
| 1589 |
} |
| 1590 |
|
| 1591 |
UNIT_FOREACH_DEPENDENCY(other, j->unit, UNIT_ATOM_BEFORE) { |
| 1592 |
if (!other->job) |
| 1593 |
continue; |
| 1594 |
if (job_compare(j, other->job, UNIT_ATOM_BEFORE) <= 0) |
| 1595 |
continue; |
| 1596 |
|
| 1597 |
if (!GREEDY_REALLOC(list, n+1)) |
| 1598 |
return -ENOMEM; |
| 1599 |
list[n++] = other->job; |
| 1600 |
} |
| 1601 |
|
| 1602 |
n = sort_job_list(list, n); |
| 1603 |
|
| 1604 |
*ret = TAKE_PTR(list); |
| 1605 |
|
| 1606 |
return (int) n; |
| 1607 |
} |
| 1608 |
|
| 1609 |
int job_get_after(Job *j, Job*** ret) { |
| 1610 |
_cleanup_free_ Job** list = NULL; |
| 1611 |
Unit *other = NULL; |
| 1612 |
size_t n = 0; |
| 1613 |
|
| 1614 |
assert(j); |
| 1615 |
assert(ret); |
| 1616 |
|
| 1617 |
/* Returns a list of all pending jobs that are waiting for this job to finish. */ |
| 1618 |
|
| 1619 |
UNIT_FOREACH_DEPENDENCY(other, j->unit, UNIT_ATOM_BEFORE) { |
| 1620 |
if (!other->job) |
| 1621 |
continue; |
| 1622 |
|
| 1623 |
if (other->job->ignore_order) |
| 1624 |
continue; |
| 1625 |
|
| 1626 |
if (job_compare(j, other->job, UNIT_ATOM_BEFORE) >= 0) |
| 1627 |
continue; |
| 1628 |
|
| 1629 |
if (!GREEDY_REALLOC(list, n+1)) |
| 1630 |
return -ENOMEM; |
| 1631 |
list[n++] = other->job; |
| 1632 |
} |
| 1633 |
|
| 1634 |
UNIT_FOREACH_DEPENDENCY(other, j->unit, UNIT_ATOM_AFTER) { |
| 1635 |
if (!other->job) |
| 1636 |
continue; |
| 1637 |
|
| 1638 |
if (other->job->ignore_order) |
| 1639 |
continue; |
| 1640 |
|
| 1641 |
if (job_compare(j, other->job, UNIT_ATOM_AFTER) >= 0) |
| 1642 |
continue; |
| 1643 |
|
| 1644 |
if (!GREEDY_REALLOC(list, n+1)) |
| 1645 |
return -ENOMEM; |
| 1646 |
list[n++] = other->job; |
| 1647 |
} |
| 1648 |
|
| 1649 |
n = sort_job_list(list, n); |
| 1650 |
|
| 1651 |
*ret = TAKE_PTR(list); |
| 1652 |
|
| 1653 |
return (int) n; |
| 1654 |
} |
| 1655 |
|
| 1656 |
static const char* const job_state_table[_JOB_STATE_MAX] = { |
| 1657 |
[JOB_WAITING] = "waiting", |
| 1658 |
[JOB_RUNNING] = "running", |
| 1659 |
[JOB_FINISHED] = "finished", |
| 1660 |
}; |
| 1661 |
|
| 1662 |
DEFINE_STRING_TABLE_LOOKUP(job_state, JobState); |
| 1663 |
|
| 1664 |
static const char* const job_type_table[_JOB_TYPE_MAX] = { |
| 1665 |
[JOB_START] = "start", |
| 1666 |
[JOB_VERIFY_ACTIVE] = "verify-active", |
| 1667 |
[JOB_STOP] = "stop", |
| 1668 |
[JOB_RELOAD] = "reload", |
| 1669 |
[JOB_RELOAD_OR_START] = "reload-or-start", |
| 1670 |
[JOB_RESTART] = "restart", |
| 1671 |
[JOB_TRY_RESTART] = "try-restart", |
| 1672 |
[JOB_TRY_RELOAD] = "try-reload", |
| 1673 |
[JOB_NOP] = "nop", |
| 1674 |
}; |
| 1675 |
|
| 1676 |
DEFINE_STRING_TABLE_LOOKUP(job_type, JobType); |
| 1677 |
|
| 1678 |
static const char* const job_result_table[_JOB_RESULT_MAX] = { |
| 1679 |
[JOB_DONE] = "done", |
| 1680 |
[JOB_CANCELED] = "canceled", |
| 1681 |
[JOB_TIMEOUT] = "timeout", |
| 1682 |
[JOB_FAILED] = "failed", |
| 1683 |
[JOB_DEPENDENCY] = "dependency", |
| 1684 |
[JOB_SKIPPED] = "skipped", |
| 1685 |
[JOB_INVALID] = "invalid", |
| 1686 |
[JOB_ASSERT] = "assert", |
| 1687 |
[JOB_UNSUPPORTED] = "unsupported", |
| 1688 |
[JOB_COLLECTED] = "collected", |
| 1689 |
[JOB_ONCE] = "once", |
| 1690 |
[JOB_FROZEN] = "frozen", |
| 1691 |
[JOB_CONCURRENCY] = "concurrency", |
| 1692 |
}; |
| 1693 |
|
| 1694 |
DEFINE_STRING_TABLE_LOOKUP(job_result, JobResult); |
| 1695 |
|
| 1696 |
const char* job_type_to_access_method(JobType t) { |
| 1697 |
assert(t >= 0); |
| 1698 |
assert(t < _JOB_TYPE_MAX); |
| 1699 |
|
| 1700 |
if (IN_SET(t, JOB_START, JOB_RESTART, JOB_TRY_RESTART)) |
| 1701 |
return "start"; |
| 1702 |
else if (t == JOB_STOP) |
| 1703 |
return "stop"; |
| 1704 |
else |
| 1705 |
return "reload"; |
| 1706 |
} |
| 1707 |
|
| 1708 |
/* |
| 1709 |
* assume_dep assumed dependency between units (a is before/after b) |
| 1710 |
* |
| 1711 |
* Returns |
| 1712 |
* 0 jobs are independent, |
| 1713 |
* >0 a should run after b, |
| 1714 |
* <0 a should run before b, |
| 1715 |
* |
| 1716 |
* The logic means that for a service a and a service b where b.After=a: |
| 1717 |
* |
| 1718 |
* start a + start b → 1st step start a, 2nd step start b |
| 1719 |
* start a + stop b → 1st step stop b, 2nd step start a |
| 1720 |
* stop a + start b → 1st step stop a, 2nd step start b |
| 1721 |
* stop a + stop b → 1st step stop b, 2nd step stop a |
| 1722 |
* |
| 1723 |
* This has the side effect that restarts are properly synchronized too. |
| 1724 |
*/ |
| 1725 |
int job_compare(Job *a, Job *b, UnitDependencyAtom assume_dep) { |
| 1726 |
assert(a); |
| 1727 |
assert(b); |
| 1728 |
assert(a->type < _JOB_TYPE_MAX_IN_TRANSACTION); |
| 1729 |
assert(b->type < _JOB_TYPE_MAX_IN_TRANSACTION); |
| 1730 |
assert(IN_SET(assume_dep, UNIT_ATOM_AFTER, UNIT_ATOM_BEFORE)); |
| 1731 |
|
| 1732 |
/* Trivial cases first */ |
| 1733 |
if (a->type == JOB_NOP || b->type == JOB_NOP) |
| 1734 |
return 0; |
| 1735 |
|
| 1736 |
if (a->ignore_order || b->ignore_order) |
| 1737 |
return 0; |
| 1738 |
|
| 1739 |
if (assume_dep == UNIT_ATOM_AFTER) |
| 1740 |
return -job_compare(b, a, UNIT_ATOM_BEFORE); |
| 1741 |
|
| 1742 |
/* Let's make it simple, JOB_STOP goes always first (in case both ua and ub stop, then ub's stop goes |
| 1743 |
* first anyway). JOB_RESTART is JOB_STOP in disguise (before it is patched to JOB_START). */ |
| 1744 |
if (IN_SET(b->type, JOB_STOP, JOB_RESTART)) |
| 1745 |
return 1; |
| 1746 |
else |
| 1747 |
return -1; |
| 1748 |
} |
| 1749 |
|
| 1750 |
void job_set_activation_details(Job *j, ActivationDetails *info) { |
| 1751 |
/* Existing (older) ActivationDetails win, newer ones are discarded. */ |
| 1752 |
if (!j || j->activation_details || !info) |
| 1753 |
return; /* Nothing to do. */ |
| 1754 |
|
| 1755 |
j->activation_details = activation_details_ref(info); |
| 1756 |
} |