From 141c6a886c47a043ee6d1572ec634e2f79a47089 Mon Sep 17 00:00:00 2001 From: Eric Biggers Date: Fri, 11 Aug 2023 21:05:14 +0000 Subject: [PATCH] Avoid two SELinux related error messages at boot time It is expected that /metadata/ota/rollback-indicator and /metadata/gsi don't always exist, so don't call selinux_android_restorecon() on them when they don't exist. This eliminates the following error messages: 0 0 E selinux : SELinux: Could not get canonical path for /metadata/ota/rollback-indicator restorecon: No such file or directory. 0 0 E selinux : SELinux: Could not stat /metadata/gsi: No such file or directory. Test: Booted Cuttlefish and verified the error messages are gone Change-Id: I94c998556c85adde5f11f134178219ba7880c2be --- init/selinux.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/init/selinux.cpp b/init/selinux.cpp index ebdcaa62d..ac102eb4e 100644 --- a/init/selinux.cpp +++ b/init/selinux.cpp @@ -735,6 +735,14 @@ void SelinuxAvcLog(char* buf) { TEMP_FAILURE_RETRY(send(fd.get(), &request, sizeof(request), 0)); } +int RestoreconIfExists(const char* path, unsigned int flags) { + if (access(path, F_OK) != 0 && errno == ENOENT) { + // Avoid error message for path that is expected to not always exist. + return 0; + } + return selinux_android_restorecon(path, flags); +} + } // namespace void SelinuxRestoreContext() { @@ -762,9 +770,9 @@ void SelinuxRestoreContext() { // adb remount, snapshot-based updates, and DSUs all create files during // first-stage init. - selinux_android_restorecon(SnapshotManager::GetGlobalRollbackIndicatorPath().c_str(), 0); - selinux_android_restorecon("/metadata/gsi", SELINUX_ANDROID_RESTORECON_RECURSE | - SELINUX_ANDROID_RESTORECON_SKIP_SEHASH); + RestoreconIfExists(SnapshotManager::GetGlobalRollbackIndicatorPath().c_str(), 0); + RestoreconIfExists("/metadata/gsi", + SELINUX_ANDROID_RESTORECON_RECURSE | SELINUX_ANDROID_RESTORECON_SKIP_SEHASH); } int SelinuxKlogCallback(int type, const char* fmt, ...) {