]> git.proxmox.com Git - mirror_lxc.git/commitdiff
arguments: move to tools/ subdirectory
authorRicardoSanchezA <ricardo.sanchez@utexas.edu>
Tue, 5 Dec 2017 04:03:52 +0000 (22:03 -0600)
committerChristian Brauner <christian.brauner@ubuntu.com>
Tue, 5 Dec 2017 11:15:09 +0000 (12:15 +0100)
The arguments.{c,h} file should not be a part of liblxc itself. It should only
be used for the lxc tools.

Signed-off-by: RicardoSanchezA <ricardo.sanchez@utexas.edu>
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/Makefile.am
src/lxc/arguments.c [deleted file]
src/lxc/arguments.h [deleted file]
src/lxc/tools/arguments.c [new file with mode: 0644]
src/lxc/tools/arguments.h [new file with mode: 0644]

index 8f0c11ecae56c68664a00e26370a85236465342e..5c946c12e7bfa873eeebb916df66f7845dc1e01b 100644 (file)
@@ -4,7 +4,7 @@ pkginclude_HEADERS = \
        version.h
 
 noinst_HEADERS = \
-       arguments.h \
+       tools/arguments.h \
        attach.h \
        storage/storage.h \
        storage/aufs.h \
@@ -79,7 +79,6 @@ endif
 
 lib_LTLIBRARIES = liblxc.la
 liblxc_la_SOURCES = \
-       arguments.c arguments.h \
        storage/storage.c storage/storage.h \
        storage/aufs.c storage/aufs.h \
        storage/btrfs.c storage/btrfs.h \
@@ -267,35 +266,35 @@ AM_LDFLAGS += -Wl,-rpath -Wl,$(libdir)
 endif
 LDADD=liblxc.la @CAP_LIBS@ @APPARMOR_LIBS@ @SELINUX_LIBS@ @SECCOMP_LIBS@
 
-lxc_attach_SOURCES = tools/lxc_attach.c
-lxc_autostart_SOURCES = tools/lxc_autostart.c
-lxc_cgroup_SOURCES = tools/lxc_cgroup.c
-lxc_config_SOURCES = tools/lxc_config.c
-lxc_console_SOURCES = tools/lxc_console.c
-lxc_destroy_SOURCES = tools/lxc_destroy.c
-lxc_device_SOURCES = tools/lxc_device.c
-lxc_execute_SOURCES = tools/lxc_execute.c
-lxc_freeze_SOURCES = tools/lxc_freeze.c
-lxc_info_SOURCES = tools/lxc_info.c
-init_lxc_SOURCES = tools/lxc_init.c
-lxc_monitor_SOURCES = tools/lxc_monitor.c
-lxc_ls_SOURCES = tools/lxc_ls.c
-lxc_copy_SOURCES = tools/lxc_copy.c
-lxc_start_SOURCES = tools/lxc_start.c
-lxc_stop_SOURCES = tools/lxc_stop.c
-lxc_top_SOURCES = tools/lxc_top.c
-lxc_unfreeze_SOURCES = tools/lxc_unfreeze.c
-lxc_unshare_SOURCES = tools/lxc_unshare.c
-lxc_wait_SOURCES = tools/lxc_wait.c
-lxc_create_SOURCES = tools/lxc_create.c
-lxc_snapshot_SOURCES = tools/lxc_snapshot.c
-lxc_usernsexec_SOURCES = tools/lxc_usernsexec.c
-lxc_checkpoint_SOURCES = tools/lxc_checkpoint.c
-lxc_user_nic_SOURCES = lxc_user_nic.c network.c network.h
-lxc_monitord_SOURCES = lxc_monitord.c
+lxc_attach_SOURCES = tools/lxc_attach.c tools/arguments.c
+lxc_autostart_SOURCES = tools/lxc_autostart.c tools/arguments.c
+lxc_cgroup_SOURCES = tools/lxc_cgroup.c tools/arguments.c
+lxc_config_SOURCES = tools/lxc_config.c tools/arguments.c
+lxc_console_SOURCES = tools/lxc_console.c tools/arguments.c
+lxc_destroy_SOURCES = tools/lxc_destroy.c tools/arguments.c
+lxc_device_SOURCES = tools/lxc_device.c tools/arguments.c
+lxc_execute_SOURCES = tools/lxc_execute.c tools/arguments.c
+lxc_freeze_SOURCES = tools/lxc_freeze.c tools/arguments.c
+lxc_info_SOURCES = tools/lxc_info.c tools/arguments.c
+init_lxc_SOURCES = tools/lxc_init.c tools/arguments.c
+lxc_monitor_SOURCES = tools/lxc_monitor.c tools/arguments.c
+lxc_ls_SOURCES = tools/lxc_ls.c tools/arguments.c
+lxc_copy_SOURCES = tools/lxc_copy.c tools/arguments.c
+lxc_start_SOURCES = tools/lxc_start.c tools/arguments.c
+lxc_stop_SOURCES = tools/lxc_stop.c tools/arguments.c
+lxc_top_SOURCES = tools/lxc_top.c tools/arguments.c
+lxc_unfreeze_SOURCES = tools/lxc_unfreeze.c tools/arguments.c
+lxc_unshare_SOURCES = tools/lxc_unshare.c tools/arguments.c
+lxc_wait_SOURCES = tools/lxc_wait.c tools/arguments.c
+lxc_create_SOURCES = tools/lxc_create.c tools/arguments.c
+lxc_snapshot_SOURCES = tools/lxc_snapshot.c tools/arguments.c
+lxc_usernsexec_SOURCES = tools/lxc_usernsexec.c tools/arguments.c
+lxc_checkpoint_SOURCES = tools/lxc_checkpoint.c tools/arguments.c
+lxc_user_nic_SOURCES = lxc_user_nic.c network.c network.h tools/arguments.c
+lxc_monitord_SOURCES = lxc_monitord.c tools/arguments.c
 
 if ENABLE_DEPRECATED
