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

systemd / systemd / 19603132989

22 Nov 2025 10:41PM UTC coverage: 72.453% (-0.07%) from 72.518%
19603132989

push

github

YHNdnzj
docs: Document cast formatting rules

308192 of 425371 relevant lines covered (72.45%)

1182701.2 hits per line

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

60.82
/src/ssh-generator/ssh-issue.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <getopt.h>
4
#include <sys/stat.h>
5
#include <unistd.h>
6

7
#include "alloc-util.h"
8
#include "ansi-color.h"
9
#include "build.h"
10
#include "fd-util.h"
11
#include "fs-util.h"
12
#include "log.h"
13
#include "main-func.h"
14
#include "mkdir.h"
15
#include "parse-argument.h"
16
#include "pretty-print.h"
17
#include "ssh-util.h"
18
#include "string-util.h"
19
#include "tmpfile-util.h"
20
#include "virt.h"
21

22
static enum {
23
        ACTION_MAKE_VSOCK,
24
        ACTION_RM_VSOCK,
25
} arg_action = ACTION_MAKE_VSOCK;
26

27
static char *arg_issue_path = NULL;
28
static bool arg_issue_stdout = false;
29

30
STATIC_DESTRUCTOR_REGISTER(arg_issue_path, freep);
124✔
31

32
static int help(void) {
×
33
        _cleanup_free_ char *link = NULL;
×
34
        int r;
×
35

36
        r = terminal_urlify_man("systemd-ssh-issue", "1", &link);
×
37
        if (r < 0)
×
38
                return log_oom();
×
39

40
        printf("%s [OPTIONS...] --make-vsock\n"
×
41
               "%s [OPTIONS...] --rm-vsock\n"
42
               "\n%sCreate ssh /run/issue.d/ file reporting VSOCK address.%s\n\n"
43
               "  -h --help            Show this help\n"
44
               "     --version         Show package version\n"
45
               "     --issue-path=PATH Change path to /run/issue.d/50-ssh-vsock.issue\n"
46
               "\nSee the %s for details.\n",
47
               program_invocation_short_name,
48
               program_invocation_short_name,
49
               ansi_highlight(),
50
               ansi_normal(),
51
               link);
52

53
        return 0;
54
}
55

56
static int parse_argv(int argc, char *argv[]) {
124✔
57

58
        enum {
124✔
59
                ARG_MAKE_VSOCK = 0x100,
60
                ARG_RM_VSOCK,
61
                ARG_ISSUE_PATH,
62
                ARG_VERSION,
63
        };
64

65
        static const struct option options[] = {
124✔
66
                { "help",       no_argument,       NULL, 'h'            },
67
                { "version",    no_argument,       NULL, ARG_VERSION    },
68
                { "make-vsock", no_argument,       NULL, ARG_MAKE_VSOCK },
69
                { "rm-vsock",   no_argument,       NULL, ARG_RM_VSOCK   },
70
                { "issue-path", required_argument, NULL, ARG_ISSUE_PATH },
71
                {}
72
        };
73

74
        int c, r;
124✔
75

76
        assert(argc >= 0);
124✔
77
        assert(argv);
124✔
78

79
        while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
248✔
80

81
                switch (c) {
124✔
82

83
                case 'h':
×
84
                        return help();
×
85

86
                case ARG_VERSION:
×
87
                        return version();
×
88

89
                case ARG_MAKE_VSOCK:
62✔
90
                        arg_action = ACTION_MAKE_VSOCK;
62✔
91
                        break;
62✔
92

93
                case ARG_RM_VSOCK:
62✔
94
                        arg_action = ACTION_RM_VSOCK;
62✔
95
                        break;
62✔
96

97
                case ARG_ISSUE_PATH:
×
98
                        if (isempty(optarg) || streq(optarg, "-")) {
×
99
                                arg_issue_path = mfree(arg_issue_path);
×
100
                                arg_issue_stdout = true;
×
101
                                break;
×
102
                        }
103

104
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_issue_path);
×
105
                        if (r < 0)
×
106
                                return r;
107

108
                        arg_issue_stdout = false;
×
109
                        break;
×
110
                }
111
        }
112

