]> git.proxmox.com Git - mirror_lxc.git/blobdiff - src/lxc/lxc_execute.c
ovl_rsync: make sure to umount
[mirror_lxc.git] / src / lxc / lxc_execute.c
index 23631d8496733e4b4160bf753801ffde03b841de..50d481f0e4e14a1365537b8ae76db5ffcd244c4f 100644 (file)
@@ -4,7 +4,7 @@
  * (C) Copyright IBM Corp. 2007, 2008
  *
  * Authors:
- * Daniel Lezcano <dlezcano at fr.ibm.com>
+ * Daniel Lezcano <daniel.lezcano at free.fr>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -18,7 +18,7 @@
  *
  * 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
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 #define _GNU_SOURCE
 #include <stdio.h>
@@ -41,7 +41,7 @@
 #include "start.h"
 #include "utils.h"
 
-lxc_log_define(lxc_execute_ui, lxc_execute);
+lxc_log_define(lxc_execute_ui, lxc);
 
 static struct lxc_list defines;
 
@@ -59,7 +59,9 @@ static int my_parser(struct lxc_arguments* args, int c, char* arg)
 {
        switch (c) {
        case 'f': args->rcfile = arg; break;
-       case 's': return lxc_config_define_add(&defines, arg);
+       case 's': return lxc_config_define_add(&defines, arg); break;
+       case 'u': args->uid = atoi(arg); break;
+       case 'g': args->gid = atoi(arg);
        }
        return 0;
 }
@@ -67,6 +69,8 @@ static int my_parser(struct lxc_arguments* args, int c, char* arg)
 static const struct option my_longopts[] = {
        {"rcfile", required_argument, 0, 'f'},
        {"define", required_argument, 0, 's'},
+       {"uid", required_argument, 0, 'u'},
+       {"gid", required_argument, 0, 'g'},
        LXC_COMMON_OPTIONS
 };
 
@@ -79,9 +83,11 @@ lxc-execute creates a container with the identifier NAME\n\
 and execs COMMAND into this container.\n\
 \n\
 Options :\n\
-  -n, --name=NAME      NAME for name of the container\n\
+  -n, --name=NAME      NAME of the container\n\
   -f, --rcfile=FILE    Load configuration file FILE\n\
-  -s, --define KEY=VAL Assign VAL to configuration variable KEY\n",
+  -s, --define KEY=VAL Assign VAL to configuration variable KEY\n\
+  -u, --uid=UID Execute COMMAND with UID inside the container\n\
+  -g, --gid=GID Execute COMMAND with GID inside the container\n",
        .options  = my_longopts,
        .parser   = my_parser,
        .checker  = my_checker,
@@ -91,18 +97,20 @@ int main(int argc, char *argv[])
 {
        char *rcfile;
        struct lxc_conf *conf;
+       int ret;
 
        lxc_list_init(&defines);
 
        if (lxc_caps_init())
-               return -1;
+               return 1;
 
        if (lxc_arguments_parse(&my_args, argc, argv))
-               return -1;
+               return 1;
 
        if (lxc_log_init(my_args.name, my_args.log_file, my_args.log_priority,
-                        my_args.progname, my_args.quiet))
-               return -1;
+                        my_args.progname, my_args.quiet, my_args.lxcpath[0]))
+               return 1;
+       lxc_log_options_no_override();
 
        /* rcfile is specified in the cli option */
        if (my_args.rcfile)
@@ -110,10 +118,10 @@ int main(int argc, char *argv[])
        else {
                int rc;
 
-               rc = asprintf(&rcfile, "%s/%s/config", my_args.lxcpath, my_args.name);
+               rc = asprintf(&rcfile, "%s/%s/config", my_args.lxcpath[0], my_args.name);
                if (rc == -1) {
                        SYSERROR("failed to allocate memory");
-                       return -1;
+                       return 1;
                }
 
                /* container configuration does not exist */
@@ -126,16 +134,28 @@ int main(int argc, char *argv[])
        conf = lxc_conf_init();
        if (!conf) {
                ERROR("failed to initialize configuration");
-               return -1;
+               return 1;
        }
 
-       if (rcfile && lxc_config_read(rcfile, conf)) {
+       if (rcfile && lxc_config_read(rcfile, conf, NULL)) {
                ERROR("failed to read configuration file");
-               return -1;
+               return 1;
        }
 
        if (lxc_config_define_load(&defines, conf))
-               return -1;
+               return 1;
+
+       if (my_args.uid)
+               conf->init_uid = my_args.uid;
+
+       if (my_args.gid)
+               conf->init_gid = my_args.gid;
+
+       ret = lxc_execute(my_args.name, my_args.argv, my_args.quiet, conf, my_args.lxcpath[0], false);
+
+       lxc_conf_free(conf);
 
-       return lxc_execute(my_args.name, my_args.argv, my_args.quiet, conf, my_args.lxcpath);
+       if (ret < 0)
+               return 1;
+       return ret;
 }