• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 13935887515

18 Mar 2025 07:10PM UTC coverage: 71.913% (-0.03%) from 71.946%
13935887515

push

github

web-flow
Several fixes and cleanups around sd_listen_fds() (#36788)

15 of 24 new or added lines in 5 files covered. (62.5%)

993 existing lines in 54 files now uncovered.

296157 of 411825 relevant lines covered (71.91%)

710024.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

54.67
/src/oom/oomd.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <getopt.h>
4

5
#include "build.h"
6
#include "bus-log-control-api.h"
7
#include "bus-object.h"
8
#include "cgroup-util.h"
9
#include "conf-parser.h"
10
#include "daemon-util.h"
11
#include "fileio.h"
12
#include "log.h"
13
#include "main-func.h"
14
#include "oomd-conf.h"
15
#include "oomd-manager-bus.h"
16
#include "oomd-manager.h"
17
#include "parse-util.h"
18
#include "pretty-print.h"
19
#include "psi-util.h"
20
#include "signal-util.h"
21

22
static bool arg_dry_run = false;
23

24
static int help(void) {
×
25
        _cleanup_free_ char *link = NULL;
×
26
        int r;
×
27

28
        r = terminal_urlify_man("systemd-oomd", "8", &link);
×
29
        if (r < 0)
×
30
                return log_oom();
×
31

32
        printf("%s [OPTIONS...]\n\n"
×
33
               "Run the userspace out-of-memory (OOM) killer.\n\n"
34
               "  -h --help                 Show this help\n"
35
               "     --version              Show package version\n"
36
               "     --dry-run              Only print destructive actions instead of doing them\n"
37
               "     --bus-introspect=PATH  Write D-Bus XML introspection data\n"
38
               "\nSee the %s for details.\n",
39
               program_invocation_short_name,
40
               link);
41

42
        return 0;
43
}
44

45
static int parse_argv(int argc, char *argv[]) {
1✔
46
        enum {
1✔
47
                ARG_VERSION = 0x100,
48
                ARG_DRY_RUN,
49
                ARG_BUS_INTROSPECT,
50
        };
51

52
        static const struct option options[] = {
1✔
53
                { "help",           no_argument,       NULL, 'h'                },
54
                { "version",        no_argument,       NULL, ARG_VERSION        },
55
                { "dry-run",        no_argument,       NULL, ARG_DRY_RUN        },
56
                { "bus-introspect", required_argument, NULL, ARG_BUS_INTROSPECT },
57
                {}
58
        };
59

60
        int c;
1✔
61

62
        assert(argc >= 0);
1✔
63
        assert(argv);
1✔
64

65
        while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
1✔
66

67
                switch (c) {
×
68

69
                case 'h':
×
70
                        return help();
×
71

72
                case ARG_VERSION:
×
73
                        return version();
×
74

75
                case ARG_DRY_RUN:
×
76
                        arg_dry_run = true;
×
77
                        break;
×
78

79
                case ARG_BUS_INTROSPECT:
×
80
                        return bus_introspect_implementations(
×
81
                                        stdout,
82
                                        optarg,
83
                                        BUS_IMPLEMENTATIONS(&manager_object,
×
84
                                                            &log_control_object));
85

86
                case '?':
87
                        return -EINVAL;
88

89
                default:
×
90
                        assert_not_reached();
×
91
                }
92

93
        if (optind < argc)
1✔
94
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
95
                                       "This program takes no arguments.");
96

97
        return 1;
98
}
99

100
static int run(int argc, char *argv[]) {
1✔
101
        _unused_ _cleanup_(notify_on_cleanup) const char *notify_msg = NULL;
1✔
102
        _cleanup_(manager_freep) Manager *m = NULL;
×
103
        _cleanup_free_ char *swap = NULL;
1✔
104
        unsigned long long s = 0;
1✔
105
        CGroupMask mask;
1✔
106
        int r;
1✔
107

108
        log_setup();
1✔
109

110
        r = parse_argv(argc, argv);
1✔
111
        if (r <= 0)
1✔
112
                return r;
113

114
        /* Do some basic requirement checks for running systemd-oomd. It's not exhaustive as some of the other
115
         * requirements do not have a reliable means to check for in code. */
116

117
        int n = sd_listen_fds(0);
1✔
118
        if (n < 0)
1✔
NEW
119
                return log_error_errno(n, "Failed to determine number of listening fds: %m");
×
120
        if (n > 1)
1✔
121
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Received too many file descriptors");
×
122

123
        int fd = n == 1 ? SD_LISTEN_FDS_START : -EBADF;
1✔
124

125
        /* SwapTotal is always available in /proc/meminfo and defaults to 0, even on swap-disabled kernels. */
126
        r = get_proc_field("/proc/meminfo", "SwapTotal", WHITESPACE, &swap);
1✔
127
        if (r < 0)
1✔
128
                return log_error_errno(r, "Failed to get SwapTotal from /proc/meminfo: %m");
×
129

130
        r = safe_atollu(swap, &s);
1✔
131
        if (r < 0 || s == 0)
1✔
132
                log_warning("No swap; memory pressure usage will be degraded");
×
133

134
        if (!is_pressure_supported())
1✔
135
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Pressure Stall Information (PSI) is not supported");
×
136

137
        r = cg_all_unified();
1✔
138
        if (r < 0)
1✔
139
                return log_error_errno(r, "Failed to determine whether the unified cgroups hierarchy is used: %m");
×
140
        if (r == 0)
1✔
141
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Requires the unified cgroups hierarchy");
×
142

143
        r = cg_mask_supported(&mask);
1✔
144
        if (r < 0)
1✔
145
                return log_error_errno(r, "Failed to get supported cgroup controllers: %m");
×
146

147
        if (!FLAGS_SET(mask, CGROUP_MASK_MEMORY))
1✔
148
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Requires the cgroup memory controller.");
×
149

150
        r = manager_new(&m);
1✔
151
        if (r < 0)
1✔
152
                return log_error_errno(r, "Failed to create manager: %m");
×
153

154
        r = manager_start(
1✔
155
                        m,
156
                        arg_dry_run,
157
                        fd);
158
        if (r < 0)
1✔
159
                return log_error_errno(r, "Failed to start up daemon: %m");
×
160

161
        notify_msg = notify_start(NOTIFY_READY, NOTIFY_STOPPING);
1✔
162

163
        log_debug("systemd-oomd started%s.", arg_dry_run ? " in dry run mode" : "");
2✔
164

165
        r = sd_event_loop(m->event);
1✔
166
        if (r < 0)
1✔
167
                return log_error_errno(r, "Event loop failed: %m");
×
168

169
        return 0;
170
}
171

172
DEFINE_MAIN_FUNCTION(run);
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc