summaryrefslogtreecommitdiff
path: root/src/nspawn/nspawn.c
blob: 72be2cef43e7291aeb1ae02e53a54aa6e6c17bb5 (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
/***
  This file is part of systemd.

  Copyright 2010 Lennart Poettering

  systemd is free software; you can redistribute it and/or modify it
  under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation; either version 2.1 of the License, or
  (at your option) any later version.

  systemd is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/

#include <fcntl.h>
#include <sched.h>
#include <sys/mount.h>
#include <sys/prctl.h>
#include <sys/wait.h>

#include "fd-util.h" /* _cleanup_close_ */
#include "path-util.h" /* prefix_roota */

static char **arg_parameters = NULL;

static int outer_child(
                const char *directory,
                const char *console,
                int pid_socket) {

        pid_t pid;
        ssize_t l;
        int r;
        const char *to;

        assert(directory);
        assert(console);
        assert(pid_socket >= 0);

        r = open(console, O_RDWR, 0);
        if (r < 0)
                return log_error_errno(r, "Failed to open console: %m");


        to = prefix_roota(directory, "/dev/console");
        if (mount(console, to, NULL, MS_BIND, NULL) < 0)
                return log_error_errno(errno, "mount /dev/console: %m");

        if (chdir(directory) < 0)
                return log_error_errno(errno, "Failed to chdir: %m");
        if (chroot(".") < 0)
                return log_error_errno(errno, "Failed to chroot: %m");

        if (unshare(CLONE_NEWNS|CLONE_NEWPID) < 0)
                return log_error_errno(errno, "unshare: %m");
        pid = fork();
        switch (pid) {
        case -1:
                return log_error_errno(errno, "Failed to fork inner child: %m");
        case 0:
                pid_socket = safe_close(pid_socket);

                printf("a\n");
                if (mount("proc", "/proc", "proc", MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL) < 0)
                        return log_error_errno(errno, "mount /proc: %m");

                printf("b\n");
                execvp(arg_parameters[0], arg_parameters);
                return -errno;
        default:
                l = send(pid_socket, &pid, sizeof(pid), MSG_NOSIGNAL);
                if (l < 0)
                        return log_error_errno(errno, "Failed to send PID: %m");
                if (l != sizeof(pid)) {
                        log_error("Short write while sending PID.");
                        return -EIO;
                }

                pid_socket = safe_close(pid_socket);

                return 0;
        }
}

static int run(const char *directory,
               int master,
               const char *console,
               int *ret) {
        pid_t pid;

        _cleanup_close_pair_ int pid_socket_pair[2] = { -1, -1 };
        int r;
        ssize_t l;

        if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, pid_socket_pair) < 0)
                return log_error_errno(errno, "Failed to create pid socket pair: %m");

        pid = fork();
        switch (pid) {
        case -1:
                return log_error_errno(errno, "clone() failed%s: %m",
                                       errno == EINVAL ?
                                ", do you have namespace support enabled in your kernel? (You need UTS, IPC, PID and NET namespacing built in)" : "");
        case 0:
                if (unshare(CLONE_NEWNS|CLONE_NEWUSER) < 0)
                        return log_error_errno(errno, "unshare: %m");
                if (unshare(CLONE_NEWNS) < 0)
                        return log_error_errno(errno, "unshare: %m");

                master = safe_close(master);

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

                r = outer_child(directory,
                                console,
                                pid_socket_pair[1]);
                if (r < 0)
                        _exit(EXIT_FAILURE);

                _exit(EXIT_SUCCESS);
        default:
                pid_socket_pair[1] = safe_close(pid_socket_pair[1]);

                printf("pid: %ld\n", (long)pid);
                r = wait_for_terminate_and_warn("outer child", pid, NULL);
                if (r != 0)
                        return r < 0 ? r : -EIO;

                /* And now retrieve the PID of the inner child. */
                l = recv(pid_socket_pair[0], &pid, sizeof pid, 0);
                if (l < 0)
                        return log_error_errno(errno, "Failed to read inner child PID: %m");
                if (l != sizeof pid) {
                        log_error("Short read while reading inner child PID.");
                        return -EIO;
                }

                printf("pid: %ld\n", (long)pid);
                r = wait_for_terminate_and_warn("inner child", pid, NULL);
                if (r < 0) {
                        /* We failed to wait for the container, or the container exited abnormally. */
                        return r;
                } else {
                        *ret = r;
                        return 0;
                }
        }
}

int main(int argc, char *argv[]) {

        char *console = NULL;
        _cleanup_close_ int master = -1;
        int r, ret = EXIT_SUCCESS;
        char *arg_directory = NULL;

        log_parse_environment();
        log_open();

        if (argc < 3)
                return 2;
        arg_directory = argv[1];
        arg_parameters = &argv[2];

        master = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC|O_NDELAY);
        if (master < 0) {
                r = log_error_errno(errno, "Failed to acquire pseudo tty: %m");
                goto finish;
        }

        console = ptsname(master);
        if (!console) {
                r = log_error_errno(errno, "Failed to determine tty name: %m");
                goto finish;
        }

        if (unlockpt(master) < 0) {
                r = log_error_errno(errno, "Failed to unlock tty: %m");
                goto finish;
        }

        /* for some reason removing this appends " (deleted)" to the entry in /proc/self/fd/ */
        if (prctl(PR_SET_CHILD_SUBREAPER, 1) < 0) {
                r = log_error_errno(errno, "Failed to become subreaper: %m");
                goto finish;
        }

        r = run(arg_directory,
                master,
                console,
                &ret);

finish:
        return r < 0 ? EXIT_FAILURE : ret;
}