From 184302b9e45d028c5f14ee3a99e90f10e86d75ad Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Fri, 26 Jun 2020 03:44:13 +0200 Subject: [PATCH 1/7] freebsd: tell newsyslog where the pid file is. --- scripts/build_freebsd.sh | 5 +++++ scripts/templates/newsyslog.conf | 2 +- scripts/templates/rc.conf | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/scripts/build_freebsd.sh b/scripts/build_freebsd.sh index d2f142c..0a539f8 100755 --- a/scripts/build_freebsd.sh +++ b/scripts/build_freebsd.sh @@ -5,6 +5,9 @@ PACKAGE_TMPDIR="${PACKAGE_TMPDIR}/freebsd" PACKAGE_RCDIR=/etc/rc.d PACKAGE_NEWSYSLOGDIR=etc/newsyslog.conf.d +# Common variables +PID_FILE=/var/run/${PACKAGE_NAME}.pid + ############################ # Create rc file # ############################ @@ -15,6 +18,7 @@ RC_NAME=$(echo ${PACKAGE_NAME} | sed "s~-~_~g") mkdir -p ${BASE_DIR}/${PACKAGE_TMPDIR}/${PACKAGE_RCDIR} cat ${TEMPLATE_DIR}/rc.conf \ | sed "s~{{ RC_NAME }}~${RC_NAME}~g" \ + | sed "s~{{ PID_FILE }}~${PID_FILE}~g" \ | sed "s~{{ DESCRIPTION }}~${PACKAGE_DESCRIPTION}~" \ | sed "s~{{ PROGRAM }}~/${PACKAGE_BINDIR}/${PACKAGE_NAME}~" \ > ${BASE_DIR}/${PACKAGE_TMPDIR}/${PACKAGE_RCDIR}/${RC_NAME} @@ -29,6 +33,7 @@ chmod 755 ${BASE_DIR}/${PACKAGE_TMPDIR}/${PACKAGE_RCDIR}/${RC_NAME} mkdir -p ${BASE_DIR}/${PACKAGE_TMPDIR}/${PACKAGE_NEWSYSLOGDIR} cat ${TEMPLATE_DIR}/newsyslog.conf \ | sed "s~{{ LOG_FILE }}~${PACKAGE_LOGFILE}~" \ + | sed "s~{{ PID_FILE }}~${PID_FILE}~g" \ > ${BASE_DIR}/${PACKAGE_TMPDIR}/${PACKAGE_NEWSYSLOGDIR}/${PACKAGE_NAME}.conf diff --git a/scripts/templates/newsyslog.conf b/scripts/templates/newsyslog.conf index ebc08e8..99b93c0 100644 --- a/scripts/templates/newsyslog.conf +++ b/scripts/templates/newsyslog.conf @@ -1,2 +1,2 @@ # logfilename [owner:group] mode count size when flags [/pid_file] [sig_num] -{{ LOG_FILE }} 640 3 * @T10 JC +{{ LOG_FILE }} 640 3 * @T10 JC {{ PID_FILE }} diff --git a/scripts/templates/rc.conf b/scripts/templates/rc.conf index 8464749..a9fb36a 100644 --- a/scripts/templates/rc.conf +++ b/scripts/templates/rc.conf @@ -16,7 +16,7 @@ name="{{ RC_NAME }}" desc="{{ DESCRIPTION }}" logfile="${eosio_api_healthcheck_logfile:-/var/log/${name}.log}" -pidfile="/var/run/${name}.pid" +pidfile="{{ PID_FILE }}" command="{{ PROGRAM }}" command_args="-p ${pidfile} ${eosio_api_healthcheck_args}" From c4e404f8738639ebfa0926822bc4de2b499e7a82 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Fri, 26 Jun 2020 18:04:11 +0200 Subject: [PATCH 2/7] src/main.go: rewrite openlog() to a more specific setLogFile() function. --- src/main.go | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/main.go b/src/main.go index 8731c1c..e566ae3 100644 --- a/src/main.go +++ b/src/main.go @@ -14,6 +14,12 @@ import ( var logFile string var pidFile string +// Global variables +// --------------------------------------------------------- + +// File descriptor to the current log file. +var logfd *os.File + // argv_listen_addr // Parse listen address from command line. // --------------------------------------------------------- @@ -38,29 +44,36 @@ func argv_listen_addr() string { return addr } -func openlog(file string) *os.File { +func setLogFile() { + // Open file fd, err := os.OpenFile(logFile, os.O_APPEND | os.O_CREATE | os.O_WRONLY, 0644) if err != nil { log.Error(err.Error()) } - return fd + + // Close old one (if open) + if logfd.Fd() < 0 { + logfd.Close() + } + + // Update variable and set log writer. + logfd = fd + log.SetWriter(logfd) } // main // --------------------------------------------------------- func main() { - var logfd *os.File - // Command line parsing getopt.FlagLong(&logFile, "log", 'l', "Path to log file", "file") getopt.FlagLong(&pidFile, "pid", 'p', "Path to pid file", "file") getopt.Parse() + // Open logfile. if len(logFile) > 0 { - logfd = openlog(logFile) - log.SetWriter(logfd) + setLogFile() } log.Info("Process is starting with PID: %d", pid.Get()) From 823832c94e60bb5fbea50c82ca2c3c0d338a539f Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Fri, 26 Jun 2020 18:10:05 +0200 Subject: [PATCH 3/7] src/main.go: act on the SIGUSR1 signal (sent by logrotate daemon) and reopen log file. --- src/main.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/main.go b/src/main.go index e566ae3..006d096 100644 --- a/src/main.go +++ b/src/main.go @@ -3,6 +3,8 @@ package main import ( "os" + "os/signal" + "syscall" "./log" "./pid" "github.com/pborman/getopt/v2" @@ -62,6 +64,44 @@ func setLogFile() { log.SetWriter(logfd) } +// signalEventLoop() +// Initialize event channel for OS signals +// and runs an event loop in a separate thread. +// --------------------------------------------------------- +func signalEventLoop() { + + // Setup a channel + sig_ch := make(chan os.Signal, 1) + + // subscribe to USR1 signal. + signal.Notify(sig_ch, syscall.SIGUSR1) + + // Event loop (runs in a seperate thread) + go func() { + for { + // Block until we get a signal. + sig := <- sig_ch + + switch sig { + // USR1 is sent when logfile is rotated. + case syscall.SIGUSR1 : + msg := "SIGUSR1 (Logfile was rotated): " + + if logfd != nil { + setLogFile() + msg += "Filedescriptor was updated" + } else { + msg += "No Filedescriptor to update (most likely uses standard out/err streams)" + } + + log.Info(msg) + default: + log.Warning("Unknown signal %s", sig) + } + } + }() +} + // main // --------------------------------------------------------- func main() { @@ -86,5 +126,9 @@ func main() { } } + // Run the signal event loop. + signalEventLoop() + + // Start listening to TCP Connections spawnTcpServer(argv_listen_addr()); } From 9532a8d5ef70789842610ad41e0ac3c511c1d973 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Fri, 24 Dec 2021 11:03:18 +0100 Subject: [PATCH 4/7] scripts/templates/rc.conf: pass logfile as "-l" flag --- scripts/templates/rc.conf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/templates/rc.conf b/scripts/templates/rc.conf index a9fb36a..ae839e5 100644 --- a/scripts/templates/rc.conf +++ b/scripts/templates/rc.conf @@ -18,14 +18,14 @@ desc="{{ DESCRIPTION }}" logfile="${eosio_api_healthcheck_logfile:-/var/log/${name}.log}" pidfile="{{ PID_FILE }}" command="{{ PROGRAM }}" -command_args="-p ${pidfile} ${eosio_api_healthcheck_args}" +command_args="-p ${pidfile} -l ${logfile} ${eosio_api_healthcheck_args}" start_cmd="${name}_start" eosio_api_healthcheck_start() { echo "Starting ${name}" - ${command} ${command_args} >>${logfile} 2>&1 & + ${command} ${command_args} 2>&1 & } load_rc_config $name From 2470062c3d09e6f6110e2890f982a9bd63c4dcd5 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Fri, 24 Dec 2021 11:04:19 +0100 Subject: [PATCH 5/7] scripts/templates/rc.conf: use "LOG_FILE" variable. --- scripts/build_freebsd.sh | 1 + scripts/templates/rc.conf | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/build_freebsd.sh b/scripts/build_freebsd.sh index 0a539f8..bded7e5 100755 --- a/scripts/build_freebsd.sh +++ b/scripts/build_freebsd.sh @@ -19,6 +19,7 @@ mkdir -p ${BASE_DIR}/${PACKAGE_TMPDIR}/${PACKAGE_RCDIR} cat ${TEMPLATE_DIR}/rc.conf \ | sed "s~{{ RC_NAME }}~${RC_NAME}~g" \ | sed "s~{{ PID_FILE }}~${PID_FILE}~g" \ + | sed "s~{{ LOG_FILE }}~${PACKAGE_LOGFILE}~" \ | sed "s~{{ DESCRIPTION }}~${PACKAGE_DESCRIPTION}~" \ | sed "s~{{ PROGRAM }}~/${PACKAGE_BINDIR}/${PACKAGE_NAME}~" \ > ${BASE_DIR}/${PACKAGE_TMPDIR}/${PACKAGE_RCDIR}/${RC_NAME} diff --git a/scripts/templates/rc.conf b/scripts/templates/rc.conf index ae839e5..094be11 100644 --- a/scripts/templates/rc.conf +++ b/scripts/templates/rc.conf @@ -15,7 +15,7 @@ name="{{ RC_NAME }}" desc="{{ DESCRIPTION }}" -logfile="${eosio_api_healthcheck_logfile:-/var/log/${name}.log}" +logfile="${eosio_api_healthcheck_logfile:-{{ LOG_FILE }}}" pidfile="{{ PID_FILE }}" command="{{ PROGRAM }}" command_args="-p ${pidfile} -l ${logfile} ${eosio_api_healthcheck_args}" From 8be4466d66ca58e09dcfa5e934bce090de4508fe Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Fri, 24 Dec 2021 11:05:47 +0100 Subject: [PATCH 6/7] src/main.go: in setLogFile() check for null when closing old FD and also check error from Close() --- src/main.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main.go b/src/main.go index 006d096..fa6ccf7 100644 --- a/src/main.go +++ b/src/main.go @@ -54,9 +54,11 @@ func setLogFile() { log.Error(err.Error()) } - // Close old one (if open) - if logfd.Fd() < 0 { - logfd.Close() + // Try close if old descriptor is defined. + if logfd != nil { + if err = logfd.Close(); err != nil { + log.Error(err.Error()) + } } // Update variable and set log writer. From 0ddbf6dea0d06bd0cbe9cd5960f05b92c56f255b Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Fri, 24 Dec 2021 11:06:30 +0100 Subject: [PATCH 7/7] src/main.go: newsyslog actually sends SIGHUP, not SIGUSR1 --- src/main.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main.go b/src/main.go index fa6ccf7..8180542 100644 --- a/src/main.go +++ b/src/main.go @@ -75,8 +75,8 @@ func signalEventLoop() { // Setup a channel sig_ch := make(chan os.Signal, 1) - // subscribe to USR1 signal. - signal.Notify(sig_ch, syscall.SIGUSR1) + // subscribe to SIGHUP signal. + signal.Notify(sig_ch, syscall.SIGHUP) // Event loop (runs in a seperate thread) go func() { @@ -85,9 +85,9 @@ func signalEventLoop() { sig := <- sig_ch switch sig { - // USR1 is sent when logfile is rotated. - case syscall.SIGUSR1 : - msg := "SIGUSR1 (Logfile was rotated): " + // SIGHUP is sent when logfile is rotated. + case syscall.SIGHUP : + msg := "SIGHUP (Logfile was rotated): " if logfd != nil { setLogFile()