From 2dfef21c0b7d4fef04ab0e65b3f8cc52944d7b44 Mon Sep 17 00:00:00 2001
From: licunlong <licunlong1@huawei.com>
Date: Thu, 29 Jun 2023 10:08:56 +0800
Subject: [PATCH] fix: use /var/log/sysmaster/sysmaster.log to save log, don't
 allow users change

---
 core/bin/main.rs            |  1 -
 core/bin/manager/config.rs  |  3 ---
 core/bin/plugin/mod.rs      |  5 +----
 core/bin/unit/manager.rs    |  5 -----
 core/lib/unit/umif.rs       |  3 +--
 libs/basic/Cargo.toml       |  1 +
 libs/basic/src/logger.rs    | 30 ++++++++----------------------
 libs/constants/src/lib.rs   |  3 +++
 units/system.toml           |  1 -
 11 files changed, 20 insertions(+), 49 deletions(-)

diff --git a/core/bin/main.rs b/core/bin/main.rs
index b75ef0b..c2010e2 100644
--- a/core/bin/main.rs
+++ b/core/bin/main.rs
@@ -86,7 +86,6 @@ fn main() -> Result<()> {
         "sysmaster",
         manager_config.LogLevel,
         &manager_config.LogTarget,
-        &manager_config.LogFile,
         manager_config.LogFileSize,
         manager_config.LogFileNumber,
     );
