summaryrefslogtreecommitdiffstats
path: root/src/machine/machined-core.c
blob: d921dfb27e91ee45548baddd537232c61b9a887e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include "cgroup-util.h"
#include "copy.h"
#include "env-file.h"
#include "fd-util.h"
#include "fileio.h"
#include "iovec-util.h"
#include "machined.h"
#include "process-util.h"
#include "socket-util.h"
#include "strv.h"
#include "user-util.h"

int manager_get_machine_by_pid(Manager *m, pid_t pid, Machine **ret) {
        Machine *mm;
        int r;

        assert(m);
        assert(pid_is_valid(pid));
        assert(ret);

        mm = hashmap_get(m->machines_by_leader, &PIDREF_MAKE_FROM_PID(pid));
        if (!mm) {
                _cleanup_free_ char *unit = NULL;

                r = cg_pid_get_unit(pid, &unit);
                if (r >= 0)
                        mm = hashmap_get(m->machines_by_unit, unit);
        }
        if (!mm) {
                *ret = NULL;
                return 0;
        }

        *ret = mm;
        return 1;
}

int manager_add_machine(Manager *m, const char *name, Machine **ret) {
        Machine *machine;
        int r;

        assert(m);
        assert(name);

        machine = hashmap_get(m->machines, name);
        if (!machine) {
                r = machine_new(_MACHINE_CLASS_INVALID, name, &machine);
                if (r < 0)
                        return r;

                r = machine_link(m, machine);
                if (r < 0) {
                        machine_free(machine);
                        return r;
                }
        }

        if (ret)
                *ret = machine;

        return 0;
}

int manager_find_machine_for_uid(Manager *m, uid_t uid, Machine **ret_machine, uid_t *ret_internal_uid) {
        Machine *machine;
        int r;

        assert(m);
        assert(uid_is_valid(uid));

        /* Finds the machine for the specified host UID and returns it along with the UID translated into the
         * internal UID inside the machine */

        HASHMAP_FOREACH(machine, m->machines) {
                uid_t converted;

                r = machine_owns_uid(machine, uid, &converted);
                if (r < 0)
                        return r;
                if (r) {
                        if (ret_machine)
                                *ret_machine = machine;

                        if (ret_internal_uid)
                                *ret_internal_uid = converted;

                        return true;
                }
        }

        if (ret_machine)
                *ret_machine = NULL;
        if (ret_internal_uid)
                *ret_internal_uid = UID_INVALID;

        return false;
}

int manager_find_machine_for_gid(Manager *m, gid_t gid, Machine **ret_machine, gid_t *ret_internal_gid) {
        Machine *machine;
        int r;

        assert(m);
        assert(gid_is_valid(gid));

        HASHMAP_FOREACH(machine, m->machines) {
                gid_t converted;

                r = machine_owns_gid(machine, gid, &converted);
                if (r < 0)
                        return r;
                if (r) {
                        if (ret_machine)
                                *ret_machine = machine;

                        if (ret_internal_gid)
                                *ret_internal_gid = converted;

                        return true;
                }
        }

        if (ret_machine)
                *ret_machine = NULL;
        if (ret_internal_gid)
                *ret_internal_gid = GID_INVALID;

        return false;
}

void manager_gc(Manager *m, bool drop_not_started) {
        Machine *machine;

        assert(m);

        while ((machine = LIST_POP(gc_queue, m->machine_gc_queue))) {
                machine->in_gc_queue = false;

                /* First, if we are not closing yet, initiate stopping */
                if (machine_may_gc(machine, drop_not_started) &&
                    machine_get_state(machine) != MACHINE_CLOSING)
                        machine_stop(machine);

                /* Now, the stop probably made this referenced
                 * again, but if it didn't, then it's time to let it
                 * go entirely. */
                if (machine_may_gc(machine, drop_not_started)) {
                        machine_finalize(machine);
                        machine_free(machine);
                }
        }
}

static int on_deferred_gc(sd_event_source *s, void *userdata) {
        manager_gc(userdata, /* drop_not_started= */ true);
        return 0;
}

