]>
Commit | Line | Data |
---|---|---|
cc73685d | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
05f05512 | 2 | |
afdad179 CB |
3 | #include "config.h" |
4 | ||
2ec1c484 | 5 | #include <ctype.h> |
05f05512 | 6 | #include <errno.h> |
b30b7eea | 7 | #include <fcntl.h> |
a6f151a7 | 8 | #include <getopt.h> |
8559e703 | 9 | #include <libgen.h> |
eb22a12b | 10 | #include <limits.h> |
a6f151a7 CB |
11 | #include <stdio.h> |
12 | #include <stdlib.h> | |
13 | #include <string.h> | |
0e322d22 | 14 | #include <sys/stat.h> |
05f05512 | 15 | #include <sys/types.h> |
2ec1c484 | 16 | #include <unistd.h> |
00b3c2e2 | 17 | |
12ae2a33 CB |
18 | #include "lxc.h" |
19 | #include "version.h" | |
1574fc24 | 20 | |
cf0fd972 | 21 | #include "compiler.h" |
4295c5de | 22 | #include "initutils.h" |
3cf7d8c2 | 23 | #include "memory_utils.h" |
1933b53f | 24 | #include "parse.h" |
37ef15bb | 25 | #include "string_utils.h" |
5ee606bc R |
26 | |
27 | /* option keys for long only options */ | |
28 | #define OPT_USAGE 0x1000 | |
e8fcdf3d | 29 | #define OPT_VERSION (OPT_USAGE - 1) |
8559e703 | 30 | |
c379af4c TA |
31 | #define QUOTE(macro) #macro |
32 | #define QUOTEVAL(macro) QUOTE(macro) | |
33 | ||
5ee606bc | 34 | static struct option long_options[] = { |
0059379f | 35 | { "name", required_argument, 0, 'n' }, |
78c16484 | 36 | { "help", no_argument, 0, 'h' }, |
0059379f CB |
37 | { "usage", no_argument, 0, OPT_USAGE }, |
38 | { "version", no_argument, 0, OPT_VERSION }, | |
39 | { "quiet", no_argument, 0, 'q' }, | |
0059379f | 40 | { "lxcpath", required_argument, 0, 'P' }, |
5ee606bc R |
41 | { 0, 0, 0, 0 } |
42 | }; | |
7f5700e6 | 43 | static const char short_options[] = "n:hqo:l:P:"; |
5ee606bc R |
44 | |
45 | struct arguments { | |
46 | const struct option *options; | |
47 | const char *shortopts; | |
48 | ||
49 | const char *name; | |
d51dde8a | 50 | bool quiet; |
5ee606bc R |
51 | const char *lxcpath; |
52 | ||
53 | /* remaining arguments */ | |
54 | char *const *argv; | |
55 | int argc; | |
a6f151a7 CB |
56 | }; |
57 | ||
5ee606bc R |
58 | static struct arguments my_args = { |
59 | .options = long_options, | |
60 | .shortopts = short_options | |
a6f151a7 CB |
61 | }; |
62 | ||
ed586164 CB |
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 | ||
a81bad13 RW |
156 | int main(int argc, char *argv[]) |
157 | { | |
5ee606bc | 158 | if (arguments_parse(&my_args, argc, argv)) |
a6f151a7 | 159 | exit(EXIT_FAILURE); |
05f05512 | 160 | |
a6f151a7 | 161 | if (!my_args.argc) { |
b30b7eea CB |
162 | if (my_args.quiet) |
163 | fprintf(stderr, "Please specify a command to execute\n"); | |
e0b0b533 | 164 | exit(EXIT_FAILURE); |
05f05512 | 165 | } |
166 | ||
96294efb | 167 | lxc_container_init(my_args.argc, my_args.argv, my_args.quiet); |
05f05512 | 168 | } |