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

systemd / systemd / 15079507752

16 May 2025 10:18PM UTC coverage: 72.276% (+0.009%) from 72.267%
15079507752

push

github

YHNdnzj
sd-bus: drop a bunch of 'else'

With the new US taxes on bits and bytes let's reduce our footprint a
bit.

11 of 19 new or added lines in 1 file covered. (57.89%)

4028 existing lines in 76 files now uncovered.

298564 of 413090 relevant lines covered (72.28%)

700985.27 hits per line

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

61.29
/src/integritysetup/integritysetup.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <stdio.h>
4
#include <sys/stat.h>
5

6
#include "alloc-util.h"
7
#include "argv-util.h"
8
#include "cryptsetup-util.h"
9
#include "fileio.h"
10
#include "integrity-util.h"
11
#include "log.h"
12
#include "main-func.h"
13
#include "path-util.h"
14
#include "pretty-print.h"
15
#include "string-util.h"
16
#include "time-util.h"
17
#include "verbs.h"
18

19
static uint32_t arg_activate_flags;
20
static int arg_percent;
21
static usec_t arg_commit_time;
22
static char *arg_existing_data_device;
23
static char *arg_integrity_algorithm;
24

25
STATIC_DESTRUCTOR_REGISTER(arg_existing_data_device, freep);
20✔
26
STATIC_DESTRUCTOR_REGISTER(arg_integrity_algorithm, freep);
20✔
27

UNCOV
28
static int help(void) {
×
UNCOV
29
        _cleanup_free_ char *link = NULL;
×
UNCOV
30
        int r;
×
31

UNCOV
32
        r = terminal_urlify_man("systemd-integritysetup@.service", "8", &link);
×
33
        if (r < 0)
×
34
                return log_oom();
×
35

UNCOV
36
        printf("%s attach VOLUME DEVICE [HMAC_KEY_FILE|-] [OPTIONS]\n"
×
37
               "%s detach VOLUME\n\n"
38
               "Attach or detach an integrity protected block device.\n"
39
               "\nSee the %s for details.\n",
40
               program_invocation_short_name,
41
               program_invocation_short_name,
42
               link);
43

44
        return 0;
45
}
46

UNCOV
47
static int load_key_file(
×
48
                const char *key_file,
49
                void **ret_key_file_contents,
50
                size_t *ret_key_file_size) {
UNCOV
51
        int r;
×
52
        _cleanup_(erase_and_freep) char *tmp_key_file_contents = NULL;
×
UNCOV
53
        size_t tmp_key_file_size;
×
54

UNCOV
55
        if (!path_is_absolute(key_file))
×
56
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "key file not absolute path: %s", key_file);
×
57

58
        r = read_full_file_full(
×
59
                        AT_FDCWD, key_file, UINT64_MAX, DM_MAX_KEY_SIZE,
60
                        READ_FULL_FILE_SECURE|READ_FULL_FILE_WARN_WORLD_READABLE|READ_FULL_FILE_CONNECT_SOCKET|READ_FULL_FILE_FAIL_WHEN_LARGER,
61
                        NULL,
62
                        &tmp_key_file_contents, &tmp_key_file_size);
63
        if (r < 0)
×
UNCOV
64
                return log_error_errno(r, "Failed to process key file: %m");
×
65

UNCOV
66
        if (ret_key_file_contents && ret_key_file_size) {
×
UNCOV
67
                *ret_key_file_contents = TAKE_PTR(tmp_key_file_contents);
×
68
                *ret_key_file_size = tmp_key_file_size;
×
69
        }
70

71
        return 0;
72
}
73

74
static const char *integrity_algorithm_select(const void *key_file_buf) {
10✔
75
        /*  To keep a bit of sanity for end users, the subset of integrity
76
            algorithms we support will match what is used in integritysetup */
77
        if (arg_integrity_algorithm) {
10✔
78
                if (streq("hmac-sha256", arg_integrity_algorithm))
10✔
79
                        return DM_HMAC_256;
80
                return arg_integrity_algorithm;
10✔
UNCOV
81
        } else if (key_file_buf)
×
UNCOV
82
                return DM_HMAC_256;
×
83
        return "crc32c";
84
}
85

