/
arguments.c
291 lines (244 loc) · 6.24 KB
/
arguments.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
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;
}