]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ | |
2 | ||
3 | #include "config.h" | |
4 | ||
5 | #include <ctype.h> | |
6 | #include <errno.h> | |
7 | #include <fcntl.h> | |
8 | #include <getopt.h> | |
9 | #include <libgen.h> | |
10 | #include <limits.h> | |
11 | #include <stdio.h> | |
12 | #include <stdlib.h> | |
13 | #include <string.h> | |
14 | #include <sys/stat.h> | |
15 | #include <sys/types.h> | |
16 | #include <unistd.h> | |
17 | ||
18 | #include "lxc.h" | |
19 | #include "version.h" | |
20 | ||
21 | #include "compiler.h" | |
22 | #include "initutils.h" | |
23 | #include "memory_utils.h" | |
24 | #include "parse.h" | |
25 | #include "string_utils.h" | |
26 | ||
27 | /* option keys for long only options */ | |
28 | #define OPT_USAGE 0x1000 | |
29 | #define OPT_VERSION (OPT_USAGE - 1) | |
30 | ||
31 | #define QUOTE(macro) #macro | |
32 | #define QUOTEVAL(macro) QUOTE(macro) | |
33 | ||
34 | static struct option long_options[] = { | |
35 | { "name", required_argument, 0, 'n' }, | |
36 | { "help", no_argument, 0, 'h' }, | |
37 | { "usage", no_argument, 0, OPT_USAGE }, | |
38 | { "version", no_argument, 0, OPT_VERSION }, | |
39 | { "quiet", no_argument, 0, 'q' }, | |
40 | { "lxcpath", required_argument, 0, 'P' }, | |
41 | { 0, 0, 0, 0 } | |
42 | }; | |
43 | static const char short_options[] = "n:hqo:l:P:"; | |
44 | ||
45 | struct arguments { | |
46 | const struct option *options; | |
47 | const char *shortopts; | |
48 | ||
49 | const char *name; | |
50 | bool quiet; | |
51 | const char *lxcpath; | |
52 | ||
53 | /* remaining arguments */ | |
54 | char *const *argv; | |
55 | int argc; | |
56 | }; | |
57 | ||
58 | static struct arguments my_args = { | |
59 | .options = long_options, | |
60 | .shortopts = short_options | |
61 | }; | |
62 | ||
63 | __noreturn static void print_usage_exit(const struct option longopts[]) | |
64 | ||
65 | { | |
66 | fprintf(stderr, "Usage: lxc-init [-n|--name=NAME] [-h|--help] [--usage] [--version]\n\ | |
67 | [-q|--quiet] [-P|--lxcpath=LXCPATH]\n"); | |
68 | exit(EXIT_SUCCESS); | |
69 | } | |
70 | ||
71 | __noreturn static void print_version_exit(void) | |
72 | { | |
73 | printf("%s\n", LXC_VERSION); | |
74 | exit(EXIT_SUCCESS); | |
75 | } | |
76 | ||
77 | static void print_help(void) | |
78 | { | |
79 | fprintf(stderr, "\ | |
80 | Usage: lxc-init --name=NAME -- COMMAND\n\ | |
81 | \n\ | |
82 | lxc-init start a COMMAND as PID 2 inside a container\n\ | |
83 | \n\ | |
84 | Options :\n\ | |
85 | -n, --name=NAME NAME of the container\n\ | |
86 | -q, --quiet Don't produce any output\n\ | |
87 | -P, --lxcpath=PATH Use specified container path\n\ | |
88 | -?, --help Give this help list\n\ | |
89 | --usage Give a short usage message\n\ | |
90 | --version Print the version number\n\ | |
91 | \n\ | |
92 | Mandatory or optional arguments to long options are also mandatory or optional\n\ | |
93 | for any corresponding short options.\n\ | |
94 | \n\ | |
95 | See the lxc-init man page for further information.\n\n"); | |
96 | } | |
97 | ||
98 | static int arguments_parse(struct arguments *args, int argc, | |
99 | char *const argv[]) | |
100 | { | |
101 | for (;;) { | |
102 | int c; | |
103 | int index = 0; | |
104 | ||
105 | c = getopt_long(argc, argv, args->shortopts, args->options, &index); | |
106 | if (c == -1) | |
107 | break; | |
108 | switch (c) { | |
109 | case 'n': | |
110 | args->name = optarg; | |
111 | break; | |
112 | case 'o': | |
113 | break; | |
114 | case 'l': | |
115 | break; | |
116 | case 'q': | |
117 | args->quiet = true; | |
118 | break; | |
119 | case 'P': | |
120 | remove_trailing_slashes(optarg); | |
121 | args->lxcpath = optarg; | |
122 | break; | |
123 | case OPT_USAGE: | |
124 | print_usage_exit(args->options); | |
125 | case OPT_VERSION: | |
126 | print_version_exit(); | |
127 | case '?': | |
128 | print_help(); | |
129 | exit(EXIT_FAILURE); | |
130 | case 'h': | |
131 | print_help(); | |
132 | exit(EXIT_SUCCESS); | |
133 | } | |
134 | } | |
135 | ||
136 | /* | |
137 | * Reclaim the remaining command arguments | |
138 | */ | |
139 | args->argv = &argv[optind]; | |
140 | args->argc = argc - optind; | |
141 | ||
142 | /* If no lxcpath was given, use default */ | |
143 | if (!args->lxcpath) | |
144 | args->lxcpath = lxc_global_config_value("lxc.lxcpath"); | |
145 | ||
146 | /* Check the command options */ | |
147 | if (!args->name) { | |
148 | if (!args->quiet) | |
149 | fprintf(stderr, "lxc-init: missing container name, use --name option\n"); | |
150 | return -1; | |
151 | } | |
152 | ||
153 | return 0; | |
154 | } | |
155 | ||
156 | int main(int argc, char *argv[]) | |
157 | { | |
158 | if (arguments_parse(&my_args, argc, argv)) | |
159 | exit(EXIT_FAILURE); | |
160 | ||
161 | if (!my_args.argc) { | |
162 | if (my_args.quiet) | |
163 | fprintf(stderr, "Please specify a command to execute\n"); | |
164 | exit(EXIT_FAILURE); | |
165 | } | |
166 | ||
167 | lxc_container_init(my_args.argc, my_args.argv, my_args.quiet); | |
168 | } |