2016-10-12 06:38:26 +00:00
|
|
|
/*
|
|
|
|
Serval DNA server main loop
|
|
|
|
Copyright (C) 2010 Paul Gardner-Stephen
|
|
|
|
Copyright (C) 2011-2015 Serval Project Inc.
|
|
|
|
Copyright (C) 2016 Flinders University
|
2010-07-13 12:15:46 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2016-10-19 05:57:20 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
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>
|
2014-08-25 04:54:00 +00:00
|
|
|
#include <sys/wait.h>
|
2016-10-12 06:38:26 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#ifdef HAVE_LINUX_THREADS
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#endif
|
2012-02-05 05:45:19 +00:00
|
|
|
|
2016-10-12 06:38:26 +00:00
|
|
|
#include "server.h"
|
2012-02-23 02:15:42 +00:00
|
|
|
#include "serval.h"
|
Switch to feature-driven linking
This introduces a new way of linking Serval executables and dynamic
libraries from static libraries like libservald.a -- called
"feature-driven" linking.
The Makefile now links servald and serval-tests from libservald.a,
rather than from an explicit list of object (.o) files. Thanks to the
section-based method for registering functions such as HTTP handlers,
CLI commands and MDP handlers, these object files had become
"stand-alone" and hence were no longer included in the link because
there was no unresolved reference that required them to be linked in.
The new "feature.h" provides the DECLARE_FEATURE(name) macro that each
stand-alone source file uses to declare the named feature(s) it
provides. Each executable can call the USE_FEATURE(name) macro in any
of its explicitly-linked source files to cause the corresponding
object(s) to be included in the link, eg, servald_features.c.
The DEFINE_BINDING() macro has been extended so that every individual
MDP binding is given a feature name based on its port number macro, eg,
"mdp_binding_MDP_PORT_ECHO".
Some features have been factored into their own separate source files so
they can be omitted or included in a build independently of each other:
- the MDP bindings for MDP_PORT_DNALOOKUP, MDP_PORT_ECHO,
MDP_PORT_TRACE, MDP_PORT_KEYMAPREQUEST, MDP_PORT_RHIZOME_xxx,
MDP_PORT_PROBE, MDP_PORT_STUN, MDP_PORT_STUNREQ
- the CLI "log" and "echo" commands
- the CLI "rhizome direct" command
The JNI source files are only compiled if the <jni.h> header is present,
otherwise they are omitted from libservald.so.
2016-10-13 02:58:23 +00:00
|
|
|
#include "rhizome.h"
|
2012-12-04 03:42:28 +00:00
|
|
|
#include "conf.h"
|
2016-10-14 06:58:55 +00:00
|
|
|
#include "log.h"
|
2016-09-20 04:03:19 +00:00
|
|
|
#include "str.h"
|
2016-10-12 06:38:26 +00:00
|
|
|
#include "numeric_str.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-06-06 06:19:16 +00:00
|
|
|
#include "keyring.h"
|
2014-08-25 04:54:00 +00:00
|
|
|
#include "commandline.h"
|
2015-05-25 02:16:37 +00:00
|
|
|
#include "mdp_client.h"
|
2015-08-31 05:48:08 +00:00
|
|
|
#include "route_link.h"
|
2017-11-24 00:03:39 +00:00
|
|
|
#include "httpd.h"
|
|
|
|
#include "debug.h"
|
2010-07-13 12:15:46 +00:00
|
|
|
|
Switch to feature-driven linking
This introduces a new way of linking Serval executables and dynamic
libraries from static libraries like libservald.a -- called
"feature-driven" linking.
The Makefile now links servald and serval-tests from libservald.a,
rather than from an explicit list of object (.o) files. Thanks to the
section-based method for registering functions such as HTTP handlers,
CLI commands and MDP handlers, these object files had become
"stand-alone" and hence were no longer included in the link because
there was no unresolved reference that required them to be linked in.
The new "feature.h" provides the DECLARE_FEATURE(name) macro that each
stand-alone source file uses to declare the named feature(s) it
provides. Each executable can call the USE_FEATURE(name) macro in any
of its explicitly-linked source files to cause the corresponding
object(s) to be included in the link, eg, servald_features.c.
The DEFINE_BINDING() macro has been extended so that every individual
MDP binding is given a feature name based on its port number macro, eg,
"mdp_binding_MDP_PORT_ECHO".
Some features have been factored into their own separate source files so
they can be omitted or included in a build independently of each other:
- the MDP bindings for MDP_PORT_DNALOOKUP, MDP_PORT_ECHO,
MDP_PORT_TRACE, MDP_PORT_KEYMAPREQUEST, MDP_PORT_RHIZOME_xxx,
MDP_PORT_PROBE, MDP_PORT_STUN, MDP_PORT_STUNREQ
- the CLI "log" and "echo" commands
- the CLI "rhizome direct" command
The JNI source files are only compiled if the <jni.h> header is present,
otherwise they are omitted from libservald.so.
2016-10-13 02:58:23 +00:00
|
|
|
DEFINE_FEATURE(cli_server);
|
|
|
|
|
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"
|
|
|
|
|
2016-10-12 06:38:26 +00:00
|
|
|
__thread enum server_mode serverMode = SERVER_NOT_RUNNING;
|
2014-06-06 06:19:16 +00:00
|
|
|
|
2016-10-12 06:38:26 +00:00
|
|
|
struct pid_tid {
|
|
|
|
pid_t pid;
|
|
|
|
pid_t tid;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct pid_tid server_pid_tid = { .pid = 0, .tid = 0 };
|
2016-04-06 05:27:07 +00:00
|
|
|
static int server_pidfd = -1;
|
2015-05-25 02:16:37 +00:00
|
|
|
static int server();
|
2014-06-11 05:24:31 +00:00
|
|
|
static int server_write_pid();
|
|
|
|
static void signal_handler(int signal);
|
|
|
|
static void serverCleanUp();
|
2014-08-25 04:54:00 +00:00
|
|
|
static const char *_server_pidfile_path(struct __sourceloc __whence);
|
|
|
|
#define server_pidfile_path() (_server_pidfile_path(__WHENCE__))
|
2011-08-08 14:41:46 +00:00
|
|
|
|
2016-10-12 06:38:26 +00:00
|
|
|
static int server_get_proc_state(const char *path, char *buff, size_t buff_len);
|
2017-09-12 05:54:10 +00:00
|
|
|
static void server_stop_alarms();
|
2016-10-12 06:38:26 +00:00
|
|
|
|
2016-10-17 07:10:28 +00:00
|
|
|
// Define our own gettid() and tgkill() if <unistd.h> doesn't provide them (eg, it does on Android).
|
|
|
|
|
|
|
|
#ifndef HAVE_GETTID
|
2016-10-12 06:38:26 +00:00
|
|
|
static pid_t gettid()
|
|
|
|
{
|
|
|
|
#ifdef HAVE_LINUX_THREADS
|
|
|
|
return syscall(SYS_gettid);
|
|
|
|
#else
|
|
|
|
return getpid();
|
|
|
|
#endif
|
|
|
|
}
|
2016-10-17 07:10:28 +00:00
|
|
|
#endif // !HAVE_GETTID
|
|
|
|
|
|
|
|
#ifdef HAVE_LINUX_THREADS
|
|
|
|
#ifndef HAVE_TGKILL
|
|
|
|
static int tgkill(int tgid, int tid, int signum)
|
|
|
|
{
|
|
|
|
return syscall(SYS_tgkill, tgid, tid, signum);
|
|
|
|
}
|
|
|
|
#endif // !HAVE_TGKILL
|
|
|
|
#endif // HAVE_LINUX_THREADS
|
2016-10-12 06:38:26 +00:00
|
|
|
|
2016-10-17 05:58:38 +00:00
|
|
|
// Read the PID and TID from the given pidfile, returning a PID of 0 if the file does not exist, or
|
|
|
|
// a PID of -1 if the file exists but contains invalid content or is not locked by process PID,
|
|
|
|
// otherwise the PID/TID of the process that has the lock on the file.
|
|
|
|
static struct pid_tid read_pidfile(const char *path)
|
|
|
|
{
|
|
|
|
int fd = open(path, O_RDONLY);
|
|
|
|
if (fd == -1) {
|
2016-10-20 02:43:05 +00:00
|
|
|
if (errno == ENOENT)
|
|
|
|
return (struct pid_tid){ .pid = 0, .tid = 0 };
|
2016-10-17 05:58:38 +00:00
|
|
|
WHYF_perror("open(%s, O_RDONLY)", alloca_str_toprint(path));
|
|
|
|
return (struct pid_tid){ .pid = -1, .tid = -1 };
|
|
|
|
}
|
|
|
|
int32_t pid = -1;
|
|
|
|
int32_t tid = -1;
|
|
|
|
char buf[30];
|
|
|
|
ssize_t len = read(fd, buf, sizeof buf);
|
2016-10-20 02:43:05 +00:00
|
|
|
if (len == -1) {
|
|
|
|
WHYF_perror("read(%s, %p, %zu)", alloca_str_toprint(path), buf, sizeof buf);
|
|
|
|
} else if (len > 0 && (size_t)len < sizeof buf) {
|
|
|
|
DEBUGF(server, "Read from pidfile %s: %s", path, alloca_toprint(-1, buf, len));
|
2016-10-17 05:58:38 +00:00
|
|
|
buf[len] = '\0';
|
|
|
|
const char *e = NULL;
|
2016-10-20 02:43:05 +00:00
|
|
|
if (!str_to_int32(buf, 10, &pid, &e))
|
|
|
|
pid = -1;
|
|
|
|
else if (*e == ' ')
|
2016-10-17 05:58:38 +00:00
|
|
|
str_to_int32(e + 1, 10, &tid, NULL);
|
|
|
|
else
|
|
|
|
tid = pid;
|
2016-10-20 02:43:05 +00:00
|
|
|
} else {
|
|
|
|
WARNF("Pidfile %s has invalid content: %s", path, alloca_toprint(-1, buf, len));
|
2016-10-17 05:58:38 +00:00
|
|
|
}
|
|
|
|
if (pid > 0) {
|
|
|
|
// Only return a valid pid/tid if the file is currently locked by the same process that it
|
|
|
|
// identifies.
|
|
|
|
struct flock lock;
|
|
|
|
bzero(&lock, sizeof lock);
|
|
|
|
lock.l_type = F_RDLCK;
|
|
|
|
lock.l_start = 0;
|
|
|
|
lock.l_whence = SEEK_SET;
|
|
|
|
lock.l_len = len;
|
|
|
|
fcntl(fd, F_GETLK, &lock);
|
2016-10-20 02:43:05 +00:00
|
|
|
if (lock.l_type == F_UNLCK || lock.l_pid != pid) {
|
|
|
|
DEBUGF(server, "Pidfile %s is not locked by pid %d", path, pid);
|
2016-10-17 05:58:38 +00:00
|
|
|
pid = tid = -1;
|
2016-10-20 02:43:05 +00:00
|
|
|
}
|
2016-10-17 05:58:38 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return (struct pid_tid){ .pid = pid, .tid = tid };
|
|
|
|
}
|
|
|
|
|
2016-10-12 06:38:26 +00:00
|
|
|
static struct pid_tid get_server_pid_tid()
|
2012-05-08 01:53:43 +00:00
|
|
|
{
|
2016-10-12 06:38:26 +00:00
|
|
|
// If the server process closes another handle on the same file, its lock will disappear, so this
|
|
|
|
// guards against that happening.
|
|
|
|
if (server_pid_tid.pid == getpid())
|
|
|
|
return server_pid_tid;
|
|
|
|
// Attempt to read the pid, and optionally the tid (Linux thread ID) from the pid file.
|
2014-03-26 05:05:43 +00:00
|
|
|
char dirname[1024];
|
|
|
|
if (!FORMF_SERVAL_RUN_PATH(dirname, NULL))
|
2016-10-12 06:38:26 +00:00
|
|
|
goto error;
|
2012-05-08 01:53:43 +00:00
|
|
|
struct stat st;
|
2016-10-12 06:38:26 +00:00
|
|
|
if (stat(dirname, &st) == -1) {
|
|
|
|
WHYF_perror("stat(%s)", alloca_str_toprint(dirname));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if ((st.st_mode & S_IFMT) != S_IFDIR) {
|
|
|
|
WHYF("Not a directory: %s", dirname);
|
|
|
|
goto error;
|
|
|
|
}
|
2016-10-17 05:58:38 +00:00
|
|
|
const char *pidfile_path = server_pidfile_path();
|
|
|
|
if (pidfile_path == NULL)
|
2016-10-12 06:38:26 +00:00
|
|
|
goto error;
|
2016-10-17 05:58:38 +00:00
|
|
|
assert(strrchr(pidfile_path, '/') != NULL);
|
|
|
|
struct pid_tid id = read_pidfile(pidfile_path);
|
|
|
|
if (id.pid == -1) {
|
2016-10-25 05:21:31 +00:00
|
|
|
DEBUGF(server, "Unlinking stale pidfile %s", pidfile_path);
|
2016-10-17 05:58:38 +00:00
|
|
|
unlink(pidfile_path);
|
2016-10-20 02:43:05 +00:00
|
|
|
id.pid = 0;
|
2012-05-08 01:53:43 +00:00
|
|
|
}
|
2016-10-17 05:58:38 +00:00
|
|
|
return id;
|
2016-10-12 06:38:26 +00:00
|
|
|
error:
|
|
|
|
return (struct pid_tid){ .pid = -1, .tid = -1 };
|
2014-03-26 05:05:43 +00:00
|
|
|
}
|
|
|
|
|
2016-10-12 06:38:26 +00:00
|
|
|
// Send a signal to a given process. Returns 0 if sent, 1 if not sent because the process is non
|
|
|
|
// existent (ESRCH), or -1 if not sent due to another error (eg, EPERM).
|
|
|
|
static int send_signal(const struct pid_tid *id, int signum)
|
2015-05-25 02:16:37 +00:00
|
|
|
{
|
2016-10-12 06:38:26 +00:00
|
|
|
#ifdef HAVE_LINUX_THREADS
|
|
|
|
if (id->tid > 0) {
|
2016-10-17 05:58:38 +00:00
|
|
|
if (tgkill(id->pid, id->tid, signum) == -1) {
|
2016-10-12 06:38:26 +00:00
|
|
|
if (errno == ESRCH)
|
|
|
|
return 1;
|
|
|
|
WHYF_perror("Cannot send %s to Servald pid=%d tid=%d (pidfile %s)", alloca_signal_name(signum), id->pid, id->tid, server_pidfile_path());
|
|
|
|
return -1;
|
2015-05-25 02:16:37 +00:00
|
|
|
}
|
2016-10-12 06:38:26 +00:00
|
|
|
return 0;
|
2015-05-25 02:16:37 +00:00
|
|
|
}
|
2016-10-12 06:38:26 +00:00
|
|
|
#endif // !HAVE_LINUX_THREADS
|
|
|
|
if (kill(id->pid, signum) == -1) {
|
|
|
|
if (errno == ESRCH)
|
|
|
|
return 1;
|
|
|
|
WHYF_perror("Cannot send %s to Servald pid=%d (pidfile %s)", alloca_signal_name(signum), id->pid, server_pidfile_path());
|
|
|
|
return -1;
|
2015-05-25 02:16:37 +00:00
|
|
|
}
|
2016-10-12 06:38:26 +00:00
|
|
|
return 0;
|
2015-05-25 02:16:37 +00:00
|
|
|
}
|
|
|
|
|
2016-10-12 06:38:26 +00:00
|
|
|
static const char *_server_pidfile_path(struct __sourceloc __whence)
|
2015-05-25 02:16:37 +00:00
|
|
|
{
|
Switch to feature-driven linking
This introduces a new way of linking Serval executables and dynamic
libraries from static libraries like libservald.a -- called
"feature-driven" linking.
The Makefile now links servald and serval-tests from libservald.a,
rather than from an explicit list of object (.o) files. Thanks to the
section-based method for registering functions such as HTTP handlers,
CLI commands and MDP handlers, these object files had become
"stand-alone" and hence were no longer included in the link because
there was no unresolved reference that required them to be linked in.
The new "feature.h" provides the DECLARE_FEATURE(name) macro that each
stand-alone source file uses to declare the named feature(s) it
provides. Each executable can call the USE_FEATURE(name) macro in any
of its explicitly-linked source files to cause the corresponding
object(s) to be included in the link, eg, servald_features.c.
The DEFINE_BINDING() macro has been extended so that every individual
MDP binding is given a feature name based on its port number macro, eg,
"mdp_binding_MDP_PORT_ECHO".
Some features have been factored into their own separate source files so
they can be omitted or included in a build independently of each other:
- the MDP bindings for MDP_PORT_DNALOOKUP, MDP_PORT_ECHO,
MDP_PORT_TRACE, MDP_PORT_KEYMAPREQUEST, MDP_PORT_RHIZOME_xxx,
MDP_PORT_PROBE, MDP_PORT_STUN, MDP_PORT_STUNREQ
- the CLI "log" and "echo" commands
- the CLI "rhizome direct" command
The JNI source files are only compiled if the <jni.h> header is present,
otherwise they are omitted from libservald.so.
2016-10-13 02:58:23 +00:00
|
|
|
static char pidfile_path[256];
|
2016-10-12 06:38:26 +00:00
|
|
|
if (!pidfile_path[0]) {
|
|
|
|
if (!FORMF_SERVAL_RUN_PATH(pidfile_path, PIDFILE_NAME))
|
|
|
|
return NULL;
|
2015-05-25 02:16:37 +00:00
|
|
|
}
|
2016-10-12 06:38:26 +00:00
|
|
|
return pidfile_path;
|
2015-05-25 02:16:37 +00:00
|
|
|
}
|
|
|
|
|
2016-10-12 06:38:26 +00:00
|
|
|
int server_pid()
|
2015-05-25 02:16:37 +00:00
|
|
|
{
|
2016-10-12 06:38:26 +00:00
|
|
|
return get_server_pid_tid().pid;
|
2015-05-25 02:16:37 +00:00
|
|
|
}
|
|
|
|
|
2016-10-12 06:38:26 +00:00
|
|
|
int server_bind()
|
2010-07-13 12:15:46 +00:00
|
|
|
{
|
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.
|
Rewrite logging system
Rename the logging primitive functions and utility functions, prefixing
all with 'serval_log', eg: logMessage() -> serval_logf() etc.
Add an XPRINTF xhexdump() function and use it to implement the
serval_log_hexdump() utility, renamed from dump(). Add macros
WHY_dump(), WARN_dump(), HINT_dump() and DEBUG_dump(), and use them
everywhere.
Remove the 'log.console.dump_config' and 'log.file.dump_config'
configuration options; configuration is now dumped in every log prolog.
The logging system now constructs the log prolog by invoking the new
'log_prolog' trigger, so that it no longer depends on the version string
and configuration system. Any system that wants to present a message in
the log prolog can define its own trigger, which calls standard log
primitives to print the message.
Split the logging system into a front-end (log.c) that provides the
logging primitives and is independent of the configuration system, and a
set of back-end "outputters" (log_output_console.c, log_output_file.c,
log_output_android.c) that may depend on the configuration system and
are decoupled from the front-end using the 'logoutput' link section.
These log outputters are explicitly linked into executables by the
Makefile rules, but could also be linked in using USE_FEATURE(). The
USE_FEATURE() calls have _not_ been added to servald_features.c, so that
different daemon executables can be built with the same feature set but
different log outputs.
2017-11-29 13:34:54 +00:00
|
|
|
serval_log_level_NoLogFileConfigured = LOG_LEVEL_WARN;
|
2014-06-11 05:23:26 +00:00
|
|
|
|
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;
|
2015-06-01 07:16:21 +00:00
|
|
|
bzero(&sig, sizeof sig);
|
|
|
|
|
|
|
|
sig.sa_flags = 0;
|
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);
|
2015-06-01 07:16:21 +00:00
|
|
|
sigaddset(&sig.sa_mask, SIGIO);
|
|
|
|
|
|
|
|
#ifdef ANDROID
|
|
|
|
// batphone depends on this constant to wake up the scheduler
|
|
|
|
// break the build if it changes.
|
|
|
|
assert(SIGIO==29);
|
|
|
|
#endif
|
|
|
|
|
2012-07-26 09:01:23 +00:00
|
|
|
sigaction(SIGHUP, &sig, NULL);
|
|
|
|
sigaction(SIGINT, &sig, NULL);
|
2015-06-01 07:16:21 +00:00
|
|
|
sigaction(SIGIO, &sig, NULL);
|
2012-05-09 09:36:44 +00:00
|
|
|
|
2018-04-03 05:02:06 +00:00
|
|
|
// Perform additional startup, which should be limited to tasks like binding sockets
|
|
|
|
// So that clients can initiate a connection once servald start has returned.
|
|
|
|
// serverMode should be cleared to indicate failures
|
|
|
|
// Any CPU or IO heavy initialisation should be performed in a config changed trigger
|
|
|
|
|
|
|
|
CALL_TRIGGER(startup);
|
2018-06-01 08:10:09 +00:00
|
|
|
if (serverMode == SERVER_NOT_RUNNING)
|
2015-05-25 02:16:37 +00:00
|
|
|
return -1;
|
2018-04-03 05:02:06 +00:00
|
|
|
|
2014-06-06 06:19:16 +00:00
|
|
|
// start the HTTP server if enabled
|
2015-08-13 19:36:27 +00:00
|
|
|
if (httpd_server_start(config.rhizome.http.port, config.rhizome.http.port + HTTPD_PORT_RANGE)==-1) {
|
2018-06-01 08:10:09 +00:00
|
|
|
serverMode = SERVER_NOT_RUNNING;
|
2015-05-25 02:16:37 +00:00
|
|
|
return -1;
|
2016-06-15 07:38:25 +00:00
|
|
|
}
|
2015-08-13 19:36:27 +00:00
|
|
|
|
2014-06-06 06:19:16 +00:00
|
|
|
/* 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);
|
2016-10-25 05:21:31 +00:00
|
|
|
DEBUGF(server, "Sleeping for %"PRId64" milliseconds", (int64_t) milliseconds);
|
2014-06-06 06:19:16 +00:00
|
|
|
sleep_ms(milliseconds);
|
|
|
|
}
|
2016-10-17 05:58:38 +00:00
|
|
|
|
2014-06-06 06:19:16 +00:00
|
|
|
/* record PID file so that servald start can return */
|
2016-10-17 05:58:38 +00:00
|
|
|
if (server_write_pid()) {
|
2018-06-01 08:10:09 +00:00
|
|
|
serverMode = SERVER_NOT_RUNNING;
|
2015-05-25 02:16:37 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2016-10-17 05:58:38 +00:00
|
|
|
|
2014-06-06 06:19:16 +00:00
|
|
|
overlay_queue_init();
|
2016-10-17 05:58:38 +00:00
|
|
|
|
2014-06-06 06:19:16 +00:00
|
|
|
time_ms_t now = gettime_ms();
|
2016-10-17 05:58:38 +00:00
|
|
|
|
2014-06-06 06:19:16 +00:00
|
|
|
/* Calculate (and possibly show) CPU usage stats periodically */
|
2015-03-30 06:00:10 +00:00
|
|
|
RESCHEDULE(&ALARM_STRUCT(fd_periodicstats), now+3000, TIME_MS_NEVER_WILL, now+3500);
|
2014-06-06 06:19:16 +00:00
|
|
|
|
2015-05-25 02:16:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-12 06:38:26 +00:00
|
|
|
void server_loop(time_ms_t (*waiting)(time_ms_t, time_ms_t, time_ms_t), void (*wokeup)())
|
2015-05-25 02:16:37 +00:00
|
|
|
{
|
2017-09-12 05:54:10 +00:00
|
|
|
// possible race condition... Shutting down before we even started
|
2016-10-24 05:39:37 +00:00
|
|
|
CALL_TRIGGER(conf_change);
|
2017-09-12 05:54:10 +00:00
|
|
|
|
2016-10-12 06:38:26 +00:00
|
|
|
// This log message is used by tests to wait for the server to start.
|
2015-05-25 02:16:37 +00:00
|
|
|
INFOF("Server initialised, entering main loop");
|
2017-09-12 05:54:10 +00:00
|
|
|
|
2014-06-11 04:13:56 +00:00
|
|
|
/* Check for activitiy and respond to it */
|
2017-09-12 05:54:10 +00:00
|
|
|
while (fd_poll2(waiting, wokeup))
|
2014-06-11 05:24:31 +00:00
|
|
|
;
|
2016-10-12 06:38:26 +00:00
|
|
|
|
|
|
|
INFOF("Server finished, exiting main loop");
|
2017-09-12 05:54:10 +00:00
|
|
|
fd_showstats();
|
2014-06-06 06:19:16 +00:00
|
|
|
serverCleanUp();
|
2016-04-06 05:27:07 +00:00
|
|
|
|
|
|
|
if (server_pidfd!=-1){
|
|
|
|
close(server_pidfd);
|
|
|
|
server_pidfd = -1;
|
2016-10-17 05:58:38 +00:00
|
|
|
unlink(server_pidfile_path());
|
2016-04-06 05:27:07 +00:00
|
|
|
}
|
2015-05-25 02:16:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int server()
|
|
|
|
{
|
|
|
|
IN();
|
|
|
|
if (server_bind()==-1)
|
|
|
|
RETURN(-1);
|
2016-10-12 06:38:26 +00:00
|
|
|
|
|
|
|
server_loop(NULL, NULL);
|
|
|
|
|
|
|
|
RETURN(0);
|
2014-02-24 00:07:14 +00:00
|
|
|
}
|
|
|
|
|
2014-06-11 05:24:31 +00:00
|
|
|
static int server_write_pid()
|
2014-02-24 00:07:14 +00:00
|
|
|
{
|
2015-05-25 02:16:37 +00:00
|
|
|
server_write_proc_state("http_port", "%d", httpd_server_port);
|
|
|
|
server_write_proc_state("mdp_inet_port", "%d", mdp_loopback_port);
|
2011-08-08 14:41:46 +00:00
|
|
|
|
2018-03-26 12:05:34 +00:00
|
|
|
// Create or unlink the "primary_sid" proc state file.
|
|
|
|
get_my_subscriber(0);
|
|
|
|
|
2016-10-17 05:58:38 +00:00
|
|
|
// Create a locked pidfile to advertise that the server is now running.
|
|
|
|
const char *pidfile_path = server_pidfile_path();
|
|
|
|
if (pidfile_path == NULL)
|
|
|
|
return -1;
|
2016-04-06 05:27:07 +00:00
|
|
|
|
2016-10-17 05:58:38 +00:00
|
|
|
// The pidfile content is simply the ASCII decimal PID, optionally followed by a single space and
|
|
|
|
// the ASCII decimal Thread ID (tid) if the server is running as a Linux thread that is not the
|
|
|
|
// process's main thread.
|
2016-10-12 06:38:26 +00:00
|
|
|
int32_t pid = server_pid_tid.pid = getpid();
|
|
|
|
int32_t tid = server_pid_tid.tid = gettid();
|
2016-10-17 05:58:38 +00:00
|
|
|
char content[30];
|
|
|
|
size_t content_size = 0;
|
|
|
|
{
|
|
|
|
strbuf sb = strbuf_local_buf(content);
|
|
|
|
strbuf_sprintf(sb, "%" PRId32, pid);
|
|
|
|
if (tid != pid)
|
|
|
|
strbuf_sprintf(sb, " %" PRId32, tid);
|
|
|
|
assert(!strbuf_overrun(sb));
|
|
|
|
content_size = strbuf_len(sb);
|
|
|
|
}
|
2016-10-12 06:38:26 +00:00
|
|
|
|
2016-10-17 05:58:38 +00:00
|
|
|
// The pidfile lock covers its whole content.
|
2016-04-06 05:27:07 +00:00
|
|
|
struct flock lock;
|
|
|
|
bzero(&lock, sizeof lock);
|
|
|
|
lock.l_type = F_WRLCK;
|
|
|
|
lock.l_start = 0;
|
|
|
|
lock.l_whence = SEEK_SET;
|
2016-10-17 05:58:38 +00:00
|
|
|
lock.l_len = content_size;
|
|
|
|
|
|
|
|
// Form the name of the temporary pidfile.
|
|
|
|
strbuf tmpfile_path_sb = strbuf_alloca(strlen(pidfile_path) + 25);
|
|
|
|
strbuf_puts(tmpfile_path_sb, pidfile_path);
|
|
|
|
strbuf_sprintf(tmpfile_path_sb, ".%d-%d", pid, tid);
|
|
|
|
assert(!strbuf_overrun(tmpfile_path_sb));
|
|
|
|
const char *tmpfile_path = strbuf_str(tmpfile_path_sb);
|
|
|
|
|
|
|
|
// Create the temporary pidfile and lock it, deleting any stale temporaries if necessary. Leave
|
|
|
|
// the temporary pidfile open to retain the lock -- if successful it will eventually be the real
|
|
|
|
// pidfile.
|
2016-10-20 02:43:05 +00:00
|
|
|
DEBUGF(server, "unlink(%s)", alloca_str_toprint(tmpfile_path));
|
2016-10-17 05:58:38 +00:00
|
|
|
unlink(tmpfile_path);
|
2016-10-20 02:43:05 +00:00
|
|
|
DEBUGF(server, "open(%s, O_RDWR|O_CREAT|O_CLOEXEC)", alloca_str_toprint(tmpfile_path));
|
2016-10-17 05:58:38 +00:00
|
|
|
int fd = open(tmpfile_path, O_RDWR | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
|
|
|
|
if (fd==-1)
|
|
|
|
return WHYF_perror("Cannot create temporary pidfile: open(%s, O_RDWR|O_CREAT|O_CLOEXEC)", alloca_str_toprint(tmpfile_path));
|
2016-10-20 02:43:05 +00:00
|
|
|
DEBUG(server, "lock");
|
2016-10-17 05:58:38 +00:00
|
|
|
if (fcntl(fd, F_SETLK, &lock) == -1) {
|
|
|
|
WHYF_perror("Cannot lock temporary pidfile %s: fcntl(%d, F_SETLK, &lock)", tmpfile_path, fd);
|
2016-04-06 05:27:07 +00:00
|
|
|
close(fd);
|
2016-10-17 05:58:38 +00:00
|
|
|
return -1;
|
2016-04-06 05:27:07 +00:00
|
|
|
}
|
2016-10-17 05:58:38 +00:00
|
|
|
if (ftruncate(fd, 0) == -1){
|
2016-04-06 05:27:07 +00:00
|
|
|
close(fd);
|
|
|
|
return WHYF_perror("ftruncate(%d, 0)", fd);
|
|
|
|
}
|
2016-10-20 02:43:05 +00:00
|
|
|
DEBUGF(server, "write %s", alloca_toprint(-1, content, content_size));
|
2016-10-17 05:58:38 +00:00
|
|
|
if (write(fd, content, content_size) != (ssize_t)content_size){
|
2016-04-06 05:27:07 +00:00
|
|
|
close(fd);
|
2016-10-17 05:58:38 +00:00
|
|
|
return WHYF_perror("write(%d, %s, %zu)", fd, alloca_str_toprint(content), content_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now the locked temporary has been created, link(2) it to the pidfile's proper name, to ensure
|
|
|
|
// that the pidfile is locked from the instant of its existence. Note that link(2) fails if the
|
|
|
|
// destination path already exists. This logic prevents racing with other processes deleting
|
|
|
|
// stale (unlocked) pidfiles. If the link(2) fails the first time because the pidfile already
|
|
|
|
// exists, then if the existent pidfile is locked, there is another daemon running, so bail out.
|
|
|
|
// If the existent pidfile is not locked, then it is stale, so delete it and re-try the link.
|
|
|
|
unsigned int tries = 0;
|
2016-10-20 02:43:05 +00:00
|
|
|
while (1) {
|
2016-10-25 05:21:31 +00:00
|
|
|
DEBUGF(server, "link(%s, %s)", alloca_str_toprint(tmpfile_path), alloca_str_toprint(pidfile_path));
|
2016-10-20 02:43:05 +00:00
|
|
|
if (link(tmpfile_path, pidfile_path) != -1)
|
|
|
|
break;
|
2016-10-17 05:58:38 +00:00
|
|
|
if (errno == EEXIST && ++tries < 2) {
|
|
|
|
struct pid_tid id = read_pidfile(pidfile_path);
|
|
|
|
if (id.pid == -1) {
|
2016-10-25 05:21:31 +00:00
|
|
|
DEBUGF(server, "Unlinking stale pidfile %s", pidfile_path);
|
2016-10-17 05:58:38 +00:00
|
|
|
unlink(pidfile_path);
|
|
|
|
} else if (id.pid > 0) {
|
|
|
|
INFOF("Another daemon is running, pid=%d tid=%d", id.pid, id.tid);
|
|
|
|
return 1;
|
|
|
|
}
|
2016-10-20 02:43:05 +00:00
|
|
|
} else {
|
2017-05-03 02:30:23 +00:00
|
|
|
WARNF_perror("Cannot link temporary pidfile %s to %s", tmpfile_path, pidfile_path);
|
|
|
|
// Android 6 wont let us link, giving a permission error (sigh), lets just rename it then
|
|
|
|
if (rename(tmpfile_path, pidfile_path)==-1){
|
|
|
|
WHYF_perror("Cannot link or rename temporary pidfile %s to %s", tmpfile_path, pidfile_path);
|
|
|
|
close(fd);
|
|
|
|
unlink(tmpfile_path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
2016-10-17 05:58:38 +00:00
|
|
|
}
|
2016-04-06 05:27:07 +00:00
|
|
|
}
|
2016-10-20 02:43:05 +00:00
|
|
|
DEBUGF(server, "Created pidfile %s", pidfile_path);
|
2016-04-06 05:27:07 +00:00
|
|
|
|
2016-10-17 05:58:38 +00:00
|
|
|
// The link was successful, so delete the temporary pidfile but leave the pid file open so that
|
|
|
|
// the lock remains held!
|
|
|
|
unlink(tmpfile_path);
|
2016-04-06 05:27:07 +00:00
|
|
|
server_pidfd = fd;
|
2014-06-11 05:24:31 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-03-26 12:05:34 +00:00
|
|
|
int server_write_proc_state(const char *path, const char *fmt, ...)
|
2014-02-25 02:12:36 +00:00
|
|
|
{
|
2018-05-31 15:42:35 +00:00
|
|
|
// Only a running server process/thread may modify the proc files.
|
|
|
|
assert(serverMode != SERVER_NOT_RUNNING);
|
|
|
|
|
2014-02-25 02:12:36 +00:00
|
|
|
char path_buf[400];
|
|
|
|
if (get_proc_path(path, path_buf, sizeof path_buf)==-1)
|
|
|
|
return -1;
|
|
|
|
|
2018-03-26 12:05:34 +00:00
|
|
|
// Create the directory that contains the path, if it does not already exist.
|
2014-02-25 02:12:36 +00:00
|
|
|
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[]
|
2018-03-26 12:05:34 +00:00
|
|
|
if (emkdirs_info(dir, 0700) == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// Format the file's new content in a local buffer on the stack.
|
|
|
|
strbuf sb;
|
|
|
|
STRBUF_ALLOCA_FIT(sb, 1024, strbuf_va_printf(sb, fmt));
|
2014-02-24 06:17:16 +00:00
|
|
|
|
2018-03-26 12:05:34 +00:00
|
|
|
// Overwrite the file, creating it if necessary, using a single write(2) system call, followed by
|
|
|
|
// a ftruncate(2) system call (in case the file already existed and was longer than its new
|
|
|
|
// content). This allows potential race conditions to be avoided for files that are overwritten
|
|
|
|
// while the server runs, as long as the written contents are always the same size, or always
|
|
|
|
// contain a terminating sequence (eg, newline).
|
|
|
|
int fd = open(path_buf, O_CREAT | O_WRONLY, 0700);
|
|
|
|
if (fd == -1)
|
|
|
|
return WHYF_perror("open(%s, O_CREAT|O_WRONLY, 0700)", alloca_str_toprint(path_buf));
|
|
|
|
int ret = write_all(fd, strbuf_str(sb), strbuf_len(sb));
|
|
|
|
if (ret != -1 && (ret = ftruncate(fd, strbuf_len(sb)) == -1))
|
|
|
|
ret = WHYF_perror("ftruncate(%s, %zu)", alloca_str_toprint(path_buf), strbuf_len(sb));
|
|
|
|
close(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int server_unlink_proc_state(const char *path)
|
|
|
|
{
|
2018-05-31 15:42:35 +00:00
|
|
|
// Only a running server process/thread may modify the proc files.
|
|
|
|
assert(serverMode != SERVER_NOT_RUNNING);
|
|
|
|
|
2018-03-26 12:05:34 +00:00
|
|
|
char path_buf[400];
|
|
|
|
if (get_proc_path(path, path_buf, sizeof path_buf)==-1)
|
|
|
|
return -1;
|
|
|
|
if (unlink(path) == -1 && errno != ENOENT)
|
|
|
|
return WHYF_perror("unlink(%s)", alloca_str_toprint(path));
|
2014-02-24 06:17:16 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-12 06:38:26 +00:00
|
|
|
static int server_get_proc_state(const char *path, char *buff, size_t buff_len)
|
2014-02-25 02:12:36 +00:00
|
|
|
{
|
|
|
|
char path_buf[400];
|
|
|
|
if (get_proc_path(path, path_buf, sizeof path_buf)==-1)
|
|
|
|
return -1;
|
|
|
|
FILE *f = fopen(path_buf, "r");
|
2018-03-26 12:05:34 +00:00
|
|
|
if (!f) {
|
|
|
|
if (errno != ENOENT)
|
|
|
|
return WHYF_perror("fopen(%s)", alloca_str_toprint(path_buf));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int ret = 0;
|
|
|
|
errno = 0; // fgets() does not set errno on end-of-file
|
|
|
|
if (!fgets(buff, buff_len, f)) {
|
|
|
|
if (errno)
|
|
|
|
ret = WHYF_perror("fgets from %s", alloca_str_toprint(path_buf));
|
|
|
|
else
|
|
|
|
ret = 1;
|
|
|
|
}
|
2014-02-25 02:12:36 +00:00
|
|
|
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)
|
|
|
|
{
|
2017-09-12 05:54:10 +00:00
|
|
|
if (serverMode == SERVER_CLOSING){
|
2018-05-31 15:42:35 +00:00
|
|
|
// All shutdown triggers should unschedule their respective alarms. Once there are no alarms
|
|
|
|
// left, the fd_poll2() in server_loop() will return zero.
|
2017-09-12 05:54:10 +00:00
|
|
|
CALL_TRIGGER(shutdown);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2016-01-11 05:53:32 +00:00
|
|
|
if (alarm){
|
|
|
|
time_ms_t now = gettime_ms();
|
|
|
|
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. */
|
Rewrite logging system
Rename the logging primitive functions and utility functions, prefixing
all with 'serval_log', eg: logMessage() -> serval_logf() etc.
Add an XPRINTF xhexdump() function and use it to implement the
serval_log_hexdump() utility, renamed from dump(). Add macros
WHY_dump(), WARN_dump(), HINT_dump() and DEBUG_dump(), and use them
everywhere.
Remove the 'log.console.dump_config' and 'log.file.dump_config'
configuration options; configuration is now dumped in every log prolog.
The logging system now constructs the log prolog by invoking the new
'log_prolog' trigger, so that it no longer depends on the version string
and configuration system. Any system that wants to present a message in
the log prolog can define its own trigger, which calls standard log
primitives to print the message.
Split the logging system into a front-end (log.c) that provides the
logging primitives and is independent of the configuration system, and a
set of back-end "outputters" (log_output_console.c, log_output_file.c,
log_output_android.c) that may depend on the configuration system and
are decoupled from the front-end using the 'logoutput' link section.
These log outputters are explicitly linked into executables by the
Makefile rules, but could also be linked in using USE_FEATURE(). The
USE_FEATURE() calls have _not_ been added to servald_features.c, so that
different daemon executables can be built with the same feature set but
different log outputs.
2017-11-29 13:34:54 +00:00
|
|
|
serval_log_close();
|
2014-04-16 08:36:27 +00:00
|
|
|
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. */
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(watchdog, "STARTED WATCHDOG pid=%u executable=%s argv=[%s]",
|
|
|
|
watchdog_pid,
|
|
|
|
alloca_str_toprint(config.server.watchdog.executable),
|
|
|
|
strbuf_str(argv_sb)
|
|
|
|
);
|
2014-04-16 08:36:27 +00:00
|
|
|
do { _exit(0); } while (1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
do { _exit(-1); } while (1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case -1:
|
|
|
|
/* child fork failed */
|
|
|
|
WHY_perror("fork");
|
|
|
|
break;
|
|
|
|
}
|
2017-09-12 05:54:10 +00:00
|
|
|
if (alarm) {
|
|
|
|
time_ms_t now = gettime_ms();
|
|
|
|
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
|
|
|
{
|
2018-04-10 01:20:06 +00:00
|
|
|
if (!config.rhizome.enable || !rhizome_database.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
|
|
|
}
|
|
|
|
|
2016-10-14 06:58:55 +00:00
|
|
|
static void server_on_config_change()
|
2014-04-29 05:34:20 +00:00
|
|
|
{
|
2018-06-01 08:10:09 +00:00
|
|
|
if (serverMode == SERVER_NOT_RUNNING)
|
2014-05-29 07:11:27 +00:00
|
|
|
return;
|
2018-06-01 08:10:09 +00:00
|
|
|
|
2014-05-29 07:11:27 +00:00
|
|
|
time_ms_t now = gettime_ms();
|
2017-09-12 05:54:10 +00:00
|
|
|
|
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
|
|
|
|
2015-09-28 00:19:29 +00:00
|
|
|
// Open the Rhizome database immediately if Rhizome is enabled and close it if disabled; this
|
|
|
|
// cannot be deferred because is_rhizome_http_enabled() only returns true if the database is open.
|
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);
|
2018-04-10 01:20:06 +00:00
|
|
|
}else if(rhizome_database.db){
|
2014-06-06 04:09:50 +00:00
|
|
|
rhizome_close_db();
|
|
|
|
}
|
2014-04-29 05:34:20 +00:00
|
|
|
}
|
2017-09-12 05:54:10 +00:00
|
|
|
DEFINE_TRIGGER(conf_change, server_on_config_change);
|
2014-04-29 05:34:20 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-12 05:54:10 +00:00
|
|
|
void server_close(){
|
|
|
|
if (serverMode != SERVER_RUNNING)
|
|
|
|
return;
|
|
|
|
|
|
|
|
DEBUGF(server,"Graceful shutdown");
|
|
|
|
|
2018-05-31 15:42:35 +00:00
|
|
|
// Cause the next server_config_reload() alarm to invoke the "shutdown" trigger, which in turn
|
|
|
|
// will cause an orderly exit from server_loop().
|
2017-09-12 05:54:10 +00:00
|
|
|
serverMode = SERVER_CLOSING;
|
|
|
|
|
2018-05-31 15:42:35 +00:00
|
|
|
// Schedule the server_config_reload() alarm to go off immediately.
|
2017-09-12 05:54:10 +00:00
|
|
|
time_ms_t now = gettime_ms();
|
|
|
|
RESCHEDULE(&ALARM_STRUCT(server_config_reload),
|
|
|
|
now,
|
|
|
|
now,
|
|
|
|
TIME_MS_NEVER_WILL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void server_stop_alarms()
|
|
|
|
{
|
|
|
|
DEBUGF(server,"Stopping alarms");
|
|
|
|
unschedule(&ALARM_STRUCT(fd_periodicstats));
|
|
|
|
unschedule(&ALARM_STRUCT(server_watchdog));
|
|
|
|
unschedule(&ALARM_STRUCT(server_config_reload));
|
|
|
|
unschedule(&ALARM_STRUCT(rhizome_clean_db));
|
|
|
|
}
|
|
|
|
DEFINE_TRIGGER(shutdown, server_stop_alarms);
|
|
|
|
|
2014-06-11 05:24:31 +00:00
|
|
|
static void serverCleanUp()
|
2012-05-08 02:49:52 +00:00
|
|
|
{
|
2016-10-12 06:38:26 +00:00
|
|
|
assert(serverMode != SERVER_NOT_RUNNING);
|
|
|
|
INFOF("Server cleaning up");
|
2017-09-12 05:54:10 +00:00
|
|
|
|
|
|
|
// release alarms, in case we aborted without attempting a graceful close
|
|
|
|
server_stop_alarms();
|
|
|
|
|
2014-06-11 05:24:31 +00:00
|
|
|
rhizome_close_db();
|
2016-06-15 07:38:25 +00:00
|
|
|
release_my_subscriber();
|
2017-09-12 05:54:10 +00:00
|
|
|
|
2016-10-12 06:38:26 +00:00
|
|
|
serverMode = SERVER_NOT_RUNNING;
|
2014-02-25 02:12:36 +00:00
|
|
|
clean_proc();
|
2012-05-08 02:49:52 +00:00
|
|
|
}
|
|
|
|
|
2016-10-12 06:38:26 +00:00
|
|
|
static void signal_handler(int signum)
|
2012-05-09 09:36:44 +00:00
|
|
|
{
|
2016-10-12 06:38:26 +00:00
|
|
|
switch (signum) {
|
2015-06-01 07:16:21 +00:00
|
|
|
case SIGIO:
|
|
|
|
// noop to break out of poll
|
|
|
|
return;
|
2012-05-09 09:36:44 +00:00
|
|
|
case SIGHUP:
|
|
|
|
case SIGINT:
|
2016-10-12 06:38:26 +00:00
|
|
|
switch (serverMode) {
|
|
|
|
case SERVER_RUNNING:
|
|
|
|
// Trigger the server to close gracefully after any current alarm has completed.
|
|
|
|
INFOF("Caught signal %s -- attempting clean shutdown", alloca_signal_name(signum));
|
2017-09-12 05:54:10 +00:00
|
|
|
server_close();
|
2016-10-12 06:38:26 +00:00
|
|
|
return;
|
|
|
|
case SERVER_CLOSING:
|
|
|
|
// If a second signal is received before the server has gracefully shut down, then forcibly
|
|
|
|
// terminate it immediately. If the server is running in a thread, then this will only call
|
|
|
|
// serverCleanUp() if the signal was received by the same thread that is running the server,
|
|
|
|
// because serverMode is thread-local. The zero exit status indicates a clean shutdown. So
|
|
|
|
// the "stop" command must send SIGHUP to the correct thread.
|
2017-09-12 05:54:10 +00:00
|
|
|
WHYF("Caught signal %s -- forced shutdown", alloca_signal_name(signum));
|
|
|
|
list_alarms(LOG_LEVEL_ERROR);
|
2016-10-12 06:38:26 +00:00
|
|
|
serverCleanUp();
|
|
|
|
exit(0);
|
|
|
|
case SERVER_NOT_RUNNING:
|
|
|
|
// If this thread is not running a server, then treat the signal as immediately fatal.
|
|
|
|
break;
|
2014-05-29 07:11:27 +00:00
|
|
|
}
|
2016-10-12 06:38:26 +00:00
|
|
|
// fall through...
|
2014-05-29 07:11:27 +00:00
|
|
|
default:
|
2016-10-12 06:38:26 +00:00
|
|
|
LOGF(LOG_LEVEL_FATAL, "Caught signal %s", alloca_signal_name(signum));
|
2014-05-29 07:11:27 +00:00
|
|
|
dump_stack(LOG_LEVEL_FATAL);
|
2016-10-12 06:38:26 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exit with a status code indicating the caught signal. This involves removing the signal
|
|
|
|
// handler for the caught signal then re-sending the same signal to ourself.
|
|
|
|
struct sigaction sig;
|
|
|
|
bzero(&sig, sizeof sig);
|
|
|
|
sig.sa_flags = 0;
|
|
|
|
sig.sa_handler = SIG_DFL;
|
|
|
|
sigemptyset(&sig.sa_mask);
|
|
|
|
sigaction(signum, &sig, NULL);
|
|
|
|
kill(getpid(), signum);
|
|
|
|
|
|
|
|
// Just in case...
|
2018-06-01 08:10:09 +00:00
|
|
|
FATALF("Sending %s to self (pid=%d) did not cause exit", alloca_signal_name(signum), getpid());
|
2016-10-12 06:38:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cli_server_details(struct cli_context *context, const struct pid_tid *id)
|
|
|
|
{
|
|
|
|
const char *ipath = instance_path();
|
|
|
|
if (ipath) {
|
|
|
|
cli_field_name(context, "instancepath", ":");
|
|
|
|
cli_put_string(context, ipath, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
cli_field_name(context, "pidfile", ":");
|
|
|
|
cli_put_string(context, server_pidfile_path(), "\n");
|
|
|
|
cli_field_name(context, "status", ":");
|
|
|
|
cli_put_string(context, id->pid > 0 ? "running" : "stopped", "\n");
|
|
|
|
|
|
|
|
if (id->pid > 0) {
|
|
|
|
cli_field_name(context, "pid", ":");
|
|
|
|
cli_put_long(context, id->pid, "\n");
|
|
|
|
if (id->tid > 0 && id->tid != id->pid) {
|
|
|
|
cli_field_name(context, "tid", ":");
|
|
|
|
cli_put_long(context, id->tid, "\n");
|
|
|
|
}
|
|
|
|
char buff[256];
|
2018-03-26 12:05:34 +00:00
|
|
|
if (server_get_proc_state("primary_sid", buff, sizeof buff) == 0){
|
|
|
|
cli_field_name(context, "primary_sid", ":");
|
|
|
|
cli_put_string(context, buff, "\n");
|
|
|
|
}
|
|
|
|
if (server_get_proc_state("http_port", buff, sizeof buff) == 0){
|
2016-10-12 06:38:26 +00:00
|
|
|
cli_field_name(context, "http_port", ":");
|
|
|
|
cli_put_string(context, buff, "\n");
|
|
|
|
}
|
2018-03-26 12:05:34 +00:00
|
|
|
if (server_get_proc_state("mdp_inet_port", buff, sizeof buff) == 0){
|
2016-10-12 06:38:26 +00:00
|
|
|
cli_field_name(context, "mdp_inet_port", ":");
|
|
|
|
cli_put_string(context, buff, "\n");
|
|
|
|
}
|
2012-05-09 09:36:44 +00:00
|
|
|
}
|
2012-07-26 09:01:23 +00:00
|
|
|
}
|
2014-08-25 04:54:00 +00:00
|
|
|
|
2018-05-31 15:42:35 +00:00
|
|
|
DEFINE_CMD(app_server_start, 0,
|
2014-08-25 04:54:00 +00:00
|
|
|
"Start daemon with instance path from SERVALINSTANCE_PATH environment variable.",
|
2016-06-15 07:38:25 +00:00
|
|
|
"start" KEYRING_PIN_OPTIONS, "[--seed]", "[foreground|exec <path>]");
|
2014-08-25 04:54:00 +00:00
|
|
|
static int app_server_start(const struct cli_parsed *parsed, struct cli_context *context)
|
|
|
|
{
|
|
|
|
IN();
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUG_cli_parsed(verbose, parsed);
|
2014-08-25 04:54:00 +00:00
|
|
|
/* Process optional arguments */
|
|
|
|
int cpid=-1;
|
|
|
|
const char *execpath;
|
|
|
|
if (cli_arg(parsed, "exec", &execpath, cli_absolute_path, NULL) == -1)
|
|
|
|
RETURN(-1);
|
2016-06-15 07:38:25 +00:00
|
|
|
int seed = cli_arg(parsed, "--seed", NULL, NULL, NULL) == 0;
|
2014-08-25 04:54:00 +00:00
|
|
|
int foregroundP = cli_arg(parsed, "foreground", NULL, NULL, NULL) == 0;
|
Switch to feature-driven linking
This introduces a new way of linking Serval executables and dynamic
libraries from static libraries like libservald.a -- called
"feature-driven" linking.
The Makefile now links servald and serval-tests from libservald.a,
rather than from an explicit list of object (.o) files. Thanks to the
section-based method for registering functions such as HTTP handlers,
CLI commands and MDP handlers, these object files had become
"stand-alone" and hence were no longer included in the link because
there was no unresolved reference that required them to be linked in.
The new "feature.h" provides the DECLARE_FEATURE(name) macro that each
stand-alone source file uses to declare the named feature(s) it
provides. Each executable can call the USE_FEATURE(name) macro in any
of its explicitly-linked source files to cause the corresponding
object(s) to be included in the link, eg, servald_features.c.
The DEFINE_BINDING() macro has been extended so that every individual
MDP binding is given a feature name based on its port number macro, eg,
"mdp_binding_MDP_PORT_ECHO".
Some features have been factored into their own separate source files so
they can be omitted or included in a build independently of each other:
- the MDP bindings for MDP_PORT_DNALOOKUP, MDP_PORT_ECHO,
MDP_PORT_TRACE, MDP_PORT_KEYMAPREQUEST, MDP_PORT_RHIZOME_xxx,
MDP_PORT_PROBE, MDP_PORT_STUN, MDP_PORT_STUNREQ
- the CLI "log" and "echo" commands
- the CLI "rhizome direct" command
The JNI source files are only compiled if the <jni.h> header is present,
otherwise they are omitted from libservald.so.
2016-10-13 02:58:23 +00:00
|
|
|
if (config.interfaces.ac == 0)
|
|
|
|
NOWHENCE(WARN("No network interfaces configured (empty 'interfaces' config option)"));
|
2014-08-25 04:54:00 +00:00
|
|
|
/* Create the instance directory if it does not yet exist */
|
|
|
|
if (create_serval_instance_dir() == -1)
|
|
|
|
RETURN(-1);
|
2018-05-31 15:42:35 +00:00
|
|
|
// Work out the Process and Thread IDs of any currently running server process, by reading an
|
|
|
|
// existing pidfile.
|
2016-10-12 06:38:26 +00:00
|
|
|
struct pid_tid id = get_server_pid_tid();
|
|
|
|
if (id.pid < 0)
|
2014-08-25 04:54:00 +00:00
|
|
|
RETURN(-1);
|
|
|
|
int ret = -1;
|
|
|
|
// If the pidfile identifies this process, it probably means we are re-spawning after a SEGV, so
|
|
|
|
// go ahead and do the fork/exec.
|
2016-10-12 06:38:26 +00:00
|
|
|
if (id.pid > 0 && id.pid != getpid()) {
|
|
|
|
WARNF("Server already running (pid=%d)", id.pid);
|
2014-08-25 04:54:00 +00:00
|
|
|
ret = 10;
|
|
|
|
} else {
|
|
|
|
if (foregroundP)
|
|
|
|
INFOF("Foreground server process %s", execpath ? execpath : "without exec");
|
|
|
|
else
|
|
|
|
INFOF("Starting background server %s", execpath ? execpath : "without exec");
|
|
|
|
/* Start the Serval process. All server settings will be read by the server process from the
|
|
|
|
instance directory when it starts up. */
|
|
|
|
// Open the keyring and ensure it contains at least one unlocked identity.
|
|
|
|
keyring = keyring_open_instance_cli(parsed);
|
|
|
|
if (!keyring)
|
|
|
|
RETURN(WHY("Could not open keyring file"));
|
2016-06-15 07:38:25 +00:00
|
|
|
if (seed && !keyring->identities){
|
|
|
|
if (keyring_create_identity(keyring, "")==NULL){
|
|
|
|
ret = WHY("Could not create new identity");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
keyring_commit(keyring);
|
2014-08-25 04:54:00 +00:00
|
|
|
}
|
|
|
|
if (foregroundP) {
|
|
|
|
ret = server();
|
2016-10-12 06:38:26 +00:00
|
|
|
// Warning: The server is not rigorous about freeing all memory it allocates, so to avoid
|
|
|
|
// memory leaks, the caller should exit() immediately.
|
2014-08-25 04:54:00 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
const char *dir = getenv("SERVALD_SERVER_CHDIR");
|
|
|
|
if (!dir)
|
|
|
|
dir = config.server.chdir;
|
|
|
|
switch (cpid = fork()) {
|
|
|
|
case -1:
|
|
|
|
/* Main process. Fork failed. There is no child process. */
|
|
|
|
WHY_perror("fork");
|
|
|
|
goto exit;
|
|
|
|
case 0: {
|
|
|
|
/* Child process. Fork then exit, to disconnect daemon from parent process, so that
|
|
|
|
when daemon exits it does not live on as a zombie. N.B. On Android, do not return from
|
|
|
|
within this process; that will unroll the JNI call stack and cause havoc -- call _exit()
|
|
|
|
instead (not exit(), because we want to avoid any Java atexit(3) callbacks as well). If
|
|
|
|
_exit() is used on non-Android systems, then source code coverage does not get reported,
|
|
|
|
because it relies on an atexit() callback to write the accumulated counters into .gcda
|
|
|
|
files. */
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUG(verbose, "Child Process");
|
2014-08-25 04:54:00 +00:00
|
|
|
// Ensure that all stdio streams are flushed before forking, so that if a child calls
|
|
|
|
// exit(), it will not result in any buffered output being written twice to the file
|
|
|
|
// descriptor.
|
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
|
|
|
switch (fork()) {
|
|
|
|
case -1:
|
2014-09-08 01:54:05 +00:00
|
|
|
exit(WHY_perror("fork"));
|
2014-08-25 04:54:00 +00:00
|
|
|
case 0: {
|
|
|
|
/* Grandchild process. Close logfile (so that it gets re-opened again on demand, with
|
|
|
|
our own file pointer), disable logging to stderr (about to get redirected to
|
|
|
|
/dev/null), disconnect from current directory, disconnect standard I/O streams, and
|
|
|
|
start a new process session so that if we are being started by an adb shell session
|
|
|
|
on an Android device, then we don't receive a SIGHUP when the adb shell process ends.
|
|
|
|
*/
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUG(verbose, "Grand-Child Process, reopening log");
|
Rewrite logging system
Rename the logging primitive functions and utility functions, prefixing
all with 'serval_log', eg: logMessage() -> serval_logf() etc.
Add an XPRINTF xhexdump() function and use it to implement the
serval_log_hexdump() utility, renamed from dump(). Add macros
WHY_dump(), WARN_dump(), HINT_dump() and DEBUG_dump(), and use them
everywhere.
Remove the 'log.console.dump_config' and 'log.file.dump_config'
configuration options; configuration is now dumped in every log prolog.
The logging system now constructs the log prolog by invoking the new
'log_prolog' trigger, so that it no longer depends on the version string
and configuration system. Any system that wants to present a message in
the log prolog can define its own trigger, which calls standard log
primitives to print the message.
Split the logging system into a front-end (log.c) that provides the
logging primitives and is independent of the configuration system, and a
set of back-end "outputters" (log_output_console.c, log_output_file.c,
log_output_android.c) that may depend on the configuration system and
are decoupled from the front-end using the 'logoutput' link section.
These log outputters are explicitly linked into executables by the
Makefile rules, but could also be linked in using USE_FEATURE(). The
USE_FEATURE() calls have _not_ been added to servald_features.c, so that
different daemon executables can be built with the same feature set but
different log outputs.
2017-11-29 13:34:54 +00:00
|
|
|
serval_log_close();
|
2014-08-25 04:54:00 +00:00
|
|
|
int fd;
|
|
|
|
if ((fd = open("/dev/null", O_RDWR, 0)) == -1)
|
2014-09-08 01:54:05 +00:00
|
|
|
exit(WHY_perror("open(\"/dev/null\")"));
|
2014-08-25 04:54:00 +00:00
|
|
|
if (setsid() == -1)
|
2014-09-08 01:54:05 +00:00
|
|
|
exit(WHY_perror("setsid"));
|
2014-08-25 04:54:00 +00:00
|
|
|
if (chdir(dir) == -1)
|
2014-09-08 01:54:05 +00:00
|
|
|
exit(WHYF_perror("chdir(%s)", alloca_str_toprint(dir)));
|
|
|
|
if (dup2(fd, STDIN_FILENO) == -1)
|
|
|
|
exit(WHYF_perror("dup2(%d,stdin)", fd));
|
|
|
|
if (dup2(fd, STDOUT_FILENO) == -1)
|
|
|
|
exit(WHYF_perror("dup2(%d,stdout)", fd));
|
Rewrite logging system
Rename the logging primitive functions and utility functions, prefixing
all with 'serval_log', eg: logMessage() -> serval_logf() etc.
Add an XPRINTF xhexdump() function and use it to implement the
serval_log_hexdump() utility, renamed from dump(). Add macros
WHY_dump(), WARN_dump(), HINT_dump() and DEBUG_dump(), and use them
everywhere.
Remove the 'log.console.dump_config' and 'log.file.dump_config'
configuration options; configuration is now dumped in every log prolog.
The logging system now constructs the log prolog by invoking the new
'log_prolog' trigger, so that it no longer depends on the version string
and configuration system. Any system that wants to present a message in
the log prolog can define its own trigger, which calls standard log
primitives to print the message.
Split the logging system into a front-end (log.c) that provides the
logging primitives and is independent of the configuration system, and a
set of back-end "outputters" (log_output_console.c, log_output_file.c,
log_output_android.c) that may depend on the configuration system and
are decoupled from the front-end using the 'logoutput' link section.
These log outputters are explicitly linked into executables by the
Makefile rules, but could also be linked in using USE_FEATURE(). The
USE_FEATURE() calls have _not_ been added to servald_features.c, so that
different daemon executables can be built with the same feature set but
different log outputs.
2017-11-29 13:34:54 +00:00
|
|
|
/* Redirect standard error to the current log file, so that any diagnostic messages
|
|
|
|
* printed directly to stderr by libraries or child processes will end up being captured
|
|
|
|
* in a log file. If standard error is not redirected, then simply direct it to
|
|
|
|
* /dev/null.
|
|
|
|
*/
|
|
|
|
if (!serval_log_capture_fd(STDERR_FILENO) && dup2(fd, STDERR_FILENO) == -1)
|
2014-09-08 01:54:05 +00:00
|
|
|
exit(WHYF_perror("dup2(%d,stderr)", fd));
|
Rewrite logging system
Rename the logging primitive functions and utility functions, prefixing
all with 'serval_log', eg: logMessage() -> serval_logf() etc.
Add an XPRINTF xhexdump() function and use it to implement the
serval_log_hexdump() utility, renamed from dump(). Add macros
WHY_dump(), WARN_dump(), HINT_dump() and DEBUG_dump(), and use them
everywhere.
Remove the 'log.console.dump_config' and 'log.file.dump_config'
configuration options; configuration is now dumped in every log prolog.
The logging system now constructs the log prolog by invoking the new
'log_prolog' trigger, so that it no longer depends on the version string
and configuration system. Any system that wants to present a message in
the log prolog can define its own trigger, which calls standard log
primitives to print the message.
Split the logging system into a front-end (log.c) that provides the
logging primitives and is independent of the configuration system, and a
set of back-end "outputters" (log_output_console.c, log_output_file.c,
log_output_android.c) that may depend on the configuration system and
are decoupled from the front-end using the 'logoutput' link section.
These log outputters are explicitly linked into executables by the
Makefile rules, but could also be linked in using USE_FEATURE(). The
USE_FEATURE() calls have _not_ been added to servald_features.c, so that
different daemon executables can be built with the same feature set but
different log outputs.
2017-11-29 13:34:54 +00:00
|
|
|
if (fd > STDERR_FILENO)
|
2014-08-25 04:54:00 +00:00
|
|
|
(void)close(fd);
|
|
|
|
/* The execpath option is provided so that a JNI call to "start" can be made which
|
|
|
|
creates a new server daemon process with the correct argv[0]. Otherwise, the servald
|
|
|
|
process appears as a process with argv[0] = "org.servalproject". */
|
|
|
|
if (execpath) {
|
|
|
|
/* Need the cast on Solaris because it defines NULL as 0L and gcc doesn't see it as a
|
|
|
|
sentinal. */
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(verbose, "Calling execl %s start foreground", execpath);
|
2014-09-08 01:54:05 +00:00
|
|
|
execl(execpath, "servald", "start", "foreground", (void *)NULL);
|
|
|
|
WHYF_perror("execl(%s, \"servald\", \"start\", \"foreground\")", alloca_str_toprint(execpath));
|
|
|
|
exit(-1);
|
2014-08-25 04:54:00 +00:00
|
|
|
}
|
2014-09-08 01:54:05 +00:00
|
|
|
exit(server());
|
|
|
|
// UNREACHABLE
|
2014-08-25 04:54:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO wait for server_write_pid() to signal more directly?
|
2014-09-08 01:54:05 +00:00
|
|
|
exit(0); // Main process is waitpid()-ing for this.
|
2014-08-25 04:54:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Main process. Wait for the child process to fork the grandchild and exit. */
|
|
|
|
waitpid(cpid, NULL, 0);
|
|
|
|
/* Allow a few seconds for the grandchild process to report for duty. */
|
|
|
|
time_ms_t timeout = gettime_ms() + 5000;
|
|
|
|
do {
|
|
|
|
sleep_ms(200); // 5 Hz
|
2016-10-12 06:38:26 +00:00
|
|
|
} while ((id = get_server_pid_tid()).pid == 0 && gettime_ms() < timeout);
|
|
|
|
if (id.pid == -1)
|
2014-08-25 04:54:00 +00:00
|
|
|
goto exit;
|
2016-10-12 06:38:26 +00:00
|
|
|
if (id.pid == 0) {
|
2014-08-25 04:54:00 +00:00
|
|
|
WHY("Server process did not start");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
}
|
2016-10-12 06:38:26 +00:00
|
|
|
cli_server_details(context, &id);
|
2014-08-25 04:54:00 +00:00
|
|
|
cli_flush(context);
|
|
|
|
/* Sleep before returning if env var is set. This is used in testing, to simulate the situation
|
|
|
|
on Android phones where the "start" command is invoked via the JNI interface and the calling
|
|
|
|
process does not die.
|
|
|
|
*/
|
|
|
|
const char *post_sleep = getenv("SERVALD_START_POST_SLEEP");
|
|
|
|
if (post_sleep) {
|
|
|
|
time_ms_t milliseconds = atoi(post_sleep);
|
|
|
|
INFOF("Sleeping for %"PRId64" milliseconds", (int64_t) milliseconds);
|
|
|
|
sleep_ms(milliseconds);
|
|
|
|
}
|
|
|
|
exit:
|
|
|
|
keyring_free(keyring);
|
|
|
|
keyring = NULL;
|
|
|
|
RETURN(ret);
|
|
|
|
OUT();
|
|
|
|
}
|
|
|
|
|
|
|
|
DEFINE_CMD(app_server_stop,CLIFLAG_PERMISSIVE_CONFIG,
|
|
|
|
"Stop a running daemon with instance path from SERVALINSTANCE_PATH environment variable.",
|
|
|
|
"stop");
|
|
|
|
static int app_server_stop(const struct cli_parsed *parsed, struct cli_context *context)
|
|
|
|
{
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUG_cli_parsed(verbose, parsed);
|
2016-10-12 06:38:26 +00:00
|
|
|
const struct pid_tid id = get_server_pid_tid();
|
|
|
|
cli_server_details(context, &id);
|
2014-08-25 04:54:00 +00:00
|
|
|
/* Not running, nothing to stop */
|
2016-10-12 06:38:26 +00:00
|
|
|
if (id.pid <= 0)
|
2014-08-25 04:54:00 +00:00
|
|
|
return 1;
|
2016-10-12 06:38:26 +00:00
|
|
|
INFOF("Stopping server (pid=%d)", id.pid);
|
2014-08-25 04:54:00 +00:00
|
|
|
/* Set the stop file and signal the process */
|
2016-10-12 06:38:26 +00:00
|
|
|
unsigned tries = 0;
|
|
|
|
pid_t running = id.pid;
|
|
|
|
while (running == id.pid) {
|
2014-08-25 04:54:00 +00:00
|
|
|
if (tries >= 5) {
|
|
|
|
WHYF("Servald pid=%d (pidfile=%s) did not stop after %d SIGHUP signals",
|
2016-10-12 06:38:26 +00:00
|
|
|
id.pid, server_pidfile_path(), tries);
|
2014-08-25 04:54:00 +00:00
|
|
|
return 253;
|
|
|
|
}
|
|
|
|
++tries;
|
2016-10-12 06:38:26 +00:00
|
|
|
switch (send_signal(&id, SIGHUP)) {
|
|
|
|
case -1:
|
2014-08-25 04:54:00 +00:00
|
|
|
return 252;
|
2016-10-12 06:38:26 +00:00
|
|
|
case 0: {
|
|
|
|
// Allow a few seconds for the process to die.
|
|
|
|
time_ms_t timeout = gettime_ms() + 2000;
|
|
|
|
do
|
|
|
|
sleep_ms(200); // 5 Hz
|
|
|
|
while ((running = get_server_pid_tid().pid) == id.pid && gettime_ms() < timeout);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Process no longer exists. DO NOT call serverCleanUp() (once used to!) because that would
|
|
|
|
// race with a starting server process.
|
|
|
|
running = 0;
|
|
|
|
break;
|
2014-08-25 04:54:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
cli_field_name(context, "tries", ":");
|
|
|
|
cli_put_long(context, tries, "\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEFINE_CMD(app_server_status,CLIFLAG_PERMISSIVE_CONFIG,
|
|
|
|
"Display information about running daemon.",
|
|
|
|
"status");
|
|
|
|
static int app_server_status(const struct cli_parsed *parsed, struct cli_context *context)
|
|
|
|
{
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUG_cli_parsed(verbose, parsed);
|
2016-10-12 06:38:26 +00:00
|
|
|
const struct pid_tid id = get_server_pid_tid();
|
|
|
|
cli_server_details(context, &id);
|
|
|
|
return id.pid > 0 ? 0 : 1;
|
2014-08-25 04:54:00 +00:00
|
|
|
}
|