Initialise client API sockets before PID file

This commit is contained in:
Jeremy Lakeman 2014-02-24 10:37:14 +10:30
parent dfda3f7a74
commit ae67dd9c56
5 changed files with 34 additions and 62 deletions

View File

@ -828,42 +828,6 @@ int app_server_start(const struct cli_parsed *parsed, struct cli_context *contex
/* Process optional arguments */ /* Process optional arguments */
int pid=-1; int pid=-1;
int cpid=-1; int cpid=-1;
#if 0
// It would have been nice if whoever disabled this code had left a comment as to why they didn't
// simply delete it altogether. In any event, this logic is largely redundant because the Android
// Batphone app automatically calls "servald stop" then "servald start" (via JNI) whenever its
// monitor interface socket is broken.
// -- Andrew Bettison <andrew@servalproject.com>
int status=server_probe(&pid);
switch(status) {
case SERVER_NOTRESPONDING:
/* server is not responding, and we have been asked to start,
so try to kill it?
*/
WHYF("Serval process already running (pid=%d), but no responding.", pid);
if (pid>-1) {
kill(pid,SIGHUP);
sleep_ms(1000);
status=server_probe(&pid);
if (status!=SERVER_NOTRUNNING) {
WHY("Tried to stop stuck servald process, but attempt failed.");
RETURN(-1);
}
WHY("Killed stuck servald process, so will try to start");
pid=-1;
}
break;
case SERVER_NOTRUNNING:
/* all is well */
break;
case SERVER_RUNNING:
/* instance running */
break;
default:
/* no idea what is going on, so try to start anyway */
break;
}
#endif
const char *execpath; const char *execpath;
if (cli_arg(parsed, "exec", &execpath, cli_absolute_path, NULL) == -1) if (cli_arg(parsed, "exec", &execpath, cli_absolute_path, NULL) == -1)
RETURN(-1); RETURN(-1);

View File

@ -84,10 +84,21 @@ int overlayServerMode(const struct cli_parsed *parsed)
{ {
IN(); IN();
/* In overlay mode we need to listen to all of our sockets, and also to /* Setup up client API sockets before writing our PID file
send periodic traffic. This means we need to */ We want clients to be able to connect to our sockets as soon
INFO("Running in overlay mode."); as servald start has returned. But we don't want servald start
to take very long.
Try to perform only minimal CPU or IO processing here.
*/
overlay_mdp_setup_sockets();
monitor_setup_sockets();
// start the HTTP server if enabled
httpd_server_start(HTTPD_PORT, HTTPD_PORT_MAX);
/* record PID file so that servald start can return */
if (server_write_pid())
RETURN(-1);
/* Get keyring available for use. /* Get keyring available for use.
Required for MDP, and very soon as a complete replacement for the Required for MDP, and very soon as a complete replacement for the
HLR for DNA lookups, even in non-overlay mode. */ HLR for DNA lookups, even in non-overlay mode. */
@ -122,10 +133,6 @@ schedule(&_sched_##X); }
/* Periodically reload configuration */ /* Periodically reload configuration */
SCHEDULE(server_config_reload, SERVER_CONFIG_RELOAD_INTERVAL_MS, SERVER_CONFIG_RELOAD_INTERVAL_MS + 100); SCHEDULE(server_config_reload, SERVER_CONFIG_RELOAD_INTERVAL_MS, SERVER_CONFIG_RELOAD_INTERVAL_MS + 100);
/* Setup up MDP & monitor interface unix domain sockets */
overlay_mdp_setup_sockets();
monitor_setup_sockets();
overlay_mdp_bind_internal_services(); overlay_mdp_bind_internal_services();
olsr_init_socket(); olsr_init_socket();
@ -138,9 +145,6 @@ schedule(&_sched_##X); }
rhizome_cleanup(NULL); rhizome_cleanup(NULL);
} }
// start the HTTP server if enabled
httpd_server_start(HTTPD_PORT, HTTPD_PORT_MAX);
// start the dna helper if configured // start the dna helper if configured
dna_helper_start(); dna_helper_start();
@ -159,7 +163,7 @@ schedule(&_sched_##X); }
#undef SCHEDULE #undef SCHEDULE
// log message used by tests to wait for the server to start // log message used by tests to wait for the server to start
INFO("Server started, entering main loop"); INFO("Server initialised, entering main loop");
/* Check for activitiy and respond to it */ /* Check for activitiy and respond to it */
while(fd_poll()); while(fd_poll());

View File

@ -305,6 +305,7 @@ struct slip_decode_state{
int server_pid(); int server_pid();
void server_save_argv(int argc, const char *const *argv); void server_save_argv(int argc, const char *const *argv);
int server(const struct cli_parsed *parsed); int server(const struct cli_parsed *parsed);
int server_write_pid();
int server_create_stopfile(); int server_create_stopfile();
int server_remove_stopfile(); int server_remove_stopfile();
int server_check_stopfile(); int server_check_stopfile();

View File

@ -112,25 +112,29 @@ int server(const struct cli_parsed *parsed)
sigaction(SIGHUP, &sig, NULL); sigaction(SIGHUP, &sig, NULL);
sigaction(SIGINT, &sig, NULL); sigaction(SIGINT, &sig, NULL);
/* Record PID to advertise that the server is now running */
char filename[1024];
if (!FORM_SERVAL_INSTANCE_PATH(filename, PIDFILE_NAME))
RETURN(-1);
FILE *f=fopen(filename,"w");
if (!f) {
WHY_perror("fopen");
RETURN(WHYF("Could not write to PID file %s", filename));
}
server_getpid = getpid();
fprintf(f,"%d\n", server_getpid);
fclose(f);
overlayServerMode(parsed); overlayServerMode(parsed);
RETURN(0); RETURN(0);
OUT(); OUT();
} }
int server_write_pid()
{
/* Record PID to advertise that the server is now running */
char filename[1024];
if (!FORM_SERVAL_INSTANCE_PATH(filename, PIDFILE_NAME))
return -1;
FILE *f=fopen(filename,"w");
if (!f) {
WHY_perror("fopen");
return WHYF("Could not write to PID file %s", filename);
}
server_getpid = getpid();
fprintf(f,"%d\n", server_getpid);
fclose(f);
return 0;
}
/* Called periodically by the server process in its main loop. /* Called periodically by the server process in its main loop.
*/ */
void server_config_reload(struct sched_ent *alarm) void server_config_reload(struct sched_ent *alarm)

View File

@ -352,7 +352,6 @@ start_servald_server() {
assert --message="a new servald process is running" --dump-on-fail="$instance_servald_log" [ -n "$new_pids" ] assert --message="a new servald process is running" --dump-on-fail="$instance_servald_log" [ -n "$new_pids" ]
assert --message="servald pidfile process is running" --dump-on-fail="$instance_servald_log" $pidfile_running assert --message="servald pidfile process is running" --dump-on-fail="$instance_servald_log" $pidfile_running
assert --message="servald log file $instance_servald_log is present" [ -r "$instance_servald_log" ] assert --message="servald log file $instance_servald_log is present" [ -r "$instance_servald_log" ]
wait_until grep -q "Server started" "$instance_servald_log"
tfw_log "# Started servald server process $instance_name, pid=$servald_pid" tfw_log "# Started servald server process $instance_name, pid=$servald_pid"
pop_instance pop_instance
} }