1fc0b0055Sopenharmony_ci/* 2fc0b0055Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3fc0b0055Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fc0b0055Sopenharmony_ci * you may not use this file except in compliance with the License. 5fc0b0055Sopenharmony_ci * You may obtain a copy of the License at 6fc0b0055Sopenharmony_ci * 7fc0b0055Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fc0b0055Sopenharmony_ci * 9fc0b0055Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fc0b0055Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fc0b0055Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fc0b0055Sopenharmony_ci * See the License for the specific language governing permissions and 13fc0b0055Sopenharmony_ci * limitations under the License. 14fc0b0055Sopenharmony_ci */ 15fc0b0055Sopenharmony_ci 16fc0b0055Sopenharmony_ci#include "nativetoken_klog.h" 17fc0b0055Sopenharmony_ci#include <fcntl.h> 18fc0b0055Sopenharmony_ci#include <unistd.h> 19fc0b0055Sopenharmony_ci#include "securec.h" 20fc0b0055Sopenharmony_ci 21fc0b0055Sopenharmony_ci#define MAX_LOG_SIZE 1024 22fc0b0055Sopenharmony_ci#define MAX_LEVEL_SIZE 3 23fc0b0055Sopenharmony_ci 24fc0b0055Sopenharmony_cistatic const char *LOG_LEVEL_STR[] = {"ERROR", "WARNING", "INFO"}; 25fc0b0055Sopenharmony_ci 26fc0b0055Sopenharmony_ci#ifndef UNLIKELY 27fc0b0055Sopenharmony_ci#define UNLIKELY(x) __builtin_expect(!!(x), 0) 28fc0b0055Sopenharmony_ci#endif 29fc0b0055Sopenharmony_ci 30fc0b0055Sopenharmony_cistatic int g_fd = -1; 31fc0b0055Sopenharmony_cistatic void NativeTokenOpenLogDevice(void) 32fc0b0055Sopenharmony_ci{ 33fc0b0055Sopenharmony_ci int fd = open("/dev/kmsg", O_WRONLY | O_CLOEXEC); 34fc0b0055Sopenharmony_ci if (fd >= 0) { 35fc0b0055Sopenharmony_ci g_fd = fd; 36fc0b0055Sopenharmony_ci } 37fc0b0055Sopenharmony_ci return; 38fc0b0055Sopenharmony_ci} 39fc0b0055Sopenharmony_ci 40fc0b0055Sopenharmony_ciint NativeTokenKmsg(int logLevel, const char *fmt, ...) 41fc0b0055Sopenharmony_ci{ 42fc0b0055Sopenharmony_ci if ((logLevel < 0) || (logLevel >= MAX_LEVEL_SIZE)) { 43fc0b0055Sopenharmony_ci return -1; 44fc0b0055Sopenharmony_ci } 45fc0b0055Sopenharmony_ci if (UNLIKELY(g_fd < 0)) { 46fc0b0055Sopenharmony_ci NativeTokenOpenLogDevice(); 47fc0b0055Sopenharmony_ci if (g_fd < 0) { 48fc0b0055Sopenharmony_ci return -1; 49fc0b0055Sopenharmony_ci } 50fc0b0055Sopenharmony_ci } 51fc0b0055Sopenharmony_ci va_list vargs; 52fc0b0055Sopenharmony_ci va_start(vargs, fmt); 53fc0b0055Sopenharmony_ci char tmpFmt[MAX_LOG_SIZE]; 54fc0b0055Sopenharmony_ci if (vsnprintf_s(tmpFmt, MAX_LOG_SIZE, MAX_LOG_SIZE - 1, fmt, vargs) == -1) { 55fc0b0055Sopenharmony_ci close(g_fd); 56fc0b0055Sopenharmony_ci g_fd = -1; 57fc0b0055Sopenharmony_ci va_end(vargs); 58fc0b0055Sopenharmony_ci return -1; 59fc0b0055Sopenharmony_ci } 60fc0b0055Sopenharmony_ci 61fc0b0055Sopenharmony_ci char logInfo[MAX_LOG_SIZE]; 62fc0b0055Sopenharmony_ci int res = snprintf_s(logInfo, MAX_LOG_SIZE, MAX_LOG_SIZE - 1, "[pid=%d][%s][%s] %s", 63fc0b0055Sopenharmony_ci getpid(), "access_token", LOG_LEVEL_STR[logLevel], tmpFmt); 64fc0b0055Sopenharmony_ci if (res == -1) { 65fc0b0055Sopenharmony_ci close(g_fd); 66fc0b0055Sopenharmony_ci g_fd = -1; 67fc0b0055Sopenharmony_ci va_end(vargs); 68fc0b0055Sopenharmony_ci return -1; 69fc0b0055Sopenharmony_ci } 70fc0b0055Sopenharmony_ci va_end(vargs); 71fc0b0055Sopenharmony_ci 72fc0b0055Sopenharmony_ci if (write(g_fd, logInfo, strlen(logInfo)) < 0) { 73fc0b0055Sopenharmony_ci close(g_fd); 74fc0b0055Sopenharmony_ci g_fd = -1; 75fc0b0055Sopenharmony_ci } 76fc0b0055Sopenharmony_ci return 0; 77fc0b0055Sopenharmony_ci} 78