Skip to content

Commit

Permalink
core: implement /run/systemd/units/-based path for passing unit info …
Browse files Browse the repository at this point in the history
…from PID 1 to journald

And let's make use of it to implement two new unit settings with it:

1. LogLevelMax= is a new per-unit setting that may be used to configure
   log priority filtering: set it to LogLevelMax=notice and only
   messages of level "notice" and lower (i.e. more important) will be
   processed, all others are dropped.

2. LogExtraField= is a new per-unit setting for configuring per-unit
   journal fields, that are implicitly included in every log record
   generated by the unit's processes. It takes field/value pairs in the
   form of FOO=BAR.

Also, related to this, one exisiting unit setting is ported to this new
facility:

3. The invocation ID is now pulled from /run/systemd/units/ instead of
   cgroupfs xattrs. This substantially relaxes requirements of systemd
   on the kernel version and the privileges it runs with (specifically,
   cgroupfs xattrs are not available in containers, since they are
   stored in kernel memory, and hence are unsafe to permit to lesser
   privileged code).

/run/systemd/units/ is a new directory, which contains a number of files
and symlinks encoding the above information. PID 1 creates and manages
these files, and journald reads them from there.

Note that this is supposed to be a direct path between PID 1 and the
journal only, due to the special runtime environment the journal runs
in. Normally, today we shouldn't introduce new interfaces that (mis-)use
a file system as IPC framework, and instead just an IPC system, but this
is very hard to do between the journal and PID 1, as long as the IPC
system is a subject PID 1 manages, and itself a client to the journal.

This patch cleans up a couple of types used in journal code:
specifically we switch to size_t for a couple of memory-sizing values,
as size_t is the right choice for everything that is memory.

Fixes: systemd#4089
Fixes: systemd#3041
Fixes: systemd#4441
  • Loading branch information
