| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "unit-def.h" |
| 4 |
#include "unit-dependency-atom.h" |
| 5 |
|
| 6 |
static const UnitDependencyAtom atom_map[_UNIT_DEPENDENCY_MAX] = { |
| 7 |
/* A table that maps high-level dependency types to low-level dependency "atoms". The latter actually |
| 8 |
* describe specific facets of dependency behaviour. The former combine them into one user-facing |
| 9 |
* concept. Atoms are a bit mask, though a bunch of dependency types have only a single bit set. |
| 10 |
* |
| 11 |
* Typically when the user configures a dependency they go via dependency type, but when we act on |
| 12 |
* them we go by atom. |
| 13 |
* |
| 14 |
* NB: when you add a new dependency type here, make sure to also add one to the (best-effort) |
| 15 |
* reverse table in unit_dependency_from_unique_atom() further down. */ |
| 16 |
|
| 17 |
[UNIT_REQUIRES] = UNIT_ATOM_PULL_IN_START | |
| 18 |
UNIT_ATOM_RETROACTIVE_START_REPLACE | |
| 19 |
UNIT_ATOM_ADD_STOP_WHEN_UNNEEDED_QUEUE | |
| 20 |
UNIT_ATOM_ADD_DEFAULT_TARGET_DEPENDENCY_QUEUE, |
| 21 |
|
| 22 |
[UNIT_REQUISITE] = UNIT_ATOM_PULL_IN_VERIFY | |
| 23 |
UNIT_ATOM_ADD_STOP_WHEN_UNNEEDED_QUEUE | |
| 24 |
UNIT_ATOM_ADD_DEFAULT_TARGET_DEPENDENCY_QUEUE, |
| 25 |
|
| 26 |
[UNIT_WANTS] = UNIT_ATOM_PULL_IN_START_IGNORED | |
| 27 |
UNIT_ATOM_RETROACTIVE_START_FAIL | |
| 28 |
UNIT_ATOM_ADD_STOP_WHEN_UNNEEDED_QUEUE | |
| 29 |
UNIT_ATOM_ADD_DEFAULT_TARGET_DEPENDENCY_QUEUE, |
| 30 |
|
| 31 |
[UNIT_BINDS_TO] = UNIT_ATOM_PULL_IN_START | |
| 32 |
UNIT_ATOM_RETROACTIVE_START_REPLACE | |
| 33 |
UNIT_ATOM_CANNOT_BE_ACTIVE_WITHOUT | |
| 34 |
UNIT_ATOM_ADD_STOP_WHEN_UNNEEDED_QUEUE | |
| 35 |
UNIT_ATOM_ADD_DEFAULT_TARGET_DEPENDENCY_QUEUE, |
| 36 |
|
| 37 |
[UNIT_UPHOLDS] = UNIT_ATOM_PULL_IN_START_IGNORED | |
| 38 |
UNIT_ATOM_RETROACTIVE_START_REPLACE | |
| 39 |
UNIT_ATOM_ADD_START_WHEN_UPHELD_QUEUE | |
| 40 |
UNIT_ATOM_ADD_STOP_WHEN_UNNEEDED_QUEUE | |
| 41 |
UNIT_ATOM_ADD_DEFAULT_TARGET_DEPENDENCY_QUEUE, |
| 42 |
|
| 43 |
[UNIT_REQUIRED_BY] = UNIT_ATOM_PROPAGATE_STOP | |
| 44 |
UNIT_ATOM_PROPAGATE_START_FAILURE | |
| 45 |
UNIT_ATOM_PINS_STOP_WHEN_UNNEEDED | |
| 46 |
UNIT_ATOM_DEFAULT_TARGET_DEPENDENCIES, |
| 47 |
|
| 48 |
[UNIT_REQUISITE_OF] = UNIT_ATOM_PROPAGATE_STOP | |
| 49 |
UNIT_ATOM_PROPAGATE_START_FAILURE | |
| 50 |
UNIT_ATOM_PROPAGATE_INACTIVE_START_AS_FAILURE | |
| 51 |
UNIT_ATOM_PINS_STOP_WHEN_UNNEEDED | |
| 52 |
UNIT_ATOM_DEFAULT_TARGET_DEPENDENCIES, |
| 53 |
|
| 54 |
[UNIT_WANTED_BY] = UNIT_ATOM_DEFAULT_TARGET_DEPENDENCIES | |
| 55 |
UNIT_ATOM_PINS_STOP_WHEN_UNNEEDED, |
| 56 |
|
| 57 |
[UNIT_BOUND_BY] = UNIT_ATOM_RETROACTIVE_STOP_ON_STOP | |
| 58 |
UNIT_ATOM_PROPAGATE_STOP | |
| 59 |
UNIT_ATOM_PROPAGATE_START_FAILURE | |
| 60 |
UNIT_ATOM_PINS_STOP_WHEN_UNNEEDED | |
| 61 |
UNIT_ATOM_ADD_CANNOT_BE_ACTIVE_WITHOUT_QUEUE | |
| 62 |
UNIT_ATOM_DEFAULT_TARGET_DEPENDENCIES, |
| 63 |
|
| 64 |
[UNIT_UPHELD_BY] = UNIT_ATOM_START_STEADILY | |
| 65 |
UNIT_ATOM_DEFAULT_TARGET_DEPENDENCIES | |
| 66 |
UNIT_ATOM_PINS_STOP_WHEN_UNNEEDED, |
| 67 |
|
| 68 |
[UNIT_CONFLICTS] = UNIT_ATOM_PULL_IN_STOP | |
| 69 |
UNIT_ATOM_RETROACTIVE_STOP_ON_START, |
| 70 |
|
| 71 |
[UNIT_CONFLICTED_BY] = UNIT_ATOM_PULL_IN_STOP_IGNORED | |
| 72 |
UNIT_ATOM_RETROACTIVE_STOP_ON_START | |
| 73 |
UNIT_ATOM_PROPAGATE_STOP_FAILURE, |
| 74 |
|
| 75 |
[UNIT_PROPAGATES_STOP_TO] = UNIT_ATOM_RETROACTIVE_STOP_ON_STOP | |
| 76 |
UNIT_ATOM_PROPAGATE_STOP_GRACEFUL, |
| 77 |
|
| 78 |
/* These are simple dependency types: they consist of a single atom only */ |
| 79 |
[UNIT_ON_FAILURE] = UNIT_ATOM_ON_FAILURE, |
| 80 |
[UNIT_ON_SUCCESS] = UNIT_ATOM_ON_SUCCESS, |
| 81 |
[UNIT_ON_FAILURE_OF] = UNIT_ATOM_ON_FAILURE_OF, |
| 82 |
[UNIT_ON_SUCCESS_OF] = UNIT_ATOM_ON_SUCCESS_OF, |
| 83 |
[UNIT_BEFORE] = UNIT_ATOM_BEFORE, |
| 84 |
[UNIT_AFTER] = UNIT_ATOM_AFTER, |
| 85 |
[UNIT_PART_OF] = UNIT_ATOM_ADD_DEFAULT_TARGET_DEPENDENCY_QUEUE, |
| 86 |
[UNIT_CONSISTS_OF] = UNIT_ATOM_PROPAGATE_STOP, |
| 87 |
[UNIT_TRIGGERS] = UNIT_ATOM_TRIGGERS, |
| 88 |
[UNIT_TRIGGERED_BY] = UNIT_ATOM_TRIGGERED_BY, |
| 89 |
[UNIT_PROPAGATES_RELOAD_TO] = UNIT_ATOM_PROPAGATES_RELOAD_TO, |
| 90 |
[UNIT_JOINS_NAMESPACE_OF] = UNIT_ATOM_JOINS_NAMESPACE_OF, |
| 91 |
[UNIT_REFERENCES] = UNIT_ATOM_REFERENCES, |
| 92 |
[UNIT_REFERENCED_BY] = UNIT_ATOM_REFERENCED_BY, |
| 93 |
[UNIT_IN_SLICE] = UNIT_ATOM_IN_SLICE, |
| 94 |
[UNIT_SLICE_OF] = UNIT_ATOM_SLICE_OF, |
| 95 |
|
| 96 |
/* These are dependency types without effect on our state engine. We maintain them only to make |
| 97 |
* things discoverable/debuggable as they are the inverse dependencies to some of the above. As they |
| 98 |
* have no effect of their own, they all map to no atoms at all, i.e. the value 0. */ |
| 99 |
[UNIT_RELOAD_PROPAGATED_FROM] = 0, |
| 100 |
[UNIT_STOP_PROPAGATED_FROM] = 0, |
| 101 |
}; |
| 102 |
|
| 103 |
UnitDependencyAtom unit_dependency_to_atom(UnitDependency d) { |
| 104 |
if (d < 0) |
| 105 |
return _UNIT_DEPENDENCY_ATOM_INVALID; |
| 106 |
|
| 107 |
assert(d < _UNIT_DEPENDENCY_MAX); |
| 108 |
|
| 109 |
return atom_map[d]; |
| 110 |
} |
| 111 |
|
| 112 |
UnitDependency unit_dependency_from_unique_atom(UnitDependencyAtom atom) { |
| 113 |
|
| 114 |
/* This is a "best-effort" function that maps the specified 'atom' mask to a dependency type that is |
| 115 |
* is equal to or has a superset of bits set if that's uniquely possible. The idea is that this |
| 116 |
* function is used when iterating through deps that have a specific atom: if there's exactly one |
| 117 |
* dependency type of the specific atom we don't need iterate through all deps a unit has, but can |
| 118 |
* pinpoint things directly. |
| 119 |
* |
| 120 |
* This function will return _UNIT_DEPENDENCY_INVALID in case the specified value is not known or not |
| 121 |
* uniquely defined, i.e. there are multiple dependencies with the atom or the combination set. */ |
| 122 |
|
| 123 |
switch ((int64_t) atom) { |
| 124 |
|
| 125 |
/* Note that we can't list UNIT_REQUIRES here since it's a true subset of UNIT_BINDS_TO, and |
| 126 |
* hence its atom bits not uniquely mappable. */ |
| 127 |
|
| 128 |
case UNIT_ATOM_PULL_IN_VERIFY | |
| 129 |
UNIT_ATOM_ADD_STOP_WHEN_UNNEEDED_QUEUE | |
| 130 |
UNIT_ATOM_ADD_DEFAULT_TARGET_DEPENDENCY_QUEUE: |
| 131 |
case UNIT_ATOM_PULL_IN_VERIFY: /* a single dep type uses this atom */ |
| 132 |
return UNIT_REQUISITE; |
| 133 |
|
| 134 |
case UNIT_ATOM_PULL_IN_START_IGNORED | |
| 135 |
UNIT_ATOM_RETROACTIVE_START_FAIL | |
| 136 |
UNIT_ATOM_ADD_STOP_WHEN_UNNEEDED_QUEUE | |
| 137 |
UNIT_ATOM_ADD_DEFAULT_TARGET_DEPENDENCY_QUEUE: |
| 138 |
case UNIT_ATOM_RETROACTIVE_START_FAIL: |
| 139 |
return UNIT_WANTS; |
| 140 |
|
| 141 |
case UNIT_ATOM_PULL_IN_START | |
| 142 |
UNIT_ATOM_RETROACTIVE_START_REPLACE | |
| 143 |
UNIT_ATOM_CANNOT_BE_ACTIVE_WITHOUT | |
| 144 |
UNIT_ATOM_ADD_STOP_WHEN_UNNEEDED_QUEUE | |
| 145 |
UNIT_ATOM_ADD_DEFAULT_TARGET_DEPENDENCY_QUEUE: |
| 146 |
case UNIT_ATOM_CANNOT_BE_ACTIVE_WITHOUT: |
| 147 |
return UNIT_BINDS_TO; |
| 148 |
|
| 149 |
case UNIT_ATOM_PULL_IN_START_IGNORED | |
| 150 |
UNIT_ATOM_RETROACTIVE_START_REPLACE | |
| 151 |
UNIT_ATOM_ADD_START_WHEN_UPHELD_QUEUE | |
| 152 |
UNIT_ATOM_ADD_STOP_WHEN_UNNEEDED_QUEUE | |
| 153 |
UNIT_ATOM_ADD_DEFAULT_TARGET_DEPENDENCY_QUEUE: |
| 154 |
case UNIT_ATOM_ADD_START_WHEN_UPHELD_QUEUE: |
| 155 |
return UNIT_UPHOLDS; |
| 156 |
|
| 157 |
case UNIT_ATOM_PROPAGATE_STOP | |
| 158 |
UNIT_ATOM_PROPAGATE_START_FAILURE | |
| 159 |
UNIT_ATOM_PROPAGATE_INACTIVE_START_AS_FAILURE | |
| 160 |
UNIT_ATOM_PINS_STOP_WHEN_UNNEEDED | |
| 161 |
UNIT_ATOM_DEFAULT_TARGET_DEPENDENCIES: |
| 162 |
case UNIT_ATOM_PROPAGATE_INACTIVE_START_AS_FAILURE: |
| 163 |
return UNIT_REQUISITE_OF; |
| 164 |
|
| 165 |
case UNIT_ATOM_RETROACTIVE_STOP_ON_STOP | |
| 166 |
UNIT_ATOM_PROPAGATE_STOP | |
| 167 |
UNIT_ATOM_PROPAGATE_START_FAILURE | |
| 168 |
UNIT_ATOM_PINS_STOP_WHEN_UNNEEDED | |
| 169 |
UNIT_ATOM_ADD_CANNOT_BE_ACTIVE_WITHOUT_QUEUE | |
| 170 |
UNIT_ATOM_DEFAULT_TARGET_DEPENDENCIES: |
| 171 |
case UNIT_ATOM_ADD_CANNOT_BE_ACTIVE_WITHOUT_QUEUE: |
| 172 |
return UNIT_BOUND_BY; |
| 173 |
|
| 174 |
case UNIT_ATOM_START_STEADILY | |
| 175 |
UNIT_ATOM_DEFAULT_TARGET_DEPENDENCIES | |
| 176 |
UNIT_ATOM_PINS_STOP_WHEN_UNNEEDED: |
| 177 |
case UNIT_ATOM_START_STEADILY: |
| 178 |
return UNIT_UPHELD_BY; |
| 179 |
|
| 180 |
case UNIT_ATOM_PULL_IN_STOP | |
| 181 |
UNIT_ATOM_RETROACTIVE_STOP_ON_START: |
| 182 |
case UNIT_ATOM_PULL_IN_STOP: |
| 183 |
return UNIT_CONFLICTS; |
| 184 |
|
| 185 |
case UNIT_ATOM_PULL_IN_STOP_IGNORED | |
| 186 |
UNIT_ATOM_RETROACTIVE_STOP_ON_START | |
| 187 |
UNIT_ATOM_PROPAGATE_STOP_FAILURE: |
| 188 |
case UNIT_ATOM_PULL_IN_STOP_IGNORED: |
| 189 |
case UNIT_ATOM_PROPAGATE_STOP_FAILURE: |
| 190 |
return UNIT_CONFLICTED_BY; |
| 191 |
|
| 192 |
case UNIT_ATOM_RETROACTIVE_STOP_ON_STOP | |
| 193 |
UNIT_ATOM_PROPAGATE_STOP_GRACEFUL: |
| 194 |
case UNIT_ATOM_PROPAGATE_STOP_GRACEFUL: |
| 195 |
return UNIT_PROPAGATES_STOP_TO; |
| 196 |
|
| 197 |
/* And now, the simple ones */ |
| 198 |
|
| 199 |
case UNIT_ATOM_ON_FAILURE: |
| 200 |
return UNIT_ON_FAILURE; |
| 201 |
|
| 202 |
case UNIT_ATOM_ON_SUCCESS: |
| 203 |
return UNIT_ON_SUCCESS; |
| 204 |
|
| 205 |
case UNIT_ATOM_ON_SUCCESS_OF: |
| 206 |
return UNIT_ON_SUCCESS_OF; |
| 207 |
|
| 208 |
case UNIT_ATOM_ON_FAILURE_OF: |
| 209 |
return UNIT_ON_FAILURE_OF; |
| 210 |
|
| 211 |
case UNIT_ATOM_BEFORE: |
| 212 |
return UNIT_BEFORE; |
| 213 |
|
| 214 |
case UNIT_ATOM_AFTER: |
| 215 |
return UNIT_AFTER; |
| 216 |
|
| 217 |
case UNIT_ATOM_TRIGGERS: |
| 218 |
return UNIT_TRIGGERS; |
| 219 |
|
| 220 |
case UNIT_ATOM_TRIGGERED_BY: |
| 221 |
return UNIT_TRIGGERED_BY; |
| 222 |
|
| 223 |
case UNIT_ATOM_PROPAGATES_RELOAD_TO: |
| 224 |
return UNIT_PROPAGATES_RELOAD_TO; |
| 225 |
|
| 226 |
case UNIT_ATOM_JOINS_NAMESPACE_OF: |
| 227 |
return UNIT_JOINS_NAMESPACE_OF; |
| 228 |
|
| 229 |
case UNIT_ATOM_REFERENCES: |
| 230 |
return UNIT_REFERENCES; |
| 231 |
|
| 232 |
case UNIT_ATOM_REFERENCED_BY: |
| 233 |
return UNIT_REFERENCED_BY; |
| 234 |
|
| 235 |
case UNIT_ATOM_IN_SLICE: |
| 236 |
return UNIT_IN_SLICE; |
| 237 |
|
| 238 |
case UNIT_ATOM_SLICE_OF: |
| 239 |
return UNIT_SLICE_OF; |
| 240 |
|
| 241 |
default: |
| 242 |
return _UNIT_DEPENDENCY_INVALID; |
| 243 |
} |
| 244 |
} |