1
0
mirror of https://github.com/systemd/systemd synced 2026-03-31 20:24:50 +02:00

Compare commits

...

12 Commits

Author SHA1 Message Date
Zbigniew Jędrzejewski-Szmek
00725c3c4f
Make journal persistent by default (#39624)
See second commit for details.
2025-11-13 09:43:00 +01:00
Daan De Meyer
c50a8fc22f
test: use new macros and move several test cases (#39708) 2025-11-13 09:42:12 +01:00
Mike Yuan
6dd412bc50 time-util: do not carry musl-specific fallback logic on glibc systems
Follow-up for 3ac4d68498dd378e2b3acd2bb86f4700263532d0

We have no sensible way to detect why strptime() fails, hence
the fallback path as it is now would fire on glibc systems too,
pointlessly. Let's guard it behind ifdeffery.
2025-11-13 09:40:20 +01:00
Yu Watanabe
4a028994df user-util: fix typo
Follow-up for b10fd796f56e4f16f7430cd22f59f544766d3bef.
2025-11-13 15:36:47 +09:00
Yu Watanabe
e9c5af74ea test: move several test cases to test-tmpfile-util.c 2025-11-13 09:19:56 +09:00
Yu Watanabe
6300e2a9ee test: move several test cases to test-env-file.c 2025-11-13 09:19:56 +09:00
Yu Watanabe
cd62f9ad71 test-fileio: migrate to use new ASSER_OK() macro and friends
This also replaces fgets() with read_full_file().
2025-11-13 09:19:28 +09:00
Zbigniew Jędrzejewski-Szmek
2eaceed0b1 journald: do not allow persistent journal storage in the initrd
If the user managed to configure persistent storage in the journal
in the initrd, e.g. by creating /var/log/journal with default of 'auto',
we could end up writing entries there. Let's make sure this doesn't
happen.
2025-11-13 00:08:31 +01:00
Zbigniew Jędrzejewski-Szmek
238bb65c0f meson: make persistent journal the default 2025-11-13 00:08:31 +01:00
Zbigniew Jędrzejewski-Szmek
7af88c1e1e journald: allow default storage mode to be configured
So far the idea was that the default is 'auto', and if appropriate, the
distribution will create /var/log/journal/ to tell journald to use persistent
mode. This doesn't work well with factory resets, because after a factory reset
obviously /var/log is gone. That old default was useful when journald was new
and people were reluctant to enable persistent mode and instead relied on
rsyslog and such for the persistent storage. But nowadays that is rarer, and
anyway various features like user journals only work with persistent storage,
so we want people to enable this by default. Add an option to flip the default
and distributions can opt in. The default default value remains unchanged.

(I also tested using tmpfiles to instead change this, since we already set
access mode for /var/log/journal through tmpfiles. Unfortunately, tmpfiles runs
too late, after journald has already started, so if tmpfiles creates the
directory, it'll only be used after a reboot. This probably could be made to
work by adding a new service to flush the journal, but that becomes complicated
and we lose the main advantage of simplicity.)

Resolves https://bugzilla.redhat.com/show_bug.cgi?id=1387796.
2025-11-13 00:08:31 +01:00
Zbigniew Jędrzejewski-Szmek
7e9a787a86 man: stop inventing custom entity names for docbook
For some reason, the entity names configured in custom-entities.ent
used abbreviated names. This just creates unnecessary confusion, so update
to use the same name as the config dict.

Reword some surrounding sentences while at it.
2025-11-13 00:08:27 +01:00
Zbigniew Jędrzejewski-Szmek
e601bed945 meson: make journald.conf templated
A noop, in preparation for subsequent changes.
2025-11-13 00:07:12 +01:00
15 changed files with 647 additions and 654 deletions

6
NEWS
View File

@ -19,6 +19,12 @@ CHANGES WITH 259 in spe:
command to add recovery keys to existing user accounts. Previously,
recovery keys could only be configured during initial user creation.
* The default storage mode for the journal is now 'persistent'.
Previously, the default was 'auto', so the presence or lack of
/var/log/journal determined the default storage mode, if no
overriding configuration was provided. The default can be changed
with -Djournal-storage-default=.
CHANGES WITH 258:
Incompatible changes:

View File

@ -10,13 +10,14 @@
<!ENTITY FALLBACK_HOSTNAME "{{FALLBACK_HOSTNAME}}">
<!ENTITY MEMORY_ACCOUNTING_DEFAULT "{{ 'yes' if MEMORY_ACCOUNTING_DEFAULT else 'no' }}">
<!ENTITY KILL_USER_PROCESSES "{{ 'yes' if KILL_USER_PROCESSES else 'no' }}">
<!ENTITY JOURNAL_STORAGE_DEFAULT "{{JOURNAL_STORAGE_DEFAULT}}">
<!ENTITY DEBUGTTY "{{DEBUGTTY}}">
<!ENTITY SYSTEM_SYSVRCLOCAL_PATH "{{SYSTEM_SYSVRCLOCAL_PATH}}">
<!ENTITY HIGH_RLIMIT_NOFILE "{{HIGH_RLIMIT_NOFILE}}">
<!ENTITY DEFAULT_DNSSEC_MODE "{{DEFAULT_DNSSEC_MODE_STR}}">
<!ENTITY DEFAULT_DNS_OVER_TLS_MODE "{{DEFAULT_DNS_OVER_TLS_MODE_STR}}">
<!ENTITY DEFAULT_TIMEOUT "{{DEFAULT_TIMEOUT_SEC}} s">
<!ENTITY DEFAULT_USER_TIMEOUT "{{DEFAULT_USER_TIMEOUT_SEC}} s">
<!ENTITY DEFAULT_KEYMAP "{{SYSTEMD_DEFAULT_KEYMAP}}">
<!ENTITY DEFAULT_TIMEOUT_SEC "{{DEFAULT_TIMEOUT_SEC}} s">
<!ENTITY DEFAULT_USER_TIMEOUT_SEC "{{DEFAULT_USER_TIMEOUT_SEC}} s">
<!ENTITY SYSTEMD_DEFAULT_KEYMAP "{{SYSTEMD_DEFAULT_KEYMAP}}">
<!ENTITY fedora_latest_version "43">
<!ENTITY fedora_cloud_release "1.6">

View File

@ -1,6 +1,9 @@
<?xml version='1.0'?> <!--*-nxml-*-->
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
<!ENTITY % entities SYSTEM "custom-entities.ent" >
%entities;
]>
<!-- SPDX-License-Identifier: LGPL-2.1-or-later -->
<refentry id="journald.conf"
@ -82,8 +85,9 @@
<filename>/var/log/journal</filename> directory exists, and <literal>volatile</literal> otherwise
(the existence of the directory controls the storage mode). <literal>none</literal> turns off all
storage, all log data received will be dropped (but forwarding to other targets, such as the console,
the kernel log buffer, or a syslog socket will still work). Defaults to <literal>auto</literal> in
the default journal namespace, and <literal>persistent</literal> in all others.</para>
the kernel log buffer, or a syslog socket will still work). Defaults to
<literal>&JOURNAL_STORAGE_DEFAULT;</literal> in the default journal namespace (this value is
determined at compilation time), and <literal>persistent</literal> in all others.</para>
<para>Note that journald will initially use volatile storage, until a call to
<command>journalctl --flush</command> (or sending <constant>SIGUSR1</constant> to journald) will cause

