aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThiƩbaud Weksteen <tweek@google.com>2023-04-03 16:43:06 +1000
committerThiƩbaud Weksteen <tweek@google.com>2023-04-04 10:26:19 +1000
commit366f01fd644f6a622c57974dc0e5d8e8fe0efa84 (patch)
tree413ffa3254b94c6df688b8d850dcab978f05008d
parent1b0711d5d821a67d1dc780a363fc912a261f74bb (diff)
downloadselinux-366f01fd644f6a622c57974dc0e5d8e8fe0efa84.tar.gz
Skip newlines for SELinux logsandroid-u-beta-2-gplandroid-u-beta-1-gpl
libselinux log messages usually end with a new line character. Android log system does not require the new line character and will include the character as-is in the log buffer. selinux_log_callback and selinux_vendor_log_callback implementations are merged as they provide similar functionalities. Match the indentation (i.e., tabs) with the rest of the file. Test: boot & inspect logcat Change-Id: I0a5e53b8f048c65f29c5df3bd7e0b38f523e42cd
-rw-r--r--libselinux/src/android/android.c92
1 files changed, 45 insertions, 47 deletions
diff --git a/libselinux/src/android/android.c b/libselinux/src/android/android.c
index c883c7aa..ab0a15f8 100644
--- a/libselinux/src/android/android.c
+++ b/libselinux/src/android/android.c
@@ -176,57 +176,55 @@ struct selabel_handle* selinux_android_keystore2_key_context_handle(void)
return context_handle(SELABEL_CTX_ANDROID_KEYSTORE2_KEY, &keystore2_context_paths, "keystore2");
}
+static void __selinux_log_callback(bool add_to_event_log, int type, const char *fmt, va_list ap) {
+ int priority;
+ char *strp;
+
+ switch(type) {
+ case SELINUX_WARNING:
+ priority = ANDROID_LOG_WARN;
+ break;
+ case SELINUX_INFO:
+ priority = ANDROID_LOG_INFO;
+ break;
+ default:
+ priority = ANDROID_LOG_ERROR;
+ break;
+ }
+
+ int len = vasprintf(&strp, fmt, ap);
+ if (len < 0) {
+ return;
+ }
+
+ /* libselinux log messages usually contain a new line character, while
+ * Android LOG() does not expect it. Remove it to avoid empty lines in
+ * the log buffers.
+ */
+ if (len > 0 && strp[len - 1] == '\n') {
+ strp[len - 1] = '\0';
+ }
+ LOG_PRI(priority, "SELinux", "%s", strp);
+ if (add_to_event_log) {
+ LOG_EVENT_STRING(AUDITD_LOG_TAG, strp);
+ }
+ free(strp);
+}
+
int selinux_log_callback(int type, const char *fmt, ...)
{
- va_list ap;
- int priority;
- char *strp;
-
- switch(type) {
- case SELINUX_WARNING:
- priority = ANDROID_LOG_WARN;
- break;
- case SELINUX_INFO:
- priority = ANDROID_LOG_INFO;
- break;
- default:
- priority = ANDROID_LOG_ERROR;
- break;
- }
-
- va_start(ap, fmt);
- if (vasprintf(&strp, fmt, ap) != -1) {
- LOG_PRI(priority, "SELinux", "%s", strp);
- LOG_EVENT_STRING(AUDITD_LOG_TAG, strp);
- free(strp);
- }
- va_end(ap);
- return 0;
+ va_list ap;
+ va_start(ap, fmt);
+ __selinux_log_callback(true, type, fmt, ap);
+ va_end(ap);
+ return 0;
}
int selinux_vendor_log_callback(int type, const char *fmt, ...)
{
- va_list ap;
- int priority;
- char *strp;
-
- switch(type) {
- case SELINUX_WARNING:
- priority = ANDROID_LOG_WARN;
- break;
- case SELINUX_INFO:
- priority = ANDROID_LOG_INFO;
- break;
- default:
- priority = ANDROID_LOG_ERROR;
- break;
- }
-
- va_start(ap, fmt);
- if (vasprintf(&strp, fmt, ap) != -1) {
- LOG_PRI(priority, "SELinux", "%s", strp);
- free(strp);
- }
- va_end(ap);
- return 0;
+ va_list ap;
+ va_start(ap, fmt);
+ __selinux_log_callback(false, type, fmt, ap);
+ va_end(ap);
+ return 0;
}