poettering committed Nov 13, 2017
1 parent 975cab8 commit f0f0fe3
Show file tree
Hide file tree
Showing 26 changed files with 747 additions and 84 deletions.
6 changes: 3 additions & 3 deletions src/basic/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -847,8 +847,8 @@ int log_oom_internal(LogRealm realm, const char *file, int line, const char *fun

int log_format_iovec(
struct iovec *iovec,
unsigned iovec_len,
unsigned *n,
size_t iovec_len,
size_t *n,
bool newline_separator,
int error,
const char *format,
Expand Down Expand Up @@ -928,7 +928,7 @@ int log_struct_internal(
if (journal_fd >= 0) {
char header[LINE_MAX];
struct iovec iovec[17] = {};
unsigned n = 0, i;
size_t n = 0, i;
int r;
struct msghdr mh = {
.msg_iov = iovec,
Expand Down
4 changes: 2 additions & 2 deletions src/basic/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ int log_oom_internal(

int log_format_iovec(
struct iovec *iovec,
unsigned iovec_len,
unsigned *n,
size_t iovec_len,
size_t *n,
bool newline_separator,
int error,
const char *format,
Expand Down
129 changes: 128 additions & 1 deletion src/core/dbus-execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@
#include "af-list.h"
#include "alloc-util.h"
#include "bus-util.h"
#include "capability-util.h"
#include "cap-list.h"
#include "capability-util.h"
#include "dbus-execute.h"
#include "env-util.h"
#include "errno-list.h"
#include "execute.h"
#include "fd-util.h"
#include "fileio.h"
#include "io-util.h"
#include "ioprio.h"
#include "journal-util.h"
#include "missing.h"
#include "mount-util.h"
#include "namespace.h"
Expand Down Expand Up @@ -771,6 +773,37 @@ static int property_get_bind_paths(
return sd_bus_message_close_container(reply);
}

static int property_get_log_extra_fields(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {

ExecContext *c = userdata;
size_t i;
int r;

assert(bus);
assert(c);
assert(property);
assert(reply);

r = sd_bus_message_open_container(reply, 'a', "ay");
if (r < 0)
return r;

for (i = 0; i < c->n_log_extra_fields; i++) {
r = sd_bus_message_append_array(reply, 'y', c->log_extra_fields[i].iov_base, c->log_extra_fields[i].iov_len);
if (r < 0)
return r;
}

return sd_bus_message_close_container(reply);
}

const sd_bus_vtable bus_exec_vtable[] = {
SD_BUS_VTABLE_START(0),
SD_BUS_PROPERTY("Environment", "as", NULL, offsetof(ExecContext, environment), SD_BUS_VTABLE_PROPERTY_CONST),
Expand Down Expand Up @@ -838,6 +871,8 @@ const sd_bus_vtable bus_exec_vtable[] = {
SD_BUS_PROPERTY("SyslogLevelPrefix", "b", bus_property_get_bool, offsetof(ExecContext, syslog_level_prefix), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("SyslogLevel", "i", property_get_syslog_level, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("SyslogFacility", "i", property_get_syslog_facility, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("LogLevelMax", "i", bus_property_get_int, offsetof(ExecContext, log_level_max), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("LogExtraFields", "aay", property_get_log_extra_fields, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("SecureBits", "i", bus_property_get_int, offsetof(ExecContext, secure_bits), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("CapabilityBoundingSet", "t", property_get_capability_bounding_set, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("AmbientCapabilities", "t", property_get_ambient_capabilities, 0, SD_BUS_VTABLE_PROPERTY_CONST),
Expand Down Expand Up @@ -1137,6 +1172,98 @@ int bus_exec_context_set_transient_property(
}

return 1;

} else if (streq(name, "LogLevelMax")) {
int32_t level;

r = sd_bus_message_read(message, "i", &level);
if (r < 0)
return r;

if (!log_level_is_valid(level))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Maximum log level value out of range");

if (mode != UNIT_CHECK) {
c->log_level_max = level;
unit_write_drop_in_private_format(u, mode, name, "LogLevelMax=%i", level);
}

return 1;

} else if (streq(name, "LogExtraFields")) {
size_t n = 0;

r = sd_bus_message_enter_container(message, 'a', "ay");
if (r < 0)
return r;

for (;;) {
_cleanup_free_ void *copy = NULL;
struct iovec *t;
const char *eq;
const void *p;
size_t sz;

/* Note that we expect a byte array for each field, instead of a string. That's because on the
* lower-level journal fields can actually contain binary data and are not restricted to text,
* and we should not "lose precision" in our types on the way. That said, I am pretty sure
* actually encoding binary data as unit metadata is not a good idea. Hence we actually refuse
* any actual binary data, and only accept UTF-8. This allows us to eventually lift this
* limitation, should a good, valid usecase arise. */

r = sd_bus_message_read_array(message, 'y', &p, &sz);
if (r < 0)
return r;
if (r == 0)
break;

if (memchr(p, 0, sz))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Journal field contains zero byte");

eq = memchr(p, '=', sz);
if (!eq)
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Journal field contains no '=' character");
if (!journal_field_valid(p, eq - (const char*) p, false))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Journal field invalid");

if (mode != UNIT_CHECK) {
t = realloc_multiply(c->log_extra_fields, sizeof(struct iovec), c->n_log_extra_fields+1);
if (!t)
return -ENOMEM;
c->log_extra_fields = t;
}

copy = malloc(sz + 1);
if (!copy)
return -ENOMEM;

memcpy(copy, p, sz);
((uint8_t*) copy)[sz] = 0;

if (!utf8_is_valid(copy))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Journal field is not valid UTF-8");

if (mode != UNIT_CHECK) {
c->log_extra_fields[c->n_log_extra_fields++] = IOVEC_MAKE(copy, sz);
unit_write_drop_in_private_format(u, mode, name, "LogExtraField=%s", (char*) copy);

copy = NULL;
}

n++;
}

r = sd_bus_message_exit_container(message);
if (r < 0)
return r;

if (mode != UNIT_CHECK && n == 0) {
exec_context_free_log_extra_fields(c);
unit_write_drop_in_private(u, mode, name, "LogExtraField=");
}

return 1;

} else if (streq(name, "SecureBits")) {
int n;

Expand Down
40 changes: 38 additions & 2 deletions src/core/execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -3483,11 +3483,12 @@ void exec_context_init(ExecContext *c) {
c->directories[i].mode = 0755;
c->capability_bounding_set = CAP_ALL;
c->restrict_namespaces = NAMESPACE_FLAGS_ALL;
c->log_level_max = -1;
}

void exec_context_done(ExecContext *c) {
unsigned l;
ExecDirectoryType i;
size_t l;

assert(c);

Expand Down Expand Up @@ -3534,6 +3535,10 @@ void exec_context_done(ExecContext *c) {

for (i = 0; i < _EXEC_DIRECTORY_TYPE_MAX; i++)
c->directories[i].paths = strv_free(c->directories[i].paths);

c->log_level_max = -1;

exec_context_free_log_extra_fields(c);
}

int exec_context_destroy_runtime_directory(ExecContext *c, const char *runtime_prefix) {
Expand Down Expand Up @@ -3796,9 +3801,9 @@ static void strv_fprintf(FILE *f, char **l) {
}

void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) {
ExecDirectoryType dt;
char **e, **d;
unsigned i;
ExecDirectoryType dt;
int r;

assert(c);
Expand Down Expand Up @@ -3966,6 +3971,26 @@ void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) {
fprintf(f, "%sSyslogLevel: %s\n", prefix, lvl_str);
}

if (c->log_level_max >= 0) {
_cleanup_free_ char *t = NULL;

(void) log_level_to_string_alloc(c->log_level_max, &t);

fprintf(f, "%sLogLevelMax: %s\n", prefix, strna(t));
}

if (c->n_log_extra_fields > 0) {
size_t j;

for (j = 0; j < c->n_log_extra_fields; j++) {
fprintf(f, "%sLogExtraField: ", prefix);
fwrite(c->log_extra_fields[j].iov_base,
1, c->log_extra_fields[j].iov_len,
f);
fputc('\n', f);
}
}

if (c->secure_bits) {
_cleanup_free_ char *str = NULL;

Expand Down Expand Up @@ -4177,6 +4202,17 @@ int exec_context_get_effective_ioprio(ExecContext *c) {
return p;
}

void exec_context_free_log_extra_fields(ExecContext *c) {
size_t l;

assert(c);

for (l = 0; l < c->n_log_extra_fields; l++)
free(c->log_extra_fields[l].iov_base);
c->log_extra_fields = mfree(c->log_extra_fields);
c->n_log_extra_fields = 0;
}

void exec_status_start(ExecStatus *s, pid_t pid) {
assert(s);

Expand Down
7 changes: 7 additions & 0 deletions src/core/execute.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ struct ExecContext {
char *syslog_identifier;
bool syslog_level_prefix;

int log_level_max;

struct iovec* log_extra_fields;
size_t n_log_extra_fields;

bool cpu_sched_reset_on_fork;
bool non_blocking;
bool private_tmp;
Expand Down Expand Up @@ -353,6 +358,8 @@ bool exec_context_maintains_privileges(ExecContext *c);

int exec_context_get_effective_ioprio(ExecContext *c);

void exec_context_free_log_extra_fields(ExecContext *c);

void exec_status_start(ExecStatus *s, pid_t pid);
void exec_status_exit(ExecStatus *s, ExecContext *context, pid_t pid, int code, int status);
void exec_status_dump(ExecStatus *s, FILE *f, const char *prefix);
Expand Down
2 changes: 2 additions & 0 deletions src/core/load-fragment-gperf.gperf.m4
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ $1.SyslogIdentifier, config_parse_unit_string_printf, 0,
$1.SyslogFacility, config_parse_log_facility, 0, offsetof($1, exec_context.syslog_priority)
$1.SyslogLevel, config_parse_log_level, 0, offsetof($1, exec_context.syslog_priority)
$1.SyslogLevelPrefix, config_parse_bool, 0, offsetof($1, exec_context.syslog_level_prefix)
$1.LogLevelMax, config_parse_log_level, 0, offsetof($1, exec_context.log_level_max)
$1.LogExtraField, config_parse_log_extra_field, 0, offsetof($1, exec_context)
$1.Capabilities, config_parse_warn_compat, DISABLED_LEGACY, offsetof($1, exec_context)
$1.SecureBits, config_parse_exec_secure_bits, 0, offsetof($1, exec_context)
$1.CapabilityBoundingSet, config_parse_capability_set, 0, offsetof($1, exec_context.capability_bounding_set)
Expand Down
60 changes: 60 additions & 0 deletions src/core/load-fragment.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
#include "escape.h"
#include "fd-util.h"
#include "fs-util.h"
#include "io-util.h"
#include "ioprio.h"
#include "journal-util.h"
#include "load-fragment.h"
#include "log.h"
#include "missing.h"
Expand Down Expand Up @@ -2331,6 +2333,64 @@ int config_parse_unset_environ(
return 0;
}

int config_parse_log_extra_field(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {

_cleanup_free_ char *k = NULL;
ExecContext *c = data;
Unit *u = userdata;
struct iovec *t;
const char *eq;
int r;

assert(filename);
assert(lvalue);
assert(rvalue);
assert(c);

if (isempty(rvalue)) {
exec_context_free_log_extra_fields(c);
return 0;
}

r = unit_full_printf(u, rvalue, &k);
if (r < 0) {
log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers on %s, ignoring: %m", rvalue);
return 0;
}

eq = strchr(k, '=');
if (!eq) {
log_syntax(unit, LOG_ERR, filename, line, 0, "Log field lacks '=' character, ignoring: %s", k);
return 0;
}

if (!journal_field_valid(k, eq-k, false)) {
log_syntax(unit, LOG_ERR, filename, line, 0, "Log field name is invalid, ignoring: %s", k);
return 0;
}

t = realloc_multiply(c->log_extra_fields, sizeof(struct iovec), c->n_log_extra_fields+1);
if (!t)
return log_oom();

c->log_extra_fields = t;
c->log_extra_fields[c->n_log_extra_fields++] = IOVEC_MAKE_STRING(k);

k = NULL;

return 0;
}

int config_parse_ip_tos(const char *unit,
const char *filename,
unsigned line,
Expand Down
1 change: 1 addition & 0 deletions src/core/load-fragment.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ int config_parse_bind_paths(const char *unit, const char *filename, unsigned lin
int config_parse_exec_keyring_mode(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
int config_parse_job_timeout_sec(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
int config_parse_job_running_timeout_sec(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
int config_parse_log_extra_field(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);

/* gperf prototypes */
const struct ConfigPerfItem* load_fragment_gperf_lookup(const char *key, GPERF_LEN_TYPE length);
Expand Down
Loading

0 comments on commit f0f0fe3

Please sign in to comment.