Skip to content

Commit e735bc4

Browse files
committed
unit: don't bother determining unit install state for transient or perpetual units
I noticed that we keep querying the preset database for transient units, which makes little sense, since transient units are well, transient, and hence not suject to enablement/disablement. Hence, let's shortcut things and simply not check the preset database for them. While we are at it, shortcut unit file state checks for transient units, too. We know they are transient already, we can return that directly, no need to go to disk. Finally, treat perpetual units like transient units for the the preset case: also bypass the preset database. (But keep checking for the unit file state for them, since it *is* relevant to know whether they were generated or not.)
1 parent c5855d9 commit e735bc4

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

src/core/unit.c

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4149,15 +4149,21 @@ UnitFileState unit_get_unit_file_state(Unit *u) {
41494149

41504150
assert(u);
41514151

4152-
if (u->unit_file_state < 0 && u->fragment_path) {
4153-
r = unit_file_get_state(
4154-
u->manager->runtime_scope,
4155-
NULL,
4156-
u->id,
4157-
&u->unit_file_state);
4158-
if (r < 0)
4159-
u->unit_file_state = UNIT_FILE_BAD;
4160-
}
4152+
if (u->unit_file_state >= 0 || !u->fragment_path)
4153+
return u->unit_file_state;
4154+
4155+
/* If we know this is a transient unit no need to ask the unit file state for details. Let's bypass
4156+
* the more expensive on-disk check. */
4157+
if (u->transient)
4158+
return (u->unit_file_state = UNIT_FILE_TRANSIENT);
4159+
4160+
r = unit_file_get_state(
4161+
u->manager->runtime_scope,
4162+
/* root_dir= */ NULL,
4163+
u->id,
4164+
&u->unit_file_state);
4165+
if (r < 0)
4166+
u->unit_file_state = UNIT_FILE_BAD;
41614167

41624168
return u->unit_file_state;
41634169
}
@@ -4167,24 +4173,26 @@ PresetAction unit_get_unit_file_preset(Unit *u) {
41674173

41684174
assert(u);
41694175

4170-
if (u->unit_file_preset < 0 && u->fragment_path) {
4171-
_cleanup_free_ char *bn = NULL;
4176+
if (u->unit_file_preset >= 0 || !u->fragment_path)
4177+
return u->unit_file_preset;
41724178

4173-
r = path_extract_filename(u->fragment_path, &bn);
4174-
if (r < 0)
4175-
return (u->unit_file_preset = r);
4179+
/* If this is a transient or perpetual unit file it doesn't make much sense to ask the preset
4180+
* database about this, because enabling/disabling makes no sense for either. Hence don't. */
4181+
if (u->transient || u->perpetual)
4182+
return (u->unit_file_preset = -ENOEXEC);
41764183

4177-
if (r == O_DIRECTORY)
4178-
return (u->unit_file_preset = -EISDIR);
4184+
_cleanup_free_ char *bn = NULL;
4185+
r = path_extract_filename(u->fragment_path, &bn);
4186+
if (r < 0)
4187+
return (u->unit_file_preset = r);
4188+
if (r == O_DIRECTORY)
4189+
return (u->unit_file_preset = -EISDIR);
41794190

4180-
u->unit_file_preset = unit_file_query_preset(
4191+
return (u->unit_file_preset = unit_file_query_preset(
41814192
u->manager->runtime_scope,
4182-
NULL,
4193+
/* root_dir= */ NULL,
41834194
bn,
4184-
NULL);
4185-
}
4186-
4187-
return u->unit_file_preset;
4195+
/* cache= */ NULL));
41884196
}
41894197

41904198
Unit* unit_ref_set(UnitRef *ref, Unit *source, Unit *target) {

0 commit comments

Comments
 (0)