jail: fix backfilling the "name" for jid-named jails

Using the cfparam variant of add_param() will actually copy the name and
flags from the passed-in param, which I hadn't considered.  We actually
want the name/flags from the "name" param so that we can do variable
expansion against it right after that -- otherwise it cannot be found,
since variable expansion actually searches by name.

While we're here, `jls -e` was the intermediate name for `jls -c` that
never saw the light of the day.  Fix our existence test.

Reviewed by:	jamie
Fixes:	02944d8c49 ("jail: consistently populate the KP_JID [...]")
Differential Revision:	https://reviews.freebsd.org/D51831
This commit is contained in:
Kyle Evans 2025-08-16 12:39:47 -05:00
parent 20f0996700
commit b81fd3fc8b
2 changed files with 15 additions and 2 deletions

View file

@ -189,7 +189,7 @@ load_config(const char *cfname)
* jail is created or found.
*/
if (j->intparams[KP_NAME] == NULL)
add_param(j, j->intparams[KP_JID], KP_NAME, NULL);
add_param(j, NULL, KP_NAME, j->name);
/* Resolve any variable substitutions. */
pgen = 0;

View file

@ -198,7 +198,7 @@ clean_jails()
fi
while read jail; do
if jls -e -j "$jail"; then
if jls -c -j "$jail"; then
jail -r "$jail"
fi
done < jails.lst
@ -211,10 +211,23 @@ jid_name_set_body()
echo "basejail" >> jails.lst
echo "$jid { name = basejail; persist; }" > jail.conf
atf_check -o match:"$jid: created" jail -f jail.conf -c "$jid"
# Confirm that we didn't override the explicitly-set name with the jid
# as the name.
atf_check -o match:"basejail" jls -j "$jid" name
atf_check -o match:"$jid: removed" jail -f jail.conf -r "$jid"
echo "$jid { host.hostname = \"\${name}\"; persist; }" > jail.conf
atf_check -o match:"$jid: created" jail -f jail.conf -c "$jid"
# Confirm that ${name} expanded and expanded correctly to the
# jid-implied name.
atf_check -o match:"$jid" jls -j "$jid" host.hostname
atf_check -o match:"$jid: removed" jail -f jail.conf -r "$jid"
echo "basejail { jid = $jid; persist; }" > jail.conf
atf_check -o match:"basejail: created" jail -f jail.conf -c basejail
# Confirm that our jid assigment in the definition worked out and we
# did in-fact create the jail there.
atf_check -o match:"$jid" jls -j "basejail" jid
atf_check -o match:"basejail: removed" jail -f jail.conf -r basejail
}