lxccontainer.c revision 735f2c6e504a541cbb2592a3f94858bf337a24ff
7cb128dc4cae2a03a742f63ba7afee23c78e3af0Phil Carmody/* liblxcapi
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen *
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen * Copyright © 2012 Canonical Ltd.
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen *
b87761f9bbef949f31dae297e619ac3f5e9c2b2eTimo Sirainen * This library is free software; you can redistribute it and/or
b87761f9bbef949f31dae297e619ac3f5e9c2b2eTimo Sirainen * modify it under the terms of the GNU Lesser General Public
a99b64f7d63812806ee40c2e8a347343fa3b84a7Timo Sirainen * License as published by the Free Software Foundation; either
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen * version 2.1 of the License, or (at your option) any later version.
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
0371406d952fe51367c7be91703e5634b7d9d225Timo Sirainen * This library is distributed in the hope that it will be useful,
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen * but WITHOUT ANY WARRANTY; without even the implied warranty of
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen * Lesser General Public License for more details.
a99b64f7d63812806ee40c2e8a347343fa3b84a7Timo Sirainen
a99b64f7d63812806ee40c2e8a347343fa3b84a7Timo Sirainen * You should have received a copy of the GNU Lesser General Public
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen * License along with this library; if not, write to the Free Software
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
22627da0fb77c1d0d9a8e8bc485ef5540b6f2e69Timo Sirainen#define _GNU_SOURCE
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen#include <assert.h>
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include <stdarg.h>
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include <pthread.h>
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include <unistd.h>
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#include <sys/types.h>
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include <sys/wait.h>
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include <sys/mount.h>
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#include <errno.h>
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen#include <fcntl.h>
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen#include <sched.h>
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen#include <dirent.h>
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen#include <sched.h>
1d132fe27d010b73aacc605b4c6257b0079f9e97Timo Sirainen#include <arpa/inet.h>
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#include <libgen.h>
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#include <stdint.h>
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include <grp.h>
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include <sys/syscall.h>
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen#include <lxc/lxccontainer.h>
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen#include <lxc/version.h>
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen#include "config.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "lxc.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "state.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "conf.h"
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#include "confile.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "console.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "cgroup.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "commands.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "log.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "bdev.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "utils.h"
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen#include "attach.h"
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen#include "monitor.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "namespace.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "lxclock.h"
68f0dfb4b2815ecbc1bd8d8a68adcfd577ec55aeTimo Sirainen#include "sync.h"
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#if HAVE_IFADDRS_H
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include <ifaddrs.h>
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen#else
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen#include <../include/ifaddrs.h>
9c2b0eb659540b9db8dd3a8a6a2515921fbe8eebTimo Sirainen#endif
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen#define MAX_BUFFER 4096
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen#define NOT_SUPPORTED_ERROR "the requested function %s is not currently supported with unprivileged containers"
892b3cbf0eba9ba455448adcf71864a409345c6dTimo Sirainen
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen/* Define faccessat() if missing from the C library */
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen#ifndef HAVE_FACCESSAT
f4c0b1874b0533bcf2df1d28d584ff02cfdae3faTimo Sirainenstatic int faccessat(int __fd, const char *__file, int __type, int __flag)
f4c0b1874b0533bcf2df1d28d584ff02cfdae3faTimo Sirainen{
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen#ifdef __NR_faccessat
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainenreturn syscall(__NR_faccessat, __fd, __file, __type, __flag);
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen#else
0db5b158a00c08955bdacc99b1e2cd1ec07f4311Timo Sirainenerrno = ENOSYS;
0db5b158a00c08955bdacc99b1e2cd1ec07f4311Timo Sirainenreturn -1;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#endif
d16b506f5540e3407d256bda35624b38a5ecf88fTimo Sirainen}
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#endif
5fbf8719b9ef072295c16bc4492f9f0ece92117dTimo Sirainen
5fbf8719b9ef072295c16bc4492f9f0ece92117dTimo Sirainen
554c1c792dc6fce1e25c74555c2da786bffde75fTimo Sirainenlxc_log_define(lxc_container, lxc);
554c1c792dc6fce1e25c74555c2da786bffde75fTimo Sirainen
5fbf8719b9ef072295c16bc4492f9f0ece92117dTimo Sirainenstatic bool config_file_exists(const char *lxcpath, const char *cname)
5fbf8719b9ef072295c16bc4492f9f0ece92117dTimo Sirainen{
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen /* $lxcpath + '/' + $cname + '/config' + \0 */
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen int ret, len = strlen(lxcpath) + strlen(cname) + 9;
6df2db16b3920346ed07cefb86e8bdcb7e1faec5Timo Sirainen char *fname = alloca(len);
6df2db16b3920346ed07cefb86e8bdcb7e1faec5Timo Sirainen
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen ret = snprintf(fname, len, "%s/%s/config", lxcpath, cname);
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen if (ret < 0 || ret >= len)
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen return false;
35f3b7e05afecacd0332c210c6e253911c2813d8Timo Sirainen
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen return file_exists(fname);
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen}
35f3b7e05afecacd0332c210c6e253911c2813d8Timo Sirainen
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen/*
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen * A few functions to help detect when a container creation failed.
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainen * If a container creation was killed partway through, then trying
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainen * to actually start that container could harm the host. We detect
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainen * this by creating a 'partial' file under the container directory,
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainen * and keeping an advisory lock. When container creation completes,
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainen * we remove that file. When we load or try to start a container, if
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainen * we find that file, without a flock, we remove the container.
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainen */
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainenstatic int ongoing_create(struct lxc_container *c)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen{
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen int len = strlen(c->config_path) + strlen(c->name) + 10;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen char *path = alloca(len);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen int fd, ret;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen struct flock lk;
407caeb5d0c8a6b158e2caef48dd909011d40340Timo Sirainen
407caeb5d0c8a6b158e2caef48dd909011d40340Timo Sirainen ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (ret < 0 || ret >= len) {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen ERROR("Error writing partial pathname");
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return -1;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen }
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (!file_exists(path))
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen return 0;
a54fa00087ba926a3d966a8449d8d7579e89911cTimo Sirainen fd = open(path, O_RDWR);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (fd < 0) {
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen // give benefit of the doubt
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen SYSERROR("Error opening partial file");
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen return 0;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen }
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen lk.l_type = F_WRLCK;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen lk.l_whence = SEEK_SET;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen lk.l_start = 0;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen lk.l_len = 0;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen lk.l_pid = -1;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (fcntl(fd, F_GETLK, &lk) == 0 && lk.l_pid != -1) {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen // create is still ongoing
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen close(fd);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return 1;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen }
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen // create completed but partial is still there.
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen close(fd);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return 2;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen}
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic int create_partial(struct lxc_container *c)
2cfe9983ce7a6280636ee12beccc2e865111967bTimo Sirainen{
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen // $lxcpath + '/' + $name + '/partial' + \0
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen int len = strlen(c->config_path) + strlen(c->name) + 10;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen char *path = alloca(len);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen int fd, ret;
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainen struct flock lk;
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (ret < 0 || ret >= len) {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen ERROR("Error writing partial pathname");
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return -1;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen }
1d132fe27d010b73aacc605b4c6257b0079f9e97Timo Sirainen if ((fd=open(path, O_RDWR | O_CREAT | O_EXCL, 0755)) < 0) {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen SYSERROR("Erorr creating partial file");
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return -1;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen }
1d132fe27d010b73aacc605b4c6257b0079f9e97Timo Sirainen lk.l_type = F_WRLCK;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen lk.l_whence = SEEK_SET;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen lk.l_start = 0;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen lk.l_len = 0;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (fcntl(fd, F_SETLKW, &lk) < 0) {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen SYSERROR("Error locking partial file %s", path);
1d132fe27d010b73aacc605b4c6257b0079f9e97Timo Sirainen close(fd);
1d132fe27d010b73aacc605b4c6257b0079f9e97Timo Sirainen return -1;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen }
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return fd;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen}
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainen
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainenstatic void remove_partial(struct lxc_container *c, int fd)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen{
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen // $lxcpath + '/' + $name + '/partial' + \0
d16b506f5540e3407d256bda35624b38a5ecf88fTimo Sirainen int len = strlen(c->config_path) + strlen(c->name) + 10;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen char *path = alloca(len);
1d132fe27d010b73aacc605b4c6257b0079f9e97Timo Sirainen int ret;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen close(fd);
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (ret < 0 || ret >= len) {
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen ERROR("Error writing partial pathname");
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen return;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen }
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (unlink(path) < 0)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen SYSERROR("Error unlink partial file %s", path);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen}
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen/* LOCKING
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen * 1. container_mem_lock(c) protects the struct lxc_container from multiple threads.
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen * 2. container_disk_lock(c) protects the on-disk container data - in particular the
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen * container configuration file.
1d132fe27d010b73aacc605b4c6257b0079f9e97Timo Sirainen * The container_disk_lock also takes the container_mem_lock.
1d132fe27d010b73aacc605b4c6257b0079f9e97Timo Sirainen * 3. thread_mutex protects process data (ex: fd table) from multiple threads.
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen * NOTHING mutexes two independent programs with their own struct
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen * lxc_container for the same c->name, between API calls. For instance,
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen * c->config_read(); c->start(); Between those calls, data on disk
a0b89f3b1df99b3a32f44623f13ad1893118825bTimo Sirainen * could change (which shouldn't bother the caller unless for instance
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen * the rootfs get moved). c->config_read(); update; c->config_write();
a0b89f3b1df99b3a32f44623f13ad1893118825bTimo Sirainen * Two such updaters could race. The callers should therefore check their
a99b64f7d63812806ee40c2e8a347343fa3b84a7Timo Sirainen * results. Trying to prevent that would necessarily expose us to deadlocks
a99b64f7d63812806ee40c2e8a347343fa3b84a7Timo Sirainen * due to hung callers. So I prefer to keep the locks only within our own
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen * functions, not across functions.
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen *
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen * If you're going to clone while holding a lxccontainer, increment
a99b64f7d63812806ee40c2e8a347343fa3b84a7Timo Sirainen * c->numthreads (under privlock) before forking. When deleting,
a99b64f7d63812806ee40c2e8a347343fa3b84a7Timo Sirainen * decrement numthreads under privlock, then if it hits 0 you can delete.
a99b64f7d63812806ee40c2e8a347343fa3b84a7Timo Sirainen * Do not ever use a lxccontainer whose numthreads you did not bump.
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen */
5fbf8719b9ef072295c16bc4492f9f0ece92117dTimo Sirainen
5fbf8719b9ef072295c16bc4492f9f0ece92117dTimo Sirainenstatic void lxc_container_free(struct lxc_container *c)
5fbf8719b9ef072295c16bc4492f9f0ece92117dTimo Sirainen{
5fbf8719b9ef072295c16bc4492f9f0ece92117dTimo Sirainen if (!c)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
5fbf8719b9ef072295c16bc4492f9f0ece92117dTimo Sirainen if (c->configfile) {
5fbf8719b9ef072295c16bc4492f9f0ece92117dTimo Sirainen free(c->configfile);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen c->configfile = NULL;
a0b89f3b1df99b3a32f44623f13ad1893118825bTimo Sirainen }
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (c->error_string) {
a99b64f7d63812806ee40c2e8a347343fa3b84a7Timo Sirainen free(c->error_string);
a99b64f7d63812806ee40c2e8a347343fa3b84a7Timo Sirainen c->error_string = NULL;
a99b64f7d63812806ee40c2e8a347343fa3b84a7Timo Sirainen }
a99b64f7d63812806ee40c2e8a347343fa3b84a7Timo Sirainen if (c->slock) {
a99b64f7d63812806ee40c2e8a347343fa3b84a7Timo Sirainen lxc_putlock(c->slock);
a99b64f7d63812806ee40c2e8a347343fa3b84a7Timo Sirainen c->slock = NULL;
a99b64f7d63812806ee40c2e8a347343fa3b84a7Timo Sirainen }
a99b64f7d63812806ee40c2e8a347343fa3b84a7Timo Sirainen if (c->privlock) {
a99b64f7d63812806ee40c2e8a347343fa3b84a7Timo Sirainen lxc_putlock(c->privlock);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen c->privlock = NULL;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen }
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (c->name) {
5fbf8719b9ef072295c16bc4492f9f0ece92117dTimo Sirainen free(c->name);
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen c->name = NULL;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen }
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (c->lxc_conf) {
1d132fe27d010b73aacc605b4c6257b0079f9e97Timo Sirainen lxc_conf_free(c->lxc_conf);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen c->lxc_conf = NULL;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen }
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen if (c->config_path) {
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen free(c->config_path);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen c->config_path = NULL;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen }
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen free(c);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen}
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
af98c413eebe0c8c49fbe785fc34915c92ebe8c1Timo Sirainen/*
af98c413eebe0c8c49fbe785fc34915c92ebe8c1Timo Sirainen * Consider the following case:
af98c413eebe0c8c49fbe785fc34915c92ebe8c1Timo Sirainenfreer | racing get()er
af98c413eebe0c8c49fbe785fc34915c92ebe8c1Timo Sirainen==================================================================
9349a0afffad990e45d3ad33081e1d2d9e68a753Timo Sirainenlxc_container_put() | lxc_container_get()
4c9c55e15f35474f53f11659e796c63b1c34e884Timo Sirainen\ lxclock(c->privlock) | c->numthreads < 1? (no)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen\ c->numthreads = 0 | \ lxclock(c->privlock) -> waits
4c9c55e15f35474f53f11659e796c63b1c34e884Timo Sirainen\ lxcunlock() | \
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen\ lxc_container_free() | \ lxclock() returns
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen | \ c->numthreads < 1 -> return 0
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen\ \ (free stuff) |
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen\ \ sem_destroy(privlock) |
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen * When the get()er checks numthreads the first time, one of the following
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen * is true:
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen * 1. freer has set numthreads = 0. get() returns 0
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen * 2. freer is between lxclock and setting numthreads to 0. get()er will
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen * sem_wait on privlock, get lxclock after freer() drops it, then see
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen * numthreads is 0 and exit without touching lxclock again..
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen * 3. freer has not yet locked privlock. If get()er runs first, then put()er
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen * will see --numthreads = 1 and not call lxc_container_free().
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen*/
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainenint lxc_container_get(struct lxc_container *c)
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen{
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen if (!c)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return 0;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen // if someone else has already started freeing the container, don't
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen // try to take the lock, which may be invalid
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (c->numthreads < 1)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return 0;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
af98c413eebe0c8c49fbe785fc34915c92ebe8c1Timo Sirainen if (container_mem_lock(c))
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen return 0;
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen if (c->numthreads < 1) {
22627da0fb77c1d0d9a8e8bc485ef5540b6f2e69Timo Sirainen // bail without trying to unlock, bc the privlock is now probably
3e28b527dd6048a40684afd29cff0ee008fc0014Timo Sirainen // in freed memory
3e28b527dd6048a40684afd29cff0ee008fc0014Timo Sirainen return 0;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen }
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen c->numthreads++;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen container_mem_unlock(c);
ccffb125d94adff0ad776de5a96e22f864d6fb0aTimo Sirainen return 1;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen}
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainenint lxc_container_put(struct lxc_container *c)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen{
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (!c)
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen return -1;
ccffb125d94adff0ad776de5a96e22f864d6fb0aTimo Sirainen if (container_mem_lock(c))
ccffb125d94adff0ad776de5a96e22f864d6fb0aTimo Sirainen return -1;
2cfe9983ce7a6280636ee12beccc2e865111967bTimo Sirainen if (--c->numthreads < 1) {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen container_mem_unlock(c);
c44f402f17f9a58ead24ac0083945cae86fb172bTimo Sirainen lxc_container_free(c);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return 1;
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainen }
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainen container_mem_unlock(c);
c44f402f17f9a58ead24ac0083945cae86fb172bTimo Sirainen return 0;
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainen}
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainen
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainenstatic bool lxcapi_is_defined(struct lxc_container *c)
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainen{
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen struct stat statbuf;
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainen bool ret = false;
c44f402f17f9a58ead24ac0083945cae86fb172bTimo Sirainen int statret;
c44f402f17f9a58ead24ac0083945cae86fb172bTimo Sirainen
c44f402f17f9a58ead24ac0083945cae86fb172bTimo Sirainen if (!c)
c44f402f17f9a58ead24ac0083945cae86fb172bTimo Sirainen return false;
c44f402f17f9a58ead24ac0083945cae86fb172bTimo Sirainen
c44f402f17f9a58ead24ac0083945cae86fb172bTimo Sirainen if (container_mem_lock(c))
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return false;
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainen if (!c->configfile)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen goto out;
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainen statret = stat(c->configfile, &statbuf);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (statret != 0)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen goto out;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen ret = true;
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenout:
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen container_mem_unlock(c);
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen return ret;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen}
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen
b457d2ecf97fb52064f9dd563fd4e8065af39dfbTimo Sirainenstatic const char *lxcapi_state(struct lxc_container *c)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen{
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen lxc_state_t s;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
a1aaf11831cab8346d6d0dc702e37b3f1d95eb43Timo Sirainen if (!c)
a1aaf11831cab8346d6d0dc702e37b3f1d95eb43Timo Sirainen return NULL;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen s = lxc_getstate(c->name, c->config_path);
a1aaf11831cab8346d6d0dc702e37b3f1d95eb43Timo Sirainen return lxc_state2str(s);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen}
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic bool is_stopped(struct lxc_container *c)
c44f402f17f9a58ead24ac0083945cae86fb172bTimo Sirainen{
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen lxc_state_t s;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen s = lxc_getstate(c->name, c->config_path);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen return (s == STOPPED);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen}
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic bool lxcapi_is_running(struct lxc_container *c)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen{
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen const char *s;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (!c)
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen return false;
e20e638805c4bd54e039891a3e92760b1dfa189aTimo Sirainen s = lxcapi_state(c);
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen if (!s || strcmp(s, "STOPPED") == 0)
e20e638805c4bd54e039891a3e92760b1dfa189aTimo Sirainen return false;
e20e638805c4bd54e039891a3e92760b1dfa189aTimo Sirainen return true;
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen}
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainenstatic bool lxcapi_freeze(struct lxc_container *c)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen{
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen int ret;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (!c)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return false;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen ret = lxc_freeze(c->name, c->config_path);
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen if (ret)
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen return false;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen return true;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen}
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainenstatic bool lxcapi_unfreeze(struct lxc_container *c)
68f0dfb4b2815ecbc1bd8d8a68adcfd577ec55aeTimo Sirainen{
e958a3c4573058f17999f0083a34080ca35e34d8Timo Sirainen int ret;
e958a3c4573058f17999f0083a34080ca35e34d8Timo Sirainen if (!c)
e958a3c4573058f17999f0083a34080ca35e34d8Timo Sirainen return false;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen ret = lxc_unfreeze(c->name, c->config_path);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (ret)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen return false;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen return true;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen}
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainenstatic int lxcapi_console_getfd(struct lxc_container *c, int *ttynum, int *masterfd)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen{
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen int ttyfd;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (!c)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen return -1;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen ttyfd = lxc_console_getfd(c, ttynum, masterfd);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen return ttyfd;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen}
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
68f0dfb4b2815ecbc1bd8d8a68adcfd577ec55aeTimo Sirainenstatic int lxcapi_console(struct lxc_container *c, int ttynum, int stdinfd,
1856c361aad526948d56d8aafd576bca94516b92Timo Sirainen int stdoutfd, int stderrfd, int escape)
1856c361aad526948d56d8aafd576bca94516b92Timo Sirainen{
68f0dfb4b2815ecbc1bd8d8a68adcfd577ec55aeTimo Sirainen return lxc_console(c, ttynum, stdinfd, stdoutfd, stderrfd, escape);
3656c91dcb8336814bebd4500e81c3dde25233e6Timo Sirainen}
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainenstatic pid_t lxcapi_init_pid(struct lxc_container *c)
2cfe9983ce7a6280636ee12beccc2e865111967bTimo Sirainen{
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (!c)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return -1;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainen return lxc_cmd_get_init_pid(c->name, c->config_path);
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainen}
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic bool load_config_locked(struct lxc_container *c, const char *fname)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen{
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (!c->lxc_conf)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen c->lxc_conf = lxc_conf_init();
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (!c->lxc_conf)
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainen return false;
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainen if (lxc_config_read(fname, c->lxc_conf, false) != 0)
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainen return false;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (!clone_update_unexp_network(c->lxc_conf))
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return false;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return true;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen}
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic bool lxcapi_load_config(struct lxc_container *c, const char *alt_file)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen{
2cfe9983ce7a6280636ee12beccc2e865111967bTimo Sirainen bool ret = false, need_disklock = false;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen int lret;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen const char *fname;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (!c)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return false;
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainen
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainen fname = c->configfile;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (alt_file)
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainen fname = alt_file;
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainen if (!fname)
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainen return false;
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainen /*
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen * If we're reading something other than the container's config,
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainen * we only need to lock the in-memory container. If loading the
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen * container's config file, take the disk lock.
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (strcmp(fname, c->configfile) == 0)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen need_disklock = true;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (need_disklock)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen lret = container_disk_lock(c);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen else
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen lret = container_mem_lock(c);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (lret)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen return false;
f119596e34bc4a7ce374f4aa5f4f1eb12061a372Timo Sirainen
f119596e34bc4a7ce374f4aa5f4f1eb12061a372Timo Sirainen ret = load_config_locked(c, fname);
f119596e34bc4a7ce374f4aa5f4f1eb12061a372Timo Sirainen
f119596e34bc4a7ce374f4aa5f4f1eb12061a372Timo Sirainen if (need_disklock)
5fbf8719b9ef072295c16bc4492f9f0ece92117dTimo Sirainen container_disk_unlock(c);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen else
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen container_mem_unlock(c);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen return ret;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen}
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic bool lxcapi_want_daemonize(struct lxc_container *c, bool state)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen{
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (!c || !c->lxc_conf)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return false;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (container_mem_lock(c)) {
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen ERROR("Error getting mem lock");
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return false;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen }
294c71436de227178c709e4d498e7be9b5d8d7feTimo Sirainen c->daemonize = state;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen /* daemonize implies close_all_fds so set it */
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (state == 1)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen c->lxc_conf->close_all_fds = 1;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen container_mem_unlock(c);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen return true;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen}
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic bool lxcapi_want_close_all_fds(struct lxc_container *c, bool state)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen{
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (!c || !c->lxc_conf)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return false;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (container_mem_lock(c)) {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen ERROR("Error getting mem lock");
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return false;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen }
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen c->lxc_conf->close_all_fds = state;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen container_mem_unlock(c);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return true;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen}
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic bool lxcapi_wait(struct lxc_container *c, const char *state, int timeout)
13a8c553f293349248b161ff851743498916e26eTimo Sirainen{
13a8c553f293349248b161ff851743498916e26eTimo Sirainen int ret;
24ce0c343cefe54af841871fa39dbc3464028b06Timo Sirainen
24ce0c343cefe54af841871fa39dbc3464028b06Timo Sirainen if (!c)
24ce0c343cefe54af841871fa39dbc3464028b06Timo Sirainen return false;
24ce0c343cefe54af841871fa39dbc3464028b06Timo Sirainen
24ce0c343cefe54af841871fa39dbc3464028b06Timo Sirainen ret = lxc_wait(c->name, state, timeout, c->config_path);
24ce0c343cefe54af841871fa39dbc3464028b06Timo Sirainen return ret == 0;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen}
9fcf7b79236b0045f7709718f7b65ada516565e7Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
9fcf7b79236b0045f7709718f7b65ada516565e7Timo Sirainenstatic bool wait_on_daemonized_start(struct lxc_container *c, int pid)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen{
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen /* we'll probably want to make this timeout configurable? */
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen int timeout = 5, ret, status;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen /*
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen * our child is going to fork again, then exit. reap the
24ce0c343cefe54af841871fa39dbc3464028b06Timo Sirainen * child
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen */
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen ret = waitpid(pid, &status, 0);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (ret == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 0)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen DEBUG("failed waiting for first dual-fork child");
24ce0c343cefe54af841871fa39dbc3464028b06Timo Sirainen return lxcapi_wait(c, "RUNNING", timeout);
24ce0c343cefe54af841871fa39dbc3464028b06Timo Sirainen}
24ce0c343cefe54af841871fa39dbc3464028b06Timo Sirainen
24ce0c343cefe54af841871fa39dbc3464028b06Timo Sirainenstatic bool am_single_threaded(void)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen{
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen struct dirent dirent, *direntp;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen DIR *dir;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen int count=0;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
5fbf8719b9ef072295c16bc4492f9f0ece92117dTimo Sirainen dir = opendir("/proc/self/task");
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (!dir) {
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen INFO("failed to open /proc/self/task");
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen return false;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen }
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen while (!readdir_r(dir, &dirent, &direntp)) {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (!direntp)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen break;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
3656c91dcb8336814bebd4500e81c3dde25233e6Timo Sirainen if (!strcmp(direntp->d_name, "."))
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen continue;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (!strcmp(direntp->d_name, ".."))
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen continue;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (++count > 1)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen break;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen }
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen closedir(dir);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return count == 1;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen}
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen/*
3656c91dcb8336814bebd4500e81c3dde25233e6Timo Sirainen * I can't decide if it'd be more convenient for callers if we accept '...',
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen * or a null-terminated array (i.e. execl vs execv)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic bool lxcapi_start(struct lxc_container *c, int useinit, char * const argv[])
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen{
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen int ret;
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen struct lxc_conf *conf;
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen bool daemonize = false;
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen FILE *pid_fp = NULL;
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen char *default_args[] = {
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen "/sbin/init",
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen NULL,
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen };
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen /* container exists */
36c4702131e5a04984ad5d07cf5d8d5c633d43c3Timo Sirainen if (!c)
36c4702131e5a04984ad5d07cf5d8d5c633d43c3Timo Sirainen return false;
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen /* container has been setup */
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (!c->lxc_conf)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen return false;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
5fe06fea9fee0f5e4e9cb49f6866877223f78b85Timo Sirainen if ((ret = ongoing_create(c)) < 0) {
5fe06fea9fee0f5e4e9cb49f6866877223f78b85Timo Sirainen ERROR("Error checking for incomplete creation");
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen return false;
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen }
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen if (ret == 2) {
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen ERROR("Error: %s creation was not completed", c->name);
36c4702131e5a04984ad5d07cf5d8d5c633d43c3Timo Sirainen c->destroy(c);
ab1b9a793d57a60c230a41f65f1a25d52c026233Timo Sirainen return false;
36c4702131e5a04984ad5d07cf5d8d5c633d43c3Timo Sirainen } else if (ret == 1) {
36c4702131e5a04984ad5d07cf5d8d5c633d43c3Timo Sirainen ERROR("Error: creation of %s is ongoing", c->name);
36c4702131e5a04984ad5d07cf5d8d5c633d43c3Timo Sirainen return false;
36c4702131e5a04984ad5d07cf5d8d5c633d43c3Timo Sirainen }
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen /* is this app meant to be run through lxcinit, as in lxc-execute? */
36c4702131e5a04984ad5d07cf5d8d5c633d43c3Timo Sirainen if (useinit && !argv)
36c4702131e5a04984ad5d07cf5d8d5c633d43c3Timo Sirainen return false;
36c4702131e5a04984ad5d07cf5d8d5c633d43c3Timo Sirainen
36c4702131e5a04984ad5d07cf5d8d5c633d43c3Timo Sirainen if (container_mem_lock(c))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen return false;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen conf = c->lxc_conf;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen daemonize = c->daemonize;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen container_mem_unlock(c);
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (useinit) {
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen ret = lxc_execute(c->name, argv, 1, conf, c->config_path);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen return ret == 0 ? true : false;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen }
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (!argv)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen argv = default_args;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen /*
840a3701b7a0f7fadd17738998c33790a8dfad2dTimo Sirainen * say, I'm not sure - what locks do we want here? Any?
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen * Is liblxc's locking enough here to protect the on disk
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen * container? We don't want to exclude things like lxc_info
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen * while container is running...
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen */
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (daemonize) {
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen lxc_monitord_spawn(c->config_path);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen pid_t pid = fork();
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (pid < 0)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen return false;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (pid != 0) {
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen /* Set to NULL because we don't want father unlink
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen * the PID file, child will do the free and unlink.
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen */
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen c->pidfile = NULL;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen return wait_on_daemonized_start(c, pid);
f6e301cb2060c4367d8145e2bf5d553ba87ceb34Timo Sirainen }
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen
06f537a8e0b399222cc2a7755015ef3963525fd2Timo Sirainen /* second fork to be reparented by init */
06f537a8e0b399222cc2a7755015ef3963525fd2Timo Sirainen pid = fork();
06f537a8e0b399222cc2a7755015ef3963525fd2Timo Sirainen if (pid < 0) {
06f537a8e0b399222cc2a7755015ef3963525fd2Timo Sirainen SYSERROR("Error doing dual-fork");
06f537a8e0b399222cc2a7755015ef3963525fd2Timo Sirainen return false;
87b8a4b6805d0b13af6a417dc70bdc74027bf1d3Timo Sirainen }
87b8a4b6805d0b13af6a417dc70bdc74027bf1d3Timo Sirainen if (pid != 0)
87b8a4b6805d0b13af6a417dc70bdc74027bf1d3Timo Sirainen exit(0);
06f537a8e0b399222cc2a7755015ef3963525fd2Timo Sirainen /* like daemon(), chdir to / and redirect 0,1,2 to /dev/null */
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen if (chdir("/")) {
06f537a8e0b399222cc2a7755015ef3963525fd2Timo Sirainen SYSERROR("Error chdir()ing to /.");
06f537a8e0b399222cc2a7755015ef3963525fd2Timo Sirainen return false;
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen }
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen lxc_check_inherited(conf, -1);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen close(0);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen close(1);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen close(2);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen open("/dev/zero", O_RDONLY);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen open("/dev/null", O_RDWR);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen open("/dev/null", O_RDWR);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen setsid();
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen } else {
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (!am_single_threaded()) {
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen ERROR("Cannot start non-daemonized container when threaded");
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen return false;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen }
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen }
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen /* We need to write PID file after daeminize, so we always
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen * write the right PID.
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (c->pidfile) {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen pid_fp = fopen(c->pidfile, "w");
26cdaf7097427fa90343260fa236af12ab93cca3Timo Sirainen if (pid_fp == NULL) {
26cdaf7097427fa90343260fa236af12ab93cca3Timo Sirainen SYSERROR("Failed to create pidfile '%s' for '%s'",
26cdaf7097427fa90343260fa236af12ab93cca3Timo Sirainen c->pidfile, c->name);
26cdaf7097427fa90343260fa236af12ab93cca3Timo Sirainen return false;
26cdaf7097427fa90343260fa236af12ab93cca3Timo Sirainen }
26cdaf7097427fa90343260fa236af12ab93cca3Timo Sirainen
26cdaf7097427fa90343260fa236af12ab93cca3Timo Sirainen if (fprintf(pid_fp, "%d\n", getpid()) < 0) {
26cdaf7097427fa90343260fa236af12ab93cca3Timo Sirainen SYSERROR("Failed to write '%s'", c->pidfile);
26cdaf7097427fa90343260fa236af12ab93cca3Timo Sirainen fclose(pid_fp);
26cdaf7097427fa90343260fa236af12ab93cca3Timo Sirainen pid_fp = NULL;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return false;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen }
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen fclose(pid_fp);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen pid_fp = NULL;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen }
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenreboot:
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen conf->reboot = 0;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen ret = lxc_start(c->name, argv, conf, c->config_path);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen c->error_num = ret;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (conf->reboot) {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen INFO("container requested reboot");
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen conf->reboot = 0;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen goto reboot;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen }
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (c->pidfile) {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen unlink(c->pidfile);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen free(c->pidfile);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen c->pidfile = NULL;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen }
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (daemonize)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen exit (ret == 0 ? true : false);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen else
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return (ret == 0 ? true : false);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen}
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen/*
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen * note there MUST be an ending NULL
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic bool lxcapi_startl(struct lxc_container *c, int useinit, ...)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen{
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen va_list ap;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen char **inargs = NULL;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen bool bret = false;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen /* container exists */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (!c)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return false;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen va_start(ap, useinit);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen inargs = lxc_va_arg_list_to_argv(ap, 0, 1);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen va_end(ap);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (!inargs) {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen ERROR("Memory allocation error.");
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen goto out;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen }
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen /* pass NULL if no arguments were supplied */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen bret = lxcapi_start(c, useinit, *inargs ? inargs : NULL);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenout:
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (inargs) {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen char **arg;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen for (arg = inargs; *arg; arg++)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen free(*arg);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen free(inargs);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen }
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return bret;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen}
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic bool lxcapi_stop(struct lxc_container *c)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen{
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen int ret;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (!c)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return false;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen ret = lxc_cmd_stop(c->name, c->config_path);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return ret == 0;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen}
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic int do_create_container_dir(const char *path, struct lxc_conf *conf)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen{
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen int ret = -1, lasterr;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen char *p = alloca(strlen(path)+1);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen mode_t mask = umask(0002);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen ret = mkdir(path, 0770);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen lasterr = errno;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen umask(mask);
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen errno = lasterr;
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen if (ret) {
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen if (errno == EEXIST)
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen ret = 0;
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen else {
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen SYSERROR("failed to create container path %s", path);
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen return -1;
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen }
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen }
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen strcpy(p, path);
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen if (!lxc_list_empty(&conf->id_map) && chown_mapped_root(p, conf) != 0) {
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen ERROR("Failed to chown container dir");
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen ret = -1;
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen }
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen return ret;
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen}
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen/*
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen * create the standard expected container dir
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen */
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainenstatic bool create_container_dir(struct lxc_container *c)
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen{
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen char *s;
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen int len, ret;
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen len = strlen(c->config_path) + strlen(c->name) + 2;
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen s = malloc(len);
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen if (!s)
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen return false;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen ret = snprintf(s, len, "%s/%s", c->config_path, c->name);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (ret < 0 || ret >= len) {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen free(s);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return false;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen }
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen ret = do_create_container_dir(s, c->lxc_conf);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen free(s);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return ret == 0;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen}
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic const char *lxcapi_get_config_path(struct lxc_container *c);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic bool lxcapi_set_config_item(struct lxc_container *c, const char *key, const char *v);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen/*
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen * do_bdev_create: thin wrapper around bdev_create(). Like bdev_create(),
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen * it returns a mounted bdev on success, NULL on error.
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic struct bdev *do_bdev_create(struct lxc_container *c, const char *type,
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen struct bdev_specs *specs)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen{
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen char *dest;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen size_t len;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen struct bdev *bdev;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen int ret;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen /* rootfs.path or lxcpath/lxcname/rootfs */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (c->lxc_conf->rootfs.path && access(c->lxc_conf->rootfs.path, F_OK) == 0) {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen const char *rpath = c->lxc_conf->rootfs.path;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen len = strlen(rpath) + 1;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen dest = alloca(len);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen ret = snprintf(dest, len, "%s", rpath);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen } else {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen const char *lxcpath = lxcapi_get_config_path(c);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen len = strlen(c->name) + strlen(lxcpath) + 9;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen dest = alloca(len);
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen ret = snprintf(dest, len, "%s/%s/rootfs", lxcpath, c->name);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen }
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (ret < 0 || ret >= len)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return NULL;
9672bb2a11c37c275d695451accd824da5c9e485Timo Sirainen
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen bdev = bdev_create(dest, type, c->name, specs);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (!bdev) {
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen ERROR("Failed to create backing store type %s", type);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen return NULL;
9672bb2a11c37c275d695451accd824da5c9e485Timo Sirainen }
9672bb2a11c37c275d695451accd824da5c9e485Timo Sirainen
9672bb2a11c37c275d695451accd824da5c9e485Timo Sirainen lxcapi_set_config_item(c, "lxc.rootfs", bdev->src);
9672bb2a11c37c275d695451accd824da5c9e485Timo Sirainen
9672bb2a11c37c275d695451accd824da5c9e485Timo Sirainen /* if we are not root, chown the rootfs dir to root in the
9672bb2a11c37c275d695451accd824da5c9e485Timo Sirainen * target uidmap */
9672bb2a11c37c275d695451accd824da5c9e485Timo Sirainen
9672bb2a11c37c275d695451accd824da5c9e485Timo Sirainen if (geteuid() != 0 || (c->lxc_conf && !lxc_list_empty(&c->lxc_conf->id_map))) {
9672bb2a11c37c275d695451accd824da5c9e485Timo Sirainen if (chown_mapped_root(bdev->dest, c->lxc_conf) < 0) {
9672bb2a11c37c275d695451accd824da5c9e485Timo Sirainen ERROR("Error chowning %s to container root", bdev->dest);
9672bb2a11c37c275d695451accd824da5c9e485Timo Sirainen suggest_default_idmap();
9672bb2a11c37c275d695451accd824da5c9e485Timo Sirainen bdev_put(bdev);
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen return NULL;
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen }
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen }
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen return bdev;
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen}
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen/*
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen * Given the '-t' template option to lxc-create, figure out what to
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen * do. If the template is a full executable path, use that. If it
9c2b0eb659540b9db8dd3a8a6a2515921fbe8eebTimo Sirainen * is something like 'sshd', then return $templatepath/lxc-sshd.
892b3cbf0eba9ba455448adcf71864a409345c6dTimo Sirainen * On success return the template, on error return NULL.
892b3cbf0eba9ba455448adcf71864a409345c6dTimo Sirainen */
892b3cbf0eba9ba455448adcf71864a409345c6dTimo Sirainenstatic char *get_template_path(const char *t)
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen{
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen int ret, len;
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen char *tpath;
9c2b0eb659540b9db8dd3a8a6a2515921fbe8eebTimo Sirainen
9c2b0eb659540b9db8dd3a8a6a2515921fbe8eebTimo Sirainen if (t[0] == '/' && access(t, X_OK) == 0) {
9c2b0eb659540b9db8dd3a8a6a2515921fbe8eebTimo Sirainen tpath = strdup(t);
9c2b0eb659540b9db8dd3a8a6a2515921fbe8eebTimo Sirainen return tpath;
9c2b0eb659540b9db8dd3a8a6a2515921fbe8eebTimo Sirainen }
9c2b0eb659540b9db8dd3a8a6a2515921fbe8eebTimo Sirainen
9c2b0eb659540b9db8dd3a8a6a2515921fbe8eebTimo Sirainen len = strlen(LXCTEMPLATEDIR) + strlen(t) + strlen("/lxc-") + 1;
892b3cbf0eba9ba455448adcf71864a409345c6dTimo Sirainen tpath = malloc(len);
9c2b0eb659540b9db8dd3a8a6a2515921fbe8eebTimo Sirainen if (!tpath)
9c2b0eb659540b9db8dd3a8a6a2515921fbe8eebTimo Sirainen return NULL;
9c2b0eb659540b9db8dd3a8a6a2515921fbe8eebTimo Sirainen ret = snprintf(tpath, len, "%s/lxc-%s", LXCTEMPLATEDIR, t);
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen if (ret < 0 || ret >= len) {
bfdf0fd7b6186f64cbdcbf1cb2bf9c42a9007b77Timo Sirainen free(tpath);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen return NULL;
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen }
bfdf0fd7b6186f64cbdcbf1cb2bf9c42a9007b77Timo Sirainen if (access(tpath, X_OK) < 0) {
bfdf0fd7b6186f64cbdcbf1cb2bf9c42a9007b77Timo Sirainen SYSERROR("bad template: %s", t);
bfdf0fd7b6186f64cbdcbf1cb2bf9c42a9007b77Timo Sirainen free(tpath);
bfdf0fd7b6186f64cbdcbf1cb2bf9c42a9007b77Timo Sirainen return NULL;
bfdf0fd7b6186f64cbdcbf1cb2bf9c42a9007b77Timo Sirainen }
bfdf0fd7b6186f64cbdcbf1cb2bf9c42a9007b77Timo Sirainen
bfdf0fd7b6186f64cbdcbf1cb2bf9c42a9007b77Timo Sirainen return tpath;
bfdf0fd7b6186f64cbdcbf1cb2bf9c42a9007b77Timo Sirainen}
bfdf0fd7b6186f64cbdcbf1cb2bf9c42a9007b77Timo Sirainen
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainenstatic char *lxcbasename(char *path)
bfdf0fd7b6186f64cbdcbf1cb2bf9c42a9007b77Timo Sirainen{
bfdf0fd7b6186f64cbdcbf1cb2bf9c42a9007b77Timo Sirainen char *p = path + strlen(path) - 1;
bfdf0fd7b6186f64cbdcbf1cb2bf9c42a9007b77Timo Sirainen while (*p != '/' && p > path)
bfdf0fd7b6186f64cbdcbf1cb2bf9c42a9007b77Timo Sirainen p--;
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen return p;
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen}
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainenstatic bool create_run_template(struct lxc_container *c, char *tpath, bool quiet,
892b3cbf0eba9ba455448adcf71864a409345c6dTimo Sirainen char *const argv[])
892b3cbf0eba9ba455448adcf71864a409345c6dTimo Sirainen{
892b3cbf0eba9ba455448adcf71864a409345c6dTimo Sirainen pid_t pid;
892b3cbf0eba9ba455448adcf71864a409345c6dTimo Sirainen
bfdf0fd7b6186f64cbdcbf1cb2bf9c42a9007b77Timo Sirainen if (!tpath)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen return true;
892b3cbf0eba9ba455448adcf71864a409345c6dTimo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen pid = fork();
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (pid < 0) {
892b3cbf0eba9ba455448adcf71864a409345c6dTimo Sirainen SYSERROR("failed to fork task for container creation template");
892b3cbf0eba9ba455448adcf71864a409345c6dTimo Sirainen return false;
892b3cbf0eba9ba455448adcf71864a409345c6dTimo Sirainen }
892b3cbf0eba9ba455448adcf71864a409345c6dTimo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (pid == 0) { // child
bfdf0fd7b6186f64cbdcbf1cb2bf9c42a9007b77Timo Sirainen char *patharg, *namearg, *rootfsarg, *src;
bfdf0fd7b6186f64cbdcbf1cb2bf9c42a9007b77Timo Sirainen struct bdev *bdev = NULL;
c93aca832ee532010ead91b85fa9f614132e1be2Stephan Bosch int i;
c93aca832ee532010ead91b85fa9f614132e1be2Stephan Bosch int ret, len, nargs = 0;
c93aca832ee532010ead91b85fa9f614132e1be2Stephan Bosch char **newargv;
892b3cbf0eba9ba455448adcf71864a409345c6dTimo Sirainen struct lxc_conf *conf = c->lxc_conf;
892b3cbf0eba9ba455448adcf71864a409345c6dTimo Sirainen
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen if (quiet) {
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen close(0);
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen close(1);
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen close(2);
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen open("/dev/zero", O_RDONLY);
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen open("/dev/null", O_RDWR);
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen open("/dev/null", O_RDWR);
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen }
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen
f4c0b1874b0533bcf2df1d28d584ff02cfdae3faTimo Sirainen src = c->lxc_conf->rootfs.path;
f4c0b1874b0533bcf2df1d28d584ff02cfdae3faTimo Sirainen /*
f4c0b1874b0533bcf2df1d28d584ff02cfdae3faTimo Sirainen * for an overlay create, what the user wants is the template to fill
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen * in what will become the readonly lower layer. So don't mount for
892b3cbf0eba9ba455448adcf71864a409345c6dTimo Sirainen * the template
892b3cbf0eba9ba455448adcf71864a409345c6dTimo Sirainen */
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (strncmp(src, "overlayfs:", 10) == 0)
892b3cbf0eba9ba455448adcf71864a409345c6dTimo Sirainen src = overlay_getlower(src+10);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (strncmp(src, "aufs:", 5) == 0)
f4c0b1874b0533bcf2df1d28d584ff02cfdae3faTimo Sirainen src = overlay_getlower(src+5);
f4c0b1874b0533bcf2df1d28d584ff02cfdae3faTimo Sirainen
892b3cbf0eba9ba455448adcf71864a409345c6dTimo Sirainen bdev = bdev_init(c->lxc_conf, src, c->lxc_conf->rootfs.mount, NULL);
892b3cbf0eba9ba455448adcf71864a409345c6dTimo Sirainen if (!bdev) {
bfdf0fd7b6186f64cbdcbf1cb2bf9c42a9007b77Timo Sirainen ERROR("Error opening rootfs");
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen exit(1);
892b3cbf0eba9ba455448adcf71864a409345c6dTimo Sirainen }
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (geteuid() == 0) {
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (unshare(CLONE_NEWNS) < 0) {
892b3cbf0eba9ba455448adcf71864a409345c6dTimo Sirainen ERROR("error unsharing mounts");
892b3cbf0eba9ba455448adcf71864a409345c6dTimo Sirainen exit(1);
28cddf411c475eb8bb84b4023398bb12346ce5adTimo Sirainen }
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen if (detect_shared_rootfs()) {
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL)) {
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen SYSERROR("Failed to make / rslave to run template");
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen ERROR("Continuing...");
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen }
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen }
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen }
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (strcmp(bdev->type, "dir") && strcmp(bdev->type, "btrfs")) {
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (geteuid() != 0) {
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen ERROR("non-root users can only create btrfs and directory-backed containers");
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen exit(1);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen }
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (bdev->ops->mount(bdev) < 0) {
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen ERROR("Error mounting rootfs");
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen exit(1);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen }
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen } else { // TODO come up with a better way here!
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (bdev->dest)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen free(bdev->dest);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen bdev->dest = strdup(bdev->src);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen }
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen /*
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen * create our new array, pre-pend the template name and
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen * base args
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen */
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (argv)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen for (nargs = 0; argv[nargs]; nargs++) ;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen nargs += 4; // template, path, rootfs and name args
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen newargv = malloc(nargs * sizeof(*newargv));
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (!newargv)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen exit(1);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen newargv[0] = lxcbasename(tpath);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen len = strlen(c->config_path) + strlen(c->name) + strlen("--path=") + 2;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen patharg = malloc(len);
c93aca832ee532010ead91b85fa9f614132e1be2Stephan Bosch if (!patharg)
c93aca832ee532010ead91b85fa9f614132e1be2Stephan Bosch exit(1);
c93aca832ee532010ead91b85fa9f614132e1be2Stephan Bosch ret = snprintf(patharg, len, "--path=%s/%s", c->config_path, c->name);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (ret < 0 || ret >= len)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen exit(1);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen newargv[1] = patharg;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen len = strlen("--name=") + strlen(c->name) + 1;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen namearg = malloc(len);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (!namearg)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen exit(1);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen ret = snprintf(namearg, len, "--name=%s", c->name);
a343811459d1d2259e668f369646a20f02301c2cTimo Sirainen if (ret < 0 || ret >= len)
a343811459d1d2259e668f369646a20f02301c2cTimo Sirainen exit(1);
a343811459d1d2259e668f369646a20f02301c2cTimo Sirainen newargv[2] = namearg;
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen
a343811459d1d2259e668f369646a20f02301c2cTimo Sirainen len = strlen("--rootfs=") + 1 + strlen(bdev->dest);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen rootfsarg = malloc(len);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (!rootfsarg)
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen exit(1);
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen ret = snprintf(rootfsarg, len, "--rootfs=%s", bdev->dest);
a343811459d1d2259e668f369646a20f02301c2cTimo Sirainen if (ret < 0 || ret >= len)
892b3cbf0eba9ba455448adcf71864a409345c6dTimo Sirainen exit(1);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen newargv[3] = rootfsarg;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
892b3cbf0eba9ba455448adcf71864a409345c6dTimo Sirainen /* add passed-in args */
892b3cbf0eba9ba455448adcf71864a409345c6dTimo Sirainen if (argv)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen for (i = 4; i < nargs; i++)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen newargv[i] = argv[i-4];
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
892b3cbf0eba9ba455448adcf71864a409345c6dTimo Sirainen /* add trailing NULL */
892b3cbf0eba9ba455448adcf71864a409345c6dTimo Sirainen nargs++;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen newargv = realloc(newargv, nargs * sizeof(*newargv));
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (!newargv)
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen exit(1);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen newargv[nargs - 1] = NULL;
f4c0b1874b0533bcf2df1d28d584ff02cfdae3faTimo Sirainen
f4c0b1874b0533bcf2df1d28d584ff02cfdae3faTimo Sirainen /*
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen * If we're running the template in a mapped userns, then
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen * we prepend the template command with:
bfdf0fd7b6186f64cbdcbf1cb2bf9c42a9007b77Timo Sirainen * lxc-usernsexec <-m map1> ... <-m mapn> --
bfdf0fd7b6186f64cbdcbf1cb2bf9c42a9007b77Timo Sirainen * and we append "--mapped-uid x", where x is the mapped uid
bfdf0fd7b6186f64cbdcbf1cb2bf9c42a9007b77Timo Sirainen * for our geteuid()
c93aca832ee532010ead91b85fa9f614132e1be2Stephan Bosch */
c93aca832ee532010ead91b85fa9f614132e1be2Stephan Bosch if (!lxc_list_empty(&conf->id_map)) {
c93aca832ee532010ead91b85fa9f614132e1be2Stephan Bosch int n2args = 1;
bfdf0fd7b6186f64cbdcbf1cb2bf9c42a9007b77Timo Sirainen char txtuid[20];
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen char txtgid[20];
892b3cbf0eba9ba455448adcf71864a409345c6dTimo Sirainen char **n2 = malloc(n2args * sizeof(*n2));
3656c91dcb8336814bebd4500e81c3dde25233e6Timo Sirainen struct lxc_list *it;
a343811459d1d2259e668f369646a20f02301c2cTimo Sirainen struct id_map *map;
a343811459d1d2259e668f369646a20f02301c2cTimo Sirainen
a343811459d1d2259e668f369646a20f02301c2cTimo Sirainen if (!n2) {
a343811459d1d2259e668f369646a20f02301c2cTimo Sirainen SYSERROR("out of memory");
a343811459d1d2259e668f369646a20f02301c2cTimo Sirainen exit(1);
a343811459d1d2259e668f369646a20f02301c2cTimo Sirainen }
a343811459d1d2259e668f369646a20f02301c2cTimo Sirainen newargv[0] = tpath;
a343811459d1d2259e668f369646a20f02301c2cTimo Sirainen tpath = "lxc-usernsexec";
a343811459d1d2259e668f369646a20f02301c2cTimo Sirainen n2[0] = "lxc-usernsexec";
a343811459d1d2259e668f369646a20f02301c2cTimo Sirainen lxc_list_for_each(it, &conf->id_map) {
a343811459d1d2259e668f369646a20f02301c2cTimo Sirainen map = it->elem;
a343811459d1d2259e668f369646a20f02301c2cTimo Sirainen n2args += 2;
a343811459d1d2259e668f369646a20f02301c2cTimo Sirainen n2 = realloc(n2, n2args * sizeof(char *));
a343811459d1d2259e668f369646a20f02301c2cTimo Sirainen if (!n2)
a343811459d1d2259e668f369646a20f02301c2cTimo Sirainen exit(1);
a343811459d1d2259e668f369646a20f02301c2cTimo Sirainen n2[n2args-2] = "-m";
a343811459d1d2259e668f369646a20f02301c2cTimo Sirainen n2[n2args-1] = malloc(200);
a343811459d1d2259e668f369646a20f02301c2cTimo Sirainen if (!n2[n2args-1])
a343811459d1d2259e668f369646a20f02301c2cTimo Sirainen exit(1);
a343811459d1d2259e668f369646a20f02301c2cTimo Sirainen ret = snprintf(n2[n2args-1], 200, "%c:%lu:%lu:%lu",
a343811459d1d2259e668f369646a20f02301c2cTimo Sirainen map->idtype == ID_TYPE_UID ? 'u' : 'g',
a343811459d1d2259e668f369646a20f02301c2cTimo Sirainen map->nsid, map->hostid, map->range);
a343811459d1d2259e668f369646a20f02301c2cTimo Sirainen if (ret < 0 || ret >= 200)
a343811459d1d2259e668f369646a20f02301c2cTimo Sirainen exit(1);
a343811459d1d2259e668f369646a20f02301c2cTimo Sirainen }
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen int hostid_mapped = mapped_hostid(geteuid(), conf, ID_TYPE_UID);
892b3cbf0eba9ba455448adcf71864a409345c6dTimo Sirainen int extraargs = hostid_mapped >= 0 ? 1 : 3;
9c2b0eb659540b9db8dd3a8a6a2515921fbe8eebTimo Sirainen n2 = realloc(n2, (nargs + n2args + extraargs) * sizeof(char *));
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen if (!n2)
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen exit(1);
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen if (hostid_mapped < 0) {
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen hostid_mapped = find_unmapped_nsuid(conf, ID_TYPE_UID);
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen n2[n2args++] = "-m";
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen if (hostid_mapped < 0) {
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen ERROR("Could not find free uid to map");
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen exit(1);
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen }
892b3cbf0eba9ba455448adcf71864a409345c6dTimo Sirainen n2[n2args++] = malloc(200);
9c2b0eb659540b9db8dd3a8a6a2515921fbe8eebTimo Sirainen if (!n2[n2args-1]) {
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen SYSERROR("out of memory");
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen exit(1);
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen }
bfdf0fd7b6186f64cbdcbf1cb2bf9c42a9007b77Timo Sirainen ret = snprintf(n2[n2args-1], 200, "u:%d:%d:1",
bfdf0fd7b6186f64cbdcbf1cb2bf9c42a9007b77Timo Sirainen hostid_mapped, geteuid());
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen if (ret < 0 || ret >= 200) {
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen ERROR("string too long");
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen exit(1);
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen }
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen }
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen int hostgid_mapped = mapped_hostid(getegid(), conf, ID_TYPE_GID);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen extraargs = hostgid_mapped >= 0 ? 1 : 3;
b87761f9bbef949f31dae297e619ac3f5e9c2b2eTimo Sirainen n2 = realloc(n2, (nargs + n2args + extraargs) * sizeof(char *));
b87761f9bbef949f31dae297e619ac3f5e9c2b2eTimo Sirainen if (!n2)
b87761f9bbef949f31dae297e619ac3f5e9c2b2eTimo Sirainen exit(1);
b87761f9bbef949f31dae297e619ac3f5e9c2b2eTimo Sirainen if (hostgid_mapped < 0) {
b87761f9bbef949f31dae297e619ac3f5e9c2b2eTimo Sirainen hostgid_mapped = find_unmapped_nsuid(conf, ID_TYPE_GID);
b87761f9bbef949f31dae297e619ac3f5e9c2b2eTimo Sirainen n2[n2args++] = "-m";
b87761f9bbef949f31dae297e619ac3f5e9c2b2eTimo Sirainen if (hostgid_mapped < 0) {
b87761f9bbef949f31dae297e619ac3f5e9c2b2eTimo Sirainen ERROR("Could not find free uid to map");
b87761f9bbef949f31dae297e619ac3f5e9c2b2eTimo Sirainen exit(1);
b87761f9bbef949f31dae297e619ac3f5e9c2b2eTimo Sirainen }
b87761f9bbef949f31dae297e619ac3f5e9c2b2eTimo Sirainen n2[n2args++] = malloc(200);
b87761f9bbef949f31dae297e619ac3f5e9c2b2eTimo Sirainen if (!n2[n2args-1]) {
0db5b158a00c08955bdacc99b1e2cd1ec07f4311Timo Sirainen SYSERROR("out of memory");
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen exit(1);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen }
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen ret = snprintf(n2[n2args-1], 200, "g:%d:%d:1",
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen hostgid_mapped, getegid());
0ae010139a1bb3b29fbf117c5da1a6a6c6b7b5a0Timo Sirainen if (ret < 0 || ret >= 200) {
0ae010139a1bb3b29fbf117c5da1a6a6c6b7b5a0Timo Sirainen ERROR("string too long");
0ae010139a1bb3b29fbf117c5da1a6a6c6b7b5a0Timo Sirainen exit(1);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen }
0ae010139a1bb3b29fbf117c5da1a6a6c6b7b5a0Timo Sirainen }
0ae010139a1bb3b29fbf117c5da1a6a6c6b7b5a0Timo Sirainen n2[n2args++] = "--";
0ae010139a1bb3b29fbf117c5da1a6a6c6b7b5a0Timo Sirainen for (i = 0; i < nargs; i++)
0ae010139a1bb3b29fbf117c5da1a6a6c6b7b5a0Timo Sirainen n2[i + n2args] = newargv[i];
0ae010139a1bb3b29fbf117c5da1a6a6c6b7b5a0Timo Sirainen n2args += nargs;
0ae010139a1bb3b29fbf117c5da1a6a6c6b7b5a0Timo Sirainen // Finally add "--mapped-uid $uid" to tell template what to chown
0ae010139a1bb3b29fbf117c5da1a6a6c6b7b5a0Timo Sirainen // cached images to
0ae010139a1bb3b29fbf117c5da1a6a6c6b7b5a0Timo Sirainen n2args += 4;
0ae010139a1bb3b29fbf117c5da1a6a6c6b7b5a0Timo Sirainen n2 = realloc(n2, n2args * sizeof(char *));
0ae010139a1bb3b29fbf117c5da1a6a6c6b7b5a0Timo Sirainen if (!n2) {
b87761f9bbef949f31dae297e619ac3f5e9c2b2eTimo Sirainen SYSERROR("out of memory");
b87761f9bbef949f31dae297e619ac3f5e9c2b2eTimo Sirainen exit(1);
b87761f9bbef949f31dae297e619ac3f5e9c2b2eTimo Sirainen }
0ae010139a1bb3b29fbf117c5da1a6a6c6b7b5a0Timo Sirainen // note n2[n2args-1] is NULL
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen n2[n2args-5] = "--mapped-uid";
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen snprintf(txtuid, 20, "%d", hostid_mapped);
0db5b158a00c08955bdacc99b1e2cd1ec07f4311Timo Sirainen n2[n2args-4] = txtuid;
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen n2[n2args-3] = "--mapped-gid";
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen snprintf(txtgid, 20, "%d", hostgid_mapped);
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen n2[n2args-2] = txtgid;
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen n2[n2args-1] = NULL;
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen free(newargv);
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen newargv = n2;
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen }
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen /* execute */
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen execvp(tpath, newargv);
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen SYSERROR("failed to execute template %s", tpath);
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen exit(1);
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainen }
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (wait_for_pid(pid) != 0) {
bd63b5b860658b01b1f46f26d406e1e4a9dc019aTimo Sirainen ERROR("container creation template for %s failed", c->name);
4371df92c07fb923aabca7e90d307eccac48b2d6Timo Sirainen return false;
0371406d952fe51367c7be91703e5634b7d9d225Timo Sirainen }
0371406d952fe51367c7be91703e5634b7d9d225Timo Sirainen
0371406d952fe51367c7be91703e5634b7d9d225Timo Sirainen return true;
0371406d952fe51367c7be91703e5634b7d9d225Timo Sirainen}
0371406d952fe51367c7be91703e5634b7d9d225Timo Sirainen
0371406d952fe51367c7be91703e5634b7d9d225Timo Sirainenstatic bool prepend_lxc_header(char *path, const char *t, char *const argv[])
0371406d952fe51367c7be91703e5634b7d9d225Timo Sirainen{
0371406d952fe51367c7be91703e5634b7d9d225Timo Sirainen long flen;
0371406d952fe51367c7be91703e5634b7d9d225Timo Sirainen char *contents;
0371406d952fe51367c7be91703e5634b7d9d225Timo Sirainen FILE *f;
0371406d952fe51367c7be91703e5634b7d9d225Timo Sirainen int ret = -1;
0371406d952fe51367c7be91703e5634b7d9d225Timo Sirainen#if HAVE_LIBGNUTLS
0371406d952fe51367c7be91703e5634b7d9d225Timo Sirainen int i;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen unsigned char md_value[SHA_DIGEST_LENGTH];
char *tpath;
#endif
f = fopen(path, "r");
if (f == NULL)
return false;
if (fseek(f, 0, SEEK_END) < 0)
goto out_error;
if ((flen = ftell(f)) < 0)
goto out_error;
if (fseek(f, 0, SEEK_SET) < 0)
goto out_error;
if ((contents = malloc(flen + 1)) == NULL)
goto out_error;
if (fread(contents, 1, flen, f) != flen)
goto out_free_contents;
contents[flen] = '\0';
ret = fclose(f);
f = NULL;
if (ret < 0)
goto out_free_contents;
#if HAVE_LIBGNUTLS
tpath = get_template_path(t);
if (!tpath) {
ERROR("bad template: %s", t);
goto out_free_contents;
}
ret = sha1sum_file(tpath, md_value);
if (ret < 0) {
ERROR("Error getting sha1sum of %s", tpath);
free(tpath);
goto out_free_contents;
}
free(tpath);
#endif
f = fopen(path, "w");
if (f == NULL) {
SYSERROR("reopening config for writing");
free(contents);
return false;
}
fprintf(f, "# Template used to create this container: %s\n", t);
if (argv) {
fprintf(f, "# Parameters passed to the template:");
while (*argv) {
fprintf(f, " %s", *argv);
argv++;
}
fprintf(f, "\n");
}
#if HAVE_LIBGNUTLS
fprintf(f, "# Template script checksum (SHA-1): ");
for (i=0; i<SHA_DIGEST_LENGTH; i++)
fprintf(f, "%02x", md_value[i]);
fprintf(f, "\n");
#endif
fprintf(f, "# For additional config options, please look at lxc.container.conf(5)\n");
if (fwrite(contents, 1, flen, f) != flen) {
SYSERROR("Writing original contents");
free(contents);
fclose(f);
return false;
}
ret = 0;
out_free_contents:
free(contents);
out_error:
if (f) {
int newret;
newret = fclose(f);
if (ret == 0)
ret = newret;
}
if (ret < 0) {
SYSERROR("Error prepending header");
return false;
}
return true;
}
static void lxcapi_clear_config(struct lxc_container *c)
{
if (c) {
if (c->lxc_conf) {
lxc_conf_free(c->lxc_conf);
c->lxc_conf = NULL;
}
}
}
static bool lxcapi_destroy(struct lxc_container *c);
static bool container_destroy(struct lxc_container *c);
static bool get_snappath_dir(struct lxc_container *c, char *snappath);
/*
* lxcapi_create:
* create a container with the given parameters.
* @c: container to be created. It has the lxcpath, name, and a starting
* configuration already set
* @t: the template to execute to instantiate the root filesystem and
* adjust the configuration.
* @bdevtype: backing store type to use. If NULL, dir will be used.
* @specs: additional parameters for the backing store, i.e. LVM vg to
* use.
*
* @argv: the arguments to pass to the template, terminated by NULL. If no
* arguments, you can just pass NULL.
*/
static bool lxcapi_create(struct lxc_container *c, const char *t,
const char *bdevtype, struct bdev_specs *specs, int flags,
char *const argv[])
{
bool ret = false;
pid_t pid;
char *tpath = NULL;
int partial_fd;
if (!c)
return false;
if (t) {
tpath = get_template_path(t);
if (!tpath) {
ERROR("bad template: %s", t);
goto out;
}
}
/*
* If a template is passed in, and the rootfs already is defined in
* the container config and exists, then * caller is trying to create
* an existing container. Return an error, but do NOT delete the
* container.
*/
if (lxcapi_is_defined(c) && c->lxc_conf && c->lxc_conf->rootfs.path &&
access(c->lxc_conf->rootfs.path, F_OK) == 0 && tpath) {
ERROR("Container %s:%s already exists", c->config_path, c->name);
goto free_tpath;
}
if (!c->lxc_conf) {
if (!c->load_config(c, lxc_global_config_value("lxc.default_config"))) {
ERROR("Error loading default configuration file %s", lxc_global_config_value("lxc.default_config"));
goto free_tpath;
}
}
if (!create_container_dir(c))
goto free_tpath;
/*
* either template or rootfs.path should be set.
* if both template and rootfs.path are set, template is setup as rootfs.path.
* container is already created if we have a config and rootfs.path is accessible
*/
if (!c->lxc_conf->rootfs.path && !tpath)
/* no template passed in and rootfs does not exist: error */
goto out;
if (c->lxc_conf->rootfs.path && access(c->lxc_conf->rootfs.path, F_OK) != 0)
/* rootfs passed into configuration, but does not exist: error */
goto out;
if (lxcapi_is_defined(c) && c->lxc_conf->rootfs.path && !tpath) {
/* Rootfs already existed, user just wanted to save the
* loaded configuration */
ret = true;
goto out;
}
/* Mark that this container is being created */
if ((partial_fd = create_partial(c)) < 0)
goto out;
/* no need to get disk lock bc we have the partial locked */
/*
* Create the backing store
* Note we can't do this in the same task as we use to execute the
* template because of the way zfs works.
* After you 'zfs create', zfs mounts the fs only in the initial
* namespace.
*/
pid = fork();
if (pid < 0) {
SYSERROR("failed to fork task for container creation template");
goto out_unlock;
}
if (pid == 0) { // child
struct bdev *bdev = NULL;
if (!(bdev = do_bdev_create(c, bdevtype, specs))) {
ERROR("Error creating backing store type %s for %s",
bdevtype ? bdevtype : "(none)", c->name);
exit(1);
}
/* save config file again to store the new rootfs location */
if (!c->save_config(c, NULL)) {
ERROR("failed to save starting configuration for %s", c->name);
// parent task won't see bdev in config so we delete it
bdev->ops->umount(bdev);
bdev->ops->destroy(bdev);
exit(1);
}
exit(0);
}
if (wait_for_pid(pid) != 0)
goto out_unlock;
/* reload config to get the rootfs */
lxc_conf_free(c->lxc_conf);
c->lxc_conf = NULL;
if (!load_config_locked(c, c->configfile))
goto out_unlock;
if (!create_run_template(c, tpath, !!(flags & LXC_CREATE_QUIET), argv))
goto out_unlock;
// now clear out the lxc_conf we have, reload from the created
// container
lxcapi_clear_config(c);
if (t) {
if (!prepend_lxc_header(c->configfile, tpath, argv)) {
ERROR("Error prepending header to configuration file");
goto out_unlock;
}
}
ret = load_config_locked(c, c->configfile);
out_unlock:
if (partial_fd >= 0)
remove_partial(c, partial_fd);
out:
if (!ret && c)
container_destroy(c);
free_tpath:
if (tpath)
free(tpath);
return ret;
}
static bool lxcapi_reboot(struct lxc_container *c)
{
pid_t pid;
if (!c)
return false;
if (!c->is_running(c))
return false;
pid = c->init_pid(c);
if (pid <= 0)
return false;
if (kill(pid, SIGINT) < 0)
return false;
return true;
}
static bool lxcapi_shutdown(struct lxc_container *c, int timeout)
{
bool retv;
pid_t pid;
int haltsignal = SIGPWR;
if (!c)
return false;
if (!c->is_running(c))
return true;
pid = c->init_pid(c);
if (pid <= 0)
return true;
if (c->lxc_conf && c->lxc_conf->haltsignal)
haltsignal = c->lxc_conf->haltsignal;
kill(pid, haltsignal);
retv = c->wait(c, "STOPPED", timeout);
return retv;
}
static bool lxcapi_createl(struct lxc_container *c, const char *t,
const char *bdevtype, struct bdev_specs *specs, int flags, ...)
{
bool bret = false;
char **args = NULL;
va_list ap;
if (!c)
return false;
/*
* since we're going to wait for create to finish, I don't think we
* need to get a copy of the arguments.
*/
va_start(ap, flags);
args = lxc_va_arg_list_to_argv(ap, 0, 0);
va_end(ap);
if (!args) {
ERROR("Memory allocation error.");
goto out;
}
bret = c->create(c, t, bdevtype, specs, flags, args);
out:
free(args);
return bret;
}
static void do_clear_unexp_config_line(struct lxc_conf *conf, const char *key)
{
if (strcmp(key, "lxc.cgroup") == 0)
clear_unexp_config_line(conf, key, true);
else if (strcmp(key, "lxc.network") == 0)
clear_unexp_config_line(conf, key, true);
else if (strcmp(key, "lxc.hook") == 0)
clear_unexp_config_line(conf, key, true);
else
clear_unexp_config_line(conf, key, false);
if (!do_append_unexp_config_line(conf, key, ""))
WARN("Error clearing configuration for %s", key);
}
static bool lxcapi_clear_config_item(struct lxc_container *c, const char *key)
{
int ret;
if (!c || !c->lxc_conf)
return false;
if (container_mem_lock(c))
return false;
ret = lxc_clear_config_item(c->lxc_conf, key);
if (!ret)
do_clear_unexp_config_line(c->lxc_conf, key);
container_mem_unlock(c);
return ret == 0;
}
static inline bool enter_to_ns(struct lxc_container *c) {
int netns, userns, ret = 0, init_pid = 0;;
char new_netns_path[MAXPATHLEN];
char new_userns_path[MAXPATHLEN];
if (!c->is_running(c))
goto out;
init_pid = c->init_pid(c);
/* Switch to new userns */
if ((geteuid() != 0 || (c->lxc_conf && !lxc_list_empty(&c->lxc_conf->id_map))) && access("/proc/self/ns/user", F_OK) == 0) {
ret = snprintf(new_userns_path, MAXPATHLEN, "/proc/%d/ns/user", init_pid);
if (ret < 0 || ret >= MAXPATHLEN)
goto out;
userns = open(new_userns_path, O_RDONLY);
if (userns < 0) {
SYSERROR("failed to open %s", new_userns_path);
goto out;
}
if (setns(userns, CLONE_NEWUSER)) {
SYSERROR("failed to setns for CLONE_NEWUSER");
close(userns);
goto out;
}
close(userns);
}
/* Switch to new netns */
ret = snprintf(new_netns_path, MAXPATHLEN, "/proc/%d/ns/net", init_pid);
if (ret < 0 || ret >= MAXPATHLEN)
goto out;
netns = open(new_netns_path, O_RDONLY);
if (netns < 0) {
SYSERROR("failed to open %s", new_netns_path);
goto out;
}
if (setns(netns, CLONE_NEWNET)) {
SYSERROR("failed to setns for CLONE_NEWNET");
close(netns);
goto out;
}
close(netns);
return true;
out:
return false;
}
// used by qsort and bsearch functions for comparing names
static inline int string_cmp(char **first, char **second)
{
return strcmp(*first, *second);
}
// used by qsort and bsearch functions for comparing container names
static inline int container_cmp(struct lxc_container **first, struct lxc_container **second)
{
return strcmp((*first)->name, (*second)->name);
}
static bool add_to_array(char ***names, char *cname, int pos)
{
char **newnames = realloc(*names, (pos+1) * sizeof(char *));
if (!newnames) {
ERROR("Out of memory");
return false;
}
*names = newnames;
newnames[pos] = strdup(cname);
if (!newnames[pos])
return false;
// sort the arrray as we will use binary search on it
qsort(newnames, pos + 1, sizeof(char *), (int (*)(const void *,const void *))string_cmp);
return true;
}
static bool add_to_clist(struct lxc_container ***list, struct lxc_container *c, int pos, bool sort)
{
struct lxc_container **newlist = realloc(*list, (pos+1) * sizeof(struct lxc_container *));
if (!newlist) {
ERROR("Out of memory");
return false;
}
*list = newlist;
newlist[pos] = c;
// sort the arrray as we will use binary search on it
if (sort)
qsort(newlist, pos + 1, sizeof(struct lxc_container *), (int (*)(const void *,const void *))container_cmp);
return true;
}
static char** get_from_array(char ***names, char *cname, int size)
{
return (char **)bsearch(&cname, *names, size, sizeof(char *), (int (*)(const void *, const void *))string_cmp);
}
static bool array_contains(char ***names, char *cname, int size) {
if(get_from_array(names, cname, size) != NULL)
return true;
return false;
}
static bool remove_from_array(char ***names, char *cname, int size)
{
char **result = get_from_array(names, cname, size);
if (result != NULL) {
free(result);
return true;
}
return false;
}
static char** lxcapi_get_interfaces(struct lxc_container *c)
{
pid_t pid;
int i, count = 0, pipefd[2];
char **interfaces = NULL;
char interface[IFNAMSIZ];
if(pipe(pipefd) < 0) {
SYSERROR("pipe failed");
return NULL;
}
pid = fork();
if (pid < 0) {
SYSERROR("failed to fork task to get interfaces information");
close(pipefd[0]);
close(pipefd[1]);
return NULL;
}
if (pid == 0) { // child
int ret = 1, nbytes;
struct ifaddrs *interfaceArray = NULL, *tempIfAddr = NULL;
/* close the read-end of the pipe */
close(pipefd[0]);
if (!enter_to_ns(c)) {
SYSERROR("failed to enter namespace");
goto out;
}
/* Grab the list of interfaces */
if (getifaddrs(&interfaceArray)) {
SYSERROR("failed to get interfaces list");
goto out;
}
/* Iterate through the interfaces */
for (tempIfAddr = interfaceArray; tempIfAddr != NULL; tempIfAddr = tempIfAddr->ifa_next) {
nbytes = write(pipefd[1], tempIfAddr->ifa_name, IFNAMSIZ);
if (nbytes < 0) {
ERROR("write failed");
goto out;
}
count++;
}
ret = 0;
out:
if (interfaceArray)
freeifaddrs(interfaceArray);
/* close the write-end of the pipe, thus sending EOF to the reader */
close(pipefd[1]);
exit(ret);
}
/* close the write-end of the pipe */
close(pipefd[1]);
while (read(pipefd[0], &interface, IFNAMSIZ) == IFNAMSIZ) {
if (array_contains(&interfaces, interface, count))
continue;
if(!add_to_array(&interfaces, interface, count))
ERROR("PARENT: add_to_array failed");
count++;
}
if (wait_for_pid(pid) != 0) {
for(i=0;i<count;i++)
free(interfaces[i]);
free(interfaces);
interfaces = NULL;
}
/* close the read-end of the pipe */
close(pipefd[0]);
/* Append NULL to the array */
if(interfaces)
interfaces = (char **)lxc_append_null_to_array((void **)interfaces, count);
return interfaces;
}
static char** lxcapi_get_ips(struct lxc_container *c, const char* interface, const char* family, int scope)
{
pid_t pid;
int i, count = 0, pipefd[2];
char **addresses = NULL;
char address[INET6_ADDRSTRLEN];
if(pipe(pipefd) < 0) {
SYSERROR("pipe failed");
return NULL;
}
pid = fork();
if (pid < 0) {
SYSERROR("failed to fork task to get container ips");
close(pipefd[0]);
close(pipefd[1]);
return NULL;
}
if (pid == 0) { // child
int ret = 1, nbytes;
struct ifaddrs *interfaceArray = NULL, *tempIfAddr = NULL;
char addressOutputBuffer[INET6_ADDRSTRLEN];
void *tempAddrPtr = NULL;
char *address = NULL;
/* close the read-end of the pipe */
close(pipefd[0]);
if (!enter_to_ns(c)) {
SYSERROR("failed to enter namespace");
goto out;
}
/* Grab the list of interfaces */
if (getifaddrs(&interfaceArray)) {
SYSERROR("failed to get interfaces list");
goto out;
}
/* Iterate through the interfaces */
for (tempIfAddr = interfaceArray; tempIfAddr != NULL; tempIfAddr = tempIfAddr->ifa_next) {
if (tempIfAddr->ifa_addr == NULL)
continue;
if(tempIfAddr->ifa_addr->sa_family == AF_INET) {
if (family && strcmp(family, "inet"))
continue;
tempAddrPtr = &((struct sockaddr_in *)tempIfAddr->ifa_addr)->sin_addr;
}
else {
if (family && strcmp(family, "inet6"))
continue;
if (((struct sockaddr_in6 *)tempIfAddr->ifa_addr)->sin6_scope_id != scope)
continue;
tempAddrPtr = &((struct sockaddr_in6 *)tempIfAddr->ifa_addr)->sin6_addr;
}
if (interface && strcmp(interface, tempIfAddr->ifa_name))
continue;
else if (!interface && strcmp("lo", tempIfAddr->ifa_name) == 0)
continue;
address = (char *)inet_ntop(tempIfAddr->ifa_addr->sa_family,
tempAddrPtr,
addressOutputBuffer,
sizeof(addressOutputBuffer));
if (!address)
continue;
nbytes = write(pipefd[1], address, INET6_ADDRSTRLEN);
if (nbytes < 0) {
ERROR("write failed");
goto out;
}
count++;
}
ret = 0;
out:
if(interfaceArray)
freeifaddrs(interfaceArray);
/* close the write-end of the pipe, thus sending EOF to the reader */
close(pipefd[1]);
exit(ret);
}
/* close the write-end of the pipe */
close(pipefd[1]);
while (read(pipefd[0], &address, INET6_ADDRSTRLEN) == INET6_ADDRSTRLEN) {
if(!add_to_array(&addresses, address, count))
ERROR("PARENT: add_to_array failed");
count++;
}
if (wait_for_pid(pid) != 0) {
for(i=0;i<count;i++)
free(addresses[i]);
free(addresses);
addresses = NULL;
}
/* close the read-end of the pipe */
close(pipefd[0]);
/* Append NULL to the array */
if(addresses)
addresses = (char **)lxc_append_null_to_array((void **)addresses, count);
return addresses;
}
static int lxcapi_get_config_item(struct lxc_container *c, const char *key, char *retv, int inlen)
{
int ret;
if (!c || !c->lxc_conf)
return -1;
if (container_mem_lock(c))
return -1;
ret = lxc_get_config_item(c->lxc_conf, key, retv, inlen);
container_mem_unlock(c);
return ret;
}
static char* lxcapi_get_running_config_item(struct lxc_container *c, const char *key)
{
char *ret;
if (!c || !c->lxc_conf)
return NULL;
if (container_mem_lock(c))
return NULL;
ret = lxc_cmd_get_config_item(c->name, key, c->get_config_path(c));
container_mem_unlock(c);
return ret;
}
static int lxcapi_get_keys(struct lxc_container *c, const char *key, char *retv, int inlen)
{
if (!key)
return lxc_listconfigs(retv, inlen);
/*
* Support 'lxc.network.<idx>', i.e. 'lxc.network.0'
* This is an intelligent result to show which keys are valid given
* the type of nic it is
*/
if (!c || !c->lxc_conf)
return -1;
if (container_mem_lock(c))
return -1;
int ret = -1;
if (strncmp(key, "lxc.network.", 12) == 0)
ret = lxc_list_nicconfigs(c->lxc_conf, key, retv, inlen);
container_mem_unlock(c);
return ret;
}
static bool lxcapi_save_config(struct lxc_container *c, const char *alt_file)
{
FILE *fout;
bool ret = false, need_disklock = false;
int lret;
if (!alt_file)
alt_file = c->configfile;
if (!alt_file)
return false; // should we write to stdout if no file is specified?
// If we haven't yet loaded a config, load the stock config
if (!c->lxc_conf) {
if (!c->load_config(c, lxc_global_config_value("lxc.default_config"))) {
ERROR("Error loading default configuration file %s while saving %s", lxc_global_config_value("lxc.default_config"), c->name);
return false;
}
}
if (!create_container_dir(c))
return false;
/*
* If we're writing to the container's config file, take the
* disk lock. Otherwise just take the memlock to protect the
* struct lxc_container while we're traversing it.
*/
if (strcmp(c->configfile, alt_file) == 0)
need_disklock = true;
if (need_disklock)
lret = container_disk_lock(c);
else
lret = container_mem_lock(c);
if (lret)
return false;
fout = fopen(alt_file, "w");
if (!fout)
goto out;
write_config(fout, c->lxc_conf);
fclose(fout);
ret = true;
out:
if (need_disklock)
container_disk_unlock(c);
else
container_mem_unlock(c);
return ret;
}
static bool mod_rdep(struct lxc_container *c, bool inc)
{
char path[MAXPATHLEN];
int ret, v = 0;
FILE *f;
bool bret = false;
if (container_disk_lock(c))
return false;
ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_snapshots", c->config_path,
c->name);
if (ret < 0 || ret > MAXPATHLEN)
goto out;
f = fopen(path, "r");
if (f) {
ret = fscanf(f, "%d", &v);
fclose(f);
if (ret != 1) {
ERROR("Corrupted file %s", path);
goto out;
}
}
v += inc ? 1 : -1;
f = fopen(path, "w");
if (!f)
goto out;
if (fprintf(f, "%d\n", v) < 0) {
ERROR("Error writing new snapshots value");
fclose(f);
goto out;
}
ret = fclose(f);
if (ret != 0) {
SYSERROR("Error writing to or closing snapshots file");
goto out;
}
bret = true;
out:
container_disk_unlock(c);
return bret;
}
static void strip_newline(char *p)
{
size_t len = strlen(p);
if (len < 1)
return;
if (p[len-1] == '\n')
p[len-1] = '\0';
}
static void mod_all_rdeps(struct lxc_container *c, bool inc)
{
struct lxc_container *p;
char *lxcpath = NULL, *lxcname = NULL, path[MAXPATHLEN];
size_t pathlen = 0, namelen = 0;
FILE *f;
int ret;
ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_rdepends",
c->config_path, c->name);
if (ret < 0 || ret >= MAXPATHLEN) {
ERROR("Path name too long");
return;
}
f = fopen(path, "r");
if (f == NULL)
return;
while (getline(&lxcpath, &pathlen, f) != -1) {
if (getline(&lxcname, &namelen, f) == -1) {
ERROR("badly formatted file %s", path);
goto out;
}
strip_newline(lxcpath);
strip_newline(lxcname);
if ((p = lxc_container_new(lxcname, lxcpath)) == NULL) {
ERROR("Unable to find dependent container %s:%s",
lxcpath, lxcname);
continue;
}
if (!mod_rdep(p, inc))
ERROR("Failed to increase numsnapshots for %s:%s",
lxcpath, lxcname);
lxc_container_put(p);
}
out:
if (lxcpath) free(lxcpath);
if (lxcname) free(lxcname);
fclose(f);
}
static bool has_fs_snapshots(struct lxc_container *c)
{
char path[MAXPATHLEN];
int ret, v;
FILE *f;
bool bret = false;
ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_snapshots", c->config_path,
c->name);
if (ret < 0 || ret > MAXPATHLEN)
goto out;
f = fopen(path, "r");
if (!f)
goto out;
ret = fscanf(f, "%d", &v);
fclose(f);
if (ret != 1)
goto out;
bret = v != 0;
out:
return bret;
}
static bool has_snapshots(struct lxc_container *c)
{
char path[MAXPATHLEN];
struct dirent dirent, *direntp;
int count=0;
DIR *dir;
if (!get_snappath_dir(c, path))
return false;
dir = opendir(path);
if (!dir)
return false;
while (!readdir_r(dir, &dirent, &direntp)) {
if (!direntp)
break;
if (!strcmp(direntp->d_name, "."))
continue;
if (!strcmp(direntp->d_name, ".."))
continue;
count++;
break;
}
closedir(dir);
return count > 0;
}
static int lxc_rmdir_onedev_wrapper(void *data)
{
char *arg = (char *) data;
return lxc_rmdir_onedev(arg, "snaps");
}
static int do_bdev_destroy(struct lxc_conf *conf)
{
struct bdev *r;
int ret = 0;
r = bdev_init(conf, conf->rootfs.path, conf->rootfs.mount, NULL);
if (!r)
return -1;
if (r->ops->destroy(r) < 0)
ret = -1;
bdev_put(r);
return ret;
}
static int bdev_destroy_wrapper(void *data)
{
struct lxc_conf *conf = data;
if (setgid(0) < 0) {
ERROR("Failed to setgid to 0");
return -1;
}
if (setgroups(0, NULL) < 0)
WARN("Failed to clear groups");
if (setuid(0) < 0) {
ERROR("Failed to setuid to 0");
return -1;
}
return do_bdev_destroy(conf);
}
static bool container_destroy(struct lxc_container *c)
{
bool bret = false;
int ret;
if (!c || !lxcapi_is_defined(c))
return false;
if (container_disk_lock(c))
return false;
if (!is_stopped(c)) {
// we should queue some sort of error - in c->error_string?
ERROR("container %s is not stopped", c->name);
goto out;
}
if (c->lxc_conf && c->lxc_conf->rootfs.path && c->lxc_conf->rootfs.mount) {
if (am_unpriv())
ret = userns_exec_1(c->lxc_conf, bdev_destroy_wrapper, c->lxc_conf);
else
ret = do_bdev_destroy(c->lxc_conf);
if (ret < 0) {
ERROR("Error destroying rootfs for %s", c->name);
goto out;
}
}
mod_all_rdeps(c, false);
const char *p1 = lxcapi_get_config_path(c);
char *path = alloca(strlen(p1) + strlen(c->name) + 2);
sprintf(path, "%s/%s", p1, c->name);
if (am_unpriv())
ret = userns_exec_1(c->lxc_conf, lxc_rmdir_onedev_wrapper, path);
else
ret = lxc_rmdir_onedev(path, "snaps");
if (ret < 0) {
ERROR("Error destroying container directory for %s", c->name);
goto out;
}
bret = true;
out:
container_disk_unlock(c);
return bret;
}
static bool lxcapi_destroy(struct lxc_container *c)
{
if (!c || !lxcapi_is_defined(c))
return false;
if (has_snapshots(c)) {
ERROR("Container %s has snapshots; not removing", c->name);
return false;
}
if (has_fs_snapshots(c)) {
ERROR("container %s has snapshots on its rootfs", c->name);
return false;
}
return container_destroy(c);
}
static bool lxcapi_snapshot_destroy_all(struct lxc_container *c);
static bool lxcapi_destroy_with_snapshots(struct lxc_container *c)
{
if (!c || !lxcapi_is_defined(c))
return false;
if (!lxcapi_snapshot_destroy_all(c)) {
ERROR("Error deleting all snapshots");
return false;
}
return lxcapi_destroy(c);
}
static bool set_config_item_locked(struct lxc_container *c, const char *key, const char *v)
{
struct lxc_config_t *config;
if (!c->lxc_conf)
c->lxc_conf = lxc_conf_init();
if (!c->lxc_conf)
return false;
config = lxc_getconfig(key);
if (!config)
return false;
if (config->cb(key, v, c->lxc_conf) != 0)
return false;
return do_append_unexp_config_line(c->lxc_conf, key, v);
}
static bool lxcapi_set_config_item(struct lxc_container *c, const char *key, const char *v)
{
bool b = false;
if (!c)
return false;
if (container_mem_lock(c))
return false;
b = set_config_item_locked(c, key, v);
container_mem_unlock(c);
return b;
}
static char *lxcapi_config_file_name(struct lxc_container *c)
{
if (!c || !c->configfile)
return NULL;
return strdup(c->configfile);
}
static const char *lxcapi_get_config_path(struct lxc_container *c)
{
if (!c || !c->config_path)
return NULL;
return (const char *)(c->config_path);
}
/*
* not for export
* Just recalculate the c->configfile based on the
* c->config_path, which must be set.
* The lxc_container must be locked or not yet public.
*/
static bool set_config_filename(struct lxc_container *c)
{
char *newpath;
int len, ret;
if (!c->config_path)
return false;
/* $lxc_path + "/" + c->name + "/" + "config" + '\0' */
len = strlen(c->config_path) + strlen(c->name) + strlen("config") + 3;
newpath = malloc(len);
if (!newpath)
return false;
ret = snprintf(newpath, len, "%s/%s/config", c->config_path, c->name);
if (ret < 0 || ret >= len) {
fprintf(stderr, "Error printing out config file name\n");
free(newpath);
return false;
}
if (c->configfile)
free(c->configfile);
c->configfile = newpath;
return true;
}
static bool lxcapi_set_config_path(struct lxc_container *c, const char *path)
{
char *p;
bool b = false;
char *oldpath = NULL;
if (!c)
return b;
if (container_mem_lock(c))
return b;
p = strdup(path);
if (!p) {
ERROR("Out of memory setting new lxc path");
goto err;
}
b = true;
if (c->config_path)
oldpath = c->config_path;
c->config_path = p;
/* Since we've changed the config path, we have to change the
* config file name too */
if (!set_config_filename(c)) {
ERROR("Out of memory setting new config filename");
b = false;
free(c->config_path);
c->config_path = oldpath;
oldpath = NULL;
}
err:
if (oldpath)
free(oldpath);
container_mem_unlock(c);
return b;
}
static bool lxcapi_set_cgroup_item(struct lxc_container *c, const char *subsys, const char *value)
{
int ret;
if (!c)
return false;
if (is_stopped(c))
return false;
if (container_disk_lock(c))
return false;
ret = lxc_cgroup_set(subsys, value, c->name, c->config_path);
container_disk_unlock(c);
return ret == 0;
}
static int lxcapi_get_cgroup_item(struct lxc_container *c, const char *subsys, char *retv, int inlen)
{
int ret;
if (!c)
return -1;
if (is_stopped(c))
return -1;
if (container_disk_lock(c))
return -1;
ret = lxc_cgroup_get(subsys, retv, inlen, c->name, c->config_path);
container_disk_unlock(c);
return ret;
}
const char *lxc_get_global_config_item(const char *key)
{
return lxc_global_config_value(key);
}
const char *lxc_get_version(void)
{
return LXC_VERSION;
}
static int copy_file(const char *old, const char *new)
{
int in, out;
ssize_t len, ret;
char buf[8096];
struct stat sbuf;
if (file_exists(new)) {
ERROR("copy destination %s exists", new);
return -1;
}
ret = stat(old, &sbuf);
if (ret < 0) {
INFO("Error stat'ing %s", old);
return -1;
}
in = open(old, O_RDONLY);
if (in < 0) {
SYSERROR("Error opening original file %s", old);
return -1;
}
out = open(new, O_CREAT | O_EXCL | O_WRONLY, 0644);
if (out < 0) {
SYSERROR("Error opening new file %s", new);
close(in);
return -1;
}
while (1) {
len = read(in, buf, 8096);
if (len < 0) {
SYSERROR("Error reading old file %s", old);
goto err;
}
if (len == 0)
break;
ret = write(out, buf, len);
if (ret < len) { // should we retry?
SYSERROR("Error: write to new file %s was interrupted", new);
goto err;
}
}
close(in);
close(out);
// we set mode, but not owner/group
ret = chmod(new, sbuf.st_mode);
if (ret) {
SYSERROR("Error setting mode on %s", new);
return -1;
}
return 0;
err:
close(in);
close(out);
return -1;
}
static int copyhooks(struct lxc_container *oldc, struct lxc_container *c)
{
int i, len, ret;
struct lxc_list *it;
char *cpath;
len = strlen(oldc->config_path) + strlen(oldc->name) + 3;
cpath = alloca(len);
ret = snprintf(cpath, len, "%s/%s/", oldc->config_path, oldc->name);
if (ret < 0 || ret >= len)
return -1;
for (i=0; i<NUM_LXC_HOOKS; i++) {
lxc_list_for_each(it, &c->lxc_conf->hooks[i]) {
char *hookname = it->elem;
char *fname = strrchr(hookname, '/');
char tmppath[MAXPATHLEN];
if (!fname) // relative path - we don't support, but maybe we should
return 0;
if (strncmp(hookname, cpath, len - 1) != 0) {
// this hook is public - ignore
continue;
}
// copy the script, and change the entry in confile
ret = snprintf(tmppath, MAXPATHLEN, "%s/%s/%s",
c->config_path, c->name, fname+1);
if (ret < 0 || ret >= MAXPATHLEN)
return -1;
ret = copy_file(it->elem, tmppath);
if (ret < 0)
return -1;
free(it->elem);
it->elem = strdup(tmppath);
if (!it->elem) {
ERROR("out of memory copying hook path");
return -1;
}
}
}
if (!clone_update_unexp_hooks(c->lxc_conf)) {
ERROR("Error saving new hooks in clone");
return -1;
}
c->save_config(c, NULL);
return 0;
}
static void new_hwaddr(char *hwaddr)
{
FILE *f;
f = fopen("/dev/urandom", "r");
if (f) {
unsigned int seed;
int ret = fread(&seed, sizeof(seed), 1, f);
if (ret != 1)
seed = time(NULL);
fclose(f);
srand(seed);
} else
srand(time(NULL));
snprintf(hwaddr, 18, "00:16:3e:%02x:%02x:%02x",
rand() % 255, rand() % 255, rand() % 255);
}
static void network_new_hwaddrs(struct lxc_container *c)
{
struct lxc_list *it;
lxc_list_for_each(it, &c->lxc_conf->network) {
struct lxc_netdev *n = it->elem;
if (n->hwaddr)
new_hwaddr(n->hwaddr);
}
}
static int copy_fstab(struct lxc_container *oldc, struct lxc_container *c)
{
char newpath[MAXPATHLEN];
char *oldpath = oldc->lxc_conf->fstab;
int ret;
if (!oldpath)
return 0;
clear_unexp_config_line(c->lxc_conf, "lxc.mount", false);
char *p = strrchr(oldpath, '/');
if (!p)
return -1;
ret = snprintf(newpath, MAXPATHLEN, "%s/%s%s",
c->config_path, c->name, p);
if (ret < 0 || ret >= MAXPATHLEN) {
ERROR("error printing new path for %s", oldpath);
return -1;
}
if (file_exists(newpath)) {
ERROR("error: fstab file %s exists", newpath);
return -1;
}
if (copy_file(oldpath, newpath) < 0) {
ERROR("error: copying %s to %s", oldpath, newpath);
return -1;
}
free(c->lxc_conf->fstab);
c->lxc_conf->fstab = strdup(newpath);
if (!c->lxc_conf->fstab) {
ERROR("error: allocating pathname");
return -1;
}
if (!do_append_unexp_config_line(c->lxc_conf, "lxc.mount", newpath)) {
ERROR("error saving new lxctab");
return -1;
}
return 0;
}
static void copy_rdepends(struct lxc_container *c, struct lxc_container *c0)
{
char path0[MAXPATHLEN], path1[MAXPATHLEN];
int ret;
ret = snprintf(path0, MAXPATHLEN, "%s/%s/lxc_rdepends", c0->config_path,
c0->name);
if (ret < 0 || ret >= MAXPATHLEN) {
WARN("Error copying reverse dependencies");
return;
}
ret = snprintf(path1, MAXPATHLEN, "%s/%s/lxc_rdepends", c->config_path,
c->name);
if (ret < 0 || ret >= MAXPATHLEN) {
WARN("Error copying reverse dependencies");
return;
}
if (copy_file(path0, path1) < 0) {
INFO("Error copying reverse dependencies");
return;
}
}
static bool add_rdepends(struct lxc_container *c, struct lxc_container *c0)
{
int ret;
char path[MAXPATHLEN];
FILE *f;
bool bret;
ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_rdepends", c->config_path,
c->name);
if (ret < 0 || ret >= MAXPATHLEN)
return false;
f = fopen(path, "a");
if (!f)
return false;
bret = true;
// if anything goes wrong, just return an error
if (fprintf(f, "%s\n%s\n", c0->config_path, c0->name) < 0)
bret = false;
if (fclose(f) != 0)
bret = false;
return bret;
}
static int copy_storage(struct lxc_container *c0, struct lxc_container *c,
const char *newtype, int flags, const char *bdevdata, uint64_t newsize)
{
struct bdev *bdev;
int need_rdep;
bdev = bdev_copy(c0, c->name, c->config_path, newtype, flags,
bdevdata, newsize, &need_rdep);
if (!bdev) {
ERROR("Error copying storage");
return -1;
}
free(c->lxc_conf->rootfs.path);
c->lxc_conf->rootfs.path = strdup(bdev->src);
bdev_put(bdev);
if (!c->lxc_conf->rootfs.path) {
ERROR("Out of memory while setting storage path");
return -1;
}
// We will simply append a new lxc.rootfs entry to the unexpanded config
clear_unexp_config_line(c->lxc_conf, "lxc.rootfs", false);
if (!do_append_unexp_config_line(c->lxc_conf, "lxc.rootfs", c->lxc_conf->rootfs.path)) {
ERROR("Error saving new rootfs to cloend config");
return -1;
}
if (flags & LXC_CLONE_SNAPSHOT)
copy_rdepends(c, c0);
if (need_rdep) {
if (!add_rdepends(c, c0))
WARN("Error adding reverse dependency from %s to %s",
c->name, c0->name);
}
mod_all_rdeps(c, true);
return 0;
}
struct clone_update_data {
struct lxc_container *c0;
struct lxc_container *c1;
int flags;
char **hookargs;
};
static int clone_update_rootfs(struct clone_update_data *data)
{
struct lxc_container *c0 = data->c0;
struct lxc_container *c = data->c1;
int flags = data->flags;
char **hookargs = data->hookargs;
int ret = -1;
char path[MAXPATHLEN];
struct bdev *bdev;
FILE *fout;
struct lxc_conf *conf = c->lxc_conf;
/* update hostname in rootfs */
/* we're going to mount, so run in a clean namespace to simplify cleanup */
if (setgid(0) < 0) {
ERROR("Failed to setgid to 0");
return -1;
}
if (setuid(0) < 0) {
ERROR("Failed to setuid to 0");
return -1;
}
if (setgroups(0, NULL) < 0)
WARN("Failed to clear groups");
if (unshare(CLONE_NEWNS) < 0)
return -1;
bdev = bdev_init(c->lxc_conf, c->lxc_conf->rootfs.path, c->lxc_conf->rootfs.mount, NULL);
if (!bdev)
return -1;
if (strcmp(bdev->type, "dir") != 0) {
if (unshare(CLONE_NEWNS) < 0) {
ERROR("error unsharing mounts");
bdev_put(bdev);
return -1;
}
if (detect_shared_rootfs()) {
if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL)) {
SYSERROR("Failed to make / rslave");
ERROR("Continuing...");
}
}
if (bdev->ops->mount(bdev) < 0) {
bdev_put(bdev);
return -1;
}
} else { // TODO come up with a better way
if (bdev->dest)
free(bdev->dest);
bdev->dest = strdup(bdev->src);
}
if (!lxc_list_empty(&conf->hooks[LXCHOOK_CLONE])) {
/* Start of environment variable setup for hooks */
if (setenv("LXC_SRC_NAME", c0->name, 1)) {
SYSERROR("failed to set environment variable for source container name");
}
if (setenv("LXC_NAME", c->name, 1)) {
SYSERROR("failed to set environment variable for container name");
}
if (setenv("LXC_CONFIG_FILE", conf->rcfile, 1)) {
SYSERROR("failed to set environment variable for config path");
}
if (setenv("LXC_ROOTFS_MOUNT", bdev->dest, 1)) {
SYSERROR("failed to set environment variable for rootfs mount");
}
if (setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1)) {
SYSERROR("failed to set environment variable for rootfs mount");
}
if (run_lxc_hooks(c->name, "clone", conf, c->get_config_path(c), hookargs)) {
ERROR("Error executing clone hook for %s", c->name);
bdev_put(bdev);
return -1;
}
}
if (!(flags & LXC_CLONE_KEEPNAME)) {
ret = snprintf(path, MAXPATHLEN, "%s/etc/hostname", bdev->dest);
bdev_put(bdev);
if (ret < 0 || ret >= MAXPATHLEN)
return -1;
if (!file_exists(path))
return 0;
if (!(fout = fopen(path, "w"))) {
SYSERROR("unable to open %s: ignoring", path);
return 0;
}
if (fprintf(fout, "%s", c->name) < 0) {
fclose(fout);
return -1;
}
if (fclose(fout) < 0)
return -1;
}
else
bdev_put(bdev);
return 0;
}
static int clone_update_rootfs_wrapper(void *data)
{
struct clone_update_data *arg = (struct clone_update_data *) data;
return clone_update_rootfs(arg);
}
/*
* We want to support:
sudo lxc-clone -o o1 -n n1 -s -L|-fssize fssize -v|--vgname vgname \
-p|--lvprefix lvprefix -t|--fstype fstype -B backingstore
-s [ implies overlayfs]
-s -B overlayfs
-s -B aufs
only rootfs gets converted (copied/snapshotted) on clone.
*/
static int create_file_dirname(char *path, struct lxc_conf *conf)
{
char *p = strrchr(path, '/');
int ret = -1;
if (!p)
return -1;
*p = '\0';
ret = do_create_container_dir(path, conf);
*p = '/';
return ret;
}
static struct lxc_container *lxcapi_clone(struct lxc_container *c, const char *newname,
const char *lxcpath, int flags,
const char *bdevtype, const char *bdevdata, uint64_t newsize,
char **hookargs)
{
struct lxc_container *c2 = NULL;
char newpath[MAXPATHLEN];
int ret, storage_copied = 0;
char *origroot = NULL;
struct clone_update_data data;
FILE *fout;
pid_t pid;
if (!c || !c->is_defined(c))
return NULL;
if (container_mem_lock(c))
return NULL;
if (!is_stopped(c)) {
ERROR("error: Original container (%s) is running", c->name);
goto out;
}
// Make sure the container doesn't yet exist.
if (!newname)
newname = c->name;
if (!lxcpath)
lxcpath = c->get_config_path(c);
ret = snprintf(newpath, MAXPATHLEN, "%s/%s/config", lxcpath, newname);
if (ret < 0 || ret >= MAXPATHLEN) {
SYSERROR("clone: failed making config pathname");
goto out;
}
if (file_exists(newpath)) {
ERROR("error: clone: %s exists", newpath);
goto out;
}
ret = create_file_dirname(newpath, c->lxc_conf);
if (ret < 0 && errno != EEXIST) {
ERROR("Error creating container dir for %s", newpath);
goto out;
}
// copy the configuration, tweak it as needed,
if (c->lxc_conf->rootfs.path) {
origroot = c->lxc_conf->rootfs.path;
c->lxc_conf->rootfs.path = NULL;
}
fout = fopen(newpath, "w");
if (!fout) {
SYSERROR("open %s", newpath);
goto out;
}
write_config(fout, c->lxc_conf);
fclose(fout);
c->lxc_conf->rootfs.path = origroot;
sprintf(newpath, "%s/%s/rootfs", lxcpath, newname);
if (mkdir(newpath, 0755) < 0) {
SYSERROR("error creating %s", newpath);
goto out;
}
if (am_unpriv()) {
if (chown_mapped_root(newpath, c->lxc_conf) < 0) {
ERROR("Error chowning %s to container root", newpath);
goto out;
}
}
c2 = lxc_container_new(newname, lxcpath);
if (!c2) {
ERROR("clone: failed to create new container (%s %s)", newname,
lxcpath);
goto out;
}
// copy/snapshot rootfs's
ret = copy_storage(c, c2, bdevtype, flags, bdevdata, newsize);
if (ret < 0)
goto out;
clear_unexp_config_line(c2->lxc_conf, "lxc.utsname", false);
// update utsname
if (!set_config_item_locked(c2, "lxc.utsname", newname)) {
ERROR("Error setting new hostname");
goto out;
}
// copy hooks
ret = copyhooks(c, c2);
if (ret < 0) {
ERROR("error copying hooks");
goto out;
}
if (copy_fstab(c, c2) < 0) {
ERROR("error copying fstab");
goto out;
}
// update macaddrs
if (!(flags & LXC_CLONE_KEEPMACADDR)) {
network_new_hwaddrs(c2);
if (!clone_update_unexp_network(c2->lxc_conf)) {
ERROR("Error updating network for clone");
goto out;
}
}
// We've now successfully created c2's storage, so clear it out if we
// fail after this
storage_copied = 1;
if (!c2->save_config(c2, NULL))
goto out;
if ((pid = fork()) < 0) {
SYSERROR("fork");
goto out;
}
if (pid > 0) {
ret = wait_for_pid(pid);
if (ret)
goto out;
container_mem_unlock(c);
return c2;
}
data.c0 = c;
data.c1 = c2;
data.flags = flags;
data.hookargs = hookargs;
if (am_unpriv())
ret = userns_exec_1(c->lxc_conf, clone_update_rootfs_wrapper,
&data);
else
ret = clone_update_rootfs(&data);
if (ret < 0)
exit(1);
container_mem_unlock(c);
exit(0);
out:
container_mem_unlock(c);
if (c2) {
if (!storage_copied)
c2->lxc_conf->rootfs.path = NULL;
c2->destroy(c2);
lxc_container_put(c2);
}
return NULL;
}
static bool lxcapi_rename(struct lxc_container *c, const char *newname)
{
struct bdev *bdev;
struct lxc_container *newc;
if (!c || !c->name || !c->config_path || !c->lxc_conf)
return false;
if (has_fs_snapshots(c) || has_snapshots(c)) {
ERROR("Renaming a container with snapshots is not supported");
return false;
}
bdev = bdev_init(c->lxc_conf, c->lxc_conf->rootfs.path, c->lxc_conf->rootfs.mount, NULL);
if (!bdev) {
ERROR("Failed to find original backing store type");
return false;
}
newc = lxcapi_clone(c, newname, c->config_path, LXC_CLONE_KEEPMACADDR, NULL, bdev->type, 0, NULL);
bdev_put(bdev);
if (!newc) {
lxc_container_put(newc);
return false;
}
if (newc && lxcapi_is_defined(newc))
lxc_container_put(newc);
if (!container_destroy(c)) {
ERROR("Could not destroy existing container %s", c->name);
return false;
}
return true;
}
static int lxcapi_attach(struct lxc_container *c, lxc_attach_exec_t exec_function, void *exec_payload, lxc_attach_options_t *options, pid_t *attached_process)
{
if (!c)
return -1;
return lxc_attach(c->name, c->config_path, exec_function, exec_payload, options, attached_process);
}
static int lxcapi_attach_run_wait(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char * const argv[])
{
lxc_attach_command_t command;
pid_t pid;
int r;
if (!c)
return -1;
command.program = (char*)program;
command.argv = (char**)argv;
r = lxc_attach(c->name, c->config_path, lxc_attach_run_command, &command, options, &pid);
if (r < 0) {
ERROR("ups");
return r;
}
return lxc_wait_for_pid_status(pid);
}
static int get_next_index(const char *lxcpath, char *cname)
{
char *fname;
struct stat sb;
int i = 0, ret;
fname = alloca(strlen(lxcpath) + 20);
while (1) {
sprintf(fname, "%s/snap%d", lxcpath, i);
ret = stat(fname, &sb);
if (ret != 0)
return i;
i++;
}
}
static bool get_snappath_dir(struct lxc_container *c, char *snappath)
{
int ret;
/*
* If the old style snapshot path exists, use it
* /var/lib/lxc -> /var/lib/lxcsnaps
*/
ret = snprintf(snappath, MAXPATHLEN, "%ssnaps", c->config_path);
if (ret < 0 || ret >= MAXPATHLEN)
return false;
if (dir_exists(snappath)) {
ret = snprintf(snappath, MAXPATHLEN, "%ssnaps/%s", c->config_path, c->name);
if (ret < 0 || ret >= MAXPATHLEN)
return false;
return true;
}
/*
* Use the new style path
* /var/lib/lxc -> /var/lib/lxc + c->name + /snaps + \0
*/
ret = snprintf(snappath, MAXPATHLEN, "%s/%s/snaps", c->config_path, c->name);
if (ret < 0 || ret >= MAXPATHLEN)
return false;
return true;
}
static int lxcapi_snapshot(struct lxc_container *c, const char *commentfile)
{
int i, flags, ret;
struct lxc_container *c2;
char snappath[MAXPATHLEN], newname[20];
if (!c || !lxcapi_is_defined(c))
return -1;
if (!bdev_can_backup(c->lxc_conf)) {
ERROR("%s's backing store cannot be backed up.", c->name);
ERROR("Your container must use another backing store type.");
return -1;
}
if (!get_snappath_dir(c, snappath))
return -1;
i = get_next_index(snappath, c->name);
if (mkdir_p(snappath, 0755) < 0) {
ERROR("Failed to create snapshot directory %s", snappath);
return -1;
}
ret = snprintf(newname, 20, "snap%d", i);
if (ret < 0 || ret >= 20)
return -1;
/*
* We pass LXC_CLONE_SNAPSHOT to make sure that a rdepends file entry is
* created in the original container
*/
flags = LXC_CLONE_SNAPSHOT | LXC_CLONE_KEEPMACADDR | LXC_CLONE_KEEPNAME |
LXC_CLONE_KEEPBDEVTYPE | LXC_CLONE_MAYBE_SNAPSHOT;
if (bdev_is_dir(c->lxc_conf, c->lxc_conf->rootfs.path)) {
ERROR("Snapshot of directory-backed container requested.");
ERROR("Making a copy-clone. If you do want snapshots, then");
ERROR("please create an aufs or overlayfs clone first, snapshot that");
ERROR("and keep the original container pristine.");
flags &= ~LXC_CLONE_SNAPSHOT | LXC_CLONE_MAYBE_SNAPSHOT;
}
c2 = c->clone(c, newname, snappath, flags, NULL, NULL, 0, NULL);
if (!c2) {
ERROR("clone of %s:%s failed", c->config_path, c->name);
return -1;
}
lxc_container_put(c2);
// Now write down the creation time
time_t timer;
char buffer[25];
struct tm* tm_info;
FILE *f;
time(&timer);
tm_info = localtime(&timer);
strftime(buffer, 25, "%Y:%m:%d %H:%M:%S", tm_info);
char *dfnam = alloca(strlen(snappath) + strlen(newname) + 5);
sprintf(dfnam, "%s/%s/ts", snappath, newname);
f = fopen(dfnam, "w");
if (!f) {
ERROR("Failed to open %s", dfnam);
return -1;
}
if (fprintf(f, "%s", buffer) < 0) {
SYSERROR("Writing timestamp");
fclose(f);
return -1;
}
ret = fclose(f);
if (ret != 0) {
SYSERROR("Writing timestamp");
return -1;
}
if (commentfile) {
// $p / $name / comment \0
int len = strlen(snappath) + strlen(newname) + 10;
char *path = alloca(len);
sprintf(path, "%s/%s/comment", snappath, newname);
return copy_file(commentfile, path) < 0 ? -1 : i;
}
return i;
}
static void lxcsnap_free(struct lxc_snapshot *s)
{
if (s->name)
free(s->name);
if (s->comment_pathname)
free(s->comment_pathname);
if (s->timestamp)
free(s->timestamp);
if (s->lxcpath)
free(s->lxcpath);
}
static char *get_snapcomment_path(char* snappath, char *name)
{
// $snappath/$name/comment
int ret, len = strlen(snappath) + strlen(name) + 10;
char *s = malloc(len);
if (s) {
ret = snprintf(s, len, "%s/%s/comment", snappath, name);
if (ret < 0 || ret >= len) {
free(s);
s = NULL;
}
}
return s;
}
static char *get_timestamp(char* snappath, char *name)
{
char path[MAXPATHLEN], *s = NULL;
int ret, len;
FILE *fin;
ret = snprintf(path, MAXPATHLEN, "%s/%s/ts", snappath, name);
if (ret < 0 || ret >= MAXPATHLEN)
return NULL;
fin = fopen(path, "r");
if (!fin)
return NULL;
(void) fseek(fin, 0, SEEK_END);
len = ftell(fin);
(void) fseek(fin, 0, SEEK_SET);
if (len > 0) {
s = malloc(len+1);
if (s) {
s[len] = '\0';
if (fread(s, 1, len, fin) != len) {
SYSERROR("reading timestamp");
free(s);
s = NULL;
}
}
}
fclose(fin);
return s;
}
static int lxcapi_snapshot_list(struct lxc_container *c, struct lxc_snapshot **ret_snaps)
{
char snappath[MAXPATHLEN], path2[MAXPATHLEN];
int count = 0, ret;
struct dirent dirent, *direntp;
struct lxc_snapshot *snaps =NULL, *nsnaps;
DIR *dir;
if (!c || !lxcapi_is_defined(c))
return -1;
if (!get_snappath_dir(c, snappath)) {
ERROR("path name too long");
return -1;
}
dir = opendir(snappath);
if (!dir) {
INFO("failed to open %s - assuming no snapshots", snappath);
return 0;
}
while (!readdir_r(dir, &dirent, &direntp)) {
if (!direntp)
break;
if (!strcmp(direntp->d_name, "."))
continue;
if (!strcmp(direntp->d_name, ".."))
continue;
ret = snprintf(path2, MAXPATHLEN, "%s/%s/config", snappath, direntp->d_name);
if (ret < 0 || ret >= MAXPATHLEN) {
ERROR("pathname too long");
goto out_free;
}
if (!file_exists(path2))
continue;
nsnaps = realloc(snaps, (count + 1)*sizeof(*snaps));
if (!nsnaps) {
SYSERROR("Out of memory");
goto out_free;
}
snaps = nsnaps;
snaps[count].free = lxcsnap_free;
snaps[count].name = strdup(direntp->d_name);
if (!snaps[count].name)
goto out_free;
snaps[count].lxcpath = strdup(snappath);
if (!snaps[count].lxcpath) {
free(snaps[count].name);
goto out_free;
}
snaps[count].comment_pathname = get_snapcomment_path(snappath, direntp->d_name);
snaps[count].timestamp = get_timestamp(snappath, direntp->d_name);
count++;
}
if (closedir(dir))
WARN("failed to close directory");
*ret_snaps = snaps;
return count;
out_free:
if (snaps) {
int i;
for (i=0; i<count; i++)
lxcsnap_free(&snaps[i]);
free(snaps);
}
if (closedir(dir))
WARN("failed to close directory");
return -1;
}
static bool lxcapi_snapshot_restore(struct lxc_container *c, const char *snapname, const char *newname)
{
char clonelxcpath[MAXPATHLEN];
int flags = 0;
struct lxc_container *snap, *rest;
struct bdev *bdev;
bool b = false;
if (!c || !c->name || !c->config_path)
return false;
if (has_fs_snapshots(c)) {
ERROR("container rootfs has dependent snapshots");
return false;
}
bdev = bdev_init(c->lxc_conf, c->lxc_conf->rootfs.path, c->lxc_conf->rootfs.mount, NULL);
if (!bdev) {
ERROR("Failed to find original backing store type");
return false;
}
if (!newname)
newname = c->name;
if (!get_snappath_dir(c, clonelxcpath)) {
bdev_put(bdev);
return false;
}
// how should we lock this?
snap = lxc_container_new(snapname, clonelxcpath);
if (!snap || !lxcapi_is_defined(snap)) {
ERROR("Could not open snapshot %s", snapname);
if (snap) lxc_container_put(snap);
bdev_put(bdev);
return false;
}
if (strcmp(c->name, newname) == 0) {
if (!container_destroy(c)) {
ERROR("Could not destroy existing container %s", newname);
lxc_container_put(snap);
bdev_put(bdev);
return false;
}
}
if (strcmp(bdev->type, "dir") != 0 && strcmp(bdev->type, "loop") != 0)
flags = LXC_CLONE_SNAPSHOT | LXC_CLONE_MAYBE_SNAPSHOT;
rest = lxcapi_clone(snap, newname, c->config_path, flags,
bdev->type, NULL, 0, NULL);
bdev_put(bdev);
if (rest && lxcapi_is_defined(rest))
b = true;
if (rest)
lxc_container_put(rest);
lxc_container_put(snap);
return b;
}
static bool do_snapshot_destroy(const char *snapname, const char *clonelxcpath)
{
struct lxc_container *snap = NULL;
bool bret = false;
snap = lxc_container_new(snapname, clonelxcpath);
if (!snap) {
ERROR("Could not find snapshot %s", snapname);
goto err;
}
if (!lxcapi_destroy(snap)) {
ERROR("Could not destroy snapshot %s", snapname);
goto err;
}
bret = true;
err:
if (snap)
lxc_container_put(snap);
return bret;
}
static bool remove_all_snapshots(const char *path)
{
DIR *dir;
struct dirent dirent, *direntp;
bool bret = true;
dir = opendir(path);
if (!dir) {
SYSERROR("opendir on snapshot path %s", path);
return false;
}
while (!readdir_r(dir, &dirent, &direntp)) {
if (!direntp)
break;
if (!strcmp(direntp->d_name, "."))
continue;
if (!strcmp(direntp->d_name, ".."))
continue;
if (!do_snapshot_destroy(direntp->d_name, path)) {
bret = false;
continue;
}
}
closedir(dir);
if (rmdir(path))
SYSERROR("Error removing directory %s", path);
return bret;
}
static bool lxcapi_snapshot_destroy(struct lxc_container *c, const char *snapname)
{
char clonelxcpath[MAXPATHLEN];
if (!c || !c->name || !c->config_path || !snapname)
return false;
if (!get_snappath_dir(c, clonelxcpath))
return false;
return do_snapshot_destroy(snapname, clonelxcpath);
}
static bool lxcapi_snapshot_destroy_all(struct lxc_container *c)
{
char clonelxcpath[MAXPATHLEN];
if (!c || !c->name || !c->config_path)
return false;
if (!get_snappath_dir(c, clonelxcpath))
return false;
return remove_all_snapshots(clonelxcpath);
}
static bool lxcapi_may_control(struct lxc_container *c)
{
return lxc_try_cmd(c->name, c->config_path) == 0;
}
static bool do_add_remove_node(pid_t init_pid, const char *path, bool add,
struct stat *st)
{
char chrootpath[MAXPATHLEN];
char *directory_path = NULL;
pid_t pid;
int ret;
if ((pid = fork()) < 0) {
SYSERROR("failed to fork a child helper");
return false;
}
if (pid) {
if (wait_for_pid(pid) != 0) {
ERROR("Failed to create note in guest");
return false;
}
return true;
}
/* prepare the path */
ret = snprintf(chrootpath, MAXPATHLEN, "/proc/%d/root", init_pid);
if (ret < 0 || ret >= MAXPATHLEN)
return false;
if (chroot(chrootpath) < 0)
exit(1);
if (chdir("/") < 0)
exit(1);
/* remove path if it exists */
if(faccessat(AT_FDCWD, path, F_OK, AT_SYMLINK_NOFOLLOW) == 0) {
if (unlink(path) < 0) {
ERROR("unlink failed");
exit(1);
}
}
if (!add)
exit(0);
/* create any missing directories */
directory_path = dirname(strdup(path));
if (mkdir_p(directory_path, 0755) < 0 && errno != EEXIST) {
ERROR("failed to create directory");
exit(1);
}
/* create the device node */
if (mknod(path, st->st_mode, st->st_rdev) < 0) {
ERROR("mknod failed");
exit(1);
}
exit(0);
}
static bool add_remove_device_node(struct lxc_container *c, const char *src_path, const char *dest_path, bool add)
{
int ret;
struct stat st;
char value[MAX_BUFFER];
const char *p;
/* make sure container is running */
if (!c->is_running(c)) {
ERROR("container is not running");
return false;
}
/* use src_path if dest_path is NULL otherwise use dest_path */
p = dest_path ? dest_path : src_path;
/* make sure we can access p */
if(access(p, F_OK) < 0 || stat(p, &st) < 0)
return false;
/* continue if path is character device or block device */
if (S_ISCHR(st.st_mode))
ret = snprintf(value, MAX_BUFFER, "c %d:%d rwm", major(st.st_rdev), minor(st.st_rdev));
else if (S_ISBLK(st.st_mode))
ret = snprintf(value, MAX_BUFFER, "b %d:%d rwm", major(st.st_rdev), minor(st.st_rdev));
else
return false;
/* check snprintf return code */
if (ret < 0 || ret >= MAX_BUFFER)
return false;
if (!do_add_remove_node(c->init_pid(c), p, add, &st))
return false;
/* add or remove device to/from cgroup access list */
if (add) {
if (!c->set_cgroup_item(c, "devices.allow", value)) {
ERROR("set_cgroup_item failed while adding the device node");
return false;
}
} else {
if (!c->set_cgroup_item(c, "devices.deny", value)) {
ERROR("set_cgroup_item failed while removing the device node");
return false;
}
}
return true;
}
static bool lxcapi_add_device_node(struct lxc_container *c, const char *src_path, const char *dest_path)
{
if (am_unpriv()) {
ERROR(NOT_SUPPORTED_ERROR, __FUNCTION__);
return false;
}
return add_remove_device_node(c, src_path, dest_path, true);
}
static bool lxcapi_remove_device_node(struct lxc_container *c, const char *src_path, const char *dest_path)
{
if (am_unpriv()) {
ERROR(NOT_SUPPORTED_ERROR, __FUNCTION__);
return false;
}
return add_remove_device_node(c, src_path, dest_path, false);
}
struct criu_opts {
/* The type of criu invocation, one of "dump" or "restore" */
char *action;
/* The directory to pass to criu */
char *directory;
/* The container to dump */
struct lxc_container *c;
/* Enable criu verbose mode? */
bool verbose;
/* dump: stop the container or not after dumping? */
bool stop;
/* restore: the file to write the init process' pid into */
char *pidfile;
};
/*
* @out must be 128 bytes long
*/
static int read_criu_file(const char *directory, const char *file, int netnr, char *out)
{
char path[PATH_MAX];
int ret;
FILE *f;
ret = snprintf(path, PATH_MAX, "%s/%s%d", directory, file, netnr);
if (ret < 0 || ret >= PATH_MAX) {
ERROR("%s: path too long", __func__);
return -1;
}
f = fopen(path, "r");
if (!f)
return -1;
ret = fscanf(f, "%127s", out);
fclose(f);
if (ret <= 0)
return -1;
return 0;
}
static void exec_criu(struct criu_opts *opts)
{
char **argv, log[PATH_MAX];
int static_args = 13, argc = 0, i, ret;
/* The command line always looks like:
* criu $(action) --tcp-established --file-locks --link-remap --manage-cgroups \
* --action-script foo.sh -D $(directory) -o $(directory)/$(action).log
* +1 for final NULL */
if (strcmp(opts->action, "dump") == 0) {
/* -t pid */
static_args += 2;
/* --leave-running */
if (!opts->stop)
static_args++;
} else if (strcmp(opts->action, "restore") == 0) {
/* --root $(lxc_mount_point) --restore-detached --pidfile $foo */
static_args += 5;
} else {
return;
}
if (opts->verbose)
static_args++;
ret = snprintf(log, PATH_MAX, "%s/%s.log", opts->directory, opts->action);
if (ret < 0 || ret >= PATH_MAX) {
ERROR("logfile name too long\n");
return;
}
argv = malloc(static_args * sizeof(*argv));
if (!argv)
return;
memset(argv, 0, static_args * sizeof(*argv));
#define DECLARE_ARG(arg) \
do { \
argv[argc++] = strdup(arg); \
if (!argv[argc-1]) \
goto err; \
} while (0)
argv[argc++] = on_path("criu", NULL);
if (!argv[argc-1]) {
ERROR("Couldn't find criu binary\n");
goto err;
}
DECLARE_ARG(opts->action);
DECLARE_ARG("--tcp-established");
DECLARE_ARG("--file-locks");
DECLARE_ARG("--link-remap");
DECLARE_ARG("--manage-cgroups");
DECLARE_ARG("--action-script");
DECLARE_ARG(LIBEXECDIR "/lxc/lxc-restore-net");
DECLARE_ARG("-D");
DECLARE_ARG(opts->directory);
DECLARE_ARG("-o");
DECLARE_ARG(log);
if (opts->verbose)
DECLARE_ARG("-vvvvvv");
if (strcmp(opts->action, "dump") == 0) {
char pid[32];
if (sprintf(pid, "%d", lxcapi_init_pid(opts->c)) < 0)
goto err;
DECLARE_ARG("-t");
DECLARE_ARG(pid);
if (!opts->stop)
DECLARE_ARG("--leave-running");
} else if (strcmp(opts->action, "restore") == 0) {
int netnr = 0;
struct lxc_list *it;
DECLARE_ARG("--root");
DECLARE_ARG(opts->c->lxc_conf->rootfs.mount);
DECLARE_ARG("--restore-detached");
DECLARE_ARG("--pidfile");
DECLARE_ARG(opts->pidfile);
lxc_list_for_each(it, &opts->c->lxc_conf->network) {
char eth[128], veth[128], buf[257];
void *m;
if (read_criu_file(opts->directory, "veth", netnr, veth))
goto err;
if (read_criu_file(opts->directory, "eth", netnr, eth))
goto err;
ret = snprintf(buf, 257, "%s=%s", eth, veth);
if (ret < 0 || ret >= 257)
goto err;
/* final NULL and --veth-pair eth0:vethASDF */
m = realloc(argv, (argc + 1 + 2) * sizeof(*argv));
if (!m)
goto err;
argv = m;
DECLARE_ARG("--veth-pair");
DECLARE_ARG(buf);
argv[argc] = NULL;
netnr++;
}
}
#undef DECLARE_ARG
execv(argv[0], argv);
err:
for (i = 0; argv[i]; i++)
free(argv[i]);
free(argv);
}
/* Check and make sure the container has a configuration that we know CRIU can
* dump. */
static bool criu_ok(struct lxc_container *c)
{
struct lxc_list *it;
bool found_deny_rule = false;
if (geteuid()) {
ERROR("Must be root to checkpoint\n");
return false;
}
/* We only know how to restore containers with veth networks. */
lxc_list_for_each(it, &c->lxc_conf->network) {
struct lxc_netdev *n = it->elem;
if (n->type != LXC_NET_VETH && n->type != LXC_NET_NONE) {
ERROR("Found network that is not VETH or NONE\n");
return false;
}
}
// These requirements come from http://criu.org/LXC
if (c->lxc_conf->console.path &&
strcmp(c->lxc_conf->console.path, "none") != 0) {
ERROR("lxc.console must be none\n");
return false;
}
if (c->lxc_conf->tty != 0) {
ERROR("lxc.tty must be 0\n");
return false;
}
lxc_list_for_each(it, &c->lxc_conf->cgroup) {
struct lxc_cgroup *cg = it->elem;
if (strcmp(cg->subsystem, "devices.deny") == 0 &&
strcmp(cg->value, "c 5:1 rwm") == 0) {
found_deny_rule = true;
break;
}
}
if (!found_deny_rule) {
ERROR("couldn't find devices.deny = c 5:1 rwm");
return false;
}
return true;
}
static bool lxcapi_checkpoint(struct lxc_container *c, char *directory, bool stop, bool verbose)
{
int netnr, status;
struct lxc_list *it;
bool error = false;
pid_t pid;
if (!criu_ok(c))
return false;
if (mkdir(directory, 0700) < 0 && errno != EEXIST)
return false;
netnr = 0;
lxc_list_for_each(it, &c->lxc_conf->network) {
char *veth = NULL, *bridge = NULL, veth_path[PATH_MAX], eth[128];
struct lxc_netdev *n = it->elem;
int pret;
pret = snprintf(veth_path, PATH_MAX, "lxc.network.%d.veth.pair", netnr);
if (pret < 0 || pret >= PATH_MAX) {
error = true;
goto out;
}
veth = lxcapi_get_running_config_item(c, veth_path);
if (!veth) {
/* criu_ok() checks that all interfaces are
* LXC_NET{VETH,NONE}, and VETHs should have this
* config */
assert(n->type == LXC_NET_NONE);
break;
}
pret = snprintf(veth_path, PATH_MAX, "lxc.network.%d.link", netnr);
if (pret < 0 || pret >= PATH_MAX) {
error = true;
goto out;
}
bridge = lxcapi_get_running_config_item(c, veth_path);
if (!bridge) {
error = true;
goto out;
}
pret = snprintf(veth_path, PATH_MAX, "%s/veth%d", directory, netnr);
if (pret < 0 || pret >= PATH_MAX || print_to_file(veth_path, veth) < 0) {
error = true;
goto out;
}
pret = snprintf(veth_path, PATH_MAX, "%s/bridge%d", directory, netnr);
if (pret < 0 || pret >= PATH_MAX || print_to_file(veth_path, bridge) < 0) {
error = true;
goto out;
}
if (n->name) {
if (strlen(n->name) >= 128) {
error = true;
goto out;
}
strncpy(eth, n->name, 128);
} else
sprintf(eth, "eth%d", netnr);
pret = snprintf(veth_path, PATH_MAX, "%s/eth%d", directory, netnr);
if (pret < 0 || pret >= PATH_MAX || print_to_file(veth_path, eth) < 0)
error = true;
out:
free(veth);
free(bridge);
if (error)
return false;
}
pid = fork();
if (pid < 0)
return false;
if (pid == 0) {
struct criu_opts os;
os.action = "dump";
os.directory = directory;
os.c = c;
os.stop = stop;
os.verbose = verbose;
/* exec_criu() returning is an error */
exec_criu(&os);
exit(1);
} else {
pid_t w = waitpid(pid, &status, 0);
if (w == -1) {
perror("waitpid");
return false;
}
if (WIFEXITED(status)) {
return !WEXITSTATUS(status);
}
return false;
}
}
static bool lxcapi_restore(struct lxc_container *c, char *directory, bool verbose)
{
pid_t pid;
struct lxc_list *it;
struct lxc_rootfs *rootfs;
char pidfile[L_tmpnam];
if (!criu_ok(c))
return false;
if (geteuid()) {
ERROR("Must be root to restore\n");
return false;
}
if (!tmpnam(pidfile))
return false;
struct lxc_handler *handler;
handler = lxc_init(c->name, c->lxc_conf, c->config_path);
if (!handler)
return false;
pid = fork();
if (pid < 0)
return false;
if (pid == 0) {
struct criu_opts os;
if (unshare(CLONE_NEWNS))
return false;
/* CRIU needs the lxc root bind mounted so that it is the root of some
* mount. */
rootfs = &c->lxc_conf->rootfs;
if (rootfs_is_blockdev(c->lxc_conf)) {
if (do_rootfs_setup(c->lxc_conf, c->name, c->config_path) < 0)
return false;
}
else {
if (mkdir(rootfs->mount, 0755) < 0 && errno != EEXIST)
return false;
if (mount(rootfs->path, rootfs->mount, NULL, MS_BIND, NULL) < 0) {
rmdir(rootfs->mount);
return false;
}
}
os.action = "restore";
os.directory = directory;
os.c = c;
os.pidfile = pidfile;
os.verbose = verbose;
/* exec_criu() returning is an error */
exec_criu(&os);
umount(rootfs->mount);
rmdir(rootfs->mount);
exit(1);
} else {
int status;
pid_t w = waitpid(pid, &status, 0);
if (w == -1) {
perror("waitpid");
return false;
}
if (WIFEXITED(status)) {
if (WEXITSTATUS(status)) {
return false;
}
else {
int netnr = 0, ret;
bool error = false;
FILE *f = fopen(pidfile, "r");
if (!f) {
perror("reading pidfile");
ERROR("couldn't read restore's init pidfile %s\n", pidfile);
return false;
}
ret = fscanf(f, "%d", (int*) &handler->pid);
fclose(f);
if (ret != 1) {
ERROR("reading restore pid failed");
return false;
}
if (container_mem_lock(c))
return false;
lxc_list_for_each(it, &c->lxc_conf->network) {
char eth[128], veth[128];
struct lxc_netdev *netdev = it->elem;
if (read_criu_file(directory, "veth", netnr, veth)) {
error = true;
goto out_unlock;
}
if (read_criu_file(directory, "eth", netnr, eth)) {
error = true;
goto out_unlock;
}
netdev->priv.veth_attr.pair = strdup(veth);
if (!netdev->priv.veth_attr.pair) {
error = true;
goto out_unlock;
}
netnr++;
}
out_unlock:
container_mem_unlock(c);
if (error)
return false;
if (lxc_set_state(c->name, handler, RUNNING))
return false;
}
}
if (lxc_poll(c->name, handler)) {
lxc_abort(c->name, handler);
return false;
}
}
return true;
}
static int lxcapi_attach_run_waitl(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char *arg, ...)
{
va_list ap;
const char **argv;
int ret;
if (!c)
return -1;
va_start(ap, arg);
argv = lxc_va_arg_list_to_argv_const(ap, 1);
va_end(ap);
if (!argv) {
ERROR("Memory allocation error.");
return -1;
}
argv[0] = arg;
ret = lxcapi_attach_run_wait(c, options, program, (const char * const *)argv);
free((void*)argv);
return ret;
}
struct lxc_container *lxc_container_new(const char *name, const char *configpath)
{
struct lxc_container *c;
if (!name)
return NULL;
c = malloc(sizeof(*c));
if (!c) {
fprintf(stderr, "failed to malloc lxc_container\n");
return NULL;
}
memset(c, 0, sizeof(*c));
if (configpath)
c->config_path = strdup(configpath);
else
c->config_path = strdup(lxc_global_config_value("lxc.lxcpath"));
if (!c->config_path) {
fprintf(stderr, "Out of memory\n");
goto err;
}
remove_trailing_slashes(c->config_path);
c->name = malloc(strlen(name)+1);
if (!c->name) {
fprintf(stderr, "Error allocating lxc_container name\n");
goto err;
}
strcpy(c->name, name);
c->numthreads = 1;
if (!(c->slock = lxc_newlock(c->config_path, name))) {
fprintf(stderr, "failed to create lock\n");
goto err;
}
if (!(c->privlock = lxc_newlock(NULL, NULL))) {
fprintf(stderr, "failed to alloc privlock\n");
goto err;
}
if (!set_config_filename(c)) {
fprintf(stderr, "Error allocating config file pathname\n");
goto err;
}
if (file_exists(c->configfile) && !lxcapi_load_config(c, NULL))
goto err;
if (ongoing_create(c) == 2) {
ERROR("Error: %s creation was not completed", c->name);
container_destroy(c);
lxcapi_clear_config(c);
}
c->daemonize = true;
c->pidfile = NULL;
// assign the member functions
c->is_defined = lxcapi_is_defined;
c->state = lxcapi_state;
c->is_running = lxcapi_is_running;
c->freeze = lxcapi_freeze;
c->unfreeze = lxcapi_unfreeze;
c->console = lxcapi_console;
c->console_getfd = lxcapi_console_getfd;
c->init_pid = lxcapi_init_pid;
c->load_config = lxcapi_load_config;
c->want_daemonize = lxcapi_want_daemonize;
c->want_close_all_fds = lxcapi_want_close_all_fds;
c->start = lxcapi_start;
c->startl = lxcapi_startl;
c->stop = lxcapi_stop;
c->config_file_name = lxcapi_config_file_name;
c->wait = lxcapi_wait;
c->set_config_item = lxcapi_set_config_item;
c->destroy = lxcapi_destroy;
c->destroy_with_snapshots = lxcapi_destroy_with_snapshots;
c->rename = lxcapi_rename;
c->save_config = lxcapi_save_config;
c->get_keys = lxcapi_get_keys;
c->create = lxcapi_create;
c->createl = lxcapi_createl;
c->shutdown = lxcapi_shutdown;
c->reboot = lxcapi_reboot;
c->clear_config = lxcapi_clear_config;
c->clear_config_item = lxcapi_clear_config_item;
c->get_config_item = lxcapi_get_config_item;
c->get_running_config_item = lxcapi_get_running_config_item;
c->get_cgroup_item = lxcapi_get_cgroup_item;
c->set_cgroup_item = lxcapi_set_cgroup_item;
c->get_config_path = lxcapi_get_config_path;
c->set_config_path = lxcapi_set_config_path;
c->clone = lxcapi_clone;
c->get_interfaces = lxcapi_get_interfaces;
c->get_ips = lxcapi_get_ips;
c->attach = lxcapi_attach;
c->attach_run_wait = lxcapi_attach_run_wait;
c->attach_run_waitl = lxcapi_attach_run_waitl;
c->snapshot = lxcapi_snapshot;
c->snapshot_list = lxcapi_snapshot_list;
c->snapshot_restore = lxcapi_snapshot_restore;
c->snapshot_destroy = lxcapi_snapshot_destroy;
c->snapshot_destroy_all = lxcapi_snapshot_destroy_all;
c->may_control = lxcapi_may_control;
c->add_device_node = lxcapi_add_device_node;
c->remove_device_node = lxcapi_remove_device_node;
c->checkpoint = lxcapi_checkpoint;
c->restore = lxcapi_restore;
/* we'll allow the caller to update these later */
if (lxc_log_init(NULL, "none", NULL, "lxc_container", 0, c->config_path)) {
fprintf(stderr, "failed to open log\n");
goto err;
}
return c;
err:
lxc_container_free(c);
return NULL;
}
int lxc_get_wait_states(const char **states)
{
int i;
if (states)
for (i=0; i<MAX_STATE; i++)
states[i] = lxc_state2str(i);
return MAX_STATE;
}
/*
* These next two could probably be done smarter with reusing a common function
* with different iterators and tests...
*/
int list_defined_containers(const char *lxcpath, char ***names, struct lxc_container ***cret)
{
DIR *dir;
int i, cfound = 0, nfound = 0;
struct dirent dirent, *direntp;
struct lxc_container *c;
if (!lxcpath)
lxcpath = lxc_global_config_value("lxc.lxcpath");
dir = opendir(lxcpath);
if (!dir) {
SYSERROR("opendir on lxcpath");
return -1;
}
if (cret)
*cret = NULL;
if (names)
*names = NULL;
while (!readdir_r(dir, &dirent, &direntp)) {
if (!direntp)
break;
if (!strcmp(direntp->d_name, "."))
continue;
if (!strcmp(direntp->d_name, ".."))
continue;
if (!config_file_exists(lxcpath, direntp->d_name))
continue;
if (names) {
if (!add_to_array(names, direntp->d_name, cfound))
goto free_bad;
}
cfound++;
if (!cret) {
nfound++;
continue;
}
c = lxc_container_new(direntp->d_name, lxcpath);
if (!c) {
INFO("Container %s:%s has a config but could not be loaded",
lxcpath, direntp->d_name);
if (names)
if(!remove_from_array(names, direntp->d_name, cfound--))
goto free_bad;
continue;
}
if (!lxcapi_is_defined(c)) {
INFO("Container %s:%s has a config but is not defined",
lxcpath, direntp->d_name);
if (names)
if(!remove_from_array(names, direntp->d_name, cfound--))
goto free_bad;
lxc_container_put(c);
continue;
}
if (!add_to_clist(cret, c, nfound, true)) {
lxc_container_put(c);
goto free_bad;
}
nfound++;
}
closedir(dir);
return nfound;
free_bad:
if (names && *names) {
for (i=0; i<cfound; i++)
free((*names)[i]);
free(*names);
}
if (cret && *cret) {
for (i=0; i<nfound; i++)
lxc_container_put((*cret)[i]);
free(*cret);
}
closedir(dir);
return -1;
}
int list_active_containers(const char *lxcpath, char ***nret,
struct lxc_container ***cret)
{
int i, ret = -1, cret_cnt = 0, ct_name_cnt = 0;
int lxcpath_len;
char *line = NULL;
char **ct_name = NULL;
size_t len = 0;
struct lxc_container *c;
if (!lxcpath)
lxcpath = lxc_global_config_value("lxc.lxcpath");
lxcpath_len = strlen(lxcpath);
if (cret)
*cret = NULL;
if (nret)
*nret = NULL;
FILE *f = fopen("/proc/net/unix", "r");
if (!f)
return -1;
while (getline(&line, &len, f) != -1) {
char *p = strrchr(line, ' '), *p2;
if (!p)
continue;
p++;
if (*p != 0x40)
continue;
p++;
if (strncmp(p, lxcpath, lxcpath_len) != 0)
continue;
p += lxcpath_len;
while (*p == '/')
p++;
// Now p is the start of lxc_name
p2 = index(p, '/');
if (!p2 || strncmp(p2, "/command", 8) != 0)
continue;
*p2 = '\0';
if (array_contains(&ct_name, p, ct_name_cnt))
continue;
if (!add_to_array(&ct_name, p, ct_name_cnt))
goto free_cret_list;
ct_name_cnt++;
if (!cret)
continue;
c = lxc_container_new(p, lxcpath);
if (!c) {
INFO("Container %s:%s is running but could not be loaded",
lxcpath, p);
remove_from_array(&ct_name, p, ct_name_cnt--);
continue;
}
/*
* If this is an anonymous container, then is_defined *can*
* return false. So we don't do that check. Count on the
* fact that the command socket exists.
*/
if (!add_to_clist(cret, c, cret_cnt, true)) {
lxc_container_put(c);
goto free_cret_list;
}
cret_cnt++;
}
assert(!nret || !cret || cret_cnt == ct_name_cnt);
ret = ct_name_cnt;
if (nret)
*nret = ct_name;
else
goto free_ct_name;
goto out;
free_cret_list:
if (cret && *cret) {
for (i = 0; i < cret_cnt; i++)
lxc_container_put((*cret)[i]);
free(*cret);
}
free_ct_name:
if (ct_name) {
for (i = 0; i < ct_name_cnt; i++)
free(ct_name[i]);
free(ct_name);
}
out:
if (line)
free(line);
fclose(f);
return ret;
}
int list_all_containers(const char *lxcpath, char ***nret,
struct lxc_container ***cret)
{
int i, ret, active_cnt, ct_cnt, ct_list_cnt;
char **active_name;
char **ct_name;
struct lxc_container **ct_list = NULL;
ct_cnt = list_defined_containers(lxcpath, &ct_name, NULL);
if (ct_cnt < 0)
return ct_cnt;
active_cnt = list_active_containers(lxcpath, &active_name, NULL);
if (active_cnt < 0) {
ret = active_cnt;
goto free_ct_name;
}
for (i = 0; i < active_cnt; i++) {
if (!array_contains(&ct_name, active_name[i], ct_cnt)) {
if (!add_to_array(&ct_name, active_name[i], ct_cnt)) {
ret = -1;
goto free_active_name;
}
ct_cnt++;
}
free(active_name[i]);
active_name[i] = NULL;
}
free(active_name);
active_name = NULL;
active_cnt = 0;
for (i = 0, ct_list_cnt = 0; i < ct_cnt && cret; i++) {
struct lxc_container *c;
c = lxc_container_new(ct_name[i], lxcpath);
if (!c) {
WARN("Container %s:%s could not be loaded", lxcpath, ct_name[i]);
remove_from_array(&ct_name, ct_name[i], ct_cnt--);
continue;
}
if (!add_to_clist(&ct_list, c, ct_list_cnt, false)) {
lxc_container_put(c);
ret = -1;
goto free_ct_list;
}
ct_list_cnt++;
}
if (cret)
*cret = ct_list;
if (nret)
*nret = ct_name;
else {
ret = ct_cnt;
goto free_ct_name;
}
return ct_cnt;
free_ct_list:
for (i = 0; i < ct_list_cnt; i++) {
lxc_container_put(ct_list[i]);
}
if (ct_list)
free(ct_list);
free_active_name:
for (i = 0; i < active_cnt; i++) {
if (active_name[i])
free(active_name[i]);
}
if (active_name)
free(active_name);
free_ct_name:
for (i = 0; i < ct_cnt; i++) {
free(ct_name[i]);
}
free(ct_name);
return ret;
}