From f185621d41f03a23b55795b89e6584253fa23505 Mon Sep 17 00:00:00 2001 From: Stefan Hajnoczi Date: Wed, 26 Jun 2019 10:25:54 +0100 Subject: virtiofsd: add --syslog command-line option MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sometimes collecting output from stderr is inconvenient or does not fit within the overall logging architecture. Add syslog(3) support for cases where stderr cannot be used. Signed-off-by: Stefan Hajnoczi dgilbert: Reworked as a logging function Signed-off-by: Dr. David Alan Gilbert Reviewed-by: Daniel P. Berrangé Signed-off-by: Dr. David Alan Gilbert --- tools/virtiofsd/passthrough_ll.c | 50 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) (limited to 'tools/virtiofsd/passthrough_ll.c') diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c index c281d817af..0372aca143 100644 --- a/tools/virtiofsd/passthrough_ll.c +++ b/tools/virtiofsd/passthrough_ll.c @@ -58,6 +58,7 @@ #include #include #include +#include #include #include "passthrough_helpers.h" @@ -138,6 +139,7 @@ static const struct fuse_opt lo_opts[] = { { "norace", offsetof(struct lo_data, norace), 1 }, FUSE_OPT_END }; +static bool use_syslog = false; static void unref_inode(struct lo_data *lo, struct lo_inode *inode, uint64_t n); @@ -2262,11 +2264,12 @@ static void setup_mounts(const char *source) * Lock down this process to prevent access to other processes or files outside * source directory. This reduces the impact of arbitrary code execution bugs. */ -static void setup_sandbox(struct lo_data *lo, struct fuse_session *se) +static void setup_sandbox(struct lo_data *lo, struct fuse_session *se, + bool enable_syslog) { setup_namespaces(lo, se); setup_mounts(lo->source); - setup_seccomp(); + setup_seccomp(enable_syslog); } /* Raise the maximum number of open file descriptors */ @@ -2298,6 +2301,42 @@ static void setup_nofile_rlimit(void) } } +static void log_func(enum fuse_log_level level, const char *fmt, va_list ap) +{ + if (use_syslog) { + int priority = LOG_ERR; + switch (level) { + case FUSE_LOG_EMERG: + priority = LOG_EMERG; + break; + case FUSE_LOG_ALERT: + priority = LOG_ALERT; + break; + case FUSE_LOG_CRIT: + priority = LOG_CRIT; + break; + case FUSE_LOG_ERR: + priority = LOG_ERR; + break; + case FUSE_LOG_WARNING: + priority = LOG_WARNING; + break; + case FUSE_LOG_NOTICE: + priority = LOG_NOTICE; + break; + case FUSE_LOG_INFO: + priority = LOG_INFO; + break; + case FUSE_LOG_DEBUG: + priority = LOG_DEBUG; + break; + } + vsyslog(priority, fmt, ap); + } else { + vfprintf(stderr, fmt, ap); + } +} + int main(int argc, char *argv[]) { struct fuse_args args = FUSE_ARGS_INIT(argc, argv); @@ -2336,6 +2375,11 @@ int main(int argc, char *argv[]) if (fuse_parse_cmdline(&args, &opts) != 0) { return 1; } + fuse_set_log_func(log_func); + use_syslog = opts.syslog; + if (use_syslog) { + openlog("virtiofsd", LOG_PID, LOG_DAEMON); + } if (opts.show_help) { printf("usage: %s [options]\n\n", argv[0]); fuse_cmdline_help(); @@ -2424,7 +2468,7 @@ int main(int argc, char *argv[]) /* Must be before sandbox since it wants /proc */ setup_capng(); - setup_sandbox(&lo, se); + setup_sandbox(&lo, se, opts.syslog); /* Block until ctrl+c or fusermount -u */ ret = virtio_loop(se); -- cgit v1.2.3