(root)/
glib-2.79.0/
glib/
gjournal-private.c
       1  /*
       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
       6   *
       7   * SPDX-License-Identifier: LGPL-2.1-or-later
       8   *
       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.
      13   *
      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.
      18   *
      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/>.
      21   */
      22  
      23  #include "gjournal-private.h"
      24  
      25  #if defined(__linux__) && !defined(__BIONIC__)
      26  #include <string.h>
      27  #include <sys/socket.h>
      28  #include <sys/un.h>
      29  
      30  /*
      31   * Reimplementation of g_str_has_prefix(), necessary when compiled into
      32   * gio-launch-desktop.
      33   */
      34  static int
      35  str_has_prefix (const char *str,
      36                  const char *prefix)
      37  {
      38    return strncmp (str, prefix, strlen (prefix)) == 0;
      39  }
      40  
      41  /*
      42   * _g_fd_is_journal:
      43   * @output_fd: output file descriptor to check
      44   *
      45   * Same as g_log_writer_is_journald(), but with no GLib dependencies.
      46   *
      47   * Returns: 1 if @output_fd points to the journal, 0 otherwise
      48   */
      49  int
      50  _g_fd_is_journal (int output_fd)
      51  {
      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
      54     */
      55    union {
      56      struct sockaddr_storage storage;
      57      struct sockaddr sa;
      58      struct sockaddr_un un;
      59    } addr;
      60    socklen_t addr_len;
      61    int err;
      62  
      63    if (output_fd < 0)
      64      return 0;
      65  
      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."));
      75  
      76    return 0;
      77  }
      78  #endif