forked from servalproject/serval-dna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommandline.c
566 lines (519 loc) · 18.8 KB
/
commandline.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/*
Serval DNA command-line functions
Copyright (C) 2010-2013 Serval Project Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "serval.h"
#include "conf.h"
#include "mdp_client.h"
#include "cli.h"
#include "keyring.h"
#include "dataformats.h"
#include "commandline.h"
#include "server.h"
DEFINE_CMD(commandline_usage,CLIFLAG_PERMISSIVE_CONFIG,
"Display command usage.",
"help|-h|--help","...");
static int commandline_usage(const struct cli_parsed *parsed, struct cli_context *UNUSED(context))
{
printf("Serval DNA version %s\nUsage:\n", version_servald);
return cli_usage_parsed(parsed, XPRINTF_STDIO(stdout));
}
DEFINE_CMD(version_message,CLIFLAG_PERMISSIVE_CONFIG,
"Display copyright information.",
"version|copyright");
static int version_message(const struct cli_parsed *UNUSED(parsed), struct cli_context *UNUSED(context))
{
printf("Serval DNA version %s\n%s\n", version_servald, copyright_servald);
printf("\
License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>.\n\
This is free software: you are free to change and redistribute it.\n\
There is NO WARRANTY, to the extent permitted by law.\n\
");
return 0;
}
/* Data structures for accumulating output of a single JNI call.
*/
#ifdef HAVE_JNI_H
#define OUTV_BUFFER_ALLOCSIZE (8192)
jclass IJniResults = NULL;
jmethodID startResultSet, setColumnName, putString, putBlob, putLong, putDouble, totalRowCount;
static int outv_growbuf(struct cli_context *context, size_t needed)
{
assert(context->outv_current <= context->outv_limit);
size_t remaining = (size_t)(context->outv_limit - context->outv_current);
if (remaining < needed) {
size_t cursize = context->outv_current - context->outv_buffer;
size_t newsize = cursize + needed;
// Round up to nearest multiple of OUTV_BUFFER_ALLOCSIZE.
newsize = newsize + OUTV_BUFFER_ALLOCSIZE - ((newsize - 1) % OUTV_BUFFER_ALLOCSIZE + 1);
assert(newsize > cursize);
assert((size_t)(newsize - cursize) >= needed);
context->outv_buffer = realloc(context->outv_buffer, newsize);
if (context->outv_buffer == NULL)
return WHYF("Out of memory allocating %lu bytes", (unsigned long) newsize);
context->outv_current = context->outv_buffer + cursize;
context->outv_limit = context->outv_buffer + newsize;
}
return 0;
}
static int put_blob(struct cli_context *context, jbyte *value, jsize length){
jbyteArray arr = NULL;
if (context->jni_exception)
return -1;
arr = (*context->jni_env)->NewByteArray(context->jni_env, length);
if (arr == NULL || (*context->jni_env)->ExceptionCheck(context->jni_env)) {
context->jni_exception = 1;
return WHY("Exception thrown from NewByteArray()");
}
if (value && length>0){
(*context->jni_env)->SetByteArrayRegion(context->jni_env, arr, 0, length, value);
if ((*context->jni_env)->ExceptionCheck(context->jni_env)) {
context->jni_exception = 1;
return WHYF("Exception thrown from SetByteArrayRegion()");
}
}
(*context->jni_env)->CallVoidMethod(context->jni_env, context->jniResults, putBlob, arr);
if ((*context->jni_env)->ExceptionCheck(context->jni_env)) {
context->jni_exception = 1;
return WHY("Exception thrown from CallVoidMethod(putBlob)");
}
if (arr)
(*context->jni_env)->DeleteLocalRef(context->jni_env, arr);
return 0;
}
static int outv_end_field(struct cli_context *context)
{
jsize length = context->outv_current - context->outv_buffer;
context->outv_current = context->outv_buffer;
return put_blob(context, (jbyte *)context->outv_buffer, length);
}
int Throw(JNIEnv *env, const char *class, const char *msg)
{
jclass exceptionClass = NULL;
if ((exceptionClass = (*env)->FindClass(env, class)) == NULL)
return -1; // exception
(*env)->ThrowNew(env, exceptionClass, msg);
return -1;
}
int initJniTypes(JNIEnv *env)
{
if (IJniResults)
return 0;
IJniResults = (*env)->FindClass(env, "org/servalproject/servaldna/IJniResults");
if (IJniResults==NULL)
return Throw(env, "java/lang/IllegalStateException", "Unable to locate class org.servalproject.servaldna.IJniResults");
// make sure the interface class cannot be garbage collected between invocations in the same process
IJniResults = (jclass)(*env)->NewGlobalRef(env, IJniResults);
if (IJniResults==NULL)
return Throw(env, "java/lang/IllegalStateException", "Unable to create global ref to class org.servalproject.servaldna.IJniResults");
startResultSet = (*env)->GetMethodID(env, IJniResults, "startResultSet", "(I)V");
if (startResultSet==NULL)
return Throw(env, "java/lang/IllegalStateException", "Unable to locate method startResultSet");
setColumnName = (*env)->GetMethodID(env, IJniResults, "setColumnName", "(ILjava/lang/String;)V");
if (setColumnName==NULL)
return Throw(env, "java/lang/IllegalStateException", "Unable to locate method setColumnName");
putString = (*env)->GetMethodID(env, IJniResults, "putString", "(Ljava/lang/String;)V");
if (putString==NULL)
return Throw(env, "java/lang/IllegalStateException", "Unable to locate method putString");
putBlob = (*env)->GetMethodID(env, IJniResults, "putBlob", "([B)V");
if (putBlob==NULL)
return Throw(env, "java/lang/IllegalStateException", "Unable to locate method putBlob");
putLong = (*env)->GetMethodID(env, IJniResults, "putLong", "(J)V");
if (putLong==NULL)
return Throw(env, "java/lang/IllegalStateException", "Unable to locate method putLong");
putDouble = (*env)->GetMethodID(env, IJniResults, "putDouble", "(D)V");
if (putDouble==NULL)
return Throw(env, "java/lang/IllegalStateException", "Unable to locate method putDouble");
totalRowCount = (*env)->GetMethodID(env, IJniResults, "totalRowCount", "(I)V");
if (totalRowCount==NULL)
return Throw(env, "java/lang/IllegalStateException", "Unable to locate method totalRowCount");
return 0;
}
/* JNI entry point to command line. See org.servalproject.servald.ServalD class for the Java side.
JNI method descriptor: "(Ljava/util/List;[Ljava/lang/String;)I"
*/
JNIEXPORT jint JNICALL Java_org_servalproject_servaldna_ServalDCommand_rawCommand(JNIEnv *env, jobject UNUSED(this), jobject outv, jobjectArray args)
{
struct cli_context context;
bzero(&context, sizeof(context));
int r;
// find jni results methods
if ((r=initJniTypes(env))!=0)
return r;
unsigned char status = 0; // to match what the shell gets: 0..255
// Construct argv, argc from this method's arguments.
jsize len = (*env)->GetArrayLength(env, args);
const char **argv = alloca(sizeof(char*) * (len + 1));
if (argv == NULL)
return Throw(env, "java/lang/OutOfMemoryError", "alloca() returned NULL");
bzero(argv, sizeof(char*) * (len + 1));
jsize i;
int argc = len;
// From now on, in case of an exception we have to free some resources before
// returning.
static const char *EMPTY="";
for (i = 0; !context.jni_exception && i < len; ++i) {
const jstring arg = (jstring)(*env)->GetObjectArrayElement(env, args, i);
if ((*env)->ExceptionCheck(env)){
context.jni_exception = 1;
}else if (arg == NULL) {
argv[i] = EMPTY;
} else {
argv[i] = (*env)->GetStringUTFChars(env, arg, NULL);
if (argv[i] == NULL)
context.jni_exception = 1;
}
}
if (!context.jni_exception) {
// Set up the output buffer.
context.jniResults = outv;
context.outv_current = context.outv_buffer;
// Execute the command.
context.jni_env = env;
status = parseCommandLine(&context, NULL, argc, argv);
}
// free any temporary output buffer
if (context.outv_buffer)
free(context.outv_buffer);
// Release argv Java string buffers.
for (i = 0; i < len; ++i) {
if (argv[i] && argv[i]!=EMPTY) {
const jstring arg = (jstring)(*env)->GetObjectArrayElement(env, args, i);
if (arg)
(*env)->ReleaseStringUTFChars(env, arg, argv[i]);
}
}
// Deal with Java exceptions: NewStringUTF out of memory in outv_end_field().
if (context.jni_exception || (context.outv_current != context.outv_buffer && outv_end_field(&context) == -1))
return -1;
return (jint) status;
}
#endif /* HAVE_JNI_H */
/* The argc and argv arguments must be passed verbatim from main(argc, argv), so argv[0] is path to
executable.
*/
int parseCommandLine(struct cli_context *context, const char *argv0, int argc, const char *const *args)
{
fd_clearstats();
IN();
struct cli_parsed parsed;
int result = cli_parse(argc, args, __start_commands, __stop_commands, &parsed);
switch (result) {
case 0:
// Do not run the command if the configuration does not load ok.
if (((parsed.commands[parsed.cmdi].flags & CLIFLAG_PERMISSIVE_CONFIG) ? cf_reload_permissive() : cf_reload()) != -1)
result = cli_invoke(&parsed, context);
else {
strbuf b = strbuf_alloca(160);
strbuf_append_argv(b, argc, args);
result = WHYF("configuration defective, not running command: %s", strbuf_str(b));
}
break;
case 1:
case 2:
// Load configuration so that log messages can get out.
cf_reload_permissive();
NOWHENCE(HINTF("Run \"%s help\" for more information.", argv0 ? argv0 : "servald"));
result =-1;
break;
default:
// Load configuration so that log error messages can get out.
cf_reload_permissive();
break;
}
cli_cleanup();
OUT();
if (config.debug.timing)
fd_showstats();
return result;
}
/* Write a buffer of data to output. If in a JNI call, then this appends the data to the
current output field, including any embedded nul characters. Returns a non-negative integer on
success, EOF on error.
*/
int cli_write(struct cli_context *UNUSED(context), const unsigned char *buf, size_t len)
{
#ifdef HAVE_JNI_H
if (context && context->jni_env) {
size_t avail = context->outv_limit - context->outv_current;
if (avail < len) {
memcpy(context->outv_current, buf, avail);
context->outv_current = context->outv_limit;
if (outv_growbuf(context, len) == -1)
return EOF;
len -= avail;
buf += avail;
}
memcpy(context->outv_current, buf, len);
context->outv_current += len;
return 0;
}
#endif
return fwrite(buf, len, 1, stdout);
}
/* Write a null-terminated string to output. If in a JNI call, then this appends the string to the
current output field. The terminating null is not included. Returns a non-negative integer on
success, EOF on error.
*/
int cli_puts(struct cli_context *UNUSED(context), const char *str)
{
#ifdef HAVE_JNI_H
if (context && context->jni_env)
return cli_write(context, (const unsigned char *) str, strlen(str));
else
#endif
return fputs(str, stdout);
}
/* Write a formatted string to output. If in a JNI call, then this appends the string to the
current output field, excluding the terminating null.
*/
void cli_printf(struct cli_context *UNUSED(context), const char *fmt, ...)
{
va_list ap;
#ifdef HAVE_JNI_H
if (context && context->jni_env) {
assert(context->outv_current <= context->outv_limit);
size_t avail = context->outv_limit - context->outv_current;
va_start(ap, fmt);
int count = vsnprintf(context->outv_current, avail, fmt, ap);
va_end(ap);
if (count < 0) {
WHYF("vsnprintf(%p,%zu,%s,...) failed", context->outv_current, avail, alloca_str_toprint(fmt));
return;
} else if ((size_t)count < avail) {
context->outv_current += count;
return;
}
if (outv_growbuf(context, count) == -1)
return;
avail = context->outv_limit - context->outv_current;
va_start(ap, fmt);
count = vsprintf(context->outv_current, fmt, ap);
va_end(ap);
if (count < 0) {
WHYF("vsprintf(%p,%s,...) failed", context->outv_current, alloca_str_toprint(fmt));
return;
}
assert((size_t)count < avail);
context->outv_current += (size_t)count;
} else
#endif
{
va_start(ap, fmt);
if (vfprintf(stdout, fmt, ap) < 0)
WHYF("vfprintf(stdout,%s,...) failed", alloca_str_toprint(fmt));
va_end(ap);
}
}
void cli_columns(struct cli_context *context, int columns, const char *names[])
{
#ifdef HAVE_JNI_H
if (context && context->jni_env) {
if (context->jni_exception)
return;
(*context->jni_env)->CallVoidMethod(context->jni_env, context->jniResults, startResultSet, columns);
if ((*context->jni_env)->ExceptionCheck(context->jni_env)) {
context->jni_exception = 1;
WHY("Exception thrown from CallVoidMethod(startResultSet)");
return;
}
int i;
for (i=0;i<columns;i++){
jstring str = (jstring)(*context->jni_env)->NewStringUTF(context->jni_env, names[i]);
if (str == NULL) {
context->jni_exception = 1;
WHY("Exception thrown from NewStringUTF()");
return;
}
(*context->jni_env)->CallVoidMethod(context->jni_env, context->jniResults, setColumnName, i, str);
(*context->jni_env)->DeleteLocalRef(context->jni_env, str);
if ((*context->jni_env)->ExceptionCheck(context->jni_env)) {
context->jni_exception = 1;
WHY("Exception thrown from CallVoidMethod(setColumnName)");
return;
}
}
return;
}
#endif
cli_printf(context, "%d", columns);
cli_delim(context, "\n");
int i;
for (i=0;i<columns;i++){
cli_puts(context, names[i]);
if (i+1==columns)
cli_delim(context, "\n");
else
cli_delim(context, ":");
}
}
void cli_field_name(struct cli_context *context, const char *name, const char *delim)
{
#ifdef HAVE_JNI_H
if (context && context->jni_env) {
if (context->jni_exception)
return;
jstring str = (jstring)(*context->jni_env)->NewStringUTF(context->jni_env, name);
if (str == NULL) {
context->jni_exception = 1;
WHY("Exception thrown from NewStringUTF()");
return;
}
(*context->jni_env)->CallVoidMethod(context->jni_env, context->jniResults, setColumnName, -1, str);
(*context->jni_env)->DeleteLocalRef(context->jni_env, str);
if ((*context->jni_env)->ExceptionCheck(context->jni_env)) {
context->jni_exception = 1;
WHY("Exception thrown from CallVoidMethod(setColumnName)");
return;
}
return;
}
#endif
cli_puts(context, name);
cli_delim(context, delim);
}
void cli_put_long(struct cli_context *context, int64_t value, const char *delim){
#ifdef HAVE_JNI_H
if (context && context->jni_env) {
if (context->jni_exception)
return;
(*context->jni_env)->CallVoidMethod(context->jni_env, context->jniResults, putLong, value);
if ((*context->jni_env)->ExceptionCheck(context->jni_env)) {
context->jni_exception = 1;
WHY("Exception thrown from CallVoidMethod(putLong)");
}
return;
}
#endif
cli_printf(context, "%" PRId64, value);
cli_delim(context, delim);
}
void cli_put_string(struct cli_context *context, const char *value, const char *delim){
#ifdef HAVE_JNI_H
if (context && context->jni_env) {
if (context->jni_exception)
return;
jstring str = NULL;
if (value){
str = (jstring)(*context->jni_env)->NewStringUTF(context->jni_env, value);
if (str == NULL) {
context->jni_exception = 1;
WHY("Exception thrown from NewStringUTF()");
return;
}
}
(*context->jni_env)->CallVoidMethod(context->jni_env, context->jniResults, putString, str);
(*context->jni_env)->DeleteLocalRef(context->jni_env, str);
if ((*context->jni_env)->ExceptionCheck(context->jni_env)) {
context->jni_exception = 1;
WHY("Exception thrown from CallVoidMethod(putLong)");
}
return;
}
#endif
if (value)
cli_puts(context, value);
cli_delim(context, delim);
}
void cli_put_hexvalue(struct cli_context *context, const unsigned char *value, int length, const char *delim){
#ifdef HAVE_JNI_H
if (context && context->jni_env) {
put_blob(context, (jbyte*)value, length);
return;
}
#endif
if (value)
cli_puts(context, alloca_tohex(value, length));
cli_delim(context, delim);
}
void cli_row_count(struct cli_context *UNUSED(context), int UNUSED(rows)){
#ifdef HAVE_JNI_H
if (context && context->jni_env) {
if (context->jni_exception)
return;
(*context->jni_env)->CallVoidMethod(context->jni_env, context->jniResults, totalRowCount, rows);
if ((*context->jni_env)->ExceptionCheck(context->jni_env)) {
context->jni_exception = 1;
WHY("Exception thrown from CallVoidMethod()");
}
return;
}
#endif
}
/* Delimit the current output field. This closes the current field, so that the next cli_ output
function will start appending to a new field. Returns 0 on success, -1 on error. If not in a
JNI call, then this simply writes a newline to standard output (or the value of the
SERVALD_OUTPUT_DELIMITER env var if set).
*/
int cli_delim(struct cli_context *UNUSED(context), const char *opt)
{
#ifdef HAVE_JNI_H
if (context && context->jni_env)
return outv_end_field(context);
#endif
const char *delim = getenv("SERVALD_OUTPUT_DELIMITER");
if (delim == NULL)
delim = opt ? opt : "\n";
fputs(delim, stdout);
return 0;
}
/* Flush the output fields if they are being written to standard output.
*/
void cli_flush(struct cli_context *UNUSED(context))
{
#ifdef HAVE_JNI_H
if (context && context->jni_env)
return;
#endif
fflush(stdout);
}
DEFINE_CMD(app_echo,CLIFLAG_PERMISSIVE_CONFIG,
"Output the supplied string.",
"echo","[-e]","[--]","...");
static int app_echo(const struct cli_parsed *parsed, struct cli_context *context)
{
if (config.debug.verbose)
DEBUG_cli_parsed(parsed);
int escapes = !cli_arg(parsed, "-e", NULL, NULL, NULL);
unsigned i;
for (i = parsed->varargi; i < parsed->argc; ++i) {
const char *arg = parsed->args[i];
if (config.debug.verbose)
DEBUGF("echo:argv[%d]=\"%s\"", i, arg);
if (escapes) {
unsigned char buf[strlen(arg)];
size_t len = strn_fromprint(buf, sizeof buf, arg, 0, '\0', NULL);
cli_write(context, buf, len);
} else
cli_puts(context, arg);
cli_delim(context, NULL);
}
return 0;
}
DEFINE_CMD(app_log,CLIFLAG_PERMISSIVE_CONFIG,
"Log the supplied message at given level.",
"log","error|warn|hint|info|debug","<message>");
static int app_log(const struct cli_parsed *parsed, struct cli_context *UNUSED(context))
{
if (config.debug.verbose)
DEBUG_cli_parsed(parsed);
assert(parsed->argc == 3);
const char *lvl = parsed->args[1];
const char *msg = parsed->args[2];
int level = string_to_log_level(lvl);
if (level == LOG_LEVEL_INVALID)
return WHYF("invalid log level: %s", lvl);
logMessage(level, __NOWHERE__, "%s", msg);
return 0;
}