Coverage Report

Created: 2025-07-11 06:47

/src/tinysparql/subprojects/glib-2.80.3/glib/gjournal-private.c
Line
Count
Source (jump to first uncovered line)
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
0
{
38
0
  return strncmp (str, prefix, strlen (prefix)) == 0;
39
0
}
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
0
{
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
0
  union {
56
0
    struct sockaddr_storage storage;
57
0
    struct sockaddr sa;
58
0
    struct sockaddr_un un;
59
0
  } addr;
60
0
  socklen_t addr_len;
61
0
  int err;
62
63
0
  if (output_fd < 0)
64
0
    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
0
  memset (&addr, 0, sizeof (addr));
70
0
  addr_len = sizeof(addr);
71
0
  err = getpeername (output_fd, &addr.sa, &addr_len);
72
0
  if (err == 0 && addr.storage.ss_family == AF_UNIX)
73
0
    return (str_has_prefix (addr.un.sun_path, "/run/systemd/journal/") ||
74
0
            str_has_prefix (addr.un.sun_path, "/run/systemd/journal."));
75
76
0
  return 0;
77
0
}
78
#endif