chiark / gitweb /
tree-wide: drop 'This file is part of systemd' blurb
[elogind.git] / src / libelogind / sd-bus / bus-container.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   Copyright 2013 Lennart Poettering
4 ***/
5
6 #include <fcntl.h>
7 #include <unistd.h>
8
9 #include "bus-container.h"
10 #include "bus-internal.h"
11 #include "bus-socket.h"
12 #include "fd-util.h"
13 #include "process-util.h"
14 #include "util.h"
15
16 int bus_container_connect_socket(sd_bus *b) {
17         _cleanup_close_pair_ int pair[2] = { -1, -1 };
18         _cleanup_close_ int pidnsfd = -1, mntnsfd = -1, usernsfd = -1, rootfd = -1;
19         int r, error_buf = 0;
20         pid_t child;
21         ssize_t n;
22
23         assert(b);
24         assert(b->input_fd < 0);
25         assert(b->output_fd < 0);
26         assert(b->nspid > 0 || b->machine);
27
28         if (b->nspid <= 0) {
29                 r = container_get_leader(b->machine, &b->nspid);
30                 if (r < 0)
31                         return r;
32         }
33
34         r = namespace_open(b->nspid, &pidnsfd, &mntnsfd, NULL, &usernsfd, &rootfd);
35         if (r < 0)
36                 return r;
37
38         b->input_fd = socket(b->sockaddr.sa.sa_family, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
39         if (b->input_fd < 0)
40                 return -errno;
41
42         b->input_fd = fd_move_above_stdio(b->input_fd);
43
44         b->output_fd = b->input_fd;
45
46         bus_socket_setup(b);
47
48         if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, pair) < 0)
49                 return -errno;
50
51         r = safe_fork("(sd-buscntr)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &child);
52         if (r < 0)
53                 return r;
54         if (r == 0) {
55                 pid_t grandchild;
56
57                 pair[0] = safe_close(pair[0]);
58
59                 r = namespace_enter(pidnsfd, mntnsfd, -1, usernsfd, rootfd);
60                 if (r < 0)
61                         _exit(EXIT_FAILURE);
62
63                 /* We just changed PID namespace, however it will only
64                  * take effect on the children we now fork. Hence,
65                  * let's fork another time, and connect from this
66                  * grandchild, so that SO_PEERCRED of our connection
67                  * comes from a process from within the container, and
68                  * not outside of it */
69
70                 r = safe_fork("(sd-buscntr2)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &grandchild);
71                 if (r < 0)
72                         _exit(EXIT_FAILURE);
73                 if (r == 0) {
74
75                         r = connect(b->input_fd, &b->sockaddr.sa, b->sockaddr_size);
76                         if (r < 0) {
77                                 /* Try to send error up */
78                                 error_buf = errno;
79                                 (void) write(pair[1], &error_buf, sizeof(error_buf));
80                                 _exit(EXIT_FAILURE);
81                         }
82
83                         _exit(EXIT_SUCCESS);
84                 }
85
86                 r = wait_for_terminate_and_check("(sd-buscntr2)", grandchild, 0);
87                 if (r < 0)
88                         _exit(EXIT_FAILURE);
89
90                 _exit(r);
91         }
92
93         pair[1] = safe_close(pair[1]);
94
95         r = wait_for_terminate_and_check("(sd-buscntr)", child, 0);
96         if (r < 0)
97                 return r;
98         if (r != EXIT_SUCCESS)
99                 return -EPROTO;
100
101         n = read(pair[0], &error_buf, sizeof(error_buf));
102         if (n < 0)
103                 return -errno;
104
105         if (n > 0) {
106                 if (n != sizeof(error_buf))
107                         return -EIO;
108
109                 if (error_buf < 0)
110                         return -EIO;
111
112                 if (error_buf == EINPROGRESS)
113                         return 1;
114
115                 if (error_buf > 0)
116                         return -error_buf;
117         }
118
119         return bus_socket_start_auth(b);
120 }