2019-08-26 11:44:36 +00:00
|
|
|
/*
|
|
|
|
logger.c -- logging code
|
2019-08-26 11:44:52 +00:00
|
|
|
Copyright (C) 2004-2017 Guus Sliepen <guus@tinc-vpn.org>
|
2019-08-26 11:44:37 +00:00
|
|
|
2004-2005 Ivo Timmermans
|
2019-08-26 11:44:36 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
2019-08-26 11:44:38 +00:00
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2019-08-26 11:44:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "system.h"
|
|
|
|
|
|
|
|
#include "conf.h"
|
2019-08-26 11:44:49 +00:00
|
|
|
#include "meta.h"
|
2019-08-26 11:44:50 +00:00
|
|
|
#include "names.h"
|
2019-08-26 11:44:36 +00:00
|
|
|
#include "logger.h"
|
2019-08-26 11:44:49 +00:00
|
|
|
#include "connection.h"
|
|
|
|
#include "control_common.h"
|
2019-08-26 11:44:52 +00:00
|
|
|
#include "process.h"
|
2019-08-26 11:44:49 +00:00
|
|
|
#include "sptps.h"
|
2019-08-26 11:44:36 +00:00
|
|
|
|
2019-08-26 11:44:52 +00:00
|
|
|
int debug_level = DEBUG_NOTHING;
|
2019-08-26 11:44:36 +00:00
|
|
|
static logmode_t logmode = LOGMODE_STDERR;
|
|
|
|
static pid_t logpid;
|
|
|
|
static FILE *logfile = NULL;
|
|
|
|
#ifdef HAVE_MINGW
|
|
|
|
static HANDLE loghandle = NULL;
|
|
|
|
#endif
|
|
|
|
static const char *logident = NULL;
|
2019-08-26 11:44:49 +00:00
|
|
|
bool logcontrol = false;
|
2019-08-26 11:44:52 +00:00
|
|
|
int umbilical = 0;
|
2019-08-26 11:44:49 +00:00
|
|
|
|
|
|
|
static void real_logger(int level, int priority, const char *message) {
|
|
|
|
char timestr[32] = "";
|
|
|
|
static bool suppress = false;
|
|
|
|
|
|
|
|
// Bail out early if there is nothing to do.
|
2019-08-26 11:44:53 +00:00
|
|
|
if(suppress) {
|
2019-08-26 11:44:49 +00:00
|
|
|
return;
|
2019-08-26 11:44:53 +00:00
|
|
|
}
|
2019-08-26 11:44:49 +00:00
|
|
|
|
2019-08-26 11:44:53 +00:00
|
|
|
if(!logcontrol && (level > debug_level || logmode == LOGMODE_NULL)) {
|
2019-08-26 11:44:49 +00:00
|
|
|
return;
|
2019-08-26 11:44:53 +00:00
|
|
|
}
|
2019-08-26 11:44:49 +00:00
|
|
|
|
|
|
|
if(level <= debug_level) {
|
|
|
|
switch(logmode) {
|
2019-08-26 11:44:53 +00:00
|
|
|
case LOGMODE_STDERR:
|
|
|
|
fprintf(stderr, "%s\n", message);
|
|
|
|
fflush(stderr);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOGMODE_FILE:
|
|
|
|
if(!now.tv_sec) {
|
|
|
|
gettimeofday(&now, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
time_t now_sec = now.tv_sec;
|
|
|
|
strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S", localtime(&now_sec));
|
|
|
|
fprintf(logfile, "%s %s[%ld]: %s\n", timestr, logident, (long)logpid, message);
|
|
|
|
fflush(logfile);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOGMODE_SYSLOG:
|
2019-08-26 11:44:49 +00:00
|
|
|
#ifdef HAVE_MINGW
|
2019-08-26 11:44:53 +00:00
|
|
|
{
|
|
|
|
const char *messages[] = {message};
|
|
|
|
ReportEvent(loghandle, priority, 0, 0, NULL, 1, 0, messages, NULL);
|
|
|
|
}
|
|
|
|
|
2019-08-26 11:44:49 +00:00
|
|
|
#else
|
|
|
|
#ifdef HAVE_SYSLOG_H
|
2019-08-26 11:44:53 +00:00
|
|
|
syslog(priority, "%s", message);
|
2019-08-26 11:44:49 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
2019-08-26 11:44:53 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LOGMODE_NULL:
|
|
|
|
break;
|
2019-08-26 11:44:49 +00:00
|
|
|
}
|
2019-08-26 11:44:52 +00:00
|
|
|
|
|
|
|
if(umbilical && do_detach) {
|
|
|
|
write(umbilical, message, strlen(message));
|
|
|
|
write(umbilical, "\n", 1);
|
|
|
|
}
|
2019-08-26 11:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(logcontrol) {
|
|
|
|
suppress = true;
|
|
|
|
logcontrol = false;
|
2019-08-26 11:44:53 +00:00
|
|
|
|
2019-08-26 11:44:49 +00:00
|
|
|
for list_each(connection_t, c, connection_list) {
|
2019-08-26 11:44:53 +00:00
|
|
|
if(!c->status.log) {
|
2019-08-26 11:44:49 +00:00
|
|
|
continue;
|
2019-08-26 11:44:53 +00:00
|
|
|
}
|
|
|
|
|
2019-08-26 11:44:49 +00:00
|
|
|
logcontrol = true;
|
2019-08-26 11:44:53 +00:00
|
|
|
|
|
|
|
if(level > (c->outcompression >= 0 ? c->outcompression : debug_level)) {
|
2019-08-26 11:44:49 +00:00
|
|
|
continue;
|
2019-08-26 11:44:53 +00:00
|
|
|
}
|
|
|
|
|
2019-08-26 11:44:49 +00:00
|
|
|
int len = strlen(message);
|
2019-08-26 11:44:53 +00:00
|
|
|
|
|
|
|
if(send_request(c, "%d %d %d", CONTROL, REQ_LOG, len)) {
|
2019-08-26 11:44:49 +00:00
|
|
|
send_meta(c, message, len);
|
2019-08-26 11:44:53 +00:00
|
|
|
}
|
2019-08-26 11:44:49 +00:00
|
|
|
}
|
2019-08-26 11:44:53 +00:00
|
|
|
|
2019-08-26 11:44:49 +00:00
|
|
|
suppress = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void logger(int level, int priority, const char *format, ...) {
|
|
|
|
va_list ap;
|
|
|
|
char message[1024] = "";
|
|
|
|
|
|
|
|
va_start(ap, format);
|
2019-08-26 11:44:53 +00:00
|
|
|
int len = vsnprintf(message, sizeof(message), format, ap);
|
|
|
|
message[sizeof(message) - 1] = 0;
|
2019-08-26 11:44:49 +00:00
|
|
|
va_end(ap);
|
|
|
|
|
2019-08-26 11:44:53 +00:00
|
|
|
if(len > 0 && (size_t)len < sizeof(message) - 1 && message[len - 1] == '\n') {
|
2019-08-26 11:44:49 +00:00
|
|
|
message[len - 1] = 0;
|
2019-08-26 11:44:53 +00:00
|
|
|
}
|
2019-08-26 11:44:49 +00:00
|
|
|
|
|
|
|
real_logger(level, priority, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sptps_logger(sptps_t *s, int s_errno, const char *format, va_list ap) {
|
2019-08-26 11:44:53 +00:00
|
|
|
(void)s_errno;
|
2019-08-26 11:44:52 +00:00
|
|
|
char message[1024];
|
2019-08-26 11:44:53 +00:00
|
|
|
size_t msglen = sizeof(message);
|
2019-08-26 11:44:52 +00:00
|
|
|
|
|
|
|
int len = vsnprintf(message, msglen, format, ap);
|
2019-08-26 11:44:53 +00:00
|
|
|
message[sizeof(message) - 1] = 0;
|
|
|
|
|
|
|
|
if(len > 0 && (size_t)len < sizeof(message) - 1) {
|
|
|
|
if(message[len - 1] == '\n') {
|
2019-08-26 11:44:52 +00:00
|
|
|
message[--len] = 0;
|
2019-08-26 11:44:53 +00:00
|
|
|
}
|
2019-08-26 11:44:52 +00:00
|
|
|
|
|
|
|
// WARNING: s->handle can point to a connection_t or a node_t,
|
|
|
|
// but both types have the name and hostname fields at the same offsets.
|
|
|
|
connection_t *c = s->handle;
|
2019-08-26 11:44:53 +00:00
|
|
|
|
|
|
|
if(c) {
|
|
|
|
snprintf(message + len, sizeof(message) - len, " from %s (%s)", c->name, c->hostname);
|
|
|
|
}
|
2019-08-26 11:44:52 +00:00
|
|
|
}
|
2019-08-26 11:44:49 +00:00
|
|
|
|
|
|
|
real_logger(DEBUG_ALWAYS, LOG_ERR, message);
|
|
|
|
}
|
2019-08-26 11:44:36 +00:00
|
|
|
|
|
|
|
void openlogger(const char *ident, logmode_t mode) {
|
|
|
|
logident = ident;
|
|
|
|
logmode = mode;
|
2019-08-26 11:44:49 +00:00
|
|
|
|
2019-08-26 11:44:36 +00:00
|
|
|
switch(mode) {
|
2019-08-26 11:44:53 +00:00
|
|
|
case LOGMODE_STDERR:
|
|
|
|
logpid = getpid();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOGMODE_FILE:
|
|
|
|
logpid = getpid();
|
|
|
|
logfile = fopen(logfilename, "a");
|
|
|
|
|
|
|
|
if(!logfile) {
|
|
|
|
fprintf(stderr, "Could not open log file %s: %s\n", logfilename, strerror(errno));
|
|
|
|
logmode = LOGMODE_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOGMODE_SYSLOG:
|
2019-08-26 11:44:36 +00:00
|
|
|
#ifdef HAVE_MINGW
|
2019-08-26 11:44:53 +00:00
|
|
|
loghandle = RegisterEventSource(NULL, logident);
|
|
|
|
|
|
|
|
if(!loghandle) {
|
|
|
|
fprintf(stderr, "Could not open log handle!\n");
|
|
|
|
logmode = LOGMODE_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2019-08-26 11:44:36 +00:00
|
|
|
#else
|
|
|
|
#ifdef HAVE_SYSLOG_H
|
2019-08-26 11:44:53 +00:00
|
|
|
openlog(logident, LOG_CONS | LOG_PID, LOG_DAEMON);
|
|
|
|
break;
|
2019-08-26 11:44:36 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
2019-08-26 11:44:53 +00:00
|
|
|
|
|
|
|
case LOGMODE_NULL:
|
|
|
|
break;
|
2019-08-26 11:44:36 +00:00
|
|
|
}
|
2019-08-26 11:44:49 +00:00
|
|
|
|
2019-08-26 11:44:53 +00:00
|
|
|
if(logmode != LOGMODE_NULL) {
|
2019-08-26 11:44:49 +00:00
|
|
|
sptps_log = sptps_logger;
|
2019-08-26 11:44:53 +00:00
|
|
|
} else {
|
2019-08-26 11:44:49 +00:00
|
|
|
sptps_log = sptps_log_quiet;
|
2019-08-26 11:44:53 +00:00
|
|
|
}
|
2019-08-26 11:44:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-26 11:44:41 +00:00
|
|
|
void reopenlogger() {
|
2019-08-26 11:44:53 +00:00
|
|
|
if(logmode != LOGMODE_FILE) {
|
2019-08-26 11:44:41 +00:00
|
|
|
return;
|
2019-08-26 11:44:53 +00:00
|
|
|
}
|
2019-08-26 11:44:41 +00:00
|
|
|
|
|
|
|
fflush(logfile);
|
|
|
|
FILE *newfile = fopen(logfilename, "a");
|
2019-08-26 11:44:53 +00:00
|
|
|
|
2019-08-26 11:44:41 +00:00
|
|
|
if(!newfile) {
|
2019-08-26 11:44:49 +00:00
|
|
|
logger(DEBUG_ALWAYS, LOG_ERR, "Unable to reopen log file %s: %s", logfilename, strerror(errno));
|
2019-08-26 11:44:41 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-08-26 11:44:53 +00:00
|
|
|
|
2019-08-26 11:44:41 +00:00
|
|
|
fclose(logfile);
|
|
|
|
logfile = newfile;
|
|
|
|
}
|
|
|
|
|
2019-08-26 11:44:36 +00:00
|
|
|
|
|
|
|
void closelogger(void) {
|
|
|
|
switch(logmode) {
|
2019-08-26 11:44:53 +00:00
|
|
|
case LOGMODE_FILE:
|
|
|
|
fclose(logfile);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOGMODE_SYSLOG:
|
2019-08-26 11:44:36 +00:00
|
|
|
#ifdef HAVE_MINGW
|
2019-08-26 11:44:53 +00:00
|
|
|
DeregisterEventSource(loghandle);
|
|
|
|
break;
|
2019-08-26 11:44:36 +00:00
|
|
|
#else
|
|
|
|
#ifdef HAVE_SYSLOG_H
|
2019-08-26 11:44:53 +00:00
|
|
|
closelog();
|
|
|
|
break;
|
2019-08-26 11:44:36 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
2019-08-26 11:44:53 +00:00
|
|
|
|
|
|
|
case LOGMODE_NULL:
|
|
|
|
case LOGMODE_STDERR:
|
|
|
|
break;
|
2019-08-26 11:44:36 +00:00
|
|
|
}
|
|
|
|
}
|