eventscript: pass struct ctdb_log_state directly to ctdb_log_handler().
[sahlberg/ctdb.git] / server / ctdb_logging.c
1 /* 
2    ctdb logging code
3
4    Copyright (C) Andrew Tridgell  2008
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 3 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
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "lib/events/events.h"
22 #include "../include/ctdb.h"
23 #include "../include/ctdb_private.h"
24 #include "system/syslog.h"
25 #include "system/time.h"
26 #include "system/filesys.h"
27
28 struct syslog_message {
29         uint32_t level;
30         uint32_t len;
31         char message[1];
32 };
33
34
35 struct ctdb_syslog_state {
36         int syslog_fd;
37         int fd[2];
38 };
39
40 static int syslogd_is_started = 0;
41
42
43 /* called when child is finished
44  * this is for the syslog daemon, we can not use DEBUG here
45  */
46 static void ctdb_syslog_handler(struct event_context *ev, struct fd_event *fde, 
47                                       uint16_t flags, void *p)
48 {
49         struct ctdb_syslog_state *state = talloc_get_type(p, struct ctdb_syslog_state);
50
51         int count;
52         char str[65536];
53         struct syslog_message *msg;
54
55         if (state == NULL) {
56                 return;
57         }
58
59         count = recv(state->syslog_fd, str, sizeof(str), 0);
60         if (count < sizeof(struct syslog_message)) {
61                 return;
62         }
63         msg = (struct syslog_message *)str;
64
65         syslog(msg->level, "%s", msg->message);
66 }
67
68
69 /* called when the pipd from the main daemon has closed
70  * this is for the syslog daemon, we can not use DEBUG here
71  */
72 static void ctdb_syslog_terminate_handler(struct event_context *ev, struct fd_event *fde, 
73                                       uint16_t flags, void *p)
74 {
75         syslog(LOG_ERR, "Shutting down SYSLOG daemon with pid:%d", (int)getpid());
76         _exit(0);
77 }
78
79
80
81 /*
82  * this is for the syslog daemon, we can not use DEBUG here
83  */
84 int start_syslog_daemon(struct ctdb_context *ctdb)
85 {
86         struct sockaddr_in syslog_sin;
87         struct ctdb_syslog_state *state;
88
89         state = talloc(ctdb, struct ctdb_syslog_state);
90         CTDB_NO_MEMORY(ctdb, state);
91
92         if (pipe(state->fd) != 0) {
93                 printf("Failed to create syslog pipe\n");
94                 talloc_free(state);
95                 return -1;
96         }
97         
98         ctdb->syslogd_pid = fork();
99         if (ctdb->syslogd_pid == (pid_t)-1) {
100                 printf("Failed to create syslog child process\n");
101                 close(state->fd[0]);
102                 close(state->fd[1]);
103                 talloc_free(state);
104                 return -1;
105         }
106
107         syslogd_is_started = 1;
108
109         if (ctdb->syslogd_pid != 0) {
110                 DEBUG(DEBUG_ERR,("Starting SYSLOG child process with pid:%d\n", (int)ctdb->syslogd_pid));
111
112                 close(state->fd[1]);
113                 set_close_on_exec(state->fd[0]);
114
115                 return 0;
116         }
117
118         talloc_free(ctdb->ev);
119         ctdb->ev = event_context_init(NULL);
120
121         syslog(LOG_ERR, "Starting SYSLOG daemon with pid:%d", (int)getpid());
122
123         close(state->fd[0]);
124         event_add_fd(ctdb->ev, state, state->fd[1], EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
125                      ctdb_syslog_terminate_handler, state);
126
127         state->syslog_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
128         if (state->syslog_fd == -1) {
129                 printf("Failed to create syslog socket\n");
130                 return -1;
131         }
132
133         syslog_sin.sin_family = AF_INET;
134         syslog_sin.sin_port   = htons(CTDB_PORT);
135         syslog_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);    
136
137         if (bind(state->syslog_fd, &syslog_sin, sizeof(syslog_sin)) == -1) {
138                 if (errno == EADDRINUSE) {
139                         /* this is ok, we already have a syslog daemon */
140                         _exit(0);
141                 }
142                 printf("syslog daemon failed to bind to socket. errno:%d(%s)\n", errno, strerror(errno));
143                 _exit(10);
144         }
145
146
147         event_add_fd(ctdb->ev, state, state->syslog_fd, EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
148                      ctdb_syslog_handler, state);
149
150         event_loop_wait(ctdb->ev);
151
152         /* this should not happen */
153         _exit(10);
154 }
155
156 struct ctdb_log_state {
157         struct ctdb_context *ctdb;
158         int fd, pfd;
159         char buf[1024];
160         uint16_t buf_used;
161         bool use_syslog;
162 };
163
164 /* we need this global to keep the DEBUG() syntax */
165 static struct ctdb_log_state *log_state;
166
167 /*
168   syslog logging function
169  */
170 static void ctdb_syslog_log(const char *format, va_list ap)
171 {
172         struct syslog_message *msg;
173         int level = LOG_DEBUG;
174         char *s = NULL;
175         int len, ret;
176         int syslog_fd;
177         struct sockaddr_in syslog_sin;
178
179         ret = vasprintf(&s, format, ap);
180         if (ret == -1) {
181                 return;
182         }
183
184         switch (this_log_level) {
185         case DEBUG_EMERG: 
186                 level = LOG_EMERG; 
187                 break;
188         case DEBUG_ALERT: 
189                 level = LOG_ALERT; 
190                 break;
191         case DEBUG_CRIT: 
192                 level = LOG_CRIT; 
193                 break;
194         case DEBUG_ERR: 
195                 level = LOG_ERR; 
196                 break;
197         case DEBUG_WARNING: 
198                 level = LOG_WARNING; 
199                 break;
200         case DEBUG_NOTICE: 
201                 level = LOG_NOTICE;
202                 break;
203         case DEBUG_INFO: 
204                 level = LOG_INFO;
205                 break;
206         default:
207                 level = LOG_DEBUG;
208                 break;          
209         }
210
211         len = offsetof(struct syslog_message, message) + strlen(s) + 1;
212         msg = malloc(len);
213         if (msg == NULL) {
214                 free(s);
215                 return;
216         }
217         msg->level = level;
218         msg->len   = strlen(s);
219         strcpy(msg->message, s);
220
221         if (syslogd_is_started == 0) {
222                 syslog(msg->level, "%s", msg->message);
223         } else {
224                 syslog_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
225                 if (syslog_fd == -1) {
226                         printf("Failed to create syslog socket\n");
227                         free(s);
228                         free(msg);
229                         return;
230                 }
231
232                 syslog_sin.sin_family = AF_INET;
233                 syslog_sin.sin_port   = htons(CTDB_PORT);
234                 syslog_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);    
235
236        
237                 ret = sendto(syslog_fd, msg, len, 0, &syslog_sin, sizeof(syslog_sin));
238                 /* no point in checking here since we cant log an error */
239
240                 close(syslog_fd);
241         }
242
243         free(s);
244         free(msg);
245 }
246
247
248 /*
249   log file logging function
250  */
251 static void ctdb_logfile_log(const char *format, va_list ap)
252 {
253         struct timeval t;
254         char *s = NULL;
255         struct tm *tm;
256         char tbuf[100];
257         char *s2 = NULL;
258         int ret;
259
260         ret = vasprintf(&s, format, ap);
261         if (ret == -1) {
262                 const char *errstr = "vasprintf failed\n";
263
264                 write(log_state->fd, errstr, strlen(errstr));
265                 return;
266         }
267
268         t = timeval_current();
269         tm = localtime(&t.tv_sec);
270
271         strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
272
273         ret = asprintf(&s2, "%s.%06u [%5u]: %s",
274                  tbuf, (unsigned)t.tv_usec, (unsigned)getpid(), s);
275         free(s);
276         if (ret == -1) {
277                 const char *errstr = "asprintf failed\n";
278                 write(log_state->fd, errstr, strlen(errstr));
279                 return;
280         }
281         if (s2) {
282                 write(log_state->fd, s2, strlen(s2));
283                 free(s2);
284         }
285 }
286
287 static void ctdb_logfile_log_add(const char *format, va_list ap)
288 {
289         char *s = NULL;
290         int ret;
291
292         ret = vasprintf(&s, format, ap);
293         if (ret == -1) {
294                 const char *errstr = "vasprintf failed\n";
295
296                 write(log_state->fd, errstr, strlen(errstr));
297                 return;
298         }
299
300         if (s) {
301                 write(log_state->fd, s, strlen(s));
302                 free(s);
303         }
304 }
305
306
307
308 /*
309   choose the logfile location
310 */
311 int ctdb_set_logfile(struct ctdb_context *ctdb, const char *logfile, bool use_syslog)
312 {
313         int ret;
314
315         ctdb->log = talloc_zero(ctdb, struct ctdb_log_state);
316         if (ctdb->log == NULL) {
317                 printf("talloc_zero failed\n");
318                 abort();
319         }
320
321         ctdb->log->ctdb = ctdb;
322         log_state = ctdb->log;
323
324         if (use_syslog) {
325                 do_debug_v = ctdb_syslog_log;
326                 do_debug_add_v = ctdb_syslog_log;
327                 ctdb->log->use_syslog = true;
328         } else if (logfile == NULL || strcmp(logfile, "-") == 0) {
329                 do_debug_v = ctdb_logfile_log;
330                 do_debug_add_v = ctdb_logfile_log_add;
331                 ctdb->log->fd = 1;
332                 /* also catch stderr of subcommands to stdout */
333                 ret = dup2(1, 2);
334                 if (ret == -1) {
335                         printf("dup2 failed: %s\n", strerror(errno));
336                         abort();
337                 }
338         } else {
339                 do_debug_v = ctdb_logfile_log;
340                 do_debug_add_v = ctdb_logfile_log_add;
341
342                 ctdb->log->fd = open(logfile, O_WRONLY|O_APPEND|O_CREAT, 0666);
343                 if (ctdb->log->fd == -1) {
344                         printf("Failed to open logfile %s\n", logfile);
345                         abort();
346                 }
347         }
348
349         return 0;
350 }
351
352
353
354 /*
355   called when log data comes in from a child process
356  */
357 static void ctdb_log_handler(struct event_context *ev, struct fd_event *fde, 
358                              uint16_t flags, void *private)
359 {
360         struct ctdb_log_state *log = talloc_get_type(private, struct ctdb_log_state);
361         char *p;
362         int n;
363
364         if (!(flags & EVENT_FD_READ)) {
365                 return;
366         }
367         
368         n = read(log->pfd, &log->buf[log->buf_used],
369                  sizeof(log->buf) - log->buf_used);
370         if (n > 0) {
371                 log->buf_used += n;
372         }
373
374         this_log_level = script_log_level;
375
376         while (log->buf_used > 0 &&
377                (p = memchr(log->buf, '\n', log->buf_used)) != NULL) {
378                 int n1 = (p - log->buf)+1;
379                 int n2 = n1 - 1;
380                 /* swallow \r from child processes */
381                 if (n2 > 0 && log->buf[n2-1] == '\r') {
382                         n2--;
383                 }
384                 if (script_log_level <= LogLevel) {
385                         do_debug("%*.*s\n", n2, n2, log->buf);
386                         /* log it in the eventsystem as well */
387                         ctdb_log_event_script_output(log->ctdb, log->buf, n2);
388                 }
389                 memmove(log->buf, p+1, sizeof(log->buf) - n1);
390                 log->buf_used -= n1;
391         }
392
393         /* the buffer could have completely filled - unfortunately we have
394            no choice but to dump it out straight away */
395         if (log->buf_used == sizeof(log->buf)) {
396                 if (script_log_level <= LogLevel) {
397                         do_debug("%*.*s\n", 
398                                 (int)log->buf_used, (int)log->buf_used, log->buf);
399                         /* log it in the eventsystem as well */
400                         ctdb_log_event_script_output(log->ctdb, log->buf, log->buf_used);
401                 }
402                 log->buf_used = 0;
403         }
404 }
405
406
407
408 /*
409   setup for logging of child process stdout
410 */
411 int ctdb_set_child_logging(struct ctdb_context *ctdb)
412 {
413         int p[2];
414         int ret;
415
416         if (ctdb->log->fd == 1) {
417                 /* not needed for stdout logging */
418                 return 0;
419         }
420
421         /* setup a pipe to catch IO from subprocesses */
422         if (pipe(p) != 0) {
423                 DEBUG(DEBUG_ERR,(__location__ " Failed to setup for child logging pipe\n"));
424                 return -1;
425         }
426
427         event_add_fd(ctdb->ev, ctdb, p[0], EVENT_FD_READ, 
428                      ctdb_log_handler, ctdb->log);
429         set_close_on_exec(p[0]);
430         ctdb->log->pfd = p[0];
431
432         DEBUG(DEBUG_NOTICE, (__location__ " Created PIPE FD:%d for logging\n", p[0]));
433
434         close(1);
435         close(2);
436         if (p[1] != 1) {
437                 ret = dup2(p[1], 1);
438                 if (ret == -1) {
439                         printf("dup2 failed: %s\n", strerror(errno));
440                         return -1;
441                 }
442                 close(p[1]);
443         }
444         /* also catch stderr of subcommands to the log */
445         ret = dup2(1, 2);
446         if (ret == -1) {
447                 printf("dup2 failed: %s\n", strerror(errno));
448                 return -1;
449         }
450
451         return 0;
452 }
453
454
455
456
457