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

systemd / systemd / 14895667988

07 May 2025 08:57PM UTC coverage: 72.225% (-0.007%) from 72.232%
14895667988

push

github

yuwata
network: log_link_message_debug_errno() automatically append %m if necessary

Follow-up for d28746ef5.
Fixes CID#1609753.

0 of 1 new or added line in 1 file covered. (0.0%)

20297 existing lines in 338 files now uncovered.

297407 of 411780 relevant lines covered (72.22%)

695716.85 hits per line

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

72.73
/src/nspawn/nspawn-register.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include "sd-bus.h"
4

5
#include "alloc-util.h"
6
#include "bus-error.h"
7
#include "bus-locator.h"
8
#include "bus-unit-util.h"
9
#include "bus-util.h"
10
#include "bus-wait-for-jobs.h"
11
#include "nspawn-register.h"
12
#include "nspawn-settings.h"
13
#include "special.h"
14
#include "stat-util.h"
15
#include "strv.h"
16

17
static int append_machine_properties(
120✔
18
                sd_bus_message *m,
19
                CustomMount *mounts,
20
                unsigned n_mounts,
21
                int kill_signal,
22
                bool coredump_receive) {
23

24
        int r;
120✔
25

26
        assert(m);
120✔
27

28
        r = sd_bus_message_append(m, "(sv)", "DevicePolicy", "s", "closed");
120✔
29
        if (r < 0)
120✔
UNCOV
30
                return bus_log_create_error(r);
×
31

32
        /* If you make changes here, also make sure to update systemd-nspawn@.service, to keep the device
33
         * policies in sync regardless if we are run with or without the --keep-unit switch. */
34
        r = sd_bus_message_append(m, "(sv)", "DeviceAllow", "a(ss)", 3,
120✔
35
                                  /* Allow the container to access and create the API device node, so that
36
                                   * PrivateDevices= in the container can work fine. */
37
                                  "/dev/net/tun", "rwm",
38
                                  /* Allow the container to access ptys. However, do not permit the container
39
                                   * to ever create these device nodes. */
40
                                  "char-pts", "rw",
41
                                  /* Allow the container to access and create the FUSE API device node. */
42
                                  "/dev/fuse", "rwm");
43
        if (r < 0)
120✔
UNCOV
44
                return bus_log_create_error(r);
×
45

46
        FOREACH_ARRAY(cm, mounts, n_mounts) {
257✔
47
                if (cm->type != CUSTOM_MOUNT_BIND)
137✔
48
                        continue;
28✔
49

50
                r = is_device_node(cm->source);
109✔
51
                if (r == -ENOENT) {
109✔
52
                        /* The bind source might only appear as the image is put together, hence don't complain */
53
                        log_debug_errno(r, "Bind mount source %s not found, ignoring: %m", cm->source);
1✔
54
                        continue;
1✔
55
                }
56
                if (r < 0)
108✔
UNCOV
57
                        return log_error_errno(r, "Failed to stat %s: %m", cm->source);
×
58

59
                if (r) {
108✔
60
                        r = sd_bus_message_append(m, "(sv)", "DeviceAllow", "a(ss)", 1,
×
61
                                                  cm->source, cm->read_only ? "r" : "rw");
×
62
                        if (r < 0)
×
UNCOV
63
                                return log_error_errno(r, "Failed to append message arguments: %m");
×
64
                }
65
        }
66

67
        if (kill_signal != 0) {
120✔
68
                r = sd_bus_message_append(m, "(sv)", "KillSignal", "i", kill_signal);
26✔
69
                if (r < 0)
26✔
UNCOV
70
                        return bus_log_create_error(r);
×
71

72
                r = sd_bus_message_append(m, "(sv)", "KillMode", "s", "mixed");
26✔
73
                if (r < 0)
26✔
UNCOV
74
                        return bus_log_create_error(r);
×
75
        }
76

77
        if (coredump_receive) {
120✔
78
                r = sd_bus_message_append(m, "(sv)", "CoredumpReceive", "b", true);
26✔
79
                if (r < 0)
26✔
UNCOV
80
                        return bus_log_create_error(r);
×
81
        }
82

83
        return 0;
84
}
85