-lxc_clone_SOURCES = tools/lxc_clone.c
+lxc_clone_SOURCES = tools/lxc_clone.c tools/arguments.c
 endif
 
 if !HAVE_GETSUBOPT
@@ -306,7 +305,7 @@ if HAVE_STATIC_LIBCAP
 sbin_PROGRAMS += init.lxc.static
 
 init_lxc_static_SOURCES = tools/lxc_init.c error.c log.c initutils.c caps.c \
-                         arguments.c
+                         tools/arguments.c
 
 if !HAVE_GETLINE
 if HAVE_FGETLN
diff --git a/src/lxc/arguments.c b/src/lxc/arguments.c
deleted file mode 100644 (file)
index f16b195..0000000
+++ /dev/null
@@ -1,291 +0,0 @@
-/*
- * lxc: linux Container library
- *
- * (C) Copyright IBM Corp. 2007, 2008
- *
- * Authors:
- * Daniel Lezcano <daniel.lezcano at free.fr>
- * Michel Normand <normand 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include <ctype.h>
-#include <errno.h>
-#include <limits.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-
-#include "arguments.h"
-#include "utils.h"
-#include "version.h"
-
-static int build_shortopts(const struct option *a_options, char *a_shortopts,
-                          size_t a_size)
-{
-       size_t i = 0;
-       const struct option *opt;
-
-       if (!a_options || !a_shortopts || !a_size)
-               return -1;
-
-       for (opt = a_options; opt->name; opt++) {
-
-               if (!isascii(opt->val))
-                       continue;
-
-               if (i < a_size)
-                       a_shortopts[i++] = opt->val;
-               else
-                       goto is2big;
-
-               if (opt->has_arg == no_argument)
-                       continue;
-
-               if (i < a_size)
-                       a_shortopts[i++] = ':';
-               else
-                       goto is2big;
-
-               if (opt->has_arg == required_argument)
-                       continue;
-
-               if (i < a_size)
-                       a_shortopts[i++] = ':';
-               else
-                       goto is2big;
-       }
-
-       if (i < a_size)
-               a_shortopts[i] = '\0';
-       else
-               goto is2big;
-
-       return 0;
-
-is2big:
-       errno = E2BIG;
-       return -1;
-}
-
-static void print_usage(const struct option longopts[],
-                       const struct lxc_arguments *a_args)
-
-{
-       int i;
-       const struct option *opt;
-
-       fprintf(stderr, "Usage: %s ", a_args->progname);
-
-       for (opt = longopts, i = 1; opt->name; opt++, i++) {
-               int j;
-               char *uppername;
-
-               uppername = strdup(opt->name);
-               if (!uppername)
-                       exit(-ENOMEM);
-
-               for (j = 0; uppername[j]; j++)
-                       uppername[j] = toupper(uppername[j]);
-
-               fprintf(stderr, "[");
-
-               if (isprint(opt->val))
-                       fprintf(stderr, "-%c|", opt->val);
-
-               fprintf(stderr, "--%s", opt->name);
-
-               if (opt->has_arg == required_argument)
-                       fprintf(stderr, "=%s", uppername);
-
-               if (opt->has_arg == optional_argument)
-                       fprintf(stderr, "[=%s]", uppername);
-
-               fprintf(stderr, "] ");
-
-               if (!(i % 4))
-                       fprintf(stderr, "\n\t");
-
-               free(uppername);
-       }
-
-       fprintf(stderr, "\n");
-       exit(0);
-}
-
-static void print_version()
-{
-       printf("%s%s\n", LXC_VERSION, LXC_DEVEL ? "-devel" : "");
-       exit(0);
-}
-
-static void print_help(const struct lxc_arguments *args, int code)
-{
-       fprintf(stderr, "\
-Usage: %s %s\
-\n\
-Common options :\n\
-  -o, --logfile=FILE               Output log to FILE instead of stderr\n\
-  -l, --logpriority=LEVEL          Set log priority to LEVEL\n\
-  -q, --quiet                      Don't produce any output\n\
-  -P, --lxcpath=PATH               Use specified container path\n\
-  -?, --help                       Give this help list\n\
-      --usage                      Give a short usage message\n\
-      --version                    Print the version number\n\
-\n\
-Mandatory or optional arguments to long options are also mandatory or optional\n\
-for any corresponding short options.\n\
-\n\
-See the %s man page for further information.\n\n",
-       args->progname, args->help, args->progname);
-
-       if (args->helpfn)
-               args->helpfn(args);
-       exit(code);
-}
-
-static int lxc_arguments_lxcpath_add(struct lxc_arguments *args,
-                                    const char *lxcpath)
-{
-       if (args->lxcpath_additional != -1 &&
-           args->lxcpath_cnt > args->lxcpath_additional) {
-               fprintf(stderr,
-                       "This command only accepts %d -P,--lxcpath arguments\n",
-                       args->lxcpath_additional + 1);
-               exit(EXIT_FAILURE);
-       }
-
-       args->lxcpath = realloc(
-           args->lxcpath, (args->lxcpath_cnt + 1) * sizeof(args->lxcpath[0]));
-       if (args->lxcpath == NULL) {
-               lxc_error(args, "no memory");
-               return -ENOMEM;
-       }
-       args->lxcpath[args->lxcpath_cnt++] = lxcpath;
-       return 0;
-}
-
-extern int lxc_arguments_parse(struct lxc_arguments *args, int argc,
-                              char *const argv[])
-{
-       int ret = 0;
-       char shortopts[256];
-
-       ret = build_shortopts(args->options, shortopts, sizeof(shortopts));
-       if (ret < 0) {
-               lxc_error(args, "build_shortopts() failed : %s",
-                         strerror(errno));
-               return ret;
-       }
-
-       while (true) {
-               int c;
-               int index = 0;
-
-               c = getopt_long(argc, argv, shortopts, args->options, &index);
-               if (c == -1)
-                       break;
-               switch (c) {
-               case 'n':
-                       args->name = optarg;
-                       break;
-               case 'o':
-                       args->log_file = optarg;
-                       break;
-               case 'l':
-                       args->log_priority = optarg;
-                       break;
-               case 'q':
-                       args->quiet = 1;
-                       break;
-               case OPT_RCFILE:
-                       args->rcfile = optarg;
-                       break;
-               case 'P':
-                       remove_trailing_slashes(optarg);
-                       ret = lxc_arguments_lxcpath_add(args, optarg);
-                       if (ret < 0)
-                               return ret;
-                       break;
-               case OPT_USAGE:
-                       print_usage(args->options, args);
-               case OPT_VERSION:
-                       print_version();
-               case '?':
-                       print_help(args, 1);
-               case 'h':
-                       print_help(args, 0);
-               default:
-                       if (args->parser) {
-                               ret = args->parser(args, c, optarg);
-                               if (ret)
-                                       goto error;
-                       }
-               }
-       }
-
-       /*
-        * Reclaim the remaining command arguments
-        */
-       args->argv = &argv[optind];
-       args->argc = argc - optind;
-
-       /* If no lxcpaths were given, use default */
-       if (!args->lxcpath_cnt) {
-               ret = lxc_arguments_lxcpath_add(
-                   args, lxc_global_config_value("lxc.lxcpath"));
-               if (ret < 0)
-                       return ret;
-       }
-
-       /* Check the command options */
-
-       if (!args->name && strcmp(args->progname, "lxc-autostart") != 0) {
-               lxc_error(args, "missing container name, use --name option");
-               return -1;
-       }
-
-       if (args->checker)
-               ret = args->checker(args);
-error:
-       if (ret)
-               lxc_error(args, "could not parse command line");
-       return ret;
-}
-
-int lxc_arguments_str_to_int(struct lxc_arguments *args, const char *str)
-{
-       long val;
-       char *endptr;
-
-       errno = 0;
-       val = strtol(str, &endptr, 10);
-       if (errno) {
-               lxc_error(args, "invalid statefd '%s' : %s", str,
-                         strerror(errno));
-               return -1;
-       }
-
-       if (*endptr) {
-               lxc_error(args, "invalid digit for statefd '%s'", str);
-               return -1;
-       }
-
-       return (int)val;
-}
diff --git a/src/lxc/arguments.h b/src/lxc/arguments.h
deleted file mode 100644 (file)
index b07caf4..0000000
+++ /dev/null
@@ -1,173 +0,0 @@
-/*
- * lxc: linux Container library
- *
- * (C) Copyright IBM Corp. 2007, 2008
- *
- * Authors:
- * Daniel Lezcano <daniel.lezcano at free.fr>
- * Michel Normand <normand 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#ifndef __LXC_ARGUMENTS_H
-#define __LXC_ARGUMENTS_H
-
-#include <getopt.h>
-#include <stdbool.h>
-#include <stdint.h>
-#include <sys/types.h>
-
-struct lxc_arguments;
-
-typedef int (*lxc_arguments_parser_t)(struct lxc_arguments *, int, char *);
-typedef int (*lxc_arguments_checker_t)(const struct lxc_arguments *);
-
-struct lxc_arguments {
-       const char *help;
-       void (*helpfn)(const struct lxc_arguments *);
-       const char *progname;
-       const struct option *options;
-       lxc_arguments_parser_t parser;
-       lxc_arguments_checker_t checker;
-
-       const char *name;
-       char *log_file;
-       char *log_priority;
-       int quiet;
-       int daemonize;
-       const char *rcfile;
-       const char *console;
-       const char *console_log;
-       const char *pidfile;
-       const char **lxcpath;
-       int lxcpath_cnt;
-       /* set to 0 to accept only 1 lxcpath, -1 for unlimited */
-       int lxcpath_additional;
-
-       /* for lxc-start */
-       const char *share_ns[32]; /* size must be greater than LXC_NS_MAX */
-
-       /* for lxc-console */
-       unsigned int ttynum;
-       char escape;
-
-       /* for lxc-wait */
-       char *states;
-       long timeout;
-
-       /* for lxc-autostart */
-       int shutdown;
-
-       /* for lxc-stop */
-       int hardstop;
-       int nokill;
-       int nolock;
-       int nowait;
-       int reboot;
-
-       /* for lxc-destroy */
-       int force;
-
-       /* close fds from parent? */
-       int close_all_fds;
-
-       /* lxc-create */
-       char *bdevtype, *configfile, *template;
-       char *fstype;
-       uint64_t fssize;
-       char *lvname, *vgname, *thinpool;
-       char *rbdname, *rbdpool;
-       char *zfsroot, *lowerdir, *dir;
-
-       /* lxc-execute */
-       uid_t uid;
-       gid_t gid;
-
-       /* auto-start */
-       int all;
-       int ignore_auto;
-       int list;
-       char *groups; /* also used by lxc-ls */
-
-       /* lxc-snapshot and lxc-copy */
-       enum task {
-               CLONE,
-               DESTROY,
-               LIST,
-               RESTORE,
-               SNAP,
-               RENAME,
-       } task;
-       int print_comments;
-       char *commentfile;
-       char *newname;
-       char *newpath;
-       char *snapname;
-       int keepdata;
-       int keepname;
-       int keepmac;
-
-       /* lxc-ls */
-       char *ls_fancy_format;
-       char *ls_filter;
-       unsigned int ls_nesting; /* maximum allowed nesting level */
-       bool ls_active;
-       bool ls_fancy;
-       bool ls_frozen;
-       bool ls_line;
-       bool ls_running;
-       bool ls_stopped;
-       bool ls_defined;
-
-       /* lxc-copy */
-       bool tmpfs;
-
-       /* remaining arguments */
-       char *const *argv;
-       int argc;
-
-       /* private arguments */
-       void *data;
-};
-
-#define LXC_COMMON_OPTIONS                                                     \
-           { "name",        required_argument, 0, 'n'         },              \
-            { "help",        no_argument,       0, 'h'         },              \
-           { "usage",       no_argument,       0, OPT_USAGE   },              \
-           { "version",     no_argument,       0, OPT_VERSION },              \
-           { "quiet",       no_argument,       0, 'q'         },              \
-           { "logfile",     required_argument, 0, 'o'         },              \
-           { "logpriority", required_argument, 0, 'l'         },              \
-           { "lxcpath",     required_argument, 0, 'P'         },              \
-           { "rcfile",      required_argument, 0, OPT_RCFILE  },              \
-           { 0,             0,                 0, 0           }
-
-/* option keys for long only options */
-#define OPT_USAGE 0x1000
-#define OPT_VERSION OPT_USAGE - 1
-#define OPT_RCFILE OPT_USAGE - 2
-
-extern int lxc_arguments_parse(struct lxc_arguments *args, int argc,
-                              char *const argv[]);
-
-extern int lxc_arguments_str_to_int(struct lxc_arguments *args,
-                                   const char *str);
-
-#define lxc_error(arg, fmt, args...)                                           \
-       if (!(arg)->quiet)                                                     \
-       fprintf(stderr, "%s: " fmt "\n", (arg)->progname, ##args)
-
-#endif /* __LXC_ARGUMENTS_H */
diff --git a/src/lxc/tools/arguments.c b/src/lxc/tools/arguments.c
new file mode 100644 (file)
index 0000000..f16b195
--- /dev/null
@@ -0,0 +1,291 @@
+/*
+ * lxc: linux Container library
+ *
+ * (C) Copyright IBM Corp. 2007, 2008
+ *
+ * Authors:
+ * Daniel Lezcano <daniel.lezcano at free.fr>
+ * Michel Normand <normand 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <ctype.h>
+#include <errno.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include "arguments.h"
+#include "utils.h"
+#include "version.h"
+
+static int build_shortopts(const struct option *a_options, char *a_shortopts,
+                          size_t a_size)
+{
+       size_t i = 0;
+       const struct option *opt;
+
+       if (!a_options || !a_shortopts || !a_size)
+               return -1;
+
+       for (opt = a_options; opt->name; opt++) {
+
+               if (!isascii(opt->val))
+                       continue;
+
+               if (i < a_size)
+                       a_shortopts[i++] = opt->val;
+               else
+                       goto is2big;
+
+               if (opt->has_arg == no_argument)
+                       continue;
+
+               if (i < a_size)
+                       a_shortopts[i++] = ':';
+               else
+                       goto is2big;
+
+               if (opt->has_arg == required_argument)
+                       continue;
+
+               if (i < a_size)
+                       a_shortopts[i++] = ':';
+               else
+                       goto is2big;
+       }
+
+       if (i < a_size)
+               a_shortopts[i] = '\0';
+       else
+               goto is2big;
+
+       return 0;
+
+is2big:
+       errno = E2BIG;
+       return -1;
+}
+
+static void print_usage(const struct option longopts[],
+                       const struct lxc_arguments *a_args)
+
+{
+       int i;
+       const struct option *opt;
+
+       fprintf(stderr, "Usage: %s ", a_args->progname);
+
+       for (opt = longopts, i = 1; opt->name; opt++, i++) {
+               int j;
+               char *uppername;
+
+               uppername = strdup(opt->name);
+               if (!uppername)
+                       exit(-ENOMEM);
+
+               for (j = 0; uppername[j]; j++)
+                       uppername[j] = toupper(uppername[j]);
+
+               fprintf(stderr, "[");
+
+               if (isprint(opt->val))
+                       fprintf(stderr, "-%c|", opt->val);
+
+               fprintf(stderr, "--%s", opt->name);
+
+               if (opt->has_arg == required_argument)
+                       fprintf(stderr, "=%s", uppername);
+
+               if (opt->has_arg == optional_argument)
+                       fprintf(stderr, "[=%s]", uppername);
+
+               fprintf(stderr, "] ");
+
+               if (!(i % 4))
+                       fprintf(stderr, "\n\t");
+
+               free(uppername);
+       }
+
+       fprintf(stderr, "\n");
+       exit(0);
+}
+
+static void print_version()
+{
+       printf("%s%s\n", LXC_VERSION, LXC_DEVEL ? "-devel" : "");
+       exit(0);
+}
+
+static void print_help(const struct lxc_arguments *args, int code)
+{
+       fprintf(stderr, "\
+Usage: %s %s\
+\n\
+Common options :\n\
+  -o, --logfile=FILE               Output log to FILE instead of stderr\n\
+  -l, --logpriority=LEVEL          Set log priority to LEVEL\n\
+  -q, --quiet                      Don't produce any output\n\
+  -P, --lxcpath=PATH               Use specified container path\n\
+  -?, --help                       Give this help list\n\
+      --usage                      Give a short usage message\n\
+      --version                    Print the version number\n\
+\n\
+Mandatory or optional arguments to long options are also mandatory or optional\n\
+for any corresponding short options.\n\
+\n\
+See the %s man page for further information.\n\n",
+       args->progname, args->help, args->progname);
+
+       if (args->helpfn)
+               args->helpfn(args);
+       exit(code);
+}
+
+static int lxc_arguments_lxcpath_add(struct lxc_arguments *args,
+                                    const char *lxcpath)
+{
+       if (args->lxcpath_additional != -1 &&
+           args->lxcpath_cnt > args->lxcpath_additional) {
+               fprintf(stderr,
+                       "This command only accepts %d -P,--lxcpath arguments\n",
+                       args->lxcpath_additional + 1);
+               exit(EXIT_FAILURE);
+       }
+
+       args->lxcpath = realloc(
+           args->lxcpath, (args->lxcpath_cnt + 1) * sizeof(args->lxcpath[0]));
+       if (args->lxcpath == NULL) {
+               lxc_error(args, "no memory");
+               return -ENOMEM;
+       }
+       args->lxcpath[args->lxcpath_cnt++] = lxcpath;
+       return 0;
+}
+
+extern int lxc_arguments_parse(struct lxc_arguments *args, int argc,
+                              char *const argv[])
+{
+       int ret = 0;
+       char shortopts[256];
+
+       ret = build_shortopts(args->options, shortopts, sizeof(shortopts));
+       if (ret < 0) {
+               lxc_error(args, "build_shortopts() failed : %s",
+                         strerror(errno));
+               return ret;
+       }
+
+       while (true) {
+               int c;
+               int index = 0;
+
+               c = getopt_long(argc, argv, shortopts, args->options, &index);
+               if (c == -1)
+                       break;
+               switch (c) {
+               case 'n':
+                       args->name = optarg;
+                       break;
+               case 'o':
+                       args->log_file = optarg;
+                       break;
+               case 'l':
+                       args->log_priority = optarg;
+                       break;
+               case 'q':
+                       args->quiet = 1;
+                       break;
+               case OPT_RCFILE:
+                       args->rcfile = optarg;
+                       break;
+               case 'P':
+                       remove_trailing_slashes(optarg);
+                       ret = lxc_arguments_lxcpath_add(args, optarg);
+                       if (ret < 0)
+                               return ret;
+                       break;
+               case OPT_USAGE:
+                       print_usage(args->options, args);
+               case OPT_VERSION:
+                       print_version();
+               case '?':
+                       print_help(args, 1);
+               case 'h':
+                       print_help(args, 0);
+               default:
+                       if (args->parser) {
+                               ret = args->parser(args, c, optarg);
+                               if (ret)
+                                       goto error;
+                       }
+               }
+       }
+
+       /*
+        * Reclaim the remaining command arguments
+        */
+       args->argv = &argv[optind];
+       args->argc = argc - optind;
+
+       /* If no lxcpaths were given, use default */
+       if (!args->lxcpath_cnt) {
+               ret = lxc_arguments_lxcpath_add(
+                   args, lxc_global_config_value("lxc.lxcpath"));
+               if (ret < 0)
+                       return ret;
+       }
+
+       /* Check the command options */
+
+       if (!args->name && strcmp(args->progname, "lxc-autostart") != 0) {
+               lxc_error(args, "missing container name, use --name option");
+               return -1;
+       }
+
+       if (args->checker)
+               ret = args->checker(args);
+error:
+       if (ret)
+               lxc_error(args, "could not parse command line");
+       return ret;
+}
+
+int lxc_arguments_str_to_int(struct lxc_arguments *args, const char *str)
+{
+       long val;
+       char *endptr;
+
+       errno = 0;
+       val = strtol(str, &endptr, 10);
+       if (errno) {
+               lxc_error(args, "invalid statefd '%s' : %s", str,
+                         strerror(errno));
+               return -1;
+       }
+
+       if (*endptr) {
+               lxc_error(args, "invalid digit for statefd '%s'", str);
+               return -1;
+       }
+
+       return (int)val;
+}
diff --git a/src/lxc/tools/arguments.h b/src/lxc/tools/arguments.h
new file mode 100644 (file)
index 0000000..b07caf4
--- /dev/null
@@ -0,0 +1,173 @@
+/*
+ * lxc: linux Container library
+ *
+ * (C) Copyright IBM Corp. 2007, 2008
+ *
+ * Authors:
+ * Daniel Lezcano <daniel.lezcano at free.fr>
+ * Michel Normand <normand 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef __LXC_ARGUMENTS_H
+#define __LXC_ARGUMENTS_H
+
+#include <getopt.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <sys/types.h>
+
+struct lxc_arguments;
+
+typedef int (*lxc_arguments_parser_t)(struct lxc_arguments *, int, char *);
+typedef int (*lxc_arguments_checker_t)(const struct lxc_arguments *);
+
+struct lxc_arguments {
+       const char *help;
+       void (*helpfn)(const struct lxc_arguments *);
+       const char *progname;
+       const struct option *options;
+       lxc_arguments_parser_t parser;
+       lxc_arguments_checker_t checker;
+
+       const char *name;
+       char *log_file;
+       char *log_priority;
+       int quiet;
+       int daemonize;
+       const char *rcfile;
+       const char *console;
+       const char *console_log;
+       const char *pidfile;
+       const char **lxcpath;
+       int lxcpath_cnt;
+       /* set to 0 to accept only 1 lxcpath, -1 for unlimited */
+       int lxcpath_additional;
+
+       /* for lxc-start */
+       const char *share_ns[32]; /* size must be greater than LXC_NS_MAX */
+
+       /* for lxc-console */
+       unsigned int ttynum;
+       char escape;
+
+       /* for lxc-wait */
+       char *states;
+       long timeout;
+
+       /* for lxc-autostart */
+       int shutdown;
+
+       /* for lxc-stop */
+       int hardstop;
+       int nokill;
+       int nolock;
+       int nowait;
+       int reboot;
+
+       /* for lxc-destroy */
+       int force;
+
+       /* close fds from parent? */
+       int close_all_fds;
+
+       /* lxc-create */
+       char *bdevtype, *configfile, *template;
+       char *fstype;
+       uint64_t fssize;
+       char *lvname, *vgname, *thinpool;
+       char *rbdname, *rbdpool;
+       char *zfsroot, *lowerdir, *dir;
+
+       /* lxc-execute */
+       uid_t uid;
+       gid_t gid;
+
+       /* auto-start */
+       int all;
+       int ignore_auto;
+       int list;
+       char *groups; /* also used by lxc-ls */
+
+       /* lxc-snapshot and lxc-copy */
+       enum task {
+               CLONE,
+               DESTROY,
+               LIST,
+               RESTORE,
+               SNAP,
+               RENAME,
+       } task;
+       int print_comments;
+       char *commentfile;
+       char *newname;
+       char *newpath;
+       char *snapname;
+       int keepdata;
+       int keepname;
+       int keepmac;
+
+       /* lxc-ls */
+       char *ls_fancy_format;
+       char *ls_filter;
+       unsigned int ls_nesting; /* maximum allowed nesting level */
+       bool ls_active;
+       bool ls_fancy;
+       bool ls_frozen;
+       bool ls_line;
+       bool ls_running;
+       bool ls_stopped;
+       bool ls_defined;
+
+       /* lxc-copy */
+       bool tmpfs;
+
+       /* remaining arguments */
+       char *const *argv;
+       int argc;
+
+       /* private arguments */
+       void *data;
+};
+
+#define LXC_COMMON_OPTIONS                                                     \
+           { "name",        required_argument, 0, 'n'         },              \
+            { "help",        no_argument,       0, 'h'         },              \
+           { "usage",       no_argument,       0, OPT_USAGE   },              \
+           { "version",     no_argument,       0, OPT_VERSION },              \
+           { "quiet",       no_argument,       0, 'q'         },              \
+           { "logfile",     required_argument, 0, 'o'         },              \
+           { "logpriority", required_argument, 0, 'l'         },              \
+           { "lxcpath",     required_argument, 0, 'P'         },              \
+           { "rcfile",      required_argument, 0, OPT_RCFILE  },              \
+           { 0,             0,                 0, 0           }
+
+/* option keys for long only options */
+#define OPT_USAGE 0x1000
+#define OPT_VERSION OPT_USAGE - 1
+#define OPT_RCFILE OPT_USAGE - 2
+
+extern int lxc_arguments_parse(struct lxc_arguments *args, int argc,
+                              char *const argv[]);
+
+extern int lxc_arguments_str_to_int(struct lxc_arguments *args,
+                                   const char *str);
+
+#define lxc_error(arg, fmt, args...)                                           \
+       if (!(arg)->quiet)                                                     \
+       fprintf(stderr, "%s: " fmt "\n", (arg)->progname, ##args)
+
+#endif /* __LXC_ARGUMENTS_H */