bus-container.c revision 2b7d6d33dc04d9ef370b51afe88d28be8a4f9f84
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 Lennart Poettering
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 <unistd.h>
#include <fcntl.h>
#include "util.h"
#include "process-util.h"
#include "bus-internal.h"
#include "bus-socket.h"
#include "bus-container.h"
int bus_container_connect_socket(sd_bus *b) {
int r, error_buf = 0;
ssize_t n;
assert(b);
if (b->nspid <= 0) {
if (r < 0)
return r;
}
if (r < 0)
return r;
if (b->input_fd < 0)
return -errno;
bus_socket_setup(b);
return -errno;
if (child < 0)
return -errno;
if (child == 0) {
if (r < 0)
/* We just changed PID namespace, however it will only
* take effect on the children we now fork. Hence,
* let's fork another time, and connect from this
* grandchild, so that SO_PEERCRED of our connection
* comes from a process from within the container, and
* not outside of it */
grandchild = fork();
if (grandchild < 0)
if (grandchild == 0) {
if (r < 0) {
/* Try to send error up */
}
}
if (r < 0)
}
if (r < 0)
return r;
if (n < 0)
return -errno;
if (n > 0) {
if (n != sizeof(error_buf))
return -EIO;
if (error_buf < 0)
return -EIO;
if (error_buf == EINPROGRESS)
return 1;
if (error_buf > 0)
return -error_buf;
}
return -EIO;
return -EIO;
return bus_socket_start_auth(b);
}
int bus_container_connect_kernel(sd_bus *b) {
union {
} control = {};
int error_buf = 0;
};
.msg_control = &control,
.msg_controllen = sizeof(control),
.msg_iovlen = 1,
};
int r, fd = -1;
ssize_t n;
assert(b);
if (b->nspid <= 0) {
if (r < 0)
return r;
}
if (r < 0)
return r;
return -errno;
if (child < 0)
return -errno;
if (child == 0) {
if (r < 0)
/* We just changed PID namespace, however it will only
* take effect on the children we now fork. Hence,
* let's fork another time, and connect from this
* grandchild, so that kdbus only sees the credentials
* of this process which comes from within the
* container, and not outside of it */
grandchild = fork();
if (grandchild < 0)
if (grandchild == 0) {
if (fd < 0) {
/* Try to send error up */
}
}
if (r < 0)
}
if (r < 0)
return r;
if (n < 0)
return -errno;
int *fds;
unsigned n_fds;
if (n_fds != 1) {
return -EIO;
}
}
}
/* If there's an fd passed, we are good. */
if (fd >= 0) {
return bus_kernel_take_fd(b);
}
/* If there's an error passed, use it */
return -error_buf;
/* Otherwise, we have no clue */
return -EIO;
}