journal-remote.c revision dad29dff1925a114e20d4eb7b47fca23c4f25fd7
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/***
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2012 Zbigniew Jędrzejewski-Szmek
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering***/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
1e02e182f1e06fcbe389474175de228103be39cbLennart Poettering#include <errno.h>
1e02e182f1e06fcbe389474175de228103be39cbLennart Poettering#include <fcntl.h>
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include <stdio.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <stdlib.h>
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering#include <string.h>
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include <sys/prctl.h>
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen#include <sys/socket.h>
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering#include <sys/stat.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <sys/types.h>
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen#include <unistd.h>
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen#include <getopt.h>
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen#include "sd-daemon.h"
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen#include "journal-file.h"
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen#include "journald-native.h"
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen#include "socket-util.h"
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen#include "mkdir.h"
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen#include "build.h"
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen#include "macro.h"
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen#include "strv.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "fileio.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "conf-parser.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "siphash24.h"
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#ifdef HAVE_GNUTLS
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering#include <gnutls/gnutls.h>
3c0cf502796be355431d4a64d738e75f543aa51dLennart Poettering#endif
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering#include "journal-remote.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "journal-remote-write.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering#define REMOTE_JOURNAL_PATH "/var/log/journal/remote"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#define PRIV_KEY_FILE CERTIFICATE_ROOT "/private/journal-remote.pem"
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering#define CERT_FILE CERTIFICATE_ROOT "/certs/journal-remote.pem"
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering#define TRUST_FILE CERTIFICATE_ROOT "/ca/trusted.pem"
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poetteringstatic char* arg_url = NULL;
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poetteringstatic char* arg_getter = NULL;
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poetteringstatic char* arg_listen_raw = NULL;
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poetteringstatic char* arg_listen_http = NULL;
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poetteringstatic char* arg_listen_https = NULL;
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poetteringstatic char** arg_files = NULL;
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poetteringstatic int arg_compress = true;
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poetteringstatic int arg_seal = false;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic int http_socket = -1, https_socket = -1;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic char** arg_gnutls_log = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic JournalWriteSplitMode arg_split_mode = JOURNAL_WRITE_SPLIT_HOST;
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersenstatic char* arg_output = NULL;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
f4461e5641d53f27d6e76e0607bdaa9c0c58c1f6Lennart Poetteringstatic char *arg_key = NULL;
f4461e5641d53f27d6e76e0607bdaa9c0c58c1f6Lennart Poetteringstatic char *arg_cert = NULL;
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersenstatic char *arg_trust = NULL;
d74fb368b18f0fbd9a4fe6f15691bbea7f3c4a01Tom Gundersenstatic bool arg_trust_all = false;
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/**********************************************************************
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering **********************************************************************
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen **********************************************************************/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poetteringstatic int spawn_child(const char* child, char** argv) {
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering int fd[2];
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering pid_t parent_pid, child_pid;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering int r;
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering if (pipe(fd) < 0) {
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering log_error("Failed to create pager pipe: %m");
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering return -errno;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering }
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering parent_pid = getpid();
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering child_pid = fork();
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering if (child_pid < 0) {
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering r = -errno;
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering log_error("Failed to fork: %m");
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering safe_close_pair(fd);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering return r;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering }
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering /* In the child */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (child_pid == 0) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering r = dup2(fd[1], STDOUT_FILENO);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r < 0) {
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering log_error("Failed to dup pipe to stdout: %m");
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering _exit(EXIT_FAILURE);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering }
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering safe_close_pair(fd);
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering /* Make sure the child goes away when the parent dies */
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _exit(EXIT_FAILURE);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* Check whether our parent died before we were able
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * to set the death signal */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (getppid() != parent_pid)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _exit(EXIT_SUCCESS);
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering execvp(child, argv);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("Failed to exec child %s: %m", child);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _exit(EXIT_FAILURE);
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen }
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen
cab5b05903096e1c9cf5575ccc73f89d15c8db69Lennart Poettering r = close(fd[1]);
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen if (r < 0)
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen log_warning("Failed to close write end of pipe: %m");
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering return fd[0];
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen}
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poetteringstatic int spawn_curl(const char* url) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering char **argv = STRV_MAKE("curl",
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering "-HAccept: application/vnd.fdo.journal",
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen "--silent",
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering "--show-error",
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering url);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int r;
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = spawn_child("curl", argv);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("Failed to spawn curl: %m");
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering return r;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering}
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poetteringstatic int spawn_getter(const char *getter, const char *url) {
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen int r;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering _cleanup_strv_free_ char **words = NULL;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering assert(getter);
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen r = strv_split_quoted(&words, getter);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering if (r < 0) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering log_error("Failed to split getter option: %s", strerror(-r));
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen return r;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering }
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering r = strv_extend(&words, url);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering if (r < 0) {
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering log_error("Failed to create command line: %s", strerror(-r));
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering return r;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering }
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering r = spawn_child(words[0], words);
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering if (r < 0)
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering log_error("Failed to spawn getter %s: %m", getter);
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering return r;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering}
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering#define filename_escape(s) xescape((s), "/ ")
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poetteringstatic int open_output(Writer *w, const char* host) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering _cleanup_free_ char *_output = NULL;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering const char *output;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering int r;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering switch (arg_split_mode) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering case JOURNAL_WRITE_SPLIT_NONE:
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering output = arg_output ?: REMOTE_JOURNAL_PATH "/remote.journal";
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering break;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering case JOURNAL_WRITE_SPLIT_HOST: {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering _cleanup_free_ char *name;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen assert(host);
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering name = filename_escape(host);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering if (!name)
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering return log_oom();
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering r = asprintf(&_output, "%s/remote-%s.journal",
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering arg_output ?: REMOTE_JOURNAL_PATH,
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering name);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering if (r < 0)
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering return log_oom();
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering output = _output;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering break;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering }
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering default:
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering assert_not_reached("what?");
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering }
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering r = journal_file_open_reliably(output,
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering O_RDWR|O_CREAT, 0640,
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering arg_compress, arg_seal,
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering &w->metrics,
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering w->mmap,
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering NULL, &w->journal);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering if (r < 0)
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering log_error("Failed to open output journal %s: %s",
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering output, strerror(-r));
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering else
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering log_info("Opened output file %s", w->journal->path);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering return r;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering}
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering/**********************************************************************
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering **********************************************************************
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering **********************************************************************/
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poetteringstatic int init_writer_hashmap(RemoteServer *s) {
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering static const struct {
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering hash_func_t hash_func;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering compare_func_t compare_func;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering } functions[] = {
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering [JOURNAL_WRITE_SPLIT_NONE] = {trivial_hash_func,
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering trivial_compare_func},
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen [JOURNAL_WRITE_SPLIT_HOST] = {string_hash_func,
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen string_compare_func},
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering };
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering
b652d4a2099d1c167584dcc1d179d47c58dc38a2Lennart Poettering assert(arg_split_mode >= 0 && arg_split_mode < (int) ELEMENTSOF(functions));
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering s->writers = hashmap_new(functions[arg_split_mode].hash_func,
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering functions[arg_split_mode].compare_func);
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering if (!s->writers)
f4461e5641d53f27d6e76e0607bdaa9c0c58c1f6Lennart Poettering return log_oom();
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen return 0;
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering}
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poetteringstatic int get_writer(RemoteServer *s, const char *host,
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering Writer **writer) {
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering const void *key;
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering _cleanup_writer_unref_ Writer *w = NULL;
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering int r;
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering switch(arg_split_mode) {
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering case JOURNAL_WRITE_SPLIT_NONE:
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering key = "one and only";
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering break;
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering case JOURNAL_WRITE_SPLIT_HOST:
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering assert(host);
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering key = host;
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering break;
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering default:
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering assert_not_reached("what split mode?");
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering }
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering w = hashmap_get(s->writers, key);
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering if (w)
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering writer_ref(w);
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering else {
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering w = writer_new(s);
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering if (!w)
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering return log_oom();
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering if (arg_split_mode == JOURNAL_WRITE_SPLIT_HOST) {
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering w->hashmap_key = strdup(key);
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering if (!w->hashmap_key)
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering return log_oom();
d74fb368b18f0fbd9a4fe6f15691bbea7f3c4a01Tom Gundersen }
d74fb368b18f0fbd9a4fe6f15691bbea7f3c4a01Tom Gundersen
d74fb368b18f0fbd9a4fe6f15691bbea7f3c4a01Tom Gundersen r = open_output(w, host);
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering if (r < 0)
d74fb368b18f0fbd9a4fe6f15691bbea7f3c4a01Tom Gundersen return r;
d74fb368b18f0fbd9a4fe6f15691bbea7f3c4a01Tom Gundersen
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen r = hashmap_put(s->writers, w->hashmap_key ?: key, w);
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen if (r < 0)
efd46a696d31097c38f653b36921e00c4df62319Lennart Poettering return r;
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen }
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen *writer = w;
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering w = NULL;
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen return 0;
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen}
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering/**********************************************************************
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering **********************************************************************
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering **********************************************************************/
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering/* This should go away as soon as µhttpd allows state to be passed around. */
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poetteringstatic RemoteServer *server;
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poetteringstatic int dispatch_raw_source_event(sd_event_source *event,
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering int fd,
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering uint32_t revents,
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering void *userdata);
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersenstatic int dispatch_raw_connection_event(sd_event_source *event,
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen int fd,
f4461e5641d53f27d6e76e0607bdaa9c0c58c1f6Lennart Poettering uint32_t revents,
4e0b8b17a7465653f4e7b819dad5f8e30d64c0c4Tom Gundersen void *userdata);
4e0b8b17a7465653f4e7b819dad5f8e30d64c0c4Tom Gundersenstatic int dispatch_http_event(sd_event_source *event,
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering int fd,
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering uint32_t revents,
f4461e5641d53f27d6e76e0607bdaa9c0c58c1f6Lennart Poettering void *userdata);
4e0b8b17a7465653f4e7b819dad5f8e30d64c0c4Tom Gundersen
4e0b8b17a7465653f4e7b819dad5f8e30d64c0c4Tom Gundersenstatic int get_source_for_fd(RemoteServer *s,
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering int fd, char *name, RemoteSource **source) {
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering Writer *writer;
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering int r;
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering assert(fd >= 0);
571370c1555d2aa697733479a50957aff024bbcbLennart Poettering assert(source);
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering if (!GREEDY_REALLOC0(s->sources, s->sources_size, fd + 1))
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering return log_oom();
571370c1555d2aa697733479a50957aff024bbcbLennart Poettering
571370c1555d2aa697733479a50957aff024bbcbLennart Poettering r = get_writer(s, name, &writer);
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering if (r < 0) {
4e0b8b17a7465653f4e7b819dad5f8e30d64c0c4Tom Gundersen log_warning("Failed to get writer for source %s: %s",
4e0b8b17a7465653f4e7b819dad5f8e30d64c0c4Tom Gundersen name, strerror(-r));
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering return r;
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering }
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering if (s->sources[fd] == NULL) {
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering s->sources[fd] = source_new(fd, false, name, writer);
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering if (!s->sources[fd]) {
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering writer_unref(writer);
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering return log_oom();
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering }
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering s->active++;
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering }
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering *source = s->sources[fd];
b652d4a2099d1c167584dcc1d179d47c58dc38a2Lennart Poettering return 0;
b652d4a2099d1c167584dcc1d179d47c58dc38a2Lennart Poettering}
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poetteringstatic int remove_source(RemoteServer *s, int fd) {
b652d4a2099d1c167584dcc1d179d47c58dc38a2Lennart Poettering RemoteSource *source;
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering assert(s);
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering assert(fd >= 0 && fd < (ssize_t) s->sources_size);
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering source = s->sources[fd];
b652d4a2099d1c167584dcc1d179d47c58dc38a2Lennart Poettering if (source) {
b652d4a2099d1c167584dcc1d179d47c58dc38a2Lennart Poettering /* this closes fd too */
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen source_free(source);
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen s->sources[fd] = NULL;
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen s->active--;
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen }
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen return 0;
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen}
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersenstatic int add_source(RemoteServer *s, int fd, char* name, bool own_name) {
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen RemoteSource *source;
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen int r;
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen assert(s);
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen assert(fd >= 0);
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen assert(name);
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen if (!own_name) {
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen name = strdup(name);
011842775f750711833526d5bba1b818713947f5Lennart Poettering if (!name)
011842775f750711833526d5bba1b818713947f5Lennart Poettering return log_oom();
011842775f750711833526d5bba1b818713947f5Lennart Poettering }
011842775f750711833526d5bba1b818713947f5Lennart Poettering
011842775f750711833526d5bba1b818713947f5Lennart Poettering r = get_source_for_fd(s, fd, name, &source);
011842775f750711833526d5bba1b818713947f5Lennart Poettering if (r < 0) {
011842775f750711833526d5bba1b818713947f5Lennart Poettering log_error("Failed to create source for fd:%d (%s): %s",
011842775f750711833526d5bba1b818713947f5Lennart Poettering fd, name, strerror(-r));
cc450722a02ab9c59bca1d9a5b5012f356336a8cLennart Poettering return r;
cc450722a02ab9c59bca1d9a5b5012f356336a8cLennart Poettering }
cc450722a02ab9c59bca1d9a5b5012f356336a8cLennart Poettering
cc450722a02ab9c59bca1d9a5b5012f356336a8cLennart Poettering r = sd_event_add_io(s->events, &source->event,
cc450722a02ab9c59bca1d9a5b5012f356336a8cLennart Poettering fd, EPOLLIN|EPOLLRDHUP|EPOLLPRI,
cc450722a02ab9c59bca1d9a5b5012f356336a8cLennart Poettering dispatch_raw_source_event, s);
cc450722a02ab9c59bca1d9a5b5012f356336a8cLennart Poettering if (r < 0) {
cc450722a02ab9c59bca1d9a5b5012f356336a8cLennart Poettering log_error("Failed to register event source for fd:%d: %s",
cc450722a02ab9c59bca1d9a5b5012f356336a8cLennart Poettering fd, strerror(-r));
cc450722a02ab9c59bca1d9a5b5012f356336a8cLennart Poettering goto error;
cc450722a02ab9c59bca1d9a5b5012f356336a8cLennart Poettering }
011842775f750711833526d5bba1b818713947f5Lennart Poettering
011842775f750711833526d5bba1b818713947f5Lennart Poettering return 1; /* work to do */
f4461e5641d53f27d6e76e0607bdaa9c0c58c1f6Lennart Poettering
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen error:
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen remove_source(s, fd);
f4461e5641d53f27d6e76e0607bdaa9c0c58c1f6Lennart Poettering return r;
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen}
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen
f4461e5641d53f27d6e76e0607bdaa9c0c58c1f6Lennart Poetteringstatic int add_raw_socket(RemoteServer *s, int fd) {
cc450722a02ab9c59bca1d9a5b5012f356336a8cLennart Poettering int r;
011842775f750711833526d5bba1b818713947f5Lennart Poettering
011842775f750711833526d5bba1b818713947f5Lennart Poettering r = sd_event_add_io(s->events, &s->listen_event,
cc450722a02ab9c59bca1d9a5b5012f356336a8cLennart Poettering fd, EPOLLIN,
cc450722a02ab9c59bca1d9a5b5012f356336a8cLennart Poettering dispatch_raw_connection_event, s);
cc450722a02ab9c59bca1d9a5b5012f356336a8cLennart Poettering if (r < 0) {
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering close(fd);
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering return r;
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering }
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering
f4461e5641d53f27d6e76e0607bdaa9c0c58c1f6Lennart Poettering s->active ++;
f4461e5641d53f27d6e76e0607bdaa9c0c58c1f6Lennart Poettering return 0;
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering}
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersenstatic int setup_raw_socket(RemoteServer *s, const char *address) {
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering int fd;
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering
6a1a5eec43892dee3ff6e208bceb1931c25c782eLennart Poettering fd = make_socket_fd(LOG_INFO, address, SOCK_STREAM | SOCK_CLOEXEC);
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering if (fd < 0)
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering return fd;
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering return add_raw_socket(s, fd);
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering}
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering/**********************************************************************
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering **********************************************************************
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering **********************************************************************/
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poetteringstatic RemoteSource *request_meta(void **connection_cls, int fd, char *hostname) {
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering RemoteSource *source;
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering Writer *writer;
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering int r;
6a1a5eec43892dee3ff6e208bceb1931c25c782eLennart Poettering
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen assert(connection_cls);
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering if (*connection_cls)
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering return *connection_cls;
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering r = get_writer(server, hostname, &writer);
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering if (r < 0) {
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering log_warning("Failed to get writer for source %s: %s",
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering hostname, strerror(-r));
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering return NULL;
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering }
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering source = source_new(fd, true, hostname, writer);
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering if (!source) {
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering log_oom();
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering writer_unref(writer);
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering return NULL;
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering }
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering log_debug("Added RemoteSource as connection metadata %p", source);
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering *connection_cls = source;
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering return source;
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering}
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poetteringstatic void request_meta_free(void *cls,
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering struct MHD_Connection *connection,
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering void **connection_cls,
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering enum MHD_RequestTerminationCode toe) {
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering RemoteSource *s;
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering assert(connection_cls);
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering s = *connection_cls;
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering log_debug("Cleaning up connection metadata %p", s);
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering source_free(s);
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering *connection_cls = NULL;
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering}
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poetteringstatic int process_http_upload(
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering struct MHD_Connection *connection,
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering const char *upload_data,
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering size_t *upload_data_size,
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering RemoteSource *source) {
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering bool finished = false;
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering size_t remaining;
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering int r;
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering assert(source);
011842775f750711833526d5bba1b818713947f5Lennart Poettering
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering log_debug("request_handler_upload: connection %p, %zu bytes",
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering connection, *upload_data_size);
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering if (*upload_data_size) {
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering log_debug("Received %zu bytes", *upload_data_size);
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen r = push_data(source, upload_data, *upload_data_size);
f4461e5641d53f27d6e76e0607bdaa9c0c58c1f6Lennart Poettering if (r < 0)
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen return mhd_respond_oom(connection);
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering *upload_data_size = 0;
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering } else
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering finished = true;
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering while (true) {
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering r = process_source(source, arg_compress, arg_seal);
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering if (r == -EAGAIN || r == -EWOULDBLOCK)
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering break;
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering else if (r < 0) {
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering log_warning("Failed to process data for connection %p", connection);
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering if (r == -E2BIG)
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering return mhd_respondf(connection,
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering MHD_HTTP_REQUEST_ENTITY_TOO_LARGE,
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering "Entry is too large, maximum is %u bytes.\n",
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering DATA_SIZE_MAX);
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering else
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering return mhd_respondf(connection,
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering MHD_HTTP_UNPROCESSABLE_ENTITY,
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering "Processing failed: %s.", strerror(-r));
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering }
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering }
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering if (!finished)
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering return MHD_YES;
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering /* The upload is finished */
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering remaining = source_non_empty(source);
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering if (remaining > 0) {
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering log_warning("Premature EOFbyte. %zu bytes lost.", remaining);
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering return mhd_respondf(connection, MHD_HTTP_EXPECTATION_FAILED,
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering "Premature EOF. %zu bytes of trailing data not processed.",
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering remaining);
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering }
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering return mhd_respond(connection, MHD_HTTP_ACCEPTED, "OK.\n");
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering};
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poetteringstatic int request_handler(
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering void *cls,
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering struct MHD_Connection *connection,
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering const char *url,
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering const char *method,
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering const char *version,
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering const char *upload_data,
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering size_t *upload_data_size,
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering void **connection_cls) {
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering const char *header;
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering int r, code, fd;
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering _cleanup_free_ char *hostname = NULL;
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering assert(connection);
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering assert(connection_cls);
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering assert(url);
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering assert(method);
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering log_debug("Handling a connection %s %s %s", method, url, version);
1e02e182f1e06fcbe389474175de228103be39cbLennart Poettering
1e02e182f1e06fcbe389474175de228103be39cbLennart Poettering if (*connection_cls)
1e02e182f1e06fcbe389474175de228103be39cbLennart Poettering return process_http_upload(connection,
1e02e182f1e06fcbe389474175de228103be39cbLennart Poettering upload_data, upload_data_size,
1e02e182f1e06fcbe389474175de228103be39cbLennart Poettering *connection_cls);
1e02e182f1e06fcbe389474175de228103be39cbLennart Poettering
1e02e182f1e06fcbe389474175de228103be39cbLennart Poettering if (!streq(method, "POST"))
1e02e182f1e06fcbe389474175de228103be39cbLennart Poettering return mhd_respond(connection, MHD_HTTP_METHOD_NOT_ACCEPTABLE,
1e02e182f1e06fcbe389474175de228103be39cbLennart Poettering "Unsupported method.\n");
1e02e182f1e06fcbe389474175de228103be39cbLennart Poettering
1e02e182f1e06fcbe389474175de228103be39cbLennart Poettering if (!streq(url, "/upload"))
1e02e182f1e06fcbe389474175de228103be39cbLennart Poettering return mhd_respond(connection, MHD_HTTP_NOT_FOUND,
1e02e182f1e06fcbe389474175de228103be39cbLennart Poettering "Not found.\n");
1e02e182f1e06fcbe389474175de228103be39cbLennart Poettering
1e02e182f1e06fcbe389474175de228103be39cbLennart Poettering header = MHD_lookup_connection_value(connection,
1e02e182f1e06fcbe389474175de228103be39cbLennart Poettering MHD_HEADER_KIND, "Content-Type");
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen if (!header || !streq(header, "application/vnd.fdo.journal"))
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering return mhd_respond(connection, MHD_HTTP_UNSUPPORTED_MEDIA_TYPE,
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering "Content-Type: application/vnd.fdo.journal"
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen " is required.\n");
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen {
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen const union MHD_ConnectionInfo *ci;
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering ci = MHD_get_connection_info(connection,
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt MHD_CONNECTION_INFO_CONNECTION_FD);
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering if (!ci) {
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering log_error("MHD_get_connection_info failed: cannot get remote fd");
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering return mhd_respond(connection, MHD_HTTP_INTERNAL_SERVER_ERROR,
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering "Cannot check remote address");
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering return code;
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering }
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering fd = ci->connect_fd;
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering assert(fd >= 0);
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt }
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt if (server->check_trust) {
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt r = check_permissions(connection, &code, &hostname);
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt if (r < 0)
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering return code;
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering } else {
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering r = getnameinfo_pretty(fd, &hostname);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering if (r < 0) {
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering return mhd_respond(connection, MHD_HTTP_INTERNAL_SERVER_ERROR,
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering "Cannot check remote hostname");
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering }
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering }
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering assert(hostname);
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering if (!request_meta(connection_cls, fd, hostname))
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering return respond_oom(connection);
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering hostname = NULL;
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering return MHD_YES;
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering}
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poetteringstatic int setup_microhttpd_server(RemoteServer *s,
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering int fd,
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering const char *key,
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering const char *cert,
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering const char *trust) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering struct MHD_OptionItem opts[] = {
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering { MHD_OPTION_NOTIFY_COMPLETED, (intptr_t) request_meta_free},
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering { MHD_OPTION_EXTERNAL_LOGGER, (intptr_t) microhttpd_logger},
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering { MHD_OPTION_LISTEN_SOCKET, fd},
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering { MHD_OPTION_END},
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering { MHD_OPTION_END},
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering { MHD_OPTION_END},
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering { MHD_OPTION_END}};
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering int opts_pos = 3;
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering int flags =
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering MHD_USE_DEBUG |
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering MHD_USE_PEDANTIC_CHECKS |
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering MHD_USE_EPOLL_LINUX_ONLY |
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering MHD_USE_DUAL_STACK;
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering const union MHD_DaemonInfo *info;
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering int r, epoll_fd;
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering MHDDaemonWrapper *d;
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering assert(fd >= 0);
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering r = fd_nonblock(fd, true);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (r < 0) {
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering log_error("Failed to make fd:%d nonblocking: %s", fd, strerror(-r));
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering return r;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering }
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (key) {
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering assert(cert);
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering opts[opts_pos++] = (struct MHD_OptionItem)
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering {MHD_OPTION_HTTPS_MEM_KEY, 0, (char*) key};
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering opts[opts_pos++] = (struct MHD_OptionItem)
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering {MHD_OPTION_HTTPS_MEM_CERT, 0, (char*) cert};
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering flags |= MHD_USE_SSL;
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (trust)
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering opts[opts_pos++] = (struct MHD_OptionItem)
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering {MHD_OPTION_HTTPS_MEM_TRUST, 0, (char*) trust};
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering }
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering d = new(MHDDaemonWrapper, 1);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (!d)
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering return log_oom();
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering d->fd = (uint64_t) fd;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering d->daemon = MHD_start_daemon(flags, 0,
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering NULL, NULL,
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering request_handler, NULL,
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering MHD_OPTION_ARRAY, opts,
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering MHD_OPTION_END);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (!d->daemon) {
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering log_error("Failed to start µhttp daemon");
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering r = -EINVAL;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering goto error;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering }
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering log_debug("Started MHD %s daemon on fd:%d (wrapper @ %p)",
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering key ? "HTTPS" : "HTTP", fd, d);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering info = MHD_get_daemon_info(d->daemon, MHD_DAEMON_INFO_EPOLL_FD_LINUX_ONLY);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (!info) {
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering log_error("µhttp returned NULL daemon info");
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering r = -ENOTSUP;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering goto error;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering }
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering epoll_fd = info->listen_fd;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (epoll_fd < 0) {
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering log_error("µhttp epoll fd is invalid");
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering r = -EUCLEAN;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering goto error;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering }
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering r = sd_event_add_io(s->events, &d->event,
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering epoll_fd, EPOLLIN,
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering dispatch_http_event, d);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (r < 0) {
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering log_error("Failed to add event callback: %s", strerror(-r));
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering goto error;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering }
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering r = hashmap_ensure_allocated(&s->daemons, uint64_hash_func, uint64_compare_func);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (r < 0) {
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering log_oom();
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering goto error;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering }
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering r = hashmap_put(s->daemons, &d->fd, d);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (r < 0) {
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering log_error("Failed to add daemon to hashmap: %s", strerror(-r));
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering goto error;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering }
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering s->active ++;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering return 0;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poetteringerror:
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering MHD_stop_daemon(d->daemon);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering free(d->daemon);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering free(d);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering return r;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering}
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poetteringstatic int setup_microhttpd_socket(RemoteServer *s,
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering const char *address,
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering const char *key,
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering const char *cert,
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen const char *trust) {
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen int fd;
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen fd = make_socket_fd(LOG_INFO, address, SOCK_STREAM | SOCK_CLOEXEC);
9c5e12a4314e7192e834e1b855e5e80111e636a6Tom Gundersen if (fd < 0)
7586f4d172dd9c3ccc3126fc47dca9e49adec132Tom Gundersen return fd;
d74fb368b18f0fbd9a4fe6f15691bbea7f3c4a01Tom Gundersen
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen return setup_microhttpd_server(s, fd, key, cert, trust);
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen}
static int dispatch_http_event(sd_event_source *event,
int fd,
uint32_t revents,
void *userdata) {
MHDDaemonWrapper *d = userdata;
int r;
assert(d);
r = MHD_run(d->daemon);
if (r == MHD_NO) {
log_error("MHD_run failed!");
// XXX: unregister daemon
return -EINVAL;
}
return 1; /* work to do */
}
/**********************************************************************
**********************************************************************
**********************************************************************/
static int dispatch_sigterm(sd_event_source *event,
const struct signalfd_siginfo *si,
void *userdata) {
RemoteServer *s = userdata;
assert(s);
log_received_signal(LOG_INFO, si);
sd_event_exit(s->events, 0);
return 0;
}
static int setup_signals(RemoteServer *s) {
sigset_t mask;
int r;
assert(s);
assert_se(sigemptyset(&mask) == 0);
sigset_add_many(&mask, SIGINT, SIGTERM, -1);
assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
r = sd_event_add_signal(s->events, &s->sigterm_event, SIGTERM, dispatch_sigterm, s);
if (r < 0)
return r;
r = sd_event_add_signal(s->events, &s->sigint_event, SIGINT, dispatch_sigterm, s);
if (r < 0)
return r;
return 0;
}
static int fd_fd(const char *spec) {
int fd, r;
r = safe_atoi(spec, &fd);
if (r < 0)
return r;
if (fd < 0)
return -EINVAL;
return fd;
}
static int remoteserver_init(RemoteServer *s,
const char* key,
const char* cert,
const char* trust) {
int r, n, fd;
char **file;
assert(s);
if ((arg_listen_raw || arg_listen_http) && trust) {
log_error("Option --trust makes all non-HTTPS connections untrusted.");
return -EINVAL;
}
sd_event_default(&s->events);
setup_signals(s);
assert(server == NULL);
server = s;
n = sd_listen_fds(true);
if (n < 0) {
log_error("Failed to read listening file descriptors from environment: %s",
strerror(-n));
return n;
} else
log_info("Received %d descriptors", n);
if (MAX(http_socket, https_socket) >= SD_LISTEN_FDS_START + n) {
log_error("Received fewer sockets than expected");
return -EBADFD;
}
for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) {
if (sd_is_socket(fd, AF_UNSPEC, 0, false)) {
log_info("Received a listening socket (fd:%d)", fd);
if (fd == http_socket)
r = setup_microhttpd_server(s, fd, NULL, NULL, NULL);
else if (fd == https_socket)
r = setup_microhttpd_server(s, fd, key, cert, trust);
else
r = add_raw_socket(s, fd);
} else if (sd_is_socket(fd, AF_UNSPEC, 0, true)) {
char *hostname;
r = getnameinfo_pretty(fd, &hostname);
if (r < 0) {
log_error("Failed to retrieve remote name: %s", strerror(-r));
return r;
}
log_info("Received a connection socket (fd:%d) from %s", fd, hostname);
r = add_source(s, fd, hostname, true);
if (r < 0)
free(hostname);
} else {
log_error("Unknown socket passed on fd:%d", fd);
return -EINVAL;
}
if(r < 0) {
log_error("Failed to register socket (fd:%d): %s",
fd, strerror(-r));
return r;
}
}
if (arg_url) {
const char *url, *hostname;
url = strappenda(arg_url, "/entries");
if (arg_getter) {
log_info("Spawning getter %s...", url);
fd = spawn_getter(arg_getter, url);
} else {
log_info("Spawning curl %s...", url);
fd = spawn_curl(url);
}
if (fd < 0)
return fd;
hostname =
startswith(arg_url, "https://") ?:
startswith(arg_url, "http://") ?:
arg_url;
r = add_source(s, fd, (char*) hostname, false);
if (r < 0)
return r;
}
if (arg_listen_raw) {
log_info("Listening on a socket...");
r = setup_raw_socket(s, arg_listen_raw);
if (r < 0)
return r;
}
if (arg_listen_http) {
r = setup_microhttpd_socket(s, arg_listen_http, NULL, NULL, NULL);
if (r < 0)
return r;
}
if (arg_listen_https) {
r = setup_microhttpd_socket(s, arg_listen_https, key, cert, trust);
if (r < 0)
return r;
}
STRV_FOREACH(file, arg_files) {
const char *output_name;
if (streq(*file, "-")) {
log_info("Using standard input as source.");
fd = STDIN_FILENO;
output_name = "stdin";
} else {
log_info("Reading file %s...", *file);
fd = open(*file, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
if (fd < 0) {
log_error("Failed to open %s: %m", *file);
return -errno;
}
output_name = *file;
}
r = add_source(s, fd, (char*) output_name, false);
if (r < 0)
return r;
}
if (s->active == 0) {
log_error("Zarro sources specified");
return -EINVAL;
}
r = init_writer_hashmap(s);
if (r < 0)
return r;
if (arg_split_mode == JOURNAL_WRITE_SPLIT_NONE) {
/* In this case we know what the writer will be
called, so we can create it and verify that we can
create output as expected. */
r = get_writer(s, NULL, &s->_single_writer);
if (r < 0)
return r;
}
return 0;
}
static void server_destroy(RemoteServer *s) {
size_t i;
MHDDaemonWrapper *d;
while ((d = hashmap_steal_first(s->daemons))) {
MHD_stop_daemon(d->daemon);
sd_event_source_unref(d->event);
free(d);
}
hashmap_free(s->daemons);
assert(s->sources_size == 0 || s->sources);
for (i = 0; i < s->sources_size; i++)
remove_source(s, i);
free(s->sources);
writer_unref(s->_single_writer);
hashmap_free(s->writers);
sd_event_source_unref(s->sigterm_event);
sd_event_source_unref(s->sigint_event);
sd_event_source_unref(s->listen_event);
sd_event_unref(s->events);
/* fds that we're listening on remain open... */
}
/**********************************************************************
**********************************************************************
**********************************************************************/
static int dispatch_raw_source_event(sd_event_source *event,
int fd,
uint32_t revents,
void *userdata) {
RemoteServer *s = userdata;
RemoteSource *source;
int r;
assert(fd >= 0 && fd < (ssize_t) s->sources_size);
source = s->sources[fd];
assert(source->fd == fd);
r = process_source(source, arg_compress, arg_seal);
if (source->state == STATE_EOF) {
size_t remaining;
log_info("EOF reached with source fd:%d (%s)",
source->fd, source->name);
remaining = source_non_empty(source);
if (remaining > 0)
log_warning("Premature EOF. %zu bytes lost.", remaining);
remove_source(s, source->fd);
log_info("%zd active sources remaining", s->active);
return 0;
} else if (r == -E2BIG) {
log_error("Entry too big, skipped");
return 1;
} else if (r == -EAGAIN) {
return 0;
} else if (r < 0) {
log_info("Closing connection: %s", strerror(-r));
remove_source(server, fd);
return 0;
} else
return 1;
}
static int accept_connection(const char* type, int fd,
SocketAddress *addr, char **hostname) {
int fd2, r;
log_debug("Accepting new %s connection on fd:%d", type, fd);
fd2 = accept4(fd, &addr->sockaddr.sa, &addr->size, SOCK_NONBLOCK|SOCK_CLOEXEC);
if (fd2 < 0) {
log_error("accept() on fd:%d failed: %m", fd);
return -errno;
}
switch(socket_address_family(addr)) {
case AF_INET:
case AF_INET6: {
_cleanup_free_ char *a = NULL;
char *b;
r = socket_address_print(addr, &a);
if (r < 0) {
log_error("socket_address_print(): %s", strerror(-r));
close(fd2);
return r;
}
r = socknameinfo_pretty(&addr->sockaddr, addr->size, &b);
if (r < 0) {
close(fd2);
return r;
}
log_info("Accepted %s %s connection from %s",
type,
socket_address_family(addr) == AF_INET ? "IP" : "IPv6",
a);
*hostname = b;
return fd2;
};
default:
log_error("Rejected %s connection with unsupported family %d",
type, socket_address_family(addr));
close(fd2);
return -EINVAL;
}
}
static int dispatch_raw_connection_event(sd_event_source *event,
int fd,
uint32_t revents,
void *userdata) {
RemoteServer *s = userdata;
int fd2, r;
SocketAddress addr = {
.size = sizeof(union sockaddr_union),
.type = SOCK_STREAM,
};
char *hostname;
fd2 = accept_connection("raw", fd, &addr, &hostname);
if (fd2 < 0)
return fd2;
r = add_source(s, fd2, hostname, true);
if (r < 0)
free(hostname);
return r;
}
/**********************************************************************
**********************************************************************
**********************************************************************/
static const char* const journal_write_split_mode_table[_JOURNAL_WRITE_SPLIT_MAX] = {
[JOURNAL_WRITE_SPLIT_NONE] = "none",
[JOURNAL_WRITE_SPLIT_HOST] = "host",
};
DEFINE_PRIVATE_STRING_TABLE_LOOKUP(journal_write_split_mode, JournalWriteSplitMode);
static DEFINE_CONFIG_PARSE_ENUM(config_parse_write_split_mode,
journal_write_split_mode,
JournalWriteSplitMode,
"Failed to parse split mode setting");
static int parse_config(void) {
const ConfigTableItem items[] = {
{ "Remote", "SplitMode", config_parse_write_split_mode, 0, &arg_split_mode },
{ "Remote", "ServerKeyFile", config_parse_path, 0, &arg_key },
{ "Remote", "ServerCertificateFile", config_parse_path, 0, &arg_cert },
{ "Remote", "TrustedCertificateFile", config_parse_path, 0, &arg_trust },
{}};
return config_parse(NULL, PKGSYSCONFDIR "/journal-remote.conf", NULL,
"Remote\0",
config_item_table_lookup, items,
false, false, true, NULL);
}
static void help(void) {
printf("%s [OPTIONS...] {FILE|-}...\n\n"
"Write external journal events to journal file(s).\n\n"
" -h --help Show this help\n"
" --version Show package version\n"
" --url=URL Read events from systemd-journal-gatewayd at URL\n"
" --getter=COMMAND Read events from the output of COMMAND\n"
" --listen-raw=ADDR Listen for connections at ADDR\n"
" --listen-http=ADDR Listen for HTTP connections at ADDR\n"
" --listen-https=ADDR Listen for HTTPS connections at ADDR\n"
" -o --output=FILE|DIR Write output to FILE or DIR/external-*.journal\n"
" --compress[=BOOL] Use XZ-compression in the output journal (default: yes)\n"
" --seal[=BOOL] Use Event sealing in the output journal (default: no)\n"
" --key=FILENAME Specify key in PEM format (default:\n"
" \"" PRIV_KEY_FILE "\")\n"
" --cert=FILENAME Specify certificate in PEM format (default:\n"
" \"" CERT_FILE "\")\n"
" --trust=FILENAME|all Specify CA certificate or disable checking (default:\n"
" \"" TRUST_FILE "\")\n"
" --gnutls-log=CATEGORY...\n"
" Specify a list of gnutls logging categories\n"
"\n"
"Note: file descriptors from sd_listen_fds() will be consumed, too.\n"
, program_invocation_short_name);
}
static int parse_argv(int argc, char *argv[]) {
enum {
ARG_VERSION = 0x100,
ARG_URL,
ARG_LISTEN_RAW,
ARG_LISTEN_HTTP,
ARG_LISTEN_HTTPS,
ARG_GETTER,
ARG_SPLIT_MODE,
ARG_COMPRESS,
ARG_SEAL,
ARG_KEY,
ARG_CERT,
ARG_TRUST,
ARG_GNUTLS_LOG,
};
static const struct option options[] = {
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, ARG_VERSION },
{ "url", required_argument, NULL, ARG_URL },
{ "getter", required_argument, NULL, ARG_GETTER },
{ "listen-raw", required_argument, NULL, ARG_LISTEN_RAW },
{ "listen-http", required_argument, NULL, ARG_LISTEN_HTTP },
{ "listen-https", required_argument, NULL, ARG_LISTEN_HTTPS },
{ "output", required_argument, NULL, 'o' },
{ "split-mode", required_argument, NULL, ARG_SPLIT_MODE },
{ "compress", optional_argument, NULL, ARG_COMPRESS },
{ "seal", optional_argument, NULL, ARG_SEAL },
{ "key", required_argument, NULL, ARG_KEY },
{ "cert", required_argument, NULL, ARG_CERT },
{ "trust", required_argument, NULL, ARG_TRUST },
{ "gnutls-log", required_argument, NULL, ARG_GNUTLS_LOG },
{}
};
int c, r;
bool type_a, type_b;
assert(argc >= 0);
assert(argv);
while ((c = getopt_long(argc, argv, "ho:", options, NULL)) >= 0)
switch(c) {
case 'h':
help();
return 0 /* done */;
case ARG_VERSION:
puts(PACKAGE_STRING);
puts(SYSTEMD_FEATURES);
return 0 /* done */;
case ARG_URL:
if (arg_url) {
log_error("cannot currently set more than one --url");
return -EINVAL;
}
arg_url = optarg;
break;
case ARG_GETTER:
if (arg_getter) {
log_error("cannot currently use --getter more than once");
return -EINVAL;
}
arg_getter = optarg;
break;
case ARG_LISTEN_RAW:
if (arg_listen_raw) {
log_error("cannot currently use --listen-raw more than once");
return -EINVAL;
}
arg_listen_raw = optarg;
break;
case ARG_LISTEN_HTTP:
if (arg_listen_http || http_socket >= 0) {
log_error("cannot currently use --listen-http more than once");
return -EINVAL;
}
r = fd_fd(optarg);
if (r >= 0)
http_socket = r;
else
arg_listen_http = optarg;
break;
case ARG_LISTEN_HTTPS:
if (arg_listen_https || https_socket >= 0) {
log_error("cannot currently use --listen-https more than once");
return -EINVAL;
}
r = fd_fd(optarg);
if (r >= 0)
https_socket = r;
else
arg_listen_https = optarg;
break;
case ARG_KEY:
if (arg_key) {
log_error("Key file specified twice");
return -EINVAL;
}
arg_key = strdup(optarg);
if (!arg_key)
return log_oom();
break;
case ARG_CERT:
if (arg_cert) {
log_error("Certificate file specified twice");
return -EINVAL;
}
arg_cert = strdup(optarg);
if (!arg_cert)
return log_oom();
break;
case ARG_TRUST:
if (arg_trust || arg_trust_all) {
log_error("Confusing trusted CA configuration");
return -EINVAL;
}
if (streq(optarg, "all"))
arg_trust_all = true;
else {
#ifdef HAVE_GNUTLS
arg_trust = strdup(optarg);
if (!arg_trust)
return log_oom();
#else
log_error("Option --trust is not available.");
return -EINVAL;
#endif
}
break;
case 'o':
if (arg_output) {
log_error("cannot use --output/-o more than once");
return -EINVAL;
}
arg_output = optarg;
break;
case ARG_SPLIT_MODE:
arg_split_mode = journal_write_split_mode_from_string(optarg);
if (arg_split_mode == _JOURNAL_WRITE_SPLIT_INVALID) {
log_error("Invalid split mode: %s", optarg);
return -EINVAL;
}
break;
case ARG_COMPRESS:
if (optarg) {
r = parse_boolean(optarg);
if (r < 0) {
log_error("Failed to parse --compress= parameter.");
return -EINVAL;
}
arg_compress = !!r;
} else
arg_compress = true;
break;
case ARG_SEAL:
if (optarg) {
r = parse_boolean(optarg);
if (r < 0) {
log_error("Failed to parse --seal= parameter.");
return -EINVAL;
}
arg_seal = !!r;
} else
arg_seal = true;
break;
case ARG_GNUTLS_LOG: {
#ifdef HAVE_GNUTLS
const char *word, *state;
size_t size;
FOREACH_WORD_SEPARATOR(word, size, optarg, ",", state) {
char *cat;
cat = strndup(word, size);
if (!cat)
return log_oom();
if (strv_consume(&arg_gnutls_log, cat) < 0)
return log_oom();
}
break;
#else
log_error("Option --gnutls-log is not available.");
return -EINVAL;
#endif
}
case '?':
return -EINVAL;
default:
assert_not_reached("Unknown option code.");
}
if (optind < argc)
arg_files = argv + optind;
type_a = arg_getter || !strv_isempty(arg_files);
type_b = arg_url
|| arg_listen_raw
|| arg_listen_http || arg_listen_https
|| sd_listen_fds(false) > 0;
if (type_a && type_b) {
log_error("Cannot use file input or --getter with "
"--arg-listen-... or socket activation.");
return -EINVAL;
}
if (type_a) {
if (!arg_output) {
log_error("Option --output must be specified with file input or --getter.");
return -EINVAL;
}
arg_split_mode = JOURNAL_WRITE_SPLIT_NONE;
}
if (arg_split_mode == JOURNAL_WRITE_SPLIT_NONE
&& arg_output && is_dir(arg_output, true) > 0) {
log_error("For SplitMode=none, output must be a file.");
return -EINVAL;
}
if (arg_split_mode == JOURNAL_WRITE_SPLIT_HOST
&& arg_output && is_dir(arg_output, true) <= 0) {
log_error("For SplitMode=host, output must be a directory.");
return -EINVAL;
}
log_debug("Full config: SplitMode=%s Key=%s Cert=%s Trust=%s",
journal_write_split_mode_to_string(arg_split_mode),
strna(arg_key),
strna(arg_cert),
strna(arg_trust));
return 1 /* work to do */;
}
static int load_certificates(char **key, char **cert, char **trust) {
int r;
r = read_full_file(arg_key ?: PRIV_KEY_FILE, key, NULL);
if (r < 0) {
log_error("Failed to read key from file '%s': %s",
arg_key ?: PRIV_KEY_FILE, strerror(-r));
return r;
}
r = read_full_file(arg_cert ?: CERT_FILE, cert, NULL);
if (r < 0) {
log_error("Failed to read certificate from file '%s': %s",
arg_cert ?: CERT_FILE, strerror(-r));
return r;
}
if (arg_trust_all)
log_info("Certificate checking disabled.");
else {
r = read_full_file(arg_trust ?: TRUST_FILE, trust, NULL);
if (r < 0) {
log_error("Failed to read CA certificate file '%s': %s",
arg_trust ?: TRUST_FILE, strerror(-r));
return r;
}
}
return 0;
}
static int setup_gnutls_logger(char **categories) {
if (!arg_listen_http && !arg_listen_https)
return 0;
#ifdef HAVE_GNUTLS
{
char **cat;
int r;
gnutls_global_set_log_function(log_func_gnutls);
if (categories)
STRV_FOREACH(cat, categories) {
r = log_enable_gnutls_category(*cat);
if (r < 0)
return r;
}
else
log_reset_gnutls_level();
}
#endif
return 0;
}
int main(int argc, char **argv) {
RemoteServer s = {};
int r;
_cleanup_free_ char *key = NULL, *cert = NULL, *trust = NULL;
log_show_color(true);
log_parse_environment();
r = parse_config();
if (r < 0)
return EXIT_FAILURE;
r = parse_argv(argc, argv);
if (r <= 0)
return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
r = setup_gnutls_logger(arg_gnutls_log);
if (r < 0)
return EXIT_FAILURE;
if (arg_listen_https || https_socket >= 0)
if (load_certificates(&key, &cert, &trust) < 0)
return EXIT_FAILURE;
if (remoteserver_init(&s, key, cert, trust) < 0)
return EXIT_FAILURE;
sd_event_set_watchdog(s.events, true);
log_debug("%s running as pid "PID_FMT,
program_invocation_short_name, getpid());
sd_notify(false,
"READY=1\n"
"STATUS=Processing requests...");
while (s.active) {
r = sd_event_get_state(s.events);
if (r < 0)
break;
if (r == SD_EVENT_FINISHED)
break;
r = sd_event_run(s.events, -1);
if (r < 0) {
log_error("Failed to run event loop: %s", strerror(-r));
break;
}
}
server_destroy(&s);
log_info("Finishing after writing %" PRIu64 " entries", s.event_count);
sd_notify(false, "STATUS=Shutting down...");
free(arg_key);
free(arg_cert);
free(arg_trust);
return r >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}