86
static int verb_attach(int argc, char *argv[], void *userdata) {
10✔
87
        _cleanup_(crypt_freep) struct crypt_device *cd = NULL;
10✔
88
        crypt_status_info status;
10✔
89
        _cleanup_(erase_and_freep) void *key_buf = NULL;
10✔
90
        size_t key_buf_size = 0;
10✔
91
        int r;
10✔
92

93
        /* attach name device optional_key_file optional_options */
94

95
        assert(argc >= 3 && argc <= 5);
10✔
96

97
        const char *volume = argv[1],
10✔
98
                *device = argv[2],
10✔
99
                *key_file = mangle_none(argc > 3 ? argv[3] : NULL),
10✔
100
                *options = mangle_none(argc > 4 ? argv[4] : NULL);
10✔
101

102
        if (!filename_is_valid(volume))
10✔
UNCOV
103
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Volume name '%s' is not valid.", volume);
×
104

105
        if (key_file) {
10✔
UNCOV
106
                r = load_key_file(key_file, &key_buf, &key_buf_size);
×
UNCOV
107
                if (r < 0)
×
108
                        return r;
109
        }
110

111
        if (options) {
10✔
112
                r = parse_integrity_options(options, &arg_activate_flags, &arg_percent,
10✔
113
                                            &arg_commit_time, &arg_existing_data_device, &arg_integrity_algorithm);
114
                if (r < 0)
10✔
115
                        return r;
116
        }
117

118
        r = crypt_init(&cd, device);
10✔
119
        if (r < 0)
10✔
UNCOV
120
                return log_error_errno(r, "Failed to open integrity device %s: %m", device);
×
121

122
        cryptsetup_enable_logging(cd);
10✔
123

124
        status = crypt_status(cd, volume);
10✔
125
        if (IN_SET(status, CRYPT_ACTIVE, CRYPT_BUSY)) {
10✔
UNCOV
126
                log_info("Volume %s already active.", volume);
×
UNCOV
127
                return 0;
×
128
        }
129

130
        r = crypt_load(cd,
20✔
131
                       CRYPT_INTEGRITY,
132
                       &(struct crypt_params_integrity) {
10✔
133
                               .journal_watermark = arg_percent,
134
                               .journal_commit_time = DIV_ROUND_UP(arg_commit_time, USEC_PER_SEC),
10✔
135
                               .integrity = integrity_algorithm_select(key_buf),
10✔
136
                       });
137
        if (r < 0)
10✔
UNCOV
138
                return log_error_errno(r, "Failed to load integrity superblock: %m");
×
139

140
        if (!isempty(arg_existing_data_device)) {
10✔
141
                r = crypt_set_data_device(cd, arg_existing_data_device);
5✔
142
                if (r < 0)
5✔
143
                        return log_error_errno(r, "Failed to add separate data device: %m");
×
144
        }
145

146
        r = crypt_activate_by_volume_key(cd, volume, key_buf, key_buf_size, arg_activate_flags);
10✔
147
        if (r < 0)
10✔
148
                return log_error_errno(r, "Failed to set up integrity device: %m");
×
149

150
        return 0;
151
}
152

153
static int verb_detach(int argc, char *argv[], void *userdata) {
10✔
154
        _cleanup_(crypt_freep) struct crypt_device *cd = NULL;
10✔
155
        int r;
10✔
156

157
        assert(argc == 2);
10✔
158

159
        const char *volume = argv[1];
10✔
160

161
        if (!filename_is_valid(volume))
10✔
UNCOV
162
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Volume name '%s' is not valid.", volume);
×
163

164
        r = crypt_init_by_name(&cd, volume);
10✔
165
        if (r == -ENODEV) {
10✔
UNCOV
166
                log_info("Volume %s already inactive.", volume);
×
167
                return 0;
×
168
        }
169
        if (r < 0)
10✔
UNCOV
170
                return log_error_errno(r, "crypt_init_by_name() failed: %m");
×
171

172
        cryptsetup_enable_logging(cd);
10✔
173

174
        r = crypt_deactivate(cd, volume);
10✔
175
        if (r < 0)
10✔
UNCOV
176
                return log_error_errno(r, "Failed to deactivate: %m");
×
177

178
        return 0;
179
}
180

181
static int run(int argc, char *argv[]) {
20✔
182
        if (argv_looks_like_help(argc, argv))
20✔
UNCOV
183
                return help();
×
184

185
        log_setup();
20✔
186

187
        cryptsetup_enable_logging(NULL);
20✔
188

189
        umask(0022);
20✔
190

191
        static const Verb verbs[] = {
20✔
192
                { "attach", 3, 5, 0, verb_attach },
193
                { "detach", 2, 2, 0, verb_detach },
194
                {}
195
        };
196

197
        return dispatch_verb(argc, argv, verbs, NULL);
20✔
198
}
199

200
DEFINE_MAIN_FUNCTION(run);
20✔
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