]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/tools/arguments.c
f6ee123dce813b12c58ba14ad820baa9202dfc09
2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <daniel.lezcano at free.fr>
8 * Michel Normand <normand at fr.ibm.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
34 #include <sys/types.h>
36 #include <lxc/lxccontainer.h>
37 #include <lxc/version.h>
39 #include "arguments.h"
40 #include "namespace.h"
41 #include "initutils.h"
43 static int build_shortopts(const struct option
*a_options
, char *a_shortopts
,
47 const struct option
*opt
;
49 if (!a_options
|| !a_shortopts
|| !a_size
)
52 for (opt
= a_options
; opt
->name
; opt
++) {
53 if (!isascii(opt
->val
))
57 a_shortopts
[i
++] = opt
->val
;
61 if (opt
->has_arg
== no_argument
)
65 a_shortopts
[i
++] = ':';
69 if (opt
->has_arg
== required_argument
)
73 a_shortopts
[i
++] = ':';
79 a_shortopts
[i
] = '\0';
90 static void print_usage_exit(const struct option longopts
[],
91 const struct lxc_arguments
*a_args
)
95 const struct option
*opt
;
97 fprintf(stderr
, "Usage: %s ", a_args
->progname
);
99 for (opt
= longopts
, i
= 1; opt
->name
; opt
++, i
++) {
100 fprintf(stderr
, "[");
102 if (isprint(opt
->val
))
103 fprintf(stderr
, "-%c|", opt
->val
);
105 fprintf(stderr
, "--%s", opt
->name
);
107 if ((opt
->has_arg
== required_argument
) ||
108 (opt
->has_arg
== optional_argument
)) {
112 uppername
= strdup(opt
->name
);
116 for (j
= 0; uppername
[j
]; j
++)
117 uppername
[j
] = toupper(uppername
[j
]);
119 if (opt
->has_arg
== required_argument
)
120 fprintf(stderr
, "=%s", uppername
);
121 else // optional_argument
122 fprintf(stderr
, "[=%s]", uppername
);
127 fprintf(stderr
, "] ");
130 fprintf(stderr
, "\n\t");
133 fprintf(stderr
, "\n");
137 static void print_version_exit()
139 printf("%s\n", lxc_get_version());
143 static void print_help_exit(const struct lxc_arguments
*args
, int code
)
149 -o, --logfile=FILE Output log to FILE instead of stderr\n\
150 -l, --logpriority=LEVEL Set log priority to LEVEL\n\
151 -q, --quiet Don't produce any output\n\
152 -P, --lxcpath=PATH Use specified container path\n\
153 -?, --help Give this help list\n\
154 --usage Give a short usage message\n\
155 --version Print the version number\n\
157 Mandatory or optional arguments to long options are also mandatory or optional\n\
158 for any corresponding short options.\n\
160 See the %s man page for further information.\n\n",
161 args
->progname
, args
->help
, args
->progname
);
169 static int lxc_arguments_lxcpath_add(struct lxc_arguments
*args
,
172 if (args
->lxcpath_additional
!= -1 &&
173 args
->lxcpath_cnt
> args
->lxcpath_additional
) {
175 "This command only accepts %d -P,--lxcpath arguments\n",
176 args
->lxcpath_additional
+ 1);
180 args
->lxcpath
= realloc(
181 args
->lxcpath
, (args
->lxcpath_cnt
+ 1) * sizeof(args
->lxcpath
[0]));
182 if (args
->lxcpath
== NULL
) {
183 lxc_error(args
, "no memory");
187 args
->lxcpath
[args
->lxcpath_cnt
++] = lxcpath
;
191 extern int lxc_arguments_parse(struct lxc_arguments
*args
, int argc
,
195 bool logfile
= false;
198 ret
= build_shortopts(args
->options
, shortopts
, sizeof(shortopts
));
200 lxc_error(args
, "build_shortopts() failed : %s",
209 c
= getopt_long(argc
, argv
, shortopts
, args
->options
, &index
);
218 args
->log_file
= optarg
;
222 args
->log_priority
= optarg
;
225 strcmp(args
->log_file
, "none") == 0)
226 args
->log_file
= NULL
;
232 args
->rcfile
= optarg
;
235 remove_trailing_slashes(optarg
);
236 ret
= lxc_arguments_lxcpath_add(args
, optarg
);
241 print_usage_exit(args
->options
, args
);
243 print_version_exit();
245 print_help_exit(args
, 1);
247 print_help_exit(args
, 0);
250 ret
= args
->parser(args
, c
, optarg
);
258 * Reclaim the remaining command arguments
260 args
->argv
= &argv
[optind
];
261 args
->argc
= argc
- optind
;
263 /* If no lxcpaths were given, use default */
264 if (!args
->lxcpath_cnt
) {
265 ret
= lxc_arguments_lxcpath_add(
266 args
, lxc_get_global_config_item("lxc.lxcpath"));
271 /* Check the command options */
272 if (!args
->name
&& strncmp(args
->progname
, "lxc-autostart", strlen(args
->progname
)) != 0
273 && strncmp(args
->progname
, "lxc-unshare", strlen(args
->progname
)) != 0) {
275 args
->name
= argv
[optind
];
277 args
->argv
= &argv
[optind
];
278 args
->argc
= argc
- optind
;
282 lxc_error(args
, "No container name specified");
288 ret
= args
->checker(args
);
292 lxc_error(args
, "could not parse command line");
297 int lxc_arguments_str_to_int(struct lxc_arguments
*args
, const char *str
)
303 val
= strtol(str
, &endptr
, 10);
305 lxc_error(args
, "invalid statefd '%s' : %s", str
,
311 lxc_error(args
, "invalid digit for statefd '%s'", str
);
318 bool lxc_setup_shared_ns(struct lxc_arguments
*args
, struct lxc_container
*c
)
322 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
323 const char *key
, *value
;
325 value
= args
->share_ns
[i
];
330 key
= "lxc.namespace.share.net";
331 else if (i
== LXC_NS_IPC
)
332 key
= "lxc.namespace.share.ipc";
333 else if (i
== LXC_NS_UTS
)
334 key
= "lxc.namespace.share.uts";
335 else if (i
== LXC_NS_PID
)
336 key
= "lxc.namespace.share.pid";
340 if (!c
->set_config_item(c
, key
, value
)) {
341 lxc_error(args
, "Failed to set \"%s = %s\"", key
, value
);