From c75faed9ec0fae2d576a07e2751ce8aa80133528 Mon Sep 17 00:00:00 2001 From: Andre Nathan Date: Fri, 6 Dec 2013 08:51:09 -0200 Subject: [PATCH] Adding LXC::arch_to_personality and LXC::run_shell --- ext/lxc.c | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/ext/lxc.c b/ext/lxc.c index b1f3838..55cbe13 100644 --- a/ext/lxc.c +++ b/ext/lxc.c @@ -8,6 +8,7 @@ #define SYMBOL(s) ID2SYM(rb_intern(s)) extern int lxc_wait_for_pid_status(pid_t pid); +extern long lxc_config_parse_arch(const char *arch); static VALUE Error; @@ -1136,6 +1137,25 @@ container_wait(int argc, VALUE *argv, VALUE self) return self; } +static VALUE +lxc_arch_to_personality(VALUE self, VALUE rb_arch) +{ + int ret; + char *arch; + + arch = StringValuePtr(rb_arch); + ret = lxc_config_parse_arch(arch); + + switch (ret) { + case PER_LINUX32: + return SYMBOL("linux32"); + case PER_LINUX: + return SYMBOL("linux"); + default: + rb_raise(Error, "unknown personality"); + } +} + static VALUE lxc_run_command(VALUE self, VALUE rb_command) { @@ -1149,7 +1169,19 @@ lxc_run_command(VALUE self, VALUE rb_command) ret = lxc_attach_run_command(&cmd); if (ret == -1) - rb_raise(Error, "unable to run command"); + rb_raise(Error, "unable to run command on attached container"); + /* NOTREACHED */ + return Qnil; +} + +static VALUE +lxc_run_shell(VALUE self) +{ + int ret; + + ret = lxc_attach_run_shell(NULL); + if (ret == -1) + rb_raise(Error, "unable to run shell on attached container"); /* NOTREACHED */ return Qnil; } @@ -1216,11 +1248,10 @@ Init_lxc(void) { VALUE LXC = rb_define_module("LXC"); - //rb_define_singleton_method(LXC, "arch_to_personality", - // lxc_arch_to_personality, 1); + rb_define_singleton_method(LXC, "arch_to_personality", + lxc_arch_to_personality, 1); rb_define_singleton_method(LXC, "run_command", lxc_run_command, 1); - //rb_define_singleton_method(LXC, "attach_run_shell", - // lxc_attach_run_shell, 0); + rb_define_singleton_method(LXC, "run_shell", lxc_run_shell, 0); rb_define_singleton_method(LXC, "default_config_path", lxc_default_config_path, 0); rb_define_singleton_method(LXC, "version", lxc_version, 0);