2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2011-10-07 21:06:39 +02:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-10-02 23:21:59 +02:00
|
|
|
#include "sd-daemon.h"
|
2015-11-16 22:09:36 +01:00
|
|
|
#include "sd-messages.h"
|
2012-01-05 16:01:58 +01:00
|
|
|
|
2016-11-07 16:14:59 +01:00
|
|
|
#include "format-util.h"
|
2012-08-21 01:29:17 +02:00
|
|
|
#include "journal-authenticate.h"
|
2012-08-22 03:09:44 +02:00
|
|
|
#include "journald-kmsg.h"
|
2015-11-16 22:09:36 +01:00
|
|
|
#include "journald-server.h"
|
2012-08-22 03:21:03 +02:00
|
|
|
#include "journald-syslog.h"
|
2018-01-11 00:39:12 +01:00
|
|
|
#include "process-util.h"
|
2014-12-30 20:57:53 +01:00
|
|
|
#include "sigbus.h"
|
|
|
|
|
2011-10-07 21:06:39 +02:00
|
|
|
int main(int argc, char *argv[]) {
|
2019-11-25 15:00:38 +01:00
|
|
|
const char *namespace;
|
2011-10-07 21:06:39 +02:00
|
|
|
Server server;
|
|
|
|
int r;
|
|
|
|
|
2019-11-25 15:00:38 +01:00
|
|
|
if (argc > 2) {
|
|
|
|
log_error("This program takes one or no arguments.");
|
2011-10-07 21:06:39 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2019-11-25 15:00:38 +01:00
|
|
|
namespace = argc > 1 ? empty_to_null(argv[1]) : NULL;
|
|
|
|
|
2018-01-24 18:01:01 +01:00
|
|
|
log_set_prohibit_ipc(true);
|
|
|
|
log_set_target(LOG_TARGET_AUTO);
|
2012-04-03 19:25:29 +02:00
|
|
|
log_set_facility(LOG_SYSLOG);
|
2011-10-07 21:06:39 +02:00
|
|
|
log_parse_environment();
|
|
|
|
log_open();
|
|
|
|
|
|
|
|
umask(0022);
|
|
|
|
|
2014-12-30 20:57:53 +01:00
|
|
|
sigbus_install();
|
|
|
|
|
2019-11-25 15:00:38 +01:00
|
|
|
r = server_init(&server, namespace);
|
2011-10-07 21:06:39 +02:00
|
|
|
if (r < 0)
|
|
|
|
goto finish;
|
|
|
|
|
2016-10-12 09:58:10 +02:00
|
|
|
server_vacuum(&server, false);
|
2016-12-12 20:54:45 +01:00
|
|
|
server_flush_to_var(&server, true);
|
2012-08-09 15:57:24 +02:00
|
|
|
server_flush_dev_kmsg(&server);
|
2012-01-04 20:40:04 +01:00
|
|
|
|
2019-11-25 15:00:38 +01:00
|
|
|
if (server.namespace)
|
2020-02-03 00:58:28 +01:00
|
|
|
log_debug("systemd-journald running as PID "PID_FMT" for namespace '%s'.", getpid_cached(), server.namespace);
|
2019-11-25 15:00:38 +01:00
|
|
|
else
|
|
|
|
log_debug("systemd-journald running as PID "PID_FMT" for the system.", getpid_cached());
|
|
|
|
|
2017-10-30 20:01:50 +01:00
|
|
|
server_driver_message(&server, 0,
|
tree-wide: add SD_ID128_MAKE_STR, remove LOG_MESSAGE_ID
Embedding sd_id128_t's in constant strings was rather cumbersome. We had
SD_ID128_CONST_STR which returned a const char[], but it had two problems:
- it wasn't possible to statically concatanate this array with a normal string
- gcc wasn't really able to optimize this, and generated code to perform the
"conversion" at runtime.
Because of this, even our own code in coredumpctl wasn't using
SD_ID128_CONST_STR.
Add a new macro to generate a constant string: SD_ID128_MAKE_STR.
It is not as elegant as SD_ID128_CONST_STR, because it requires a repetition
of the numbers, but in practice it is more convenient to use, and allows gcc
to generate smarter code:
$ size .libs/systemd{,-logind,-journald}{.old,}
text data bss dec hex filename
1265204 149564 4808 1419576 15a938 .libs/systemd.old
1260268 149564 4808 1414640 1595f0 .libs/systemd
246805 13852 209 260866 3fb02 .libs/systemd-logind.old
240973 13852 209 255034 3e43a .libs/systemd-logind
146839 4984 34 151857 25131 .libs/systemd-journald.old
146391 4984 34 151409 24f71 .libs/systemd-journald
It is also much easier to check if a certain binary uses a certain MESSAGE_ID:
$ strings .libs/systemd.old|grep MESSAGE_ID
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
$ strings .libs/systemd|grep MESSAGE_ID
MESSAGE_ID=c7a787079b354eaaa9e77b371893cd27
MESSAGE_ID=b07a249cd024414a82dd00cd181378ff
MESSAGE_ID=641257651c1b4ec9a8624d7a40a9e1e7
MESSAGE_ID=de5b426a63be47a7b6ac3eaac82e2f6f
MESSAGE_ID=d34d037fff1847e6ae669a370e694725
MESSAGE_ID=7d4958e842da4a758f6c1cdc7b36dcc5
MESSAGE_ID=1dee0369c7fc4736b7099b38ecb46ee7
MESSAGE_ID=39f53479d3a045ac8e11786248231fbf
MESSAGE_ID=be02cf6855d2428ba40df7e9d022f03d
MESSAGE_ID=7b05ebc668384222baa8881179cfda54
MESSAGE_ID=9d1aaa27d60140bd96365438aad20286
2016-11-06 18:48:23 +01:00
|
|
|
"MESSAGE_ID=" SD_MESSAGE_JOURNAL_START_STR,
|
2015-11-08 20:05:55 +01:00
|
|
|
LOG_MESSAGE("Journal started"),
|
|
|
|
NULL);
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2016-10-11 16:51:37 +02:00
|
|
|
/* Make sure to send the usage message *after* flushing the
|
|
|
|
* journal so entries from the runtime journals are ordered
|
|
|
|
* before this message. See #4190 for some details. */
|
|
|
|
server_space_usage_message(&server, NULL);
|
|
|
|
|
2011-10-07 21:06:39 +02:00
|
|
|
for (;;) {
|
2014-07-29 12:23:31 +02:00
|
|
|
usec_t t = USEC_INFINITY, n;
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2013-12-22 21:10:06 +01:00
|
|
|
r = sd_event_get_state(server.event);
|
2019-11-27 13:03:51 +01:00
|
|
|
if (r < 0) {
|
|
|
|
log_error_errno(r, "Failed to get event loop state: %m");
|
2013-12-22 21:10:06 +01:00
|
|
|
goto finish;
|
2019-11-27 13:03:51 +01:00
|
|
|
}
|
2013-12-22 21:10:06 +01:00
|
|
|
if (r == SD_EVENT_FINISHED)
|
|
|
|
break;
|
|
|
|
|
2012-10-16 22:58:07 +02:00
|
|
|
n = now(CLOCK_REALTIME);
|
2012-08-21 01:29:17 +02:00
|
|
|
|
2012-10-16 22:58:07 +02:00
|
|
|
if (server.max_retention_usec > 0 && server.oldest_file_usec > 0) {
|
2012-08-21 01:29:17 +02:00
|
|
|
|
2012-10-16 22:58:07 +02:00
|
|
|
/* The retention time is reached, so let's vacuum! */
|
|
|
|
if (server.oldest_file_usec + server.max_retention_usec < n) {
|
|
|
|
log_info("Retention time reached.");
|
|
|
|
server_rotate(&server);
|
2016-10-12 09:58:10 +02:00
|
|
|
server_vacuum(&server, false);
|
2012-10-16 22:58:07 +02:00
|
|
|
continue;
|
|
|
|
}
|
2012-08-21 01:29:17 +02:00
|
|
|
|
2012-10-16 22:58:07 +02:00
|
|
|
/* Calculate when to rotate the next time */
|
2013-12-11 20:13:44 +01:00
|
|
|
t = server.oldest_file_usec + server.max_retention_usec - n;
|
2012-10-16 22:58:07 +02:00
|
|
|
}
|
|
|
|
|
2017-10-03 10:41:51 +02:00
|
|
|
#if HAVE_GCRYPT
|
2012-10-16 22:58:07 +02:00
|
|
|
if (server.system_journal) {
|
|
|
|
usec_t u;
|
|
|
|
|
|
|
|
if (journal_file_next_evolve_usec(server.system_journal, &u)) {
|
|
|
|
if (n >= u)
|
|
|
|
t = 0;
|
|
|
|
else
|
2013-12-11 20:13:44 +01:00
|
|
|
t = MIN(t, u - n);
|
2012-10-16 22:58:07 +02:00
|
|
|
}
|
|
|
|
}
|
2012-08-21 01:29:17 +02:00
|
|
|
#endif
|
|
|
|
|
2013-12-11 20:13:44 +01:00
|
|
|
r = sd_event_run(server.event, t);
|
2011-10-07 21:06:39 +02:00
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to run event loop: %m");
|
2011-10-07 21:06:39 +02:00
|
|
|
goto finish;
|
2012-08-21 01:29:17 +02:00
|
|
|
}
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2012-08-22 04:45:17 +02:00
|
|
|
server_maybe_append_tags(&server);
|
2012-09-17 23:59:26 +02:00
|
|
|
server_maybe_warn_forward_syslog_missed(&server);
|
2011-10-07 21:06:39 +02:00
|
|
|
}
|
|
|
|
|
2019-11-25 15:00:38 +01:00
|
|
|
if (server.namespace)
|
2020-02-03 00:58:28 +01:00
|
|
|
log_debug("systemd-journald stopped as PID "PID_FMT" for namespace '%s'.", getpid_cached(), server.namespace);
|
2019-11-25 15:00:38 +01:00
|
|
|
else
|
|
|
|
log_debug("systemd-journald stopped as PID "PID_FMT" for the system.", getpid_cached());
|
|
|
|
|
2017-10-30 20:01:50 +01:00
|
|
|
server_driver_message(&server, 0,
|
tree-wide: add SD_ID128_MAKE_STR, remove LOG_MESSAGE_ID
Embedding sd_id128_t's in constant strings was rather cumbersome. We had
SD_ID128_CONST_STR which returned a const char[], but it had two problems:
- it wasn't possible to statically concatanate this array with a normal string
- gcc wasn't really able to optimize this, and generated code to perform the
"conversion" at runtime.
Because of this, even our own code in coredumpctl wasn't using
SD_ID128_CONST_STR.
Add a new macro to generate a constant string: SD_ID128_MAKE_STR.
It is not as elegant as SD_ID128_CONST_STR, because it requires a repetition
of the numbers, but in practice it is more convenient to use, and allows gcc
to generate smarter code:
$ size .libs/systemd{,-logind,-journald}{.old,}
text data bss dec hex filename
1265204 149564 4808 1419576 15a938 .libs/systemd.old
1260268 149564 4808 1414640 1595f0 .libs/systemd
246805 13852 209 260866 3fb02 .libs/systemd-logind.old
240973 13852 209 255034 3e43a .libs/systemd-logind
146839 4984 34 151857 25131 .libs/systemd-journald.old
146391 4984 34 151409 24f71 .libs/systemd-journald
It is also much easier to check if a certain binary uses a certain MESSAGE_ID:
$ strings .libs/systemd.old|grep MESSAGE_ID
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
$ strings .libs/systemd|grep MESSAGE_ID
MESSAGE_ID=c7a787079b354eaaa9e77b371893cd27
MESSAGE_ID=b07a249cd024414a82dd00cd181378ff
MESSAGE_ID=641257651c1b4ec9a8624d7a40a9e1e7
MESSAGE_ID=de5b426a63be47a7b6ac3eaac82e2f6f
MESSAGE_ID=d34d037fff1847e6ae669a370e694725
MESSAGE_ID=7d4958e842da4a758f6c1cdc7b36dcc5
MESSAGE_ID=1dee0369c7fc4736b7099b38ecb46ee7
MESSAGE_ID=39f53479d3a045ac8e11786248231fbf
MESSAGE_ID=be02cf6855d2428ba40df7e9d022f03d
MESSAGE_ID=7b05ebc668384222baa8881179cfda54
MESSAGE_ID=9d1aaa27d60140bd96365438aad20286
2016-11-06 18:48:23 +01:00
|
|
|
"MESSAGE_ID=" SD_MESSAGE_JOURNAL_STOP_STR,
|
2015-11-08 20:05:55 +01:00
|
|
|
LOG_MESSAGE("Journal stopped"),
|
|
|
|
NULL);
|
2011-12-23 20:50:48 +01:00
|
|
|
|
2011-10-07 21:06:39 +02:00
|
|
|
finish:
|
|
|
|
server_done(&server);
|
|
|
|
|
|
|
|
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
|
|
|
|
}
|