1
0
mirror of https://github.com/systemd/systemd synced 2026-04-07 07:34:50 +02:00

Compare commits

..

9 Commits

Author SHA1 Message Date
Luca Boccassi
21d00e52db man/kernel-command-line: add reference to getty_auto variable
Follow-up for #21422
2021-11-18 15:29:43 +00:00
Luca Boccassi
26b2832992
Merge pull request #21424 from keszybz/json-double
Use double and int64_t types in json
2021-11-18 13:37:20 +00:00
Luca Boccassi
ee3fddcc8a getty-generator: add kernel cmdline and env vars to disable it
systemd.getty_auto/rd.systemd.getty_auto/SYSTEMD_GETTY_AUTO can be used
to disable the generator. Enabled by default.
2021-11-18 10:38:48 +00:00
Lennart Poettering
3510cef8fa
Merge pull request #21401 from poettering/open-mkdir-at
add open_mkdir_at() helper and use it
2021-11-18 10:13:26 +01:00
Zbigniew Jędrzejewski-Szmek
718ca77232 shared/json: use int64_t instead of intmax_t
We were already asserting that the intmax_t and uintmax_t types
are the same as int64_t and uint64_t. Pretty much everywhere in
the code base we use the latter types. In principle intmax_t could
be something different on some new architecture, and then the code would
fail to compile or behave differently. We actually do not want the code
to behave differently on those architectures, because that'd break
interoperability. So let's just use int64_t/uint64_t since that's what
we indend to use.
2021-11-18 01:34:31 +01:00
Zbigniew Jędrzejewski-Szmek
a810dd5c00 test-sizeof: add intmax types 2021-11-18 01:34:31 +01:00
Zbigniew Jędrzejewski-Szmek
337712e777 shared/json: stop using long double
It seems that the implementation of long double on ppc64el doesn't really work:
long double cast to integer and back compares as unequal to itself. Strangely,
this effect happens without optimization and both with gcc and clang, so it
seems to be an effect of how long double is implemented by the architecture.

Dumping the values shows the following pattern:
00 00 00 00 00 00 24 40  00 00 00 00 00 00 00 00   # long double v = 10;
00 00 00 00 00 00 24 40  00 00 00 00 00 00 80 39   # (long double)(intmax_t) v

Instead of trying to make this work, I think it's most reasonable to switch to
normal doubles. Notably, we had no tests for floating point behaviour. The
first test we added (for values even not in the range outside of double),
showed failures.

Common implementations of JSON (in particular JavaScript) use 64 bit double.
If we stick to this, users are likely to be happy when they exchange data with
those tools. Exporting values that cannot be represented in other tools would
just cause interop problems.

I don't think the extra precision would be much used. Long double seems to make
most sense as a transient format used in calculations to get extra precision in
operations, and not a storage or exchange format. So I expect low-level
numerical routines that have to know about hardware to make use of it, but it
shouldn't be used by our (higher-level) system library. In particular, we would
have to add tests for implementations conforming to IEEE 754, and those that
don't conform, and account for various implementation differences. It just
doesn't seem worth the effort.

https://en.wikipedia.org/wiki/Long_double#Implementations shows that the
situation is "complicated":

> On the x86 architecture, most C compilers implement long double as the 80-bit
> extended precision type supported by x86 hardware. An exception is Microsoft
> Visual C++ for x86, which makes long double a synonym for double. The Intel
> C++ compiler on Microsoft Windows supports extended precision, but requires
> the /Qlong‑double switch for long double to correspond to the hardware's
> extended precision format.

> Compilers may also use long double for the IEEE 754 quadruple-precision
> binary floating-point format (binary128). This is the case on HP-UX,
> Solaris/SPARC, MIPS with the 64-bit or n32 ABI, 64-bit ARM (AArch64) (on
> operating systems using the standard AAPCS calling conventions, such as
> Linux), and z/OS with FLOAT(IEEE). Most implementations are in software, but
> some processors have hardware support.

> On some PowerPC and SPARCv9 machines, long double is implemented as a
> double-double arithmetic, where a long double value is regarded as the exact
> sum of two double-precision values, giving at least a 106-bit precision; with
> such a format, the long double type does not conform to the IEEE
> floating-point standard. Otherwise, long double is simply a synonym for
> double (double precision), e.g. on 32-bit ARM, 64-bit ARM (AArch64) (on
> Windows and macOS) and on 32-bit MIPS (old ABI, a.k.a. o32).

> With the GNU C Compiler, long double is 80-bit extended precision on x86
> processors regardless of the physical storage used for the type (which can be
> either 96 or 128 bits). On some other architectures, long double can be
> double-double (e.g. on PowerPC) or 128-bit quadruple precision (e.g. on
> SPARC). As of gcc 4.3, a quadruple precision is also supported on x86, but as
> the nonstandard type __float128 rather than long double.

> Although the x86 architecture, and specifically the x87 floating-point
> instructions on x86, supports 80-bit extended-precision operations, it is
> possible to configure the processor to automatically round operations to
> double (or even single) precision. Conversely, in extended-precision mode,
> extended precision may be used for intermediate compiler-generated
> calculations even when the final results are stored at a lower precision
> (i.e. FLT_EVAL_METHOD == 2). With gcc on Linux, 80-bit extended precision is
> the default; on several BSD operating systems (FreeBSD and OpenBSD),
> double-precision mode is the default, and long double operations are
> effectively reduced to double precision. (NetBSD 7.0 and later, however,
> defaults to 80-bit extended precision). However, it is possible to override
> this within an individual program via the FLDCW "floating-point load
> control-word" instruction. On x86_64, the BSDs default to 80-bit extended
> precision. Microsoft Windows with Visual C++ also sets the processor in
> double-precision mode by default, but this can again be overridden within an
> individual program (e.g. by the _controlfp_s function in Visual C++). The
> Intel C++ Compiler for x86, on the other hand, enables extended-precision
> mode by default. On IA-32 OS X, long double is 80-bit extended precision.

So, in short, the only thing that can be said is that nothing can be said. In
common scenarios, we are getting only a bit of extra precision (80 bits instead
of 64), but use space for padding. In other scenarios we are getting no extra
precision. And the variance in implementations is a big issue: we can expect
strange differences in behaviour between architectures, systems, compiler
versions, compilation options, and even the other things that the program is
doing.

Fixes #21390.
2021-11-18 01:34:31 +01:00
Lennart Poettering
96603ea076 tree-wide: port various places over to open_mkdir_at() 2021-11-17 21:48:29 +01:00
Lennart Poettering
c73094f3ac fs-util: add new helper open_mkdir_at() 2021-11-17 21:48:18 +01:00
26 changed files with 385 additions and 198 deletions

View File

@ -371,6 +371,16 @@
</listitem>
</varlistentry>
<varlistentry>
<term><varname>systemd.getty_auto=</varname></term>
<listitem>
<para>Configures whether the <filename>serial-getty@.service</filename> will run.
For details, see
<citerefentry><refentrytitle>systemd-getty-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><varname>systemd.gpt_auto=</varname></term>
<term><varname>rd.systemd.gpt_auto=</varname></term>
@ -541,6 +551,7 @@
<citerefentry><refentrytitle>systemd-cryptsetup-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
<citerefentry><refentrytitle>systemd-veritysetup-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
<citerefentry><refentrytitle>systemd-fstab-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
<citerefentry><refentrytitle>systemd-getty-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
<citerefentry><refentrytitle>systemd-gpt-auto-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
<citerefentry><refentrytitle>systemd-volatile-root.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
<citerefentry><refentrytitle>systemd-modules-load.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>,

View File

