mirror of
https://github.com/servalproject/serval-dna.git
synced 2025-01-29 15:43:56 +00:00
Gracefully disable rhizome if the database cannot be opened
- close database after every command line operation - don't cache rhizome enabled configuration - don't send advertisements unless the database is open and the web server is running - don't provess advertisements unless the database is open
This commit is contained in:
parent
f7ee962df3
commit
de95bb3971
@ -188,6 +188,7 @@ int parseCommandLine(const char *argv0, int argc, const char *const *args)
|
||||
int result = cli_execute(argv0, argc, args, command_line_options, NULL);
|
||||
/* clean up after ourselves */
|
||||
overlay_mdp_client_done();
|
||||
rhizome_close_db();
|
||||
OUT();
|
||||
|
||||
if (debug&DEBUG_TIMING)
|
||||
@ -564,8 +565,6 @@ int app_server_start(int argc, const char *const *argv, struct command_line_opti
|
||||
instance directory when it starts up. */
|
||||
if (server_remove_stopfile() == -1)
|
||||
return -1;
|
||||
if (rhizome_enabled() && rhizome_opendb() == -1)
|
||||
return -1;
|
||||
overlayMode = 1;
|
||||
if (foregroundP)
|
||||
return server(NULL);
|
||||
@ -589,6 +588,9 @@ int app_server_start(int argc, const char *const *argv, struct command_line_opti
|
||||
streams, and start a new process session so that if we are being started by an adb
|
||||
shell session, then we don't receive a SIGHUP when the adb shell process ends. */
|
||||
close_logging();
|
||||
|
||||
//TODO close config
|
||||
|
||||
int fd;
|
||||
if ((fd = open("/dev/null", O_RDWR, 0)) == -1)
|
||||
_exit(WHY_perror("open"));
|
||||
|
18
overlay.c
18
overlay.c
@ -147,14 +147,16 @@ schedule(&_sched_##X); }
|
||||
|
||||
/* Get rhizome server started BEFORE populating fd list so that
|
||||
the server's listen socket is in the list for poll() */
|
||||
if (rhizome_enabled())
|
||||
/* Rhizome http server needs to know which callback to attach
|
||||
to client sockets, so provide it here, along with the name to
|
||||
appear in time accounting statistics. */
|
||||
rhizome_http_server_start(rhizome_server_parse_http_request,
|
||||
"rhizome_server_parse_http_request",
|
||||
RHIZOME_HTTP_PORT,RHIZOME_HTTP_PORT_MAX);
|
||||
|
||||
if (rhizome_enabled()) {
|
||||
if (!rhizome_opendb()){
|
||||
/* Rhizome http server needs to know which callback to attach
|
||||
to client sockets, so provide it here, along with the name to
|
||||
appear in time accounting statistics. */
|
||||
rhizome_http_server_start(rhizome_server_parse_http_request,
|
||||
"rhizome_server_parse_http_request",
|
||||
RHIZOME_HTTP_PORT,RHIZOME_HTTP_PORT_MAX);
|
||||
}
|
||||
}
|
||||
// start the dna helper if configured
|
||||
dna_helper_start();
|
||||
|
||||
|
@ -1226,9 +1226,7 @@ overlay_fill_send_packet(struct outgoing_packet *packet, time_ms_t now) {
|
||||
// send the packet
|
||||
if (packet->buffer->position>=HEADERFIELDS_LEN){
|
||||
// stuff rhizome announcements at the last moment
|
||||
if (rhizome_enabled() && rhizome_http_server_running()){
|
||||
overlay_rhizome_add_advertisements(packet->i,packet->buffer);
|
||||
}
|
||||
overlay_rhizome_add_advertisements(packet->i,packet->buffer);
|
||||
|
||||
if (debug&DEBUG_PACKETCONSTRUCTION)
|
||||
dump("assembled packet",&packet->buffer->bytes[0],packet->buffer->position);
|
||||
|
21
rhizome.c
21
rhizome.c
@ -21,31 +21,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
#include "rhizome.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
static int _rhizome_enabled = -1;
|
||||
static int _rhizome_fetch_delay_ms = -1;
|
||||
|
||||
/* Configure rhizome.
|
||||
@author Andrew Bettison <andrew@servalproject.com>
|
||||
*/
|
||||
int rhizome_configure()
|
||||
{
|
||||
_rhizome_enabled = confValueGetBoolean("rhizome.enable", 1);
|
||||
_rhizome_fetch_delay_ms = (int) confValueGetInt64Range("rhizome.fetch_delay_ms", 50, 1, 3600000);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int rhizome_enabled()
|
||||
{
|
||||
if (_rhizome_enabled < 0)
|
||||
rhizome_configure();
|
||||
return _rhizome_enabled;
|
||||
return confValueGetBoolean("rhizome.enable", 1);;
|
||||
}
|
||||
|
||||
int rhizome_fetch_delay_ms()
|
||||
{
|
||||
if (_rhizome_fetch_delay_ms < 1)
|
||||
rhizome_configure();
|
||||
return _rhizome_fetch_delay_ms;
|
||||
return confValueGetInt64Range("rhizome.fetch_delay_ms", 50, 1, 3600000);
|
||||
}
|
||||
|
||||
/* Import a bundle from a pair of files, one containing the manifest and the optional other
|
||||
|
@ -174,6 +174,7 @@ int create_rhizome_import_dir();
|
||||
extern sqlite3 *rhizome_db;
|
||||
|
||||
int rhizome_opendb();
|
||||
int rhizome_close_db();
|
||||
int rhizome_manifest_createid(rhizome_manifest *m);
|
||||
|
||||
int rhizome_strn_is_manifest_id(const char *text);
|
||||
|
@ -226,6 +226,17 @@ int rhizome_opendb()
|
||||
RETURN(0);
|
||||
}
|
||||
|
||||
int rhizome_close_db(){
|
||||
if (rhizome_db){
|
||||
// TODO if we prepare statements globally we need to make sure any prepared statements are closed first.
|
||||
int r = sqlite3_close(rhizome_db);
|
||||
if (r != SQLITE_OK)
|
||||
return WHYF("Failed to close sqlite database, %s",sqlite3_errmsg(rhizome_db));
|
||||
}
|
||||
rhizome_db=NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* SQL query retry logic.
|
||||
|
||||
The common retry-on-busy logic is factored into this function. This logic encapsulates the
|
||||
|
@ -153,6 +153,9 @@ int overlay_rhizome_add_advertisements(int interface_number, struct overlay_buff
|
||||
so that the CPU delays caused by Rhizome verifying signatures isn't a problem.
|
||||
We will still want to limit network usage during calls, however.
|
||||
*/
|
||||
if (!rhizome_http_server_running() || !rhizome_db)
|
||||
RETURN(0);
|
||||
|
||||
time_ms_t now = gettime_ms();
|
||||
if (now<rhizome_voice_timeout) voice_mode=1;
|
||||
if (voice_mode) if (random()&3) { RETURN(0); }
|
||||
@ -166,8 +169,6 @@ int overlay_rhizome_add_advertisements(int interface_number, struct overlay_buff
|
||||
|
||||
if (slots<1) { RETURN(WHY("No room for node advertisements")); }
|
||||
|
||||
if (!rhizome_db) { RETURN(WHY("Rhizome not enabled")); }
|
||||
|
||||
if (ob_append_byte(e,OF_TYPE_RHIZOME_ADVERT))
|
||||
RETURN(WHY("could not add rhizome bundle advertisement header"));
|
||||
ob_append_byte(e, 1); /* TTL (1 byte) */
|
||||
@ -341,6 +342,9 @@ int overlay_rhizome_saw_advertisements(int i, struct overlay_frame *f, long long
|
||||
{
|
||||
IN();
|
||||
if (!f) { RETURN(-1); }
|
||||
|
||||
if (!rhizome_db) { RETURN(0); }
|
||||
|
||||
int ad_frame_type=ob_get(f->payload);
|
||||
struct sockaddr_in httpaddr = *(struct sockaddr_in *)f->recvaddr;
|
||||
httpaddr.sin_port = htons(RHIZOME_HTTP_PORT);
|
||||
|
Loading…
x
Reference in New Issue
Block a user