[syslog-ng] [PATCH] afsocket: Support a smooth upgrade path to recent systemd
Gergely Nagy
algernon at balabit.hu
Fri May 4 14:07:39 CEST 2012
When the journal was introduced in systemd, it started to pass
/run/systemd/journal/syslog to syslogds (it was passing down /dev/log
before). Since we verify that the FD to open matches the expected
filename, we didn't pick up the fd due to the filename mismatch.
This can be fixed on the system() source level, where it can decide
which to generate into the config. However, for setups that do not use
system() for one reason or the other, this does not work.
For these systems that do not use the system() source, the best
solution is to try and replace /dev/log with
/run/systemd/journal/syslog at run-time. This patch does just that,
and ties it to config versions 3.4 or earlier.
This means that post-3.4, the automatic replacement will stop working,
and users will get the generic failed to acquire socket error message
instead.
Signed-off-by: Gergely Nagy <algernon at balabit.hu>
---
modules/afsocket/afunix.c | 56 ++++++++++++++++++++++++++++++++++-----------
1 file changed, 43 insertions(+), 13 deletions(-)
diff --git a/modules/afsocket/afunix.c b/modules/afsocket/afunix.c
index 8145f1a..a5644ac 100644
--- a/modules/afsocket/afunix.c
+++ b/modules/afsocket/afunix.c
@@ -72,7 +72,8 @@ afunix_sd_set_perm(LogDriver *s, gint perm)
#if ENABLE_SYSTEMD
static gboolean
-afunix_sd_acquire_socket(AFSocketSourceDriver *s, gint *result_fd)
+afunix_sd_acquire_named_socket(AFSocketSourceDriver *s, gint *result_fd,
+ const gchar *filename)
{
AFUnixSourceDriver *self = (AFUnixSourceDriver *) s;
gint fd, fds;
@@ -101,12 +102,12 @@ afunix_sd_acquire_socket(AFSocketSourceDriver *s, gint *result_fd)
for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + fds; fd++)
{
/* check if any type is available */
- if (sd_is_socket_unix(fd, 0, -1, self->filename, 0))
+ if (sd_is_socket_unix(fd, 0, -1, filename, 0))
{
int type = (self->super.flags & AFSOCKET_STREAM) ? SOCK_STREAM : SOCK_DGRAM;
/* check if it matches our idea of the socket type */
- if (sd_is_socket_unix(fd, type, -1, self->filename, 0))
+ if (sd_is_socket_unix(fd, type, -1, filename, 0))
{
*result_fd = fd;
break;
@@ -114,7 +115,7 @@ afunix_sd_acquire_socket(AFSocketSourceDriver *s, gint *result_fd)
else
{
msg_error("The systemd supplied UNIX domain socket is of a different type, check the configured driver and the matching systemd unit file",
- evt_tag_str("filename", self->filename),
+ evt_tag_str("filename", filename),
evt_tag_int("systemd-sock-fd", fd),
evt_tag_str("expecting", type == SOCK_STREAM ? "unix-stream()" : "unix-dgram()"),
NULL);
@@ -130,31 +131,60 @@ afunix_sd_acquire_socket(AFSocketSourceDriver *s, gint *result_fd)
*/
msg_debug("Ignoring systemd supplied fd as it is not a UNIX domain socket",
- evt_tag_str("filename", self->filename),
+ evt_tag_str("filename", filename),
evt_tag_int("systemd-sock-fd", fd),
NULL);
}
}
}
- else
- return TRUE;
if (*result_fd != -1)
{
g_fd_set_nonblock(*result_fd, TRUE);
g_fd_set_cloexec(*result_fd, TRUE);
msg_verbose("Acquired systemd socket",
- evt_tag_str("filename", self->filename),
+ evt_tag_str("filename", filename),
evt_tag_int("systemd-sock-fd", *result_fd),
NULL);
+ return TRUE;
}
- else
+ return TRUE;
+}
+
+static gboolean
+afunix_sd_acquire_socket(AFSocketSourceDriver *s, gint *result_fd)
+{
+ AFUnixSourceDriver *self = (AFUnixSourceDriver *) s;
+ gboolean fd_ok;
+ GlobalConfig *cfg = log_pipe_get_config(&s->super.super.super);
+
+ fd_ok = afunix_sd_acquire_named_socket(s, result_fd, self->filename);
+
+ if (fd_ok && (*result_fd == -1) && (strcmp(self->filename, "/dev/log") == 0))
{
- msg_debug("Failed to acquire systemd socket, trying to open ourselves",
- evt_tag_str("filename", self->filename),
- NULL);
+ fd_ok = afunix_sd_acquire_named_socket(s, result_fd, "/run/systemd/journal/syslog");
+
+ if (fd_ok && *result_fd > -1)
+ {
+ if (cfg->version <= 0x0304)
+ {
+ msg_warning("WARNING: systemd detected while using /dev/log; migrating automatically to /run/systemd/journal/syslog. Please update your configuration to use the system() source.",
+ evt_tag_str("id", self->super.super.super.id),
+ NULL);
+
+ g_free(self->filename);
+ self->filename = g_strdup("/run/systemd/journal/syslog");
+ return TRUE;
+ }
+ }
}
- return TRUE;
+
+ if (!fd_ok)
+ msg_debug("Failed to acquire systemd socket, trying to open ourselves",
+ evt_tag_str("filename", self->filename),
+ NULL);
+
+ return fd_ok;
}
#else
--
1.7.10
More information about the syslog-ng
mailing list