2023-03-09 02:46:04 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person
|
|
|
|
* obtaining a copy of this software and associated documentation files
|
|
|
|
* (the "Software"), to deal in the Software without restriction,
|
|
|
|
* including without limitation the rights to use, copy, modify, merge,
|
|
|
|
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
* and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
|
|
|
|
#include <Telodendria.h>
|
|
|
|
#include <Memory.h>
|
2023-03-22 17:17:30 +00:00
|
|
|
#include <Config.h>
|
2023-03-09 02:46:04 +00:00
|
|
|
#include <Log.h>
|
|
|
|
#include <HashMap.h>
|
|
|
|
#include <Json.h>
|
|
|
|
#include <HttpServer.h>
|
|
|
|
#include <Matrix.h>
|
|
|
|
#include <Db.h>
|
|
|
|
#include <Cron.h>
|
|
|
|
#include <Uia.h>
|
2023-03-23 02:12:45 +00:00
|
|
|
#include <Util.h>
|
2023-03-09 02:46:04 +00:00
|
|
|
|
2023-03-22 17:00:48 +00:00
|
|
|
static Array *httpServers = NULL;
|
2023-03-09 02:46:04 +00:00
|
|
|
|
|
|
|
static void
|
2023-03-23 16:39:15 +00:00
|
|
|
TelodendriaSignalHandler(int signal)
|
2023-03-09 02:46:04 +00:00
|
|
|
{
|
2023-03-22 17:00:48 +00:00
|
|
|
size_t i;
|
|
|
|
|
2023-03-23 16:39:15 +00:00
|
|
|
switch (signal)
|
2023-03-22 17:00:48 +00:00
|
|
|
{
|
2023-03-23 16:39:15 +00:00
|
|
|
case SIGPIPE:
|
|
|
|
return;
|
|
|
|
case SIGINT:
|
|
|
|
if (!httpServers)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2023-03-23 02:12:45 +00:00
|
|
|
|
2023-03-23 16:39:15 +00:00
|
|
|
for (i = 0; i < ArraySize(httpServers); i++)
|
|
|
|
{
|
|
|
|
HttpServer *server = ArrayGet(httpServers, i);
|
2023-03-24 02:41:01 +00:00
|
|
|
|
2023-03-23 16:39:15 +00:00
|
|
|
HttpServerStop(server);
|
|
|
|
}
|
|
|
|
break;
|
2023-03-22 17:00:48 +00:00
|
|
|
}
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef enum ArgFlag
|
|
|
|
{
|
|
|
|
ARG_VERSION = (1 << 0),
|
|
|
|
ARG_CONFIGTEST = (1 << 1),
|
|
|
|
ARG_VERBOSE = (1 << 2)
|
|
|
|
} ArgFlag;
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int exit = EXIT_SUCCESS;
|
|
|
|
|
|
|
|
/* Arg parsing */
|
|
|
|
int opt;
|
|
|
|
int flags = 0;
|
|
|
|
char *configArg = NULL;
|
|
|
|
|
|
|
|
/* Config file */
|
2023-03-18 14:32:09 +00:00
|
|
|
Stream *configFile = NULL;
|
2023-03-09 02:46:04 +00:00
|
|
|
HashMap *config = NULL;
|
|
|
|
|
|
|
|
/* Program configuration */
|
2023-03-22 17:17:30 +00:00
|
|
|
Config *tConfig = NULL;
|
2023-03-09 02:46:04 +00:00
|
|
|
|
|
|
|
/* User validation */
|
|
|
|
struct passwd *userInfo = NULL;
|
|
|
|
struct group *groupInfo = NULL;
|
|
|
|
|
2023-03-22 17:00:48 +00:00
|
|
|
/* HTTP server management */
|
|
|
|
size_t i;
|
|
|
|
HttpServer *server;
|
|
|
|
|
2023-03-09 02:46:04 +00:00
|
|
|
/* Signal handling */
|
|
|
|
struct sigaction sigAction;
|
|
|
|
|
|
|
|
MatrixHttpHandlerArgs matrixArgs;
|
|
|
|
Cron *cron = NULL;
|
|
|
|
|
|
|
|
memset(&matrixArgs, 0, sizeof(matrixArgs));
|
|
|
|
|
2023-03-22 14:52:04 +00:00
|
|
|
if (!LogConfigGlobal())
|
2023-03-09 02:46:04 +00:00
|
|
|
{
|
|
|
|
printf("Fatal error: unable to allocate memory for logger.\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2023-03-22 14:52:04 +00:00
|
|
|
TelodendriaPrintHeader();
|
2023-03-09 02:46:04 +00:00
|
|
|
|
|
|
|
while ((opt = getopt(argc, argv, "f:Vvn")) != -1)
|
|
|
|
{
|
|
|
|
switch (opt)
|
|
|
|
{
|
|
|
|
case 'f':
|
|
|
|
configArg = optarg;
|
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
flags |= ARG_VERSION;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
flags |= ARG_VERBOSE;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
flags |= ARG_CONFIGTEST;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & ARG_VERBOSE)
|
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
LogConfigLevelSet(LogConfigGlobal(), LOG_DEBUG);
|
2023-03-22 18:29:05 +00:00
|
|
|
MemoryHook(TelodendriaMemoryHook, NULL);
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & ARG_VERSION)
|
|
|
|
{
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!configArg)
|
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_ERR, "No configuration file specified.");
|
2023-03-09 02:46:04 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
else if (strcmp(configArg, "-") == 0)
|
|
|
|
{
|
2023-03-18 14:32:09 +00:00
|
|
|
configFile = StreamStdin();
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-18 14:32:09 +00:00
|
|
|
StreamClose(StreamStdin());
|
2023-03-20 19:23:37 +00:00
|
|
|
|
2023-03-18 14:32:09 +00:00
|
|
|
configFile = StreamOpen(configArg, "r");
|
2023-03-09 02:46:04 +00:00
|
|
|
if (!configFile)
|
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_ERR, "Unable to open configuration file '%s' for reading.", configArg);
|
2023-03-09 02:46:04 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_NOTICE, "Processing configuration file '%s'.", configArg);
|
2023-03-09 02:46:04 +00:00
|
|
|
|
|
|
|
config = JsonDecode(configFile);
|
2023-03-18 14:32:09 +00:00
|
|
|
StreamClose(configFile);
|
2023-03-09 02:46:04 +00:00
|
|
|
|
|
|
|
if (!config)
|
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_ERR, "Syntax error in configuration file.");
|
2023-03-09 02:46:04 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2023-03-22 17:17:30 +00:00
|
|
|
tConfig = ConfigParse(config);
|
2023-03-09 02:46:04 +00:00
|
|
|
JsonFree(config);
|
|
|
|
|
|
|
|
if (!tConfig)
|
|
|
|
{
|
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & ARG_CONFIGTEST)
|
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_INFO, "Configuration is OK.");
|
2023-03-09 02:46:04 +00:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tConfig->logTimestamp || strcmp(tConfig->logTimestamp, "default") != 0)
|
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
LogConfigTimeStampFormatSet(LogConfigGlobal(), tConfig->logTimestamp);
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Free(tConfig->logTimestamp);
|
|
|
|
tConfig->logTimestamp = NULL;
|
|
|
|
}
|
|
|
|
|
2023-03-22 17:17:30 +00:00
|
|
|
if (tConfig->flags & CONFIG_LOG_COLOR)
|
2023-03-09 02:46:04 +00:00
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
LogConfigFlagSet(LogConfigGlobal(), LOG_FLAG_COLOR);
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
LogConfigFlagClear(LogConfigGlobal(), LOG_FLAG_COLOR);
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
|
2023-03-22 14:52:04 +00:00
|
|
|
LogConfigLevelSet(LogConfigGlobal(), flags & ARG_VERBOSE ? LOG_DEBUG : tConfig->logLevel);
|
2023-03-09 02:46:04 +00:00
|
|
|
|
|
|
|
if (chdir(tConfig->dataDir) != 0)
|
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_ERR, "Unable to change into data directory: %s.", strerror(errno));
|
2023-03-09 02:46:04 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_DEBUG, "Changed working directory to: %s", tConfig->dataDir);
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-22 17:17:30 +00:00
|
|
|
if (tConfig->flags & CONFIG_LOG_FILE)
|
2023-03-09 02:46:04 +00:00
|
|
|
{
|
2023-03-18 14:32:09 +00:00
|
|
|
Stream *logFile = StreamOpen("telodendria.log", "a");
|
2023-03-09 02:46:04 +00:00
|
|
|
|
|
|
|
if (!logFile)
|
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_ERR, "Unable to open log file for appending.");
|
2023-03-09 02:46:04 +00:00
|
|
|
exit = EXIT_FAILURE;
|
2023-03-23 02:12:45 +00:00
|
|
|
tConfig->flags &= CONFIG_LOG_STDOUT;
|
2023-03-09 02:46:04 +00:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_INFO, "Logging to the log file. Check there for all future messages.");
|
|
|
|
LogConfigOutputSet(LogConfigGlobal(), logFile);
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
2023-03-22 17:17:30 +00:00
|
|
|
else if (tConfig->flags & CONFIG_LOG_STDOUT)
|
2023-03-09 02:46:04 +00:00
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_DEBUG, "Already logging to standard output.");
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
2023-03-22 17:17:30 +00:00
|
|
|
else if (tConfig->flags & CONFIG_LOG_SYSLOG)
|
2023-03-09 02:46:04 +00:00
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_INFO, "Logging to the syslog. Check there for all future messages.");
|
|
|
|
LogConfigFlagSet(LogConfigGlobal(), LOG_FLAG_SYSLOG);
|
2023-03-09 02:46:04 +00:00
|
|
|
|
|
|
|
openlog("telodendria", LOG_PID | LOG_NDELAY, LOG_DAEMON);
|
|
|
|
/* Always log everything, because the Log API will control what
|
|
|
|
* messages get passed to the syslog */
|
|
|
|
setlogmask(LOG_UPTO(LOG_DEBUG));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_ERR, "Unknown logging method in flags: '%d'", tConfig->flags);
|
|
|
|
Log(LOG_ERR, "This is a programmer error; please report it.");
|
2023-03-09 02:46:04 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_DEBUG, "Configuration:");
|
|
|
|
LogConfigIndent(LogConfigGlobal());
|
|
|
|
Log(LOG_DEBUG, "Server Name: %s", tConfig->serverName);
|
|
|
|
Log(LOG_DEBUG, "Base URL: %s", tConfig->baseUrl);
|
|
|
|
Log(LOG_DEBUG, "Identity Server: %s", tConfig->identityServer);
|
|
|
|
Log(LOG_DEBUG, "Run As: %s:%s", tConfig->uid, tConfig->gid);
|
|
|
|
Log(LOG_DEBUG, "Data Directory: %s", tConfig->dataDir);
|
|
|
|
Log(LOG_DEBUG, "Max Cache: %ld", tConfig->maxCache);
|
|
|
|
Log(LOG_DEBUG, "Flags: %x", tConfig->flags);
|
|
|
|
LogConfigUnindent(LogConfigGlobal());
|
2023-03-09 02:46:04 +00:00
|
|
|
|
|
|
|
/* Arguments to pass into the HTTP handler */
|
|
|
|
matrixArgs.config = tConfig;
|
|
|
|
|
2023-03-22 17:00:48 +00:00
|
|
|
httpServers = ArrayCreate();
|
|
|
|
if (!httpServers)
|
|
|
|
{
|
|
|
|
Log(LOG_ERR, "Error setting up HTTP server.");
|
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2023-03-23 02:12:45 +00:00
|
|
|
/* Bind servers before possibly dropping permissions. */
|
|
|
|
for (i = 0; i < ArraySize(tConfig->servers); i++)
|
|
|
|
{
|
|
|
|
HttpServerConfig *serverCfg = ArrayGet(tConfig->servers, i);
|
|
|
|
|
|
|
|
Log(LOG_DEBUG, "HTTP listener: %lu", i);
|
|
|
|
LogConfigIndent(LogConfigGlobal());
|
|
|
|
Log(LOG_DEBUG, "Port: %hu", serverCfg->port);
|
|
|
|
Log(LOG_DEBUG, "Threads: %u", serverCfg->threads);
|
|
|
|
Log(LOG_DEBUG, "Max Connections: %u", serverCfg->maxConnections);
|
|
|
|
Log(LOG_DEBUG, "Flags: %d", serverCfg->flags);
|
|
|
|
Log(LOG_DEBUG, "TLS Cert: %s", serverCfg->tlsCert);
|
|
|
|
Log(LOG_DEBUG, "TLS Key: %s", serverCfg->tlsKey);
|
|
|
|
LogConfigUnindent(LogConfigGlobal());
|
|
|
|
|
|
|
|
serverCfg->handler = MatrixHttpHandler;
|
|
|
|
serverCfg->handlerArgs = &matrixArgs;
|
|
|
|
|
|
|
|
if (serverCfg->flags & HTTP_FLAG_TLS)
|
|
|
|
{
|
|
|
|
if (!UtilLastModified(serverCfg->tlsCert))
|
|
|
|
{
|
|
|
|
Log(LOG_ERR, "%s: %s", strerror(errno), serverCfg->tlsCert);
|
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!UtilLastModified(serverCfg->tlsKey))
|
|
|
|
{
|
|
|
|
Log(LOG_ERR, "%s: %s", strerror(errno), serverCfg->tlsKey);
|
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
server = HttpServerCreate(serverCfg);
|
|
|
|
if (!server)
|
|
|
|
{
|
|
|
|
Log(LOG_ERR, "Unable to create HTTP server on port %d: %s",
|
|
|
|
serverCfg->port, strerror(errno));
|
|
|
|
|
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
ArrayAdd(httpServers, server);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ArraySize(httpServers))
|
2023-03-09 02:46:04 +00:00
|
|
|
{
|
2023-03-23 02:12:45 +00:00
|
|
|
Log(LOG_ERR, "No valid HTTP listeners specified in the configuration.");
|
2023-03-09 02:46:04 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_DEBUG, "Running as uid:gid: %d:%d.", getuid(), getgid());
|
2023-03-09 02:46:04 +00:00
|
|
|
|
|
|
|
if (tConfig->uid && tConfig->gid)
|
|
|
|
{
|
|
|
|
userInfo = getpwnam(tConfig->uid);
|
|
|
|
groupInfo = getgrnam(tConfig->gid);
|
|
|
|
|
|
|
|
if (!userInfo || !groupInfo)
|
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_ERR, "Unable to locate the user/group specified in the configuration.");
|
2023-03-09 02:46:04 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_DEBUG, "Found user/group information using getpwnam() and getgrnam().");
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_DEBUG, "No user/group info specified in the config.");
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (getuid() == 0)
|
|
|
|
{
|
|
|
|
if (userInfo && groupInfo)
|
|
|
|
{
|
|
|
|
if (setgid(groupInfo->gr_gid) != 0 || setuid(userInfo->pw_uid) != 0)
|
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_ERR, "Unable to set process uid/gid.");
|
2023-03-09 02:46:04 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_DEBUG, "Set uid/gid to %s:%s.", tConfig->uid, tConfig->gid);
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_WARNING, "We are running as root, and we are not dropping to another user");
|
|
|
|
Log(LOG_WARNING, "because none was specified in the configuration file.");
|
|
|
|
Log(LOG_WARNING, "This is probably a security issue.");
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (tConfig->uid && tConfig->gid)
|
|
|
|
{
|
|
|
|
if (getuid() != userInfo->pw_uid || getgid() != groupInfo->gr_gid)
|
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_WARNING, "Not running as the uid/gid specified in the configuration.");
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_DEBUG, "Running as the uid/gid specified in the configuration.");
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* These config values are no longer needed; don't hold them in
|
|
|
|
* memory anymore */
|
|
|
|
Free(tConfig->dataDir);
|
|
|
|
Free(tConfig->uid);
|
|
|
|
Free(tConfig->gid);
|
|
|
|
|
|
|
|
tConfig->dataDir = NULL;
|
|
|
|
tConfig->uid = NULL;
|
|
|
|
tConfig->gid = NULL;
|
|
|
|
|
|
|
|
if (!tConfig->maxCache)
|
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_WARNING, "Database caching is disabled.");
|
|
|
|
Log(LOG_WARNING, "If this is not what you intended, check the config file");
|
|
|
|
Log(LOG_WARNING, "and ensure that maxCache is a valid number of bytes.");
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
matrixArgs.db = DbOpen(".", tConfig->maxCache);
|
|
|
|
|
|
|
|
if (!matrixArgs.db)
|
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_ERR, "Unable to open data directory as a database.");
|
2023-03-09 02:46:04 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
2023-03-23 02:12:45 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Log(LOG_DEBUG, "Opened database.");
|
|
|
|
}
|
2023-03-09 02:46:04 +00:00
|
|
|
|
|
|
|
cron = CronCreate(60 * 1000); /* 1-minute tick */
|
|
|
|
if (!cron)
|
|
|
|
{
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_ERR, "Unable to set up job scheduler.");
|
2023-03-09 02:46:04 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_DEBUG, "Registering jobs...");
|
2023-03-09 02:46:04 +00:00
|
|
|
|
|
|
|
CronEvery(cron, 30 * 60 * 1000, (JobFunc *) UiaCleanup, &matrixArgs);
|
|
|
|
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_NOTICE, "Starting job scheduler...");
|
2023-03-09 02:46:04 +00:00
|
|
|
CronStart(cron);
|
|
|
|
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_NOTICE, "Starting server...");
|
2023-03-09 02:46:04 +00:00
|
|
|
|
2023-03-22 17:00:48 +00:00
|
|
|
for (i = 0; i < ArraySize(httpServers); i++)
|
2023-03-09 02:46:04 +00:00
|
|
|
{
|
2023-03-23 02:12:45 +00:00
|
|
|
HttpServerConfig *serverCfg;
|
|
|
|
|
2023-03-22 17:00:48 +00:00
|
|
|
server = ArrayGet(httpServers, i);
|
2023-03-23 02:12:45 +00:00
|
|
|
serverCfg = HttpServerConfigGet(server);
|
2023-03-22 17:00:48 +00:00
|
|
|
|
|
|
|
if (!HttpServerStart(server))
|
|
|
|
{
|
2023-03-23 02:12:45 +00:00
|
|
|
Log(LOG_ERR, "Unable to start HTTP server %lu on port %hu.", i, serverCfg->port);
|
2023-03-22 17:00:48 +00:00
|
|
|
exit = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log(LOG_DEBUG, "Started HTTP server %lu.", i);
|
2023-03-23 02:12:45 +00:00
|
|
|
Log(LOG_INFO, "Listening on port: %hu", serverCfg->port);
|
2023-03-22 17:00:48 +00:00
|
|
|
}
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sigAction.sa_handler = TelodendriaSignalHandler;
|
|
|
|
sigfillset(&sigAction.sa_mask);
|
|
|
|
sigAction.sa_flags = SA_RESTART;
|
|
|
|
|
2023-03-23 16:39:15 +00:00
|
|
|
#define SIGACTION(sig, act, oact) \
|
|
|
|
if (sigaction(sig, act, oact) < 0) \
|
|
|
|
{ \
|
|
|
|
Log(LOG_ERR, "Unable to install signal handler: %s", #sig); \
|
|
|
|
exit = EXIT_FAILURE; \
|
|
|
|
goto finish; \
|
|
|
|
} \
|
|
|
|
else \
|
|
|
|
{ \
|
|
|
|
Log(LOG_DEBUG, "Installed signal handler: %s", #sig); \
|
2023-03-23 02:12:45 +00:00
|
|
|
}
|
2023-03-09 02:46:04 +00:00
|
|
|
|
2023-03-23 16:39:15 +00:00
|
|
|
SIGACTION(SIGINT, &sigAction, NULL);
|
|
|
|
SIGACTION(SIGPIPE, &sigAction, NULL);
|
|
|
|
|
|
|
|
#undef SIGACTION
|
|
|
|
|
2023-03-22 17:00:48 +00:00
|
|
|
/* Block this thread until the servers are terminated by a signal
|
2023-03-09 02:46:04 +00:00
|
|
|
* handler */
|
2023-03-22 17:00:48 +00:00
|
|
|
for (i = 0; i < ArraySize(httpServers); i++)
|
|
|
|
{
|
|
|
|
server = ArrayGet(httpServers, i);
|
|
|
|
HttpServerJoin(server);
|
2023-03-23 02:12:45 +00:00
|
|
|
Log(LOG_DEBUG, "Joined HTTP server %lu.", i);
|
2023-03-22 17:00:48 +00:00
|
|
|
}
|
2023-03-09 02:46:04 +00:00
|
|
|
|
|
|
|
finish:
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_NOTICE, "Shutting down...");
|
2023-03-22 17:00:48 +00:00
|
|
|
if (httpServers)
|
2023-03-09 02:46:04 +00:00
|
|
|
{
|
2023-03-22 17:00:48 +00:00
|
|
|
for (i = 0; i < ArraySize(httpServers); i++)
|
|
|
|
{
|
2023-03-23 02:12:45 +00:00
|
|
|
Log(LOG_DEBUG, "Freeing HTTP server %lu...", i);
|
2023-03-22 17:00:48 +00:00
|
|
|
server = ArrayGet(httpServers, i);
|
|
|
|
HttpServerStop(server);
|
|
|
|
HttpServerFree(server);
|
2023-03-23 02:12:45 +00:00
|
|
|
Log(LOG_DEBUG, "Freed HTTP server %lu.", i);
|
2023-03-22 17:00:48 +00:00
|
|
|
}
|
|
|
|
ArrayFree(httpServers);
|
2023-03-23 02:12:45 +00:00
|
|
|
Log(LOG_DEBUG, "Freed HTTP servers array.");
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cron)
|
|
|
|
{
|
|
|
|
CronStop(cron);
|
|
|
|
CronFree(cron);
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_DEBUG, "Stopped and freed job scheduler.");
|
2023-03-09 02:46:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DbClose(matrixArgs.db);
|
2023-03-23 02:12:45 +00:00
|
|
|
Log(LOG_DEBUG, "Closed database.");
|
2023-03-09 02:46:04 +00:00
|
|
|
|
2023-03-22 17:17:30 +00:00
|
|
|
ConfigFree(tConfig);
|
2023-03-09 02:46:04 +00:00
|
|
|
|
2023-03-22 14:52:04 +00:00
|
|
|
Log(LOG_DEBUG, "Exiting with code '%d'.", exit);
|
2023-03-18 14:32:09 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Uninstall the memory hook because it uses the Log
|
|
|
|
* API, whose configuration is being freed now, so it
|
|
|
|
* won't work anymore.
|
|
|
|
*/
|
|
|
|
MemoryHook(NULL, NULL);
|
|
|
|
|
2023-03-22 14:52:04 +00:00
|
|
|
LogConfigFree(LogConfigGlobal());
|
2023-03-09 02:46:04 +00:00
|
|
|
|
2023-03-18 14:32:09 +00:00
|
|
|
/* Standard error should never have been opened, but just in case
|
|
|
|
* it was, this doesn't hurt anything. */
|
|
|
|
StreamClose(StreamStderr());
|
|
|
|
|
|
|
|
/* Generate a memory report if any leaks occurred. At this point no
|
|
|
|
* memory should be allocated. */
|
|
|
|
TelodendriaGenerateMemReport();
|
2023-03-09 02:46:04 +00:00
|
|
|
|
2023-03-23 02:12:45 +00:00
|
|
|
/* Free any leaked memory now, just in case the operating system
|
|
|
|
* we're running on won't do it for us. */
|
2023-03-18 14:32:09 +00:00
|
|
|
MemoryFreeAll();
|
2023-03-09 02:46:04 +00:00
|
|
|
return exit;
|
|
|
|
}
|