diff --git a/scripts/build_freebsd.sh b/scripts/build_freebsd.sh index d2f142c..bded7e5 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,8 @@ 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~{{ LOG_FILE }}~${PACKAGE_LOGFILE}~" \ | sed "s~{{ DESCRIPTION }}~${PACKAGE_DESCRIPTION}~" \ | sed "s~{{ PROGRAM }}~/${PACKAGE_BINDIR}/${PACKAGE_NAME}~" \ > ${BASE_DIR}/${PACKAGE_TMPDIR}/${PACKAGE_RCDIR}/${RC_NAME} @@ -29,6 +34,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..094be11 100644 --- a/scripts/templates/rc.conf +++ b/scripts/templates/rc.conf @@ -15,17 +15,17 @@ name="{{ RC_NAME }}" desc="{{ DESCRIPTION }}" -logfile="${eosio_api_healthcheck_logfile:-/var/log/${name}.log}" -pidfile="/var/run/${name}.pid" +logfile="${eosio_api_healthcheck_logfile:-{{ LOG_FILE }}}" +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 diff --git a/src/main.go b/src/main.go index 7e399a9..bd27ecf 100644 --- a/src/main.go +++ b/src/main.go @@ -3,8 +3,10 @@ package main import ( "os" - "internal/pid" + "os/signal" + "syscall" "internal/log" + "internal/pid" "github.com/pborman/getopt/v2" ) @@ -14,6 +16,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,13 +46,62 @@ 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 + + // 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. + logfd = fd + 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 SIGHUP signal. + signal.Notify(sig_ch, syscall.SIGHUP) + + // Event loop (runs in a seperate thread) + go func() { + for { + // Block until we get a signal. + sig := <- sig_ch + + switch sig { + // SIGHUP is sent when logfile is rotated. + case syscall.SIGHUP : + msg := "SIGHUP (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 @@ -52,7 +109,6 @@ func openlog(file string) *os.File { func main() { var version bool - var logfd *os.File // Command line parsing getopt.FlagLong(&version, "version", 'v', "Print version") @@ -65,9 +121,9 @@ func main() { return; } + // Open logfile. if len(logFile) > 0 { - logfd = openlog(logFile) - log.SetWriter(logfd) + setLogFile() } log.Info("Process is starting with PID: %d", pid.Get()) @@ -80,5 +136,9 @@ func main() { } } + // Run the signal event loop. + signalEventLoop() + + // Start listening to TCP Connections spawnTcpServer(argv_listen_addr()); }