chcon: print the entire file name in errors setting the context

Previously, only the basenames were printed, leading to ambiguity when
multiple directories had a file with the same name. E.g., here is the
old output:

    $ mkdir -p a/{b,c}
    $ touch a/{b,c}/d
    $ strace -o /dev/null -e inject=lsetxattr:error=EIO \
        chcon -R -u root a
    chcon: failed to change context of 'd' to '...': ...
    chcon: failed to change context of 'b' to '...': ...
    chcon: failed to change context of 'd' to '...': ...
    chcon: failed to change context of 'c' to '...': ...
    chcon: failed to change context of 'a' to '...': ...

Here is the new output:

    $ strace -o /dev/null -e inject=lsetxattr:error=EIO \
        src/chcon -R -u root a
    chcon: failed to change context of 'a/b/d' to '...': ...
    chcon: failed to change context of 'a/b' to '...': ...
    chcon: failed to change context of 'a/c/d' to '...': ...
    chcon: failed to change context of 'a/c' to '...': ...
    chcon: failed to change context of 'a' to '...': ...

This also aligns the output with other programs, e.g., 'chown':

    $ strace -o /dev/null -e inject=chown,fchown,fchownat:error=EIO \
        chown -R root a
    chown: changing ownership of 'a/b/d': ...
    chown: changing ownership of 'a/b': ...
    chown: changing ownership of 'a/c/d': ...
    chown: changing ownership of 'a/c': ...
    chown: changing ownership of 'a': ...

* src/chcon.c (change_file_context): Add an argument for the full file
name. Use it in error messages.
(process_file): Pass the full file name from the FTSENT.
* NEWS: Mention the bug fix.
This commit is contained in:
Collin Funk committed 2026-10-30 15:58:04 -07:00
1 parent 977b071eaf
commit e10de8ddd0
2 files changed
+12 -5

No files matched your search

+4
View File
@@ -7,6 +7,10 @@ GNU coreutils NEWS -*- outline -*-
'basenc --base32hex' no longer decodes BASE32 only characters "WXYZ".
[bug introduced with the basenc program in coreutils-8.31]
'chcon' now emits the entire file name in error messages when setting the
security context fails instead of the basename.
[bug introduced with the chcon program in coreutils-6.9.90]
'chroot --userspec=USER' now uses the primary group of USER as looked up
inside the chroot. Previously the group looked up outside the chroot
was used, even when the look-up inside the chroot succeeded.
+8 -5
View File
@@ -138,7 +138,8 @@ compute_context_from_mask (char const *context, context_t *ret)
Return 0 if successful, 1 if errors occurred. */
static int
change_file_context (int fd, char const *file, bool ignore_missing)
change_file_context (char const *file_full_name, int fd, char const *file,
bool ignore_missing)
{
char *file_context = NULL;
context_t context IF_LINT (= NULL);
@@ -156,7 +157,7 @@ change_file_context (int fd, char const *file, bool ignore_missing)
if (ignore_missing && ignorable_traversal_errno (errno))
return 0;
error (0, errno, _("failed to get security context of %s"),
quoteaf (file));
quoteaf (file_full_name));
return 1;
}
@@ -166,7 +167,7 @@ change_file_context (int fd, char const *file, bool ignore_missing)
if (file_context == NULL)
{
error (0, 0, _("can't apply partial context to unlabeled file %s"),
quoteaf (file));
quoteaf (file_full_name));
return 1;
}
@@ -195,7 +196,8 @@ change_file_context (int fd, char const *file, bool ignore_missing)
{
errors = 1;
error (0, errno, _("failed to change context of %s to %s"),
quoteaf_n (0, file), quote_n (1, context_string));
quoteaf_n (0, file_full_name),
quote_n (1, context_string));
}
}
}
@@ -305,7 +307,8 @@ process_file (FTS *fts, FTSENT *ent)
printf (_("changing security context of %s\n"),
quoteaf (file_full_name));
if (change_file_context (fts->fts_cwd_fd, file, ignore_missing) != 0)
if (change_file_context (file_full_name, fts->fts_cwd_fd, file,
ignore_missing) != 0)
ok = false;
}