summary refs log tree commit diff
path: root/sys_util/src/syslog.rs
diff options
context:
space:
mode:
authorChirantan Ekbote <chirantan@chromium.org>2018-01-04 16:07:54 -0800
committerchrome-bot <chrome-bot@chromium.org>2018-01-05 14:28:45 -0800
commit00600771148778e423b62614cc069b6c177bd660 (patch)
tree787333317cfbc62bcddb592d4b9a736ebd349b7d /sys_util/src/syslog.rs
parentd42e4931436c67cfbf5750c5bc27f029f4fe64ac (diff)
downloadcrosvm-00600771148778e423b62614cc069b6c177bd660.tar
crosvm-00600771148778e423b62614cc069b6c177bd660.tar.gz
crosvm-00600771148778e423b62614cc069b6c177bd660.tar.bz2
crosvm-00600771148778e423b62614cc069b6c177bd660.tar.lz
crosvm-00600771148778e423b62614cc069b6c177bd660.tar.xz
crosvm-00600771148778e423b62614cc069b6c177bd660.tar.zst
crosvm-00600771148778e423b62614cc069b6c177bd660.zip
syslog: closelog before trying to figure out the fd
The syslog subsystem tries to figure out the file descriptor for the
connection to the system logger so that it can ensure that it doesn't
get closed in each device process.

However, the check does not work properly if there was already an open
connection to the system logger.  In this case the openlog call does not
do anything and we end up guessing the wrong file descriptor number for
the syslog connection.

Work around this by adding a closelog() call before attempting all of
this cleverness.  In the long run this should be fixed properly by just
bind mounting /dev/log into each device process's jail.

BUG=none
TEST=Running crosvm under minijail0 does not cause an InvalidFd error.

Change-Id: Iffd535d62acdf8053817af74b9e97444c746a0cf
Signed-off-by: Chirantan Ekbote <chirantan@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/851271
Reviewed-by: Zach Reizner <zachr@chromium.org>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Diffstat (limited to 'sys_util/src/syslog.rs')
-rw-r--r--sys_util/src/syslog.rs7
1 files changed, 6 insertions, 1 deletions
diff --git a/sys_util/src/syslog.rs b/sys_util/src/syslog.rs
index 550f913..81aea6b 100644
--- a/sys_util/src/syslog.rs
+++ b/sys_util/src/syslog.rs
@@ -38,7 +38,7 @@ use std::ptr::null;
 use std::str::from_utf8;
 use std::sync::{Mutex, MutexGuard, Once, ONCE_INIT};
 
-use libc::{tm, time, time_t, localtime_r, gethostname, openlog, fcntl, c_char, LOG_NDELAY,
+use libc::{tm, time, time_t, localtime_r, gethostname, openlog, closelog, fcntl, c_char, LOG_NDELAY,
            LOG_PERROR, LOG_PID, LOG_USER, F_GETFD};
 
 use getpid;
@@ -148,6 +148,11 @@ fn get_proc_name() -> Option<String> {
 // libraries in use that hard depend on libc's syslogger. Remove this and go back to making the
 // connection directly once minjail is ready.
 fn openlog_and_get_socket() -> Result<UnixDatagram, Error> {
+    // closelog first in case there was already a file descriptor open.  Safe because it takes no
+    // arguments and just closes an open file descriptor.  Does nothing if the file descriptor
+    // was not already open.
+    unsafe { closelog(); }
+
     // Ordinarily libc's FD for the syslog connection can't be accessed, but we can guess that the
     // FD that openlog will be getting is the lowest unused FD. To guarantee that an FD is opened in
     // this function we use the LOG_NDELAY to tell openlog to connect to the syslog now. To get the