1 |
commit: 3f593b47e284cd9defa15e19a37357c3e31b1b7f |
2 |
Author: Mike Frysinger <vapier <AT> gentoo <DOT> org> |
3 |
AuthorDate: Mon Sep 21 00:14:44 2015 +0000 |
4 |
Commit: Mike Frysinger <vapier <AT> gentoo <DOT> org> |
5 |
CommitDate: Mon Sep 21 00:14:44 2015 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/sandbox.git/commit/?id=3f593b47 |
7 |
|
8 |
sandbox: add proper option parsing |
9 |
|
10 |
This lays the groundwork for adding more runtime options. |
11 |
|
12 |
Signed-off-by: Mike Frysinger <vapier <AT> gentoo.org> |
13 |
|
14 |
configure.ac | 1 + |
15 |
headers.h | 3 ++ |
16 |
src/Makefile.am | 1 + |
17 |
src/options.c | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
18 |
src/sandbox.c | 45 +++------------------- |
19 |
src/sandbox.h | 3 ++ |
20 |
6 files changed, 129 insertions(+), 40 deletions(-) |
21 |
|
22 |
diff --git a/configure.ac b/configure.ac |
23 |
index b57263e..4c4cb20 100644 |
24 |
--- a/configure.ac |
25 |
+++ b/configure.ac |
26 |
@@ -109,6 +109,7 @@ AC_CHECK_HEADERS_ONCE(m4_flatten([ |
27 |
errno.h |
28 |
execinfo.h |
29 |
fcntl.h |
30 |
+ getopt.h |
31 |
grp.h |
32 |
inttypes.h |
33 |
libgen.h |
34 |
|
35 |
diff --git a/headers.h b/headers.h |
36 |
index 1dc140e..458958b 100644 |
37 |
--- a/headers.h |
38 |
+++ b/headers.h |
39 |
@@ -29,6 +29,9 @@ |
40 |
#ifdef HAVE_FCNTL_H |
41 |
# include <fcntl.h> |
42 |
#endif |
43 |
+#ifdef HAVE_GETOPT_H |
44 |
+# include <getopt.h> |
45 |
+#endif |
46 |
#ifdef HAVE_GRP_H |
47 |
# include <grp.h> |
48 |
#endif |
49 |
|
50 |
diff --git a/src/Makefile.am b/src/Makefile.am |
51 |
index c3c1f45..24ffdcf 100644 |
52 |
--- a/src/Makefile.am |
53 |
+++ b/src/Makefile.am |
54 |
@@ -11,5 +11,6 @@ AM_CPPFLAGS = \ |
55 |
sandbox_LDADD = $(top_builddir)/libsbutil/libsbutil.la $(LIBDL) |
56 |
sandbox_SOURCES = \ |
57 |
environ.c \ |
58 |
+ options.c \ |
59 |
sandbox.h \ |
60 |
sandbox.c |
61 |
|
62 |
diff --git a/src/options.c b/src/options.c |
63 |
new file mode 100644 |
64 |
index 0000000..10f937c |
65 |
--- /dev/null |
66 |
+++ b/src/options.c |
67 |
@@ -0,0 +1,116 @@ |
68 |
+/* |
69 |
+ * Handle command line arguments |
70 |
+ * |
71 |
+ * Copyright 1999-2015 Gentoo Foundation |
72 |
+ * Licensed under the GPL-2 |
73 |
+ */ |
74 |
+ |
75 |
+#include "headers.h" |
76 |
+#include "sbutil.h" |
77 |
+#include "sandbox.h" |
78 |
+ |
79 |
+static void show_version(void) |
80 |
+{ |
81 |
+ puts( |
82 |
+ "Gentoo path sandbox\n" |
83 |
+ " version: " PACKAGE_VERSION "\n" |
84 |
+ " C lib: " LIBC_VERSION " (" LIBC_PATH ")\n" |
85 |
+ " build: " __DATE__ " " __TIME__ "\n" |
86 |
+ " contact: " PACKAGE_BUGREPORT " via http://bugs.gentoo.org/\n" |
87 |
+ " rtld: " |
88 |
+#ifdef BROKEN_RTLD_NEXT |
89 |
+ "next is broken ;(\n" |
90 |
+#else |
91 |
+ "next is OK! :D\n" |
92 |
+#endif |
93 |
+#ifndef SB_SCHIZO |
94 |
+# define SB_SCHIZO "no" |
95 |
+#endif |
96 |
+ " schizo: " SB_SCHIZO "\n" |
97 |
+ "\nconfigured with these options:\n" |
98 |
+ SANDBOX_CONFIGURE_OPTS |
99 |
+ ); |
100 |
+ exit(0); |
101 |
+} |
102 |
+ |
103 |
+#define PARSE_FLAGS "+hV" |
104 |
+#define a_argument required_argument |
105 |
+static struct option const long_opts[] = { |
106 |
+ {"help", no_argument, NULL, 'h'}, |
107 |
+ {"version", no_argument, NULL, 'V'}, |
108 |
+ {NULL, no_argument, NULL, 0x0} |
109 |
+}; |
110 |
+static const char * const opts_help[] = { |
111 |
+ "Print this help and exit", |
112 |
+ "Print version and exit", |
113 |
+ NULL |
114 |
+}; |
115 |
+ |
116 |
+static void show_usage(int status) |
117 |
+{ |
118 |
+ const char a_arg[] = "<arg>"; |
119 |
+ size_t a_arg_len = strlen(a_arg) + 2; |
120 |
+ size_t i; |
121 |
+ int optlen; |
122 |
+ FILE *fp = status ? stderr : stdout; |
123 |
+ |
124 |
+ fprintf(fp, |
125 |
+ "Usage: sandbox [options] [program [program args...]]\n" |
126 |
+ "\n" |
127 |
+ "Sandbox will start up a sandbox session and execute the specified program.\n" |
128 |
+ "If no program is specified, an interactive shell is automatically launched.\n" |
129 |
+ "You can use this to quickly test out sandbox behavior.\n" |
130 |
+ "\n" |
131 |
+ "Upon startup, initial settings are taken from these files / directories:\n" |
132 |
+ "\t" SANDBOX_CONF_FILE "\n" |
133 |
+ "\t" SANDBOX_CONFD_DIR "\n" |
134 |
+ ); |
135 |
+ |
136 |
+ fprintf(fp, "\nOptions: -[%s]\n", PARSE_FLAGS + 1); |
137 |
+ /* prescan the --long opt length to auto-align */ |
138 |
+ optlen = 0; |
139 |
+ for (i = 0; long_opts[i].name; ++i) { |
140 |
+ int l = strlen(long_opts[i].name); |
141 |
+ if (long_opts[i].has_arg == a_argument) |
142 |
+ l += a_arg_len; |
143 |
+ optlen = MAX(l, optlen); |
144 |
+ } |
145 |
+ |
146 |
+ for (i = 0; long_opts[i].name; ++i) { |
147 |
+ /* first output the short flag if it has one */ |
148 |
+ if (long_opts[i].val > '~' || long_opts[i].val < ' ') |
149 |
+ printf(" "); |
150 |
+ else |
151 |
+ printf(" -%c, ", long_opts[i].val); |
152 |
+ |
153 |
+ /* then the long flag */ |
154 |
+ if (long_opts[i].has_arg == no_argument) |
155 |
+ printf("--%-*s", optlen, long_opts[i].name); |
156 |
+ else |
157 |
+ printf("--%s %s %*s", long_opts[i].name, a_arg, |
158 |
+ (int)(optlen - strlen(long_opts[i].name) - a_arg_len), ""); |
159 |
+ |
160 |
+ /* finally the help text */ |
161 |
+ printf(" * %s\n", opts_help[i]); |
162 |
+ } |
163 |
+ |
164 |
+ fprintf(fp, "\nContact: " PACKAGE_BUGREPORT " via http://bugs.gentoo.org/\n"); |
165 |
+ |
166 |
+ exit(status); |
167 |
+} |
168 |
+ |
169 |
+void parseargs(int argc, char *argv[]) |
170 |
+{ |
171 |
+ int i; |
172 |
+ |
173 |
+ while ((i = getopt_long(argc, argv, PARSE_FLAGS, long_opts, NULL)) != -1) { |
174 |
+ switch (i) { |
175 |
+ case 'V': |
176 |
+ show_version(); |
177 |
+ case 'h': |
178 |
+ show_usage(0); |
179 |
+ case '?': |
180 |
+ show_usage(1); |
181 |
+ } |
182 |
+ } |
183 |
+} |
184 |
|
185 |
diff --git a/src/sandbox.c b/src/sandbox.c |
186 |
index c2a1d25..15c87b2 100644 |
187 |
--- a/src/sandbox.c |
188 |
+++ b/src/sandbox.c |
189 |
@@ -204,49 +204,14 @@ int main(int argc, char **argv) |
190 |
|
191 |
char *run_str = "-c"; |
192 |
|
193 |
+ /* Process the sandbox opts and leave argc/argv for the target. */ |
194 |
+ parseargs(argc, argv); |
195 |
+ argc -= optind - 1; |
196 |
+ argv[optind - 1] = argv[0]; |
197 |
+ argv += optind - 1; |
198 |
/* Only print info if called with no arguments .... */ |
199 |
if (argc < 2) |
200 |
print_debug = 1; |
201 |
- else { |
202 |
- /* handle a few common options */ |
203 |
- if (!strcmp(argv[1], "--version") || !strcmp(argv[1], "-V")) { |
204 |
- puts( |
205 |
- "Gentoo path sandbox\n" |
206 |
- " version: " PACKAGE_VERSION "\n" |
207 |
- " C lib: " LIBC_VERSION " (" LIBC_PATH ")\n" |
208 |
- " build: " __DATE__ " " __TIME__ "\n" |
209 |
- " contact: " PACKAGE_BUGREPORT " via http://bugs.gentoo.org/\n" |
210 |
- " rtld: " |
211 |
-#ifdef BROKEN_RTLD_NEXT |
212 |
- "next is broken ;(\n" |
213 |
-#else |
214 |
- "next is OK! :D\n" |
215 |
-#endif |
216 |
-#ifndef SB_SCHIZO |
217 |
-# define SB_SCHIZO "no" |
218 |
-#endif |
219 |
- " schizo: " SB_SCHIZO "\n" |
220 |
- "\nconfigured with these options:\n" |
221 |
- SANDBOX_CONFIGURE_OPTS |
222 |
- ); |
223 |
- return 0; |
224 |
- } else if (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")) { |
225 |
- puts( |
226 |
- "Usage: sandbox [program [program args...]]\n" |
227 |
- "\n" |
228 |
- "Sandbox will start up a sandbox session and execute the specified program.\n" |
229 |
- "If no program is specified, an interactive shell is automatically launched.\n" |
230 |
- "You can use this to quickly test out sandbox behavior.\n" |
231 |
- "\n" |
232 |
- "Upon startup, initial settings are taken from these files / directories:\n" |
233 |
- "\t" SANDBOX_CONF_FILE "\n" |
234 |
- "\t" SANDBOX_CONFD_DIR "\n" |
235 |
- "\n" |
236 |
- "Contact: " PACKAGE_BUGREPORT " via http://bugs.gentoo.org/" |
237 |
- ); |
238 |
- return 0; |
239 |
- } |
240 |
- } |
241 |
|
242 |
dputs(sandbox_banner); |
243 |
|
244 |
|
245 |
diff --git a/src/sandbox.h b/src/sandbox.h |
246 |
index c0c4315..361d468 100644 |
247 |
--- a/src/sandbox.h |
248 |
+++ b/src/sandbox.h |
249 |
@@ -32,4 +32,7 @@ extern char **setup_environ(struct sandbox_info_t *sandbox_info); |
250 |
#define sb_err(fmt, args...) _sb_err(warn, fmt, ## args) |
251 |
#define sb_perr(fmt, args...) _sb_err(pwarn, fmt, ## args) |
252 |
|
253 |
+/* Option parsing related code */ |
254 |
+extern void parseargs(int argc, char *argv[]); |
255 |
+ |
256 |
#endif |