View File

@ -581,9 +581,9 @@
</para>
<para><varname>DefaultTimeoutStartSec=</varname> and <varname>DefaultTimeoutStopSec=</varname>
default to &DEFAULT_TIMEOUT; in the system manager and &DEFAULT_USER_TIMEOUT; in the user manager.
<varname>DefaultTimeoutAbortSec=</varname> is not set by default so that all units fall back to
<varname>TimeoutStopSec=</varname>. <varname>DefaultRestartSec=</varname> defaults to 100 ms.
default to &DEFAULT_TIMEOUT_SEC; in the system manager and &DEFAULT_USER_TIMEOUT_SEC; in the user
manager. <varname>DefaultTimeoutAbortSec=</varname> is not set by default so that all units fall back
to <varname>TimeoutStopSec=</varname>. <varname>DefaultRestartSec=</varname> defaults to 100 ms.
</para>
<xi:include href="version-info.xml" xpointer="v209"/></listitem>
@ -597,8 +597,8 @@
and <filename>/etc/crypttab</filename> (see
<citerefentry><refentrytitle>systemd.mount</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
<citerefentry><refentrytitle>crypttab</refentrytitle><manvolnum>5</manvolnum></citerefentry>).
Defaults to &DEFAULT_TIMEOUT; in the system manager and &DEFAULT_USER_TIMEOUT; in the user manager.
</para>
Defaults to &DEFAULT_TIMEOUT_SEC; in the system manager and &DEFAULT_USER_TIMEOUT_SEC; in the user
manager.</para>
<xi:include href="version-info.xml" xpointer="v252"/></listitem>
</varlistentry>