86
static int append_controller_property(sd_bus *bus, sd_bus_message *m) {
120✔
87
        const char *unique;
120✔
88
        int r;
120✔
89

90
        assert(bus);
120✔
91
        assert(m);
120✔
92

93
        r = sd_bus_get_unique_name(bus, &unique);
120✔
94
        if (r < 0)
120✔
UNCOV
95
                return log_error_errno(r, "Failed to get unique name: %m");
×
96

97
        r = sd_bus_message_append(m, "(sv)", "Controller", "s", unique);
120✔
98
        if (r < 0)
120✔
UNCOV
99
                return bus_log_create_error(r);
×
100

101
        return 0;
102
}
103

104
static int can_set_coredump_receive(sd_bus *bus) {
26✔
UNCOV
105
        _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
×
106
        _cleanup_free_ char *path = NULL;
26✔
107
        int b, r;
26✔
108

109
        assert(bus);
26✔
110

111
        path = unit_dbus_path_from_name(SPECIAL_INIT_SCOPE);
26✔
112
        if (!path)
26✔
UNCOV
113
                return log_oom();
×
114

115
        r = sd_bus_get_property_trivial(
26✔
116
                        bus,
117
                        "org.freedesktop.systemd1",
118
                        path,
119
                        "org.freedesktop.systemd1.Scope",
120
                        "CoredumpReceive",
121
                        &e,
122
                        'b', &b);
123
        if (r < 0 && !sd_bus_error_has_names(&e, SD_BUS_ERROR_UNKNOWN_PROPERTY, SD_BUS_ERROR_PROPERTY_READ_ONLY))
26✔
UNCOV
124
                log_warning_errno(r, "Failed to determine if CoredumpReceive= can be set, assuming it cannot be: %s",
×
125
                                  bus_error_message(&e, r));
126

127
        return r >= 0;
26✔
128
}
129

130
int register_machine(
82✔
131
                sd_bus *bus,
132
                const char *machine_name,
133
                pid_t pid,
134
                const char *directory,
135
                sd_id128_t uuid,
136
                int local_ifindex,
137
                const char *slice,
138
                CustomMount *mounts,
139
                unsigned n_mounts,
140
                int kill_signal,
141
                char **properties,
142
                sd_bus_message *properties_message,
143
                const char *service,
144
                StartMode start_mode,
145
                RegisterMachineFlags flags) {
146

147
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
82✔
148
        int r;
82✔
149

150
        assert(bus);
82✔
151

152
        if (FLAGS_SET(flags, REGISTER_MACHINE_KEEP_UNIT)) {
82✔
153
                r = bus_call_method(
13✔
154
                                bus,
155
                                bus_machine_mgr,
156
                                "RegisterMachineWithNetwork",
157
                                &error,
158
                                NULL,
159
                                "sayssusai",
160
                                machine_name,
161
                                SD_BUS_MESSAGE_APPEND_ID128(uuid),
13✔
162
                                service,
163
                                "container",
164
                                (uint32_t) pid,
165
                                strempty(directory),
166
                                local_ifindex > 0 ? 1 : 0, local_ifindex);
167
        } else {
168
                _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
69✔
169

170
                r = bus_message_new_method_call(bus, &m,  bus_machine_mgr, "CreateMachineWithNetwork");
69✔
171
                if (r < 0)
69✔
UNCOV
172
                        return bus_log_create_error(r);
×
173

174
                r = sd_bus_message_append(
69✔
175
                                m,
176
                                "sayssusai",
177
                                machine_name,
178
                                SD_BUS_MESSAGE_APPEND_ID128(uuid),
69✔
179
                                service,
180
                                "container",
181
                                (uint32_t) pid,
182
                                strempty(directory),
183
                                local_ifindex > 0 ? 1 : 0, local_ifindex);
184
                if (r < 0)
69✔
UNCOV
185
                        return bus_log_create_error(r);
×
186

187
                r = sd_bus_message_open_container(m, 'a', "(sv)");
69✔
188
                if (r < 0)
69✔
UNCOV
189
                        return bus_log_create_error(r);
×
190

191
                if (!isempty(slice)) {
69✔
192
                        r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice);
×
193
                        if (r < 0)
×
UNCOV
194
                                return bus_log_create_error(r);
×
195
                }
196

197
                r = append_controller_property(bus, m);
69✔
198
                if (r < 0)
69✔
199
                        return r;
200

201
                r = append_machine_properties(
69✔
202
                                m,
203
                                mounts,
204
                                n_mounts,
205
                                kill_signal,
206
                                start_mode == START_BOOT && can_set_coredump_receive(bus) > 0);
69✔
207
                if (r < 0)
69✔
208
                        return r;
209

210
                if (properties_message) {
69✔
211
                        r = sd_bus_message_copy(m, properties_message, true);
1✔
212
                        if (r < 0)
1✔
UNCOV
213
                                return bus_log_create_error(r);
×
214
                }
215

216
                r = bus_append_unit_property_assignment_many(m, UNIT_SERVICE, properties);
69✔
217
                if (r < 0)
69✔
218
                        return r;
219

220
                r = sd_bus_message_close_container(m);
69✔
221
                if (r < 0)
69✔
UNCOV
222
                        return bus_log_create_error(r);
×
223

224
                r = sd_bus_call(bus, m, 0, &error, NULL);
69✔
225
        }
