lxc: minimal C/R plugin

From: Cedric Le Goater <clg@fr.ibm.com>

Plugin for columbia CR.

Signed-off-by: Cedric Le Goater <clg@fr.ibm.com>
This commit is contained in:
dlezcano 2008-11-25 13:02:29 +00:00
parent 939229eb16
commit f66af38b49
7 changed files with 143 additions and 55 deletions

View File

@ -24,6 +24,7 @@ liblxc_la_SOURCES = \
version.c \
error.h error.c \
cgroup.c cgroup.h \
cr_plugin_columbia.c \
lxc.h \
lxc_utils.h \
lxc_lock.c lxc_lock.h \

View File

@ -37,33 +37,13 @@
#include <net/if.h>
#include "error.h"
#include "lxc_plugin.h"
#include <lxc.h>
#define MAXPIDLEN 20
#if __i386__
# define __NR_checkpoint 333
static inline long sys_checkpoint(pid_t pid, int fd, unsigned long flags)
{
return syscall(__NR_checkpoint, pid, fd, flags);
}
#elif __x86_64__
# define __NR_checkpoint 295
static inline long sys_checkpoint(pid_t pid, int fd, unsigned long flags)
{
return syscall(__NR_checkpoint, pid, fd, flags);
}
#else
# warning "Architecture not supported for checkpoint"
static inline long sys_checkpoint(pid_t pid, int fd, unsigned long flags)
{
errno = ENOSYS;
return -1;
}
#endif
int lxc_checkpoint(const char *name, int cfd, unsigned long flags)
int lxc_checkpoint(const char *name, const char *statefile,
unsigned long flags)
{
char init[MAXPATHLEN];
char val[MAXPIDLEN];
@ -93,7 +73,7 @@ int lxc_checkpoint(const char *name, int cfd, unsigned long flags)
pid = atoi(val);
if (sys_checkpoint(pid, cfd, flags) < 0) {
if (lxc_plugin_checkpoint(pid, statefile, flags) < 0) {
lxc_log_syserror("failed to checkpoint %zd", pid);
goto out_close;
}

View File

@ -0,0 +1,118 @@
/*
* lxc: linux Container library
*
* (C) Copyright IBM Corp. 2007, 2008
*
* Authors:
* Daniel Lezcano <dlezcano at fr.ibm.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#define _GNU_SOURCE
#include <stdio.h>
#undef _GNU_SOURCE
#include <fcntl.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/file.h>
#include "error.h"
#include <lxc.h>
#if __i386__
# define __NR_checkpoint 333
# define __NR_restart 334
static inline long sys_checkpoint(pid_t pid, int fd, unsigned long flags)
{
return syscall(__NR_checkpoint, pid, fd, flags);
}
static inline long sys_restart(pid_t pid, int fd, unsigned long flags)
{
return syscall(__NR_restart, pid, fd, flags);
}
#elif __x86_64__
# define __NR_checkpoint 295
# define __NR_restart 296
static inline long sys_checkpoint(pid_t pid, int fd, unsigned long flags)
{
return syscall(__NR_checkpoint, pid, fd, flags);
}
static inline long sys_restart(pid_t pid, int fd, unsigned long flags)
{
return syscall(__NR_restart, pid, fd, flags);
}
#else
# warning "Architecture not supported for checkpoint"
static inline long sys_checkpoint(pid_t pid, int fd, unsigned long flags)
{
errno = ENOSYS;
return -1;
}
static inline long sys_restart(pid_t pid, int fd, unsigned long flags)
{
errno = ENOSYS;
return -1;
}
# warning "Architecture not supported for restart syscall"
#endif
int lxc_plugin_checkpoint(pid_t pid, const char *statefile,
unsigned long flags)
{
int fd, ret;
fd = open(statefile, O_RDWR);
if (fd < 0) {
lxc_log_syserror("failed to open init file for %s", statefile);
return -1;
}
ret = sys_checkpoint(pid, fd, flags);
if (ret < 0) {
lxc_log_syserror("failed to checkpoint %zd", pid);
goto out_close;
}
ret = 0;
out_close:
close(fd);
return ret;
}
int lxc_plugin_restart(pid_t pid, const char *statefile,
unsigned long flags)
{
int fd;
fd = open(statefile, O_RDONLY);
if (fd < 0) {
lxc_log_syserror("failed to open init file for %s", statefile);
return -1;
}
sys_restart(pid, fd, flags);
lxc_log_syserror("failed to restart %zd", pid);
close(fd);
return -1;
}

View File

@ -180,7 +180,8 @@ extern const char *const lxc_strerror(int error);
* @flags : checkpoint flags
* Returns 0 on success, < 0 otherwise
*/
extern int lxc_checkpoint(const char *name, int fd, unsigned long flags);
extern int lxc_checkpoint(const char *name, const char *statefile,
unsigned long flags);
/*
* Restart a container previously frozen
@ -189,7 +190,8 @@ extern int lxc_checkpoint(const char *name, int fd, unsigned long flags);
* @flags : restart flags
* Returns 0 on success, < 0 otherwise
*/
extern int lxc_restart(const char *name, int fd, unsigned long flags);
extern int lxc_restart(const char *name, const char *statefile,
unsigned long flags);
/*
* Returns the version number of the library

View File

@ -29,7 +29,7 @@
void usage(char *cmd)
{
fprintf(stderr, "%s <command>\n", basename(cmd));
fprintf(stderr, "%s <statefile>\n", basename(cmd));
fprintf(stderr, "\t -n <name> : name of the container\n");
_exit(1);
}
@ -58,12 +58,15 @@ int main(int argc, char *argv[])
if (!name)
usage(argv[0]);
if (!argv[1])
usage(argv[0]);
if (lxc_freeze(name)) {
fprintf(stderr, "failed to freeze '%s'\n", name);
return -1;
}
if (lxc_checkpoint(name, STDOUT_FILENO, 0)) {
if (lxc_checkpoint(name, argv[1], 0)) {
fprintf(stderr, "failed to checkpoint %s\n", name);
goto out;
}

View File

@ -29,7 +29,7 @@
void usage(char *cmd)
{
fprintf(stderr, "%s <command>\n", basename(cmd));
fprintf(stderr, "%s <statefile>\n", basename(cmd));
fprintf(stderr, "\t -n <name> : name of the container\n");
_exit(1);
}
@ -53,7 +53,10 @@ int main(int argc, char *argv[])
if (!name)
usage(argv[0]);
if (lxc_restart(name, STDIN_FILENO, 0)) {
if (!argv[1])
usage(argv[0]);
if (lxc_restart(name, argv[1], 0)) {
fprintf(stderr, "failed to restart %s\n", name);
return 1;
}

View File

@ -38,34 +38,14 @@
#include <sys/mount.h>
#include "error.h"
#include "lxc_plugin.h"
#include <lxc/lxc.h>
LXC_TTY_HANDLER(SIGINT);
LXC_TTY_HANDLER(SIGQUIT);
#if __i386__
# define __NR_restart 334
static inline long sys_restart(pid_t pid, int fd, unsigned long flags)
{
return syscall(__NR_restart, pid, fd, flags);
}
#elif __x86_64__
# define __NR_restart 296
static inline long sys_restart(pid_t pid, int fd, unsigned long flags)
{
return syscall(__NR_restart, pid, fd, flags);
}
#else
static inline long sys_restart(pid_t pid, int fd, unsigned long flags)
{
errno = ENOSYS;
return -1;
}
# warning "Architecture not supported for restart syscall"
#endif
int lxc_restart(const char *name, int cfd, unsigned long flags)
int lxc_restart(const char *name, const char *statefile,
unsigned long flags)
{
char *init = NULL, *val = NULL;
char ttyname[MAXPATHLEN];
@ -116,6 +96,7 @@ int lxc_restart(const char *name, int cfd, unsigned long flags)
if (!pid) {
char dummytty = '\0';
close(sv[1]);
/* Be sure we don't inherit this after the exec */
@ -134,7 +115,7 @@ int lxc_restart(const char *name, int cfd, unsigned long flags)
}
/* Setup the container, ip, names, utsname, ... */
if (lxc_setup(name)) {
if (lxc_setup(name, &dummytty)) {
lxc_log_error("failed to setup the container");
if (write(sv[0], &sync, sizeof(sync)) < 0)
lxc_log_syserror("failed to write the socket");
@ -146,7 +127,7 @@ int lxc_restart(const char *name, int cfd, unsigned long flags)
return -1;
}
sys_restart(getpid(), cfd, flags);
lxc_plugin_restart(getpid(), statefile, flags);
lxc_log_syserror("failed to restart");
/* If the exec fails, tell that to our father */