View File

@ -75,11 +75,11 @@
<term><varname>KEYMAP=</varname></term>
<term><varname>KEYMAP_TOGGLE=</varname></term>
<listitem><para>Configures the key mapping table for the keyboard. <varname>KEYMAP=</varname>
defaults to <literal>&DEFAULT_KEYMAP;</literal> if not set. Specially, if <literal>@kernel</literal>
is specified, no keymap will be loaded, i.e. the kernel's default keymap is used. The
<varname>KEYMAP_TOGGLE=</varname> can be used to configure a second toggle keymap and is by default
unset.</para></listitem>
<listitem><para>Configures the key mapping table for the keyboard. <varname>KEYMAP=</varname>
defaults to <literal>&SYSTEMD_DEFAULT_KEYMAP;</literal> if not set. As a special case, if
<literal>@kernel</literal> is specified, no keymap will be loaded, i.e. the kernel's default keymap
will be used. The <varname>KEYMAP_TOGGLE=</varname> can be used to configure a second toggle keymap
and is by default unset.</para></listitem>
</varlistentry>
<varlistentry>

View File

@ -117,6 +117,9 @@ conf.set10('BUMP_PROC_SYS_FS_FILE_MAX', get_option('bump-proc-sys-fs-file-max'))
conf.set10('BUMP_PROC_SYS_FS_NR_OPEN', get_option('bump-proc-sys-fs-nr-open'))
conf.set('HIGH_RLIMIT_NOFILE', 512*1024)
conf.set('JOURNAL_STORAGE_DEFAULT', get_option('journal-storage-default'))
conf.set('JOURNAL_STORAGE_DEFAULT_VAL', 'STORAGE_' + get_option('journal-storage-default').to_upper())
# Meson ignores the preceding arguments when joining paths if an absolute
# component is encountered, so this should canonicalize various paths when they
# are absolute or relative.
@ -3096,6 +3099,7 @@ summary({
'default locale' : default_locale,
'default nspawn locale' : nspawn_locale,
'default status unit format' : status_unit_format_default,
'default journal storage mode' : conf.get('JOURNAL_STORAGE_DEFAULT'),
'default user $PATH' : default_user_path != '' ? default_user_path : '(same as system services)',
'systemd service watchdog' : service_watchdog == '' ? 'disabled' : service_watchdog,
'time epoch' : f'@time_epoch@ (@alt_time_epoch@)',

View File

@ -139,6 +139,8 @@ option('timedated', type : 'boolean',
description : 'install the systemd-timedated daemon')
option('timesyncd', type : 'boolean',
description : 'install the systemd-timesyncd daemon')
option('journal-storage-default', type : 'combo', choices : ['persistent', 'auto', 'volatile', 'none'],
description : 'default storage mode for journald (main namespace)')
option('remote', type : 'feature', deprecated : { 'true' : 'enabled', 'false' : 'disabled' },
description : 'support for "journal over the network"')
option('create-log-dirs', type : 'boolean',

View File

@ -634,8 +634,6 @@ const char* get_tzname(bool dst) {
}
int parse_gmtoff(const char *t, long *ret) {
int r;
assert(t);
struct tm tm;
@ -647,6 +645,11 @@ int parse_gmtoff(const char *t, long *ret) {
return 0;
}
#ifdef __GLIBC__
return -EINVAL;
#else
int r;
/* musl v1.2.5 does not support %z specifier in strptime(). Since
* https://github.com/kraj/musl/commit/fced99e93daeefb0192fd16304f978d4401d1d77
* %z is supported, but it only supports strict RFC-822/ISO 8601 format, that is, 4 digits with sign
@ -714,6 +717,7 @@ finalize:
}
return 0;
#endif
}
static int parse_timestamp_impl(

View File

@ -831,7 +831,7 @@ char* mangle_gecos(const char *d) {
char *mangled;
/* Makes sure the provided string becomes valid as a GEGOS field, by dropping bad chars. glibc's
* putwent() only changes \n and : to spaces. We do more: replace all CC too, and remove invalid
* putpwent() only changes \n and : to spaces. We do more: replace all CC too, and remove invalid
* UTF-8 */
mangled = strdup(d);

View File

@ -5,6 +5,7 @@
#include "conf-parser.h"
#include "creds-util.h"
#include "daemon-util.h"
#include "initrd-util.h"
#include "journald-audit.h"
#include "journald-config.h"
#include "journald-context.h"
@ -116,7 +117,12 @@ void manager_merge_configs(Manager *m) {
journal_config_done(&m->config);
MERGE_NON_NEGATIVE(storage, STORAGE_AUTO);
MERGE_NON_NEGATIVE(storage, JOURNAL_STORAGE_DEFAULT_VAL);
/* In the initrd, only 'none' and 'volatile' make sense. */
if (in_initrd() && IN_SET(m->config.storage, STORAGE_PERSISTENT, STORAGE_AUTO))
m->config.storage = STORAGE_VOLATILE;
manager_merge_journal_compress_options(m);
MERGE_NON_NEGATIVE(seal, true);
/* By default, /dev/kmsg is read only by the main namespace instance. */

View File

@ -17,7 +17,7 @@
# See journald.conf(5) for details.
[Journal]
#Storage=auto
#Storage={{ JOURNAL_STORAGE_DEFAULT }}
#Compress=yes
#Seal=yes
#SplitMode=uid

View File

@ -153,10 +153,12 @@ executables += [
},
]
if install_sysconfdir_samples
install_data('journald.conf',
install_dir : pkgconfigfiledir)
endif
custom_target(
input : 'journald.conf.in',
output: 'journald.conf',
command : [jinja2_cmdline, '@INPUT@', '@OUTPUT@'],
install : install_sysconfdir_samples,
install_dir : pkgconfigfiledir)
if get_option('create-log-dirs')
install_emptydir('/var/log/journal',

View File

@ -1,6 +1,7 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "env-file.h"
#include "env-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "fs-util.h"
@ -187,4 +188,315 @@ TEST(write_and_load_env_file) {
}
}
TEST(parse_env_file) {
_cleanup_(unlink_tempfilep) char
t[] = "/tmp/test-fileio-in-XXXXXX",
p[] = "/tmp/test-fileio-out-XXXXXX";
FILE *f;
_cleanup_free_ char *one = NULL, *two = NULL, *three = NULL, *four = NULL, *five = NULL,
*six = NULL, *seven = NULL, *eight = NULL, *nine = NULL, *ten = NULL,
*eleven = NULL, *twelve = NULL, *thirteen = NULL;
_cleanup_strv_free_ char **a = NULL, **b = NULL;
unsigned k;
ASSERT_OK(fmkostemp_safe(t, "w", &f));
fputs("one=BAR \n"
"# comment\n"
" # comment \n"
" ; comment \n"
" two = bar \n"
"invalid line\n"
"invalid line #comment\n"
"three = \"333\n"
"xxxx\"\n"
"four = \'44\\\"44\'\n"
"five = \"55\\\"55\" \"FIVE\" cinco \n"
"six = seis sechs\\\n"
" sis\n"
"seven=\"sevenval\" #nocomment\n"
"eight=eightval #nocomment\n"
"export nine=nineval\n"
"ten=ignored\n"
"ten=ignored\n"
"ten=\n"
"eleven=\\value\n"
"twelve=\"\\value\"\n"
"thirteen='\\value'", f);
fflush(f);
fclose(f);
ASSERT_OK(load_env_file(NULL, t, &a));
STRV_FOREACH(i, a)
log_debug("Got: <%s>", *i);
ASSERT_STREQ(a[0], "one=BAR");
ASSERT_STREQ(a[1], "two=bar");
ASSERT_STREQ(a[2], "three=333\nxxxx");
ASSERT_STREQ(a[3], "four=44\\\"44");
ASSERT_STREQ(a[4], "five=55\"55FIVEcinco");
ASSERT_STREQ(a[5], "six=seis sechs sis");
ASSERT_STREQ(a[6], "seven=sevenval#nocomment");
ASSERT_STREQ(a[7], "eight=eightval #nocomment");
ASSERT_STREQ(a[8], "export nine=nineval");
ASSERT_STREQ(a[9], "ten=");
ASSERT_STREQ(a[10], "eleven=value");
ASSERT_STREQ(a[11], "twelve=\\value");
ASSERT_STREQ(a[12], "thirteen=\\value");
ASSERT_NULL(a[13]);
strv_env_clean(a);
k = 0;
STRV_FOREACH(i, b) {
log_debug("Got2: <%s>", *i);
ASSERT_STREQ(*i, a[k++]);
}
ASSERT_OK(parse_env_file(NULL, t,
"one", &one,
"two", &two,
"three", &three,
"four", &four,
"five", &five,
"six", &six,
"seven", &seven,
"eight", &eight,
"export nine", &nine,
"ten", &ten,
"eleven", &eleven,
"twelve", &twelve,
"thirteen", &thirteen));
log_debug("one=[%s]", strna(one));
log_debug("two=[%s]", strna(two));
log_debug("three=[%s]", strna(three));
log_debug("four=[%s]", strna(four));
log_debug("five=[%s]", strna(five));
log_debug("six=[%s]", strna(six));
log_debug("seven=[%s]", strna(seven));
log_debug("eight=[%s]", strna(eight));
log_debug("export nine=[%s]", strna(nine));
log_debug("ten=[%s]", strna(nine));
log_debug("eleven=[%s]", strna(eleven));
log_debug("twelve=[%s]", strna(twelve));
log_debug("thirteen=[%s]", strna(thirteen));
ASSERT_STREQ(one, "BAR");
ASSERT_STREQ(two, "bar");
ASSERT_STREQ(three, "333\nxxxx");
ASSERT_STREQ(four, "44\\\"44");
ASSERT_STREQ(five, "55\"55FIVEcinco");
ASSERT_STREQ(six, "seis sechs sis");
ASSERT_STREQ(seven, "sevenval#nocomment");
ASSERT_STREQ(eight, "eightval #nocomment");
ASSERT_STREQ(nine, "nineval");
ASSERT_NULL(ten);
ASSERT_STREQ(eleven, "value");
ASSERT_STREQ(twelve, "\\value");
ASSERT_STREQ(thirteen, "\\value");
/* prepare a temporary file to write the environment to */
_cleanup_close_ int fd = -EBADF;
ASSERT_OK(fd = mkostemp_safe(p));
ASSERT_OK(write_env_file(AT_FDCWD, p, /* headers= */ NULL, a, /* flags= */ 0));
ASSERT_OK(load_env_file(NULL, p, &b));
}
static void test_one_shell_var(const char *file, const char *variable, const char *value) {
_cleanup_free_ char *cmd = NULL, *from_shell = NULL;
_cleanup_pclose_ FILE *f = NULL;
size_t sz;
ASSERT_NOT_NULL(cmd = strjoin(". ", file, " && /bin/echo -n \"$", variable, "\""));
ASSERT_NOT_NULL(f = popen(cmd, "re"));
ASSERT_OK(read_full_stream(f, &from_shell, &sz));
ASSERT_EQ(sz, strlen(value));
ASSERT_STREQ(from_shell, value);
}
TEST(parse_multiline_env_file) {
_cleanup_(unlink_tempfilep) char
t[] = "/tmp/test-fileio-in-XXXXXX",
p[] = "/tmp/test-fileio-out-XXXXXX";
FILE *f;
_cleanup_strv_free_ char **a = NULL, **b = NULL;
ASSERT_OK(fmkostemp_safe(t, "w", &f));
fputs("one=BAR\\\n"
"\\ \\ \\ \\ VAR\\\n"
"\\\tGAR\n"
"#comment\n"
"two=\"bar\\\n"
" var\\\n"
"\tgar\"\n"
"#comment\n"
"tri=\"bar \\\n"
" var \\\n"
"\tgar \"\n", f);
ASSERT_OK(fflush_and_check(f));
fclose(f);
test_one_shell_var(t, "one", "BAR VAR\tGAR");
test_one_shell_var(t, "two", "bar var\tgar");
test_one_shell_var(t, "tri", "bar var \tgar ");
ASSERT_OK(load_env_file(NULL, t, &a));
STRV_FOREACH(i, a)
log_debug("Got: <%s>", *i);
ASSERT_STREQ(a[0], "one=BAR VAR\tGAR");
ASSERT_STREQ(a[1], "two=bar var\tgar");
ASSERT_STREQ(a[2], "tri=bar var \tgar ");
ASSERT_NULL(a[3]);
_cleanup_close_ int fd = -EBADF;
ASSERT_OK(fd = mkostemp_safe(p));
ASSERT_OK(write_env_file(AT_FDCWD, p, /* headers= */ NULL, a, /* flags= */ 0));
ASSERT_OK(load_env_file(NULL, p, &b));
}
TEST(merge_env_file) {
_cleanup_(unlink_tempfilep) char t[] = "/tmp/test-fileio-XXXXXX";
_cleanup_fclose_ FILE *f = NULL;
_cleanup_strv_free_ char **a = NULL;
ASSERT_OK(fmkostemp_safe(t, "w", &f));
ASSERT_OK(write_string_stream(f,
"one=1 \n"
"twelve=${one}2\n"
"twentyone=2${one}\n"
"one=2\n"
"twentytwo=2${one}\n"
"xxx_minus_three=$xxx - 3\n"
"xxx=0x$one$one$one\n"
"yyy=${one:-fallback}\n"
"zzz=${one:+replacement}\n"
"zzzz=${foobar:-${nothing}}\n"
"zzzzz=${nothing:+${nothing}}\n"
, WRITE_STRING_FILE_AVOID_NEWLINE));
ASSERT_OK(merge_env_file(&a, NULL, t));
strv_sort(a);
STRV_FOREACH(i, a)
log_debug("Got: <%s>", *i);
ASSERT_STREQ(a[0], "one=2");
ASSERT_STREQ(a[1], "twelve=12");
ASSERT_STREQ(a[2], "twentyone=21");
ASSERT_STREQ(a[3], "twentytwo=22");
ASSERT_STREQ(a[4], "xxx=0x222");
ASSERT_STREQ(a[5], "xxx_minus_three= - 3");
ASSERT_STREQ(a[6], "yyy=2");
ASSERT_STREQ(a[7], "zzz=replacement");
ASSERT_STREQ(a[8], "zzzz=");
ASSERT_STREQ(a[9], "zzzzz=");
ASSERT_NULL(a[10]);
ASSERT_OK(merge_env_file(&a, NULL, t));
strv_sort(a);
STRV_FOREACH(i, a)
log_debug("Got2: <%s>", *i);
ASSERT_STREQ(a[0], "one=2");
ASSERT_STREQ(a[1], "twelve=12");
ASSERT_STREQ(a[2], "twentyone=21");
ASSERT_STREQ(a[3], "twentytwo=22");
ASSERT_STREQ(a[4], "xxx=0x222");
ASSERT_STREQ(a[5], "xxx_minus_three=0x222 - 3");
ASSERT_STREQ(a[6], "yyy=2");
ASSERT_STREQ(a[7], "zzz=replacement");
ASSERT_STREQ(a[8], "zzzz=");
ASSERT_STREQ(a[9], "zzzzz=");
ASSERT_NULL(a[10]);
}
TEST(merge_env_file_invalid) {
_cleanup_(unlink_tempfilep) char t[] = "/tmp/test-fileio-XXXXXX";
_cleanup_fclose_ FILE *f = NULL;
_cleanup_strv_free_ char **a = NULL;
ASSERT_OK(fmkostemp_safe(t, "w", &f));
ASSERT_OK(write_string_stream(f,
"unset one \n"
"unset one= \n"
"unset one=1 \n"
"one \n"
"one = \n"
"one two =\n"
"\x20two=\n"
"#comment=comment\n"
";comment2=comment2\n"
"#\n"
"\n\n" /* empty line */
, WRITE_STRING_FILE_AVOID_NEWLINE));
ASSERT_OK(merge_env_file(&a, NULL, t));
STRV_FOREACH(i, a)
log_debug("Got: <%s>", *i);
ASSERT_TRUE(strv_isempty(a));
}
static void check_file_pairs_one(char **l) {
ASSERT_EQ(strv_length(l), 14U);
STRV_FOREACH_PAIR(k, v, l) {
if (streq(*k, "NAME"))
ASSERT_STREQ(*v, "Arch Linux");
else if (streq(*k, "ID"))
ASSERT_STREQ(*v, "arch");
else if (streq(*k, "PRETTY_NAME"))
ASSERT_STREQ(*v, "Arch Linux");
else if (streq(*k, "ANSI_COLOR"))
ASSERT_STREQ(*v, "0;36");
else if (streq(*k, "HOME_URL"))
ASSERT_STREQ(*v, "https://www.archlinux.org/");
else if (streq(*k, "SUPPORT_URL"))
ASSERT_STREQ(*v, "https://bbs.archlinux.org/");
else if (streq(*k, "BUG_REPORT_URL"))
ASSERT_STREQ(*v, "https://bugs.archlinux.org/");
else
assert_not_reached();
}
}
TEST(load_env_file_pairs) {
_cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-load_env_file_pairs-XXXXXX";
_cleanup_fclose_ FILE *f = NULL;
_cleanup_strv_free_ char **l = NULL;
int fd;
ASSERT_OK(fd = mkostemp_safe(fn));
ASSERT_OK(write_string_file(fn,
"NAME=\"Arch Linux\"\n"
"ID=arch\n"
"PRETTY_NAME=\"Arch Linux\"\n"
"ANSI_COLOR=\"0;36\"\n"
"HOME_URL=\"https://www.archlinux.org/\"\n"
"SUPPORT_URL=\"https://bbs.archlinux.org/\"\n"
"BUG_REPORT_URL=\"https://bugs.archlinux.org/\"\n"
, WRITE_STRING_FILE_CREATE));
ASSERT_OK(load_env_file_pairs_fd(fd, fn, &l));
check_file_pairs_one(l);
l = strv_free(l);
ASSERT_NOT_NULL(f = fdopen(fd, "r"));
ASSERT_OK(load_env_file_pairs(f, fn, &l));
check_file_pairs_one(l);
}
DEFINE_TEST_MAIN(LOG_INFO);

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
@ -10,6 +11,7 @@
#include "fileio.h"
#include "format-util.h"
#include "fs-util.h"
#include "iovec-util.h"
#include "log.h"
#include "path-util.h"
#include "process-util.h"
@ -17,6 +19,62 @@
#include "tests.h"
#include "tmpfile-util.h"
TEST(writing_tmpfile) {
_cleanup_(unlink_tempfilep) char name[] = "/tmp/test-systemd_writing_tmpfile.XXXXXX";
_cleanup_free_ char *contents = NULL;
size_t size;
_cleanup_close_ int fd = -EBADF;
struct iovec iov[] = {
IOVEC_MAKE_STRING("abc\n"),
IOVEC_MAKE_STRING(ALPHANUMERICAL "\n"),
IOVEC_MAKE_STRING(""),
};
ASSERT_OK(fd = mkostemp_safe(name));
ASSERT_OK_ERRNO(writev(fd, iov, 3));
ASSERT_OK(read_full_file(name, &contents, &size));
ASSERT_STREQ(contents, "abc\n" ALPHANUMERICAL "\n");
}
TEST(tempfn) {
char *ret = NULL, *p;
ASSERT_OK(tempfn_xxxxxx("/foo/bar/waldo", NULL, &ret));
ASSERT_STREQ(ret, "/foo/bar/.#waldoXXXXXX");
free(ret);
ASSERT_OK(tempfn_xxxxxx("/foo/bar/waldo", "[miau]", &ret));
ASSERT_STREQ(ret, "/foo/bar/.#[miau]waldoXXXXXX");
free(ret);
ASSERT_OK(tempfn_random("/foo/bar/waldo", NULL, &ret));
ASSERT_NOT_NULL(p = startswith(ret, "/foo/bar/.#waldo"));
ASSERT_EQ(strlen(p), 16U);
ASSERT_TRUE(in_charset(p, "0123456789abcdef"));
free(ret);
ASSERT_OK(tempfn_random("/foo/bar/waldo", "[wuff]", &ret));
ASSERT_NOT_NULL(p = startswith(ret, "/foo/bar/.#[wuff]waldo"));
ASSERT_EQ(strlen(p), 16U);
ASSERT_TRUE(in_charset(p, "0123456789abcdef"));
free(ret);
ASSERT_OK(tempfn_random_child("/foo/bar/waldo", NULL, &ret));
ASSERT_NOT_NULL(p = startswith(ret, "/foo/bar/waldo/.#"));
ASSERT_EQ(strlen(p), 16U);
ASSERT_TRUE(in_charset(p, "0123456789abcdef"));
free(ret);
ASSERT_OK(tempfn_random_child("/foo/bar/waldo", "[kikiriki]", &ret));
ASSERT_NOT_NULL(p = startswith(ret, "/foo/bar/waldo/.#[kikiriki]"));
ASSERT_EQ(strlen(p), 16U);
ASSERT_TRUE(in_charset(p, "0123456789abcdef"));
free(ret);
}
static void test_tempfn_random_one(const char *p, const char *extra, const char *expect, int ret) {
_cleanup_free_ char *s = NULL;
int r;