Support ToS/DiffServ for IPv6 meta and UDP connections.
[tinc] / src / logger.c
1 /*
2     logger.c -- logging code
3     Copyright (C) 2004-2015 Guus Sliepen <guus@tinc-vpn.org>
4                   2004-2005 Ivo Timmermans
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "system.h"
22
23 #include "conf.h"
24 #include "meta.h"
25 #include "names.h"
26 #include "logger.h"
27 #include "connection.h"
28 #include "control_common.h"
29 #include "process.h"
30 #include "sptps.h"
31
32 debug_t debug_level = DEBUG_NOTHING;
33 static logmode_t logmode = LOGMODE_STDERR;
34 static pid_t logpid;
35 static FILE *logfile = NULL;
36 #ifdef HAVE_MINGW
37 static HANDLE loghandle = NULL;
38 #endif
39 static const char *logident = NULL;
40 bool logcontrol = false;
41 int umbilical = 0;
42
43 static void real_logger(int level, int priority, const char *message) {
44         char timestr[32] = "";
45         static bool suppress = false;
46
47         // Bail out early if there is nothing to do.
48         if(suppress)
49                 return;
50
51         if(!logcontrol && (level > debug_level || logmode == LOGMODE_NULL))
52                 return;
53
54         if(level <= debug_level) {
55                 switch(logmode) {
56                         case LOGMODE_STDERR:
57                                 fprintf(stderr, "%s\n", message);
58                                 fflush(stderr);
59                                 break;
60                         case LOGMODE_FILE:
61                                 if(!now.tv_sec)
62                                         gettimeofday(&now, NULL);
63                                 time_t now_sec = now.tv_sec;
64                                 strftime(timestr, sizeof timestr, "%Y-%m-%d %H:%M:%S", localtime(&now_sec));
65                                 fprintf(logfile, "%s %s[%ld]: %s\n", timestr, logident, (long)logpid, message);
66                                 fflush(logfile);
67                                 break;
68                         case LOGMODE_SYSLOG:
69 #ifdef HAVE_MINGW
70                                 {
71                                         const char *messages[] = {message};
72                                         ReportEvent(loghandle, priority, 0, 0, NULL, 1, 0, messages, NULL);
73                                 }
74 #else
75 #ifdef HAVE_SYSLOG_H
76                                 syslog(priority, "%s", message);
77 #endif
78 #endif
79                                 break;
80                         case LOGMODE_NULL:
81                                 break;
82                 }
83
84                 if(umbilical && do_detach) {
85                         write(umbilical, message, strlen(message));
86                         write(umbilical, "\n", 1);
87                 }
88         }
89
90         if(logcontrol) {
91                 suppress = true;
92                 logcontrol = false;
93                 for list_each(connection_t, c, connection_list) {
94                         if(!c->status.log)
95                                 continue;
96                         logcontrol = true;
97                         if(level > (c->outcompression >= 0 ? c->outcompression : debug_level))
98                                 continue;
99                         int len = strlen(message);
100                         if(send_request(c, "%d %d %d", CONTROL, REQ_LOG, len))
101                                 send_meta(c, message, len);
102                 }
103                 suppress = false;
104         }
105 }
106
107 void logger(int level, int priority, const char *format, ...) {
108         va_list ap;
109         char message[1024] = "";
110
111         va_start(ap, format);
112         int len = vsnprintf(message, sizeof message, format, ap);
113         va_end(ap);
114
115         if(len > 0 && len < sizeof message && message[len - 1] == '\n')
116                 message[len - 1] = 0;
117
118         real_logger(level, priority, message);
119 }
120
121 static void sptps_logger(sptps_t *s, int s_errno, const char *format, va_list ap) {
122         char message[1024] = "";
123         size_t msglen = sizeof message;
124
125         int len = vsnprintf(message, msglen, format, ap);
126         if(len > 0 && len < sizeof message) {
127                 if(message[len - 1] == '\n')
128                         message[--len] = 0;
129
130                 // WARNING: s->handle can point to a connection_t or a node_t,
131                 // but both types have the name and hostname fields at the same offsets.
132                 connection_t *c = s->handle;
133                 if(c)
134                         snprintf(message + len, sizeof message - len, " from %s (%s)", c->name, c->hostname);
135         }
136
137         real_logger(DEBUG_ALWAYS, LOG_ERR, message);
138 }
139
140 void openlogger(const char *ident, logmode_t mode) {
141         logident = ident;
142         logmode = mode;
143
144         switch(mode) {
145                 case LOGMODE_STDERR:
146                         logpid = getpid();
147                         break;
148                 case LOGMODE_FILE:
149                         logpid = getpid();
150                         logfile = fopen(logfilename, "a");
151                         if(!logfile) {
152                                 fprintf(stderr, "Could not open log file %s: %s\n", logfilename, strerror(errno));
153                                 logmode = LOGMODE_NULL;
154                         }
155                         break;
156                 case LOGMODE_SYSLOG:
157 #ifdef HAVE_MINGW
158                         loghandle = RegisterEventSource(NULL, logident);
159                         if(!loghandle) {
160                                 fprintf(stderr, "Could not open log handle!");
161                                 logmode = LOGMODE_NULL;
162                         }
163                         break;
164 #else
165 #ifdef HAVE_SYSLOG_H
166                         openlog(logident, LOG_CONS | LOG_PID, LOG_DAEMON);
167                         break;
168 #endif
169 #endif
170                 case LOGMODE_NULL:
171                         break;
172         }
173
174         if(logmode != LOGMODE_NULL)
175                 sptps_log = sptps_logger;
176         else
177                 sptps_log = sptps_log_quiet;
178 }
179
180 void reopenlogger() {
181         if(logmode != LOGMODE_FILE)
182                 return;
183
184         fflush(logfile);
185         FILE *newfile = fopen(logfilename, "a");
186         if(!newfile) {
187                 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to reopen log file %s: %s", logfilename, strerror(errno));
188                 return;
189         }
190         fclose(logfile);
191         logfile = newfile;
192 }
193
194
195 void closelogger(void) {
196         switch(logmode) {
197                 case LOGMODE_FILE:
198                         fclose(logfile);
199                         break;
200                 case LOGMODE_SYSLOG:
201 #ifdef HAVE_MINGW
202                         DeregisterEventSource(loghandle);
203                         break;
204 #else
205 #ifdef HAVE_SYSLOG_H
206                         closelog();
207                         break;
208 #endif
209 #endif
210                 case LOGMODE_NULL:
211                 case LOGMODE_STDERR:
212                         break;
213         }
214 }