void manager_enqueue_gc(Manager *m) {
        int r;

        assert(m);

        if (m->deferred_gc_event_source) {
                r = sd_event_source_set_enabled(m->deferred_gc_event_source, SD_EVENT_ONESHOT);
                if (r < 0)
                        log_warning_errno(r, "Failed to enable GC event source, ignoring: %m");

                return;
        }

        r = sd_event_add_defer(m->event, &m->deferred_gc_event_source, on_deferred_gc, m);
        if (r < 0)
                return (void) log_warning_errno(r, "Failed to allocate GC event source, ignoring: %m");

        r = sd_event_source_set_priority(m->deferred_gc_event_source, SD_EVENT_PRIORITY_IDLE);
        if (r < 0)
                log_warning_errno(r, "Failed to tweak priority of event source, ignoring: %m");

        (void) sd_event_source_set_description(m->deferred_gc_event_source, "deferred-gc");
}

int machine_get_addresses(Machine* machine, struct local_address **ret_addresses) {
        assert(machine);
        assert(ret_addresses);

        switch (machine->class) {

        case MACHINE_HOST: {
                _cleanup_free_ struct local_address *addresses = NULL;
                int n;

                n = local_addresses(/* rtnl = */ NULL, /* ifindex = */ 0, AF_UNSPEC, &addresses);
                if (n < 0)
                        return log_debug_errno(n, "Failed to get local addresses: %m");

                *ret_addresses = TAKE_PTR(addresses);
                return n;
        }

        case MACHINE_CONTAINER: {
                _cleanup_close_pair_ int pair[2] = EBADF_PAIR;
                _cleanup_close_ int netns_fd = -EBADF;
                pid_t child;
                int r;

                r = in_same_namespace(/* pid1 = */ 0, machine->leader.pid, NAMESPACE_NET);
                if (r < 0)
                        return log_debug_errno(r, "Failed to check if container has private network: %m");
                if (r > 0)
                        return -ENONET;

                r = pidref_namespace_open(&machine->leader,
                                          /* ret_pidns_fd = */ NULL,
                                          /* ret_mntns_fd = */ NULL,
                                          &netns_fd,
                                          /* ret_userns_fd = */ NULL,
                                          /* ret_root_fd = */ NULL);
                if (r < 0)
                        return log_debug_errno(r, "Failed to open namespace: %m");

                if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, pair) < 0)
                        return log_debug_errno(errno, "Failed to call socketpair(): %m");

                r = namespace_fork("(sd-addrns)",
                                   "(sd-addr)",
                                   /* except_fds = */ NULL,
                                   /* n_except_fds = */ 0,
                                   FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGKILL,
                                   /* pidns_fd = */ -1,
                                   /* mntns_fd = */ -1,
                                   netns_fd,
                                   /* userns_fd = */ -1,
                                   /* root_fd = */ -1,
                                   &child);
                if (r < 0)
                        return log_debug_errno(r, "Failed to fork(): %m");
                if (r == 0) {
                        _cleanup_free_ struct local_address *addresses = NULL;

                        pair[0] = safe_close(pair[0]);

                        int n = local_addresses(/* rtnl = */ NULL, /* ifindex = */ 0, AF_UNSPEC, &addresses);
                        if (n < 0) {
                                log_debug_errno(n, "Failed to get local addresses: %m");
                                _exit(EXIT_FAILURE);
                        }

                        FOREACH_ARRAY(a, addresses, n) {
                                r = write(pair[1], a, sizeof(*a));
                                if (r < 0) {
                                        log_debug_errno(errno, "Failed to write to socket: %m");
                                        _exit(EXIT_FAILURE);
                                }
                        }

                        pair[1] = safe_close(pair[1]);

                        _exit(EXIT_SUCCESS);
                }

                pair[1] = safe_close(pair[1]);

                _cleanup_free_ struct local_address *list = NULL;
                size_t n_list = 0;

                for (;;) {
                        ssize_t n;
                        struct local_address la;

                        n = read(pair[0], &la, sizeof(la));
                        if (n < 0)
                                return log_debug_errno(errno, "Failed to read from socket(): %m");
                        if (n == 0)
                                break;
                        if ((size_t) n < sizeof(la))
                                return log_debug_errno(SYNTHETIC_ERRNO(EIO), "Received unexpectedly short message");

                        r = add_local_address(&list,
                                              &n_list,
                                              la.ifindex,
                                              la.scope,
                                              la.family,
                                              &la.address);
                        if (r < 0)
                                return log_debug_errno(r, "Failed to add local address: %m");
                }

                r = wait_for_terminate_and_check("(sd-addrns)", child, /* flags = */ 0);
                if (r < 0)
                        return log_debug_errno(r, "Failed to wait for child: %m");
                if (r != EXIT_SUCCESS)
                        return log_debug_errno(SYNTHETIC_ERRNO(ESHUTDOWN), "Child died abnormally");

                *ret_addresses = TAKE_PTR(list);
                return (int) n_list;
        }

        default:
                return -EOPNOTSUPP;
        }
}