226
        if (r < 0)
82✔
UNCOV
227
                return log_error_errno(r, "Failed to register machine: %s", bus_error_message(&error, r));
×
228

229
        return 0;
230
}
231

232
int unregister_machine(
79✔
233
                sd_bus *bus,
234
                const char *machine_name) {
235

UNCOV
236
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
237
        int r;
79✔
238

239
        assert(bus);
79✔
240

241
        r = bus_call_method(bus, bus_machine_mgr, "UnregisterMachine", &error, NULL, "s", machine_name);
79✔
242
        if (r < 0)
79✔
243
                log_debug("Failed to unregister machine: %s", bus_error_message(&error, r));
46✔
244

245
        return 0;
79✔
246
}
247

248
int allocate_scope(
51✔
249
                sd_bus *bus,
250
                const char *machine_name,
251
                pid_t pid,
252
                const char *slice,
253
                CustomMount *mounts,
254
                unsigned n_mounts,
255
                int kill_signal,
256
                char **properties,
257
                sd_bus_message *properties_message,
258
                StartMode start_mode,
259
                AllocateScopeFlags flags) {
260

261
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
102✔
262
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
51✔
UNCOV
263
        _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
×
264
        _cleanup_free_ char *scope = NULL;
51✔
265
        const char *description, *object;
51✔
266
        int r;
51✔
267

268
        assert(bus);
51✔
269

270
        r = bus_wait_for_jobs_new(bus, &w);
51✔
271
        if (r < 0)
51✔
UNCOV
272
                return log_error_errno(r, "Could not watch job: %m");
×
273

274
        r = unit_name_mangle_with_suffix(machine_name, "as machine name", 0, ".scope", &scope);
51✔
275
        if (r < 0)
51✔
UNCOV
276
                return log_error_errno(r, "Failed to mangle scope name: %m");
×
277

278
        r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "StartTransientUnit");
51✔
279
        if (r < 0)
51✔
UNCOV
280
                return bus_log_create_error(r);
×
281

282
        r = sd_bus_message_append(m, "ss", scope, "fail");
51✔
283
        if (r < 0)
51✔
UNCOV
284
                return bus_log_create_error(r);
×
285

286
        /* Properties */
287
        r = sd_bus_message_open_container(m, 'a', "(sv)");
51✔
288
        if (r < 0)
51✔
UNCOV
289
                return bus_log_create_error(r);
×
290

291
        description = strjoina("Container ", machine_name);
255✔
292

293
        _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
51✔
294
        r = pidref_set_pid(&pidref, pid);
51✔
295
        if (r < 0)
51✔
UNCOV
296
                return log_error_errno(r, "Failed to allocate PID reference: %m");
×
297

298
        r = bus_append_scope_pidref(m, &pidref, FLAGS_SET(flags, ALLOCATE_SCOPE_ALLOW_PIDFD));
51✔
299
        if (r < 0)
51✔
UNCOV
300
                return bus_log_create_error(r);
×
301

302
        r = sd_bus_message_append(m, "(sv)(sv)(sv)(sv)(sv)",
51✔
303
                                  "Description", "s", description,
304
                                  "Delegate", "b", 1,
305
                                  "CollectMode", "s", "inactive-or-failed",
306
                                  "AddRef", "b", 1,
307
                                  "Slice", "s", isempty(slice) ? SPECIAL_MACHINE_SLICE : slice);
51✔
308
        if (r < 0)
51✔
UNCOV
309
                return bus_log_create_error(r);
×
310

311
        r = append_controller_property(bus, m);
51✔
312
        if (r < 0)
51✔
313
                return r;
314

315
        if (properties_message) {
51✔
316
                r = sd_bus_message_copy(m, properties_message, true);
×
317
                if (r < 0)
×
UNCOV
318
                        return bus_log_create_error(r);
×
319
        }
