summaryrefslogtreecommitdiffstats
path: root/vespalog
diff options
context:
space:
mode:
authorArne H Juul <arnej27959@users.noreply.github.com>2023-01-16 10:27:21 +0100
committerGitHub <noreply@github.com>2023-01-16 10:27:21 +0100
commitbb09fd3a3d969a65d4c87378e75773b97e8cf61c (patch)
tree492e8a12538cd85441843aa36ca484b4b40ee073 /vespalog
parent6bf0a330a819f0b1cecdf8f5553cecdfb9b6a4d8 (diff)
parent1a7839b48ecc3d87f71123756653969c2f9bfa5f (diff)
Merge pull request #25563 from vespa-engine/arnej/make-dup-of-logging-fd
make a duplicate of the file descriptor in target
Diffstat (limited to 'vespalog')
-rw-r--r--vespalog/src/vespa/log/log-target-fd.cpp10
-rw-r--r--vespalog/src/vespa/log/log-target-fd.h8
-rw-r--r--vespalog/src/vespa/log/log-target.cpp14
3 files changed, 17 insertions, 15 deletions
diff --git a/vespalog/src/vespa/log/log-target-fd.cpp b/vespalog/src/vespa/log/log-target-fd.cpp
index cb1b2e792b4..bb7590a9908 100644
--- a/vespalog/src/vespa/log/log-target-fd.cpp
+++ b/vespalog/src/vespa/log/log-target-fd.cpp
@@ -1,6 +1,7 @@
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <sys/types.h>
#include <unistd.h>
+#include <fcntl.h>
#include <cstring>
#include <cstdlib>
@@ -11,22 +12,23 @@ LOG_SETUP(".log");
namespace ns_log {
-LogTargetFd::LogTargetFd(const char *target)
+LogTargetFd::LogTargetFd(int fd_spec, const char *target)
: LogTarget(target),
_fd(-1), _istty(false)
{
- if (strncmp(target, "fd:", 3) != 0) {
+ _fd = dup(fd_spec);
+ if (_fd == -1) {
throwInvalid("Bad target for LogTargetFd: '%s'", target);
}
- _fd = strtol(target + 3, NULL, 0);
if (isatty(_fd) == 1) {
_istty = true;
}
+ fcntl(_fd, F_SETFD, FD_CLOEXEC);
}
LogTargetFd::~LogTargetFd()
{
- // Must not close _fd, we did not open it!
+ close(_fd);
}
diff --git a/vespalog/src/vespa/log/log-target-fd.h b/vespalog/src/vespa/log/log-target-fd.h
index fa8df8ce8ed..85857c206b5 100644
--- a/vespalog/src/vespa/log/log-target-fd.h
+++ b/vespalog/src/vespa/log/log-target-fd.h
@@ -9,12 +9,12 @@ class LogTargetFd : public LogTarget {
private:
int _fd;
bool _istty;
- LogTargetFd();
- LogTargetFd(const LogTargetFd&);
- LogTargetFd& operator = (const LogTargetFd);
+ LogTargetFd() = delete;
+ LogTargetFd(const LogTargetFd&) = delete;
+ LogTargetFd& operator= (const LogTargetFd) = delete;
public:
- explicit LogTargetFd(const char *target);
+ LogTargetFd(int fd_spec, const char *target);
int write(const char *buf, int len) override;
~LogTargetFd();
bool makeHumanReadable() const override { return _istty; }
diff --git a/vespalog/src/vespa/log/log-target.cpp b/vespalog/src/vespa/log/log-target.cpp
index 4ac037354e1..26f064634ca 100644
--- a/vespalog/src/vespa/log/log-target.cpp
+++ b/vespalog/src/vespa/log/log-target.cpp
@@ -25,21 +25,21 @@ LogTarget *
LogTarget::defaultTarget()
{
// Note! This function cannot LOG().
- return new LogTargetFd("fd:2");
+ return new LogTargetFd(2, "fd:2");
}
LogTarget *
LogTarget::makeTarget(const char *const target)
{
- LogTarget *res = NULL;
if (strncmp(target, "fd:", 3) == 0) {
- res = new LogTargetFd(target);
+ int fd_spec = strtol(target + 3, NULL, 0);
+ if (fd_spec > 0) {
+ return new LogTargetFd(fd_spec, target);
+ }
} else if (strncmp(target, "file:", 5) == 0) {
- res = new LogTargetFile(target);
- } else {
- throwInvalid("Log target '%s' is invalid.", target);
+ return new LogTargetFile(target);
}
- return res;
+ throwInvalid("Log target '%s' is invalid.", target);
}