@ -53,10 +53,43 @@
Elsewhere)</ulink>.</para>
</refsect1>
<refsect1>
<title>Kernel Command Line</title>
<para><filename>systemd-getty-generator</filename> understands the following
<citerefentry><refentrytitle>kernel-command-line</refentrytitle><manvolnum>7</manvolnum></citerefentry>
parameters:</para>
<variablelist class='kernel-commandline-options'>
<varlistentry>
<term><varname>systemd.getty_auto=</varname></term>
<listitem><para>this options take an optional boolean argument, and default to yes.
The generator is enabled by default, and a false value may be used to disable it.
</para></listitem>
</varlistentry>
</variablelist>
</refsect1>
<refsect1>
<title>Environment</title>
<variablelist class='environment-variables'>
<varlistentry>
<term><varname>$SYSTEMD_GETTY_AUTO</varname></term>
<listitem><para>This variable takes an optional boolean argument, and default to yes.
The generator is enabled by default, and a false value may be used to disable it.
</para></listitem>
</varlistentry>
</variablelist>
</refsect1>
<refsect1>
<title>See Also</title>
<para>
<citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
<citerefentry><refentrytitle>kernel-command-line</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
<citerefentry project='man-pages'><refentrytitle>agetty</refentrytitle><manvolnum>8</manvolnum></citerefentry>
</para>
</refsect1>

View File

@ -1011,3 +1011,77 @@ int parse_cifs_service(
return 0;
}
int open_mkdir_at(int dirfd, const char *path, int flags, mode_t mode) {
_cleanup_close_ int fd = -1, parent_fd = -1;
_cleanup_free_ char *fname = NULL;
bool made;
int r;
/* Creates a directory with mkdirat() and then opens it, in the "most atomic" fashion we can
* do. Guarantees that the returned fd refers to a directory. If O_EXCL is specified will fail if the
* dir already exists. Otherwise will open an existing dir, but only if it is one. */
if (flags & ~(O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_EXCL|O_NOATIME|O_NOFOLLOW|O_PATH))
return -EINVAL;
if ((flags & O_ACCMODE) != O_RDONLY)
return -EINVAL;
/* Note that O_DIRECTORY|O_NOFOLLOW is implied, but we allow specifying it anyway. The following
* flags actually make sense to specify: O_CLOEXEC, O_EXCL, O_NOATIME, O_PATH */
if (isempty(path))
return -EINVAL;
if (!filename_is_valid(path)) {
_cleanup_free_ char *parent = NULL;
/* If this is not a valid filename, it's a path. Let's open the parent directory then, so
* that we can pin it, and operate below it. */
r = path_extract_directory(path, &parent);
if (r < 0)
return r;
r = path_extract_filename(path, &fname);
if (r < 0)
return r;
parent_fd = openat(dirfd, parent, O_PATH|O_DIRECTORY|O_CLOEXEC);
if (parent_fd < 0)
return -errno;
dirfd = parent_fd;
path = fname;
}
r = RET_NERRNO(mkdirat(dirfd, path, mode));
if (r == -EEXIST) {
if (FLAGS_SET(flags, O_EXCL))
return -EEXIST;
made = false;
} else if (r < 0)
return r;
else
made = true;
fd = RET_NERRNO(openat(dirfd, path, (flags & ~O_EXCL)|O_DIRECTORY|O_NOFOLLOW));
if (fd < 0) {
if (fd == -ENOENT) /* We got ENOENT? then someone else immediately removed it after we
* created it. In that case let's return immediately without unlinking
* anything, because there simply isn't anything to unlink anymore. */
return -ENOENT;
if (fd == -ELOOP) /* is a symlink? exists already → created by someone else, don't unlink */
return -EEXIST;
if (fd == -ENOTDIR) /* not a directory? exists already → created by someone else, don't unlink */
return -EEXIST;
if (made)
(void) unlinkat(dirfd, path, AT_REMOVEDIR);
return fd;
}
return TAKE_FD(fd);
}

View File

@ -108,3 +108,5 @@ static inline int conservative_rename(const char *oldpath, const char *newpath)
int posix_fallocate_loop(int fd, uint64_t offset, uint64_t size);
int parse_cifs_service(const char *s, char **ret_host, char **ret_service, char **ret_path);
int open_mkdir_at(int dirfd, const char *path, int flags, mode_t mode);

View File

