]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/tools/arguments.c
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"
41 #include "namespace.h"
42 #include "initutils.h"
44 static int build_shortopts(const struct option
*a_options
, char *a_shortopts
,
48 const struct option
*opt
;
50 if (!a_options
|| !a_shortopts
|| !a_size
)
53 for (opt
= a_options
; opt
->name
; opt
++) {
54 if (!isascii(opt
->val
))
58 a_shortopts
[i
++] = opt
->val
;
62 if (opt
->has_arg
== no_argument
)
66 a_shortopts
[i
++] = ':';
70 if (opt
->has_arg
== required_argument
)
74 a_shortopts
[i
++] = ':';
80 a_shortopts
[i
] = '\0';
91 __noreturn__
static void print_usage_exit(const struct option longopts
[],
92 const struct lxc_arguments
*a_args
)
96 const struct option
*opt
;
98 fprintf(stderr
, "Usage: %s ", a_args
->progname
);
100 for (opt
= longopts
, i
= 1; opt
->name
; opt
++, i
++) {
101 fprintf(stderr
, "[");
103 if (isprint(opt
->val
))
104 fprintf(stderr
, "-%c|", opt
->val
);
106 fprintf(stderr
, "--%s", opt
->name
);
108 if ((opt
->has_arg
== required_argument
) ||
109 (opt
->has_arg
== optional_argument
)) {
113 uppername
= strdup(opt
->name
);
117 for (j
= 0; uppername
[j
]; j
++)
118 uppername
[j
] = toupper(uppername
[j
]);
120 if (opt
->has_arg
== required_argument
)
121 fprintf(stderr
, "=%s", uppername
);
122 else // optional_argument
123 fprintf(stderr
, "[=%s]", uppername
);
128 fprintf(stderr
, "] ");
131 fprintf(stderr
, "\n\t");
134 fprintf(stderr
, "\n");
138 __noreturn__
static void print_version_exit()
140 printf("%s\n", lxc_get_version());
144 __noreturn__
static void print_help_exit(const struct lxc_arguments
*args
,
151 -o, --logfile=FILE Output log to FILE instead of stderr\n\
152 -l, --logpriority=LEVEL Set log priority to LEVEL\n\
153 -q, --quiet Don't produce any output\n\
154 -P, --lxcpath=PATH Use specified container path\n\
155 -?, --help Give this help list\n\
156 --usage Give a short usage message\n\
157 --version Print the version number\n\
159 Mandatory or optional arguments to long options are also mandatory or optional\n\
160 for any corresponding short options.\n\
162 See the %s man page for further information.\n\n",
163 args
->progname
, args
->help
, args
->progname
);
171 static int lxc_arguments_lxcpath_add(struct lxc_arguments
*args
,
174 if (args
->lxcpath_additional
!= -1 &&
175 args
->lxcpath_cnt
> args
->lxcpath_additional
) {
177 "This command only accepts %d -P,--lxcpath arguments\n",
178 args
->lxcpath_additional
+ 1);
182 args
->lxcpath
= realloc(
183 args
->lxcpath
, (args
->lxcpath_cnt
+ 1) * sizeof(args
->lxcpath
[0]));
184 if (args
->lxcpath
== NULL
) {
185 lxc_error(args
, "no memory");
189 args
->lxcpath
[args
->lxcpath_cnt
++] = lxcpath
;
193 extern int lxc_arguments_parse(struct lxc_arguments
*args
, int argc
,
197 bool logfile
= false;
200 ret
= build_shortopts(args
->options
, shortopts
, sizeof(shortopts
));
202 lxc_error(args
, "build_shortopts() failed : %s",
211 c
= getopt_long(argc
, argv
, shortopts
, args
->options
, &index
);
220 args
->log_file
= optarg
;
224 args
->log_priority
= optarg
;
227 strcmp(args
->log_file
, "none") == 0)
228 args
->log_file
= NULL
;
234 args
->rcfile
= optarg
;
237 remove_trailing_slashes(optarg
);
238 ret
= lxc_arguments_lxcpath_add(args
, optarg
);
243 print_usage_exit(args
->options
, args
);
245 print_version_exit();
247 print_help_exit(args
, 1);
249 print_help_exit(args
, 0);
252 ret
= args
->parser(args
, c
, optarg
);
260 * Reclaim the remaining command arguments
262 args
->argv
= &argv
[optind
];
263 args
->argc
= argc
- optind
;
265 /* If no lxcpaths were given, use default */
266 if (!args
->lxcpath_cnt
) {
267 ret
= lxc_arguments_lxcpath_add(
268 args
, lxc_get_global_config_item("lxc.lxcpath"));
273 /* Check the command options */
274 if (!args
->name
&& strncmp(args
->progname
, "lxc-autostart", strlen(args
->progname
)) != 0
275 && strncmp(args
->progname
, "lxc-unshare", strlen(args
->progname
)) != 0) {
277 args
->name
= argv
[optind
];
279 args
->argv
= &argv
[optind
];
280 args
->argc
= argc
- optind
;
284 lxc_error(args
, "No container name specified");
290 ret
= args
->checker(args
);
294 lxc_error(args
, "could not parse command line");
299 int lxc_arguments_str_to_int(struct lxc_arguments
*args
, const char *str
)
305 val
= strtol(str
, &endptr
, 10);
307 lxc_error(args
, "invalid statefd '%s' : %s", str
,
313 lxc_error(args
, "invalid digit for statefd '%s'", str
);
320 bool lxc_setup_shared_ns(struct lxc_arguments
*args
, struct lxc_container
*c
)
324 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
325 const char *key
, *value
;
327 value
= args
->share_ns
[i
];
332 key
= "lxc.namespace.share.net";
333 else if (i
== LXC_NS_IPC
)
334 key
= "lxc.namespace.share.ipc";
335 else if (i
== LXC_NS_UTS
)
336 key
= "lxc.namespace.share.uts";
337 else if (i
== LXC_NS_PID
)
338 key
= "lxc.namespace.share.pid";
342 if (!c
->set_config_item(c
, key
, value
)) {
343 lxc_error(args
, "Failed to set \"%s = %s\"", key
, value
);