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

systemd / systemd / 14554080340

19 Apr 2025 11:46AM UTC coverage: 72.101% (-0.03%) from 72.13%
14554080340

push

github

web-flow
Add two new paragraphs to coding style about header files (#37188)

296880 of 411754 relevant lines covered (72.1%)

687547.52 hits per line

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

71.62
/src/shared/machine-credential.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include "alloc-util.h"
4
#include "creds-util.h"
5
#include "escape.h"
6
#include "extract-word.h"
7
#include "fileio.h"
8
#include "log.h"
9
#include "macro.h"
10
#include "memory-util.h"
11
#include "machine-credential.h"
12
#include "path-util.h"
13
#include "string-util-fundamental.h"
14

15
static void machine_credential_done(MachineCredential *cred) {
20✔
16
        assert(cred);
20✔
17

18
        cred->id = mfree(cred->id);
20✔
19
        cred->data = erase_and_free(cred->data);
20✔
20
        cred->size = 0;
20✔
21
}
20✔
22

23
void machine_credential_context_done(MachineCredentialContext *ctx) {
356✔
24
        assert(ctx);
356✔
25

26
        FOREACH_ARRAY(cred, ctx->credentials, ctx->n_credentials)
360✔
27
                machine_credential_done(cred);
4✔
28

29
        free(ctx->credentials);
356✔
30
}
356✔
31

32
bool machine_credentials_contains(const MachineCredentialContext *ctx, const char *id) {
12✔
33
        assert(ctx);
12✔
34
        assert(id);
12✔
35

36
        FOREACH_ARRAY(cred, ctx->credentials, ctx->n_credentials)
18✔
37
                if (streq(cred->id, id))
6✔
38
                        return true;
39

40
        return false;
41
}
42

43
int machine_credential_set(MachineCredentialContext *ctx, const char *cred_str) {
8✔
44
        _cleanup_(machine_credential_done) MachineCredential cred = {};
8✔
45
        ssize_t l;
8✔
46
        int r;
8✔
47

48
        assert(ctx);
8✔
49

50
        const char *p = ASSERT_PTR(cred_str);
8✔
51

52
        r = extract_first_word(&p, &cred.id, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
8✔
53
        if (r < 0)
8✔
54
                return log_error_errno(r, "Failed to parse --set-credential= parameter: %m");
×
55
        if (r == 0 || !p)
8✔
56
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2✔
57
                                       "Missing value for --set-credential=: %s", cred_str);
58

59
        if (!credential_name_valid(cred.id))
6✔
60
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Credential name is not valid: %s", cred.id);
×
61

62
        if (machine_credentials_contains(ctx, cred.id))
6✔
63
                return log_error_errno(SYNTHETIC_ERRNO(EEXIST), "Duplicate credential '%s', refusing.", cred.id);
×
64

65
        l = cunescape(p, UNESCAPE_ACCEPT_NUL, &cred.data);
6✔
66
        if (l < 0)
6✔
67
                return log_error_errno(l, "Failed to unescape credential data: %s", p);
×
68
        cred.size = l;
6✔
69

70
        if (!GREEDY_REALLOC(ctx->credentials, ctx->n_credentials + 1))
6✔
71
                return log_oom();
×
72

73
        ctx->credentials[ctx->n_credentials++] = TAKE_STRUCT(cred);
6✔
74

75
        return 0;
6✔
76
}
77

78
int machine_credential_load(MachineCredentialContext *ctx, const char *cred_path) {
8✔
79
        _cleanup_(machine_credential_done) MachineCredential cred = {};
×
80
        _cleanup_free_ char *path_alloc = NULL;
8✔
81
        ReadFullFileFlags flags = READ_FULL_FILE_SECURE;
8✔
82
        int r;
8✔
83

84
        assert(ctx);
8✔
85

86
        const char *p = ASSERT_PTR(cred_path);
8✔
87

88
        r = extract_first_word(&p, &cred.id, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
8✔
89
        if (r < 0)
8✔
90
                return log_error_errno(r, "Failed to parse --load-credential= parameter: %m");
×
91
        if (r == 0 || !p)
8✔
92
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Missing value for --load-credential=: %s", cred_path);
2✔
93

94
        if (!credential_name_valid(cred.id))
6✔
95
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Credential name is not valid: %s", cred.id);
×
96

97
        if (machine_credentials_contains(ctx, cred.id))
6✔
98
                return log_error_errno(SYNTHETIC_ERRNO(EEXIST), "Duplicate credential '%s', refusing.", cred.id);
×
99

100
        if (is_path(p) && path_is_valid(p))
6✔
101
                flags |= READ_FULL_FILE_CONNECT_SOCKET;
102
        else if (credential_name_valid(p)) {
×
103
                const char *e;
×
104

105
                r = get_credentials_dir(&e);
×
106
                if (r < 0)
×
107
                        return log_error_errno(r,
×
108
                                               "Credential not available (no credentials passed at all): %s", cred.id);
109

110
                path_alloc = path_join(e, p);
×
111
                if (!path_alloc)
×
112
                        return log_oom();
×
113

114
                p = path_alloc;
×
115
        } else
116
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
117
                                       "Credential source appears to be neither a valid path nor a credential name: %s", p);
118

119
        r = read_full_file_full(AT_FDCWD, p, UINT64_MAX, SIZE_MAX,
6✔
120
                                flags,
121
                                NULL,
122
                                &cred.data, &cred.size);
123
        if (r < 0)
6✔
124
                return log_error_errno(r, "Failed to read credential '%s': %m", p);
×
125

126
        if (!GREEDY_REALLOC(ctx->credentials, ctx->n_credentials + 1))
6✔
127
                return log_oom();
×
128

129
        ctx->credentials[ctx->n_credentials++] = TAKE_STRUCT(cred);
6✔
130

131
        return 0;
6✔
132
}
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