Don't set up an ongoing connection to myself.
[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                 connection_t *c = s->handle;
131                 if(c)
132                         snprintf(message + len, sizeof message - len, " from %s (%s)", c->name, c->hostname);
133         }
134
135         real_logger(DEBUG_ALWAYS, LOG_ERR, message);
136 }
137
138 void openlogger(const char *ident, logmode_t mode) {
139         logident = ident;
140         logmode = mode;
141
142         switch(mode) {
143                 case LOGMODE_STDERR:
144                         logpid = getpid();
145                         break;
146                 case LOGMODE_FILE:
147                         logpid = getpid();
148                         logfile = fopen(logfilename, "a");
149                         if(!logfile) {
150                                 fprintf(stderr, "Could not open log file %s: %s\n", logfilename, strerror(errno));
151                                 logmode = LOGMODE_NULL;
152                         }
153                         break;
154                 case LOGMODE_SYSLOG:
155 #ifdef HAVE_MINGW
156                         loghandle = RegisterEventSource(NULL, logident);
157                         if(!loghandle) {
158                                 fprintf(stderr, "Could not open log handle!");
159                                 logmode = LOGMODE_NULL;
160                         }
161                         break;
162 #else
163 #ifdef HAVE_SYSLOG_H
164                         openlog(logident, LOG_CONS | LOG_PID, LOG_DAEMON);
165                         break;
166 #endif
167 #endif
168                 case LOGMODE_NULL:
169                         break;
170         }
171
172         if(logmode != LOGMODE_NULL)
173                 sptps_log = sptps_logger;
174         else
175                 sptps_log = sptps_log_quiet;
176 }
177
178 void reopenlogger() {
179         if(logmode != LOGMODE_FILE)
180                 return;
181
182         fflush(logfile);
183         FILE *newfile = fopen(logfilename, "a");
184         if(!newfile) {
185                 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to reopen log file %s: %s", logfilename, strerror(errno));
186                 return;
187         }
188         fclose(logfile);
189         logfile = newfile;
190 }
191
192
193 void closelogger(void) {
194         switch(logmode) {
195                 case LOGMODE_FILE:
196                         fclose(logfile);
197                         break;
198                 case LOGMODE_SYSLOG:
199 #ifdef HAVE_MINGW
200                         DeregisterEventSource(loghandle);
201                         break;
202 #else
203 #ifdef HAVE_SYSLOG_H
204                         closelog();
205                         break;
206 #endif
207 #endif
208                 case LOGMODE_NULL:
209                 case LOGMODE_STDERR:
210                         break;
211                         break;
212         }
213 }