113
        if (!arg_issue_path && !arg_issue_stdout) {
124✔
114
                arg_issue_path = strdup("/run/issue.d/50-ssh-vsock.issue");
124✔
115
                if (!arg_issue_path)
124✔
116
                        return log_oom();
×
117
        }
118

119
        return 1;
120
}
121

122
static int acquire_cid(unsigned *ret_cid) {
62✔
123
        int r;
62✔
124

125
        assert(ret_cid);
62✔
126

127
        Virtualization v = detect_virtualization();
62✔
128
        if (v < 0)
62✔
129
                return log_error_errno(v, "Failed to detect if we run in a VM: %m");
×
130
        if (!VIRTUALIZATION_IS_VM(v)) {
62✔
131
                /* NB: if we are running in a container inside a VM, then we'll *not* do AF_VSOCK stuff */
132
                log_debug("Not running in a VM, not creating issue file.");
×
133
                *ret_cid = 0;
×
134
                return 0;
×
135
        }
136

137
        r = vsock_open_or_warn(/* ret= */ NULL);
62✔
138
        if (r <= 0)
62✔
139
                return r;
140

141
        return vsock_get_local_cid_or_warn(ret_cid);
62✔
142
}
143

144
static int run(int argc, char* argv[]) {
124✔
145
        int r;
124✔
146

147
        log_setup();
124✔
148

149
        r = parse_argv(argc, argv);
124✔
150
        if (r <= 0)
124✔
151
                return r;
152

153
        switch (arg_action) {
124✔
154
        case ACTION_MAKE_VSOCK: {
62✔
155
                unsigned cid;
62✔
156

157
                r = acquire_cid(&cid);
62✔
158
                if (r < 0)
62✔
159
                        return r;
×
160
                if (r == 0) {
62✔
161
                        log_debug("Not running in a VSOCK enabled VM, skipping.");
×
162
                        break;
62✔
163
                }
164

165
                _cleanup_(unlink_and_freep) char *t = NULL;
×
166
                _cleanup_(fclosep) FILE *f = NULL;
62✔
167
                FILE *out;
62✔
168

169
                if (arg_issue_path)  {
62✔
170
                        r = mkdir_parents(arg_issue_path, 0755);
62✔
171
                        if (r < 0)
62✔
172
                                return log_error_errno(r, "Failed to create parent directories of '%s': %m", arg_issue_path);
×
173

174
                        r = fopen_tmpfile_linkable(arg_issue_path, O_WRONLY|O_CLOEXEC, &t, &f);
62✔
175
                        if (r < 0)
62✔
176
                                return log_error_errno(r, "Failed to create '%s': %m", arg_issue_path);
×
177

178
                        out = f;
62✔
179
                } else
180
                        out = stdout;
×
181

182
                fprintf(out,
62✔
183
                        "Try contacting this VM's SSH server via 'ssh vsock%%%u' from host.\n"
184
                        "\n", cid);
185

186
                if (f) {
62✔
187
                        if (fchmod(fileno(f), 0644) < 0)
62✔
188
                                return log_error_errno(errno, "Failed to adjust access mode of '%s': %m", arg_issue_path);
×
189

190
                        r = flink_tmpfile(f, t, arg_issue_path, LINK_TMPFILE_REPLACE);
62✔
191
                        if (r < 0)
62✔
192
                                return log_error_errno(r, "Failed to move '%s' into place: %m", arg_issue_path);
×
193
                }
194

195
                break;
62✔
196
        }
197

198
        case ACTION_RM_VSOCK:
62✔
199
                if (arg_issue_path) {
62✔
200
                        if (unlink(arg_issue_path) < 0) {
62✔
201
                                if (errno != ENOENT)
×
202
                                        return log_error_errno(errno, "Failed to remove '%s': %m", arg_issue_path);
×
203

204
                                log_debug_errno(errno, "File '%s' does not exist, no operation executed.", arg_issue_path);
124✔
205
                        } else
206
                                log_debug("Successfully removed '%s'.", arg_issue_path);
62✔
207
                } else
208
                        log_notice("STDOUT selected for issue file, not removing.");
×
209

210
                break;
211

212
        default:
×
213
                assert_not_reached();
×
214
        }
215

216
        return 0;
217
}
218

219
DEFINE_MAIN_FUNCTION(run);
124✔
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