@ -2499,6 +2499,7 @@ int temporary_filesystem_add(
static int make_tmp_prefix(const char *prefix) {
_cleanup_free_ char *t = NULL;
_cleanup_close_ int fd = -1;
int r;
/* Don't do anything unless we know the dir is actually missing */
@ -2517,18 +2518,20 @@ static int make_tmp_prefix(const char *prefix) {
if (r < 0)
return r;
if (mkdir(t, 0777) < 0) /* umask will corrupt this access mode, but that doesn't matter, we need to
* call chmod() anyway for the suid bit, below. */
return -errno;
/* umask will corrupt this access mode, but that doesn't matter, we need to call chmod() anyway for
* the suid bit, below. */
fd = open_mkdir_at(AT_FDCWD, t, O_EXCL|O_CLOEXEC, 0777);
if (fd < 0)
return fd;
if (chmod(t, 01777) < 0) {
r = -errno;
r = RET_NERRNO(fchmod(fd, 01777));
if (r < 0) {
(void) rmdir(t);
return r;
}
if (rename(t, prefix) < 0) {
r = -errno;
r = RET_NERRNO(rename(t, prefix));
if (r < 0) {
(void) rmdir(t);
return r == -EEXIST ? 0 : r; /* it's fine if someone else created the dir by now */
}

View File

@ -200,7 +200,7 @@ _public_ int cryptsetup_token_validate(
}
JSON_VARIANT_ARRAY_FOREACH(e, w) {
uintmax_t u;
uint64_t u;
if (!json_variant_is_number(e)) {
crypt_log_debug(cd, "TPM2 PCR is not a number.");

View File

@ -78,7 +78,7 @@ int parse_luks2_tpm2_data(
return -EINVAL;
JSON_VARIANT_ARRAY_FOREACH(e, w) {
uintmax_t u;
uint64_t u;
if (!json_variant_is_number(e))
return -EINVAL;

View File

@ -116,7 +116,7 @@ int find_tpm2_auto_data(
assert(pcr_mask == 0);
JSON_VARIANT_ARRAY_FOREACH(e, w) {
uintmax_t u;
uint64_t u;
if (!json_variant_is_number(e))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),

View File

@ -11,8 +11,10 @@
#include "generator.h"
#include "log.h"
#include "mkdir-label.h"
#include "parse-util.h"
#include "path-util.h"
#include "process-util.h"
#include "proc-cmdline.h"
#include "strv.h"
#include "terminal-util.h"
#include "unit-name.h"
@ -20,6 +22,7 @@
#include "virt.h"
static const char *arg_dest = NULL;
static bool arg_enabled = true;
static int add_symlink(const char *fservice, const char *tservice) {
char *from, *to;
@ -139,11 +142,48 @@ static int run_container(void) {
}
}
static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
int r;
assert(key);
if (proc_cmdline_key_streq(key, "systemd.getty_auto")) {
r = value ? parse_boolean(value) : 1;
if (r < 0)
log_warning_errno(r, "Failed to parse getty_auto switch \"%s\", ignoring: %m", value);
else
arg_enabled = r;
}
return 0;
}
static int run(const char *dest, const char *dest_early, const char *dest_late) {
_cleanup_free_ char *getty_auto = NULL;
int r;
assert_se(arg_dest = dest);
r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0);
if (r < 0)
log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
r = getenv_for_pid(1, "SYSTEMD_GETTY_AUTO", &getty_auto);
if (r < 0)
log_warning_errno(r, "Failed to parse $SYSTEMD_GETTY_AUTO environment variable, ignoring: %m");
else if (r > 0) {
r = parse_boolean(getty_auto);
if (r < 0)
log_warning_errno(r, "Failed to parse $SYSTEMD_GETTY_AUTO value \"%s\", ignoring: %m", getty_auto);
else
arg_enabled = r;
}
if (!arg_enabled) {
log_debug("Disabled, exiting.");
return 0;
}
if (detect_container() > 0)
/* Add console shell and look at $container_ttys, but don't do add any
* further magic if we are in a container. */

View File

@ -837,7 +837,7 @@ static int update_last_change(JsonVariant **v, bool with_password, bool override
c = json_variant_by_key(*v, "lastChangeUSec");
if (c) {
uintmax_t u;
uint64_t u;
if (!override)
goto update_password;
@ -860,7 +860,7 @@ update_password:
c = json_variant_by_key(*v, "lastPasswordChangeUSec");
if (c) {
uintmax_t u;
uint64_t u;
if (!override)
return 0;

View File

@ -127,15 +127,17 @@ int home_setup_cifs(
return log_oom();
if (FLAGS_SET(flags, HOME_SETUP_CIFS_MKDIR)) {
r = mkdir_p(j, 0700);
if (r < 0)
return log_error_errno(r, "Failed to create CIFS subdirectory: %m");
setup->root_fd = open_mkdir_at(AT_FDCWD, j, O_CLOEXEC, 0700);
if (setup->root_fd < 0)
return log_error_errno(setup->root_fd, "Failed to create CIFS subdirectory: %m");
}
}
setup->root_fd = open(j ?: HOME_RUNTIME_WORK_DIR, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW);
if (setup->root_fd < 0)
return log_error_errno(errno, "Failed to open home directory: %m");
if (setup->root_fd < 0) {
setup->root_fd = open(j ?: HOME_RUNTIME_WORK_DIR, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW);
if (setup->root_fd < 0)
return log_error_errno(errno, "Failed to open home directory: %m");
}
setup->mount_suffix = TAKE_PTR(cdir);
return 0;

View File

@ -133,9 +133,9 @@ static int dump_link_description(char **patterns) {
return log_oom();
JSON_VARIANT_ARRAY_FOREACH(i, json_variant_by_key(v, "Interfaces")) {
char ifindex_str[DECIMAL_STR_MAX(intmax_t)];
char ifindex_str[DECIMAL_STR_MAX(int64_t)];
const char *name;
intmax_t index;
int64_t index;
size_t pos;
name = json_variant_string(json_variant_by_key(i, "Name"));

View File

@ -94,7 +94,7 @@ int link_build_json(Link *link, JsonVariant **ret) {
}
static int link_json_compare(JsonVariant * const *a, JsonVariant * const *b) {
intmax_t index_a, index_b;
int64_t index_a, index_b;
assert(a && *a);
assert(b && *b);

View File

@ -99,7 +99,7 @@ static int oci_terminal(const char *name, JsonVariant *v, JsonDispatchFlags flag
static int oci_console_dimension(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
unsigned *u = userdata;
uintmax_t k;
uint64_t k;
assert(u);
@ -226,7 +226,7 @@ static int oci_rlimit_value(const char *name, JsonVariant *v, JsonDispatchFlags
z = (rlim_t) json_variant_unsigned(v);
if ((uintmax_t) z != json_variant_unsigned(v))
if ((uint64_t) z != json_variant_unsigned(v))
return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
"rlimits limit out of range, refusing.");
}
@ -346,7 +346,7 @@ static int oci_capabilities(const char *name, JsonVariant *v, JsonDispatchFlags
static int oci_oom_score_adj(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
Settings *s = userdata;
intmax_t k;
int64_t k;
assert(s);
@ -363,14 +363,14 @@ static int oci_oom_score_adj(const char *name, JsonVariant *v, JsonDispatchFlags
static int oci_uid_gid(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
uid_t *uid = userdata, u;
uintmax_t k;
uint64_t k;
assert(uid);
assert_cc(sizeof(uid_t) == sizeof(gid_t));
k = json_variant_unsigned(v);
u = (uid_t) k;
if ((uintmax_t) u != k)
if ((uint64_t) u != k)
return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
"UID/GID out of range: %ji", k);
@ -698,7 +698,7 @@ static int oci_namespaces(const char *name, JsonVariant *v, JsonDispatchFlags fl
static int oci_uid_gid_range(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
uid_t *uid = userdata, u;
uintmax_t k;
uint64_t k;
assert(uid);
assert_cc(sizeof(uid_t) == sizeof(gid_t));
@ -709,7 +709,7 @@ static int oci_uid_gid_range(const char *name, JsonVariant *v, JsonDispatchFlags
k = json_variant_unsigned(v);
u = (uid_t) k;
if ((uintmax_t) u != k)
if ((uint64_t) u != k)
return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
"UID/GID out of range: %ji", k);
if (u == 0)
@ -803,7 +803,7 @@ static int oci_device_type(const char *name, JsonVariant *v, JsonDispatchFlags f
static int oci_device_major(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
unsigned *u = userdata;
uintmax_t k;
uint64_t k;
assert_se(u);
@ -818,7 +818,7 @@ static int oci_device_major(const char *name, JsonVariant *v, JsonDispatchFlags
static int oci_device_minor(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
unsigned *u = userdata;
uintmax_t k;
uint64_t k;
assert_se(u);
@ -833,14 +833,14 @@ static int oci_device_minor(const char *name, JsonVariant *v, JsonDispatchFlags
static int oci_device_file_mode(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
mode_t *mode = userdata, m;
uintmax_t k;
uint64_t k;
assert(mode);
k = json_variant_unsigned(v);
m = (mode_t) k;
if ((m & ~07777) != 0 || (uintmax_t) m != k)
if ((m & ~07777) != 0 || (uint64_t) m != k)
return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
"fileMode out of range, refusing.");
@ -1175,7 +1175,7 @@ static int oci_cgroup_devices(const char *name, JsonVariant *v, JsonDispatchFlag
static int oci_cgroup_memory_limit(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
uint64_t *m = userdata;
uintmax_t k;
uint64_t k;
assert(m);
@ -1275,7 +1275,7 @@ struct cpu_data {
static int oci_cgroup_cpu_shares(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
uint64_t *u = userdata;
uintmax_t k;
uint64_t k;
assert(u);
@ -1290,7 +1290,7 @@ static int oci_cgroup_cpu_shares(const char *name, JsonVariant *v, JsonDispatchF
static int oci_cgroup_cpu_quota(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
uint64_t *u = userdata;
uintmax_t k;
uint64_t k;
assert(u);
@ -1382,7 +1382,7 @@ static int oci_cgroup_cpu(const char *name, JsonVariant *v, JsonDispatchFlags fl
static int oci_cgroup_block_io_weight(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
Settings *s = userdata;
uintmax_t k;
uint64_t k;
int r;
assert(s);
@ -1414,18 +1414,18 @@ static int oci_cgroup_block_io_weight_device(const char *name, JsonVariant *v, J
struct device_data {
unsigned major;
unsigned minor;
uintmax_t weight;
uint64_t weight;
} data = {
.major = UINT_MAX,
.minor = UINT_MAX,
.weight = UINTMAX_MAX,
.weight = UINT64_MAX,
};
static const JsonDispatch table[] = {
{ "major", JSON_VARIANT_UNSIGNED, oci_device_major, offsetof(struct device_data, major), JSON_MANDATORY },
{ "minor", JSON_VARIANT_UNSIGNED, oci_device_minor, offsetof(struct device_data, minor), JSON_MANDATORY },
{ "weight", JSON_VARIANT_UNSIGNED, json_dispatch_uintmax, offsetof(struct device_data, weight), 0 },
{ "leafWeight", JSON_VARIANT_INTEGER, oci_unsupported, 0, JSON_PERMISSIVE },
{ "major", JSON_VARIANT_UNSIGNED, oci_device_major, offsetof(struct device_data, major), JSON_MANDATORY },
{ "minor", JSON_VARIANT_UNSIGNED, oci_device_minor, offsetof(struct device_data, minor), JSON_MANDATORY },
{ "weight", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(struct device_data, weight), 0 },
{ "leafWeight", JSON_VARIANT_INTEGER, oci_unsupported, 0, JSON_PERMISSIVE },
{}
};
@ -1435,7 +1435,7 @@ static int oci_cgroup_block_io_weight_device(const char *name, JsonVariant *v, J
if (r < 0)
return r;
if (data.weight == UINTMAX_MAX)
if (data.weight == UINT64_MAX)
continue;
if (data.weight < CGROUP_BLKIO_WEIGHT_MIN || data.weight > CGROUP_BLKIO_WEIGHT_MAX)
@ -1475,16 +1475,16 @@ static int oci_cgroup_block_io_throttle(const char *name, JsonVariant *v, JsonDi
struct device_data {
unsigned major;
unsigned minor;
uintmax_t rate;
uint64_t rate;
} data = {
.major = UINT_MAX,
.minor = UINT_MAX,
};
static const JsonDispatch table[] = {
{ "major", JSON_VARIANT_UNSIGNED, oci_device_major, offsetof(struct device_data, major), JSON_MANDATORY },
{ "minor", JSON_VARIANT_UNSIGNED, oci_device_minor, offsetof(struct device_data, minor), JSON_MANDATORY },
{ "rate", JSON_VARIANT_UNSIGNED, json_dispatch_uintmax, offsetof(struct device_data, rate), JSON_MANDATORY },
{ "major", JSON_VARIANT_UNSIGNED, oci_device_major, offsetof(struct device_data, major), JSON_MANDATORY },
{ "minor", JSON_VARIANT_UNSIGNED, oci_device_minor, offsetof(struct device_data, minor), JSON_MANDATORY },
{ "rate", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(struct device_data, rate), JSON_MANDATORY },
{}
};
@ -1557,7 +1557,7 @@ static int oci_cgroup_pids(const char *name, JsonVariant *v, JsonDispatchFlags f
m = (uint64_t) json_variant_unsigned(k);
if ((uintmax_t) m != json_variant_unsigned(k))
if ((uint64_t) m != json_variant_unsigned(k))
return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
"pids limit out of range, refusing.");
}
@ -2097,7 +2097,7 @@ static int oci_linux(const char *name, JsonVariant *v, JsonDispatchFlags flags,
static int oci_hook_timeout(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
usec_t *u = userdata;
uintmax_t k;
uint64_t k;
k = json_variant_unsigned(v);
if (k == 0 || k > (UINT64_MAX-1)/USEC_PER_SEC)

View File

@ -82,7 +82,7 @@ static uint32_t ifindex_to_scopeid(int family, const void *a, int ifindex) {
static int json_dispatch_ifindex(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
int *ifi = userdata;
intmax_t t;
int64_t t;
assert(variant);
assert(ifi);
@ -100,7 +100,7 @@ static int json_dispatch_ifindex(const char *name, JsonVariant *variant, JsonDis
static int json_dispatch_family(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
int *family = userdata;
intmax_t t;
int64_t t;
assert(variant);
assert(family);
@ -160,7 +160,7 @@ static int json_dispatch_address(const char *name, JsonVariant *variant, JsonDis
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is array of unexpected size.", strna(name));
JSON_VARIANT_ARRAY_FOREACH(i, variant) {
intmax_t b;
int64_t b;
if (!json_variant_is_integer(i))
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "Element %zu of JSON field '%s' is not an integer.", k, strna(name));

View File

@ -364,7 +364,7 @@ static int json_dispatch_address(const char *name, JsonVariant *variant, JsonDis
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is array of unexpected size.", strna(name));
JSON_VARIANT_ARRAY_FOREACH(i, variant) {
intmax_t b;
int64_t b;
if (!json_variant_is_integer(i))
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "Element %zu of JSON field '%s' is not an integer.", k, strna(name));

View File

@ -485,8 +485,6 @@ static int hardlink_context_setup(
}
static int hardlink_context_realize(HardlinkContext *c) {
int r;
if (!c)
return 0;
@ -498,15 +496,9 @@ static int hardlink_context_realize(HardlinkContext *c) {
assert(c->subdir);
if (mkdirat(c->parent_fd, c->subdir, 0700) < 0)
return -errno;
c->dir_fd = openat(c->parent_fd, c->subdir, O_RDONLY|O_DIRECTORY|O_CLOEXEC);
if (c->dir_fd < 0) {
r = -errno;
(void) unlinkat(c->parent_fd, c->subdir, AT_REMOVEDIR);
return r;
}
c->dir_fd = open_mkdir_at(c->parent_fd, c->subdir, O_EXCL|O_CLOEXEC, 0700);
if (c->dir_fd < 0)
return c->dir_fd;
return 1;
}

View File

@ -215,10 +215,10 @@ int get_credential_host_secret(CredentialSecretFlags flags, void **ret, size_t *
fn = "credential.secret";
}
(void) mkdir_p(p, 0755);
dfd = open(p, O_CLOEXEC|O_DIRECTORY|O_RDONLY);
mkdir_parents(p, 0755);
dfd = open_mkdir_at(AT_FDCWD, p, O_CLOEXEC, 0755);
if (dfd < 0)
return -errno;
return dfd;
if (FLAGS_SET(flags, CREDENTIAL_SECRET_FAIL_ON_TEMPORARY_FS)) {
r = fd_is_temporary_fs(dfd);

View File

@ -9,16 +9,16 @@
* interface with this. */
typedef union JsonValue {
/* Encodes a simple value. On x86-64 this structure is 16 bytes wide (as long double is 128bit). */
/* Encodes a simple value. This structure is generally 8 bytes wide (as double is 64bit). */
bool boolean;
long double real;
intmax_t integer;
uintmax_t unsig;
double real;
int64_t integer;
uint64_t unsig;
} JsonValue;
/* Let's protect us against accidental structure size changes on our most relevant arch */
#ifdef __x86_64__
assert_cc(sizeof(JsonValue) == 16U);
assert_cc(sizeof(JsonValue) == 8U);
#endif
#define JSON_VALUE_NULL ((JsonValue) {})

View File

@ -114,16 +114,16 @@ struct JsonVariant {
};
};
/* Inside string arrays we have a series of JasonVariant structures one after the other. In this case, strings longer
/* Inside string arrays we have a series of JsonVariant structures one after the other. In this case, strings longer
* than INLINE_STRING_MAX are stored as references, and all shorter ones inline. (This means on x86-64 strings up
* to 15 chars are stored within the array elements, and all others in separate allocations) */
* to 7 chars are stored within the array elements, and all others in separate allocations) */
#define INLINE_STRING_MAX (sizeof(JsonVariant) - offsetof(JsonVariant, string) - 1U)
/* Let's make sure this structure isn't increased in size accidentally. This check is only for our most relevant arch
* (x86-64). */
#ifdef __x86_64__
assert_cc(sizeof(JsonVariant) == 48U);
assert_cc(INLINE_STRING_MAX == 15U);
assert_cc(sizeof(JsonVariant) == 40U);
assert_cc(INLINE_STRING_MAX == 7U);
#endif
static JsonSource* json_source_new(const char *name) {
@ -305,7 +305,7 @@ static int json_variant_new(JsonVariant **ret, JsonVariantType type, size_t spac
return 0;
}
int json_variant_new_integer(JsonVariant **ret, intmax_t i) {
int json_variant_new_integer(JsonVariant **ret, int64_t i) {
JsonVariant *v;
int r;
@ -326,7 +326,7 @@ int json_variant_new_integer(JsonVariant **ret, intmax_t i) {
return 0;
}
int json_variant_new_unsigned(JsonVariant **ret, uintmax_t u) {
int json_variant_new_unsigned(JsonVariant **ret, uint64_t u) {
JsonVariant *v;
int r;
@ -346,7 +346,7 @@ int json_variant_new_unsigned(JsonVariant **ret, uintmax_t u) {
return 0;
}
int json_variant_new_real(JsonVariant **ret, long double d) {
int json_variant_new_real(JsonVariant **ret, double d) {
JsonVariant *v;
int r;
@ -748,13 +748,13 @@ static size_t json_variant_size(JsonVariant* v) {
return offsetof(JsonVariant, string) + strlen(v->string) + 1;
case JSON_VARIANT_REAL:
return offsetof(JsonVariant, value) + sizeof(long double);
return offsetof(JsonVariant, value) + sizeof(double);
case JSON_VARIANT_UNSIGNED:
return offsetof(JsonVariant, value) + sizeof(uintmax_t);
return offsetof(JsonVariant, value) + sizeof(uint64_t);
case JSON_VARIANT_INTEGER:
return offsetof(JsonVariant, value) + sizeof(intmax_t);
return offsetof(JsonVariant, value) + sizeof(int64_t);
case JSON_VARIANT_BOOLEAN:
return offsetof(JsonVariant, value) + sizeof(bool);
@ -895,7 +895,7 @@ mismatch:
return false;
}
intmax_t json_variant_integer(JsonVariant *v) {
int64_t json_variant_integer(JsonVariant *v) {
if (!v)
goto mismatch;
if (v == JSON_VARIANT_MAGIC_ZERO_INTEGER ||
@ -913,23 +913,23 @@ intmax_t json_variant_integer(JsonVariant *v) {
return v->value.integer;
case JSON_VARIANT_UNSIGNED:
if (v->value.unsig <= INTMAX_MAX)
return (intmax_t) v->value.unsig;
if (v->value.unsig <= INT64_MAX)
return (int64_t) v->value.unsig;
log_debug("Unsigned integer %ju requested as signed integer and out of range, returning 0.", v->value.unsig);
return 0;
case JSON_VARIANT_REAL: {
intmax_t converted;
int64_t converted;
converted = (intmax_t) v->value.real;
converted = (int64_t) v->value.real;
DISABLE_WARNING_FLOAT_EQUAL;
if ((long double) converted == v->value.real)
if ((double) converted == v->value.real)
return converted;
REENABLE_WARNING;
log_debug("Real %Lg requested as integer, and cannot be converted losslessly, returning 0.", v->value.real);
log_debug("Real %g requested as integer, and cannot be converted losslessly, returning 0.", v->value.real);
return 0;
}
@ -942,7 +942,7 @@ mismatch:
return 0;
}
uintmax_t json_variant_unsigned(JsonVariant *v) {
uint64_t json_variant_unsigned(JsonVariant *v) {
if (!v)
goto mismatch;
if (v == JSON_VARIANT_MAGIC_ZERO_INTEGER ||
@ -958,7 +958,7 @@ uintmax_t json_variant_unsigned(JsonVariant *v) {
case JSON_VARIANT_INTEGER:
if (v->value.integer >= 0)
return (uintmax_t) v->value.integer;
return (uint64_t) v->value.integer;
log_debug("Signed integer %ju requested as unsigned integer and out of range, returning 0.", v->value.integer);
return 0;
@ -967,16 +967,16 @@ uintmax_t json_variant_unsigned(JsonVariant *v) {
return v->value.unsig;
case JSON_VARIANT_REAL: {
uintmax_t converted;
uint64_t converted;
converted = (uintmax_t) v->value.real;
converted = (uint64_t) v->value.real;
DISABLE_WARNING_FLOAT_EQUAL;
if ((long double) converted == v->value.real)
if ((double) converted == v->value.real)
return converted;
REENABLE_WARNING;
log_debug("Real %Lg requested as unsigned integer, and cannot be converted losslessly, returning 0.", v->value.real);
log_debug("Real %g requested as unsigned integer, and cannot be converted losslessly, returning 0.", v->value.real);
return 0;
}
@ -989,7 +989,7 @@ mismatch:
return 0;
}
long double json_variant_real(JsonVariant *v) {
double json_variant_real(JsonVariant *v) {
if (!v)
return 0.0;
if (v == JSON_VARIANT_MAGIC_ZERO_INTEGER ||
@ -1007,11 +1007,9 @@ long double json_variant_real(JsonVariant *v) {
return v->value.real;
case JSON_VARIANT_INTEGER: {
long double converted;
double converted = (double) v->value.integer;
converted = (long double) v->value.integer;
if ((intmax_t) converted == v->value.integer)
if ((int64_t) converted == v->value.integer)
return converted;
log_debug("Signed integer %ji requested as real, and cannot be converted losslessly, returning 0.", v->value.integer);
@ -1019,11 +1017,9 @@ long double json_variant_real(JsonVariant *v) {
}
case JSON_VARIANT_UNSIGNED: {
long double converted;
double converted = (double) v->value.unsig;
converted = (long double) v->value.unsig;
if ((uintmax_t) converted == v->value.unsig)
if ((uint64_t) converted == v->value.unsig)
return converted;
log_debug("Unsigned integer %ju requested as real, and cannot be converted losslessly, returning 0.", v->value.unsig);
@ -1123,10 +1119,11 @@ JsonVariantType json_variant_type(JsonVariant *v) {
return v->type;
}
_function_no_sanitize_float_cast_overflow_ bool json_variant_has_type(JsonVariant *v, JsonVariantType type) {
_function_no_sanitize_float_cast_overflow_
bool json_variant_has_type(JsonVariant *v, JsonVariantType type) {
JsonVariantType rt;
/* Note: we turn off ubsan float cast overflo detection for this function, since it would complain
/* Note: we turn off ubsan float cast overflow detection for this function, since it would complain
* about our float casts but we do them explicitly to detect conversion errors. */
v = json_variant_dereference(v);
@ -1158,21 +1155,21 @@ _function_no_sanitize_float_cast_overflow_ bool json_variant_has_type(JsonVarian
if (rt == JSON_VARIANT_INTEGER && type == JSON_VARIANT_UNSIGNED)
return v->value.integer >= 0;
if (rt == JSON_VARIANT_UNSIGNED && type == JSON_VARIANT_INTEGER)
return v->value.unsig <= INTMAX_MAX;
return v->value.unsig <= INT64_MAX;
/* Any integer that can be converted lossley to a real and back may also be considered a real */
if (rt == JSON_VARIANT_INTEGER && type == JSON_VARIANT_REAL)
return (intmax_t) (long double) v->value.integer == v->value.integer;
return (int64_t) (double) v->value.integer == v->value.integer;
if (rt == JSON_VARIANT_UNSIGNED && type == JSON_VARIANT_REAL)
return (uintmax_t) (long double) v->value.unsig == v->value.unsig;
return (uint64_t) (double) v->value.unsig == v->value.unsig;
DISABLE_WARNING_FLOAT_EQUAL;
/* Any real that can be converted losslessly to an integer and back may also be considered an integer */
if (rt == JSON_VARIANT_REAL && type == JSON_VARIANT_INTEGER)
return (long double) (intmax_t) v->value.real == v->value.real;
return (double) (int64_t) v->value.real == v->value.real;
if (rt == JSON_VARIANT_REAL && type == JSON_VARIANT_UNSIGNED)
return (long double) (uintmax_t) v->value.real == v->value.real;
return (double) (uint64_t) v->value.real == v->value.real;
REENABLE_WARNING;
@ -1576,7 +1573,7 @@ static int json_format(FILE *f, JsonVariant *v, JsonFormatFlags flags, const cha
if (flags & JSON_FORMAT_COLOR)
fputs(ansi_highlight_blue(), f);
fprintf(f, "%.*Le", DECIMAL_DIG, json_variant_real(v));
fprintf(f, "%.*e", DECIMAL_DIG, json_variant_real(v));
if (flags & JSON_FORMAT_COLOR)
fputs(ANSI_NORMAL, f);
@ -1964,7 +1961,7 @@ int json_variant_set_field_string(JsonVariant **v, const char *field, const char
return json_variant_set_field(v, field, m);
}
int json_variant_set_field_integer(JsonVariant **v, const char *field, intmax_t i) {
int json_variant_set_field_integer(JsonVariant **v, const char *field, int64_t i) {
_cleanup_(json_variant_unrefp) JsonVariant *m = NULL;
int r;
@ -1975,7 +1972,7 @@ int json_variant_set_field_integer(JsonVariant **v, const char *field, intmax_t
return json_variant_set_field(v, field, m);
}
int json_variant_set_field_unsigned(JsonVariant **v, const char *field, uintmax_t u) {
int json_variant_set_field_unsigned(JsonVariant **v, const char *field, uint64_t u) {
_cleanup_(json_variant_unrefp) JsonVariant *m = NULL;
int r;
@ -2192,19 +2189,19 @@ static int json_variant_copy(JsonVariant **nv, JsonVariant *v) {
t = json_variant_type(v);
switch (t) {
case JSON_VARIANT_INTEGER:
k = sizeof(intmax_t);
k = sizeof(int64_t);
value.integer = json_variant_integer(v);
source = &value;
break;
case JSON_VARIANT_UNSIGNED:
k = sizeof(uintmax_t);
k = sizeof(uint64_t);
value.unsig = json_variant_unsigned(v);
source = &value;
break;
case JSON_VARIANT_REAL:
k = sizeof(long double);
k = sizeof(double);
value.real = json_variant_real(v);
source = &value;
break;
@ -2527,9 +2524,9 @@ static int json_parse_string(const char **p, char **ret) {
static int json_parse_number(const char **p, JsonValue *ret) {
bool negative = false, exponent_negative = false, is_real = false;
long double x = 0.0, y = 0.0, exponent = 0.0, shift = 1.0;
intmax_t i = 0;
uintmax_t u = 0;
double x = 0.0, y = 0.0, exponent = 0.0, shift = 1.0;
int64_t i = 0;
uint64_t u = 0;
const char *c;
assert(p);
@ -2553,23 +2550,23 @@ static int json_parse_number(const char **p, JsonValue *ret) {
if (!is_real) {
if (negative) {
if (i < INTMAX_MIN / 10) /* overflow */
if (i < INT64_MIN / 10) /* overflow */
is_real = true;
else {
intmax_t t = 10 * i;
int64_t t = 10 * i;
if (t < INTMAX_MIN + (*c - '0')) /* overflow */
if (t < INT64_MIN + (*c - '0')) /* overflow */
is_real = true;
else
i = t - (*c - '0');
}
} else {
if (u > UINTMAX_MAX / 10) /* overflow */
if (u > UINT64_MAX / 10) /* overflow */
is_real = true;
else {
uintmax_t t = 10 * u;
uint64_t t = 10 * u;
if (t > UINTMAX_MAX - (*c - '0')) /* overflow */
if (t > UINT64_MAX - (*c - '0')) /* overflow */
is_real = true;
else
u = t + (*c - '0');
@ -2619,7 +2616,7 @@ static int json_parse_number(const char **p, JsonValue *ret) {
*p = c;
if (is_real) {
ret->real = ((negative ? -1.0 : 1.0) * (x + (y / shift))) * exp10l((exponent_negative ? -1.0 : 1.0) * exponent);
ret->real = ((negative ? -1.0 : 1.0) * (x + (y / shift))) * exp10((exponent_negative ? -1.0 : 1.0) * exponent);
return JSON_TOKEN_REAL;
} else if (negative) {
ret->integer = i;
@ -3291,14 +3288,14 @@ int json_buildv(JsonVariant **ret, va_list ap) {
}
case _JSON_BUILD_INTEGER: {
intmax_t j;
int64_t j;
if (!IN_SET(current->expect, EXPECT_TOPLEVEL, EXPECT_OBJECT_VALUE, EXPECT_ARRAY_ELEMENT)) {
r = -EINVAL;
goto finish;
}
j = va_arg(ap, intmax_t);
j = va_arg(ap, int64_t);
if (current->n_suppress == 0) {
r = json_variant_new_integer(&add, j);
@ -3319,14 +3316,14 @@ int json_buildv(JsonVariant **ret, va_list ap) {
}
case _JSON_BUILD_UNSIGNED: {
uintmax_t j;
uint64_t j;
if (!IN_SET(current->expect, EXPECT_TOPLEVEL, EXPECT_OBJECT_VALUE, EXPECT_ARRAY_ELEMENT)) {
r = -EINVAL;
goto finish;
}
j = va_arg(ap, uintmax_t);
j = va_arg(ap, uint64_t);
if (current->n_suppress == 0) {
r = json_variant_new_unsigned(&add, j);
@ -3347,14 +3344,14 @@ int json_buildv(JsonVariant **ret, va_list ap) {
}
case _JSON_BUILD_REAL: {
long double d;
double d;
if (!IN_SET(current->expect, EXPECT_TOPLEVEL, EXPECT_OBJECT_VALUE, EXPECT_ARRAY_ELEMENT)) {
r = -EINVAL;
goto finish;
}
d = va_arg(ap, long double);
d = va_arg(ap, double);
if (current->n_suppress == 0) {
r = json_variant_new_real(&add, d);
@ -4067,8 +4064,8 @@ int json_dispatch_tristate(const char *name, JsonVariant *variant, JsonDispatchF
return 0;
}
int json_dispatch_intmax(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
intmax_t *i = userdata;
int json_dispatch_int64(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
int64_t *i = userdata;
assert(variant);
assert(i);
@ -4080,8 +4077,8 @@ int json_dispatch_intmax(const char *name, JsonVariant *variant, JsonDispatchFla
return 0;
}
int json_dispatch_uintmax(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
uintmax_t *u = userdata;
int json_dispatch_uint64(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
uint64_t *u = userdata;
assert(variant);
assert(u);
@ -4231,7 +4228,7 @@ int json_dispatch_variant(const char *name, JsonVariant *variant, JsonDispatchFl
int json_dispatch_uid_gid(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
uid_t *uid = userdata;
uintmax_t k;
uint64_t k;
assert_cc(sizeof(uid_t) == sizeof(uint32_t));
assert_cc(sizeof(gid_t) == sizeof(uint32_t));

View File

@ -29,10 +29,10 @@
Limitations:
- Doesn't allow embedded NUL in strings
- Can't store integers outside of the -922337203685477580818446744073709551615 range (it will use 'long double' for
- Can't store integers outside of the -922337203685477580818446744073709551615 range (it will use 'double' for
values outside this range, which is lossy)
- Can't store negative zero (will be treated identical to positive zero, and not retained across serialization)
- Can't store non-integer numbers that can't be stored in "long double" losslessly
- Can't store non-integer numbers that can't be stored in "double" losslessly
- Allows creation and parsing of objects with duplicate keys. The "dispatcher" will refuse them however. This means
we can parse and pass around such objects, but will carefully refuse them when we convert them into our own data.
@ -59,9 +59,9 @@ typedef enum JsonVariantType {
int json_variant_new_stringn(JsonVariant **ret, const char *s, size_t n);
int json_variant_new_base64(JsonVariant **ret, const void *p, size_t n);
int json_variant_new_hex(JsonVariant **ret, const void *p, size_t n);
int json_variant_new_integer(JsonVariant **ret, intmax_t i);
int json_variant_new_unsigned(JsonVariant **ret, uintmax_t u);
int json_variant_new_real(JsonVariant **ret, long double d);
int json_variant_new_integer(JsonVariant **ret, int64_t i);
int json_variant_new_unsigned(JsonVariant **ret, uint64_t u);
int json_variant_new_real(JsonVariant **ret, double d);
int json_variant_new_boolean(JsonVariant **ret, bool b);
int json_variant_new_array(JsonVariant **ret, JsonVariant **array, size_t n);
int json_variant_new_array_bytes(JsonVariant **ret, const void *p, size_t n);
@ -81,9 +81,9 @@ void json_variant_unref_many(JsonVariant **array, size_t n);
DEFINE_TRIVIAL_CLEANUP_FUNC(JsonVariant *, json_variant_unref);
const char *json_variant_string(JsonVariant *v);
intmax_t json_variant_integer(JsonVariant *v);
uintmax_t json_variant_unsigned(JsonVariant *v);
long double json_variant_real(JsonVariant *v);
int64_t json_variant_integer(JsonVariant *v);
uint64_t json_variant_unsigned(JsonVariant *v);
double json_variant_real(JsonVariant *v);
bool json_variant_boolean(JsonVariant *v);
JsonVariantType json_variant_type(JsonVariant *v);
@ -189,8 +189,8 @@ int json_variant_filter(JsonVariant **v, char **to_remove);
int json_variant_set_field(JsonVariant **v, const char *field, JsonVariant *value);
int json_variant_set_field_string(JsonVariant **v, const char *field, const char *value);
int json_variant_set_field_integer(JsonVariant **v, const char *field, intmax_t value);
int json_variant_set_field_unsigned(JsonVariant **v, const char *field, uintmax_t value);
int json_variant_set_field_integer(JsonVariant **v, const char *field, int64_t value);
int json_variant_set_field_unsigned(JsonVariant **v, const char *field, uint64_t value);
int json_variant_set_field_boolean(JsonVariant **v, const char *field, bool b);
int json_variant_set_field_strv(JsonVariant **v, const char *field, char **l);
@ -240,9 +240,9 @@ enum {
};
#define JSON_BUILD_STRING(s) _JSON_BUILD_STRING, (const char*) { s }
#define JSON_BUILD_INTEGER(i) _JSON_BUILD_INTEGER, (intmax_t) { i }
#define JSON_BUILD_UNSIGNED(u) _JSON_BUILD_UNSIGNED, (uintmax_t) { u }
#define JSON_BUILD_REAL(d) _JSON_BUILD_REAL, (long double) { d }
#define JSON_BUILD_INTEGER(i) _JSON_BUILD_INTEGER, (int64_t) { i }
#define JSON_BUILD_UNSIGNED(u) _JSON_BUILD_UNSIGNED, (uint64_t) { u }
#define JSON_BUILD_REAL(d) _JSON_BUILD_REAL, (double) { d }
#define JSON_BUILD_BOOLEAN(b) _JSON_BUILD_BOOLEAN, (bool) { b }
#define JSON_BUILD_ARRAY(...) _JSON_BUILD_ARRAY_BEGIN, __VA_ARGS__, _JSON_BUILD_ARRAY_END
#define JSON_BUILD_EMPTY_ARRAY _JSON_BUILD_ARRAY_BEGIN, _JSON_BUILD_ARRAY_END
@ -297,8 +297,8 @@ int json_dispatch_strv(const char *name, JsonVariant *variant, JsonDispatchFlags
int json_dispatch_boolean(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata);
int json_dispatch_tristate(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata);
int json_dispatch_variant(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata);
int json_dispatch_intmax(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata);
int json_dispatch_uintmax(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata);
int json_dispatch_int64(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata);
int json_dispatch_uint64(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata);
int json_dispatch_uint32(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata);
int json_dispatch_int32(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata);
int json_dispatch_uid_gid(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata);
@ -306,12 +306,6 @@ int json_dispatch_user_group_name(const char *name, JsonVariant *variant, JsonDi
int json_dispatch_id128(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata);
int json_dispatch_unsupported(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata);
assert_cc(sizeof(uintmax_t) == sizeof(uint64_t));
#define json_dispatch_uint64 json_dispatch_uintmax
assert_cc(sizeof(intmax_t) == sizeof(int64_t));
#define json_dispatch_int64 json_dispatch_intmax
assert_cc(sizeof(uint32_t) == sizeof(unsigned));
#define json_dispatch_uint json_dispatch_uint32

View File

@ -252,7 +252,7 @@ int json_dispatch_gecos(const char *name, JsonVariant *variant, JsonDispatchFlag
static int json_dispatch_nice(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
int *nl = userdata;
intmax_t m;
int64_t m;
if (json_variant_is_null(variant)) {
*nl = INT_MAX;
@ -276,10 +276,10 @@ static int json_dispatch_rlimit_value(const char *name, JsonVariant *variant, Js
if (json_variant_is_null(variant))
*ret = RLIM_INFINITY;
else if (json_variant_is_unsigned(variant)) {
uintmax_t w;
uint64_t w;
w = json_variant_unsigned(variant);
if (w == RLIM_INFINITY || (uintmax_t) w != json_variant_unsigned(variant))
if (w == RLIM_INFINITY || (uint64_t) w != json_variant_unsigned(variant))
return json_log(variant, flags, SYNTHETIC_ERRNO(ERANGE), "Resource limit value '%s' is out of range.", name);
*ret = (rlim_t) w;
@ -453,7 +453,7 @@ static int json_dispatch_image_path(const char *name, JsonVariant *variant, Json
static int json_dispatch_umask(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
mode_t *m = userdata;
uintmax_t k;
uint64_t k;
if (json_variant_is_null(variant)) {
*m = MODE_INVALID;
@ -473,7 +473,7 @@ static int json_dispatch_umask(const char *name, JsonVariant *variant, JsonDispa
static int json_dispatch_access_mode(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
mode_t *m = userdata;
uintmax_t k;
uint64_t k;
if (json_variant_is_null(variant)) {
*m = MODE_INVALID;
@ -564,8 +564,7 @@ static int json_dispatch_storage(const char *name, JsonVariant *variant, JsonDis
}
static int json_dispatch_tasks_or_memory_max(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
uint64_t *limit = userdata;
uintmax_t k;
uint64_t *limit = userdata, k;
if (json_variant_is_null(variant)) {
*limit = UINT64_MAX;
@ -584,8 +583,7 @@ static int json_dispatch_tasks_or_memory_max(const char *name, JsonVariant *vari
}
static int json_dispatch_weight(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
uint64_t *weight = userdata;
uintmax_t k;
uint64_t *weight = userdata, k;
if (json_variant_is_null(variant)) {
*weight = UINT64_MAX;

View File

@ -952,6 +952,49 @@ static void test_parse_cifs_service(void) {
test_parse_cifs_service_one("//./a", NULL, NULL, NULL, -EINVAL);
}
static void test_open_mkdir_at(void) {
_cleanup_close_ int fd = -1, subdir_fd = -1, subsubdir_fd = -1;
_cleanup_(rm_rf_physical_and_freep) char *t = NULL;
log_info("/* %s */", __func__);
assert_se(open_mkdir_at(AT_FDCWD, "/proc", O_EXCL|O_CLOEXEC, 0) == -EEXIST);
fd = open_mkdir_at(AT_FDCWD, "/proc", O_CLOEXEC, 0);
assert_se(fd >= 0);
fd = safe_close(fd);
assert_se(open_mkdir_at(AT_FDCWD, "/bin/sh", O_EXCL|O_CLOEXEC, 0) == -EEXIST);
assert_se(open_mkdir_at(AT_FDCWD, "/bin/sh", O_CLOEXEC, 0) == -EEXIST);
assert_se(mkdtemp_malloc(NULL, &t) >= 0);
assert_se(open_mkdir_at(AT_FDCWD, t, O_EXCL|O_CLOEXEC, 0) == -EEXIST);
assert_se(open_mkdir_at(AT_FDCWD, t, O_PATH|O_EXCL|O_CLOEXEC, 0) == -EEXIST);
fd = open_mkdir_at(AT_FDCWD, t, O_CLOEXEC, 0000);
assert_se(fd >= 0);
fd = safe_close(fd);
fd = open_mkdir_at(AT_FDCWD, t, O_PATH|O_CLOEXEC, 0000);
assert_se(fd >= 0);
subdir_fd = open_mkdir_at(fd, "xxx", O_PATH|O_EXCL|O_CLOEXEC, 0700);
assert_se(subdir_fd >= 0);
assert_se(open_mkdir_at(fd, "xxx", O_PATH|O_EXCL|O_CLOEXEC, 0) == -EEXIST);
subsubdir_fd = open_mkdir_at(subdir_fd, "yyy", O_EXCL|O_CLOEXEC, 0700);
assert_se(subsubdir_fd >= 0);
subsubdir_fd = safe_close(subsubdir_fd);
assert_se(open_mkdir_at(subdir_fd, "yyy", O_EXCL|O_CLOEXEC, 0) == -EEXIST);
assert_se(open_mkdir_at(fd, "xxx/yyy", O_EXCL|O_CLOEXEC, 0) == -EEXIST);
subsubdir_fd = open_mkdir_at(fd, "xxx/yyy", O_CLOEXEC, 0700);
assert_se(subsubdir_fd >= 0);
}
int main(int argc, char *argv[]) {
test_setup_logging(LOG_INFO);
@ -972,6 +1015,7 @@ int main(int argc, char *argv[]) {
test_conservative_rename();
test_rmdir_parents();
test_parse_cifs_service();
test_open_mkdir_at();
return 0;
}

View File

@ -46,28 +46,23 @@ static void test_tokenizer(const char *data, ...) {
assert_se(streq_ptr(nn, str));
} else if (t == JSON_TOKEN_REAL) {
long double d;
double d;
d = va_arg(ap, long double);
/* Valgrind doesn't support long double calculations and automatically downgrades to 80bit:
* http://www.valgrind.org/docs/manual/manual-core.html#manual-core.limits.
* Some architectures might not support long double either.
*/
d = va_arg(ap, double);
assert_se(fabsl(d - v.real) < 1e-10 ||
fabsl((d - v.real) / v.real) < 1e-10);
} else if (t == JSON_TOKEN_INTEGER) {
intmax_t i;
int64_t i;
i = va_arg(ap, intmax_t);
i = va_arg(ap, int64_t);
assert_se(i == v.integer);
} else if (t == JSON_TOKEN_UNSIGNED) {
uintmax_t u;
uint64_t u;
u = va_arg(ap, uintmax_t);
u = va_arg(ap, uint64_t);
assert_se(u == v.unsig);
} else if (t == JSON_TOKEN_BOOLEAN) {
@ -523,17 +518,17 @@ static void test_bisect(void) {
}
static void test_float_match(JsonVariant *v) {
const long double delta = 0.0001;
const double delta = 0.0001;
assert_se(json_variant_is_array(v));
assert_se(json_variant_elements(v) == 9);
assert_se(fabsl((long double) 1.0 - ((long double) DBL_MIN / json_variant_real(json_variant_by_index(v, 0)))) <= delta);
assert_se(fabsl((long double) 1.0 - ((long double) DBL_MAX / json_variant_real(json_variant_by_index(v, 1)))) <= delta);
assert_se(fabsl((double) 1.0 - ((double) DBL_MIN / json_variant_real(json_variant_by_index(v, 0)))) <= delta);
assert_se(fabsl((double) 1.0 - ((double) DBL_MAX / json_variant_real(json_variant_by_index(v, 1)))) <= delta);
assert_se(json_variant_is_null(json_variant_by_index(v, 2))); /* nan is not supported by json → null */
assert_se(json_variant_is_null(json_variant_by_index(v, 3))); /* +inf is not supported by json → null */
assert_se(json_variant_is_null(json_variant_by_index(v, 4))); /* -inf is not supported by json → null */
assert_se(json_variant_is_null(json_variant_by_index(v, 5)) ||
fabsl((long double) 1.0 - ((long double) HUGE_VAL / json_variant_real(json_variant_by_index(v, 5)))) <= delta); /* HUGE_VAL might be +inf, but might also be something else */
fabsl((double) 1.0 - ((double) HUGE_VAL / json_variant_real(json_variant_by_index(v, 5)))) <= delta); /* HUGE_VAL might be +inf, but might also be something else */
assert_se(json_variant_is_real(json_variant_by_index(v, 6)) &&
json_variant_is_integer(json_variant_by_index(v, 6)) &&
json_variant_integer(json_variant_by_index(v, 6)) == 0);
@ -580,19 +575,19 @@ int main(int argc, char *argv[]) {
test_tokenizer("x", -EINVAL);
test_tokenizer("", JSON_TOKEN_END);
test_tokenizer(" ", JSON_TOKEN_END);
test_tokenizer("0", JSON_TOKEN_UNSIGNED, (uintmax_t) 0, JSON_TOKEN_END);
test_tokenizer("-0", JSON_TOKEN_INTEGER, (intmax_t) 0, JSON_TOKEN_END);
test_tokenizer("1234", JSON_TOKEN_UNSIGNED, (uintmax_t) 1234, JSON_TOKEN_END);
test_tokenizer("-1234", JSON_TOKEN_INTEGER, (intmax_t) -1234, JSON_TOKEN_END);
test_tokenizer("18446744073709551615", JSON_TOKEN_UNSIGNED, (uintmax_t) UINT64_MAX, JSON_TOKEN_END);
test_tokenizer("-9223372036854775808", JSON_TOKEN_INTEGER, (intmax_t) INT64_MIN, JSON_TOKEN_END);
test_tokenizer("18446744073709551616", JSON_TOKEN_REAL, (long double) 18446744073709551616.0L, JSON_TOKEN_END);
test_tokenizer("-9223372036854775809", JSON_TOKEN_REAL, (long double) -9223372036854775809.0L, JSON_TOKEN_END);
test_tokenizer("-1234", JSON_TOKEN_INTEGER, (intmax_t) -1234, JSON_TOKEN_END);
test_tokenizer("3.141", JSON_TOKEN_REAL, (long double) 3.141, JSON_TOKEN_END);
test_tokenizer("0.0", JSON_TOKEN_REAL, (long double) 0.0, JSON_TOKEN_END);
test_tokenizer("7e3", JSON_TOKEN_REAL, (long double) 7e3, JSON_TOKEN_END);
test_tokenizer("-7e-3", JSON_TOKEN_REAL, (long double) -7e-3, JSON_TOKEN_END);
test_tokenizer("0", JSON_TOKEN_UNSIGNED, (uint64_t) 0, JSON_TOKEN_END);
test_tokenizer("-0", JSON_TOKEN_INTEGER, (int64_t) 0, JSON_TOKEN_END);
test_tokenizer("1234", JSON_TOKEN_UNSIGNED, (uint64_t) 1234, JSON_TOKEN_END);
test_tokenizer("-1234", JSON_TOKEN_INTEGER, (int64_t) -1234, JSON_TOKEN_END);
test_tokenizer("18446744073709551615", JSON_TOKEN_UNSIGNED, (uint64_t) UINT64_MAX, JSON_TOKEN_END);
test_tokenizer("-9223372036854775808", JSON_TOKEN_INTEGER, (int64_t) INT64_MIN, JSON_TOKEN_END);
test_tokenizer("18446744073709551616", JSON_TOKEN_REAL, (double) 18446744073709551616.0L, JSON_TOKEN_END);
test_tokenizer("-9223372036854775809", JSON_TOKEN_REAL, (double) -9223372036854775809.0L, JSON_TOKEN_END);
test_tokenizer("-1234", JSON_TOKEN_INTEGER, (int64_t) -1234, JSON_TOKEN_END);
test_tokenizer("3.141", JSON_TOKEN_REAL, (double) 3.141, JSON_TOKEN_END);
test_tokenizer("0.0", JSON_TOKEN_REAL, (double) 0.0, JSON_TOKEN_END);
test_tokenizer("7e3", JSON_TOKEN_REAL, (double) 7e3, JSON_TOKEN_END);
test_tokenizer("-7e-3", JSON_TOKEN_REAL, (double) -7e-3, JSON_TOKEN_END);
test_tokenizer("true", JSON_TOKEN_BOOLEAN, true, JSON_TOKEN_END);
test_tokenizer("false", JSON_TOKEN_BOOLEAN, false, JSON_TOKEN_END);
test_tokenizer("null", JSON_TOKEN_NULL, JSON_TOKEN_END);
@ -612,7 +607,7 @@ int main(int argc, char *argv[]) {
test_tokenizer("\"\\udc00\\udc00\"", -EINVAL);
test_tokenizer("\"\\ud801\\udc37\"", JSON_TOKEN_STRING, "\xf0\x90\x90\xb7", JSON_TOKEN_END);
test_tokenizer("[1, 2, -3]", JSON_TOKEN_ARRAY_OPEN, JSON_TOKEN_UNSIGNED, (uintmax_t) 1, JSON_TOKEN_COMMA, JSON_TOKEN_UNSIGNED, (uintmax_t) 2, JSON_TOKEN_COMMA, JSON_TOKEN_INTEGER, (intmax_t) -3, JSON_TOKEN_ARRAY_CLOSE, JSON_TOKEN_END);
test_tokenizer("[1, 2, -3]", JSON_TOKEN_ARRAY_OPEN, JSON_TOKEN_UNSIGNED, (uint64_t) 1, JSON_TOKEN_COMMA, JSON_TOKEN_UNSIGNED, (uint64_t) 2, JSON_TOKEN_COMMA, JSON_TOKEN_INTEGER, (int64_t) -3, JSON_TOKEN_ARRAY_CLOSE, JSON_TOKEN_END);
test_variant("{\"k\": \"v\", \"foo\": [1, 2, 3], \"bar\": {\"zap\": null}}", test_1);
test_variant("{\"mutant\": [1, null, \"1\", {\"1\": [1, \"1\"]}], \"thisisaverylongproperty\": 1.27}", test_2);

View File

@ -57,6 +57,8 @@ int main(void) {
info(long long unsigned);
info(__syscall_ulong_t);
info(__syscall_slong_t);
info(intmax_t);
info(uintmax_t);
info(float);
info(double);

View File

@ -26,7 +26,7 @@ static int block_write_fd = -1;
static int method_something(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) {
_cleanup_(json_variant_unrefp) JsonVariant *ret = NULL;
JsonVariant *a, *b;
intmax_t x, y;
int64_t x, y;
int r;
a = json_variant_by_key(parameters, "a");