summaryrefslogtreecommitdiff
path: root/ctdb/server/ctdb_logging_syslog.c
blob: 6a3fd02c1622a1fd7bc8dedab71ee220586b9790 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
   ctdb logging code - syslog backend

   Copyright (C) Andrew Tridgell  2008
   Copyright (C) Martin Schwenke  2014

   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 3 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.

   You should have received a copy of the GNU General Public License
   along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include "replace.h"
#include "system/network.h"
#include "system/syslog.h"
#include "lib/util/debug.h"
#include "lib/util/blocking.h"
#include "ctdb_logging.h"

/* Linux and FreeBSD define this appropriately - try good old /dev/log
 * for anything that doesn't... */
#ifndef _PATH_LOG
#define _PATH_LOG "/dev/log"
#endif

#define CTDB_LOG_SYSLOG_PREFIX "syslog"
#define CTDB_SYSLOG_FACILITY LOG_USER

struct ctdb_syslog_sock_state {
	int fd;
	const char *app_name;
};

/**********************************************************************/

static int ctdb_debug_to_syslog_level(int dbglevel)
{
	int level;

	switch (dbglevel) {
	case DEBUG_ERR:
		level = LOG_ERR;
		break;
	case DEBUG_WARNING:
		level = LOG_WARNING;
		break;
	case DEBUG_NOTICE:
		level = LOG_NOTICE;
		break;
	case DEBUG_INFO:
		level = LOG_INFO;
		break;
	default:
		level = LOG_DEBUG;
		break;
	}

	return level;
}

/**********************************************************************/

/* Format messages as per RFC3164. */

/* It appears that some syslog daemon implementations do not allow a
 * hostname when messages are sent via a Unix domain socket, so omit
 * it.  A timestamp could be sent but rsyslogd on Linux limits the
 * timestamp logged to the precision that was received on /dev/log.
 * It seems sane to send degenerate RFC3164 messages without a header
 * at all, so that the daemon will generate high resolution timestamps
 * if configured. */
static int format_rfc3164(int dbglevel, struct ctdb_syslog_sock_state *state,
			  const char *str, char *buf, int bsize)
{
	int pri;
	int len;

	pri = CTDB_SYSLOG_FACILITY | ctdb_debug_to_syslog_level(dbglevel);
	len = snprintf(buf, bsize, "<%d>%s[%u]: %s%s",
		       pri, state->app_name, getpid(), debug_extra, str);
	len = MIN(len, bsize - 1);

	return len;
}

/**********************************************************************/

/* Non-blocking logging */

static void ctdb_log_to_syslog_sock(void *private_ptr,
				    int dbglevel, const char *str)
{
	struct ctdb_syslog_sock_state *state = talloc_get_type(
		private_ptr, struct ctdb_syslog_sock_state);

	/* RFC3164 says: The total length of the packet MUST be 1024
	   bytes or less. */
	char buf[1024];
	int n;

	n = format_rfc3164(dbglevel, state, str, buf, sizeof(buf));
	if (n == -1) {
		fprintf(stderr, "Failed to format syslog message %s\n", str);
		return;
	}

	/* Could extend this to count failures, which probably
	 * indicate dropped messages due to EAGAIN or EWOULDBLOCK */
	(void)send(state->fd, buf, n, 0);
}

static int
ctdb_syslog_sock_state_destructor(struct ctdb_syslog_sock_state *state)
{
	if (state->fd != -1) {
		close(state->fd);
		state->fd = -1;
	}
	return 0;
}

static int ctdb_log_setup_syslog_un(TALLOC_CTX *mem_ctx,
				    const char *app_name)
{
	struct ctdb_syslog_sock_state *state;
	struct sockaddr_un dest;
	int ret;

	state = talloc_zero(mem_ctx, struct ctdb_syslog_sock_state);
	if (state == NULL) {
		return ENOMEM;
	}

	state->fd = socket(AF_UNIX, SOCK_DGRAM, 0);
	if (state->fd == -1) {
		int save_errno = errno;
		talloc_free(state);
		return save_errno;
	}

	dest.sun_family = AF_UNIX;
	strncpy(dest.sun_path, _PATH_LOG, sizeof(dest.sun_path)-1);
	ret = connect(state->fd,
		      (struct sockaddr *)&dest, sizeof(dest));
	if (ret == -1) {
		int save_errno = errno;
		talloc_free(state);
		return save_errno;
	}
	set_blocking(state->fd, false);

	state->app_name = app_name;

	talloc_set_destructor(state, ctdb_syslog_sock_state_destructor);
	debug_set_callback(state, ctdb_log_to_syslog_sock);

	return 0;
}

/**********************************************************************/

static void ctdb_log_to_syslog(void *private_ptr, int dbglevel, const char *s)
{
	syslog(ctdb_debug_to_syslog_level(dbglevel),
	       "%s%s", debug_extra, s);
}

static int ctdb_log_setup_syslog(TALLOC_CTX *mem_ctx,
				 const char *logging,
				 const char *app_name)
{
	size_t l = strlen(CTDB_LOG_SYSLOG_PREFIX);

	if (logging[l] != '\0') {
		/* Handle non-blocking extensions here */
		const char *method;

		if (logging[l] != ':') {
			return EINVAL;
		}
		method = &logging[0] + l + 1;
		if (strcmp(method, "nonblocking") == 0) {
			ctdb_log_setup_syslog_un(mem_ctx, app_name);
			return 0;
		}

		return EINVAL;
	}

	debug_set_callback(NULL, ctdb_log_to_syslog);
	return 0;
}

void ctdb_log_init_syslog(void)
{
	ctdb_log_register_backend(CTDB_LOG_SYSLOG_PREFIX,
				  ctdb_log_setup_syslog);
}