summary refs log tree commit diff
path: root/sys_util/src/syslog.rs
diff options
context:
space:
mode:
authorChris Morin <cmtm@google.com>2019-02-25 11:35:06 -0800
committerchrome-bot <chrome-bot@chromium.org>2019-03-20 23:55:09 -0700
commit7abdbfe201e001a60e5cd9db1653e20ac0f78d91 (patch)
treef53f483f05b9a0e9df960c6c9993661b1c76c427 /sys_util/src/syslog.rs
parentf1a01f6e635f0b2ee95a3727dbd4f6ab1042cadb (diff)
downloadcrosvm-7abdbfe201e001a60e5cd9db1653e20ac0f78d91.tar
crosvm-7abdbfe201e001a60e5cd9db1653e20ac0f78d91.tar.gz
crosvm-7abdbfe201e001a60e5cd9db1653e20ac0f78d91.tar.bz2
crosvm-7abdbfe201e001a60e5cd9db1653e20ac0f78d91.tar.lz
crosvm-7abdbfe201e001a60e5cd9db1653e20ac0f78d91.tar.xz
crosvm-7abdbfe201e001a60e5cd9db1653e20ac0f78d91.tar.zst
crosvm-7abdbfe201e001a60e5cd9db1653e20ac0f78d91.zip
sys_util: don't use hostname in syslog message header
The putting the hostname in the syslog header of messages sent to
/dev/log isn't widely supported. It isn't understood by rsyslogd by
default, and it isn't understood by journald. Remove it as it provides
no value to us.

BUG=None
TEST=Ensure journal properly parses the header from crosvm log messages

Change-Id: I9bba78925f048f7d2ce6320b00b9fa52f070ce51
Reviewed-on: https://chromium-review.googlesource.com/1525139
Commit-Ready: Christopher Morin <cmtm@google.com>
Tested-by: Christopher Morin <cmtm@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Zach Reizner <zachr@chromium.org>
Diffstat (limited to 'sys_util/src/syslog.rs')
-rw-r--r--sys_util/src/syslog.rs35
1 files changed, 3 insertions, 32 deletions
diff --git a/sys_util/src/syslog.rs b/sys_util/src/syslog.rs
index 113ad56..a87a2ea 100644
--- a/sys_util/src/syslog.rs
+++ b/sys_util/src/syslog.rs
@@ -35,12 +35,11 @@ use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
 use std::os::unix::net::UnixDatagram;
 use std::path::PathBuf;
 use std::ptr::null;
-use std::str::from_utf8;
 use std::sync::{MutexGuard, Once, ONCE_INIT};
 
 use libc::{
-    c_char, closelog, fcntl, gethostname, localtime_r, openlog, time, time_t, tm, F_GETFD,
-    LOG_NDELAY, LOG_PERROR, LOG_PID, LOG_USER,
+    closelog, fcntl, localtime_r, openlog, time, time_t, tm, F_GETFD, LOG_NDELAY, LOG_PERROR,
+    LOG_PID, LOG_USER,
 };
 
 use sync::Mutex;
@@ -138,20 +137,6 @@ impl Display for Error {
     }
 }
 
-fn get_hostname() -> Result<String, ()> {
-    let mut hostname: [u8; 256] = [b'\0'; 256];
-    // Safe because we give a valid pointer to a buffer of the indicated length and check for the
-    // result.
-    let ret = unsafe { gethostname(hostname.as_mut_ptr() as *mut c_char, hostname.len()) };
-    if ret == -1 {
-        return Err(());
-    }
-
-    let len = hostname.iter().position(|&v| v == b'\0').ok_or(())?;
-
-    Ok(from_utf8(&hostname[..len]).map_err(|_| ())?.to_string())
-}
-
 fn get_proc_name() -> Option<String> {
     env::args_os()
         .next()
@@ -206,7 +191,6 @@ struct State {
     stderr: bool,
     socket: Option<UnixDatagram>,
     file: Option<File>,
-    hostname: Option<String>,
     proc_name: Option<String>,
 }
 
@@ -217,7 +201,6 @@ impl State {
             stderr: true,
             socket: Some(s),
             file: None,
-            hostname: get_hostname().ok(),
             proc_name: get_proc_name(),
         })
     }
@@ -272,17 +255,6 @@ macro_rules! lock {
     };
 }
 
-/// Replaces the hostname reported in each syslog message.
-///
-/// The default hostname is whatever `gethostname()` returned when `syslog::init()` was first
-/// called.
-///
-/// Does nothing if syslog was never initialized.
-pub fn set_hostname<T: Into<String>>(hostname: T) {
-    let mut state = lock!();
-    state.hostname = Some(hostname.into());
-}
-
 /// Replaces the process name reported in each syslog message.
 ///
 /// The default process name is the _file name_ of `argv[0]`. For example, if this program was
@@ -453,14 +425,13 @@ pub fn log(pri: Priority, fac: Facility, file_name: &str, line: u32, args: fmt::
             (
                 write!(
                     &mut buf_cursor,
-                    "<{}>{} {:02} {:02}:{:02}:{:02} {} {}[{}]: [{}:{}] {}",
+                    "<{}>{} {:02} {:02}:{:02}:{:02} {}[{}]: [{}:{}] {}",
                     prifac,
                     MONTHS[tm.tm_mon as usize],
                     tm.tm_mday,
                     tm.tm_hour,
                     tm.tm_min,
                     tm.tm_sec,
-                    state.hostname.as_ref().map(|s| s.as_ref()).unwrap_or("-"),
                     state.proc_name.as_ref().map(|s| s.as_ref()).unwrap_or("-"),
                     getpid(),
                     file_name,