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:
Jeremy Lakeman 2012-10-29 11:55:14 +10:30
parent f7ee962df3
commit de95bb3971
7 changed files with 35 additions and 34 deletions

View File

@ -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"));

View File

@ -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();

View File

@ -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);

View File

@ -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

View File

@ -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);

View File

@ -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

View File

@ -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);