diff --git a/core/bin/manager/config.rs b/core/bin/manager/config.rs
index 691b692..229537f 100644
--- a/core/bin/manager/config.rs
+++ b/core/bin/manager/config.rs
@@ -28,8 +28,6 @@ pub struct ManagerConfig {
     pub LogLevel: log::LevelFilter,
     #[config(default = "syslog")]
     pub LogTarget: String,
-    #[config(default = "")]
-    pub LogFile: String,
     #[config(default = 10240)]
     pub LogFileSize: u32,
     #[config(default = 10)]
@@ -58,7 +56,6 @@ impl Default for ManagerConfig {
             DefaultTimeoutSec: 90,
             LogLevel: log::LevelFilter::Debug,
             LogTarget: "syslog".to_string(),
-            LogFile: String::new(),
             LogFileSize: 10240,
             LogFileNumber: 10,
             DbSize: RELI_HISTORY_MAPSIZE_DEFAULT,
diff --git a/core/bin/plugin/mod.rs b/core/bin/plugin/mod.rs
index 9a8e4f6..e6f5183 100644
--- a/core/bin/plugin/mod.rs
+++ b/core/bin/plugin/mod.rs
@@ -317,7 +317,6 @@ impl Plugin {
             um: Rc<dyn UmIf>,
             level: LevelFilter,
             target: &str,
-            file_path: &str,
             file_size: u32,
             file_number: u32,
         ) -> *mut dyn UnitManagerObj;
@@ -479,7 +478,6 @@ impl Plugin {
         &self,
         unit_type: UnitType,
         target: &str,
-        file: &str,
         file_size: u32,
         file_number: u32,
     ) -> Result<Box<dyn UnitManagerObj>> {
@@ -495,7 +493,6 @@ impl Plugin {
         type FnType = fn(
             level: LevelFilter,
             target: &str,
-            file: &str,
             file_size: u32,
             file_number: u32,
         ) -> *mut dyn UnitManagerObj;
@@ -516,7 +513,7 @@ impl Plugin {
             Ok(v) => v,
         };
 
-        let boxed_raw = fun(log::max_level(), target, file, file_size, file_number);
+        let boxed_raw = fun(log::max_level(), target, file_size, file_number);
         Ok(unsafe { Box::from_raw(boxed_raw) })
     }
 
diff --git a/core/bin/unit/manager.rs b/core/bin/unit/manager.rs
index 2e6caba..1e8d08f 100644
--- a/core/bin/unit/manager.rs
+++ b/core/bin/unit/manager.rs
@@ -1030,10 +1030,6 @@ impl UnitManager {
 
     fn remove_job_result(&self, _source: &str) {}
 
-    fn get_log_file(&self) -> &str {
-        &self.manager_config.LogFile
-    }
-
     fn get_log_target(&self) -> &str {
         &self.manager_config.LogTarget
     }
@@ -1314,7 +1310,6 @@ mod unit_submanager {
             let sub = match Plugin::get_instance().create_um_obj(
                 unit_type,
                 um.get_log_target(),
-                um.get_log_file(),
                 um.get_log_file_size(),
                 um.get_log_file_number(),
             ) {
diff --git a/core/lib/unit/umif.rs b/core/lib/unit/umif.rs
index 85603fa..f0019d0 100644
--- a/core/lib/unit/umif.rs
+++ b/core/lib/unit/umif.rs
@@ -230,11 +230,10 @@ macro_rules! declare_umobj_plugin {
         pub fn __um_obj_create(
             level: LevelFilter,
             target: &str,
-            file_path: &str,
             file_size: u32,
             file_number: u32,
         ) -> *mut dyn $crate::unit::UnitManagerObj {
-            logger::init_log_for_subum($name, level, target, file_path, file_size, file_number);
+            logger::init_log_for_subum($name, level, target, file_size, file_number);
             let construcotr: fn() -> $unit_type = $constructor;
             let obj = construcotr();
             let boxed: Box<dyn $crate::unit::UnitManagerObj> = Box::new(obj);
diff --git a/libs/basic/Cargo.toml b/libs/basic/Cargo.toml
index 6ee7162..b7d343c 100644
--- a/libs/basic/Cargo.toml
+++ b/libs/basic/Cargo.toml
@@ -22,6 +22,7 @@ bitflags = "1.3.2"
 pkg-config = "0.3"
 rand = "0.4.6"
 time = {version = "=0.3.10", features = ["formatting", "macros"] }
+constants = { path = "../constants"}
 
 [dev-dependencies]
 libtests = { path = "../libtests" }
diff --git a/libs/basic/src/logger.rs b/libs/basic/src/logger.rs
index 3161513..83fce0e 100644
--- a/libs/basic/src/logger.rs
+++ b/libs/basic/src/logger.rs
@@ -19,6 +19,7 @@ use std::{
     sync::Mutex,
 };
 
+use constants::LOG_FILE_PATH;
 use log::{LevelFilter, Log};
 use log4rs::{
     append::{
@@ -378,8 +379,6 @@ impl CombinedLogger {
 ///
 /// target: log target
 ///
-/// file_path: file path (valid when target == "file")
-///
 /// file_size: the maximum size of an active log file (valid when target == "file")
 ///
 /// file_number: the maximum number of rotated log files (valid when target == "file")
@@ -387,14 +386,13 @@ pub fn init_log_for_subum(
     app_name: &str,
     level: LevelFilter,
     target: &str,
-    file_path: &str,
     file_size: u32,
     file_number: u32,
 ) {
     /* We should avoid calling init_log here, or we will get many "attempted
      * to set a logger after the logging system was already initialized" error
      * message. */
-    init_log(app_name, level, target, file_path, file_size, file_number);
+    init_log(app_name, level, target, file_size, file_number);
 }
 
 /// Init and set the log target to console
@@ -403,7 +401,7 @@ pub fn init_log_for_subum(
 ///
 /// level: maximum log level
 pub fn init_log_to_console(app_name: &str, level: LevelFilter) {
-    init_log(app_name, level, "console-syslog", "", 0, 0);
+    init_log(app_name, level, "console-syslog", 0, 0);
 }
 
 /// Init and set the log target to file
@@ -412,19 +410,11 @@ pub fn init_log_to_console(app_name: &str, level: LevelFilter) {
 ///
 /// level: maximum log level
 ///
-/// file_path: file path
-///
 /// file_size: the maximum size of an active log file
 ///
 /// file_number: the maximum number of rotated log files
-pub fn init_log_to_file(
-    app_name: &str,
-    level: LevelFilter,
-    file_path: &str,
-    file_size: u32,
-    file_number: u32,
-) {
-    init_log(app_name, level, "file", file_path, file_size, file_number);
+pub fn init_log_to_file(app_name: &str, level: LevelFilter, file_size: u32, file_number: u32) {
+    init_log(app_name, level, "file", file_size, file_number);
 }
 
 /// Init and set the logger
@@ -435,8 +425,6 @@ pub fn init_log_to_file(
 ///
 /// target: log target
 ///
-/// file_path: file path (valid when target == "file")
-///
 /// file_size: the maximum size of an active log file (valid when target == "file")
 ///
 /// file_number: the maximum number of rotated log files (valid when target == "file")
@@ -444,16 +432,14 @@ pub fn init_log(
     _app_name: &str,
     level: LevelFilter,
     target: &str,
-    file_path: &str,
     file_size: u32,
     file_number: u32,
 ) {
     let mut target = target;
-    if target == "file" && (file_path.is_empty() || file_size == 0 || file_number == 0) {
+    if target == "file" && (file_size == 0 || file_number == 0) {
         println!(
             "LogTarget is configured to `file`, but configuration is invalid, changing the \
-             LogTarget to `console`, file: {file_path}, file_size: {file_size}, file_number: \
-             {file_number}"
+             LogTarget to `syslog`, file_size: {file_size}, file_number: {file_number}"
         );
         target = "syslog";
     }
@@ -474,7 +460,7 @@ pub fn init_log(
     if target == "file" {
         let _ = log::set_boxed_logger(Box::new(FileLogger::new(
             log::Level::Debug,
-            PathBuf::from(&file_path),
+            PathBuf::from(LOG_FILE_PATH),
             0o600,
             file_size,
             file_number,
diff --git a/libs/constants/src/lib.rs b/libs/constants/src/lib.rs
index b1db27c..6005067 100644
--- a/libs/constants/src/lib.rs
+++ b/libs/constants/src/lib.rs
@@ -26,3 +26,6 @@ pub const ALIVE: &str = "ALIVE01234567890";
 
 /// Socket used to transfer message between sysmaster and sctl
 pub const SCTL_SOCKET: &str = "/run/sysmaster/sctl";
+
+/// Default log file path when LogTarget is configured to "file"
+pub const LOG_FILE_PATH: &str = "/var/log/sysmaster/sysmaster.log";
-- 
2.33.0

