From: dlezcano Date: Tue, 25 Nov 2008 13:02:29 +0000 (+0000) Subject: lxc: minimal C/R plugin X-Git-Tag: lxc_0_5_0~17 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f66af38b495095f90753bf3ae90e69096dbfe918;p=thirdparty%2Flxc.git lxc: minimal C/R plugin From: Cedric Le Goater Plugin for columbia CR. Signed-off-by: Cedric Le Goater --- diff --git a/src/lxc/Makefile.am b/src/lxc/Makefile.am index 2ab5b94c5..464361cd0 100644 --- a/src/lxc/Makefile.am +++ b/src/lxc/Makefile.am @@ -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 \ diff --git a/src/lxc/checkpoint.c b/src/lxc/checkpoint.c index cec69b5ad..995de100f 100644 --- a/src/lxc/checkpoint.c +++ b/src/lxc/checkpoint.c @@ -37,33 +37,13 @@ #include #include "error.h" +#include "lxc_plugin.h" #include #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; } diff --git a/src/lxc/cr_plugin_columbia.c b/src/lxc/cr_plugin_columbia.c new file mode 100644 index 000000000..8345e7b3e --- /dev/null +++ b/src/lxc/cr_plugin_columbia.c @@ -0,0 +1,118 @@ +/* + * lxc: linux Container library + * + * (C) Copyright IBM Corp. 2007, 2008 + * + * Authors: + * Daniel Lezcano + * + * 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 +#undef _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "error.h" +#include + +#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; +} diff --git a/src/lxc/lxc.h b/src/lxc/lxc.h index 012ab3cc0..de8c3533c 100644 --- a/src/lxc/lxc.h +++ b/src/lxc/lxc.h @@ -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 diff --git a/src/lxc/lxc_checkpoint.c b/src/lxc/lxc_checkpoint.c index 412c138f8..480ffba79 100644 --- a/src/lxc/lxc_checkpoint.c +++ b/src/lxc/lxc_checkpoint.c @@ -29,7 +29,7 @@ void usage(char *cmd) { - fprintf(stderr, "%s \n", basename(cmd)); + fprintf(stderr, "%s \n", basename(cmd)); fprintf(stderr, "\t -n : 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; } diff --git a/src/lxc/lxc_restart.c b/src/lxc/lxc_restart.c index a2e1cc23b..b586f0c0a 100644 --- a/src/lxc/lxc_restart.c +++ b/src/lxc/lxc_restart.c @@ -29,7 +29,7 @@ void usage(char *cmd) { - fprintf(stderr, "%s \n", basename(cmd)); + fprintf(stderr, "%s \n", basename(cmd)); fprintf(stderr, "\t -n : 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; } diff --git a/src/lxc/restart.c b/src/lxc/restart.c index d1e9bf283..28a58d89b 100644 --- a/src/lxc/restart.c +++ b/src/lxc/restart.c @@ -38,34 +38,14 @@ #include #include "error.h" +#include "lxc_plugin.h" #include 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 */