summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon McVittie <smcv@collabora.com>2022-07-14 14:30:02 +0100
committerPhilip Withnall <pwithnall@endlessos.org>2022-07-25 01:00:54 +0200
commit6c3e52bb1d3e7977b90c7a8d61cc464ca8e7fb42 (patch)
tree18a7437b4d44079b06e5198aa2a7e9a8fb2704e8
parent763643ceaacc4b9863562d35b606cb15104211fa (diff)
downloadglib-6c3e52bb1d3e7977b90c7a8d61cc464ca8e7fb42.tar.gz
gmessages: Factor out _g_fd_is_journal into its own translation unit
I want to use this in gio-launch-desktop, but gio-launch-desktop doesn't depend on GLib, so I can't just call g_log_writer_is_journald(). Signed-off-by: Simon McVittie <smcv@collabora.com>
-rw-r--r--glib/gjournal-private.c78
-rw-r--r--glib/gjournal-private.h25
-rw-r--r--glib/gmessages.c32
-rw-r--r--glib/meson.build4
4 files changed, 114 insertions, 25 deletions
diff --git a/glib/gjournal-private.c b/glib/gjournal-private.c
new file mode 100644
index 000000000..f45e2cf1c
--- /dev/null
+++ b/glib/gjournal-private.c
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2016 Red Hat, Inc.
+ * Copyright 2016-2022 Collabora Ltd.
+ * Copyright 2017-2022 Endless OS Foundation, LLC
+ * Copyright 2018 Will Thompson
+ *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "gjournal-private.h"
+
+#if defined(__linux__) && !defined(__BIONIC__)
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+/*
+ * Reimplementation of g_str_has_prefix(), necessary when compiled into
+ * gio-launch-desktop.
+ */
+static int
+str_has_prefix (const char *str,
+ const char *prefix)
+{
+ return strncmp (str, prefix, strlen (prefix)) == 0;
+}
+
+/*
+ * _g_fd_is_journal:
+ * @output_fd: output file descriptor to check
+ *
+ * Same as g_log_writer_is_journald(), but with no GLib dependencies.
+ *
+ * Returns: 1 if @output_fd points to the journal, 0 otherwise
+ */
+int
+_g_fd_is_journal (int output_fd)
+{
+ /* FIXME: Use the new journal API for detecting whether we’re writing to the
+ * journal. See: https://github.com/systemd/systemd/issues/2473
+ */
+ union {
+ struct sockaddr_storage storage;
+ struct sockaddr sa;
+ struct sockaddr_un un;
+ } addr;
+ socklen_t addr_len;
+ int err;
+
+ if (output_fd < 0)
+ return 0;
+
+ /* Namespaced journals start with `/run/systemd/journal.${name}/` (see
+ * `RuntimeDirectory=systemd/journal.%i` in `systemd-journald@.service`. The
+ * default journal starts with `/run/systemd/journal/`. */
+ memset (&addr, 0, sizeof (addr));
+ addr_len = sizeof(addr);
+ err = getpeername (output_fd, &addr.sa, &addr_len);
+ if (err == 0 && addr.storage.ss_family == AF_UNIX)
+ return (str_has_prefix (addr.un.sun_path, "/run/systemd/journal/") ||
+ str_has_prefix (addr.un.sun_path, "/run/systemd/journal."));
+
+ return 0;
+}
+#endif
diff --git a/glib/gjournal-private.h b/glib/gjournal-private.h
new file mode 100644
index 000000000..46e0e5a85
--- /dev/null
+++ b/glib/gjournal-private.h
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2016-2022 Collabora Ltd.
+ *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __GLIB_JOURNAL_PRIVATE_H__
+#define __GLIB_JOURNAL_PRIVATE_H__
+
+int _g_fd_is_journal (int output_fd);
+
+#endif
diff --git a/glib/gmessages.c b/glib/gmessages.c
index 97c5b31b1..bbea25375 100644
--- a/glib/gmessages.c
+++ b/glib/gmessages.c
@@ -202,6 +202,10 @@
#include "gpattern.h"
#include "gthreadprivate.h"
+#if defined(__linux__) && !defined(__BIONIC__)
+#include "gjournal-private.h"
+#endif
+
#ifdef G_OS_UNIX
#include <unistd.h>
#endif
@@ -2223,32 +2227,10 @@ gboolean
g_log_writer_is_journald (gint output_fd)
{
#if defined(__linux__) && !defined(__BIONIC__)
- /* FIXME: Use the new journal API for detecting whether we’re writing to the
- * journal. See: https://github.com/systemd/systemd/issues/2473
- */
- union {
- struct sockaddr_storage storage;
- struct sockaddr sa;
- struct sockaddr_un un;
- } addr;
- socklen_t addr_len;
- int err;
-
- if (output_fd < 0)
- return FALSE;
-
- /* Namespaced journals start with `/run/systemd/journal.${name}/` (see
- * `RuntimeDirectory=systemd/journal.%i` in `systemd-journald@.service`. The
- * default journal starts with `/run/systemd/journal/`. */
- memset (&addr, 0, sizeof (addr));
- addr_len = sizeof(addr);
- err = getpeername (output_fd, &addr.sa, &addr_len);
- if (err == 0 && addr.storage.ss_family == AF_UNIX)
- return (g_str_has_prefix (addr.un.sun_path, "/run/systemd/journal/") ||
- g_str_has_prefix (addr.un.sun_path, "/run/systemd/journal."));
-#endif
-
+ return _g_fd_is_journal (output_fd);
+#else
return FALSE;
+#endif
}
static void escape_string (GString *string);
diff --git a/glib/meson.build b/glib/meson.build
index 6062c11a1..bcfcba94f 100644
--- a/glib/meson.build
+++ b/glib/meson.build
@@ -336,6 +336,10 @@ else
platform_deps = []
endif
+if host_system == 'linux'
+ glib_sources += files('gjournal-private.c')
+endif
+
if glib_have_cocoa
glib_sources += files('gosxutils.m')
framework_dep = dependency('appleframeworks', modules : ['Foundation', 'CoreFoundation', 'AppKit'])