Ensure "tinc start" knows if the daemon really started succesfully.
[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 "sptps.h"
30
31 debug_t debug_level = DEBUG_NOTHING;
32 static logmode_t logmode = LOGMODE_STDERR;
33 static pid_t logpid;
34 static FILE *logfile = NULL;
35 #ifdef HAVE_MINGW
36 static HANDLE loghandle = NULL;
37 #endif
38 static const char *logident = NULL;
39 bool logcontrol = false;
40 int umbilical = 0;
41
42 static void real_logger(int level, int priority, const char *message) {
43         char timestr[32] = "";
44         static bool suppress = false;
45
46         // Bail out early if there is nothing to do.
47         if(suppress)
48                 return;
49
50         if(!logcontrol && (level > debug_level || logmode == LOGMODE_NULL))
51                 return;
52
53         if(level <= debug_level) {
54                 switch(logmode) {
55                         case LOGMODE_STDERR:
56                                 fprintf(stderr, "%s\n", message);
57                                 fflush(stderr);
58                                 break;
59                         case LOGMODE_FILE:
60                                 if(!now.tv_sec)
61                                         gettimeofday(&now, NULL);
62                                 time_t now_sec = now.tv_sec;
63                                 strftime(timestr, sizeof timestr, "%Y-%m-%d %H:%M:%S", localtime(&now_sec));
64                                 fprintf(logfile, "%s %s[%ld]: %s\n", timestr, logident, (long)logpid, message);
65                                 fflush(logfile);
66                                 break;
67                         case LOGMODE_SYSLOG:
68 #ifdef HAVE_MINGW
69                                 {
70                                         const char *messages[] = {message};
71                                         ReportEvent(loghandle, priority, 0, 0, NULL, 1, 0, messages, NULL);
72                                 }
73 #else
74 #ifdef HAVE_SYSLOG_H
75                                 syslog(priority, "%s", message);
76 #endif
77 #endif
78                                 break;
79                         case LOGMODE_NULL:
80                                 break;
81                 }
82
83                 if(umbilical) {
84                         write(umbilical, message, strlen(message));
85                         write(umbilical, "\n", 1);
86                 }
87         }
88
89         if(logcontrol) {
90                 suppress = true;
91                 logcontrol = false;
92                 for list_each(connection_t, c, connection_list) {
93                         if(!c->status.log)
94                                 continue;
95                         logcontrol = true;
96                         if(level > (c->outcompression >= 0 ? c->outcompression : debug_level))
97                                 continue;
98                         int len = strlen(message);
99                         if(send_request(c, "%d %d %d", CONTROL, REQ_LOG, len))
100                                 send_meta(c, message, len);
101                 }
102                 suppress = false;
103         }
104 }
105
106 void logger(int level, int priority, const char *format, ...) {
107         va_list ap;
108         char message[1024] = "";
109
110         va_start(ap, format);
111         int len = vsnprintf(message, sizeof message, format, ap);
112         va_end(ap);
113
114         if(len > 0 && len < sizeof message && message[len - 1] == '\n')
115                 message[len - 1] = 0;
116
117         real_logger(level, priority, message);
118 }
119
120 static void sptps_logger(sptps_t *s, int s_errno, const char *format, va_list ap) {
121         char message[1024] = "";
122         size_t msglen = sizeof message;
123
124         int len = vsnprintf(message, msglen, format, ap);
125         if(len > 0 && len < sizeof message) {
126                 if(message[len - 1] == '\n')
127                         message[--len] = 0;
128
129                 connection_t *c = s->handle;
130                 if(c)
131                         snprintf(message + len, sizeof message - len, " from %s (%s)", c->name, c->hostname);
132         }
133
134         real_logger(DEBUG_ALWAYS, LOG_ERR, message);
135 }
136
137 void openlogger(const char *ident, logmode_t mode) {
138         logident = ident;
139         logmode = mode;
140
141         switch(mode) {
142                 case LOGMODE_STDERR:
143                         logpid = getpid();
144                         break;
145                 case LOGMODE_FILE:
146                         logpid = getpid();
147                         logfile = fopen(logfilename, "a");
148                         if(!logfile) {
149                                 fprintf(stderr, "Could not open log file %s: %s\n", logfilename, strerror(errno));
150                                 logmode = LOGMODE_NULL;
151                         }
152                         break;
153                 case LOGMODE_SYSLOG:
154 #ifdef HAVE_MINGW
155                         loghandle = RegisterEventSource(NULL, logident);
156                         if(!loghandle) {
157                                 fprintf(stderr, "Could not open log handle!");
158                                 logmode = LOGMODE_NULL;
159                         }
160                         break;
161 #else
162 #ifdef HAVE_SYSLOG_H
163                         openlog(logident, LOG_CONS | LOG_PID, LOG_DAEMON);
164                         break;
165 #endif
166 #endif
167                 case LOGMODE_NULL:
168                         break;
169         }
170
171         if(logmode != LOGMODE_NULL)
172                 sptps_log = sptps_logger;
173         else
174                 sptps_log = sptps_log_quiet;
175 }
176
177 void reopenlogger() {
178         if(logmode != LOGMODE_FILE)
179                 return;
180
181         fflush(logfile);
182         FILE *newfile = fopen(logfilename, "a");
183         if(!newfile) {
184                 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to reopen log file %s: %s", logfilename, strerror(errno));
185                 return;
186         }
187         fclose(logfile);
188         logfile = newfile;
189 }
190
191
192 void closelogger(void) {
193         switch(logmode) {
194                 case LOGMODE_FILE:
195                         fclose(logfile);
196                         break;
197                 case LOGMODE_SYSLOG:
198 #ifdef HAVE_MINGW
199                         DeregisterEventSource(loghandle);
200                         break;
201 #else
202 #ifdef HAVE_SYSLOG_H
203                         closelog();
204                         break;
205 #endif
206 #endif
207                 case LOGMODE_NULL:
208                 case LOGMODE_STDERR:
209                         break;
210                         break;
211         }
212 }