320

321
        r = append_machine_properties(
51✔
322
                        m,
323
                        mounts,
324
                        n_mounts,
325
                        kill_signal,
326
                        start_mode == START_BOOT && can_set_coredump_receive(bus) > 0);
51✔
327
        if (r < 0)
51✔
328
                return r;
329

330
        r = bus_append_unit_property_assignment_many(m, UNIT_SCOPE, properties);
51✔
331
        if (r < 0)
51✔
332
                return r;
333

334
        r = sd_bus_message_close_container(m);
51✔
335
        if (r < 0)
51✔
UNCOV
336
                return bus_log_create_error(r);
×
337

338
        /* No auxiliary units */
339
        r = sd_bus_message_append(
51✔
340
                        m,
341
                        "a(sa(sv))",
342
                        0);
343
        if (r < 0)
51✔
UNCOV
344
                return bus_log_create_error(r);
×
345

346
        r = sd_bus_call(bus, m, 0, &error, &reply);
51✔
347
        if (r < 0) {
51✔
348
                /* If this failed with a property we couldn't write, this is quite likely because the server
349
                 * doesn't support PIDFDs yet, let's try without. */
350
                if (FLAGS_SET(flags, ALLOCATE_SCOPE_ALLOW_PIDFD) &&
×
351
                    sd_bus_error_has_names(&error, SD_BUS_ERROR_UNKNOWN_PROPERTY, SD_BUS_ERROR_PROPERTY_READ_ONLY))
×
UNCOV
352
                        return allocate_scope(
×
353
                                        bus,
354
                                        machine_name,
355
                                        pid,
356
                                        slice,
357
                                        mounts,
358
                                        n_mounts,
359
                                        kill_signal,
360
                                        properties,
361
                                        properties_message,
362
                                        start_mode,
UNCOV
363
                                        flags & ~ALLOCATE_SCOPE_ALLOW_PIDFD);
×
364

UNCOV
365
                return log_error_errno(r, "Failed to allocate scope: %s", bus_error_message(&error, r));
×
366
        }
367

368
        r = sd_bus_message_read(reply, "o", &object);
51✔
369
        if (r < 0)
51✔
UNCOV
370
                return bus_log_parse_error(r);
×
371

372
        r = bus_wait_for_jobs_one(
51✔
373
                        w,
374
                        object,
375
                        BUS_WAIT_JOBS_LOG_ERROR,
376
                        /* extra_args= */ NULL);
377
        if (r < 0)
51✔
UNCOV
378
                return r;
×
379

380
        return 0;
381
}
382

383
int terminate_scope(
51✔
384
                sd_bus *bus,
385
                const char *machine_name) {
386

UNCOV
387
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
388
        _cleanup_free_ char *scope = NULL;
51✔
389
        int r;
51✔
390

391
        r = unit_name_mangle_with_suffix(machine_name, "to terminate", 0, ".scope", &scope);
51✔
392
        if (r < 0)
51✔
UNCOV
393
                return log_error_errno(r, "Failed to mangle scope name: %m");
×
394

395
        r = bus_call_method(bus, bus_systemd_mgr, "AbandonScope", &error, NULL, "s", scope);
51✔
396
        if (r < 0) {
51✔
397
                log_debug_errno(r, "Failed to abandon scope '%s', ignoring: %s", scope, bus_error_message(&error, r));
48✔
398
                sd_bus_error_free(&error);
48✔
399
        }
400

401
        r = bus_call_method(
51✔
402
                        bus,
403
                        bus_systemd_mgr,
404
                        "KillUnit",
405
                        &error,
406
                        NULL,
407
                        "ssi",
408
                        scope,
409
                        "all",
410
                        (int32_t) SIGKILL);
411
        if (r < 0) {
51✔
412
                log_debug_errno(r, "Failed to SIGKILL scope '%s', ignoring: %s", scope, bus_error_message(&error, r));
×
UNCOV
413
                sd_bus_error_free(&error);
×
414
        }
415

416
        r = bus_call_method(bus, bus_systemd_mgr, "UnrefUnit", &error, NULL, "s", scope);
51✔
417
        if (r < 0)
51✔
UNCOV
418
                log_debug_errno(r, "Failed to drop reference to scope '%s', ignoring: %s", scope, bus_error_message(&error, r));
×
419

420
        return 0;
421
}
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