2010-07-13 12:15:46 +00:00
|
|
|
/*
|
|
|
|
Serval Distributed Numbering Architecture (DNA)
|
|
|
|
Copyright (C) 2010 Paul Gardner-Stephen
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2014-05-29 07:11:27 +00:00
|
|
|
|
2014-06-11 05:24:31 +00:00
|
|
|
#include <assert.h>
|
2014-02-25 02:12:36 +00:00
|
|
|
#include <dirent.h>
|
2012-07-26 09:01:23 +00:00
|
|
|
#include <signal.h>
|
2012-05-08 02:49:52 +00:00
|
|
|
#include <unistd.h>
|
2012-09-27 01:32:58 +00:00
|
|
|
#include <time.h>
|
2014-02-24 06:17:16 +00:00
|
|
|
#include <libgen.h>
|
2012-02-05 05:45:19 +00:00
|
|
|
#include <sys/socket.h>
|
2012-05-08 01:57:07 +00:00
|
|
|
#include <sys/stat.h>
|
2012-02-05 05:45:19 +00:00
|
|
|
|
2012-02-23 02:15:42 +00:00
|
|
|
#include "serval.h"
|
2012-12-04 03:42:28 +00:00
|
|
|
#include "conf.h"
|
2012-07-02 03:22:21 +00:00
|
|
|
#include "strbuf.h"
|
2012-07-24 06:09:36 +00:00
|
|
|
#include "strbuf_helpers.h"
|
2013-12-09 07:15:47 +00:00
|
|
|
#include "overlay_interface.h"
|
2014-04-29 06:01:50 +00:00
|
|
|
#include "overlay_packet.h"
|
2014-05-29 07:11:27 +00:00
|
|
|
#include "server.h"
|
2014-06-06 06:19:16 +00:00
|
|
|
#include "keyring.h"
|
2010-07-13 12:15:46 +00:00
|
|
|
|
2014-03-26 05:05:43 +00:00
|
|
|
#define PROC_SUBDIR "proc"
|
2012-05-08 02:49:52 +00:00
|
|
|
#define PIDFILE_NAME "servald.pid"
|
|
|
|
#define STOPFILE_NAME "servald.stop"
|
|
|
|
|
2014-06-11 05:23:26 +00:00
|
|
|
int serverMode = 0;
|
2014-06-06 06:19:16 +00:00
|
|
|
keyring_file *keyring=NULL;
|
|
|
|
|
2014-03-26 05:05:43 +00:00
|
|
|
static char pidfile_path[256];
|
2012-05-08 01:53:43 +00:00
|
|
|
static int server_getpid = 0;
|
2014-06-11 05:24:31 +00:00
|
|
|
static int server_write_pid();
|
|
|
|
static int server_unlink_pid();
|
|
|
|
static void signal_handler(int signal);
|
|
|
|
static void serverCleanUp();
|
2011-08-08 14:41:46 +00:00
|
|
|
|
2012-05-08 01:53:43 +00:00
|
|
|
/** Return the PID of the currently running server process, return 0 if there is none.
|
|
|
|
*/
|
|
|
|
int server_pid()
|
|
|
|
{
|
2014-03-26 05:05:43 +00:00
|
|
|
char dirname[1024];
|
|
|
|
if (!FORMF_SERVAL_RUN_PATH(dirname, NULL))
|
|
|
|
return -1;
|
2012-05-08 01:53:43 +00:00
|
|
|
struct stat st;
|
2014-03-26 05:05:43 +00:00
|
|
|
if (stat(dirname, &st) == -1)
|
|
|
|
return WHYF_perror("stat(%s)", alloca_str_toprint(dirname));
|
2012-05-08 01:53:43 +00:00
|
|
|
if ((st.st_mode & S_IFMT) != S_IFDIR)
|
2014-03-26 05:05:43 +00:00
|
|
|
return WHYF("Not a directory: %s", dirname);
|
|
|
|
const char *ppath = server_pidfile_path();
|
|
|
|
if (ppath == NULL)
|
2012-05-08 01:53:43 +00:00
|
|
|
return -1;
|
2014-03-26 05:05:43 +00:00
|
|
|
const char *p = strrchr(ppath, '/');
|
|
|
|
assert(p != NULL);
|
|
|
|
FILE *f = fopen(ppath, "r");
|
2013-10-06 18:06:22 +00:00
|
|
|
if (f == NULL) {
|
|
|
|
if (errno != ENOENT)
|
2014-03-26 05:05:43 +00:00
|
|
|
return WHYF_perror("fopen(%s,\"r\")", alloca_str_toprint(ppath));
|
2013-10-06 18:06:22 +00:00
|
|
|
} else {
|
2012-05-08 01:53:43 +00:00
|
|
|
char buf[20];
|
2013-03-18 06:09:08 +00:00
|
|
|
int pid = (fgets(buf, sizeof buf, f) != NULL) ? atoi(buf) : -1;
|
2012-05-08 01:53:43 +00:00
|
|
|
fclose(f);
|
|
|
|
if (pid > 0 && kill(pid, 0) != -1)
|
|
|
|
return pid;
|
2014-03-26 05:05:43 +00:00
|
|
|
INFOF("Unlinking stale pidfile %s", ppath);
|
|
|
|
unlink(ppath);
|
2012-05-08 01:53:43 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2011-08-08 14:41:46 +00:00
|
|
|
|
2014-03-26 05:05:43 +00:00
|
|
|
const char *_server_pidfile_path(struct __sourceloc __whence)
|
|
|
|
{
|
|
|
|
if (!pidfile_path[0]) {
|
|
|
|
if (!FORMF_SERVAL_RUN_PATH(pidfile_path, PIDFILE_NAME))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return pidfile_path;
|
|
|
|
}
|
|
|
|
|
2014-04-07 06:41:11 +00:00
|
|
|
int server()
|
2010-07-13 12:15:46 +00:00
|
|
|
{
|
2013-02-15 20:21:33 +00:00
|
|
|
IN();
|
2014-06-11 04:13:56 +00:00
|
|
|
serverMode = SERVER_RUNNING;
|
2012-07-26 09:01:23 +00:00
|
|
|
|
2014-06-11 05:23:26 +00:00
|
|
|
// Warn, not merely Info, if there is no configured log file.
|
|
|
|
logLevel_NoLogFileConfigured = LOG_LEVEL_WARN;
|
|
|
|
|
2012-07-26 09:01:23 +00:00
|
|
|
/* Catch SIGHUP etc so that we can respond to requests to do things, eg, shut down. */
|
2014-05-08 01:49:43 +00:00
|
|
|
struct sigaction sig;
|
2012-07-26 09:01:23 +00:00
|
|
|
sig.sa_handler = signal_handler;
|
|
|
|
sigemptyset(&sig.sa_mask); // Block the same signals during handler
|
|
|
|
sigaddset(&sig.sa_mask, SIGHUP);
|
|
|
|
sigaddset(&sig.sa_mask, SIGINT);
|
|
|
|
sig.sa_flags = 0;
|
|
|
|
sigaction(SIGHUP, &sig, NULL);
|
|
|
|
sigaction(SIGINT, &sig, NULL);
|
2012-05-09 09:36:44 +00:00
|
|
|
|
2014-06-06 06:19:16 +00:00
|
|
|
/* Setup up client API sockets before writing our PID file
|
|
|
|
We want clients to be able to connect to our sockets as soon
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
if (overlay_mdp_setup_sockets()==-1)
|
|
|
|
RETURN(-1);
|
|
|
|
|
|
|
|
if (monitor_setup_sockets()==-1)
|
|
|
|
RETURN(-1);
|
|
|
|
|
|
|
|
// start the HTTP server if enabled
|
|
|
|
if (httpd_server_start(HTTPD_PORT, HTTPD_PORT_MAX)==-1)
|
|
|
|
RETURN(-1);
|
|
|
|
|
|
|
|
/* For testing, it can be very helpful to delay the start of the server process, for example to
|
|
|
|
* check that the start/stop logic is robust.
|
|
|
|
*/
|
|
|
|
const char *delay = getenv("SERVALD_SERVER_START_DELAY");
|
|
|
|
if (delay){
|
|
|
|
time_ms_t milliseconds = atoi(delay);
|
|
|
|
INFOF("Sleeping for %"PRId64" milliseconds", (int64_t) milliseconds);
|
|
|
|
sleep_ms(milliseconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* record PID file so that servald start can return */
|
|
|
|
if (server_write_pid())
|
|
|
|
RETURN(-1);
|
|
|
|
|
|
|
|
overlay_queue_init();
|
|
|
|
|
|
|
|
time_ms_t now = gettime_ms();
|
|
|
|
|
|
|
|
// Periodically check for server shut down
|
|
|
|
RESCHEDULE(&ALARM_STRUCT(server_shutdown_check), now, now+30000, now);
|
|
|
|
|
|
|
|
overlay_mdp_bind_internal_services();
|
|
|
|
|
|
|
|
olsr_init_socket();
|
|
|
|
|
|
|
|
/* Calculate (and possibly show) CPU usage stats periodically */
|
|
|
|
RESCHEDULE(&ALARM_STRUCT(fd_periodicstats), now+3000, now+30000, TIME_MS_NEVER_WILL);
|
|
|
|
|
|
|
|
cf_on_config_change();
|
|
|
|
|
|
|
|
// log message used by tests to wait for the server to start
|
|
|
|
INFO("Server initialised, entering main loop");
|
2014-06-11 04:13:56 +00:00
|
|
|
|
|
|
|
/* Check for activitiy and respond to it */
|
|
|
|
while((serverMode==SERVER_RUNNING) && fd_poll())
|
2014-06-11 05:24:31 +00:00
|
|
|
;
|
2014-06-06 06:19:16 +00:00
|
|
|
serverCleanUp();
|
2014-06-11 04:13:56 +00:00
|
|
|
|
2014-06-11 05:24:31 +00:00
|
|
|
/* It is safe to unlink the pidfile here without checking whether it actually contains our own
|
|
|
|
* PID, because server_shutdown_check() will have been executed very recently (in fd_poll()), so
|
|
|
|
* if the code reaches here, the check has been done recently.
|
|
|
|
*/
|
|
|
|
server_unlink_pid();
|
2014-06-11 04:13:56 +00:00
|
|
|
serverMode = 0;
|
2014-02-24 00:07:14 +00:00
|
|
|
RETURN(0);
|
|
|
|
OUT();
|
|
|
|
}
|
|
|
|
|
2014-06-11 05:24:31 +00:00
|
|
|
static int server_write_pid()
|
2014-02-24 00:07:14 +00:00
|
|
|
{
|
2012-05-08 01:53:43 +00:00
|
|
|
/* Record PID to advertise that the server is now running */
|
2014-03-26 05:05:43 +00:00
|
|
|
const char *ppath = server_pidfile_path();
|
|
|
|
if (ppath == NULL)
|
2014-02-24 00:07:14 +00:00
|
|
|
return -1;
|
2014-03-26 05:05:43 +00:00
|
|
|
FILE *f = fopen(ppath, "w");
|
|
|
|
if (!f)
|
|
|
|
return WHYF_perror("fopen(%s,\"w\")", alloca_str_toprint(ppath));
|
2012-05-08 01:53:43 +00:00
|
|
|
server_getpid = getpid();
|
|
|
|
fprintf(f,"%d\n", server_getpid);
|
2012-03-04 22:57:31 +00:00
|
|
|
fclose(f);
|
2014-02-24 00:07:14 +00:00
|
|
|
return 0;
|
2011-08-08 14:41:46 +00:00
|
|
|
}
|
|
|
|
|
2014-06-11 05:24:31 +00:00
|
|
|
static int server_unlink_pid()
|
|
|
|
{
|
|
|
|
/* Remove PID file to indicate that the server is no longer running */
|
|
|
|
const char *ppath = server_pidfile_path();
|
|
|
|
if (ppath == NULL)
|
|
|
|
return -1;
|
|
|
|
if (unlink(ppath) == -1)
|
|
|
|
WHYF_perror("unlink(%s)", alloca_str_toprint(ppath));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-26 05:05:43 +00:00
|
|
|
static int get_proc_path(const char *path, char *buf, size_t bufsiz)
|
2014-02-24 06:17:16 +00:00
|
|
|
{
|
2014-03-26 05:05:43 +00:00
|
|
|
if (!formf_serval_run_path(buf, bufsiz, PROC_SUBDIR "/%s", path))
|
|
|
|
return -1;
|
2014-02-25 02:12:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int server_write_proc_state(const char *path, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
char path_buf[400];
|
|
|
|
if (get_proc_path(path, path_buf, sizeof path_buf)==-1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
size_t dirsiz = strlen(path_buf) + 1;
|
2014-02-24 06:17:16 +00:00
|
|
|
char dir_buf[dirsiz];
|
2014-02-25 02:12:36 +00:00
|
|
|
strcpy(dir_buf, path_buf);
|
2014-02-24 06:17:16 +00:00
|
|
|
const char *dir = dirname(dir_buf); // modifies dir_buf[]
|
2014-03-26 05:05:43 +00:00
|
|
|
if (mkdirs_info(dir, 0700) == -1)
|
2014-02-24 06:17:16 +00:00
|
|
|
return WHY_perror("mkdirs()");
|
|
|
|
|
2014-02-25 02:12:36 +00:00
|
|
|
FILE *f = fopen(path_buf, "w");
|
|
|
|
if (!f)
|
|
|
|
return WHY_perror("fopen()");
|
2014-02-24 06:17:16 +00:00
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vfprintf(f, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-25 02:12:36 +00:00
|
|
|
int server_get_proc_state(const char *path, char *buff, size_t buff_len)
|
|
|
|
{
|
|
|
|
char path_buf[400];
|
|
|
|
if (get_proc_path(path, path_buf, sizeof path_buf)==-1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
FILE *f = fopen(path_buf, "r");
|
|
|
|
if (!f)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
int ret=0;
|
|
|
|
|
|
|
|
if (!fgets(buff, buff_len, f))
|
|
|
|
ret = WHY_perror("fgets");
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-12-14 05:57:04 +00:00
|
|
|
/* Called periodically by the server process in its main loop.
|
|
|
|
*/
|
2014-05-29 07:11:27 +00:00
|
|
|
DEFINE_ALARM(server_config_reload);
|
2012-12-14 05:57:04 +00:00
|
|
|
void server_config_reload(struct sched_ent *alarm)
|
|
|
|
{
|
2013-03-28 13:52:08 +00:00
|
|
|
switch (cf_reload_strict()) {
|
2012-12-14 05:57:04 +00:00
|
|
|
case -1:
|
|
|
|
WARN("server continuing with prior config");
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
default:
|
2014-04-29 06:01:50 +00:00
|
|
|
INFO("server config reloaded");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (reload_mdp_packet_rules()) {
|
|
|
|
case -1:
|
|
|
|
WARN("server continuing with prior packet filter rules");
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
INFO("server packet filter rules reloaded");
|
2012-12-14 05:57:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (alarm) {
|
|
|
|
time_ms_t now = gettime_ms();
|
2014-06-04 06:40:36 +00:00
|
|
|
RESCHEDULE(alarm,
|
|
|
|
now+config.server.config_reload_interval_ms,
|
|
|
|
TIME_MS_NEVER_WILL,
|
|
|
|
now+config.server.config_reload_interval_ms+100);
|
2012-12-14 05:57:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-16 08:36:27 +00:00
|
|
|
/* Called periodically by the server process in its main loop.
|
|
|
|
*/
|
2014-05-29 07:11:27 +00:00
|
|
|
DEFINE_ALARM(server_watchdog);
|
2014-04-16 08:36:27 +00:00
|
|
|
void server_watchdog(struct sched_ent *alarm)
|
|
|
|
{
|
|
|
|
if (config.server.watchdog.executable[0]) {
|
|
|
|
const char *argv[2];
|
|
|
|
argv[0] = config.server.watchdog.executable;
|
|
|
|
argv[1] = NULL;
|
|
|
|
strbuf argv_sb = strbuf_append_argv(strbuf_alloca(1024), 1, argv);
|
|
|
|
switch (fork()) {
|
|
|
|
case 0: {
|
|
|
|
/* Child, should fork() again to create orphan process. */
|
|
|
|
pid_t watchdog_pid;
|
|
|
|
switch (watchdog_pid = fork()) {
|
|
|
|
case 0:
|
|
|
|
/* Grandchild, should exec() watchdog. */
|
|
|
|
close_log_file();
|
|
|
|
signal(SIGTERM, SIG_DFL);
|
|
|
|
close(0);
|
|
|
|
close(1);
|
|
|
|
close(2);
|
|
|
|
execv(config.server.watchdog.executable, (char **)argv);
|
|
|
|
// Don't use FATALF_perror() because we want to use _exit(2) not exit(2).
|
|
|
|
LOGF_perror(LOG_LEVEL_FATAL, "execv(%s, [%s])",
|
|
|
|
alloca_str_toprint(config.server.watchdog.executable),
|
|
|
|
strbuf_str(argv_sb)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case -1:
|
|
|
|
/* grandchild fork failed */
|
|
|
|
WHY_perror("fork");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Child, report grandchild's PID. */
|
|
|
|
if (config.debug.watchdog)
|
|
|
|
LOGF(LOG_LEVEL_DEBUG, "STARTED WATCHDOG pid=%u executable=%s argv=[%s]",
|
|
|
|
watchdog_pid,
|
|
|
|
alloca_str_toprint(config.server.watchdog.executable),
|
|
|
|
strbuf_str(argv_sb)
|
|
|
|
);
|
|
|
|
do { _exit(0); } while (1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
do { _exit(-1); } while (1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case -1:
|
|
|
|
/* child fork failed */
|
|
|
|
WHY_perror("fork");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (alarm) {
|
|
|
|
time_ms_t now = gettime_ms();
|
2014-06-04 06:40:36 +00:00
|
|
|
RESCHEDULE(alarm,
|
|
|
|
now+config.server.watchdog.interval_ms,
|
|
|
|
now+config.server.watchdog.interval_ms,
|
|
|
|
now+100);
|
2014-04-16 08:36:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-23 04:19:37 +00:00
|
|
|
DEFINE_ALARM(rhizome_clean_db);
|
|
|
|
void rhizome_clean_db(struct sched_ent *alarm)
|
2014-06-06 04:09:50 +00:00
|
|
|
{
|
2014-06-23 05:50:01 +00:00
|
|
|
if (!config.rhizome.enable || !rhizome_db)
|
2014-06-23 04:19:37 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
time_ms_t now = gettime_ms();
|
2014-06-23 05:50:01 +00:00
|
|
|
rhizome_cleanup(NULL);
|
|
|
|
// clean up every 30 minutes or so
|
|
|
|
RESCHEDULE(alarm, now + 30*60*1000, TIME_MS_NEVER_WILL, TIME_MS_NEVER_WILL);
|
2014-06-06 04:09:50 +00:00
|
|
|
}
|
|
|
|
|
2014-04-29 05:34:20 +00:00
|
|
|
void cf_on_config_change()
|
|
|
|
{
|
2014-05-29 07:11:27 +00:00
|
|
|
if (!serverMode)
|
|
|
|
return;
|
|
|
|
|
|
|
|
time_ms_t now = gettime_ms();
|
|
|
|
|
|
|
|
dna_helper_start();
|
|
|
|
directory_service_init();
|
|
|
|
|
2014-06-02 04:39:22 +00:00
|
|
|
// check for interfaces at least once after config change
|
2014-06-04 06:40:36 +00:00
|
|
|
RESCHEDULE(&ALARM_STRUCT(overlay_interface_discover), now, now, now);
|
2014-05-29 07:11:27 +00:00
|
|
|
|
|
|
|
if (link_has_neighbours())
|
|
|
|
// send rhizome sync periodically
|
2014-06-04 06:40:36 +00:00
|
|
|
RESCHEDULE(&ALARM_STRUCT(rhizome_sync_announce),
|
|
|
|
now+1000, now+1000, TIME_MS_NEVER_WILL);
|
2014-05-29 07:11:27 +00:00
|
|
|
|
|
|
|
if (config.server.watchdog.executable[0])
|
2014-06-04 06:40:36 +00:00
|
|
|
RESCHEDULE(&ALARM_STRUCT(server_watchdog),
|
|
|
|
now+config.server.watchdog.interval_ms,
|
|
|
|
now+config.server.watchdog.interval_ms,
|
|
|
|
now+100);
|
2014-05-29 07:11:27 +00:00
|
|
|
|
2014-06-04 06:40:36 +00:00
|
|
|
// Periodically check for modified configuration
|
|
|
|
RESCHEDULE(&ALARM_STRUCT(server_config_reload),
|
|
|
|
now+config.server.config_reload_interval_ms,
|
|
|
|
TIME_MS_NEVER_WILL,
|
|
|
|
now+config.server.config_reload_interval_ms+100);
|
2014-06-06 04:09:50 +00:00
|
|
|
|
|
|
|
if (config.rhizome.enable){
|
2014-06-23 05:50:01 +00:00
|
|
|
rhizome_opendb();
|
|
|
|
RESCHEDULE(&ALARM_STRUCT(rhizome_clean_db), now + 30*60*1000, TIME_MS_NEVER_WILL, TIME_MS_NEVER_WILL);
|
2014-06-06 04:09:50 +00:00
|
|
|
}else if(rhizome_db){
|
|
|
|
rhizome_close_db();
|
|
|
|
}
|
2014-04-29 05:34:20 +00:00
|
|
|
}
|
|
|
|
|
2012-05-08 01:53:43 +00:00
|
|
|
/* Called periodically by the server process in its main loop.
|
|
|
|
*/
|
2014-05-29 07:11:27 +00:00
|
|
|
DEFINE_ALARM(server_shutdown_check);
|
2012-07-02 03:49:54 +00:00
|
|
|
void server_shutdown_check(struct sched_ent *alarm)
|
2012-05-08 01:53:43 +00:00
|
|
|
{
|
2014-06-06 06:19:16 +00:00
|
|
|
// TODO we should watch a descriptor and quit when it closes
|
2012-05-08 01:53:43 +00:00
|
|
|
/* If this server has been supplanted with another or Serval has been uninstalled, then its PID
|
|
|
|
file will change or be unaccessible. In this case, shut down without all the cleanup.
|
|
|
|
Perform this check at most once per second. */
|
2012-08-09 02:44:32 +00:00
|
|
|
static time_ms_t server_pid_time_ms = 0;
|
|
|
|
time_ms_t now = gettime_ms();
|
|
|
|
if (server_pid_time_ms == 0 || now - server_pid_time_ms > 1000) {
|
|
|
|
server_pid_time_ms = now;
|
2012-05-08 01:53:43 +00:00
|
|
|
if (server_pid() != server_getpid) {
|
2012-06-28 06:07:36 +00:00
|
|
|
WARNF("Server pid file no longer contains pid=%d -- shutting down without cleanup", server_getpid);
|
2012-05-08 01:53:43 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2012-07-02 03:49:54 +00:00
|
|
|
if (alarm){
|
2014-06-06 04:09:50 +00:00
|
|
|
RESCHEDULE(alarm, now+1000, now+30000, now+1100);
|
2012-07-02 03:49:54 +00:00
|
|
|
}
|
2012-05-08 01:53:43 +00:00
|
|
|
}
|
|
|
|
|
2014-02-25 02:12:36 +00:00
|
|
|
static void clean_proc()
|
|
|
|
{
|
|
|
|
char path_buf[400];
|
2014-03-26 05:05:43 +00:00
|
|
|
if (FORMF_SERVAL_RUN_PATH(path_buf, PROC_SUBDIR)) {
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *dp;
|
|
|
|
if ((dir = opendir(path_buf)) == NULL) {
|
|
|
|
WARNF_perror("opendir(%s)", alloca_str_toprint(path_buf));
|
|
|
|
return;
|
2014-02-25 02:12:36 +00:00
|
|
|
}
|
2014-03-26 05:05:43 +00:00
|
|
|
while ((dp = readdir(dir)) != NULL) {
|
|
|
|
if (FORMF_SERVAL_RUN_PATH(path_buf, PROC_SUBDIR "/%s", dp->d_name)) {
|
|
|
|
struct stat st;
|
|
|
|
if (lstat(path_buf, &st) == -1)
|
|
|
|
WARNF_perror("stat(%s)", path_buf);
|
|
|
|
else if (S_ISREG(st.st_mode))
|
|
|
|
unlink(path_buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
2014-02-25 02:12:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-11 05:24:31 +00:00
|
|
|
static void serverCleanUp()
|
2012-05-08 02:49:52 +00:00
|
|
|
{
|
2014-06-11 05:24:31 +00:00
|
|
|
assert(serverMode);
|
|
|
|
rhizome_close_db();
|
|
|
|
dna_helper_shutdown();
|
|
|
|
overlay_interface_close_all();
|
2013-12-11 06:11:57 +00:00
|
|
|
overlay_mdp_clean_socket_files();
|
2014-02-25 02:12:36 +00:00
|
|
|
clean_proc();
|
2012-05-08 02:49:52 +00:00
|
|
|
}
|
|
|
|
|
2012-05-09 09:36:44 +00:00
|
|
|
void signal_handler(int signal)
|
|
|
|
{
|
|
|
|
switch (signal) {
|
|
|
|
case SIGHUP:
|
|
|
|
case SIGINT:
|
2014-05-29 07:11:27 +00:00
|
|
|
/* Trigger the server to close gracefully after any current alarm has completed.
|
|
|
|
If we get a second signal, exit now.
|
|
|
|
*/
|
2014-06-11 04:13:56 +00:00
|
|
|
if (serverMode==SERVER_RUNNING){
|
2014-05-29 07:11:27 +00:00
|
|
|
INFO("Attempting clean shutdown");
|
2014-06-11 04:13:56 +00:00
|
|
|
serverMode=SERVER_CLOSING;
|
2014-05-29 07:11:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
LOGF(LOG_LEVEL_FATAL, "Caught signal %s", alloca_signal_name(signal));
|
|
|
|
LOGF(LOG_LEVEL_FATAL, "The following clue may help: %s", crash_handler_clue);
|
|
|
|
dump_stack(LOG_LEVEL_FATAL);
|
2012-05-09 09:36:44 +00:00
|
|
|
}
|
2013-07-31 05:21:24 +00:00
|
|
|
|
2012-07-26 09:01:23 +00:00
|
|
|
serverCleanUp();
|
|
|
|
exit(0);
|
|
|
|
}
|