2 * Copyright 2016 Red Hat, Inc.
3 * Copyright 2016-2022 Collabora Ltd.
4 * Copyright 2017-2022 Endless OS Foundation, LLC
5 * Copyright 2018 Will Thompson
7 * SPDX-License-Identifier: LGPL-2.1-or-later
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this library; if not, see <http://www.gnu.org/licenses/>.
23 #include "gjournal-private.h"
25 #if defined(__linux__) && !defined(__BIONIC__)
27 #include <sys/socket.h>
31 * Reimplementation of g_str_has_prefix(), necessary when compiled into
35 str_has_prefix (const char *str,
38 return strncmp (str, prefix, strlen (prefix)) == 0;
43 * @output_fd: output file descriptor to check
45 * Same as g_log_writer_is_journald(), but with no GLib dependencies.
47 * Returns: 1 if @output_fd points to the journal, 0 otherwise
50 _g_fd_is_journal (int output_fd)
52 /* FIXME: Use the new journal API for detecting whether we’re writing to the
53 * journal. See: https://github.com/systemd/systemd/issues/2473
56 struct sockaddr_storage storage;
58 struct sockaddr_un un;
66 /* Namespaced journals start with `/run/systemd/journal.${name}/` (see
67 * `RuntimeDirectory=systemd/journal.%i` in `systemd-journald@.service`. The
68 * default journal starts with `/run/systemd/journal/`. */
69 memset (&addr, 0, sizeof (addr));
70 addr_len = sizeof(addr);
71 err = getpeername (output_fd, &addr.sa, &addr_len);
72 if (err == 0 && addr.storage.ss_family == AF_UNIX)
73 return (str_has_prefix (addr.un.sun_path, "/run/systemd/journal/") ||
74 str_has_prefix (addr.un.sun_path, "/run/systemd/journal."));