Make commandline.c error messages appear on Android log

This commit is contained in:
Andrew Bettison 2012-05-03 14:38:30 +09:30
parent 490d9ef472
commit eab6dafcc7
3 changed files with 60 additions and 65 deletions

View File

@ -38,22 +38,13 @@ static int servalNodeRunning(int *pid)
const char *instancepath = serval_instancepath(); const char *instancepath = serval_instancepath();
struct stat st; struct stat st;
int r=stat(instancepath,&st); int r=stat(instancepath,&st);
if (r) { if (r)
fprintf(stderr, return setReason("Instance path '%s' non existant or not accessable: %s [errno=%d]"
"ERROR: Instance path '%s' non existant or not accessable.\n" " (Set SERVALINSTANCE_PATH to specify an alternate location)",
" Operating system says: %s (errno=%d)\n", instancepath, strerror(errno), errno
instancepath,strerror(errno),errno); );
fprintf(stderr, if ((st.st_mode&S_IFMT)!=S_IFDIR)
" (Set SERVALINSTANCE_PATH to specify an alternate location.)\n"); return setReason("Instance path '%s' is not a directory", instancepath);
return -1;
}
if ((st.st_mode&S_IFMT)!=S_IFDIR) {
fprintf(stderr,
"ERROR: Instance path must be a valid directory.\n"
" '%s' is not a directory.\n",instancepath);
*pid=-1;
return -1;
}
int running=0; int running=0;
char filename[1024]; char filename[1024];
@ -218,6 +209,25 @@ JNIEXPORT jint JNICALL Java_org_servalproject_servald_ServalD_rawCommand(JNIEnv
#endif /* HAVE_JNI_H */ #endif /* HAVE_JNI_H */
static void complainCommandLine(const char *prefix, int argc, const char *const *argv)
{
char buf[1024];
char *b = buf;
int i;
char *e = buf + sizeof(buf) - 4;
for (i = 0; b < e && i != argc; ++i) {
if (i)
*b++ = ' ';
if (b < e)
b += snprintf(b, e - b, "%s", argv[i]);
}
if (b < e)
*b = '\0';
else
strcpy(e, "...");
setReason("%s%s", prefix, buf);
}
/* args[] excludes command name (unless hardlinks are used to use first words /* args[] excludes command name (unless hardlinks are used to use first words
of command sequences as alternate names of the command. */ of command sequences as alternate names of the command. */
int parseCommandLine(int argc, const char *const *args) int parseCommandLine(int argc, const char *const *args)
@ -234,19 +244,19 @@ int parseCommandLine(int argc, const char *const *args)
for (j = 0; (word = command_line_options[i].words[j]); ++j) { for (j = 0; (word = command_line_options[i].words[j]); ++j) {
int wordlen = strlen(word); int wordlen = strlen(word);
if (optional < 0) { if (optional < 0) {
fprintf(stderr,"Internal error: command_line_options[%d].word[%d]=\"%s\" not allowed after \"...\"\n", i, j, word); WHYF("Internal error: command_line_options[%d].word[%d]=\"%s\" not allowed after \"...\"", i, j, word);
break; break;
} }
else if (!( (wordlen > 2 && word[0] == '<' && word[wordlen-1] == '>') else if (!( (wordlen > 2 && word[0] == '<' && word[wordlen-1] == '>')
|| (wordlen > 4 && word[0] == '[' && word[1] == '<' && word[wordlen-2] == '>' && word[wordlen-1] == ']') || (wordlen > 4 && word[0] == '[' && word[1] == '<' && word[wordlen-2] == '>' && word[wordlen-1] == ']')
|| (wordlen > 0) || (wordlen > 0)
)) { )) {
fprintf(stderr,"Internal error: command_line_options[%d].word[%d]=\"%s\" is malformed\n", i, j, word); WHYF("Internal error: command_line_options[%d].word[%d]=\"%s\" is malformed", i, j, word);
break; break;
} else if (word[0] == '<') { } else if (word[0] == '<') {
++mandatory; ++mandatory;
if (optional) { if (optional) {
fprintf(stderr,"Internal error: command_line_options[%d].word[%d]=\"%s\" should be optional\n", i, j, word); WHYF("Internal error: command_line_options[%d].word[%d]=\"%s\" should be optional", i, j, word);
break; break;
} }
} else if (word[0] == '[') { } else if (word[0] == '[') {
@ -265,14 +275,12 @@ int parseCommandLine(int argc, const char *const *args)
that the call is ambiguous. */ that the call is ambiguous. */
if (cli_call>=0) ambiguous++; if (cli_call>=0) ambiguous++;
if (ambiguous==1) { if (ambiguous==1) {
fprintf(stderr,"Ambiguous command line call:\n "); setReason("Ambiguous command line call:");
for(j=0;j<argc;j++) fprintf(stderr," %s",args[j]); complainCommandLine(" ", argc, args);
fprintf(stderr,"\nMatches the following known command line calls:\n"); setReason("Matches the following known command line calls:");
} }
if (ambiguous) { if (ambiguous) {
fprintf(stderr," "); complainCommandLine(" ", argc, command_line_options[i].words);
for(j=0;j<argc;j++) fprintf(stderr," %s",command_line_options[i].words[j]);
fprintf(stderr,"\n");
} }
cli_call=i; cli_call=i;
} }
@ -282,8 +290,8 @@ int parseCommandLine(int argc, const char *const *args)
if (ambiguous) return -1; if (ambiguous) return -1;
/* Complain if we found no matching calls */ /* Complain if we found no matching calls */
if (cli_call<0) { if (cli_call<0) {
fprintf(stderr,"Unknown command line call:\n "); setReason("Unknown command line call:");
int j; for(j=0;j<argc;j++) fprintf(stderr," %s",args[j]); complainCommandLine(" ", argc, args);
return cli_usage(); return cli_usage();
} }
@ -309,10 +317,8 @@ int cli_arg(int argc, const char *const *argv, command_line_option *o, char *arg
|| (wordlen == arglen + 4 && word[0] == '[' && !strncasecmp(&word[2], argname, arglen))) || (wordlen == arglen + 4 && word[0] == '[' && !strncasecmp(&word[2], argname, arglen)))
) { ) {
const char *value = argv[i]; const char *value = argv[i];
if (validator && !(*validator)(value)) { if (validator && !(*validator)(value))
fprintf(stderr, "Invalid argument %d '%s': \"%s\"\n", i, argname, value); return setReason("Invalid argument %d '%s': \"%s\"", i, argname, value);
return -1;
}
*dst = value; *dst = value;
return 0; return 0;
} }
@ -492,7 +498,7 @@ int app_dna_lookup(int argc, const char *const *argv, struct command_line_option
{ {
if (rx.packetTypeAndFlags==MDP_ERROR) if (rx.packetTypeAndFlags==MDP_ERROR)
{ {
fprintf(stderr," Error message: %s\n",mdp.error.message); WHYF(" Error message: %s", mdp.error.message);
} }
else if ((rx.packetTypeAndFlags&MDP_TYPE_MASK)==MDP_TX) { else if ((rx.packetTypeAndFlags&MDP_TYPE_MASK)==MDP_TX) {
/* Display match unless it is a duplicate. /* Display match unless it is a duplicate.
@ -534,12 +540,12 @@ char *confValueGet(char *var,char *defaultValue)
char filename[1024]; char filename[1024];
if (!FORM_SERVAL_INSTANCE_PATH(filename, "serval.conf")) { if (!FORM_SERVAL_INSTANCE_PATH(filename, "serval.conf")) {
fprintf(stderr, "Using default value of %s: %s\n", var, defaultValue); WHYF("Using default value of %s: %s", var, defaultValue);
return defaultValue; return defaultValue;
} }
FILE *f = fopen(filename,"r"); FILE *f = fopen(filename,"r");
if (!f) { if (!f) {
fprintf(stderr, "Cannot open serval.conf. Using default value of %s: %s\n", var, defaultValue); WHYF("Cannot open serval.conf, using default value of %s: %s", var, defaultValue);
return defaultValue; return defaultValue;
} }
@ -585,18 +591,15 @@ int app_server_start(int argc, const char *const *argv, struct command_line_opti
network interfaces that we will take interest in. */ network interfaces that we will take interest in. */
overlay_interface_args(confValueGet("interfaces","")); overlay_interface_args(confValueGet("interfaces",""));
if (strlen(confValueGet("interfaces",""))<1) { if (strlen(confValueGet("interfaces",""))<1) {
fprintf(stderr, WHY("Noone has told me which network interfaces to listen on; "
"WARNING: Noone has told me which network interfaces to listen on.\n" "you should probably put something in the interfaces setting.");
" You should probably put something in the interfaces setting.\n");
} }
int pid=-1; int pid=-1;
int running = servalNodeRunning(&pid); int running = servalNodeRunning(&pid);
if (running<0) return -1; if (running<0) return -1;
if (running>0) { if (running>0)
fprintf(stderr,"ERROR: Serval process already running (pid=%d)\n",pid); return WHYF("Serval process already running (pid=%d)", pid);
return -1;
}
/* Start the Serval process. /* Start the Serval process.
All server settings will be read by the server process from the All server settings will be read by the server process from the
instance directory when it starts up. instance directory when it starts up.
@ -643,7 +646,7 @@ int app_server_stop(int argc, const char *const *argv, struct command_line_optio
fclose(f); fclose(f);
int result=kill(pid,SIGHUP); int result=kill(pid,SIGHUP);
if (!result) { if (!result) {
fprintf(stderr,"Stop request sent to Serval process.\n"); WHY("Stop request sent to Serval process.");
} else { } else {
WHY("Could not send SIGHUP to Serval process."); WHY("Could not send SIGHUP to Serval process.");
switch (errno) { switch (errno) {
@ -667,7 +670,7 @@ int app_server_stop(int argc, const char *const *argv, struct command_line_optio
pid=-1; pid=-1;
int running = servalNodeRunning(&pid); int running = servalNodeRunning(&pid);
if (running<1) { if (running<1) {
fprintf(stderr,"Serval process appears to have stopped.\n"); WHY("Serval process appears to have stopped.");
return 0; return 0;
} }
} }
@ -755,7 +758,7 @@ int app_mdp_ping(int argc, const char *const *argv, struct command_line_option *
long long rx_times[1024]; long long rx_times[1024];
if (broadcast) if (broadcast)
fprintf(stderr,"WARNING: broadcast ping packets will not be encryped.\n"); WHY("WARNING: broadcast ping packets will not be encryped.");
while(1) { while(1) {
/* Now send the ping packets */ /* Now send the ping packets */
mdp.packetTypeAndFlags=MDP_TX; mdp.packetTypeAndFlags=MDP_TX;
@ -773,10 +776,9 @@ int app_mdp_ping(int argc, const char *const *argv, struct command_line_option *
int res=overlay_mdp_send(&mdp,0,0); int res=overlay_mdp_send(&mdp,0,0);
if (res) { if (res) {
fprintf(stderr,"ERROR: Could not dispatch PING frame #%d (error %d)\n", WHYF("ERROR: Could not dispatch PING frame #%d (error %d)", sequence_number - firstSeq, res);
sequence_number-firstSeq,res); if (mdp.packetTypeAndFlags==MDP_ERROR)
if (mdp.packetTypeAndFlags==MDP_ERROR) WHYF(" Error message: %s", mdp.error.message);
fprintf(stderr," Error message: %s\n",mdp.error.message);
} else tx_count++; } else tx_count++;
/* Now look for replies until one second has passed, and print any replies /* Now look for replies until one second has passed, and print any replies
@ -793,8 +795,7 @@ int app_mdp_ping(int argc, const char *const *argv, struct command_line_option *
while (overlay_mdp_recv(&mdp,&ttl)==0) { while (overlay_mdp_recv(&mdp,&ttl)==0) {
switch(mdp.packetTypeAndFlags&MDP_TYPE_MASK) { switch(mdp.packetTypeAndFlags&MDP_TYPE_MASK) {
case MDP_ERROR: case MDP_ERROR:
fprintf(stderr,"mdpping: overlay_mdp_recv: %s (code %d)\n", WHYF("mdpping: overlay_mdp_recv: %s (code %d)", mdp.error.message, mdp.error.error);
mdp.error.message,mdp.error.error);
break; break;
case MDP_TX: case MDP_TX:
{ {
@ -814,8 +815,7 @@ int app_mdp_ping(int argc, const char *const *argv, struct command_line_option *
} }
break; break;
default: default:
fprintf(stderr,"mdpping: overlay_mdp_recv: Unexpected MDP frame type" WHYF("mdpping: overlay_mdp_recv: Unexpected MDP frame type 0x%x", mdp.packetTypeAndFlags);
" 0x%x\n",mdp.packetTypeAndFlags);
break; break;
} }
} }
@ -1114,7 +1114,7 @@ int app_keyring_create(int argc, const char *const *argv, struct command_line_op
const char *pin; const char *pin;
cli_arg(argc, argv, o, "pin,pin ...", &pin, NULL, ""); cli_arg(argc, argv, o, "pin,pin ...", &pin, NULL, "");
keyring_file *k=keyring_open_with_pins(pin); keyring_file *k=keyring_open_with_pins(pin);
if (!k) fprintf(stderr,"keyring create:Failed to create/open keyring file\n"); if (!k) WHY("keyring create: Failed to create/open keyring file");
return 0; return 0;
} }
@ -1156,18 +1156,13 @@ int app_keyring_add(int argc, const char *const *argv, struct command_line_optio
cli_arg(argc, argv, o, "pin", &pin, NULL, ""); cli_arg(argc, argv, o, "pin", &pin, NULL, "");
keyring_file *k=keyring_open_with_pins(""); keyring_file *k=keyring_open_with_pins("");
if (!k) { fprintf(stderr,"keyring add:Failed to create/open keyring file\n"); if (!k) { WHY("keyring add: Failed to create/open keyring file");
return -1; } return -1; }
if (keyring_create_identity(k,k->contexts[0],(char *)pin)==NULL) if (keyring_create_identity(k,k->contexts[0],(char *)pin)==NULL)
{ return setReason("Could not create new identity (keyring_create_identity() failed)");
fprintf(stderr,"Could not create new identity (keyring_create_identity() failed)\n"); if (keyring_commit(k))
return -1; return setReason("Could not write new identity (keyring_commit() failed)");
}
if (keyring_commit(k)) {
fprintf(stderr,"Could not write new identity (keyring_commit() failed)\n");
return -1;
}
keyring_free(k); keyring_free(k);
return 0; return 0;
} }

View File

@ -1356,11 +1356,11 @@ int vomp_send_status(vomp_call_state *call,int flags,overlay_mdp_frame *arg);
typedef struct command_line_option { typedef struct command_line_option {
int (*function)(int argc, const char *const *argv, struct command_line_option *o); int (*function)(int argc, const char *const *argv, struct command_line_option *o);
char *words[32]; // 32 words should be plenty! const char *words[32]; // 32 words should be plenty!
unsigned long long flags; unsigned long long flags;
#define CLIFLAG_NONOVERLAY (1<<0) /* Uses a legacy IPv4 DNA call instead of overlay mnetwork */ #define CLIFLAG_NONOVERLAY (1<<0) /* Uses a legacy IPv4 DNA call instead of overlay mnetwork */
#define CLIFLAG_STANDALONE (1<<1) /* Cannot be issued to a running instance */ #define CLIFLAG_STANDALONE (1<<1) /* Cannot be issued to a running instance */
char *description; // describe this invocation const char *description; // describe this invocation
} command_line_option; } command_line_option;
extern command_line_option command_line_options[]; extern command_line_option command_line_options[];

View File

@ -23,7 +23,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
int main(int argc,char **argv) int main(int argc,char **argv)
{ {
void *h = dlopen("/data/data/org.servalproject/lib/libserval.so",RTLD_LAZY); void *h = dlopen("/data/data/org.servalproject/lib/libserval.so",RTLD_LAZY);
int (*servalmain)(int,char **) = dlsym(h,"parseCommandLine"); int (*servalmain)(int,const char *const*) = dlsym(h,"parseCommandLine");
if (!servalmain) return fprintf(stderr,"Could not load libserval.so\n"); if (!servalmain) return fprintf(stderr,"Could not load libserval.so\n");
return (*servalmain)(argc - 1, (const char*const*)&argv[1]); return (*servalmain)(argc - 1, (const char*const*)&argv[1]);