#define EXIT_NOT_FOUND 2

int machine_get_os_release(Machine *machine, char ***ret_os_release) {
        _cleanup_strv_free_ char **l = NULL;
        int r;

        assert(machine);
        assert(ret_os_release);

        switch (machine->class) {

        case MACHINE_HOST:
                r = load_os_release_pairs(/* root = */ NULL, &l);
                if (r < 0)
                        return log_debug_errno(r, "Failed to load OS release information: %m");

                break;

        case MACHINE_CONTAINER: {
                _cleanup_close_ int mntns_fd = -EBADF, root_fd = -EBADF, pidns_fd = -EBADF;
                _cleanup_close_pair_ int pair[2] = EBADF_PAIR;
                _cleanup_fclose_ FILE *f = NULL;
                pid_t child;

                r = pidref_namespace_open(&machine->leader,
                                          &pidns_fd,
                                          &mntns_fd,
                                          /* ret_netns_fd = */ NULL,
                                          /* ret_userns_fd = */ NULL,
                                          &root_fd);
                if (r < 0)
                        return log_debug_errno(r, "Failed to open namespace: %m");

                if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, pair) < 0)
                        return log_debug_errno(errno, "Failed to call socketpair(): %m");

                r = namespace_fork("(sd-osrelns)",
                                   "(sd-osrel)",
                                   /* except_fds = */ NULL,
                                   /* n_except_fds = */ 0,
                                   FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGKILL,
                                   pidns_fd,
                                   mntns_fd,
                                   /* netns_fd = */ -1,
                                   /* userns_fd = */ -1,
                                   root_fd,
                                   &child);
                if (r < 0)
                        return log_debug_errno(r, "Failed to fork(): %m");
                if (r == 0) {
                        _cleanup_close_ int fd = -EBADF;

                        pair[0] = safe_close(pair[0]);

                        r = open_os_release(/* root = */ NULL, /* ret_path = */ NULL, &fd);
                        if (r == -ENOENT)
                                _exit(EXIT_NOT_FOUND);
                        if (r < 0) {
                                log_debug_errno(r, "Failed to read OS release: %m");
                                _exit(EXIT_FAILURE);
                        }

                        r = copy_bytes(fd, pair[1], UINT64_MAX, /* copy_flags = */ 0);
                        if (r < 0) {
                                log_debug_errno(r, "Failed to write to fd: %m");
                                _exit(EXIT_FAILURE);
                        }

                        _exit(EXIT_SUCCESS);
                }

                pair[1] = safe_close(pair[1]);

                f = take_fdopen(&pair[0], "r");
                if (!f)
                        return log_debug_errno(errno, "Failed to fdopen(): %m");

                r = load_env_file_pairs(f, "/etc/os-release", &l);
                if (r < 0)
                        return log_debug_errno(r, "Failed to load OS release information: %m");

                r = wait_for_terminate_and_check("(sd-osrelns)", child, /* flags = */ 0);
                if (r < 0)
                        return log_debug_errno(r, "Failed to wait for child: %m");
                if (r == EXIT_NOT_FOUND)
                        return -ENOENT;
                if (r != EXIT_SUCCESS)
                        return log_debug_errno(SYNTHETIC_ERRNO(ESHUTDOWN), "Child died abnormally");

                break;
        }

        default:
                return -EOPNOTSUPP;
        }

        *ret_os_release = TAKE_PTR(l);
        return 0;
}